import { init, getEngineChecksum } from '../theme_data/theme_data_3.service.js' import { getCssRules } from '../theme_data/css_utils.js' import { defaultState } from 'src/modules/default_config_state.js' import { chunk, throttle } from 'lodash' import localforage from 'localforage' // On platforms where this is not supported, it will return undefined // Otherwise it will return an array const supportsAdoptedStyleSheets = !!document.adoptedStyleSheets const stylesheets = {} export const createStyleSheet = (id, priority = 1000) => { if (stylesheets[id]) return stylesheets[id] const newStyleSheet = { rules: [], ready: false, priority, clear () { this.rules = [] }, addRule (rule) { let newRule = rule if (!CSS.supports?.('backdrop-filter', 'blur()')) { newRule = newRule.replace(/backdrop-filter:[^;]+;/g, '') // Remove backdrop-filter } // firefox doesn't like invalid selectors if (!CSS.supports?.('::-webkit') && newRule.startsWith('::-webkit')) { return } this.rules.push( newRule .replace(/var\(--shadowFilter\)[^;]*;/g, '') // Remove shadowFilter references ) } } stylesheets[id] = newStyleSheet return newStyleSheet } export const adoptStyleSheets = throttle(() => { if (supportsAdoptedStyleSheets) { document.adoptedStyleSheets = Object .values(stylesheets) .filter(x => x.ready) .sort((a, b) => a.priority - b.priority) .map(sheet => { const css = new CSSStyleSheet() sheet.rules.forEach(r => css.insertRule(r)) return css }) } else { const holder = document.getElementById('custom-styles-holder') for (let i = holder.sheet.cssRules.length - 1; i >= 0; --i) { holder.sheet.deleteRule(i) } Object .values(stylesheets) .filter(x => x.ready) .sort((a, b) => a.priority - b.priority) .forEach(sheet => { sheet.rules.forEach(r => holder.sheet.insertRule(r)) }) } // Some older browsers do not support document.adoptedStyleSheets. // In this case, we use the