mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-24 23:56:51 +00:00
Remove unnecessary json parse and stringify step when importing
This commit is contained in:
64
package-lock.json
generated
64
package-lock.json
generated
@@ -19,7 +19,6 @@
|
||||
"@zip.js/zip.js": "^2.4.5",
|
||||
"brace": "^0.11.1",
|
||||
"buffer": "^6.0.3",
|
||||
"comment-json": "^4.1.1",
|
||||
"deepslate": "^0.22.3",
|
||||
"deepslate-1.18": "npm:deepslate@0.9.0-beta.9",
|
||||
"deepslate-1.18.2": "npm:deepslate@0.9.0",
|
||||
@@ -1185,11 +1184,6 @@
|
||||
"sprintf-js": "~1.0.2"
|
||||
}
|
||||
},
|
||||
"node_modules/array-timsort": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/array-timsort/-/array-timsort-1.0.3.tgz",
|
||||
"integrity": "sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ=="
|
||||
},
|
||||
"node_modules/array-union": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz",
|
||||
@@ -1529,21 +1523,6 @@
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/comment-json": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/comment-json/-/comment-json-4.1.1.tgz",
|
||||
"integrity": "sha512-v8gmtPvxhBlhdRBLwdHSjGy9BgA23t9H1FctdQKyUrErPjSrJcdDMqBq9B4Irtm7w3TNYLQJNH6ARKnpyag1sA==",
|
||||
"dependencies": {
|
||||
"array-timsort": "^1.0.3",
|
||||
"core-util-is": "^1.0.2",
|
||||
"esprima": "^4.0.1",
|
||||
"has-own-prop": "^2.0.0",
|
||||
"repeat-string": "^1.6.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
@@ -2842,14 +2821,6 @@
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/has-own-prop": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-own-prop/-/has-own-prop-2.0.0.tgz",
|
||||
"integrity": "sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ==",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/highlight.js": {
|
||||
"version": "11.5.1",
|
||||
"resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-11.5.1.tgz",
|
||||
@@ -3764,14 +3735,6 @@
|
||||
"url": "https://github.com/sponsors/mysticatea"
|
||||
}
|
||||
},
|
||||
"node_modules/repeat-string": {
|
||||
"version": "1.6.1",
|
||||
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
|
||||
"integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=",
|
||||
"engines": {
|
||||
"node": ">=0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/require-directory": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
|
||||
@@ -5531,11 +5494,6 @@
|
||||
"sprintf-js": "~1.0.2"
|
||||
}
|
||||
},
|
||||
"array-timsort": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/array-timsort/-/array-timsort-1.0.3.tgz",
|
||||
"integrity": "sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ=="
|
||||
},
|
||||
"array-union": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz",
|
||||
@@ -5747,18 +5705,6 @@
|
||||
"integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==",
|
||||
"dev": true
|
||||
},
|
||||
"comment-json": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/comment-json/-/comment-json-4.1.1.tgz",
|
||||
"integrity": "sha512-v8gmtPvxhBlhdRBLwdHSjGy9BgA23t9H1FctdQKyUrErPjSrJcdDMqBq9B4Irtm7w3TNYLQJNH6ARKnpyag1sA==",
|
||||
"requires": {
|
||||
"array-timsort": "^1.0.3",
|
||||
"core-util-is": "^1.0.2",
|
||||
"esprima": "^4.0.1",
|
||||
"has-own-prop": "^2.0.0",
|
||||
"repeat-string": "^1.6.1"
|
||||
}
|
||||
},
|
||||
"concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
@@ -6643,11 +6589,6 @@
|
||||
"integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==",
|
||||
"dev": true
|
||||
},
|
||||
"has-own-prop": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-own-prop/-/has-own-prop-2.0.0.tgz",
|
||||
"integrity": "sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ=="
|
||||
},
|
||||
"highlight.js": {
|
||||
"version": "11.5.1",
|
||||
"resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-11.5.1.tgz",
|
||||
@@ -7282,11 +7223,6 @@
|
||||
"integrity": "sha512-pq2bWo9mVD43nbts2wGv17XLiNLya+GklZ8kaDLV2Z08gDCsGpnKn9BFMepvWuHCbyVvY7J5o5+BVvoQbmlJLg==",
|
||||
"dev": true
|
||||
},
|
||||
"repeat-string": {
|
||||
"version": "1.6.1",
|
||||
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
|
||||
"integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc="
|
||||
},
|
||||
"require-directory": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
"@zip.js/zip.js": "^2.4.5",
|
||||
"brace": "^0.11.1",
|
||||
"buffer": "^6.0.3",
|
||||
"comment-json": "^4.1.1",
|
||||
"deepslate": "^0.22.3",
|
||||
"deepslate-1.18": "npm:deepslate@0.9.0-beta.9",
|
||||
"deepslate-1.18.2": "npm:deepslate@0.9.0",
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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}`
|
||||
|
||||
@@ -10,28 +10,18 @@ const INDENTS: Record<string, number | string | undefined> = {
|
||||
minified: undefined,
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||
let commentJson: typeof import('comment-json') | null = null
|
||||
|
||||
const FORMATS: Record<string, {
|
||||
parse: (v: string) => Promise<unknown>,
|
||||
stringify: (v: unknown, indentation: string | number | undefined) => string,
|
||||
parse: (source: string) => string,
|
||||
stringify: (source: string, indent: string | number | undefined) => string,
|
||||
}> = {
|
||||
json: {
|
||||
parse: async (v) => {
|
||||
try {
|
||||
return JSON.parse(v)
|
||||
} catch (e) {
|
||||
commentJson = await import('comment-json')
|
||||
return commentJson.parse(v)
|
||||
}
|
||||
},
|
||||
stringify: (v, i) => (commentJson ?? JSON).stringify(v, null, i) + '\n',
|
||||
parse: (s) => s,
|
||||
stringify: (s) => s,
|
||||
},
|
||||
snbt: {
|
||||
parse: async (v) => NbtTag.fromString(v).toSimplifiedJson(),
|
||||
stringify: (v, i) => {
|
||||
const tag = jsonToNbt(v)
|
||||
parse: (s) => JSON.stringify(NbtTag.fromString(s).toSimplifiedJson(), null, 2),
|
||||
stringify: (s, i) => {
|
||||
const tag = jsonToNbt(JSON.parse(s))
|
||||
if (i === undefined) {
|
||||
return tag.toString()
|
||||
}
|
||||
@@ -39,20 +29,20 @@ const FORMATS: Record<string, {
|
||||
},
|
||||
},
|
||||
yaml: {
|
||||
parse: async (v) => yaml.load(v),
|
||||
stringify: (v, i) => yaml.dump(v, {
|
||||
parse: (s) => JSON.stringify(yaml.load(s), null, 2),
|
||||
stringify: (s, i) => yaml.dump(JSON.parse(s), {
|
||||
flowLevel: i === undefined ? 0 : -1,
|
||||
indent: typeof i === 'string' ? 4 : i,
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
export function stringifySource(data: unknown, format?: string, indent?: string) {
|
||||
return FORMATS[format ?? Store.getFormat()].stringify(data, INDENTS[indent ?? Store.getIndent()])
|
||||
export function stringifySource(source: string, format?: string, indent?: string) {
|
||||
return FORMATS[format ?? Store.getFormat()].stringify(source, INDENTS[indent ?? Store.getIndent()])
|
||||
}
|
||||
|
||||
export async function parseSource(data: string, format: string) {
|
||||
return await FORMATS[format].parse(data)
|
||||
export async function parseSource(source: string, format: string) {
|
||||
return FORMATS[format].parse(source)
|
||||
}
|
||||
|
||||
export function getSourceIndent(indent: string) {
|
||||
|
||||
Reference in New Issue
Block a user