diff --git a/src/components/select/select_motion.vue b/src/components/select/select_motion.vue index b08d2088..38d3f261 100644 --- a/src/components/select/select_motion.vue +++ b/src/components/select/select_motion.vue @@ -72,6 +72,7 @@ const moveUp = () => { const moveDnValid = computed(() => { return props.selectedId < props.modelValue.length - 1 }) + const moveDn = () => { const newModel = [...props.modelValue] const movable = newModel.splice(props.selectedId.value, 1)[0] diff --git a/src/components/settings_modal/tabs/style_tab/style_tab.js b/src/components/settings_modal/tabs/style_tab/style_tab.js index c5ac8d06..906a2f88 100644 --- a/src/components/settings_modal/tabs/style_tab/style_tab.js +++ b/src/components/settings_modal/tabs/style_tab/style_tab.js @@ -1,4 +1,4 @@ -import { ref, reactive, computed, watch } from 'vue' +import { ref, reactive, computed, watch, provide } from 'vue' import { get, set } from 'lodash' import Select from 'src/components/select/select.vue' @@ -15,12 +15,14 @@ import Tooltip from 'src/components/tooltip/tooltip.vue' import ContrastRatio from 'src/components/contrast_ratio/contrast_ratio.vue' import Preview from '../theme_tab/theme_preview.vue' +import VirtualDirectivesTab from './virtual_directives_tab.vue' + import { init, findColor } from 'src/services/theme_data/theme_data_3.service.js' import { getCssRules, getScopedVersion } from 'src/services/theme_data/css_utils.js' -import { serializeShadow, serialize } from 'src/services/theme_data/iss_serializer.js' +import { serialize } from 'src/services/theme_data/iss_serializer.js' import { deserializeShadow, deserialize } from 'src/services/theme_data/iss_deserializer.js' import { rgb2hex, @@ -70,7 +72,8 @@ export default { PaletteEditor, OpacityInput, ContrastRatio, - Preview + Preview, + VirtualDirectivesTab }, setup () { const exports = {} @@ -147,6 +150,7 @@ export default { }) exports.selectedPaletteId = selectedPaletteId exports.selectedPalette = selectedPalette + provide('selectedPalette', selectedPalette) exports.getNewPalette = () => ({ name: 'new palette', @@ -313,10 +317,6 @@ export default { usedRule = get(fallback, path) } - if (directive === 'shadow') { - console.log('EDITED', usedRule) - console.log('PP', postProcess(usedRule)) - } return postProcess(usedRule) }, set (value) { @@ -378,6 +378,7 @@ export default { return null }) } + provide('normalizeShadows', normalizeShadows) // Shadow is partially edited outside the ShadowControl // for better space utilization @@ -571,7 +572,6 @@ export default { updateSelectedComponent ) - // ## Variables const allCustomVirtualDirectives = [...componentsMap.values()] .map(c => { return c @@ -590,117 +590,21 @@ export default { value: valVal.trim() } }) - const virtualDirectives = reactive(allCustomVirtualDirectives) + + const virtualDirectives = ref(allCustomVirtualDirectives) exports.virtualDirectives = virtualDirectives - - exports.onVirtualDirectivesUpdate = (e) => { - virtualDirectives.splice(0, virtualDirectives.length) - virtualDirectives.push(...e) + exports.updateVirtualDirectives = (value) => { + virtualDirectives.value = value } - const selectedVirtualDirectiveId = ref(0) - exports.selectedVirtualDirectiveId = selectedVirtualDirectiveId - - const selectedVirtualDirective = computed({ - get () { - return virtualDirectives[selectedVirtualDirectiveId.value] - }, - set (value) { - virtualDirectives[selectedVirtualDirectiveId.value].value = value - } - }) - exports.selectedVirtualDirective = selectedVirtualDirective - - exports.selectedVirtualDirectiveValType = computed({ - get () { - return virtualDirectives[selectedVirtualDirectiveId.value].valType - }, - set (value) { - const newValType = value - let newValue - switch (value) { - case 'shadow': - newValue = '0 0 0 #000000 / 1' - break - case 'color': - newValue = '#000000' - break - default: - newValue = 'none' - } - const newName = virtualDirectives[selectedVirtualDirectiveId.value].name - virtualDirectives[selectedVirtualDirectiveId.value] = { - name: newName, - value: newValue, - valType: newValType - } - } - }) - - const draftVirtualDirectiveValid = ref(true) - const draftVirtualDirective = ref({}) - exports.draftVirtualDirective = draftVirtualDirective - - watch( - selectedVirtualDirective, - (directive) => { - switch (directive.valType) { - case 'shadow': { - if (Array.isArray(directive.value)) { - draftVirtualDirective.value = normalizeShadows(directive.value) - } else { - const splitShadow = directive.value.split(/,/g).map(x => x.trim()) - draftVirtualDirective.value = normalizeShadows(splitShadow) - } - break - } - case 'color': - draftVirtualDirective.value = directive.value - break - default: - draftVirtualDirective.value = directive.value - break - } - }, - { immediate: true } - ) - - watch( - draftVirtualDirective, - (directive) => { - try { - switch (selectedVirtualDirective.value.valType) { - case 'shadow': { - virtualDirectives[selectedVirtualDirectiveId.value].value = - directive.map(x => serializeShadow(x)).join(', ') - break - } - default: - virtualDirectives[selectedVirtualDirectiveId.value].value = directive - } - draftVirtualDirectiveValid.value = true - } catch (e) { - console.error('Invalid virtual directive value', e) - draftVirtualDirectiveValid.value = false - } - }, - { immediate: true } - ) - const virtualDirectivesOut = computed(() => { return [ 'Root {', - ...virtualDirectives.map(vd => ` --${vd.name}: ${vd.valType} | ${vd.value};`), + ...virtualDirectives.value.map(vd => ` --${vd.name}: ${vd.valType} | ${vd.value};`), '}' ].join('\n') }) - exports.getNewVirtualDirective = () => ({ - name: 'newDirective', - valType: 'generic', - value: 'foobar' - }) - exports.computeColor = (color) => { const computedColor = findColor(color, { dynamicVars: {}, staticVars: selectedPalette.value }) if (computedColor) { @@ -708,6 +612,7 @@ export default { } return null } + provide('computeColor', exports.computeColor) exports.contrast = computed(() => { return getContrast( @@ -735,7 +640,7 @@ export default { const virtualDirectivesRule = { component: 'Root', directives: Object.fromEntries( - virtualDirectives.map(vd => [`--${vd.name}`, `${vd.valType} | ${vd.value}`]) + virtualDirectives.value.map(vd => [`--${vd.name}`, `${vd.valType} | ${vd.value}`]) ) } @@ -782,14 +687,13 @@ export default { exports.author.value = metaIn.author exports.website.value = metaIn.website - virtualDirectives.splice(0, virtualDirectives.length) const newVirtualDirectives = Object .entries(rootComponent.directives) .map(([name, value]) => { const [valType, valVal] = value.split('|').map(x => x.trim()) return { name: name.substring(2), valType, value: valVal } }) - virtualDirectives.push(...newVirtualDirectives) + virtualDirectives.value = newVirtualDirectives onPalettesUpdate(palettesIn.map(x => ({ name: x.variant, ...x.directives }))) diff --git a/src/components/settings_modal/tabs/style_tab/style_tab.vue b/src/components/settings_modal/tabs/style_tab/style_tab.vue index 5ca274c3..53fa0aac 100644 --- a/src/components/settings_modal/tabs/style_tab/style_tab.vue +++ b/src/components/settings_modal/tabs/style_tab/style_tab.vue @@ -355,89 +355,13 @@ /> -
- - - -
-
- - - - -
- - -
-
+ :model-value="virtualDirectives" + @update:modelValue="updateVirtualDirectives" + :normalize-shadows="normalizeShadows" + /> diff --git a/src/components/settings_modal/tabs/style_tab/virtual_directives_tab.js b/src/components/settings_modal/tabs/style_tab/virtual_directives_tab.js new file mode 100644 index 00000000..8233d907 --- /dev/null +++ b/src/components/settings_modal/tabs/style_tab/virtual_directives_tab.js @@ -0,0 +1,132 @@ +import { ref, computed, watch, inject } from 'vue' + +import Select from 'src/components/select/select.vue' +import SelectMotion from 'src/components/select/select_motion.vue' +import ShadowControl from 'src/components/shadow_control/shadow_control.vue' +import ColorInput from 'src/components/color_input/color_input.vue' + +import { serializeShadow } from 'src/services/theme_data/iss_serializer.js' + +// helper for debugging +// eslint-disable-next-line no-unused-vars +const toValue = (x) => JSON.parse(JSON.stringify(x === undefined ? 'null' : x)) + +export default { + components: { + Select, + SelectMotion, + ShadowControl, + ColorInput + }, + props: ['modelValue'], + emits: ['update:modelValue'], + setup (props, context) { + const exports = {} + const emit = context.emit + + exports.emit = emit + exports.computeColor = inject('computeColor') + exports.selectedPalette = inject('selectedPalette') + + const selectedVirtualDirectiveId = ref(0) + exports.selectedVirtualDirectiveId = selectedVirtualDirectiveId + + const selectedVirtualDirective = computed({ + get () { + return props.modelValue[selectedVirtualDirectiveId.value] + }, + set (value) { + const newVD = [...props.modelValue] + newVD[selectedVirtualDirectiveId.value] = value + + emit('update:modelValue', newVD) + } + }) + exports.selectedVirtualDirective = selectedVirtualDirective + + exports.selectedVirtualDirectiveValType = computed({ + get () { + return props.modelValue[selectedVirtualDirectiveId.value].valType + }, + set (value) { + const newValType = value + let newValue + switch (value) { + case 'shadow': + newValue = '0 0 0 #000000 / 1' + break + case 'color': + newValue = '#000000' + break + default: + newValue = 'none' + } + const newName = props.modelValue[selectedVirtualDirectiveId.value].name + props.modelValue[selectedVirtualDirectiveId.value] = { + name: newName, + value: newValue, + valType: newValType + } + } + }) + + const draftVirtualDirectiveValid = ref(true) + const draftVirtualDirective = ref({}) + exports.draftVirtualDirective = draftVirtualDirective + const normalizeShadows = inject('normalizeShadows') + + watch( + selectedVirtualDirective, + (directive) => { + switch (directive.valType) { + case 'shadow': { + if (Array.isArray(directive.value)) { + draftVirtualDirective.value = normalizeShadows(directive.value) + } else { + const splitShadow = directive.value.split(/,/g).map(x => x.trim()) + draftVirtualDirective.value = normalizeShadows(splitShadow) + } + break + } + case 'color': + draftVirtualDirective.value = directive.value + break + default: + draftVirtualDirective.value = directive.value + break + } + }, + { immediate: true } + ) + + watch( + draftVirtualDirective, + (directive) => { + try { + switch (selectedVirtualDirective.value.valType) { + case 'shadow': { + props.modelValue[selectedVirtualDirectiveId.value].value = + directive.map(x => serializeShadow(x)).join(', ') + break + } + default: + props.modelValue[selectedVirtualDirectiveId.value].value = directive + } + draftVirtualDirectiveValid.value = true + } catch (e) { + console.error('Invalid virtual directive value', e) + draftVirtualDirectiveValid.value = false + } + }, + { immediate: true } + ) + + exports.getNewVirtualDirective = () => ({ + name: 'newDirective', + valType: 'generic', + value: 'foobar' + }) + + return exports + } +} diff --git a/src/components/settings_modal/tabs/style_tab/virtual_directives_tab.vue b/src/components/settings_modal/tabs/style_tab/virtual_directives_tab.vue new file mode 100644 index 00000000..c66b3af4 --- /dev/null +++ b/src/components/settings_modal/tabs/style_tab/virtual_directives_tab.vue @@ -0,0 +1,83 @@ + + +