Remove unnecessary json parse and stringify step when importing

This commit is contained in:
Misode
2024-10-24 15:57:14 +02:00
parent 9f1ae01d91
commit ee655b39e5
7 changed files with 28 additions and 102 deletions

View File

@@ -1,8 +1,8 @@
import { useCallback, useMemo, useRef, useState } from 'preact/hooks'
import config from '../../Config.js'
import { deepClone, deepEqual, writeZip } from '../../Utils.js'
import { useVersion } from '../../contexts/Version.jsx'
import { stringifySource } from '../../services/Source.js'
import { deepClone, deepEqual, writeZip } from '../../Utils.js'
import { Btn } from '../Btn.jsx'
import { ErrorPanel } from '../ErrorPanel.jsx'
import { Octicon } from '../Octicon.jsx'
@@ -44,11 +44,11 @@ export function CustomizedPanel({ tab }: Props) {
const entries = Object.entries(pack).flatMap(([type, files]) => {
const prefix = `data/minecraft/${type}/`
return [...files.entries()].map(([name, data]) => {
return [prefix + name + '.json', stringifySource(data, 'json')] as [string, string]
return [prefix + name + '.json', stringifySource(JSON.stringify(data, null, 2), 'json')] as [string, string]
})
})
const pack_format = config.versions.find(v => v.id === version)!.pack_format
entries.push(['pack.mcmeta', stringifySource({ pack: { pack_format, description: 'Customized world from misode.github.io' } }, 'json')])
entries.push(['pack.mcmeta', stringifySource(JSON.stringify({ pack: { pack_format, description: 'Customized world from misode.github.io' } }, null, 2), 'json')])
const url = await writeZip(entries)
download.current.setAttribute('href', url)
download.current.setAttribute('download', 'customized.zip')

View File

@@ -48,7 +48,8 @@ export function ProjectCreation({ onClose }: Props) {
const file = disectFilePath(entry[0], version)
if (file) {
try {
const data = await parseSource(entry[1], 'json')
const text = await parseSource(entry[1], 'json')
const data = JSON.parse(text)
project.files!.push({ ...file, data })
return
} catch (e) {

View File

@@ -70,14 +70,14 @@ export function ProjectPanel({ onRename, onCreate, onDeleteProject }: Props) {
const path = getFilePath(file, version)
if (path === undefined) return []
if (path === 'pack.mcmeta') hasPack = true
return [[path, stringifySource(file.data)]] as [string, string][]
return [[path, stringifySource(JSON.stringify(file.data))]] as [string, string][]
})
project.unknownFiles?.forEach(({ path, data }) => {
entries.push([path, data])
})
if (!hasPack) {
const pack_format = config.versions.find(v => v.id === version)!.pack_format
entries.push(['pack.mcmeta', stringifySource({ pack: { pack_format, description: '' } })])
entries.push(['pack.mcmeta', stringifySource(JSON.stringify({ pack: { pack_format, description: '' } }, null, 2))])
}
const url = await writeZip(entries)
download.current.setAttribute('href', url)

View File

@@ -4,7 +4,7 @@ import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
import { useLocale } from '../../contexts/index.js'
import { useDocAndNode, useSpyglass } from '../../contexts/Spyglass.jsx'
import { useLocalStorage } from '../../hooks/index.js'
import { getSourceFormats, getSourceIndent, getSourceIndents, parseSource, sortData, stringifySource } from '../../services/index.js'
import { getSourceFormats, getSourceIndent, getSourceIndents, parseSource, stringifySource } from '../../services/index.js'
import { Store } from '../../Store.js'
import { message } from '../../Utils.js'
import { Btn, BtnMenu } from '../index.js'
@@ -40,11 +40,11 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
const editor = useRef<Editor>()
const getSerializedOutput = useCallback((text: string) => {
let data = JSON.parse(text)
if (sort === 'alphabetically') {
data = sortData(data)
}
return stringifySource(data, format, indent)
// TODO: implement sort
// if (sort === 'alphabetically') {
// data = sortData(data)
// }
return stringifySource(text, format, indent)
}, [indent, format, sort])
const text = useDocAndNode(docAndNode)?.doc.getText()
@@ -75,8 +75,8 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
if (value.length === 0) return
if (!service || !docAndNode) return
try {
const data = await parseSource(value, format)
await service.writeFile(docAndNode.doc.uri, JSON.stringify(data))
const text = await parseSource(value, format)
await service.writeFile(docAndNode.doc.uri, text)
} catch (e) {
if (e instanceof Error) {
e.message = `Error importing: ${e.message}`