improve robustness and responsiveness

This commit is contained in:
Henry Jameson 2024-11-12 21:10:02 +02:00
parent 4aaf6bcc59
commit a2a58dc082
2 changed files with 31 additions and 15 deletions

View file

@ -1,6 +1,6 @@
import { ref, reactive, computed, watch, provide } from 'vue' import { ref, reactive, computed, watch, watchEffect, provide } from 'vue'
import { useStore } from 'vuex' import { useStore } from 'vuex'
import { get, set, unset } from 'lodash' import { get, set, unset, throttle } from 'lodash'
import Select from 'src/components/select/select.vue' import Select from 'src/components/select/select.vue'
import SelectMotion from 'src/components/select/select_motion.vue' import SelectMotion from 'src/components/select/select_motion.vue'
@ -639,13 +639,21 @@ export default {
const overallPreviewRules = ref([]) const overallPreviewRules = ref([])
exports.overallPreviewRules = overallPreviewRules exports.overallPreviewRules = overallPreviewRules
const overallPreviewCssRules = computed(() => getScopedVersion( const overallPreviewCssRules = ref([])
getCssRules(overallPreviewRules.value), watchEffect(throttle(() => {
'#edited-style-preview' try {
).join('\n')) overallPreviewCssRules.value = getScopedVersion(
getCssRules(overallPreviewRules.value),
'#edited-style-preview'
).join('\n')
} catch (e) {
console.error(e)
}
}, 500))
exports.overallPreviewCssRules = overallPreviewCssRules exports.overallPreviewCssRules = overallPreviewCssRules
const updateOverallPreview = () => { const updateOverallPreview = throttle(() => {
try { try {
overallPreviewRules.value = init({ overallPreviewRules.value = init({
inputRuleset: exportRules.value, inputRuleset: exportRules.value,
@ -657,7 +665,7 @@ export default {
console.error('Could not compile preview theme', e) console.error('Could not compile preview theme', e)
return null return null
} }
} }, 1000)
// //
// Apart from "hover" we can't really show how component looks like in // Apart from "hover" we can't really show how component looks like in
// certain states, so we have to fake them. // certain states, so we have to fake them.
@ -730,10 +738,13 @@ export default {
return r.component === 'Root' return r.component === 'Root'
}) })
const rootDirectivesEntries = Object.entries(rootComponent.directives) const rootDirectivesEntries = Object.entries(rootComponent.directives)
const directives = Object.fromEntries( const directives = {}
rootDirectivesEntries rootDirectivesEntries
.filter(([k, v]) => k.startsWith('--') && v.startsWith('color | ')) .filter(([k, v]) => k.startsWith('--') && v.startsWith('color | '))
.map(([k, v]) => [k.substring(2), v.substring('color | '.length)])) .map(([k, v]) => [k.substring(2), v.substring('color | '.length)])
.forEach(([k, v]) => {
directives[k] = findColor(v, { dynamicVars: {}, staticVars: directives })
})
return directives return directives
}) })
provide('staticVars', staticVars) provide('staticVars', staticVars)

View file

@ -112,9 +112,14 @@ export default {
}, },
getColorFallback () { getColorFallback () {
if (this.staticVars && this.selected?.color) { if (this.staticVars && this.selected?.color) {
const computedColor = findColor(this.selected.color, { dynamicVars: {}, staticVars: this.staticVars }, true) try {
if (computedColor) return rgb2hex(computedColor) const computedColor = findColor(this.selected.color, { dynamicVars: {}, staticVars: this.staticVars }, true)
return null if (computedColor) return rgb2hex(computedColor)
return null
} catch (e) {
console.warn(e)
return null
}
} else { } else {
return this.currentFallback?.color return this.currentFallback?.color
} }