pleroma-fe/src/services/style_setter/style_setter.js

303 lines
8.1 KiB
JavaScript
Raw Normal View History

2024-04-24 15:09:52 +03:00
import { init, getEngineChecksum } from '../theme_data/theme_data_3.service.js'
import { getCssRules } from '../theme_data/css_utils.js'
2025-01-30 21:56:07 +02:00
import { defaultState } from 'src/modules/default_config_state.js'
2025-07-02 22:54:45 +03:00
import { chunk, throttle } from 'lodash'
import localforage from 'localforage'
2024-05-31 14:33:44 -04:00
// On platforms where this is not supported, it will return undefined
// Otherwise it will return an array
const supportsAdoptedStyleSheets = !!document.adoptedStyleSheets
2025-07-02 19:39:25 +03:00
const stylesheets = {}
2025-07-02 22:54:45 +03:00
export const createStyleSheet = (id) => {
if (stylesheets[id]) return stylesheets[id]
2025-07-02 19:39:25 +03:00
const newStyleSheet = {
rules: [],
ready: false,
2025-07-02 22:54:45 +03:00
clear () {
this.rules = []
},
2025-07-02 19:39:25 +03:00
addRule (rule) {
this.rules.push(
rule
.replace(/backdrop-filter:[^;]+;/g, '') // Remove backdrop-filter
.replace(/var\(--shadowFilter\)[^;]*;/g, '') // Remove shadowFilter references
)
2024-05-31 14:33:44 -04:00
}
}
2025-07-02 19:39:25 +03:00
stylesheets[id] = newStyleSheet
return newStyleSheet
2024-05-31 14:33:44 -04:00
}
2025-07-02 22:54:45 +03:00
export const adoptStyleSheets = throttle(() => {
2024-05-31 14:33:44 -04:00
if (supportsAdoptedStyleSheets) {
2025-07-02 19:39:25 +03:00
document.adoptedStyleSheets = Object
.values(stylesheets)
.filter(x => x.ready)
.map(x => {
const css = new CSSStyleSheet()
x.rules.forEach(r => css.insertRule(r, css.cssRules.length))
return css
})
} else {
const holder = document.getElementById('custom-styles-holder')
Object
.values(stylesheets)
.forEach(sheet => {
sheet.rules.forEach(r => holder.sheet.insertRule(r))
})
2024-05-31 14:33:44 -04:00
}
// Some older browsers do not support document.adoptedStyleSheets.
// In this case, we use the <style> elements.
// Since the <style> elements we need are already in the DOM, there
// is nothing to do here.
2025-07-02 22:54:45 +03:00
}, 500)
2024-05-31 14:33:44 -04:00
2025-07-02 19:39:25 +03:00
const EAGER_STYLE_ID = 'pleroma-eager-styles'
const LAZY_STYLE_ID = 'pleroma-lazy-styles'
export const generateTheme = (inputRuleset, callbacks, debug) => {
const {
2025-02-04 15:23:21 +02:00
onNewRule = () => {},
onLazyFinished = () => {},
onEagerFinished = () => {}
} = callbacks
2024-07-04 03:20:26 +03:00
const themes3 = init({
inputRuleset,
2024-07-04 03:20:26 +03:00
debug
})
getCssRules(themes3.eager, debug).forEach(rule => {
2024-03-06 20:27:05 +02:00
// Hacks to support multiple selectors on same component
2024-10-21 23:10:54 +03:00
onNewRule(rule, false)
2024-01-31 17:39:51 +02:00
})
onEagerFinished()
// Optimization - instead of processing all lazy rules in one go, process them in small chunks
// so that UI can do other things and be somewhat responsive while less important rules are being
// processed
let counter = 0
const chunks = chunk(themes3.lazy, 200)
// let t0 = performance.now()
const processChunk = () => {
const chunk = chunks[counter]
Promise.all(chunk.map(x => x())).then(result => {
getCssRules(result.filter(x => x), debug).forEach(rule => {
2024-10-21 23:10:54 +03:00
onNewRule(rule, true)
})
// const t1 = performance.now()
// console.debug('Chunk ' + counter + ' took ' + (t1 - t0) + 'ms')
// t0 = t1
counter += 1
if (counter < chunks.length) {
setTimeout(processChunk, 0)
} else {
onLazyFinished()
}
})
}
return { lazyProcessFunc: processChunk }
}
export const tryLoadCache = async () => {
console.info('Trying to load compiled theme data from cache')
const cache = await localforage.getItem('pleromafe-theme-cache')
if (!cache) return null
try {
if (cache.engineChecksum === getEngineChecksum()) {
const eagerStyles = createStyleSheet(EAGER_STYLE_ID)
const lazyStyles = createStyleSheet(LAZY_STYLE_ID)
2025-07-02 19:39:25 +03:00
cache.data[0].forEach(rule => eagerStyles.addRule(rule, 'index-max'))
cache.data[1].forEach(rule => lazyStyles.addRule(rule, 'index-max'))
eagerStyles.ready = true
lazyStyles.ready = true
2025-07-02 19:39:25 +03:00
adoptStyleSheets()
console.info(`Loaded theme from cache`)
return true
} else {
console.warn('Engine checksum doesn\'t match, cache not usable, clearing')
localStorage.removeItem('pleroma-fe-theme-cache')
}
} catch (e) {
console.error('Failed to load theme cache:', e)
return false
}
}
2024-12-18 16:29:38 +02:00
export const applyTheme = (
input,
2025-02-04 15:23:21 +02:00
onEagerFinish = () => {},
onFinish = () => {},
2024-12-18 16:29:38 +02:00
debug
) => {
2024-05-31 14:33:44 -04:00
const eagerStyles = createStyleSheet(EAGER_STYLE_ID)
const lazyStyles = createStyleSheet(LAZY_STYLE_ID)
2024-12-03 19:30:35 +02:00
const { lazyProcessFunc } = generateTheme(
input,
{
onNewRule (rule, isLazy) {
if (isLazy) {
2025-07-02 19:39:25 +03:00
lazyStyles.addRule(rule)
} else {
2025-07-02 19:39:25 +03:00
eagerStyles.addRule(rule)
}
},
onEagerFinished () {
2025-07-02 19:39:25 +03:00
eagerStyles.ready = true
adoptStyleSheets()
onEagerFinish()
2025-02-10 23:10:04 +02:00
console.info('Eager part of theme finished, waiting for lazy part to finish to store cache')
},
onLazyFinished () {
2025-07-02 19:39:25 +03:00
eagerStyles.ready = true
adoptStyleSheets()
const cache = { engineChecksum: getEngineChecksum(), data: [eagerStyles.rules, lazyStyles.rules] }
onFinish(cache)
2025-02-10 23:10:04 +02:00
localforage.setItem('pleromafe-theme-cache', cache)
console.info('Theme cache stored')
}
},
debug
)
setTimeout(lazyProcessFunc, 0)
}
2024-06-13 02:22:47 +03:00
const extractStyleConfig = ({
sidebarColumnWidth,
contentColumnWidth,
notifsColumnWidth,
2025-03-10 23:36:04 -04:00
themeEditorMinWidth,
emojiReactionsScale,
2024-06-13 02:22:47 +03:00
emojiSize,
navbarSize,
panelHeaderSize,
2024-06-21 22:46:01 +03:00
textSize,
forcedRoundness
}) => {
const result = {
sidebarColumnWidth,
contentColumnWidth,
notifsColumnWidth,
2025-03-10 23:36:04 -04:00
themeEditorMinWidth: parseInt(themeEditorMinWidth) === 0 ? 'fit-content' : themeEditorMinWidth,
2024-06-21 22:46:01 +03:00
emojiReactionsScale,
emojiSize,
navbarSize,
panelHeaderSize,
textSize
}
switch (forcedRoundness) {
case 'disable':
break
case '0':
result.forcedRoundness = '0'
break
case '1':
result.forcedRoundness = '1px'
break
case '2':
result.forcedRoundness = '0.4rem'
break
default:
}
return result
}
2024-06-13 02:22:47 +03:00
const defaultStyleConfig = extractStyleConfig(defaultState)
2025-02-04 15:23:21 +02:00
export const applyConfig = (input) => {
2024-06-13 02:22:47 +03:00
const config = extractStyleConfig(input)
2024-06-13 02:22:47 +03:00
if (config === defaultStyleConfig) {
return
}
const rules = Object
2024-06-13 02:22:47 +03:00
.entries(config)
2025-02-04 15:23:21 +02:00
.filter(([, v]) => v)
.map(([k, v]) => `--${k}: ${v}`).join(';')
2025-07-02 19:39:25 +03:00
const styleSheet = createStyleSheet('theme-holder')
2025-07-02 19:39:25 +03:00
styleSheet.addRule(`:root { ${rules} }`, 'index-max')
// TODO find a way to make this not apply to theme previews
2024-06-21 22:46:01 +03:00
if (Object.prototype.hasOwnProperty.call(config, 'forcedRoundness')) {
2025-07-02 19:39:25 +03:00
styleSheet.addRule(` *:not(.preview-block) {
2024-06-21 22:46:01 +03:00
--roundness: var(--forcedRoundness) !important;
}`, 'index-max')
}
2025-07-02 19:39:25 +03:00
styleSheet.ready = true
adoptStyleSheets()
}
export const getResourcesIndex = async (url, parser = JSON.parse) => {
2020-02-11 10:42:15 +02:00
const cache = 'no-store'
const customUrl = url.replace(/\.(\w+)$/, '.custom.$1')
let builtin
let custom
const resourceTransform = (resources) => {
return Object
.entries(resources)
.map(([k, v]) => {
if (typeof v === 'object') {
return [k, () => Promise.resolve(v)]
} else if (typeof v === 'string') {
return [
k,
() => window
.fetch(v, { cache })
.then(data => data.text())
.then(text => parser(text))
.catch(e => {
console.error(e)
return null
})
]
} else {
console.error(`Unknown resource format - ${k} is a ${typeof v}`)
return [k, null]
}
})
}
2020-02-11 10:42:15 +02:00
try {
const builtinData = await window.fetch(url, { cache })
const builtinResources = await builtinData.json()
builtin = resourceTransform(builtinResources)
2025-02-04 15:23:21 +02:00
} catch {
builtin = []
console.warn(`Builtin resources at ${url} unavailable`)
}
try {
const customData = await window.fetch(customUrl, { cache })
const customResources = await customData.json()
custom = resourceTransform(customResources)
2025-02-04 15:23:21 +02:00
} catch {
custom = []
console.warn(`Custom resources at ${customUrl} unavailable`)
}
const total = [...custom, ...builtin]
if (total.length === 0) {
return Promise.reject(new Error(`Resource at ${url} and ${customUrl} completely unavailable. Panicking`))
}
return Promise.resolve(Object.fromEntries(total))
2017-01-16 17:44:26 +01:00
}