allow setting palettes from style

This commit is contained in:
Henry Jameson 2024-11-12 23:24:28 +02:00
parent d787fb1a60
commit 86585cc644
9 changed files with 143 additions and 46 deletions

View file

@ -390,6 +390,13 @@ const afterStoreSetup = async ({ store, i18n }) => {
app.use(store) app.use(store)
app.use(i18n) app.use(i18n)
// Little thing to get out of invalid theme state
window.resetThemes = () => {
store.dispatch('resetThemeV3')
store.dispatch('resetThemeV3Palette')
store.dispatch('resetThemeV2')
}
app.use(vClickOutside) app.use(vClickOutside)
app.use(VBodyScrollLock) app.use(VBodyScrollLock)
app.use(VueVirtualScroller) app.use(VueVirtualScroller)

View file

@ -33,7 +33,7 @@ const AppearanceTab = {
data () { data () {
return { return {
availableStyles: [], availableStyles: [],
availablePalettes: [], bundledPalettes: [],
fileImporter: newImporter({ fileImporter: newImporter({
accept: '.json, .piss', accept: '.json, .piss',
validator: this.importValidator, validator: this.importValidator,
@ -41,8 +41,8 @@ const AppearanceTab = {
onImportFailure: this.onImportFailure onImportFailure: this.onImportFailure
}), }),
palettesKeys: [ palettesKeys: [
'background', 'bg',
'foreground', 'fg',
'link', 'link',
'text', 'text',
'cRed', 'cRed',
@ -103,13 +103,13 @@ const AppearanceTab = {
})) }))
}) })
updateIndex('palette').then(palettes => { updateIndex('palette').then(bundledPalettes => {
palettes.forEach(([key, palettePromise]) => palettePromise.then(v => { bundledPalettes.forEach(([key, palettePromise]) => palettePromise.then(v => {
if (Array.isArray(v)) { if (Array.isArray(v)) {
const [ const [
name, name,
background, bg,
foreground, fg,
text, text,
link, link,
cRed = '#FF0000', cRed = '#FF0000',
@ -117,9 +117,9 @@ const AppearanceTab = {
cBlue = '#0000FF', cBlue = '#0000FF',
cOrange = '#E3FF00' cOrange = '#E3FF00'
] = v ] = v
this.availablePalettes.push({ key, name, background, foreground, text, link, cRed, cBlue, cGreen, cOrange }) this.bundledPalettes.push({ key, name, bg, fg, text, link, cRed, cBlue, cGreen, cOrange })
} else { } else {
this.availablePalettes.push({ key, ...v }) this.bundledPalettes.push({ key, ...v })
} }
})) }))
}) })
@ -147,6 +147,50 @@ const AppearanceTab = {
}) })
}, },
computed: { computed: {
availablePalettes () {
return [
...this.bundledPalettes,
...this.stylePalettes
]
},
stylePalettes () {
if (!this.mergedConfig.styleCustomData) return
const meta = this.mergedConfig.styleCustomData
.find(x => x.component === '@meta')
const result = this.mergedConfig.styleCustomData
.filter(x => x.component.startsWith('@palette'))
.map(x => {
const {
variant,
bg,
fg,
text,
link,
accent,
cRed,
cBlue,
cGreen,
cOrange,
wallpaper
} = x
const result = {
name: `${meta.name}: ${variant}`,
bg,
fg,
text,
link,
accent,
cRed,
cBlue,
cGreen,
cOrange,
wallpaper
}
return Object.fromEntries(Object.entries(result).filter(([k, v]) => v))
})
return result
},
noIntersectionObserver () { noIntersectionObserver () {
return !window.IntersectionObserver return !window.IntersectionObserver
}, },
@ -253,6 +297,11 @@ const AppearanceTab = {
this.$store.dispatch('setPalette', name) this.$store.dispatch('setPalette', name)
this.$store.dispatch('applyTheme') this.$store.dispatch('applyTheme')
}, },
setPaletteCustom (p) {
this.$store.dispatch('resetThemeV2')
this.$store.dispatch('setPaletteCustom', p)
this.$store.dispatch('applyTheme')
},
resetTheming (name) { resetTheming (name) {
this.$store.dispatch('resetThemeV2') this.$store.dispatch('resetThemeV2')
this.$store.dispatch('resetThemeV3') this.$store.dispatch('resetThemeV3')

View file

@ -25,6 +25,7 @@
grid-template-columns: 1fr 1fr; grid-template-columns: 1fr 1fr;
grid-gap: 0.5em; grid-gap: 0.5em;
h4,
.unsupported-theme-v2 { .unsupported-theme-v2 {
grid-column: 1 / span 2; grid-column: 1 / span 2;
} }

View file

@ -72,8 +72,9 @@
<h3>{{ $t('settings.style.themes3.palette.label') }}</h3> <h3>{{ $t('settings.style.themes3.palette.label') }}</h3>
<div class="palettes"> <div class="palettes">
<template v-if="customThemeVersion === 'v3'"> <template v-if="customThemeVersion === 'v3'">
<h4>{{ $t('settings.style.themes3.palette.bundled') }}</h4>
<button <button
v-for="p in availablePalettes" v-for="p in bundledPalettes"
:key="p.name" :key="p.name"
class="btn button-default palette-entry" class="btn button-default palette-entry"
:class="{ toggled: isPaletteActive(p.key) }" :class="{ toggled: isPaletteActive(p.key) }"
@ -89,6 +90,26 @@
:style="{ backgroundColor: p[c], border: '1px solid ' + (p[c] ?? 'var(--text)') }" :style="{ backgroundColor: p[c], border: '1px solid ' + (p[c] ?? 'var(--text)') }"
/> />
</button> </button>
<h4 v-if="stylePalettes?.length > 0">
{{ $t('settings.style.themes3.palette.style') }}
</h4>
<button
v-for="p in stylePalettes || []"
:key="p.name"
class="btn button-default palette-entry"
:class="{ toggled: isPaletteActive(p.key) }"
@click="() => setPaletteCustom(p)"
>
<label>
{{ p.name }}
</label>
<span
v-for="c in palettesKeys"
:key="c"
class="palette-square"
:style="{ backgroundColor: p[c], border: '1px solid ' + (p[c] ?? 'var(--text)') }"
/>
</button>
</template> </template>
<template v-else-if="customThemeVersion === 'v2'"> <template v-else-if="customThemeVersion === 'v2'">
<div class="alert neutral theme-notice unsupported-theme-v2"> <div class="alert neutral theme-notice unsupported-theme-v2">

View file

@ -101,6 +101,14 @@ export default {
].join('\n') ].join('\n')
}) })
const metaRule = computed(() => ({
component: '@meta',
name: exports.name.value,
author: exports.author.value,
license: exports.license.value,
website: exports.website.value
}))
// ## Palette stuff // ## Palette stuff
const palettes = reactive([ const palettes = reactive([
{ {
@ -169,6 +177,22 @@ export default {
cOrange: '#ffa500' cOrange: '#ffa500'
}) })
// Raw format
const palettesRule = computed(() => {
return palettes.map(palette => {
const { name, ...rest } = palette
return {
component: '@palette',
variant: name,
...Object
.entries(rest)
.filter(([k, v]) => v)
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})
}
})
})
// Text format
const palettesOut = computed(() => { const palettesOut = computed(() => {
return palettes.map(({ name, ...palette }) => { return palettes.map(({ name, ...palette }) => {
const entries = Object const entries = Object
@ -519,6 +543,15 @@ export default {
virtualDirectives.value = value virtualDirectives.value = value
} }
// Raw format
const virtualDirectivesRule = computed(() => ({
component: 'Root',
directives: Object.fromEntries(
virtualDirectives.value.map(vd => [`--${vd.name}`, `${vd.valType} | ${vd.value}`])
)
}))
// Text format
const virtualDirectivesOut = computed(() => { const virtualDirectivesOut = computed(() => {
return [ return [
'Root {', 'Root {',
@ -548,25 +581,6 @@ export default {
) )
}) })
const paletteRule = computed(() => {
const { name, ...rest } = selectedPalette.value
return {
component: 'Root',
directives: Object
.entries(rest)
.filter(([k, v]) => v)
.map(([k, v]) => ['--' + k, v])
.reduce((acc, [k, v]) => ({ ...acc, [k]: `color | ${v}` }), {})
}
})
const virtualDirectivesRule = computed(() => ({
component: 'Root',
directives: Object.fromEntries(
virtualDirectives.value.map(vd => [`--${vd.name}`, `${vd.valType} | ${vd.value}`])
)
}))
// ## Export and Import // ## Export and Import
const styleExporter = newExporter({ const styleExporter = newExporter({
filename: () => exports.name.value ?? 'pleroma_theme', filename: () => exports.name.value ?? 'pleroma_theme',
@ -613,6 +627,15 @@ export default {
} }
}) })
// Raw format
const exportRules = computed(() => [
metaRule.value,
...palettesRule.value,
virtualDirectivesRule.value,
...editorFriendlyToOriginal.value
])
// Text format
const exportStyleData = computed(() => { const exportStyleData = computed(() => {
return [ return [
metaOut.value, metaOut.value,
@ -630,12 +653,6 @@ export default {
styleImporter.importData() styleImporter.importData()
} }
const exportRules = computed(() => [
paletteRule.value,
virtualDirectivesRule.value,
...editorFriendlyToOriginal.value
])
exports.applyStyle = () => { exports.applyStyle = () => {
store.dispatch('setStyleCustom', exportRules.value) store.dispatch('setStyleCustom', exportRules.value)
} }

View file

@ -771,7 +771,9 @@
"cGreen": "Green color", "cGreen": "Green color",
"cOrange": "Orange color", "cOrange": "Orange color",
"wallpaper": "Wallpaper", "wallpaper": "Wallpaper",
"v2_unsupported": "Older v2 themes don't support palettes. Switch to v3 theme to make use of palettes" "v2_unsupported": "Older v2 themes don't support palettes. Switch to v3 theme to make use of palettes",
"bundled": "Bundled palettes",
"style": "Palettes provided by selected style"
}, },
"editor": { "editor": {
"title": "Style", "title": "Style",

View file

@ -460,8 +460,8 @@ const interfaceMod = {
if (Array.isArray(paletteDataUsed)) { if (Array.isArray(paletteDataUsed)) {
const [ const [
name, name,
background, bg,
foreground, fg,
text, text,
link, link,
cRed = '#FF0000', cRed = '#FF0000',
@ -469,10 +469,10 @@ const interfaceMod = {
cBlue = '#0000FF', cBlue = '#0000FF',
cOrange = '#E3FF00' cOrange = '#E3FF00'
] = paletteDataUsed ] = paletteDataUsed
paletteDataUsed = { name, background, foreground, text, link, cRed, cBlue, cGreen, cOrange } paletteDataUsed = { name, bg, fg, text, link, cRed, cBlue, cGreen, cOrange }
} }
console.log(paletteDataUsed)
console.log('USCD', userStyleCustomData)
const style = await getData( const style = await getData(
'style', 'style',
stylesIndex, stylesIndex,

View file

@ -227,8 +227,8 @@ export const init = ({
bScore += b.component === 'Text' ? 1 : 0 bScore += b.component === 'Text' ? 1 : 0
// Debug // Debug
a.specifityScore = aScore a._specificityScore = aScore
b.specifityScore = bScore b._specificityScore = bScore
if (aScore === bScore) { if (aScore === bScore) {
return ai - bi return ai - bi

View file

@ -3,8 +3,8 @@
"pleroma-light": [ "Pleroma Light", "#f2f4f6", "#dbe0e8", "#304055", "#f86f0f", "#d31014", "#0fa00f", "#0095ff", "#ffa500" ], "pleroma-light": [ "Pleroma Light", "#f2f4f6", "#dbe0e8", "#304055", "#f86f0f", "#d31014", "#0fa00f", "#0095ff", "#ffa500" ],
"classic-dark": { "classic-dark": {
"name": "Classic Dark", "name": "Classic Dark",
"background": "#161c20", "bg": "#161c20",
"foreground": "#282e32", "fg": "#282e32",
"text": "#b9b9b9", "text": "#b9b9b9",
"link": "#baaa9c", "link": "#baaa9c",
"cRed": "#d31014", "cRed": "#d31014",
@ -16,8 +16,8 @@
"pleroma-amoled": [ "Pleroma Dark AMOLED", "#000000", "#111111", "#b0b0b1", "#d8a070", "#aa0000", "#0fa00f", "#0095ff", "#d59500"], "pleroma-amoled": [ "Pleroma Dark AMOLED", "#000000", "#111111", "#b0b0b1", "#d8a070", "#aa0000", "#0fa00f", "#0095ff", "#d59500"],
"tomorrow-night": { "tomorrow-night": {
"name": "Tomorrow Night", "name": "Tomorrow Night",
"background": "#1d1f21", "bg": "#1d1f21",
"foreground": "#373b41", "fg": "#373b41",
"link": "#81a2be", "link": "#81a2be",
"text": "#c5c8c6", "text": "#c5c8c6",
"cRed": "#cc6666", "cRed": "#cc6666",