Import/export works!

This commit is contained in:
Henry Jameson 2024-10-09 23:54:34 +03:00
parent 78e4f0ecd8
commit 202d77e0cc
2 changed files with 79 additions and 40 deletions

View file

@ -21,14 +21,14 @@ import {
getScopedVersion getScopedVersion
} from 'src/services/theme_data/css_utils.js' } from 'src/services/theme_data/css_utils.js'
import { serializeShadow, serialize } from 'src/services/theme_data/iss_serializer.js' import { serializeShadow, serialize } from 'src/services/theme_data/iss_serializer.js'
import { parseShadow /* , deserialize */ } from 'src/services/theme_data/iss_deserializer.js' import { parseShadow, deserialize } from 'src/services/theme_data/iss_deserializer.js'
import { import {
// rgb2hex, // rgb2hex,
hex2rgb, hex2rgb,
getContrastRatio getContrastRatio
} from 'src/services/color_convert/color_convert.js' } from 'src/services/color_convert/color_convert.js'
import { import {
// newImporter, newImporter,
newExporter newExporter
} from 'src/services/export_import/export_import.js' } from 'src/services/export_import/export_import.js'
@ -195,47 +195,53 @@ export default {
// The native structure of separate rules and the child -> parent // The native structure of separate rules and the child -> parent
// relation isn't very convenient for editor, we replace the array // relation isn't very convenient for editor, we replace the array
// and child -> parent structure with map and parent -> child structure // and child -> parent structure with map and parent -> child structure
const rulesToEditorFriendly = (rules, root = {}) => rules.reduce((acc, rule) => {
const { parent: rParent, component: rComponent } = rule
const parent = rParent ?? rule
const hasChildren = !!rParent
const child = hasChildren ? rule : null
const {
component: pComponent,
variant: pVariant = 'normal',
state: pState = [] // no relation to Intel CPUs whatsoever
} = parent
const pPath = `${hasChildren ? pComponent : rComponent}.${pVariant}.${normalizeStates(pState)}`
let output = get(acc, pPath)
if (!output) {
set(acc, pPath, {})
output = get(acc, pPath)
}
if (hasChildren) {
acc._children = acc._children ?? {}
const {
component: cComponent,
variant: cVariant = 'normal',
state: cState = [],
directives
} = child
const cPath = `${cComponent}.${cVariant}.${normalizeStates(cState)}`
set(output._children, cPath, directives)
} else {
output.directives = parent.directives
}
return acc
}, root)
const editorFriendlyFallbackStructure = computed(() => { const editorFriendlyFallbackStructure = computed(() => {
const root = {} const root = {}
componentKeys.forEach((componentKey) => { componentKeys.forEach((componentKey) => {
const componentValue = componentsMap.get(componentKey) const componentValue = componentsMap.get(componentKey)
const { defaultRules } = componentValue const { defaultRules, name } = componentValue
defaultRules.forEach((rule) => { rulesToEditorFriendly(
const { parent: rParent } = rule defaultRules.map((rule) => ({ ...rule, component: name })),
const parent = rParent ?? rule root
const hasChildren = !!rParent )
const child = hasChildren ? rule : null
const {
component: pComponent,
variant: pVariant = 'normal',
state: pState = [] // no relation to Intel CPUs whatsoever
} = parent
const pPath = `${hasChildren ? pComponent : componentValue.name}.${pVariant}.${normalizeStates(pState)}`
let output = get(root, pPath)
if (!output) {
set(root, pPath, {})
output = get(root, pPath)
}
if (hasChildren) {
output._children = output._children ?? {}
const {
component: cComponent,
variant: cVariant = 'normal',
state: cState = [],
directives
} = child
const cPath = `${cComponent}.${cVariant}.${normalizeStates(cState)}`
set(output._children, cPath, directives)
} else {
output.directives = parent.directives
}
})
}) })
return root return root
@ -642,6 +648,32 @@ export default {
extension: 'piss', extension: 'piss',
getExportedObject: () => exportStyleData.value getExportedObject: () => exportStyleData.value
}) })
const styleImporter = newImporter({
accept: '.piss',
parser: (string) => deserialize(string),
onImport (parsed, filename) {
const editorComponents = parsed.filter(x => x.component.startsWith('@'))
const rules = parsed.filter(x => !x.component.startsWith('@'))
const metaIn = editorComponents.find(x => x.component === '@meta').directives
const palettesIn = editorComponents.filter(x => x.component === '@palette').directives
name.value = metaIn.name
license.value = metaIn.license
author.value = metaIn.author
website.value = metaIn.website
Object.keys(allEditedRules).forEach((k) => delete allEditedRules[k])
rules.forEach(rule => {
rulesToEditorFriendly(
[rule],
allEditedRules
)
})
}
})
const exportStyleData = computed(() => { const exportStyleData = computed(() => {
return [ return [
metaOut.value, metaOut.value,
@ -649,10 +681,15 @@ export default {
serialize(editorFriendlyToOriginal.value) serialize(editorFriendlyToOriginal.value)
].join('\n\n') ].join('\n\n')
}) })
const exportStyle = () => { const exportStyle = () => {
styleExporter.exportData() styleExporter.exportData()
} }
const importStyle = () => {
styleImporter.importData()
}
return { return {
// ## Meta // ## Meta
name, name,
@ -727,7 +764,8 @@ export default {
getNewDirective, getNewDirective,
// ## Export and Import // ## Export and Import
exportStyle exportStyle,
importStyle
} }
} }
} }

View file

@ -28,6 +28,7 @@ export const newExporter = ({
export const newImporter = ({ export const newImporter = ({
accept = '.json', accept = '.json',
parser = (string) => JSON.parse(string),
onImport, onImport,
onImportFailure, onImportFailure,
validator = () => true validator = () => true
@ -44,7 +45,7 @@ export const newImporter = ({
const reader = new FileReader() const reader = new FileReader()
reader.onload = ({ target }) => { reader.onload = ({ target }) => {
try { try {
const parsed = JSON.parse(target.result) const parsed = parser(target.result)
const validationResult = validator(parsed, filename) const validationResult = validator(parsed, filename)
if (validationResult === true) { if (validationResult === true) {
onImport(parsed, filename) onImport(parsed, filename)