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,14 +195,8 @@ 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 editorFriendlyFallbackStructure = computed(() => { const rulesToEditorFriendly = (rules, root = {}) => rules.reduce((acc, rule) => {
const root = {} const { parent: rParent, component: rComponent } = rule
componentKeys.forEach((componentKey) => {
const componentValue = componentsMap.get(componentKey)
const { defaultRules } = componentValue
defaultRules.forEach((rule) => {
const { parent: rParent } = rule
const parent = rParent ?? rule const parent = rParent ?? rule
const hasChildren = !!rParent const hasChildren = !!rParent
const child = hasChildren ? rule : null const child = hasChildren ? rule : null
@ -213,16 +207,16 @@ export default {
state: pState = [] // no relation to Intel CPUs whatsoever state: pState = [] // no relation to Intel CPUs whatsoever
} = parent } = parent
const pPath = `${hasChildren ? pComponent : componentValue.name}.${pVariant}.${normalizeStates(pState)}` const pPath = `${hasChildren ? pComponent : rComponent}.${pVariant}.${normalizeStates(pState)}`
let output = get(root, pPath) let output = get(acc, pPath)
if (!output) { if (!output) {
set(root, pPath, {}) set(acc, pPath, {})
output = get(root, pPath) output = get(acc, pPath)
} }
if (hasChildren) { if (hasChildren) {
output._children = output._children ?? {} acc._children = acc._children ?? {}
const { const {
component: cComponent, component: cComponent,
variant: cVariant = 'normal', variant: cVariant = 'normal',
@ -235,7 +229,19 @@ export default {
} else { } else {
output.directives = parent.directives output.directives = parent.directives
} }
}) return acc
}, root)
const editorFriendlyFallbackStructure = computed(() => {
const root = {}
componentKeys.forEach((componentKey) => {
const componentValue = componentsMap.get(componentKey)
const { defaultRules, name } = componentValue
rulesToEditorFriendly(
defaultRules.map((rule) => ({ ...rule, component: name })),
root
)
}) })
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)