Skip to content

Commit 9c99f9a

Browse files
committed
includecheck for colors defined by other css custom property relations
1 parent fcdc5d9 commit 9c99f9a

1 file changed

Lines changed: 45 additions & 7 deletions

File tree

src/common/utils/getColorConfiguration.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,64 @@
1+
import Color from "color";
2+
13
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
24

35
import CssCustomProperties from "./CssCustomProperties";
46

57
// Configurations can be found in `src/cmem/react-flow/configuration/_colors-*.scss`
68
type colorconfigs = "react-flow-graph" | "react-flow-linking" | "react-flow-workflow" | "stickynotes";
79

8-
const colorConfigurationMemo = new Map<colorconfigs, any>();
10+
const colorConfigurationMemo = new Map<colorconfigs, Record<string, string>>();
911

1012
/**
1113
* Read and returns color values provided by CSS custom properties.
1214
* They are defined for special CSS classes.
1315
* Currently color configurations for the react flow editors are supported.
1416
**/
15-
const getColorConfiguration = (configId: colorconfigs) => {
17+
const getColorConfiguration = (configId: colorconfigs): Record<string, string> => {
1618
if (!colorConfigurationMemo.has(configId)) {
19+
const selectorClass = `${eccgui}-configuration--colors__${configId}`;
1720
colorConfigurationMemo.set(
1821
configId,
19-
new CssCustomProperties({
20-
selectorText: `.${eccgui}-configuration--colors__${configId}`,
21-
removeDashPrefix: true,
22-
returnObject: true,
23-
}).customProperties()
22+
Object.fromEntries(
23+
(
24+
new CssCustomProperties({
25+
selectorText: `.${selectorClass}`,
26+
removeDashPrefix: true,
27+
returnObject: false,
28+
}).customProperties() as string[][]
29+
).map((setting) => {
30+
// check if the value could be a color
31+
32+
let testColorValue = setting[1];
33+
// check if value itself is a reference to another css custom property
34+
if (testColorValue.slice(0, 3) === "var") {
35+
// we currently only extract the first part and ignore any fallbacks
36+
const customPropertyName = /var\(\s*(--[a-zA-Z0-9_-]+)/g.exec(testColorValue);
37+
if (customPropertyName && customPropertyName[1]) {
38+
let selectorElement = document.getElementsByClassName(selectorClass)[0];
39+
if (!selectorElement) {
40+
// we need to add an empty element that the JS API can read the value of the custom prop
41+
selectorElement = document.createElement("div");
42+
selectorElement.classList.add(selectorClass);
43+
selectorElement.setAttribute("style", "display: none");
44+
document.body.appendChild(selectorElement);
45+
}
46+
// only check 1 time, not recursive
47+
testColorValue = getComputedStyle(selectorElement).getPropertyValue(customPropertyName[1]);
48+
}
49+
}
50+
51+
try {
52+
if (Color(testColorValue)) {
53+
return [setting[0], testColorValue];
54+
} else {
55+
return [setting[0], undefined];
56+
}
57+
} catch {
58+
return [setting[0], undefined];
59+
}
60+
})
61+
) as Record<string, string>
2462
);
2563
}
2664
return colorConfigurationMemo.get(configId)!;

0 commit comments

Comments
 (0)