-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetColorConfiguration.ts
More file actions
48 lines (40 loc) · 1.87 KB
/
Copy pathgetColorConfiguration.ts
File metadata and controls
48 lines (40 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Color from "color";
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
import CssCustomProperties from "./CssCustomProperties";
// Configurations can be found in `src/cmem/react-flow/configuration/_colors-*.scss`
type colorconfigs = "react-flow-graph" | "react-flow-linking" | "react-flow-workflow" | "stickynotes";
const colorConfigurationMemo = new Map<colorconfigs, Record<string, string>>();
/**
* Read and returns color values provided by CSS custom properties.
* They are defined for special CSS classes.
* Currently color configurations for the react flow editors are supported.
**/
const getColorConfiguration = (configId: colorconfigs): Record<string, string> => {
if (!colorConfigurationMemo.has(configId)) {
const selectorClass = `${eccgui}-configuration--colors__${configId}`;
const colorConfiguration = Object.fromEntries(
(
new CssCustomProperties({
selectorText: `.${selectorClass}`,
removeDashPrefix: true,
returnObject: false,
}).customProperties() as string[][]
).map((setting) => {
// check if the value could be a color, references to other custom properties are already resolved
try {
Color(setting[1]);
return [setting[0], setting[1]];
} catch {
return [setting[0], undefined];
}
}),
) as Record<string, string>;
if (Object.keys(colorConfiguration).length === 0) {
// an empty result is not cached, the stylesheets may be loaded later on
return colorConfiguration;
}
colorConfigurationMemo.set(configId, colorConfiguration);
}
return colorConfigurationMemo.get(configId)!;
};
export default getColorConfiguration;