import { injectable } from 'inversify'; import { IDisposable } from 'ts-toolset'; import { IStyleRuleEntity } from '../../../models/html'; import { createStyleSheet } from '../../../common/browser/css'; export interface IStyleAccess extends IDisposable { hasRule(key: string): boolean; //insertRules(): void; initStyleSheet(doc: Document): IStyleAccess; setCssRuleStore(store: Map): void; } @injectable() export class StyleAccess implements IStyleAccess { private _styleSheet!: CSSStyleSheet; private _styleNode!: HTMLElement; private _cssRuleStore!: Map; private _cssRuleMap: Map = new Map(); initStyleSheet(doc: Document) { [this._styleNode, this._styleSheet] = createStyleSheet(doc)!; //this.insertRule(); return this; } setCssRuleStore(store: Map) { this._cssRuleStore = store; return this; } hasRule(key: string) { return !!this._cssRuleStore.get(key); } insertRule(rule: IStyleRuleEntity) { this._cssRuleStore.set(rule.key, rule); rule.rules && Object.keys(rule.rules) .forEach(key => { let ruleText = rule.rules[key]; if (ruleText && ruleText.trim()) { try { let index = this._styleSheet.insertRule(ruleText); let cssRule = this._styleSheet.cssRules.item(index) as CSSStyleRule; this._cssRuleMap.set(cssRule.selectorText, cssRule); } catch { } } }); return this; } insertRules(rules: IStyleRuleEntity[]) { rules.forEach(rule => this.insertRule(rule)); return this; } updateRule(key: string, state: string, content: string) { } deleteRule(key: string) { } deleteRules(keys: string[]) { } dispose() { this._cssRuleStore.clear(); this._styleNode.innerHTML = ''; return this; } }