@microsoft/load-themed-styles 2.0.77 → 2.0.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +55 -39
- package/lib/index.js.map +1 -1
- package/lib-amd/index.js +55 -39
- package/lib-amd/index.js.map +1 -1
- package/lib-es6/index.js +55 -39
- package/lib-es6/index.js.map +1 -1
- package/package.json +3 -5
package/lib/index.js
CHANGED
|
@@ -1,36 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
|
+
var __assign = (this && this.__assign) || function () {
|
|
5
|
+
__assign = Object.assign || function(t) {
|
|
6
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
7
|
+
s = arguments[i];
|
|
8
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
9
|
+
t[p] = s[p];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
return __assign.apply(this, arguments);
|
|
14
|
+
};
|
|
4
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
16
|
exports.splitStyles = exports.detokenize = exports.clearStyles = exports.loadTheme = exports.flush = exports.configureRunMode = exports.configureLoadStyles = exports.loadStyles = void 0;
|
|
6
17
|
// Store the theming state in __themeState__ global scope for reuse in the case of duplicate
|
|
7
18
|
// load-themed-styles hosted on the page.
|
|
8
|
-
|
|
19
|
+
var _root = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
20
|
// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).
|
|
10
|
-
|
|
11
|
-
|
|
21
|
+
var _styleNonce = _root && _root.CSPSettings && _root.CSPSettings.nonce;
|
|
22
|
+
var _themeState = initializeThemeState();
|
|
12
23
|
/**
|
|
13
24
|
* Matches theming tokens. For example, "[theme: themeSlotName, default: #FFF]" (including the quotes).
|
|
14
25
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
26
|
+
var _themeTokenRegex = /[\'\"]\[theme:\s*(\w+)\s*(?:\,\s*default:\s*([\\"\']?[\.\,\(\)\#\-\s\w]*[\.\,\(\)\#\-\w][\"\']?))?\s*\][\'\"]/g;
|
|
27
|
+
var now = function () {
|
|
28
|
+
return typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();
|
|
29
|
+
};
|
|
17
30
|
function measure(func) {
|
|
18
|
-
|
|
31
|
+
var start = now();
|
|
19
32
|
func();
|
|
20
|
-
|
|
33
|
+
var end = now();
|
|
21
34
|
_themeState.perf.duration += end - start;
|
|
22
35
|
}
|
|
23
36
|
/**
|
|
24
37
|
* initialize global state object
|
|
25
38
|
*/
|
|
26
39
|
function initializeThemeState() {
|
|
27
|
-
|
|
40
|
+
var state = _root.__themeState__ || {
|
|
28
41
|
theme: undefined,
|
|
29
42
|
lastStyleElement: undefined,
|
|
30
43
|
registeredStyles: []
|
|
31
44
|
};
|
|
32
45
|
if (!state.runState) {
|
|
33
|
-
state =
|
|
46
|
+
state = __assign(__assign({}, state), { perf: {
|
|
34
47
|
count: 0,
|
|
35
48
|
duration: 0
|
|
36
49
|
}, runState: {
|
|
@@ -40,7 +53,7 @@ function initializeThemeState() {
|
|
|
40
53
|
} });
|
|
41
54
|
}
|
|
42
55
|
if (!state.registeredThemableStyles) {
|
|
43
|
-
state =
|
|
56
|
+
state = __assign(__assign({}, state), { registeredThemableStyles: [] });
|
|
44
57
|
}
|
|
45
58
|
_root.__themeState__ = state;
|
|
46
59
|
return state;
|
|
@@ -51,10 +64,11 @@ function initializeThemeState() {
|
|
|
51
64
|
* @param {string | ThemableArray} styles Themable style text to register.
|
|
52
65
|
* @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.
|
|
53
66
|
*/
|
|
54
|
-
function loadStyles(styles, loadAsync
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
67
|
+
function loadStyles(styles, loadAsync) {
|
|
68
|
+
if (loadAsync === void 0) { loadAsync = false; }
|
|
69
|
+
measure(function () {
|
|
70
|
+
var styleParts = Array.isArray(styles) ? styles : splitStyles(styles);
|
|
71
|
+
var _a = _themeState.runState, mode = _a.mode, buffer = _a.buffer, flushTimer = _a.flushTimer;
|
|
58
72
|
if (loadAsync || mode === 1 /* Mode.async */) {
|
|
59
73
|
buffer.push(styleParts);
|
|
60
74
|
if (!flushTimer) {
|
|
@@ -88,10 +102,10 @@ exports.configureRunMode = configureRunMode;
|
|
|
88
102
|
* external code can call flush to synchronously force processing of currently buffered styles
|
|
89
103
|
*/
|
|
90
104
|
function flush() {
|
|
91
|
-
measure(()
|
|
92
|
-
|
|
105
|
+
measure(function () {
|
|
106
|
+
var styleArrays = _themeState.runState.buffer.slice();
|
|
93
107
|
_themeState.runState.buffer = [];
|
|
94
|
-
|
|
108
|
+
var mergedStyleArray = [].concat.apply([], styleArrays);
|
|
95
109
|
if (mergedStyleArray.length > 0) {
|
|
96
110
|
applyThemableStyles(mergedStyleArray);
|
|
97
111
|
}
|
|
@@ -104,7 +118,7 @@ exports.flush = flush;
|
|
|
104
118
|
function asyncLoadStyles() {
|
|
105
119
|
// Use "self" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node
|
|
106
120
|
// https://github.com/jestjs/jest/issues/14418
|
|
107
|
-
return self.setTimeout(()
|
|
121
|
+
return self.setTimeout(function () {
|
|
108
122
|
_themeState.runState.flushTimer = 0;
|
|
109
123
|
flush();
|
|
110
124
|
}, 0);
|
|
@@ -139,7 +153,8 @@ exports.loadTheme = loadTheme;
|
|
|
139
153
|
* @param option - specify which group of registered styles should be cleared.
|
|
140
154
|
* Default to be both themable and non-themable styles will be cleared
|
|
141
155
|
*/
|
|
142
|
-
function clearStyles(option
|
|
156
|
+
function clearStyles(option) {
|
|
157
|
+
if (option === void 0) { option = 3 /* ClearStyleOptions.all */; }
|
|
143
158
|
if (option === 3 /* ClearStyleOptions.all */ || option === 2 /* ClearStyleOptions.onlyNonThemable */) {
|
|
144
159
|
clearStylesInternal(_themeState.registeredStyles);
|
|
145
160
|
_themeState.registeredStyles = [];
|
|
@@ -151,8 +166,8 @@ function clearStyles(option = 3 /* ClearStyleOptions.all */) {
|
|
|
151
166
|
}
|
|
152
167
|
exports.clearStyles = clearStyles;
|
|
153
168
|
function clearStylesInternal(records) {
|
|
154
|
-
records.forEach((styleRecord)
|
|
155
|
-
|
|
169
|
+
records.forEach(function (styleRecord) {
|
|
170
|
+
var styleElement = styleRecord && styleRecord.styleElement;
|
|
156
171
|
if (styleElement && styleElement.parentElement) {
|
|
157
172
|
styleElement.parentElement.removeChild(styleElement);
|
|
158
173
|
}
|
|
@@ -163,8 +178,9 @@ function clearStylesInternal(records) {
|
|
|
163
178
|
*/
|
|
164
179
|
function reloadStyles() {
|
|
165
180
|
if (_themeState.theme) {
|
|
166
|
-
|
|
167
|
-
for (
|
|
181
|
+
var themableStyles = [];
|
|
182
|
+
for (var _i = 0, _a = _themeState.registeredThemableStyles; _i < _a.length; _i++) {
|
|
183
|
+
var styleRecord = _a[_i];
|
|
168
184
|
themableStyles.push(styleRecord.themableStyle);
|
|
169
185
|
}
|
|
170
186
|
if (themableStyles.length > 0) {
|
|
@@ -189,17 +205,17 @@ exports.detokenize = detokenize;
|
|
|
189
205
|
* @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.
|
|
190
206
|
*/
|
|
191
207
|
function resolveThemableArray(splitStyleArray) {
|
|
192
|
-
|
|
193
|
-
|
|
208
|
+
var theme = _themeState.theme;
|
|
209
|
+
var themable = false;
|
|
194
210
|
// Resolve the array of theming instructions to an array of strings.
|
|
195
211
|
// Then join the array to produce the final CSS string.
|
|
196
|
-
|
|
197
|
-
|
|
212
|
+
var resolvedArray = (splitStyleArray || []).map(function (currentValue) {
|
|
213
|
+
var themeSlot = currentValue.theme;
|
|
198
214
|
if (themeSlot) {
|
|
199
215
|
themable = true;
|
|
200
216
|
// A theming annotation. Resolve it.
|
|
201
|
-
|
|
202
|
-
|
|
217
|
+
var themedValue = theme ? theme[themeSlot] : undefined;
|
|
218
|
+
var defaultValue = currentValue.defaultValue || 'inherit';
|
|
203
219
|
// Warn to console if we hit an unthemed value even when themes are provided, but only if "DEBUG" is true.
|
|
204
220
|
// Allow the themedValue to be undefined to explicitly request the default value.
|
|
205
221
|
if (theme &&
|
|
@@ -208,7 +224,7 @@ function resolveThemableArray(splitStyleArray) {
|
|
|
208
224
|
!(themeSlot in theme) &&
|
|
209
225
|
typeof DEBUG !== 'undefined' &&
|
|
210
226
|
DEBUG) {
|
|
211
|
-
console.warn(
|
|
227
|
+
console.warn("Theming value not provided for \"".concat(themeSlot, "\". Falling back to \"").concat(defaultValue, "\"."));
|
|
212
228
|
}
|
|
213
229
|
return themedValue || defaultValue;
|
|
214
230
|
}
|
|
@@ -227,12 +243,12 @@ function resolveThemableArray(splitStyleArray) {
|
|
|
227
243
|
* @param {string} styles Tokenized styles to split.
|
|
228
244
|
*/
|
|
229
245
|
function splitStyles(styles) {
|
|
230
|
-
|
|
246
|
+
var result = [];
|
|
231
247
|
if (styles) {
|
|
232
|
-
|
|
233
|
-
|
|
248
|
+
var pos = 0; // Current position in styles.
|
|
249
|
+
var tokenMatch = void 0;
|
|
234
250
|
while ((tokenMatch = _themeTokenRegex.exec(styles))) {
|
|
235
|
-
|
|
251
|
+
var matchIndex = tokenMatch.index;
|
|
236
252
|
if (matchIndex > pos) {
|
|
237
253
|
result.push({
|
|
238
254
|
rawString: styles.substring(pos, matchIndex)
|
|
@@ -263,9 +279,9 @@ function registerStyles(styleArray) {
|
|
|
263
279
|
if (typeof document === 'undefined') {
|
|
264
280
|
return;
|
|
265
281
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
282
|
+
var head = document.getElementsByTagName('head')[0];
|
|
283
|
+
var styleElement = document.createElement('style');
|
|
284
|
+
var _a = resolveThemableArray(styleArray), styleString = _a.styleString, themable = _a.themable;
|
|
269
285
|
styleElement.setAttribute('data-load-themed-styles', 'true');
|
|
270
286
|
if (_styleNonce) {
|
|
271
287
|
styleElement.setAttribute('nonce', _styleNonce);
|
|
@@ -273,13 +289,13 @@ function registerStyles(styleArray) {
|
|
|
273
289
|
styleElement.appendChild(document.createTextNode(styleString));
|
|
274
290
|
_themeState.perf.count++;
|
|
275
291
|
head.appendChild(styleElement);
|
|
276
|
-
|
|
292
|
+
var ev = document.createEvent('HTMLEvents');
|
|
277
293
|
ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);
|
|
278
294
|
ev.args = {
|
|
279
295
|
newStyle: styleElement
|
|
280
296
|
};
|
|
281
297
|
document.dispatchEvent(ev);
|
|
282
|
-
|
|
298
|
+
var record = {
|
|
283
299
|
styleElement: styleElement,
|
|
284
300
|
themableStyle: styleArray
|
|
285
301
|
};
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAyG3D,4FAA4F;AAC5F,yCAAyC;AACzC,MAAM,KAAK,GAAQ,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,yDAAyD;AAE7H,yGAAyG;AACzG,MAAM,WAAW,GAAW,KAAK,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC;AAElF,MAAM,WAAW,GAAgB,oBAAoB,EAAE,CAAC;AAExD;;GAEG;AACH,MAAM,gBAAgB,GACpB,gHAAgH,CAAC;AAEnH,MAAM,GAAG,GAAiB,GAAG,EAAE,CAC7B,OAAO,WAAW,KAAK,WAAW,IAAI,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,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,MAAM,YAAY,GAAqB,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IACvE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,oBAAoB,CAAC,UAAU,CAAC,CAAC;IAEnE,YAAY,CAAC,YAAY,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;IAC7D,IAAI,WAAW,EAAE;QACf,YAAY,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;KACjD;IACD,YAAY,CAAC,WAAW,CAAC,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC;IAC/D,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACzB,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAE/B,MAAM,EAAE,GAAiD,QAAQ,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAC5F,EAAE,CAAC,SAAS,CAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAC5E,EAAE,CAAC,IAAI,GAAG;QACR,QAAQ,EAAE,YAAY;KACvB,CAAC;IACF,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;IAE3B,MAAM,MAAM,GAAiB;QAC3B,YAAY,EAAE,YAAY;QAC1B,aAAa,EAAE,UAAU;KAC1B,CAAC;IAEF,IAAI,QAAQ,EAAE;QACZ,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KACnD;SAAM;QACL,WAAW,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KAC3C;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * An IThemingInstruction can specify a rawString to be preserved or a theme slot and a default value\n * to use if that slot is not specified by the theme.\n */\n\n/* eslint-disable @typescript-eslint/no-use-before-define */\n\n// Declaring a global here in case that the execution environment is Node.js (without importing the\n// entire node.js d.ts for now)\ndeclare let global: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\nexport interface IThemingInstruction {\n theme?: string;\n defaultValue?: string;\n rawString?: string;\n}\n\nexport type ThemableArray = IThemingInstruction[];\n\nexport interface ITheme {\n [key: string]: string;\n}\n\ninterface IStyleSheet {\n cssText: string;\n}\n\ninterface IExtendedHtmlStyleElement extends HTMLStyleElement {\n styleSheet: IStyleSheet;\n}\n\n/**\n * Performance Measurement of loading styles\n */\ninterface IMeasurement {\n /**\n * Count of style element injected, which is the slow operation in IE\n */\n count: number;\n /**\n * Total duration of all loadStyles exections\n */\n duration: number;\n}\n\ninterface IRunState {\n mode: Mode;\n buffer: ThemableArray[];\n flushTimer: number;\n}\n\ninterface IThemeState {\n theme: ITheme | undefined;\n lastStyleElement: IExtendedHtmlStyleElement;\n registeredStyles: IStyleRecord[]; // records of already registered non-themable styles\n registeredThemableStyles: IStyleRecord[]; // records of already registered themable styles\n loadStyles: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined;\n perf: IMeasurement;\n runState: IRunState;\n}\n\ninterface IStyleRecord {\n styleElement: Element;\n themableStyle: ThemableArray;\n}\n\ninterface ICustomEvent<T> extends Event {\n args?: T;\n}\n\n/**\n * object returned from resolveThemableArray function\n */\ninterface IThemableArrayResolveResult {\n /** this string is the processed styles in string */\n styleString: string;\n\n /** this boolean indicates if this style array is themable */\n themable: boolean;\n}\n\n/**\n * In sync mode, styles are registered as style elements synchronously with loadStyles() call.\n * In async mode, styles are buffered and registered as batch in async timer for performance purpose.\n */\nexport const enum Mode {\n sync,\n async\n}\n\n/**\n * Themable styles and non-themable styles are tracked separately\n * Specify ClearStyleOptions when calling clearStyles API to specify which group of registered styles should be cleared.\n */\nexport const enum ClearStyleOptions {\n /** only themable styles will be cleared */\n onlyThemable = 1,\n /** only non-themable styles will be cleared */\n onlyNonThemable = 2,\n /** both themable and non-themable styles will be cleared */\n all = 3\n}\n\n// Store the theming state in __themeState__ global scope for reuse in the case of duplicate\n// load-themed-styles hosted on the page.\nconst _root: any = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).\nconst _styleNonce: string = _root && _root.CSPSettings && _root.CSPSettings.nonce;\n\nconst _themeState: IThemeState = initializeThemeState();\n\n/**\n * Matches theming tokens. For example, \"[theme: themeSlotName, default: #FFF]\" (including the quotes).\n */\nconst _themeTokenRegex: RegExp =\n /[\\'\\\"]\\[theme:\\s*(\\w+)\\s*(?:\\,\\s*default:\\s*([\\\\\"\\']?[\\.\\,\\(\\)\\#\\-\\s\\w]*[\\.\\,\\(\\)\\#\\-\\w][\\\"\\']?))?\\s*\\][\\'\\\"]/g;\n\nconst now: () => number = () =>\n typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();\n\nfunction measure(func: () => void): void {\n const start: number = now();\n func();\n const end: number = now();\n _themeState.perf.duration += end - start;\n}\n\n/**\n * initialize global state object\n */\nfunction initializeThemeState(): IThemeState {\n let state: IThemeState = _root.__themeState__ || {\n theme: undefined,\n lastStyleElement: undefined,\n registeredStyles: []\n };\n\n if (!state.runState) {\n state = {\n ...state,\n perf: {\n count: 0,\n duration: 0\n },\n runState: {\n flushTimer: 0,\n mode: Mode.sync,\n buffer: []\n }\n };\n }\n if (!state.registeredThemableStyles) {\n state = {\n ...state,\n registeredThemableStyles: []\n };\n }\n _root.__themeState__ = state;\n return state;\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load\n * event is fired.\n * @param {string | ThemableArray} styles Themable style text to register.\n * @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.\n */\nexport function loadStyles(styles: string | ThemableArray, loadAsync: boolean = false): void {\n measure(() => {\n const styleParts: ThemableArray = Array.isArray(styles) ? styles : splitStyles(styles);\n const { mode, buffer, flushTimer } = _themeState.runState;\n if (loadAsync || mode === Mode.async) {\n buffer.push(styleParts);\n if (!flushTimer) {\n _themeState.runState.flushTimer = asyncLoadStyles();\n }\n } else {\n applyThemableStyles(styleParts);\n }\n });\n}\n\n/**\n * Allows for customizable loadStyles logic. e.g. for server side rendering application\n * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void}\n * a loadStyles callback that gets called when styles are loaded or reloaded\n */\nexport function configureLoadStyles(\n loadStylesFn: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined\n): void {\n _themeState.loadStyles = loadStylesFn;\n}\n\n/**\n * Configure run mode of load-themable-styles\n * @param mode load-themable-styles run mode, async or sync\n */\nexport function configureRunMode(mode: Mode): void {\n _themeState.runState.mode = mode;\n}\n\n/**\n * external code can call flush to synchronously force processing of currently buffered styles\n */\nexport function flush(): void {\n measure(() => {\n const styleArrays: ThemableArray[] = _themeState.runState.buffer.slice();\n _themeState.runState.buffer = [];\n const mergedStyleArray: ThemableArray = ([] as ThemableArray).concat.apply([], styleArrays);\n if (mergedStyleArray.length > 0) {\n applyThemableStyles(mergedStyleArray);\n }\n });\n}\n\n/**\n * register async loadStyles\n */\nfunction asyncLoadStyles(): number {\n // Use \"self\" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node\n // https://github.com/jestjs/jest/issues/14418\n return self.setTimeout(() => {\n _themeState.runState.flushTimer = 0;\n flush();\n }, 0);\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load event\n * is fired.\n * @param {string} styleText Style to register.\n * @param {IStyleRecord} styleRecord Existing style record to re-apply.\n */\nfunction applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord): void {\n if (_themeState.loadStyles) {\n _themeState.loadStyles(resolveThemableArray(stylesArray).styleString, stylesArray);\n } else {\n registerStyles(stylesArray);\n }\n}\n\n/**\n * Registers a set theme tokens to find and replace. If styles were already registered, they will be\n * replaced.\n * @param {theme} theme JSON object of theme tokens to values.\n */\nexport function loadTheme(theme: ITheme | undefined): void {\n _themeState.theme = theme;\n\n // reload styles.\n reloadStyles();\n}\n\n/**\n * Clear already registered style elements and style records in theme_State object\n * @param option - specify which group of registered styles should be cleared.\n * Default to be both themable and non-themable styles will be cleared\n */\nexport function clearStyles(option: ClearStyleOptions = ClearStyleOptions.all): void {\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyNonThemable) {\n clearStylesInternal(_themeState.registeredStyles);\n _themeState.registeredStyles = [];\n }\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyThemable) {\n clearStylesInternal(_themeState.registeredThemableStyles);\n _themeState.registeredThemableStyles = [];\n }\n}\n\nfunction clearStylesInternal(records: IStyleRecord[]): void {\n records.forEach((styleRecord: IStyleRecord) => {\n const styleElement: HTMLStyleElement = styleRecord && (styleRecord.styleElement as HTMLStyleElement);\n if (styleElement && styleElement.parentElement) {\n styleElement.parentElement.removeChild(styleElement);\n }\n });\n}\n\n/**\n * Reloads styles.\n */\nfunction reloadStyles(): void {\n if (_themeState.theme) {\n const themableStyles: ThemableArray[] = [];\n for (const styleRecord of _themeState.registeredThemableStyles) {\n themableStyles.push(styleRecord.themableStyle);\n }\n if (themableStyles.length > 0) {\n clearStyles(ClearStyleOptions.onlyThemable);\n applyThemableStyles(([] as ThemableArray).concat.apply([], themableStyles));\n }\n }\n}\n\n/**\n * Find theme tokens and replaces them with provided theme values.\n * @param {string} styles Tokenized styles to fix.\n */\nexport function detokenize(styles: string | undefined): string | undefined {\n if (styles) {\n styles = resolveThemableArray(splitStyles(styles)).styleString;\n }\n\n return styles;\n}\n\n/**\n * Resolves ThemingInstruction objects in an array and joins the result into a string.\n * @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.\n */\nfunction resolveThemableArray(splitStyleArray: ThemableArray): IThemableArrayResolveResult {\n const { theme }: IThemeState = _themeState;\n let themable: boolean = false;\n // Resolve the array of theming instructions to an array of strings.\n // Then join the array to produce the final CSS string.\n const resolvedArray: (string | undefined)[] = (splitStyleArray || []).map(\n (currentValue: IThemingInstruction) => {\n const themeSlot: string | undefined = currentValue.theme;\n if (themeSlot) {\n themable = true;\n // A theming annotation. Resolve it.\n const themedValue: string | undefined = theme ? theme[themeSlot] : undefined;\n const defaultValue: string = currentValue.defaultValue || 'inherit';\n\n // Warn to console if we hit an unthemed value even when themes are provided, but only if \"DEBUG\" is true.\n // Allow the themedValue to be undefined to explicitly request the default value.\n if (\n theme &&\n !themedValue &&\n console &&\n !(themeSlot in theme) &&\n typeof DEBUG !== 'undefined' &&\n DEBUG\n ) {\n console.warn(`Theming value not provided for \"${themeSlot}\". Falling back to \"${defaultValue}\".`);\n }\n\n return themedValue || defaultValue;\n } else {\n // A non-themable string. Preserve it.\n return currentValue.rawString;\n }\n }\n );\n\n return {\n styleString: resolvedArray.join(''),\n themable: themable\n };\n}\n\n/**\n * Split tokenized CSS into an array of strings and theme specification objects\n * @param {string} styles Tokenized styles to split.\n */\nexport function splitStyles(styles: string): ThemableArray {\n const result: ThemableArray = [];\n if (styles) {\n let pos: number = 0; // Current position in styles.\n let tokenMatch: RegExpExecArray | null;\n while ((tokenMatch = _themeTokenRegex.exec(styles))) {\n const matchIndex: number = tokenMatch.index;\n if (matchIndex > pos) {\n result.push({\n rawString: styles.substring(pos, matchIndex)\n });\n }\n\n result.push({\n theme: tokenMatch[1],\n defaultValue: tokenMatch[2] // May be undefined\n });\n\n // index of the first character after the current match\n pos = _themeTokenRegex.lastIndex;\n }\n\n // Push the rest of the string after the last match.\n result.push({\n rawString: styles.substring(pos)\n });\n }\n\n return result;\n}\n\n/**\n * Registers a set of style text. If it is registered too early, we will register it when the\n * window.load event is fired.\n * @param {ThemableArray} styleArray Array of IThemingInstruction objects to register.\n * @param {IStyleRecord} styleRecord May specify a style Element to update.\n */\nfunction registerStyles(styleArray: ThemableArray): void {\n if (typeof document === 'undefined') {\n return;\n }\n const head: HTMLHeadElement = document.getElementsByTagName('head')[0];\n const styleElement: HTMLStyleElement = document.createElement('style');\n const { styleString, themable } = resolveThemableArray(styleArray);\n\n styleElement.setAttribute('data-load-themed-styles', 'true');\n if (_styleNonce) {\n styleElement.setAttribute('nonce', _styleNonce);\n }\n styleElement.appendChild(document.createTextNode(styleString));\n _themeState.perf.count++;\n head.appendChild(styleElement);\n\n const ev: ICustomEvent<{ newStyle: HTMLStyleElement }> = document.createEvent('HTMLEvents');\n ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);\n ev.args = {\n newStyle: styleElement\n };\n document.dispatchEvent(ev);\n\n const record: IStyleRecord = {\n styleElement: styleElement,\n themableStyle: styleArray\n };\n\n if (themable) {\n _themeState.registeredThemableStyles.push(record);\n } else {\n _themeState.registeredStyles.push(record);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;AAyG3D,4FAA4F;AAC5F,yCAAyC;AACzC,IAAM,KAAK,GAAQ,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,yDAAyD;AAE7H,yGAAyG;AACzG,IAAM,WAAW,GAAW,KAAK,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC;AAElF,IAAM,WAAW,GAAgB,oBAAoB,EAAE,CAAC;AAExD;;GAEG;AACH,IAAM,gBAAgB,GACpB,gHAAgH,CAAC;AAEnH,IAAM,GAAG,GAAiB;IACxB,OAAA,OAAO,WAAW,KAAK,WAAW,IAAI,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;AAAxF,CAAwF,CAAC;AAE3F,SAAS,OAAO,CAAC,IAAgB;IAC/B,IAAM,KAAK,GAAW,GAAG,EAAE,CAAC;IAC5B,IAAI,EAAE,CAAC;IACP,IAAM,GAAG,GAAW,GAAG,EAAE,CAAC;IAC1B,WA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AAU,CAAC,EAA1D,WAAW,iBAAA,EAAE,QAAQ,cAAqC,CAAC;IAEnE,YAAY,CAAC,YAAY,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;IAC7D,IAAI,WAAW,EAAE;QACf,YAAY,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;KACjD;IACD,YAAY,CAAC,WAAW,CAAC,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC;IAC/D,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACzB,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAE/B,IAAM,EAAE,GAAiD,QAAQ,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAC5F,EAAE,CAAC,SAAS,CAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAC5E,EAAE,CAAC,IAAI,GAAG;QACR,QAAQ,EAAE,YAAY;KACvB,CAAC;IACF,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;IAE3B,IAAM,MAAM,GAAiB;QAC3B,YAAY,EAAE,YAAY;QAC1B,aAAa,EAAE,UAAU;KAC1B,CAAC;IAEF,IAAI,QAAQ,EAAE;QACZ,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KACnD;SAAM;QACL,WAAW,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KAC3C;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * An IThemingInstruction can specify a rawString to be preserved or a theme slot and a default value\n * to use if that slot is not specified by the theme.\n */\n\n/* eslint-disable @typescript-eslint/no-use-before-define */\n\n// Declaring a global here in case that the execution environment is Node.js (without importing the\n// entire node.js d.ts for now)\ndeclare let global: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\nexport interface IThemingInstruction {\n theme?: string;\n defaultValue?: string;\n rawString?: string;\n}\n\nexport type ThemableArray = IThemingInstruction[];\n\nexport interface ITheme {\n [key: string]: string;\n}\n\ninterface IStyleSheet {\n cssText: string;\n}\n\ninterface IExtendedHtmlStyleElement extends HTMLStyleElement {\n styleSheet: IStyleSheet;\n}\n\n/**\n * Performance Measurement of loading styles\n */\ninterface IMeasurement {\n /**\n * Count of style element injected, which is the slow operation in IE\n */\n count: number;\n /**\n * Total duration of all loadStyles exections\n */\n duration: number;\n}\n\ninterface IRunState {\n mode: Mode;\n buffer: ThemableArray[];\n flushTimer: number;\n}\n\ninterface IThemeState {\n theme: ITheme | undefined;\n lastStyleElement: IExtendedHtmlStyleElement;\n registeredStyles: IStyleRecord[]; // records of already registered non-themable styles\n registeredThemableStyles: IStyleRecord[]; // records of already registered themable styles\n loadStyles: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined;\n perf: IMeasurement;\n runState: IRunState;\n}\n\ninterface IStyleRecord {\n styleElement: Element;\n themableStyle: ThemableArray;\n}\n\ninterface ICustomEvent<T> extends Event {\n args?: T;\n}\n\n/**\n * object returned from resolveThemableArray function\n */\ninterface IThemableArrayResolveResult {\n /** this string is the processed styles in string */\n styleString: string;\n\n /** this boolean indicates if this style array is themable */\n themable: boolean;\n}\n\n/**\n * In sync mode, styles are registered as style elements synchronously with loadStyles() call.\n * In async mode, styles are buffered and registered as batch in async timer for performance purpose.\n */\nexport const enum Mode {\n sync,\n async\n}\n\n/**\n * Themable styles and non-themable styles are tracked separately\n * Specify ClearStyleOptions when calling clearStyles API to specify which group of registered styles should be cleared.\n */\nexport const enum ClearStyleOptions {\n /** only themable styles will be cleared */\n onlyThemable = 1,\n /** only non-themable styles will be cleared */\n onlyNonThemable = 2,\n /** both themable and non-themable styles will be cleared */\n all = 3\n}\n\n// Store the theming state in __themeState__ global scope for reuse in the case of duplicate\n// load-themed-styles hosted on the page.\nconst _root: any = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).\nconst _styleNonce: string = _root && _root.CSPSettings && _root.CSPSettings.nonce;\n\nconst _themeState: IThemeState = initializeThemeState();\n\n/**\n * Matches theming tokens. For example, \"[theme: themeSlotName, default: #FFF]\" (including the quotes).\n */\nconst _themeTokenRegex: RegExp =\n /[\\'\\\"]\\[theme:\\s*(\\w+)\\s*(?:\\,\\s*default:\\s*([\\\\\"\\']?[\\.\\,\\(\\)\\#\\-\\s\\w]*[\\.\\,\\(\\)\\#\\-\\w][\\\"\\']?))?\\s*\\][\\'\\\"]/g;\n\nconst now: () => number = () =>\n typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();\n\nfunction measure(func: () => void): void {\n const start: number = now();\n func();\n const end: number = now();\n _themeState.perf.duration += end - start;\n}\n\n/**\n * initialize global state object\n */\nfunction initializeThemeState(): IThemeState {\n let state: IThemeState = _root.__themeState__ || {\n theme: undefined,\n lastStyleElement: undefined,\n registeredStyles: []\n };\n\n if (!state.runState) {\n state = {\n ...state,\n perf: {\n count: 0,\n duration: 0\n },\n runState: {\n flushTimer: 0,\n mode: Mode.sync,\n buffer: []\n }\n };\n }\n if (!state.registeredThemableStyles) {\n state = {\n ...state,\n registeredThemableStyles: []\n };\n }\n _root.__themeState__ = state;\n return state;\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load\n * event is fired.\n * @param {string | ThemableArray} styles Themable style text to register.\n * @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.\n */\nexport function loadStyles(styles: string | ThemableArray, loadAsync: boolean = false): void {\n measure(() => {\n const styleParts: ThemableArray = Array.isArray(styles) ? styles : splitStyles(styles);\n const { mode, buffer, flushTimer } = _themeState.runState;\n if (loadAsync || mode === Mode.async) {\n buffer.push(styleParts);\n if (!flushTimer) {\n _themeState.runState.flushTimer = asyncLoadStyles();\n }\n } else {\n applyThemableStyles(styleParts);\n }\n });\n}\n\n/**\n * Allows for customizable loadStyles logic. e.g. for server side rendering application\n * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void}\n * a loadStyles callback that gets called when styles are loaded or reloaded\n */\nexport function configureLoadStyles(\n loadStylesFn: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined\n): void {\n _themeState.loadStyles = loadStylesFn;\n}\n\n/**\n * Configure run mode of load-themable-styles\n * @param mode load-themable-styles run mode, async or sync\n */\nexport function configureRunMode(mode: Mode): void {\n _themeState.runState.mode = mode;\n}\n\n/**\n * external code can call flush to synchronously force processing of currently buffered styles\n */\nexport function flush(): void {\n measure(() => {\n const styleArrays: ThemableArray[] = _themeState.runState.buffer.slice();\n _themeState.runState.buffer = [];\n const mergedStyleArray: ThemableArray = ([] as ThemableArray).concat.apply([], styleArrays);\n if (mergedStyleArray.length > 0) {\n applyThemableStyles(mergedStyleArray);\n }\n });\n}\n\n/**\n * register async loadStyles\n */\nfunction asyncLoadStyles(): number {\n // Use \"self\" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node\n // https://github.com/jestjs/jest/issues/14418\n return self.setTimeout(() => {\n _themeState.runState.flushTimer = 0;\n flush();\n }, 0);\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load event\n * is fired.\n * @param {string} styleText Style to register.\n * @param {IStyleRecord} styleRecord Existing style record to re-apply.\n */\nfunction applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord): void {\n if (_themeState.loadStyles) {\n _themeState.loadStyles(resolveThemableArray(stylesArray).styleString, stylesArray);\n } else {\n registerStyles(stylesArray);\n }\n}\n\n/**\n * Registers a set theme tokens to find and replace. If styles were already registered, they will be\n * replaced.\n * @param {theme} theme JSON object of theme tokens to values.\n */\nexport function loadTheme(theme: ITheme | undefined): void {\n _themeState.theme = theme;\n\n // reload styles.\n reloadStyles();\n}\n\n/**\n * Clear already registered style elements and style records in theme_State object\n * @param option - specify which group of registered styles should be cleared.\n * Default to be both themable and non-themable styles will be cleared\n */\nexport function clearStyles(option: ClearStyleOptions = ClearStyleOptions.all): void {\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyNonThemable) {\n clearStylesInternal(_themeState.registeredStyles);\n _themeState.registeredStyles = [];\n }\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyThemable) {\n clearStylesInternal(_themeState.registeredThemableStyles);\n _themeState.registeredThemableStyles = [];\n }\n}\n\nfunction clearStylesInternal(records: IStyleRecord[]): void {\n records.forEach((styleRecord: IStyleRecord) => {\n const styleElement: HTMLStyleElement = styleRecord && (styleRecord.styleElement as HTMLStyleElement);\n if (styleElement && styleElement.parentElement) {\n styleElement.parentElement.removeChild(styleElement);\n }\n });\n}\n\n/**\n * Reloads styles.\n */\nfunction reloadStyles(): void {\n if (_themeState.theme) {\n const themableStyles: ThemableArray[] = [];\n for (const styleRecord of _themeState.registeredThemableStyles) {\n themableStyles.push(styleRecord.themableStyle);\n }\n if (themableStyles.length > 0) {\n clearStyles(ClearStyleOptions.onlyThemable);\n applyThemableStyles(([] as ThemableArray).concat.apply([], themableStyles));\n }\n }\n}\n\n/**\n * Find theme tokens and replaces them with provided theme values.\n * @param {string} styles Tokenized styles to fix.\n */\nexport function detokenize(styles: string | undefined): string | undefined {\n if (styles) {\n styles = resolveThemableArray(splitStyles(styles)).styleString;\n }\n\n return styles;\n}\n\n/**\n * Resolves ThemingInstruction objects in an array and joins the result into a string.\n * @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.\n */\nfunction resolveThemableArray(splitStyleArray: ThemableArray): IThemableArrayResolveResult {\n const { theme }: IThemeState = _themeState;\n let themable: boolean = false;\n // Resolve the array of theming instructions to an array of strings.\n // Then join the array to produce the final CSS string.\n const resolvedArray: (string | undefined)[] = (splitStyleArray || []).map(\n (currentValue: IThemingInstruction) => {\n const themeSlot: string | undefined = currentValue.theme;\n if (themeSlot) {\n themable = true;\n // A theming annotation. Resolve it.\n const themedValue: string | undefined = theme ? theme[themeSlot] : undefined;\n const defaultValue: string = currentValue.defaultValue || 'inherit';\n\n // Warn to console if we hit an unthemed value even when themes are provided, but only if \"DEBUG\" is true.\n // Allow the themedValue to be undefined to explicitly request the default value.\n if (\n theme &&\n !themedValue &&\n console &&\n !(themeSlot in theme) &&\n typeof DEBUG !== 'undefined' &&\n DEBUG\n ) {\n console.warn(`Theming value not provided for \"${themeSlot}\". Falling back to \"${defaultValue}\".`);\n }\n\n return themedValue || defaultValue;\n } else {\n // A non-themable string. Preserve it.\n return currentValue.rawString;\n }\n }\n );\n\n return {\n styleString: resolvedArray.join(''),\n themable: themable\n };\n}\n\n/**\n * Split tokenized CSS into an array of strings and theme specification objects\n * @param {string} styles Tokenized styles to split.\n */\nexport function splitStyles(styles: string): ThemableArray {\n const result: ThemableArray = [];\n if (styles) {\n let pos: number = 0; // Current position in styles.\n let tokenMatch: RegExpExecArray | null;\n while ((tokenMatch = _themeTokenRegex.exec(styles))) {\n const matchIndex: number = tokenMatch.index;\n if (matchIndex > pos) {\n result.push({\n rawString: styles.substring(pos, matchIndex)\n });\n }\n\n result.push({\n theme: tokenMatch[1],\n defaultValue: tokenMatch[2] // May be undefined\n });\n\n // index of the first character after the current match\n pos = _themeTokenRegex.lastIndex;\n }\n\n // Push the rest of the string after the last match.\n result.push({\n rawString: styles.substring(pos)\n });\n }\n\n return result;\n}\n\n/**\n * Registers a set of style text. If it is registered too early, we will register it when the\n * window.load event is fired.\n * @param {ThemableArray} styleArray Array of IThemingInstruction objects to register.\n * @param {IStyleRecord} styleRecord May specify a style Element to update.\n */\nfunction registerStyles(styleArray: ThemableArray): void {\n if (typeof document === 'undefined') {\n return;\n }\n const head: HTMLHeadElement = document.getElementsByTagName('head')[0];\n const styleElement: HTMLStyleElement = document.createElement('style');\n const { styleString, themable } = resolveThemableArray(styleArray);\n\n styleElement.setAttribute('data-load-themed-styles', 'true');\n if (_styleNonce) {\n styleElement.setAttribute('nonce', _styleNonce);\n }\n styleElement.appendChild(document.createTextNode(styleString));\n _themeState.perf.count++;\n head.appendChild(styleElement);\n\n const ev: ICustomEvent<{ newStyle: HTMLStyleElement }> = document.createEvent('HTMLEvents');\n ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);\n ev.args = {\n newStyle: styleElement\n };\n document.dispatchEvent(ev);\n\n const record: IStyleRecord = {\n styleElement: styleElement,\n themableStyle: styleArray\n };\n\n if (themable) {\n _themeState.registeredThemableStyles.push(record);\n } else {\n _themeState.registeredStyles.push(record);\n }\n}\n"]}
|
package/lib-amd/index.js
CHANGED
|
@@ -1,37 +1,50 @@
|
|
|
1
1
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
2
|
// See LICENSE in the project root for license information.
|
|
3
|
+
var __assign = (this && this.__assign) || function () {
|
|
4
|
+
__assign = Object.assign || function(t) {
|
|
5
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
6
|
+
s = arguments[i];
|
|
7
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
8
|
+
t[p] = s[p];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
return __assign.apply(this, arguments);
|
|
13
|
+
};
|
|
3
14
|
define(["require", "exports"], function (require, exports) {
|
|
4
15
|
"use strict";
|
|
5
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
17
|
exports.splitStyles = exports.detokenize = exports.clearStyles = exports.loadTheme = exports.flush = exports.configureRunMode = exports.configureLoadStyles = exports.loadStyles = void 0;
|
|
7
18
|
// Store the theming state in __themeState__ global scope for reuse in the case of duplicate
|
|
8
19
|
// load-themed-styles hosted on the page.
|
|
9
|
-
|
|
20
|
+
var _root = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
21
|
// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).
|
|
11
|
-
|
|
12
|
-
|
|
22
|
+
var _styleNonce = _root && _root.CSPSettings && _root.CSPSettings.nonce;
|
|
23
|
+
var _themeState = initializeThemeState();
|
|
13
24
|
/**
|
|
14
25
|
* Matches theming tokens. For example, "[theme: themeSlotName, default: #FFF]" (including the quotes).
|
|
15
26
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
27
|
+
var _themeTokenRegex = /[\'\"]\[theme:\s*(\w+)\s*(?:\,\s*default:\s*([\\"\']?[\.\,\(\)\#\-\s\w]*[\.\,\(\)\#\-\w][\"\']?))?\s*\][\'\"]/g;
|
|
28
|
+
var now = function () {
|
|
29
|
+
return typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();
|
|
30
|
+
};
|
|
18
31
|
function measure(func) {
|
|
19
|
-
|
|
32
|
+
var start = now();
|
|
20
33
|
func();
|
|
21
|
-
|
|
34
|
+
var end = now();
|
|
22
35
|
_themeState.perf.duration += end - start;
|
|
23
36
|
}
|
|
24
37
|
/**
|
|
25
38
|
* initialize global state object
|
|
26
39
|
*/
|
|
27
40
|
function initializeThemeState() {
|
|
28
|
-
|
|
41
|
+
var state = _root.__themeState__ || {
|
|
29
42
|
theme: undefined,
|
|
30
43
|
lastStyleElement: undefined,
|
|
31
44
|
registeredStyles: []
|
|
32
45
|
};
|
|
33
46
|
if (!state.runState) {
|
|
34
|
-
state =
|
|
47
|
+
state = __assign(__assign({}, state), { perf: {
|
|
35
48
|
count: 0,
|
|
36
49
|
duration: 0
|
|
37
50
|
}, runState: {
|
|
@@ -41,7 +54,7 @@ define(["require", "exports"], function (require, exports) {
|
|
|
41
54
|
} });
|
|
42
55
|
}
|
|
43
56
|
if (!state.registeredThemableStyles) {
|
|
44
|
-
state =
|
|
57
|
+
state = __assign(__assign({}, state), { registeredThemableStyles: [] });
|
|
45
58
|
}
|
|
46
59
|
_root.__themeState__ = state;
|
|
47
60
|
return state;
|
|
@@ -52,10 +65,11 @@ define(["require", "exports"], function (require, exports) {
|
|
|
52
65
|
* @param {string | ThemableArray} styles Themable style text to register.
|
|
53
66
|
* @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.
|
|
54
67
|
*/
|
|
55
|
-
function loadStyles(styles, loadAsync
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
function loadStyles(styles, loadAsync) {
|
|
69
|
+
if (loadAsync === void 0) { loadAsync = false; }
|
|
70
|
+
measure(function () {
|
|
71
|
+
var styleParts = Array.isArray(styles) ? styles : splitStyles(styles);
|
|
72
|
+
var _a = _themeState.runState, mode = _a.mode, buffer = _a.buffer, flushTimer = _a.flushTimer;
|
|
59
73
|
if (loadAsync || mode === 1 /* Mode.async */) {
|
|
60
74
|
buffer.push(styleParts);
|
|
61
75
|
if (!flushTimer) {
|
|
@@ -89,10 +103,10 @@ define(["require", "exports"], function (require, exports) {
|
|
|
89
103
|
* external code can call flush to synchronously force processing of currently buffered styles
|
|
90
104
|
*/
|
|
91
105
|
function flush() {
|
|
92
|
-
measure(()
|
|
93
|
-
|
|
106
|
+
measure(function () {
|
|
107
|
+
var styleArrays = _themeState.runState.buffer.slice();
|
|
94
108
|
_themeState.runState.buffer = [];
|
|
95
|
-
|
|
109
|
+
var mergedStyleArray = [].concat.apply([], styleArrays);
|
|
96
110
|
if (mergedStyleArray.length > 0) {
|
|
97
111
|
applyThemableStyles(mergedStyleArray);
|
|
98
112
|
}
|
|
@@ -105,7 +119,7 @@ define(["require", "exports"], function (require, exports) {
|
|
|
105
119
|
function asyncLoadStyles() {
|
|
106
120
|
// Use "self" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node
|
|
107
121
|
// https://github.com/jestjs/jest/issues/14418
|
|
108
|
-
return self.setTimeout(()
|
|
122
|
+
return self.setTimeout(function () {
|
|
109
123
|
_themeState.runState.flushTimer = 0;
|
|
110
124
|
flush();
|
|
111
125
|
}, 0);
|
|
@@ -140,7 +154,8 @@ define(["require", "exports"], function (require, exports) {
|
|
|
140
154
|
* @param option - specify which group of registered styles should be cleared.
|
|
141
155
|
* Default to be both themable and non-themable styles will be cleared
|
|
142
156
|
*/
|
|
143
|
-
function clearStyles(option
|
|
157
|
+
function clearStyles(option) {
|
|
158
|
+
if (option === void 0) { option = 3 /* ClearStyleOptions.all */; }
|
|
144
159
|
if (option === 3 /* ClearStyleOptions.all */ || option === 2 /* ClearStyleOptions.onlyNonThemable */) {
|
|
145
160
|
clearStylesInternal(_themeState.registeredStyles);
|
|
146
161
|
_themeState.registeredStyles = [];
|
|
@@ -152,8 +167,8 @@ define(["require", "exports"], function (require, exports) {
|
|
|
152
167
|
}
|
|
153
168
|
exports.clearStyles = clearStyles;
|
|
154
169
|
function clearStylesInternal(records) {
|
|
155
|
-
records.forEach((styleRecord)
|
|
156
|
-
|
|
170
|
+
records.forEach(function (styleRecord) {
|
|
171
|
+
var styleElement = styleRecord && styleRecord.styleElement;
|
|
157
172
|
if (styleElement && styleElement.parentElement) {
|
|
158
173
|
styleElement.parentElement.removeChild(styleElement);
|
|
159
174
|
}
|
|
@@ -164,8 +179,9 @@ define(["require", "exports"], function (require, exports) {
|
|
|
164
179
|
*/
|
|
165
180
|
function reloadStyles() {
|
|
166
181
|
if (_themeState.theme) {
|
|
167
|
-
|
|
168
|
-
for (
|
|
182
|
+
var themableStyles = [];
|
|
183
|
+
for (var _i = 0, _a = _themeState.registeredThemableStyles; _i < _a.length; _i++) {
|
|
184
|
+
var styleRecord = _a[_i];
|
|
169
185
|
themableStyles.push(styleRecord.themableStyle);
|
|
170
186
|
}
|
|
171
187
|
if (themableStyles.length > 0) {
|
|
@@ -190,17 +206,17 @@ define(["require", "exports"], function (require, exports) {
|
|
|
190
206
|
* @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.
|
|
191
207
|
*/
|
|
192
208
|
function resolveThemableArray(splitStyleArray) {
|
|
193
|
-
|
|
194
|
-
|
|
209
|
+
var theme = _themeState.theme;
|
|
210
|
+
var themable = false;
|
|
195
211
|
// Resolve the array of theming instructions to an array of strings.
|
|
196
212
|
// Then join the array to produce the final CSS string.
|
|
197
|
-
|
|
198
|
-
|
|
213
|
+
var resolvedArray = (splitStyleArray || []).map(function (currentValue) {
|
|
214
|
+
var themeSlot = currentValue.theme;
|
|
199
215
|
if (themeSlot) {
|
|
200
216
|
themable = true;
|
|
201
217
|
// A theming annotation. Resolve it.
|
|
202
|
-
|
|
203
|
-
|
|
218
|
+
var themedValue = theme ? theme[themeSlot] : undefined;
|
|
219
|
+
var defaultValue = currentValue.defaultValue || 'inherit';
|
|
204
220
|
// Warn to console if we hit an unthemed value even when themes are provided, but only if "DEBUG" is true.
|
|
205
221
|
// Allow the themedValue to be undefined to explicitly request the default value.
|
|
206
222
|
if (theme &&
|
|
@@ -209,7 +225,7 @@ define(["require", "exports"], function (require, exports) {
|
|
|
209
225
|
!(themeSlot in theme) &&
|
|
210
226
|
typeof DEBUG !== 'undefined' &&
|
|
211
227
|
DEBUG) {
|
|
212
|
-
console.warn(
|
|
228
|
+
console.warn("Theming value not provided for \"".concat(themeSlot, "\". Falling back to \"").concat(defaultValue, "\"."));
|
|
213
229
|
}
|
|
214
230
|
return themedValue || defaultValue;
|
|
215
231
|
}
|
|
@@ -228,12 +244,12 @@ define(["require", "exports"], function (require, exports) {
|
|
|
228
244
|
* @param {string} styles Tokenized styles to split.
|
|
229
245
|
*/
|
|
230
246
|
function splitStyles(styles) {
|
|
231
|
-
|
|
247
|
+
var result = [];
|
|
232
248
|
if (styles) {
|
|
233
|
-
|
|
234
|
-
|
|
249
|
+
var pos = 0; // Current position in styles.
|
|
250
|
+
var tokenMatch = void 0;
|
|
235
251
|
while ((tokenMatch = _themeTokenRegex.exec(styles))) {
|
|
236
|
-
|
|
252
|
+
var matchIndex = tokenMatch.index;
|
|
237
253
|
if (matchIndex > pos) {
|
|
238
254
|
result.push({
|
|
239
255
|
rawString: styles.substring(pos, matchIndex)
|
|
@@ -264,9 +280,9 @@ define(["require", "exports"], function (require, exports) {
|
|
|
264
280
|
if (typeof document === 'undefined') {
|
|
265
281
|
return;
|
|
266
282
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
283
|
+
var head = document.getElementsByTagName('head')[0];
|
|
284
|
+
var styleElement = document.createElement('style');
|
|
285
|
+
var _a = resolveThemableArray(styleArray), styleString = _a.styleString, themable = _a.themable;
|
|
270
286
|
styleElement.setAttribute('data-load-themed-styles', 'true');
|
|
271
287
|
if (_styleNonce) {
|
|
272
288
|
styleElement.setAttribute('nonce', _styleNonce);
|
|
@@ -274,13 +290,13 @@ define(["require", "exports"], function (require, exports) {
|
|
|
274
290
|
styleElement.appendChild(document.createTextNode(styleString));
|
|
275
291
|
_themeState.perf.count++;
|
|
276
292
|
head.appendChild(styleElement);
|
|
277
|
-
|
|
293
|
+
var ev = document.createEvent('HTMLEvents');
|
|
278
294
|
ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);
|
|
279
295
|
ev.args = {
|
|
280
296
|
newStyle: styleElement
|
|
281
297
|
};
|
|
282
298
|
document.dispatchEvent(ev);
|
|
283
|
-
|
|
299
|
+
var record = {
|
|
284
300
|
styleElement: styleElement,
|
|
285
301
|
themableStyle: styleArray
|
|
286
302
|
};
|
package/lib-amd/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;;;;;IAyG3D,4FAA4F;IAC5F,yCAAyC;IACzC,MAAM,KAAK,GAAQ,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,yDAAyD;IAE7H,yGAAyG;IACzG,MAAM,WAAW,GAAW,KAAK,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC;IAElF,MAAM,WAAW,GAAgB,oBAAoB,EAAE,CAAC;IAExD;;OAEG;IACH,MAAM,gBAAgB,GACpB,gHAAgH,CAAC;IAEnH,MAAM,GAAG,GAAiB,GAAG,EAAE,CAC7B,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c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * An IThemingInstruction can specify a rawString to be preserved or a theme slot and a default value\n * to use if that slot is not specified by the theme.\n */\n\n/* eslint-disable @typescript-eslint/no-use-before-define */\n\n// Declaring a global here in case that the execution environment is Node.js (without importing the\n// entire node.js d.ts for now)\ndeclare let global: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\nexport interface IThemingInstruction {\n theme?: string;\n defaultValue?: string;\n rawString?: string;\n}\n\nexport type ThemableArray = IThemingInstruction[];\n\nexport interface ITheme {\n [key: string]: string;\n}\n\ninterface IStyleSheet {\n cssText: string;\n}\n\ninterface IExtendedHtmlStyleElement extends HTMLStyleElement {\n styleSheet: IStyleSheet;\n}\n\n/**\n * Performance Measurement of loading styles\n */\ninterface IMeasurement {\n /**\n * Count of style element injected, which is the slow operation in IE\n */\n count: number;\n /**\n * Total duration of all loadStyles exections\n */\n duration: number;\n}\n\ninterface IRunState {\n mode: Mode;\n buffer: ThemableArray[];\n flushTimer: number;\n}\n\ninterface IThemeState {\n theme: ITheme | undefined;\n lastStyleElement: IExtendedHtmlStyleElement;\n registeredStyles: IStyleRecord[]; // records of already registered non-themable styles\n registeredThemableStyles: IStyleRecord[]; // records of already registered themable styles\n loadStyles: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined;\n perf: IMeasurement;\n runState: IRunState;\n}\n\ninterface IStyleRecord {\n styleElement: Element;\n themableStyle: ThemableArray;\n}\n\ninterface ICustomEvent<T> extends Event {\n args?: T;\n}\n\n/**\n * object returned from resolveThemableArray function\n */\ninterface IThemableArrayResolveResult {\n /** this string is the processed styles in string */\n styleString: string;\n\n /** this boolean indicates if this style array is themable */\n themable: boolean;\n}\n\n/**\n * In sync mode, styles are registered as style elements synchronously with loadStyles() call.\n * In async mode, styles are buffered and registered as batch in async timer for performance purpose.\n */\nexport const enum Mode {\n sync,\n async\n}\n\n/**\n * Themable styles and non-themable styles are tracked separately\n * Specify ClearStyleOptions when calling clearStyles API to specify which group of registered styles should be cleared.\n */\nexport const enum ClearStyleOptions {\n /** only themable styles will be cleared */\n onlyThemable = 1,\n /** only non-themable styles will be cleared */\n onlyNonThemable = 2,\n /** both themable and non-themable styles will be cleared */\n all = 3\n}\n\n// Store the theming state in __themeState__ global scope for reuse in the case of duplicate\n// load-themed-styles hosted on the page.\nconst _root: any = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).\nconst _styleNonce: string = _root && _root.CSPSettings && _root.CSPSettings.nonce;\n\nconst _themeState: IThemeState = initializeThemeState();\n\n/**\n * Matches theming tokens. For example, \"[theme: themeSlotName, default: #FFF]\" (including the quotes).\n */\nconst _themeTokenRegex: RegExp =\n /[\\'\\\"]\\[theme:\\s*(\\w+)\\s*(?:\\,\\s*default:\\s*([\\\\\"\\']?[\\.\\,\\(\\)\\#\\-\\s\\w]*[\\.\\,\\(\\)\\#\\-\\w][\\\"\\']?))?\\s*\\][\\'\\\"]/g;\n\nconst now: () => number = () =>\n typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();\n\nfunction measure(func: () => void): void {\n const start: number = now();\n func();\n const end: number = now();\n _themeState.perf.duration += end - start;\n}\n\n/**\n * initialize global state object\n */\nfunction initializeThemeState(): IThemeState {\n let state: IThemeState = _root.__themeState__ || {\n theme: undefined,\n lastStyleElement: undefined,\n registeredStyles: []\n };\n\n if (!state.runState) {\n state = {\n ...state,\n perf: {\n count: 0,\n duration: 0\n },\n runState: {\n flushTimer: 0,\n mode: Mode.sync,\n buffer: []\n }\n };\n }\n if (!state.registeredThemableStyles) {\n state = {\n ...state,\n registeredThemableStyles: []\n };\n }\n _root.__themeState__ = state;\n return state;\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load\n * event is fired.\n * @param {string | ThemableArray} styles Themable style text to register.\n * @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.\n */\nexport function loadStyles(styles: string | ThemableArray, loadAsync: boolean = false): void {\n measure(() => {\n const styleParts: ThemableArray = Array.isArray(styles) ? styles : splitStyles(styles);\n const { mode, buffer, flushTimer } = _themeState.runState;\n if (loadAsync || mode === Mode.async) {\n buffer.push(styleParts);\n if (!flushTimer) {\n _themeState.runState.flushTimer = asyncLoadStyles();\n }\n } else {\n applyThemableStyles(styleParts);\n }\n });\n}\n\n/**\n * Allows for customizable loadStyles logic. e.g. for server side rendering application\n * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void}\n * a loadStyles callback that gets called when styles are loaded or reloaded\n */\nexport function configureLoadStyles(\n loadStylesFn: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined\n): void {\n _themeState.loadStyles = loadStylesFn;\n}\n\n/**\n * Configure run mode of load-themable-styles\n * @param mode load-themable-styles run mode, async or sync\n */\nexport function configureRunMode(mode: Mode): void {\n _themeState.runState.mode = mode;\n}\n\n/**\n * external code can call flush to synchronously force processing of currently buffered styles\n */\nexport function flush(): void {\n measure(() => {\n const styleArrays: ThemableArray[] = _themeState.runState.buffer.slice();\n _themeState.runState.buffer = [];\n const mergedStyleArray: ThemableArray = ([] as ThemableArray).concat.apply([], styleArrays);\n if (mergedStyleArray.length > 0) {\n applyThemableStyles(mergedStyleArray);\n }\n });\n}\n\n/**\n * register async loadStyles\n */\nfunction asyncLoadStyles(): number {\n // Use \"self\" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node\n // https://github.com/jestjs/jest/issues/14418\n return self.setTimeout(() => {\n _themeState.runState.flushTimer = 0;\n flush();\n }, 0);\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load event\n * is fired.\n * @param {string} styleText Style to register.\n * @param {IStyleRecord} styleRecord Existing style record to re-apply.\n */\nfunction applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord): void {\n if (_themeState.loadStyles) {\n _themeState.loadStyles(resolveThemableArray(stylesArray).styleString, stylesArray);\n } else {\n registerStyles(stylesArray);\n }\n}\n\n/**\n * Registers a set theme tokens to find and replace. If styles were already registered, they will be\n * replaced.\n * @param {theme} theme JSON object of theme tokens to values.\n */\nexport function loadTheme(theme: ITheme | undefined): void {\n _themeState.theme = theme;\n\n // reload styles.\n reloadStyles();\n}\n\n/**\n * Clear already registered style elements and style records in theme_State object\n * @param option - specify which group of registered styles should be cleared.\n * Default to be both themable and non-themable styles will be cleared\n */\nexport function clearStyles(option: ClearStyleOptions = ClearStyleOptions.all): void {\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyNonThemable) {\n clearStylesInternal(_themeState.registeredStyles);\n _themeState.registeredStyles = [];\n }\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyThemable) {\n clearStylesInternal(_themeState.registeredThemableStyles);\n _themeState.registeredThemableStyles = [];\n }\n}\n\nfunction clearStylesInternal(records: IStyleRecord[]): void {\n records.forEach((styleRecord: IStyleRecord) => {\n const styleElement: HTMLStyleElement = styleRecord && (styleRecord.styleElement as HTMLStyleElement);\n if (styleElement && styleElement.parentElement) {\n styleElement.parentElement.removeChild(styleElement);\n }\n });\n}\n\n/**\n * Reloads styles.\n */\nfunction reloadStyles(): void {\n if (_themeState.theme) {\n const themableStyles: ThemableArray[] = [];\n for (const styleRecord of _themeState.registeredThemableStyles) {\n themableStyles.push(styleRecord.themableStyle);\n }\n if (themableStyles.length > 0) {\n clearStyles(ClearStyleOptions.onlyThemable);\n applyThemableStyles(([] as ThemableArray).concat.apply([], themableStyles));\n }\n }\n}\n\n/**\n * Find theme tokens and replaces them with provided theme values.\n * @param {string} styles Tokenized styles to fix.\n */\nexport function detokenize(styles: string | undefined): string | undefined {\n if (styles) {\n styles = resolveThemableArray(splitStyles(styles)).styleString;\n }\n\n return styles;\n}\n\n/**\n * Resolves ThemingInstruction objects in an array and joins the result into a string.\n * @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.\n */\nfunction resolveThemableArray(splitStyleArray: ThemableArray): IThemableArrayResolveResult {\n const { theme }: IThemeState = _themeState;\n let themable: boolean = false;\n // Resolve the array of theming instructions to an array of strings.\n // Then join the array to produce the final CSS string.\n const resolvedArray: (string | undefined)[] = (splitStyleArray || []).map(\n (currentValue: IThemingInstruction) => {\n const themeSlot: string | undefined = currentValue.theme;\n if (themeSlot) {\n themable = true;\n // A theming annotation. Resolve it.\n const themedValue: string | undefined = theme ? theme[themeSlot] : undefined;\n const defaultValue: string = currentValue.defaultValue || 'inherit';\n\n // Warn to console if we hit an unthemed value even when themes are provided, but only if \"DEBUG\" is true.\n // Allow the themedValue to be undefined to explicitly request the default value.\n if (\n theme &&\n !themedValue &&\n console &&\n !(themeSlot in theme) &&\n typeof DEBUG !== 'undefined' &&\n DEBUG\n ) {\n console.warn(`Theming value not provided for \"${themeSlot}\". Falling back to \"${defaultValue}\".`);\n }\n\n return themedValue || defaultValue;\n } else {\n // A non-themable string. Preserve it.\n return currentValue.rawString;\n }\n }\n );\n\n return {\n styleString: resolvedArray.join(''),\n themable: themable\n };\n}\n\n/**\n * Split tokenized CSS into an array of strings and theme specification objects\n * @param {string} styles Tokenized styles to split.\n */\nexport function splitStyles(styles: string): ThemableArray {\n const result: ThemableArray = [];\n if (styles) {\n let pos: number = 0; // Current position in styles.\n let tokenMatch: RegExpExecArray | null;\n while ((tokenMatch = _themeTokenRegex.exec(styles))) {\n const matchIndex: number = tokenMatch.index;\n if (matchIndex > pos) {\n result.push({\n rawString: styles.substring(pos, matchIndex)\n });\n }\n\n result.push({\n theme: tokenMatch[1],\n defaultValue: tokenMatch[2] // May be undefined\n });\n\n // index of the first character after the current match\n pos = _themeTokenRegex.lastIndex;\n }\n\n // Push the rest of the string after the last match.\n result.push({\n rawString: styles.substring(pos)\n });\n }\n\n return result;\n}\n\n/**\n * Registers a set of style text. If it is registered too early, we will register it when the\n * window.load event is fired.\n * @param {ThemableArray} styleArray Array of IThemingInstruction objects to register.\n * @param {IStyleRecord} styleRecord May specify a style Element to update.\n */\nfunction registerStyles(styleArray: ThemableArray): void {\n if (typeof document === 'undefined') {\n return;\n }\n const head: HTMLHeadElement = document.getElementsByTagName('head')[0];\n const styleElement: HTMLStyleElement = document.createElement('style');\n const { styleString, themable } = resolveThemableArray(styleArray);\n\n styleElement.setAttribute('data-load-themed-styles', 'true');\n if (_styleNonce) {\n styleElement.setAttribute('nonce', _styleNonce);\n }\n styleElement.appendChild(document.createTextNode(styleString));\n _themeState.perf.count++;\n head.appendChild(styleElement);\n\n const ev: ICustomEvent<{ newStyle: HTMLStyleElement }> = document.createEvent('HTMLEvents');\n ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);\n ev.args = {\n newStyle: styleElement\n };\n document.dispatchEvent(ev);\n\n const record: IStyleRecord = {\n styleElement: styleElement,\n themableStyle: styleArray\n };\n\n if (themable) {\n _themeState.registeredThemableStyles.push(record);\n } else {\n _themeState.registeredStyles.push(record);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;IAyG3D,4FAA4F;IAC5F,yCAAyC;IACzC,IAAM,KAAK,GAAQ,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,yDAAyD;IAE7H,yGAAyG;IACzG,IAAM,WAAW,GAAW,KAAK,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC;IAElF,IAAM,WAAW,GAAgB,oBAAoB,EAAE,CAAC;IAExD;;OAEG;IACH,IAAM,gBAAgB,GACpB,gHAAgH,CAAC;IAEnH,IAAM,GAAG,GAAiB;QACxB,OAAA,OAAO,WAAW,KAAK,WAAW,IAAI,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC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E in the project root for license information.\n\n/**\n * An IThemingInstruction can specify a rawString to be preserved or a theme slot and a default value\n * to use if that slot is not specified by the theme.\n */\n\n/* eslint-disable @typescript-eslint/no-use-before-define */\n\n// Declaring a global here in case that the execution environment is Node.js (without importing the\n// entire node.js d.ts for now)\ndeclare let global: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\nexport interface IThemingInstruction {\n theme?: string;\n defaultValue?: string;\n rawString?: string;\n}\n\nexport type ThemableArray = IThemingInstruction[];\n\nexport interface ITheme {\n [key: string]: string;\n}\n\ninterface IStyleSheet {\n cssText: string;\n}\n\ninterface IExtendedHtmlStyleElement extends HTMLStyleElement {\n styleSheet: IStyleSheet;\n}\n\n/**\n * Performance Measurement of loading styles\n */\ninterface IMeasurement {\n /**\n * Count of style element injected, which is the slow operation in IE\n */\n count: number;\n /**\n * Total duration of all loadStyles exections\n */\n duration: number;\n}\n\ninterface IRunState {\n mode: Mode;\n buffer: ThemableArray[];\n flushTimer: number;\n}\n\ninterface IThemeState {\n theme: ITheme | undefined;\n lastStyleElement: IExtendedHtmlStyleElement;\n registeredStyles: IStyleRecord[]; // records of already registered non-themable styles\n registeredThemableStyles: IStyleRecord[]; // records of already registered themable styles\n loadStyles: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined;\n perf: IMeasurement;\n runState: IRunState;\n}\n\ninterface IStyleRecord {\n styleElement: Element;\n themableStyle: ThemableArray;\n}\n\ninterface ICustomEvent<T> extends Event {\n args?: T;\n}\n\n/**\n * object returned from resolveThemableArray function\n */\ninterface IThemableArrayResolveResult {\n /** this string is the processed styles in string */\n styleString: string;\n\n /** this boolean indicates if this style array is themable */\n themable: boolean;\n}\n\n/**\n * In sync mode, styles are registered as style elements synchronously with loadStyles() call.\n * In async mode, styles are buffered and registered as batch in async timer for performance purpose.\n */\nexport const enum Mode {\n sync,\n async\n}\n\n/**\n * Themable styles and non-themable styles are tracked separately\n * Specify ClearStyleOptions when calling clearStyles API to specify which group of registered styles should be cleared.\n */\nexport const enum ClearStyleOptions {\n /** only themable styles will be cleared */\n onlyThemable = 1,\n /** only non-themable styles will be cleared */\n onlyNonThemable = 2,\n /** both themable and non-themable styles will be cleared */\n all = 3\n}\n\n// Store the theming state in __themeState__ global scope for reuse in the case of duplicate\n// load-themed-styles hosted on the page.\nconst _root: any = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).\nconst _styleNonce: string = _root && _root.CSPSettings && _root.CSPSettings.nonce;\n\nconst _themeState: IThemeState = initializeThemeState();\n\n/**\n * Matches theming tokens. For example, \"[theme: themeSlotName, default: #FFF]\" (including the quotes).\n */\nconst _themeTokenRegex: RegExp =\n /[\\'\\\"]\\[theme:\\s*(\\w+)\\s*(?:\\,\\s*default:\\s*([\\\\\"\\']?[\\.\\,\\(\\)\\#\\-\\s\\w]*[\\.\\,\\(\\)\\#\\-\\w][\\\"\\']?))?\\s*\\][\\'\\\"]/g;\n\nconst now: () => number = () =>\n typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();\n\nfunction measure(func: () => void): void {\n const start: number = now();\n func();\n const end: number = now();\n _themeState.perf.duration += end - start;\n}\n\n/**\n * initialize global state object\n */\nfunction initializeThemeState(): IThemeState {\n let state: IThemeState = _root.__themeState__ || {\n theme: undefined,\n lastStyleElement: undefined,\n registeredStyles: []\n };\n\n if (!state.runState) {\n state = {\n ...state,\n perf: {\n count: 0,\n duration: 0\n },\n runState: {\n flushTimer: 0,\n mode: Mode.sync,\n buffer: []\n }\n };\n }\n if (!state.registeredThemableStyles) {\n state = {\n ...state,\n registeredThemableStyles: []\n };\n }\n _root.__themeState__ = state;\n return state;\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load\n * event is fired.\n * @param {string | ThemableArray} styles Themable style text to register.\n * @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.\n */\nexport function loadStyles(styles: string | ThemableArray, loadAsync: boolean = false): void {\n measure(() => {\n const styleParts: ThemableArray = Array.isArray(styles) ? styles : splitStyles(styles);\n const { mode, buffer, flushTimer } = _themeState.runState;\n if (loadAsync || mode === Mode.async) {\n buffer.push(styleParts);\n if (!flushTimer) {\n _themeState.runState.flushTimer = asyncLoadStyles();\n }\n } else {\n applyThemableStyles(styleParts);\n }\n });\n}\n\n/**\n * Allows for customizable loadStyles logic. e.g. for server side rendering application\n * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void}\n * a loadStyles callback that gets called when styles are loaded or reloaded\n */\nexport function configureLoadStyles(\n loadStylesFn: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined\n): void {\n _themeState.loadStyles = loadStylesFn;\n}\n\n/**\n * Configure run mode of load-themable-styles\n * @param mode load-themable-styles run mode, async or sync\n */\nexport function configureRunMode(mode: Mode): void {\n _themeState.runState.mode = mode;\n}\n\n/**\n * external code can call flush to synchronously force processing of currently buffered styles\n */\nexport function flush(): void {\n measure(() => {\n const styleArrays: ThemableArray[] = _themeState.runState.buffer.slice();\n _themeState.runState.buffer = [];\n const mergedStyleArray: ThemableArray = ([] as ThemableArray).concat.apply([], styleArrays);\n if (mergedStyleArray.length > 0) {\n applyThemableStyles(mergedStyleArray);\n }\n });\n}\n\n/**\n * register async loadStyles\n */\nfunction asyncLoadStyles(): number {\n // Use \"self\" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node\n // https://github.com/jestjs/jest/issues/14418\n return self.setTimeout(() => {\n _themeState.runState.flushTimer = 0;\n flush();\n }, 0);\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load event\n * is fired.\n * @param {string} styleText Style to register.\n * @param {IStyleRecord} styleRecord Existing style record to re-apply.\n */\nfunction applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord): void {\n if (_themeState.loadStyles) {\n _themeState.loadStyles(resolveThemableArray(stylesArray).styleString, stylesArray);\n } else {\n registerStyles(stylesArray);\n }\n}\n\n/**\n * Registers a set theme tokens to find and replace. If styles were already registered, they will be\n * replaced.\n * @param {theme} theme JSON object of theme tokens to values.\n */\nexport function loadTheme(theme: ITheme | undefined): void {\n _themeState.theme = theme;\n\n // reload styles.\n reloadStyles();\n}\n\n/**\n * Clear already registered style elements and style records in theme_State object\n * @param option - specify which group of registered styles should be cleared.\n * Default to be both themable and non-themable styles will be cleared\n */\nexport function clearStyles(option: ClearStyleOptions = ClearStyleOptions.all): void {\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyNonThemable) {\n clearStylesInternal(_themeState.registeredStyles);\n _themeState.registeredStyles = [];\n }\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyThemable) {\n clearStylesInternal(_themeState.registeredThemableStyles);\n _themeState.registeredThemableStyles = [];\n }\n}\n\nfunction clearStylesInternal(records: IStyleRecord[]): void {\n records.forEach((styleRecord: IStyleRecord) => {\n const styleElement: HTMLStyleElement = styleRecord && (styleRecord.styleElement as HTMLStyleElement);\n if (styleElement && styleElement.parentElement) {\n styleElement.parentElement.removeChild(styleElement);\n }\n });\n}\n\n/**\n * Reloads styles.\n */\nfunction reloadStyles(): void {\n if (_themeState.theme) {\n const themableStyles: ThemableArray[] = [];\n for (const styleRecord of _themeState.registeredThemableStyles) {\n themableStyles.push(styleRecord.themableStyle);\n }\n if (themableStyles.length > 0) {\n clearStyles(ClearStyleOptions.onlyThemable);\n applyThemableStyles(([] as ThemableArray).concat.apply([], themableStyles));\n }\n }\n}\n\n/**\n * Find theme tokens and replaces them with provided theme values.\n * @param {string} styles Tokenized styles to fix.\n */\nexport function detokenize(styles: string | undefined): string | undefined {\n if (styles) {\n styles = resolveThemableArray(splitStyles(styles)).styleString;\n }\n\n return styles;\n}\n\n/**\n * Resolves ThemingInstruction objects in an array and joins the result into a string.\n * @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.\n */\nfunction resolveThemableArray(splitStyleArray: ThemableArray): IThemableArrayResolveResult {\n const { theme }: IThemeState = _themeState;\n let themable: boolean = false;\n // Resolve the array of theming instructions to an array of strings.\n // Then join the array to produce the final CSS string.\n const resolvedArray: (string | undefined)[] = (splitStyleArray || []).map(\n (currentValue: IThemingInstruction) => {\n const themeSlot: string | undefined = currentValue.theme;\n if (themeSlot) {\n themable = true;\n // A theming annotation. Resolve it.\n const themedValue: string | undefined = theme ? theme[themeSlot] : undefined;\n const defaultValue: string = currentValue.defaultValue || 'inherit';\n\n // Warn to console if we hit an unthemed value even when themes are provided, but only if \"DEBUG\" is true.\n // Allow the themedValue to be undefined to explicitly request the default value.\n if (\n theme &&\n !themedValue &&\n console &&\n !(themeSlot in theme) &&\n typeof DEBUG !== 'undefined' &&\n DEBUG\n ) {\n console.warn(`Theming value not provided for \"${themeSlot}\". Falling back to \"${defaultValue}\".`);\n }\n\n return themedValue || defaultValue;\n } else {\n // A non-themable string. Preserve it.\n return currentValue.rawString;\n }\n }\n );\n\n return {\n styleString: resolvedArray.join(''),\n themable: themable\n };\n}\n\n/**\n * Split tokenized CSS into an array of strings and theme specification objects\n * @param {string} styles Tokenized styles to split.\n */\nexport function splitStyles(styles: string): ThemableArray {\n const result: ThemableArray = [];\n if (styles) {\n let pos: number = 0; // Current position in styles.\n let tokenMatch: RegExpExecArray | null;\n while ((tokenMatch = _themeTokenRegex.exec(styles))) {\n const matchIndex: number = tokenMatch.index;\n if (matchIndex > pos) {\n result.push({\n rawString: styles.substring(pos, matchIndex)\n });\n }\n\n result.push({\n theme: tokenMatch[1],\n defaultValue: tokenMatch[2] // May be undefined\n });\n\n // index of the first character after the current match\n pos = _themeTokenRegex.lastIndex;\n }\n\n // Push the rest of the string after the last match.\n result.push({\n rawString: styles.substring(pos)\n });\n }\n\n return result;\n}\n\n/**\n * Registers a set of style text. If it is registered too early, we will register it when the\n * window.load event is fired.\n * @param {ThemableArray} styleArray Array of IThemingInstruction objects to register.\n * @param {IStyleRecord} styleRecord May specify a style Element to update.\n */\nfunction registerStyles(styleArray: ThemableArray): void {\n if (typeof document === 'undefined') {\n return;\n }\n const head: HTMLHeadElement = document.getElementsByTagName('head')[0];\n const styleElement: HTMLStyleElement = document.createElement('style');\n const { styleString, themable } = resolveThemableArray(styleArray);\n\n styleElement.setAttribute('data-load-themed-styles', 'true');\n if (_styleNonce) {\n styleElement.setAttribute('nonce', _styleNonce);\n }\n styleElement.appendChild(document.createTextNode(styleString));\n _themeState.perf.count++;\n head.appendChild(styleElement);\n\n const ev: ICustomEvent<{ newStyle: HTMLStyleElement }> = document.createEvent('HTMLEvents');\n ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);\n ev.args = {\n newStyle: styleElement\n };\n document.dispatchEvent(ev);\n\n const record: IStyleRecord = {\n styleElement: styleElement,\n themableStyle: styleArray\n };\n\n if (themable) {\n _themeState.registeredThemableStyles.push(record);\n } else {\n _themeState.registeredStyles.push(record);\n }\n}\n"]}
|
package/lib-es6/index.js
CHANGED
|
@@ -1,33 +1,46 @@
|
|
|
1
1
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
2
|
// See LICENSE in the project root for license information.
|
|
3
|
+
var __assign = (this && this.__assign) || function () {
|
|
4
|
+
__assign = Object.assign || function(t) {
|
|
5
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
6
|
+
s = arguments[i];
|
|
7
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
8
|
+
t[p] = s[p];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
return __assign.apply(this, arguments);
|
|
13
|
+
};
|
|
3
14
|
// Store the theming state in __themeState__ global scope for reuse in the case of duplicate
|
|
4
15
|
// load-themed-styles hosted on the page.
|
|
5
|
-
|
|
16
|
+
var _root = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
6
17
|
// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).
|
|
7
|
-
|
|
8
|
-
|
|
18
|
+
var _styleNonce = _root && _root.CSPSettings && _root.CSPSettings.nonce;
|
|
19
|
+
var _themeState = initializeThemeState();
|
|
9
20
|
/**
|
|
10
21
|
* Matches theming tokens. For example, "[theme: themeSlotName, default: #FFF]" (including the quotes).
|
|
11
22
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
var _themeTokenRegex = /[\'\"]\[theme:\s*(\w+)\s*(?:\,\s*default:\s*([\\"\']?[\.\,\(\)\#\-\s\w]*[\.\,\(\)\#\-\w][\"\']?))?\s*\][\'\"]/g;
|
|
24
|
+
var now = function () {
|
|
25
|
+
return typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();
|
|
26
|
+
};
|
|
14
27
|
function measure(func) {
|
|
15
|
-
|
|
28
|
+
var start = now();
|
|
16
29
|
func();
|
|
17
|
-
|
|
30
|
+
var end = now();
|
|
18
31
|
_themeState.perf.duration += end - start;
|
|
19
32
|
}
|
|
20
33
|
/**
|
|
21
34
|
* initialize global state object
|
|
22
35
|
*/
|
|
23
36
|
function initializeThemeState() {
|
|
24
|
-
|
|
37
|
+
var state = _root.__themeState__ || {
|
|
25
38
|
theme: undefined,
|
|
26
39
|
lastStyleElement: undefined,
|
|
27
40
|
registeredStyles: []
|
|
28
41
|
};
|
|
29
42
|
if (!state.runState) {
|
|
30
|
-
state =
|
|
43
|
+
state = __assign(__assign({}, state), { perf: {
|
|
31
44
|
count: 0,
|
|
32
45
|
duration: 0
|
|
33
46
|
}, runState: {
|
|
@@ -37,7 +50,7 @@ function initializeThemeState() {
|
|
|
37
50
|
} });
|
|
38
51
|
}
|
|
39
52
|
if (!state.registeredThemableStyles) {
|
|
40
|
-
state =
|
|
53
|
+
state = __assign(__assign({}, state), { registeredThemableStyles: [] });
|
|
41
54
|
}
|
|
42
55
|
_root.__themeState__ = state;
|
|
43
56
|
return state;
|
|
@@ -48,10 +61,11 @@ function initializeThemeState() {
|
|
|
48
61
|
* @param {string | ThemableArray} styles Themable style text to register.
|
|
49
62
|
* @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.
|
|
50
63
|
*/
|
|
51
|
-
export function loadStyles(styles, loadAsync
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
64
|
+
export function loadStyles(styles, loadAsync) {
|
|
65
|
+
if (loadAsync === void 0) { loadAsync = false; }
|
|
66
|
+
measure(function () {
|
|
67
|
+
var styleParts = Array.isArray(styles) ? styles : splitStyles(styles);
|
|
68
|
+
var _a = _themeState.runState, mode = _a.mode, buffer = _a.buffer, flushTimer = _a.flushTimer;
|
|
55
69
|
if (loadAsync || mode === 1 /* Mode.async */) {
|
|
56
70
|
buffer.push(styleParts);
|
|
57
71
|
if (!flushTimer) {
|
|
@@ -82,10 +96,10 @@ export function configureRunMode(mode) {
|
|
|
82
96
|
* external code can call flush to synchronously force processing of currently buffered styles
|
|
83
97
|
*/
|
|
84
98
|
export function flush() {
|
|
85
|
-
measure(()
|
|
86
|
-
|
|
99
|
+
measure(function () {
|
|
100
|
+
var styleArrays = _themeState.runState.buffer.slice();
|
|
87
101
|
_themeState.runState.buffer = [];
|
|
88
|
-
|
|
102
|
+
var mergedStyleArray = [].concat.apply([], styleArrays);
|
|
89
103
|
if (mergedStyleArray.length > 0) {
|
|
90
104
|
applyThemableStyles(mergedStyleArray);
|
|
91
105
|
}
|
|
@@ -97,7 +111,7 @@ export function flush() {
|
|
|
97
111
|
function asyncLoadStyles() {
|
|
98
112
|
// Use "self" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node
|
|
99
113
|
// https://github.com/jestjs/jest/issues/14418
|
|
100
|
-
return self.setTimeout(()
|
|
114
|
+
return self.setTimeout(function () {
|
|
101
115
|
_themeState.runState.flushTimer = 0;
|
|
102
116
|
flush();
|
|
103
117
|
}, 0);
|
|
@@ -131,7 +145,8 @@ export function loadTheme(theme) {
|
|
|
131
145
|
* @param option - specify which group of registered styles should be cleared.
|
|
132
146
|
* Default to be both themable and non-themable styles will be cleared
|
|
133
147
|
*/
|
|
134
|
-
export function clearStyles(option
|
|
148
|
+
export function clearStyles(option) {
|
|
149
|
+
if (option === void 0) { option = 3 /* ClearStyleOptions.all */; }
|
|
135
150
|
if (option === 3 /* ClearStyleOptions.all */ || option === 2 /* ClearStyleOptions.onlyNonThemable */) {
|
|
136
151
|
clearStylesInternal(_themeState.registeredStyles);
|
|
137
152
|
_themeState.registeredStyles = [];
|
|
@@ -142,8 +157,8 @@ export function clearStyles(option = 3 /* ClearStyleOptions.all */) {
|
|
|
142
157
|
}
|
|
143
158
|
}
|
|
144
159
|
function clearStylesInternal(records) {
|
|
145
|
-
records.forEach((styleRecord)
|
|
146
|
-
|
|
160
|
+
records.forEach(function (styleRecord) {
|
|
161
|
+
var styleElement = styleRecord && styleRecord.styleElement;
|
|
147
162
|
if (styleElement && styleElement.parentElement) {
|
|
148
163
|
styleElement.parentElement.removeChild(styleElement);
|
|
149
164
|
}
|
|
@@ -154,8 +169,9 @@ function clearStylesInternal(records) {
|
|
|
154
169
|
*/
|
|
155
170
|
function reloadStyles() {
|
|
156
171
|
if (_themeState.theme) {
|
|
157
|
-
|
|
158
|
-
for (
|
|
172
|
+
var themableStyles = [];
|
|
173
|
+
for (var _i = 0, _a = _themeState.registeredThemableStyles; _i < _a.length; _i++) {
|
|
174
|
+
var styleRecord = _a[_i];
|
|
159
175
|
themableStyles.push(styleRecord.themableStyle);
|
|
160
176
|
}
|
|
161
177
|
if (themableStyles.length > 0) {
|
|
@@ -179,17 +195,17 @@ export function detokenize(styles) {
|
|
|
179
195
|
* @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.
|
|
180
196
|
*/
|
|
181
197
|
function resolveThemableArray(splitStyleArray) {
|
|
182
|
-
|
|
183
|
-
|
|
198
|
+
var theme = _themeState.theme;
|
|
199
|
+
var themable = false;
|
|
184
200
|
// Resolve the array of theming instructions to an array of strings.
|
|
185
201
|
// Then join the array to produce the final CSS string.
|
|
186
|
-
|
|
187
|
-
|
|
202
|
+
var resolvedArray = (splitStyleArray || []).map(function (currentValue) {
|
|
203
|
+
var themeSlot = currentValue.theme;
|
|
188
204
|
if (themeSlot) {
|
|
189
205
|
themable = true;
|
|
190
206
|
// A theming annotation. Resolve it.
|
|
191
|
-
|
|
192
|
-
|
|
207
|
+
var themedValue = theme ? theme[themeSlot] : undefined;
|
|
208
|
+
var defaultValue = currentValue.defaultValue || 'inherit';
|
|
193
209
|
// Warn to console if we hit an unthemed value even when themes are provided, but only if "DEBUG" is true.
|
|
194
210
|
// Allow the themedValue to be undefined to explicitly request the default value.
|
|
195
211
|
if (theme &&
|
|
@@ -198,7 +214,7 @@ function resolveThemableArray(splitStyleArray) {
|
|
|
198
214
|
!(themeSlot in theme) &&
|
|
199
215
|
typeof DEBUG !== 'undefined' &&
|
|
200
216
|
DEBUG) {
|
|
201
|
-
console.warn(
|
|
217
|
+
console.warn("Theming value not provided for \"".concat(themeSlot, "\". Falling back to \"").concat(defaultValue, "\"."));
|
|
202
218
|
}
|
|
203
219
|
return themedValue || defaultValue;
|
|
204
220
|
}
|
|
@@ -217,12 +233,12 @@ function resolveThemableArray(splitStyleArray) {
|
|
|
217
233
|
* @param {string} styles Tokenized styles to split.
|
|
218
234
|
*/
|
|
219
235
|
export function splitStyles(styles) {
|
|
220
|
-
|
|
236
|
+
var result = [];
|
|
221
237
|
if (styles) {
|
|
222
|
-
|
|
223
|
-
|
|
238
|
+
var pos = 0; // Current position in styles.
|
|
239
|
+
var tokenMatch = void 0;
|
|
224
240
|
while ((tokenMatch = _themeTokenRegex.exec(styles))) {
|
|
225
|
-
|
|
241
|
+
var matchIndex = tokenMatch.index;
|
|
226
242
|
if (matchIndex > pos) {
|
|
227
243
|
result.push({
|
|
228
244
|
rawString: styles.substring(pos, matchIndex)
|
|
@@ -252,9 +268,9 @@ function registerStyles(styleArray) {
|
|
|
252
268
|
if (typeof document === 'undefined') {
|
|
253
269
|
return;
|
|
254
270
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
271
|
+
var head = document.getElementsByTagName('head')[0];
|
|
272
|
+
var styleElement = document.createElement('style');
|
|
273
|
+
var _a = resolveThemableArray(styleArray), styleString = _a.styleString, themable = _a.themable;
|
|
258
274
|
styleElement.setAttribute('data-load-themed-styles', 'true');
|
|
259
275
|
if (_styleNonce) {
|
|
260
276
|
styleElement.setAttribute('nonce', _styleNonce);
|
|
@@ -262,13 +278,13 @@ function registerStyles(styleArray) {
|
|
|
262
278
|
styleElement.appendChild(document.createTextNode(styleString));
|
|
263
279
|
_themeState.perf.count++;
|
|
264
280
|
head.appendChild(styleElement);
|
|
265
|
-
|
|
281
|
+
var ev = document.createEvent('HTMLEvents');
|
|
266
282
|
ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);
|
|
267
283
|
ev.args = {
|
|
268
284
|
newStyle: styleElement
|
|
269
285
|
};
|
|
270
286
|
document.dispatchEvent(ev);
|
|
271
|
-
|
|
287
|
+
var record = {
|
|
272
288
|
styleElement: styleElement,
|
|
273
289
|
themableStyle: styleArray
|
|
274
290
|
};
|
package/lib-es6/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAyG3D,4FAA4F;AAC5F,yCAAyC;AACzC,MAAM,KAAK,GAAQ,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,yDAAyD;AAE7H,yGAAyG;AACzG,MAAM,WAAW,GAAW,KAAK,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC;AAElF,MAAM,WAAW,GAAgB,oBAAoB,EAAE,CAAC;AAExD;;GAEG;AACH,MAAM,gBAAgB,GACpB,gHAAgH,CAAC;AAEnH,MAAM,GAAG,GAAiB,GAAG,EAAE,CAC7B,OAAO,WAAW,KAAK,WAAW,IAAI,CAAC,CAAC,WAAW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CAAC,CAAC,CAAC,CAAC,CAAC;IACvE,MAAM,YAAY,GAAqB,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IACvE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,oBAAoB,CAAC,UAAU,CAAC,CAAC;IAEnE,YAAY,CAAC,YAAY,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;IAC7D,IAAI,WAAW,EAAE;QACf,YAAY,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;KACjD;IACD,YAAY,CAAC,WAAW,CAAC,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC;IAC/D,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACzB,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAE/B,MAAM,EAAE,GAAiD,QAAQ,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAC5F,EAAE,CAAC,SAAS,CAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAC5E,EAAE,CAAC,IAAI,GAAG;QACR,QAAQ,EAAE,YAAY;KACvB,CAAC;IACF,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;IAE3B,MAAM,MAAM,GAAiB;QAC3B,YAAY,EAAE,YAAY;QAC1B,aAAa,EAAE,UAAU;KAC1B,CAAC;IAEF,IAAI,QAAQ,EAAE;QACZ,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KACnD;SAAM;QACL,WAAW,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KAC3C;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * An IThemingInstruction can specify a rawString to be preserved or a theme slot and a default value\n * to use if that slot is not specified by the theme.\n */\n\n/* eslint-disable @typescript-eslint/no-use-before-define */\n\n// Declaring a global here in case that the execution environment is Node.js (without importing the\n// entire node.js d.ts for now)\ndeclare let global: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\nexport interface IThemingInstruction {\n theme?: string;\n defaultValue?: string;\n rawString?: string;\n}\n\nexport type ThemableArray = IThemingInstruction[];\n\nexport interface ITheme {\n [key: string]: string;\n}\n\ninterface IStyleSheet {\n cssText: string;\n}\n\ninterface IExtendedHtmlStyleElement extends HTMLStyleElement {\n styleSheet: IStyleSheet;\n}\n\n/**\n * Performance Measurement of loading styles\n */\ninterface IMeasurement {\n /**\n * Count of style element injected, which is the slow operation in IE\n */\n count: number;\n /**\n * Total duration of all loadStyles exections\n */\n duration: number;\n}\n\ninterface IRunState {\n mode: Mode;\n buffer: ThemableArray[];\n flushTimer: number;\n}\n\ninterface IThemeState {\n theme: ITheme | undefined;\n lastStyleElement: IExtendedHtmlStyleElement;\n registeredStyles: IStyleRecord[]; // records of already registered non-themable styles\n registeredThemableStyles: IStyleRecord[]; // records of already registered themable styles\n loadStyles: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined;\n perf: IMeasurement;\n runState: IRunState;\n}\n\ninterface IStyleRecord {\n styleElement: Element;\n themableStyle: ThemableArray;\n}\n\ninterface ICustomEvent<T> extends Event {\n args?: T;\n}\n\n/**\n * object returned from resolveThemableArray function\n */\ninterface IThemableArrayResolveResult {\n /** this string is the processed styles in string */\n styleString: string;\n\n /** this boolean indicates if this style array is themable */\n themable: boolean;\n}\n\n/**\n * In sync mode, styles are registered as style elements synchronously with loadStyles() call.\n * In async mode, styles are buffered and registered as batch in async timer for performance purpose.\n */\nexport const enum Mode {\n sync,\n async\n}\n\n/**\n * Themable styles and non-themable styles are tracked separately\n * Specify ClearStyleOptions when calling clearStyles API to specify which group of registered styles should be cleared.\n */\nexport const enum ClearStyleOptions {\n /** only themable styles will be cleared */\n onlyThemable = 1,\n /** only non-themable styles will be cleared */\n onlyNonThemable = 2,\n /** both themable and non-themable styles will be cleared */\n all = 3\n}\n\n// Store the theming state in __themeState__ global scope for reuse in the case of duplicate\n// load-themed-styles hosted on the page.\nconst _root: any = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).\nconst _styleNonce: string = _root && _root.CSPSettings && _root.CSPSettings.nonce;\n\nconst _themeState: IThemeState = initializeThemeState();\n\n/**\n * Matches theming tokens. For example, \"[theme: themeSlotName, default: #FFF]\" (including the quotes).\n */\nconst _themeTokenRegex: RegExp =\n /[\\'\\\"]\\[theme:\\s*(\\w+)\\s*(?:\\,\\s*default:\\s*([\\\\\"\\']?[\\.\\,\\(\\)\\#\\-\\s\\w]*[\\.\\,\\(\\)\\#\\-\\w][\\\"\\']?))?\\s*\\][\\'\\\"]/g;\n\nconst now: () => number = () =>\n typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();\n\nfunction measure(func: () => void): void {\n const start: number = now();\n func();\n const end: number = now();\n _themeState.perf.duration += end - start;\n}\n\n/**\n * initialize global state object\n */\nfunction initializeThemeState(): IThemeState {\n let state: IThemeState = _root.__themeState__ || {\n theme: undefined,\n lastStyleElement: undefined,\n registeredStyles: []\n };\n\n if (!state.runState) {\n state = {\n ...state,\n perf: {\n count: 0,\n duration: 0\n },\n runState: {\n flushTimer: 0,\n mode: Mode.sync,\n buffer: []\n }\n };\n }\n if (!state.registeredThemableStyles) {\n state = {\n ...state,\n registeredThemableStyles: []\n };\n }\n _root.__themeState__ = state;\n return state;\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load\n * event is fired.\n * @param {string | ThemableArray} styles Themable style text to register.\n * @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.\n */\nexport function loadStyles(styles: string | ThemableArray, loadAsync: boolean = false): void {\n measure(() => {\n const styleParts: ThemableArray = Array.isArray(styles) ? styles : splitStyles(styles);\n const { mode, buffer, flushTimer } = _themeState.runState;\n if (loadAsync || mode === Mode.async) {\n buffer.push(styleParts);\n if (!flushTimer) {\n _themeState.runState.flushTimer = asyncLoadStyles();\n }\n } else {\n applyThemableStyles(styleParts);\n }\n });\n}\n\n/**\n * Allows for customizable loadStyles logic. e.g. for server side rendering application\n * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void}\n * a loadStyles callback that gets called when styles are loaded or reloaded\n */\nexport function configureLoadStyles(\n loadStylesFn: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined\n): void {\n _themeState.loadStyles = loadStylesFn;\n}\n\n/**\n * Configure run mode of load-themable-styles\n * @param mode load-themable-styles run mode, async or sync\n */\nexport function configureRunMode(mode: Mode): void {\n _themeState.runState.mode = mode;\n}\n\n/**\n * external code can call flush to synchronously force processing of currently buffered styles\n */\nexport function flush(): void {\n measure(() => {\n const styleArrays: ThemableArray[] = _themeState.runState.buffer.slice();\n _themeState.runState.buffer = [];\n const mergedStyleArray: ThemableArray = ([] as ThemableArray).concat.apply([], styleArrays);\n if (mergedStyleArray.length > 0) {\n applyThemableStyles(mergedStyleArray);\n }\n });\n}\n\n/**\n * register async loadStyles\n */\nfunction asyncLoadStyles(): number {\n // Use \"self\" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node\n // https://github.com/jestjs/jest/issues/14418\n return self.setTimeout(() => {\n _themeState.runState.flushTimer = 0;\n flush();\n }, 0);\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load event\n * is fired.\n * @param {string} styleText Style to register.\n * @param {IStyleRecord} styleRecord Existing style record to re-apply.\n */\nfunction applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord): void {\n if (_themeState.loadStyles) {\n _themeState.loadStyles(resolveThemableArray(stylesArray).styleString, stylesArray);\n } else {\n registerStyles(stylesArray);\n }\n}\n\n/**\n * Registers a set theme tokens to find and replace. If styles were already registered, they will be\n * replaced.\n * @param {theme} theme JSON object of theme tokens to values.\n */\nexport function loadTheme(theme: ITheme | undefined): void {\n _themeState.theme = theme;\n\n // reload styles.\n reloadStyles();\n}\n\n/**\n * Clear already registered style elements and style records in theme_State object\n * @param option - specify which group of registered styles should be cleared.\n * Default to be both themable and non-themable styles will be cleared\n */\nexport function clearStyles(option: ClearStyleOptions = ClearStyleOptions.all): void {\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyNonThemable) {\n clearStylesInternal(_themeState.registeredStyles);\n _themeState.registeredStyles = [];\n }\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyThemable) {\n clearStylesInternal(_themeState.registeredThemableStyles);\n _themeState.registeredThemableStyles = [];\n }\n}\n\nfunction clearStylesInternal(records: IStyleRecord[]): void {\n records.forEach((styleRecord: IStyleRecord) => {\n const styleElement: HTMLStyleElement = styleRecord && (styleRecord.styleElement as HTMLStyleElement);\n if (styleElement && styleElement.parentElement) {\n styleElement.parentElement.removeChild(styleElement);\n }\n });\n}\n\n/**\n * Reloads styles.\n */\nfunction reloadStyles(): void {\n if (_themeState.theme) {\n const themableStyles: ThemableArray[] = [];\n for (const styleRecord of _themeState.registeredThemableStyles) {\n themableStyles.push(styleRecord.themableStyle);\n }\n if (themableStyles.length > 0) {\n clearStyles(ClearStyleOptions.onlyThemable);\n applyThemableStyles(([] as ThemableArray).concat.apply([], themableStyles));\n }\n }\n}\n\n/**\n * Find theme tokens and replaces them with provided theme values.\n * @param {string} styles Tokenized styles to fix.\n */\nexport function detokenize(styles: string | undefined): string | undefined {\n if (styles) {\n styles = resolveThemableArray(splitStyles(styles)).styleString;\n }\n\n return styles;\n}\n\n/**\n * Resolves ThemingInstruction objects in an array and joins the result into a string.\n * @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.\n */\nfunction resolveThemableArray(splitStyleArray: ThemableArray): IThemableArrayResolveResult {\n const { theme }: IThemeState = _themeState;\n let themable: boolean = false;\n // Resolve the array of theming instructions to an array of strings.\n // Then join the array to produce the final CSS string.\n const resolvedArray: (string | undefined)[] = (splitStyleArray || []).map(\n (currentValue: IThemingInstruction) => {\n const themeSlot: string | undefined = currentValue.theme;\n if (themeSlot) {\n themable = true;\n // A theming annotation. Resolve it.\n const themedValue: string | undefined = theme ? theme[themeSlot] : undefined;\n const defaultValue: string = currentValue.defaultValue || 'inherit';\n\n // Warn to console if we hit an unthemed value even when themes are provided, but only if \"DEBUG\" is true.\n // Allow the themedValue to be undefined to explicitly request the default value.\n if (\n theme &&\n !themedValue &&\n console &&\n !(themeSlot in theme) &&\n typeof DEBUG !== 'undefined' &&\n DEBUG\n ) {\n console.warn(`Theming value not provided for \"${themeSlot}\". Falling back to \"${defaultValue}\".`);\n }\n\n return themedValue || defaultValue;\n } else {\n // A non-themable string. Preserve it.\n return currentValue.rawString;\n }\n }\n );\n\n return {\n styleString: resolvedArray.join(''),\n themable: themable\n };\n}\n\n/**\n * Split tokenized CSS into an array of strings and theme specification objects\n * @param {string} styles Tokenized styles to split.\n */\nexport function splitStyles(styles: string): ThemableArray {\n const result: ThemableArray = [];\n if (styles) {\n let pos: number = 0; // Current position in styles.\n let tokenMatch: RegExpExecArray | null;\n while ((tokenMatch = _themeTokenRegex.exec(styles))) {\n const matchIndex: number = tokenMatch.index;\n if (matchIndex > pos) {\n result.push({\n rawString: styles.substring(pos, matchIndex)\n });\n }\n\n result.push({\n theme: tokenMatch[1],\n defaultValue: tokenMatch[2] // May be undefined\n });\n\n // index of the first character after the current match\n pos = _themeTokenRegex.lastIndex;\n }\n\n // Push the rest of the string after the last match.\n result.push({\n rawString: styles.substring(pos)\n });\n }\n\n return result;\n}\n\n/**\n * Registers a set of style text. If it is registered too early, we will register it when the\n * window.load event is fired.\n * @param {ThemableArray} styleArray Array of IThemingInstruction objects to register.\n * @param {IStyleRecord} styleRecord May specify a style Element to update.\n */\nfunction registerStyles(styleArray: ThemableArray): void {\n if (typeof document === 'undefined') {\n return;\n }\n const head: HTMLHeadElement = document.getElementsByTagName('head')[0];\n const styleElement: HTMLStyleElement = document.createElement('style');\n const { styleString, themable } = resolveThemableArray(styleArray);\n\n styleElement.setAttribute('data-load-themed-styles', 'true');\n if (_styleNonce) {\n styleElement.setAttribute('nonce', _styleNonce);\n }\n styleElement.appendChild(document.createTextNode(styleString));\n _themeState.perf.count++;\n head.appendChild(styleElement);\n\n const ev: ICustomEvent<{ newStyle: HTMLStyleElement }> = document.createEvent('HTMLEvents');\n ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);\n ev.args = {\n newStyle: styleElement\n };\n document.dispatchEvent(ev);\n\n const record: IStyleRecord = {\n styleElement: styleElement,\n themableStyle: styleArray\n };\n\n if (themable) {\n _themeState.registeredThemableStyles.push(record);\n } else {\n _themeState.registeredStyles.push(record);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;AAyG3D,4FAA4F;AAC5F,yCAAyC;AACzC,IAAM,KAAK,GAAQ,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,yDAAyD;AAE7H,yGAAyG;AACzG,IAAM,WAAW,GAAW,KAAK,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC;AAElF,IAAM,WAAW,GAAgB,oBAAoB,EAAE,CAAC;AAExD;;GAEG;AACH,IAAM,gBAAgB,GACpB,gHAAgH,CAAC;AAEnH,IAAM,GAAG,GAAiB;IACxB,OAAA,OAAO,WAAW,KAAK,WAAW,IAAI,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;AAAxF,CAAwF,CAAC;AAE3F,SAAS,OAAO,CAAC,IAAgB;IAC/B,IAAM,KAAK,GAAW,GAAG,EAAE,CAAC;IAC5B,IAAI,EAAE,CAAC;IACP,IAAM,GAAG,G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,EAAE,QAAQ,cAAqC,CAAC;IAEnE,YAAY,CAAC,YAAY,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;IAC7D,IAAI,WAAW,EAAE;QACf,YAAY,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;KACjD;IACD,YAAY,CAAC,WAAW,CAAC,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC;IAC/D,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACzB,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAE/B,IAAM,EAAE,GAAiD,QAAQ,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAC5F,EAAE,CAAC,SAAS,CAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAC5E,EAAE,CAAC,IAAI,GAAG;QACR,QAAQ,EAAE,YAAY;KACvB,CAAC;IACF,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;IAE3B,IAAM,MAAM,GAAiB;QAC3B,YAAY,EAAE,YAAY;QAC1B,aAAa,EAAE,UAAU;KAC1B,CAAC;IAEF,IAAI,QAAQ,EAAE;QACZ,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KACnD;SAAM;QACL,WAAW,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KAC3C;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * An IThemingInstruction can specify a rawString to be preserved or a theme slot and a default value\n * to use if that slot is not specified by the theme.\n */\n\n/* eslint-disable @typescript-eslint/no-use-before-define */\n\n// Declaring a global here in case that the execution environment is Node.js (without importing the\n// entire node.js d.ts for now)\ndeclare let global: any; // eslint-disable-line @typescript-eslint/no-explicit-any\n\nexport interface IThemingInstruction {\n theme?: string;\n defaultValue?: string;\n rawString?: string;\n}\n\nexport type ThemableArray = IThemingInstruction[];\n\nexport interface ITheme {\n [key: string]: string;\n}\n\ninterface IStyleSheet {\n cssText: string;\n}\n\ninterface IExtendedHtmlStyleElement extends HTMLStyleElement {\n styleSheet: IStyleSheet;\n}\n\n/**\n * Performance Measurement of loading styles\n */\ninterface IMeasurement {\n /**\n * Count of style element injected, which is the slow operation in IE\n */\n count: number;\n /**\n * Total duration of all loadStyles exections\n */\n duration: number;\n}\n\ninterface IRunState {\n mode: Mode;\n buffer: ThemableArray[];\n flushTimer: number;\n}\n\ninterface IThemeState {\n theme: ITheme | undefined;\n lastStyleElement: IExtendedHtmlStyleElement;\n registeredStyles: IStyleRecord[]; // records of already registered non-themable styles\n registeredThemableStyles: IStyleRecord[]; // records of already registered themable styles\n loadStyles: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined;\n perf: IMeasurement;\n runState: IRunState;\n}\n\ninterface IStyleRecord {\n styleElement: Element;\n themableStyle: ThemableArray;\n}\n\ninterface ICustomEvent<T> extends Event {\n args?: T;\n}\n\n/**\n * object returned from resolveThemableArray function\n */\ninterface IThemableArrayResolveResult {\n /** this string is the processed styles in string */\n styleString: string;\n\n /** this boolean indicates if this style array is themable */\n themable: boolean;\n}\n\n/**\n * In sync mode, styles are registered as style elements synchronously with loadStyles() call.\n * In async mode, styles are buffered and registered as batch in async timer for performance purpose.\n */\nexport const enum Mode {\n sync,\n async\n}\n\n/**\n * Themable styles and non-themable styles are tracked separately\n * Specify ClearStyleOptions when calling clearStyles API to specify which group of registered styles should be cleared.\n */\nexport const enum ClearStyleOptions {\n /** only themable styles will be cleared */\n onlyThemable = 1,\n /** only non-themable styles will be cleared */\n onlyNonThemable = 2,\n /** both themable and non-themable styles will be cleared */\n all = 3\n}\n\n// Store the theming state in __themeState__ global scope for reuse in the case of duplicate\n// load-themed-styles hosted on the page.\nconst _root: any = typeof window === 'undefined' ? global : window; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n// Nonce string to inject into script tag if one provided. This is used in CSP (Content Security Policy).\nconst _styleNonce: string = _root && _root.CSPSettings && _root.CSPSettings.nonce;\n\nconst _themeState: IThemeState = initializeThemeState();\n\n/**\n * Matches theming tokens. For example, \"[theme: themeSlotName, default: #FFF]\" (including the quotes).\n */\nconst _themeTokenRegex: RegExp =\n /[\\'\\\"]\\[theme:\\s*(\\w+)\\s*(?:\\,\\s*default:\\s*([\\\\\"\\']?[\\.\\,\\(\\)\\#\\-\\s\\w]*[\\.\\,\\(\\)\\#\\-\\w][\\\"\\']?))?\\s*\\][\\'\\\"]/g;\n\nconst now: () => number = () =>\n typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();\n\nfunction measure(func: () => void): void {\n const start: number = now();\n func();\n const end: number = now();\n _themeState.perf.duration += end - start;\n}\n\n/**\n * initialize global state object\n */\nfunction initializeThemeState(): IThemeState {\n let state: IThemeState = _root.__themeState__ || {\n theme: undefined,\n lastStyleElement: undefined,\n registeredStyles: []\n };\n\n if (!state.runState) {\n state = {\n ...state,\n perf: {\n count: 0,\n duration: 0\n },\n runState: {\n flushTimer: 0,\n mode: Mode.sync,\n buffer: []\n }\n };\n }\n if (!state.registeredThemableStyles) {\n state = {\n ...state,\n registeredThemableStyles: []\n };\n }\n _root.__themeState__ = state;\n return state;\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load\n * event is fired.\n * @param {string | ThemableArray} styles Themable style text to register.\n * @param {boolean} loadAsync When true, always load styles in async mode, irrespective of current sync mode.\n */\nexport function loadStyles(styles: string | ThemableArray, loadAsync: boolean = false): void {\n measure(() => {\n const styleParts: ThemableArray = Array.isArray(styles) ? styles : splitStyles(styles);\n const { mode, buffer, flushTimer } = _themeState.runState;\n if (loadAsync || mode === Mode.async) {\n buffer.push(styleParts);\n if (!flushTimer) {\n _themeState.runState.flushTimer = asyncLoadStyles();\n }\n } else {\n applyThemableStyles(styleParts);\n }\n });\n}\n\n/**\n * Allows for customizable loadStyles logic. e.g. for server side rendering application\n * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void}\n * a loadStyles callback that gets called when styles are loaded or reloaded\n */\nexport function configureLoadStyles(\n loadStylesFn: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined\n): void {\n _themeState.loadStyles = loadStylesFn;\n}\n\n/**\n * Configure run mode of load-themable-styles\n * @param mode load-themable-styles run mode, async or sync\n */\nexport function configureRunMode(mode: Mode): void {\n _themeState.runState.mode = mode;\n}\n\n/**\n * external code can call flush to synchronously force processing of currently buffered styles\n */\nexport function flush(): void {\n measure(() => {\n const styleArrays: ThemableArray[] = _themeState.runState.buffer.slice();\n _themeState.runState.buffer = [];\n const mergedStyleArray: ThemableArray = ([] as ThemableArray).concat.apply([], styleArrays);\n if (mergedStyleArray.length > 0) {\n applyThemableStyles(mergedStyleArray);\n }\n });\n}\n\n/**\n * register async loadStyles\n */\nfunction asyncLoadStyles(): number {\n // Use \"self\" to distinguish conflicting global typings for setTimeout() from lib.dom.d.ts vs Jest's @types/node\n // https://github.com/jestjs/jest/issues/14418\n return self.setTimeout(() => {\n _themeState.runState.flushTimer = 0;\n flush();\n }, 0);\n}\n\n/**\n * Loads a set of style text. If it is registered too early, we will register it when the window.load event\n * is fired.\n * @param {string} styleText Style to register.\n * @param {IStyleRecord} styleRecord Existing style record to re-apply.\n */\nfunction applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord): void {\n if (_themeState.loadStyles) {\n _themeState.loadStyles(resolveThemableArray(stylesArray).styleString, stylesArray);\n } else {\n registerStyles(stylesArray);\n }\n}\n\n/**\n * Registers a set theme tokens to find and replace. If styles were already registered, they will be\n * replaced.\n * @param {theme} theme JSON object of theme tokens to values.\n */\nexport function loadTheme(theme: ITheme | undefined): void {\n _themeState.theme = theme;\n\n // reload styles.\n reloadStyles();\n}\n\n/**\n * Clear already registered style elements and style records in theme_State object\n * @param option - specify which group of registered styles should be cleared.\n * Default to be both themable and non-themable styles will be cleared\n */\nexport function clearStyles(option: ClearStyleOptions = ClearStyleOptions.all): void {\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyNonThemable) {\n clearStylesInternal(_themeState.registeredStyles);\n _themeState.registeredStyles = [];\n }\n if (option === ClearStyleOptions.all || option === ClearStyleOptions.onlyThemable) {\n clearStylesInternal(_themeState.registeredThemableStyles);\n _themeState.registeredThemableStyles = [];\n }\n}\n\nfunction clearStylesInternal(records: IStyleRecord[]): void {\n records.forEach((styleRecord: IStyleRecord) => {\n const styleElement: HTMLStyleElement = styleRecord && (styleRecord.styleElement as HTMLStyleElement);\n if (styleElement && styleElement.parentElement) {\n styleElement.parentElement.removeChild(styleElement);\n }\n });\n}\n\n/**\n * Reloads styles.\n */\nfunction reloadStyles(): void {\n if (_themeState.theme) {\n const themableStyles: ThemableArray[] = [];\n for (const styleRecord of _themeState.registeredThemableStyles) {\n themableStyles.push(styleRecord.themableStyle);\n }\n if (themableStyles.length > 0) {\n clearStyles(ClearStyleOptions.onlyThemable);\n applyThemableStyles(([] as ThemableArray).concat.apply([], themableStyles));\n }\n }\n}\n\n/**\n * Find theme tokens and replaces them with provided theme values.\n * @param {string} styles Tokenized styles to fix.\n */\nexport function detokenize(styles: string | undefined): string | undefined {\n if (styles) {\n styles = resolveThemableArray(splitStyles(styles)).styleString;\n }\n\n return styles;\n}\n\n/**\n * Resolves ThemingInstruction objects in an array and joins the result into a string.\n * @param {ThemableArray} splitStyleArray ThemableArray to resolve and join.\n */\nfunction resolveThemableArray(splitStyleArray: ThemableArray): IThemableArrayResolveResult {\n const { theme }: IThemeState = _themeState;\n let themable: boolean = false;\n // Resolve the array of theming instructions to an array of strings.\n // Then join the array to produce the final CSS string.\n const resolvedArray: (string | undefined)[] = (splitStyleArray || []).map(\n (currentValue: IThemingInstruction) => {\n const themeSlot: string | undefined = currentValue.theme;\n if (themeSlot) {\n themable = true;\n // A theming annotation. Resolve it.\n const themedValue: string | undefined = theme ? theme[themeSlot] : undefined;\n const defaultValue: string = currentValue.defaultValue || 'inherit';\n\n // Warn to console if we hit an unthemed value even when themes are provided, but only if \"DEBUG\" is true.\n // Allow the themedValue to be undefined to explicitly request the default value.\n if (\n theme &&\n !themedValue &&\n console &&\n !(themeSlot in theme) &&\n typeof DEBUG !== 'undefined' &&\n DEBUG\n ) {\n console.warn(`Theming value not provided for \"${themeSlot}\". Falling back to \"${defaultValue}\".`);\n }\n\n return themedValue || defaultValue;\n } else {\n // A non-themable string. Preserve it.\n return currentValue.rawString;\n }\n }\n );\n\n return {\n styleString: resolvedArray.join(''),\n themable: themable\n };\n}\n\n/**\n * Split tokenized CSS into an array of strings and theme specification objects\n * @param {string} styles Tokenized styles to split.\n */\nexport function splitStyles(styles: string): ThemableArray {\n const result: ThemableArray = [];\n if (styles) {\n let pos: number = 0; // Current position in styles.\n let tokenMatch: RegExpExecArray | null;\n while ((tokenMatch = _themeTokenRegex.exec(styles))) {\n const matchIndex: number = tokenMatch.index;\n if (matchIndex > pos) {\n result.push({\n rawString: styles.substring(pos, matchIndex)\n });\n }\n\n result.push({\n theme: tokenMatch[1],\n defaultValue: tokenMatch[2] // May be undefined\n });\n\n // index of the first character after the current match\n pos = _themeTokenRegex.lastIndex;\n }\n\n // Push the rest of the string after the last match.\n result.push({\n rawString: styles.substring(pos)\n });\n }\n\n return result;\n}\n\n/**\n * Registers a set of style text. If it is registered too early, we will register it when the\n * window.load event is fired.\n * @param {ThemableArray} styleArray Array of IThemingInstruction objects to register.\n * @param {IStyleRecord} styleRecord May specify a style Element to update.\n */\nfunction registerStyles(styleArray: ThemableArray): void {\n if (typeof document === 'undefined') {\n return;\n }\n const head: HTMLHeadElement = document.getElementsByTagName('head')[0];\n const styleElement: HTMLStyleElement = document.createElement('style');\n const { styleString, themable } = resolveThemableArray(styleArray);\n\n styleElement.setAttribute('data-load-themed-styles', 'true');\n if (_styleNonce) {\n styleElement.setAttribute('nonce', _styleNonce);\n }\n styleElement.appendChild(document.createTextNode(styleString));\n _themeState.perf.count++;\n head.appendChild(styleElement);\n\n const ev: ICustomEvent<{ newStyle: HTMLStyleElement }> = document.createEvent('HTMLEvents');\n ev.initEvent('styleinsert', true /* bubbleEvent */, false /* cancelable */);\n ev.args = {\n newStyle: styleElement\n };\n document.dispatchEvent(ev);\n\n const record: IStyleRecord = {\n styleElement: styleElement,\n themableStyle: styleArray\n };\n\n if (themable) {\n _themeState.registeredThemableStyles.push(record);\n } else {\n _themeState.registeredStyles.push(record);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microsoft/load-themed-styles",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.78",
|
|
4
4
|
"description": "Loads themed styles.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -13,11 +13,9 @@
|
|
|
13
13
|
"typings": "lib/index.d.ts",
|
|
14
14
|
"keywords": [],
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"@types/heft-jest": "1.0.1",
|
|
17
|
-
"@types/webpack-env": "1.18.0",
|
|
18
16
|
"@rushstack/eslint-config": "3.3.4",
|
|
19
|
-
"
|
|
20
|
-
"@rushstack/heft
|
|
17
|
+
"local-web-rig": "1.0.0",
|
|
18
|
+
"@rushstack/heft": "0.61.0"
|
|
21
19
|
},
|
|
22
20
|
"scripts": {
|
|
23
21
|
"build": "heft build --clean",
|