|
| 1 | +import Color from "color"; |
| 2 | + |
1 | 3 | import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
2 | 4 |
|
3 | 5 | import CssCustomProperties from "./CssCustomProperties"; |
4 | 6 |
|
5 | 7 | // Configurations can be found in `src/cmem/react-flow/configuration/_colors-*.scss` |
6 | 8 | type colorconfigs = "react-flow-graph" | "react-flow-linking" | "react-flow-workflow" | "stickynotes"; |
7 | 9 |
|
8 | | -const colorConfigurationMemo = new Map<colorconfigs, any>(); |
| 10 | +const colorConfigurationMemo = new Map<colorconfigs, Record<string, string>>(); |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * Read and returns color values provided by CSS custom properties. |
12 | 14 | * They are defined for special CSS classes. |
13 | 15 | * Currently color configurations for the react flow editors are supported. |
14 | 16 | **/ |
15 | | -const getColorConfiguration = (configId: colorconfigs) => { |
| 17 | +const getColorConfiguration = (configId: colorconfigs): Record<string, string> => { |
16 | 18 | if (!colorConfigurationMemo.has(configId)) { |
| 19 | + const selectorClass = `${eccgui}-configuration--colors__${configId}`; |
17 | 20 | colorConfigurationMemo.set( |
18 | 21 | 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> |
24 | 62 | ); |
25 | 63 | } |
26 | 64 | return colorConfigurationMemo.get(configId)!; |
|
0 commit comments