|
@@ -0,0 +1,160 @@
|
|
1
|
+// @flow
|
|
2
|
+
|
|
3
|
+import { toState } from '../redux';
|
|
4
|
+import { StyleType } from '../styles';
|
|
5
|
+
|
|
6
|
+import defaultScheme from './defaultScheme';
|
|
7
|
+
|
|
8
|
+/**
|
|
9
|
+ * A registry class to register styles that need to be color-schemed.
|
|
10
|
+ *
|
|
11
|
+ * This class uses lazy initialization for scheme-ified style definitions on
|
|
12
|
+ * request.
|
|
13
|
+ */
|
|
14
|
+class ColorSchemeRegistry {
|
|
15
|
+ /**
|
|
16
|
+ * A map of already scheme-ified style definitions.
|
|
17
|
+ */
|
|
18
|
+ _schemedStyles = new Map();
|
|
19
|
+
|
|
20
|
+ /**
|
|
21
|
+ * A map of registered style templates.
|
|
22
|
+ */
|
|
23
|
+ _styleTemplates = new Map();
|
|
24
|
+
|
|
25
|
+ /**
|
|
26
|
+ * Clears the already scheme-ified style definitions. This is useful when
|
|
27
|
+ * the {@code SET_COLOR_SCHEME} action is dispatched (again).
|
|
28
|
+ *
|
|
29
|
+ * @returns {void}
|
|
30
|
+ */
|
|
31
|
+ clear() {
|
|
32
|
+ this._schemedStyles.clear();
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ /**
|
|
36
|
+ * Retreives the color-scheme applied style definition of a component.
|
|
37
|
+ *
|
|
38
|
+ * @param {Object | Function} stateful - An object or function that can be
|
|
39
|
+ * resolved to Redux state using the {@code toState} function.
|
|
40
|
+ * @param {string} componentName - The name of the component whose style we
|
|
41
|
+ * want to retreive.
|
|
42
|
+ * @returns {StyleType}
|
|
43
|
+ */
|
|
44
|
+ get(stateful: Object | Function, componentName: string): StyleType {
|
|
45
|
+ let schemedStyle = this._schemedStyles.get(componentName);
|
|
46
|
+
|
|
47
|
+ if (!schemedStyle) {
|
|
48
|
+ schemedStyle
|
|
49
|
+ = this._applyColorScheme(
|
|
50
|
+ stateful,
|
|
51
|
+ componentName,
|
|
52
|
+ this._styleTemplates.get(componentName));
|
|
53
|
+ this._schemedStyles.set(componentName, schemedStyle);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ return schemedStyle;
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ /**
|
|
60
|
+ * Registers a style definition to the registry for color-scheming.
|
|
61
|
+ *
|
|
62
|
+ * NOTE: It's suggested to only use this registry on styles where color
|
|
63
|
+ * scheming is needed, otherwise just use a static style object as before.
|
|
64
|
+ *
|
|
65
|
+ * @param {string} componentName - The name of the component to register the
|
|
66
|
+ * style to (e.g. {@code 'Toolbox'}).
|
|
67
|
+ * @param {StyleType} style - The style definition to register.
|
|
68
|
+ * @returns {void}
|
|
69
|
+ */
|
|
70
|
+ register(componentName: string, style: StyleType): void {
|
|
71
|
+ this._styleTemplates.set(componentName, style);
|
|
72
|
+
|
|
73
|
+ // If this is a style overwrite, we need to delete the processed version
|
|
74
|
+ // of the style from the other map
|
|
75
|
+ this._schemedStyles.delete(componentName);
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ /**
|
|
79
|
+ * Creates a color schemed style object applying the color scheme to every
|
|
80
|
+ * colors in the style object prepared in a special way.
|
|
81
|
+ *
|
|
82
|
+ * @param {Object | Function} stateful - An object or function that can be
|
|
83
|
+ * resolved to Redux state using the {@code toState} function.
|
|
84
|
+ * @param {string} componentName - The name of the component to apply the
|
|
85
|
+ * color scheme to.
|
|
86
|
+ * @param {StyleType} style - The style definition to apply the color scheme
|
|
87
|
+ * to.
|
|
88
|
+ * @returns {StyleType}
|
|
89
|
+ */
|
|
90
|
+ _applyColorScheme(
|
|
91
|
+ stateful: Object | Function,
|
|
92
|
+ componentName: string,
|
|
93
|
+ style: StyleType): StyleType {
|
|
94
|
+ let schemedStyle;
|
|
95
|
+
|
|
96
|
+ if (Array.isArray(style)) {
|
|
97
|
+ // The style is an array of styles, we apply the same transformation
|
|
98
|
+ // to each, recursively.
|
|
99
|
+ schemedStyle = [];
|
|
100
|
+
|
|
101
|
+ for (const entry of style) {
|
|
102
|
+ schemedStyle.push(this._applyColorScheme(
|
|
103
|
+ stateful, componentName, entry));
|
|
104
|
+ }
|
|
105
|
+ } else {
|
|
106
|
+ // The style is an object, we create a copy of it to avoid in-place
|
|
107
|
+ // modification.
|
|
108
|
+ schemedStyle = {
|
|
109
|
+ ...style
|
|
110
|
+ };
|
|
111
|
+
|
|
112
|
+ for (const [
|
|
113
|
+ styleName,
|
|
114
|
+ styleValue
|
|
115
|
+ ] of Object.entries(schemedStyle)) {
|
|
116
|
+ if (typeof styleValue === 'object') {
|
|
117
|
+ // The value is another style object, we apply the same
|
|
118
|
+ // transformation recusively.
|
|
119
|
+ schemedStyle[styleName]
|
|
120
|
+ = this._applyColorScheme(
|
|
121
|
+ stateful, componentName, styleValue);
|
|
122
|
+ } else if (typeof styleValue === 'function') {
|
|
123
|
+ // The value is a function, which indicates that it's a
|
|
124
|
+ // dynamic, schemed color we need to resolve.
|
|
125
|
+ schemedStyle[styleName]
|
|
126
|
+ = this._getColor(stateful, componentName, styleValue());
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ }
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ return schemedStyle;
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ /**
|
|
136
|
+ * Function to get the color value for the provided identifier.
|
|
137
|
+ *
|
|
138
|
+ * @param {Object | Function} stateful - An object or function that can be
|
|
139
|
+ * resolved to Redux state using the {@code toState} function.
|
|
140
|
+ * @param {string} componentName - The name of the component to get the
|
|
141
|
+ * color value for.
|
|
142
|
+ * @param {string} colorDefinition - The string identifier of the color,
|
|
143
|
+ * e.g. {@code appBackground}.
|
|
144
|
+ * @returns {string}
|
|
145
|
+ */
|
|
146
|
+ _getColor(
|
|
147
|
+ stateful: Object | Function,
|
|
148
|
+ componentName: string,
|
|
149
|
+ colorDefinition: string): string {
|
|
150
|
+ const colorScheme = toState(stateful)['features/base/color-scheme'];
|
|
151
|
+
|
|
152
|
+ return {
|
|
153
|
+ ...defaultScheme[componentName],
|
|
154
|
+ ...colorScheme[componentName]
|
|
155
|
+ }[colorDefinition];
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+}
|
|
159
|
+
|
|
160
|
+export default new ColorSchemeRegistry();
|