shadow editor now can handle expressions (functions and variables)

This commit is contained in:
Henry Jameson 2024-10-04 00:27:53 +03:00
parent 24663b2f04
commit c937736fea
7 changed files with 285 additions and 207 deletions

View file

@ -219,9 +219,13 @@ export default {
return selectors.map(x => x.substring(1)).join('') return selectors.map(x => x.substring(1)).join('')
}) })
const previewCss = computed(() => { const previewCss = computed(() => {
const scoped = getCssRules(previewRules) try {
.map(simulatePseudoSelectors) const scoped = getCssRules(previewRules).map(simulatePseudoSelectors)
return scoped.join('\n') return scoped.join('\n')
} catch (e) {
console.error('Invalid ruleset', e)
return null
}
}) })
// ### Rules stuff aka meat and potatoes // ### Rules stuff aka meat and potatoes
@ -415,8 +419,8 @@ export default {
}) })
const updatePreview = () => { const updatePreview = () => {
previewRules.splice(0, previewRules.length) try {
previewRules.push(...init({ const rules = init({
inputRuleset: editorFriendlyToOriginal.value, inputRuleset: editorFriendlyToOriginal.value,
initialStaticVars: { initialStaticVars: {
...palette.value ...palette.value
@ -425,7 +429,12 @@ export default {
rootComponentName: selectedComponentName.value, rootComponentName: selectedComponentName.value,
editMode: true, editMode: true,
debug: true debug: true
}).eager) }).eager
previewRules.splice(0, previewRules.length)
previewRules.push(...rules)
} catch (e) {
console.error('Could not compile preview theme', e)
}
} }
const updateSelectedComponent = () => { const updateSelectedComponent = () => {

View file

@ -125,7 +125,7 @@
:shadow-control="isShadowTabOpen" :shadow-control="isShadowTabOpen"
:preview-class="previewClass" :preview-class="previewClass"
:preview-style="editorHintStyle" :preview-style="editorHintStyle"
:disabled="!editedSubShadow" :disabled="!editedSubShadow && typeof editedShadow !== 'string'"
:shadow="editedSubShadow" :shadow="editedSubShadow"
@update:shadow="({ axis, value }) => updateSubShadow(axis, value)" @update:shadow="({ axis, value }) => updateSubShadow(axis, value)"
/> />

View file

@ -21,7 +21,9 @@ library.add(
faPlus faPlus
) )
const toModel = (object = {}) => ({ const toModel = (input) => {
if (typeof input === 'object') {
return {
x: 0, x: 0,
y: 0, y: 0,
blur: 0, blur: 0,
@ -29,8 +31,12 @@ const toModel = (object = {}) => ({
inset: false, inset: false,
color: '#000000', color: '#000000',
alpha: 1, alpha: 1,
...object ...input
}) }
} else if (typeof input === 'string') {
return input
}
}
export default { export default {
props: [ props: [
@ -56,13 +62,30 @@ export default {
this.cValue = (this.modelValue ?? this.fallback ?? []).map(toModel) this.cValue = (this.modelValue ?? this.fallback ?? []).map(toModel)
}, },
computed: { computed: {
selected () { selectedType: {
get () {
return typeof this.selected
},
set (newType) {
this.selected = toModel(newType === 'object' ? {} : '')
}
},
selected: {
get () {
const selected = this.cValue[this.selectedId] const selected = this.cValue[this.selectedId]
if (selected) { console.log('SELECTED', selected)
if (selected && typeof selected === 'object') {
return { ...selected } return { ...selected }
} else if (typeof selected === 'string') {
return selected
} }
return null return null
}, },
set (value) {
this.cValue[this.selectedId] = toModel(value)
this.$emit('update:modelValue', this.cValue)
}
},
present () { present () {
return this.selected != null && !this.usingFallback return this.selected != null && !this.usingFallback
}, },
@ -82,6 +105,7 @@ export default {
return this.modelValue == null return this.modelValue == null
}, },
style () { style () {
try {
if (this.separateInset) { if (this.separateInset) {
return { return {
filter: getCssShadowFilter(this.cValue), filter: getCssShadowFilter(this.cValue),
@ -91,6 +115,11 @@ export default {
return { return {
boxShadow: getCssShadow(this.cValue) boxShadow: getCssShadow(this.cValue)
} }
} catch (e) {
return {
border: '1px solid red'
}
}
} }
}, },
watch: { watch: {
@ -99,6 +128,13 @@ export default {
} }
}, },
methods: { methods: {
getSubshadowLabel (shadow, index) {
if (typeof shadow === 'object') {
return shadow?.name ?? this.$t('settings.style.shadows.shadow_id', { value: index })
} else if (typeof shadow === 'string') {
return shadow || this.$t('settings.style.shadows.empty_expression')
}
},
updateProperty: throttle(function (prop, value) { updateProperty: throttle(function (prop, value) {
this.cValue[this.selectedId][prop] = value this.cValue[this.selectedId][prop] = value
if (prop === 'inset' && value === false && this.separateInset) { if (prop === 'inset' && value === false && this.separateInset) {

View file

@ -4,6 +4,7 @@
justify-content: stretch; justify-content: stretch;
grid-gap: 0.25em; grid-gap: 0.25em;
margin-bottom: 1em; margin-bottom: 1em;
width: 100%;
.shadow-switcher { .shadow-switcher {
order: 1; order: 1;
@ -37,6 +38,9 @@
min-width: 10em; min-width: 10em;
margin-left: 0.125em; margin-left: 0.125em;
margin-right: 0.125em; margin-right: 0.125em;
display: grid;
grid-template-rows: auto 1fr;
grid-gap: 0.25em;
/* hack */ /* hack */
.input-boolean { .input-boolean {
@ -52,6 +56,11 @@
flex: 1 0 5em; flex: 1 0 5em;
} }
.shadow-expression {
width: 100%;
height: 100%;
}
.id-control { .id-control {
align-items: stretch; align-items: stretch;
@ -100,6 +109,5 @@
} }
.inset-tooltip { .inset-tooltip {
padding: 0.5em;
max-width: 30em; max-width: 30em;
} }

View file

@ -8,7 +8,6 @@
class="shadow-preview" class="shadow-preview"
:shadow-control="true" :shadow-control="true"
:shadow="selected" :shadow="selected"
:preview-style="style"
:disabled="disabled || !present" :disabled="disabled || !present"
@update:shadow="({ axis, value }) => updateProperty(axis, value)" @update:shadow="({ axis, value }) => updateProperty(axis, value)"
/> />
@ -18,7 +17,7 @@
v-model="selectedId" v-model="selectedId"
class="shadow-list" class="shadow-list"
size="10" size="10"
:disabled="shadowsAreNull" :disabled="disabled || shadowsAreNull"
> >
<option <option
v-for="(shadow, index) in cValue" v-for="(shadow, index) in cValue"
@ -26,7 +25,7 @@
:value="index" :value="index"
:class="{ '-active': index === Number(selectedId) }" :class="{ '-active': index === Number(selectedId) }"
> >
{{ shadow?.name ?? $t('settings.style.shadows.shadow_id', { value: index }) }} {{ getSubshadowLabel(shadow, index) }}
</option> </option>
</Select> </Select>
<div <div
@ -78,6 +77,27 @@
</div> </div>
</div> </div>
<div class="shadow-tweak"> <div class="shadow-tweak">
<Select
v-model="selectedType"
:disabled="disabled || !present"
>
<option value="object">
{{ $t('settings.style.shadows.raw') }}
</option>
<option value="string">
{{ $t('settings.style.shadows.expression') }}
</option>
</Select>
<template v-if="selectedType === 'string'">
<textarea
v-model="selected"
class="input shadow-expression"
:disabled="disabled || shadowsAreNull"
:class="{disabled: disabled || shadowsAreNull}"
>
</textarea>
</template>
<template v-else-if="selectedType === 'object'">
<div <div
:class="{ disabled: disabled || !present }" :class="{ disabled: disabled || !present }"
class="name-control style-control" class="name-control style-control"
@ -218,7 +238,7 @@
</div> </div>
</template> </template>
<template #content> <template #content>
<div class="inset-tooltip"> <div class="inset-tooltip tooltip">
<i18n-t <i18n-t
scope="global" scope="global"
keypath="settings.style.shadows.filter_hint.always_drop_shadow" keypath="settings.style.shadows.filter_hint.always_drop_shadow"
@ -247,6 +267,7 @@
</div> </div>
</template> </template>
</Popover> </Popover>
</template>
</div> </div>
</div> </div>
</template> </template>

View file

@ -966,6 +966,9 @@
"blur": "Blur", "blur": "Blur",
"spread": "Spread", "spread": "Spread",
"inset": "Inset", "inset": "Inset",
"raw": "Plain shadow",
"expression": "Expression (advanced)",
"empty_expression": "Empty expression",
"hintV3": "For shadows you can also use the {0} notation to use other color slot.", "hintV3": "For shadows you can also use the {0} notation to use other color slot.",
"filter_hint": { "filter_hint": {
"always_drop_shadow": "Warning, this shadow always uses {0} when browser supports it.", "always_drop_shadow": "Warning, this shadow always uses {0} when browser supports it.",

View file

@ -44,8 +44,8 @@ const findShadow = (shadows, { dynamicVars, staticVars }) => {
if (shadow.startsWith('$')) { if (shadow.startsWith('$')) {
targetShadow = process(shadow, shadowFunctions, { findColor, findShadow }, { dynamicVars, staticVars }) targetShadow = process(shadow, shadowFunctions, { findColor, findShadow }, { dynamicVars, staticVars })
} else if (shadow.startsWith('--')) { } else if (shadow.startsWith('--')) {
const [variable] = shadow.split(/,/g).map(str => str.trim()) // discarding modifier since it's not supported // modifiers are completely unsupported here
const variableSlot = variable.substring(2) const variableSlot = shadow.substring(2)
return findShadow(staticVars[variableSlot], { dynamicVars, staticVars }) return findShadow(staticVars[variableSlot], { dynamicVars, staticVars })
} else { } else {
targetShadow = parseShadow(shadow) targetShadow = parseShadow(shadow)
@ -66,6 +66,7 @@ const findColor = (color, { dynamicVars, staticVars }) => {
if (typeof color !== 'string' || (!color.startsWith('--') && !color.startsWith('$'))) return color if (typeof color !== 'string' || (!color.startsWith('--') && !color.startsWith('$'))) return color
let targetColor = null let targetColor = null
if (color.startsWith('--')) { if (color.startsWith('--')) {
// Modifier support is pretty much for v2 themes only
const [variable, modifier] = color.split(/,/g).map(str => str.trim()) const [variable, modifier] = color.split(/,/g).map(str => str.trim())
const variableSlot = variable.substring(2) const variableSlot = variable.substring(2)
if (variableSlot === 'stack') { if (variableSlot === 'stack') {
@ -421,7 +422,7 @@ export const init = ({
break break
} }
case 'shadow': { case 'shadow': {
const shadow = value.split(/,/g).map(s => s.trim()) const shadow = value.split(/,/g).map(s => s.trim()).filter(x => x)
dynamicVars[k] = shadow dynamicVars[k] = shadow
if (combination.component === rootComponentName) { if (combination.component === rootComponentName) {
staticVars[k.substring(2)] = shadow staticVars[k.substring(2)] = shadow