debt - allow to pass in disposables to createStyleSheet

This commit is contained in:
Benjamin Pasero
2023-10-28 07:54:27 +02:00
committed by Benjamin Pasero
parent 81f9858f1d
commit 494cc43ec8
9 changed files with 41 additions and 38 deletions

View File

@@ -792,13 +792,17 @@ export function focusWindow(element: Node): void {
const globalStylesheets = new Map<HTMLStyleElement /* main stylesheet */, Set<HTMLStyleElement /* aux window clones that track the main stylesheet */>>();
export function createStyleSheet(container: HTMLElement = document.head, beforeAppend?: (style: HTMLStyleElement) => void): HTMLStyleElement {
export function createStyleSheet(container: HTMLElement = document.head, beforeAppend?: (style: HTMLStyleElement) => void, disposableStore?: DisposableStore): HTMLStyleElement {
const style = document.createElement('style');
style.type = 'text/css';
style.media = 'screen';
beforeAppend?.(style);
container.appendChild(style);
if (disposableStore) {
disposableStore.add(toDisposable(() => container.removeChild(style)));
}
// With <head> as container, the stylesheet becomes global and is tracked
// to support auxiliary windows to clone the stylesheet.
if (container === document.head) {
@@ -807,14 +811,16 @@ export function createStyleSheet(container: HTMLElement = document.head, beforeA
continue; // main window is already tracked
}
const disposable = cloneGlobalStyleSheet(style, targetWindow);
const cloneDisposable = cloneGlobalStyleSheet(style, targetWindow);
disposableStore?.add(cloneDisposable);
event.Event.once(onDidUnregisterWindow)(unregisteredWindow => {
disposableStore?.add(event.Event.once(onDidUnregisterWindow)(unregisteredWindow => {
if (unregisteredWindow === targetWindow) {
disposable.dispose();
cloneDisposable.dispose();
}
});
}));
}
}
return style;