forked from google/blockly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththeme.js
236 lines (210 loc) · 6.51 KB
/
theme.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview The class representing a theme.
*/
'use strict';
goog.provide('Blockly.Theme');
goog.require('Blockly.registry');
goog.require('Blockly.utils');
goog.require('Blockly.utils.colour');
goog.require('Blockly.utils.object');
/**
* Class for a theme.
* @param {string} name Theme name.
* @param {!Object.<string, Blockly.Theme.BlockStyle>=} opt_blockStyles A map
* from style names (strings) to objects with style attributes for blocks.
* @param {!Object.<string, Blockly.Theme.CategoryStyle>=} opt_categoryStyles A
* map from style names (strings) to objects with style attributes for
* categories.
* @param {!Blockly.Theme.ComponentStyle=} opt_componentStyles A map of Blockly
* component names to style value.
* @constructor
*/
Blockly.Theme = function(name, opt_blockStyles, opt_categoryStyles,
opt_componentStyles) {
/**
* The theme name. This can be used to reference a specific theme in CSS.
* @type {string}
*/
this.name = name;
/**
* The block styles map.
* @type {!Object.<string, !Blockly.Theme.BlockStyle>}
* @package
*/
this.blockStyles = opt_blockStyles || Object.create(null);
/**
* The category styles map.
* @type {!Object.<string, Blockly.Theme.CategoryStyle>}
* @package
*/
this.categoryStyles = opt_categoryStyles || Object.create(null);
/**
* The UI components styles map.
* @type {!Blockly.Theme.ComponentStyle}
* @package
*/
this.componentStyles = opt_componentStyles ||
(/** @type {Blockly.Theme.ComponentStyle} */ (Object.create(null)));
/**
* The font style.
* @type {!Blockly.Theme.FontStyle}
* @package
*/
this.fontStyle = /** @type {Blockly.Theme.FontStyle} */ (Object.create(null));
/**
* Whether or not to add a 'hat' on top of all blocks with no previous or
* output connections.
* @type {?boolean}
* @package
*/
this.startHats = null;
// Register the theme by name.
Blockly.registry.register(Blockly.registry.Type.THEME, name, this);
};
/**
* A block style.
* @typedef {{
* colourPrimary:string,
* colourSecondary:string,
* colourTertiary:string,
* hat:string
* }}
*/
Blockly.Theme.BlockStyle;
/**
* A category style.
* @typedef {{
* colour:string
* }}
*/
Blockly.Theme.CategoryStyle;
/**
* A component style.
* @typedef {{
* workspaceBackgroundColour:?string,
* toolboxBackgroundColour:?string,
* toolboxForegroundColour:?string,
* flyoutBackgroundColour:?string,
* flyoutForegroundColour:?string,
* flyoutOpacity:?number,
* scrollbarColour:?string,
* scrollbarOpacity:?number,
* insertionMarkerColour:?string,
* insertionMarkerOpacity:?number,
* markerColour:?string,
* cursorColour:?string,
* selectedGlowColour:?string,
* selectedGlowOpacity:?number,
* replacementGlowColour:?string,
* replacementGlowOpacity:?number
* }}
*/
Blockly.Theme.ComponentStyle;
/**
* A font style.
* @typedef {{
* family:?string,
* weight:?string,
* size:?number
* }}
*/
Blockly.Theme.FontStyle;
/**
* Gets the class name that identifies this theme.
* @return {string} The CSS class name.
* @package
*/
Blockly.Theme.prototype.getClassName = function() {
return this.name + '-theme';
};
/**
* Overrides or adds a style to the blockStyles map.
* @param {string} blockStyleName The name of the block style.
* @param {Blockly.Theme.BlockStyle} blockStyle The block style.
*/
Blockly.Theme.prototype.setBlockStyle = function(blockStyleName, blockStyle) {
this.blockStyles[blockStyleName] = blockStyle;
};
/**
* Overrides or adds a style to the categoryStyles map.
* @param {string} categoryStyleName The name of the category style.
* @param {Blockly.Theme.CategoryStyle} categoryStyle The category style.
*/
Blockly.Theme.prototype.setCategoryStyle = function(categoryStyleName,
categoryStyle) {
this.categoryStyles[categoryStyleName] = categoryStyle;
};
/**
* Gets the style for a given Blockly UI component. If the style value is a
* string, we attempt to find the value of any named references.
* @param {string} componentName The name of the component.
* @return {?string} The style value.
*/
Blockly.Theme.prototype.getComponentStyle = function(componentName) {
var style = this.componentStyles[componentName];
if (style && typeof propertyValue == 'string' &&
this.getComponentStyle(/** @type {string} */ (style))) {
return this.getComponentStyle(/** @type {string} */ (style));
}
return style ? String(style) : null;
};
/**
* Configure a specific Blockly UI component with a style value.
* @param {string} componentName The name of the component.
* @param {*} styleValue The style value.
*/
Blockly.Theme.prototype.setComponentStyle = function(componentName,
styleValue) {
this.componentStyles[componentName] = styleValue;
};
/**
* Configure a theme's font style.
* @param {Blockly.Theme.FontStyle} fontStyle The font style.
*/
Blockly.Theme.prototype.setFontStyle = function(fontStyle) {
this.fontStyle = fontStyle;
};
/**
* Configure a theme's start hats.
* @param {boolean} startHats True if the theme enables start hats, false
* otherwise.
*/
Blockly.Theme.prototype.setStartHats = function(startHats) {
this.startHats = startHats;
};
/**
* Define a new Blockly theme.
* @param {string} name The name of the theme.
* @param {!Object} themeObj An object containing theme properties.
* @return {!Blockly.Theme} A new Blockly theme.
*/
Blockly.Theme.defineTheme = function(name, themeObj) {
var theme = new Blockly.Theme(name);
var base = themeObj['base'];
if (base) {
if (typeof base == "string") {
base = Blockly.registry.getObject(Blockly.registry.Type.THEME, base);
}
if (base instanceof Blockly.Theme) {
Blockly.utils.object.deepMerge(theme, base);
theme.name = name;
}
}
Blockly.utils.object.deepMerge(theme.blockStyles,
themeObj['blockStyles']);
Blockly.utils.object.deepMerge(theme.categoryStyles,
themeObj['categoryStyles']);
Blockly.utils.object.deepMerge(theme.componentStyles,
themeObj['componentStyles']);
Blockly.utils.object.deepMerge(theme.fontStyle,
themeObj['fontStyle']);
if (themeObj['startHats'] != null) {
theme.startHats = themeObj['startHats'];
}
return theme;
};