Skip to content

Commit 4dd905c

Browse files
committed
batch 1
1 parent 0b94044 commit 4dd905c

File tree

1 file changed

+53
-14
lines changed
  • libraries/load-themed-styles/src

1 file changed

+53
-14
lines changed

libraries/load-themed-styles/src/index.ts

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface IThemeState {
5151
theme: ITheme | undefined;
5252
lastStyleElement: IExtendedHtmlStyleElement;
5353
registeredStyles: IStyleRecord[];
54+
registeredThemableStyles: IStyleRecord[];
5455
loadStyles: ((processedStyles: string, rawStyles?: string | ThemableArray) => void) | undefined;
5556
perf: IMeasurement;
5657
runState: IRunState;
@@ -59,6 +60,7 @@ interface IThemeState {
5960
interface IStyleRecord {
6061
styleElement: Element;
6162
themableStyle: ThemableArray;
63+
processedStyleString?: string;
6264
}
6365

6466
/**
@@ -106,7 +108,8 @@ function initializeThemeState(): IThemeState {
106108
let state: IThemeState = _root.__themeState__ || {
107109
theme: undefined,
108110
lastStyleElement: undefined,
109-
registeredStyles: []
111+
registeredStyles: [],
112+
registeredThemableStyles: []
110113
};
111114

112115
if (!state.runState) {
@@ -188,6 +191,27 @@ export function flush(): void {
188191
});
189192
}
190193

194+
function prepareRegisteredThemableStyles(): ThemableArray {
195+
const buffer: ThemableArray[] = [];
196+
for (let index: number = 0; index < _themeState.registeredStyles.length; index++) {
197+
const styleRecord: IStyleRecord = _themeState.registeredStyles[index];
198+
const {
199+
themableStyle,
200+
styleElement
201+
} = styleRecord;
202+
const styleString: string = resolveThemableArray(themableStyle);
203+
if (styleString !== styleElement.textContent) {
204+
_themeState.registeredThemableStyles.push(styleRecord);
205+
_themeState.registeredStyles.splice(index, 1);
206+
buffer.push(themableStyle);
207+
}
208+
}
209+
for (const styleRecord of _themeState.registeredThemableStyles) {
210+
buffer.push(styleRecord.themableStyle);
211+
}
212+
return [].concat.apply([], buffer);
213+
}
214+
191215
/**
192216
* register async loadStyles
193217
*/
@@ -204,13 +228,13 @@ function asyncLoadStyles(): number {
204228
* @param {string} styleText Style to register.
205229
* @param {IStyleRecord} styleRecord Existing style record to re-apply.
206230
*/
207-
function applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord): void {
231+
function applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRecord, intoRegisteredThemedStyles?: boolean): void {
208232
if (_themeState.loadStyles) {
209233
_themeState.loadStyles(resolveThemableArray(stylesArray), stylesArray);
210234
} else {
211235
_injectStylesWithCssText ?
212236
registerStylesIE(stylesArray, styleRecord) :
213-
registerStyles(stylesArray, styleRecord);
237+
registerStyles(stylesArray, styleRecord, intoRegisteredThemedStyles);
214238
}
215239
}
216240

@@ -229,24 +253,32 @@ export function loadTheme(theme: ITheme | undefined): void {
229253
/**
230254
* Clear already registered style elements and style records in theme_State object
231255
*/
232-
export function clearStyles(): void {
233-
_themeState.registeredStyles.forEach((styleRecord: IStyleRecord) => {
256+
export function clearStyles(clearOnlyThemable?: boolean): void {
257+
if (!clearOnlyThemable) {
258+
clearStylesInternal(_themeState.registeredStyles);
259+
_themeState.registeredStyles = [];
260+
}
261+
clearStylesInternal(_themeState.registeredThemableStyles);
262+
_themeState.registeredThemableStyles = [];
263+
}
264+
265+
function clearStylesInternal(records: IStyleRecord[]): void {
266+
records.forEach((styleRecord: IStyleRecord) => {
234267
const styleElement: HTMLStyleElement = styleRecord && styleRecord.styleElement as HTMLStyleElement;
235268
if (styleElement && styleElement.parentElement) {
236269
styleElement.parentElement.removeChild(styleElement);
237270
}
238271
});
239-
_themeState.registeredStyles = [];
240272
}
241273

242274
/**
243275
* Reloads styles.
244276
*/
245277
function reloadStyles(): void {
246278
if (_themeState.theme) {
247-
for (const styleRecord of _themeState.registeredStyles) {
248-
applyThemableStyles(styleRecord.themableStyle, styleRecord);
249-
}
279+
const buffer: ThemableArray = prepareRegisteredThemableStyles();
280+
clearStyles(true);
281+
applyThemableStyles(buffer, undefined, true);
250282
}
251283
}
252284

@@ -334,7 +366,7 @@ export function splitStyles(styles: string): ThemableArray {
334366
* @param {ThemableArray} styleArray Array of IThemingInstruction objects to register.
335367
* @param {IStyleRecord} styleRecord May specify a style Element to update.
336368
*/
337-
function registerStyles(styleArray: ThemableArray, styleRecord?: IStyleRecord): void {
369+
function registerStyles(styleArray: ThemableArray, styleRecord?: IStyleRecord, intoRegisteredThemedStyles?: boolean): void {
338370
const head: HTMLHeadElement = document.getElementsByTagName('head')[0];
339371
const styleElement: HTMLStyleElement = document.createElement('style');
340372

@@ -350,10 +382,17 @@ function registerStyles(styleArray: ThemableArray, styleRecord?: IStyleRecord):
350382
}
351383

352384
if (!styleRecord) {
353-
_themeState.registeredStyles.push({
354-
styleElement: styleElement,
355-
themableStyle: styleArray
356-
});
385+
if (intoRegisteredThemedStyles) {
386+
_themeState.registeredThemableStyles.push({
387+
styleElement: styleElement,
388+
themableStyle: styleArray
389+
});
390+
} else {
391+
_themeState.registeredStyles.push({
392+
styleElement: styleElement,
393+
themableStyle: styleArray
394+
});
395+
}
357396
}
358397
}
359398

0 commit comments

Comments
 (0)