Add configurable indentation

This commit is contained in:
Misode
2021-06-26 19:19:51 +02:00
parent 56ab28f4d8
commit 2fb4848425
9 changed files with 94 additions and 41 deletions
+9
View File
@@ -5,6 +5,7 @@ export namespace Store {
export const ID_LANGUAGE = 'language' export const ID_LANGUAGE = 'language'
export const ID_THEME = 'theme' export const ID_THEME = 'theme'
export const ID_VERSION = 'schema_version' export const ID_VERSION = 'schema_version'
export const ID_INDENT = 'indentation'
export function getLanguage() { export function getLanguage() {
return localStorage.getItem(ID_LANGUAGE) ?? 'en' return localStorage.getItem(ID_LANGUAGE) ?? 'en'
@@ -22,6 +23,10 @@ export namespace Store {
return '1.17' return '1.17'
} }
export function getIndent() {
return localStorage.getItem(ID_INDENT) ?? '2_spaces'
}
export function setLanguage(language: string | undefined) { export function setLanguage(language: string | undefined) {
if (language) localStorage.setItem(ID_LANGUAGE, language) if (language) localStorage.setItem(ID_LANGUAGE, language)
} }
@@ -33,4 +38,8 @@ export namespace Store {
export function setVersion(version: VersionId | undefined) { export function setVersion(version: VersionId | undefined) {
if (version) localStorage.setItem(ID_VERSION, version) if (version) localStorage.setItem(ID_VERSION, version)
} }
export function setIndent(indent: string) {
if (indent) localStorage.setItem(ID_INDENT, indent)
}
} }
+44 -10
View File
@@ -1,10 +1,18 @@
import type { DataModel } from '@mcschema/core' import type { DataModel } from '@mcschema/core'
import { ModelPath } from '@mcschema/core' import { ModelPath } from '@mcschema/core'
import { useEffect, useRef } from 'preact/hooks' import { useEffect, useRef, useState } from 'preact/hooks'
import { Btn, BtnMenu } from '.'
import { useModel } from '../hooks' import { useModel } from '../hooks'
import { locale } from '../Locales' import { locale } from '../Locales'
import { transformOutput } from '../schema/transformOutput' import { transformOutput } from '../schema/transformOutput'
import type { BlockStateRegistry } from '../Schemas' import type { BlockStateRegistry } from '../Schemas'
import { Store } from '../Store'
const INDENT: Record<string, number | string> = {
'2_spaces': 2,
'4_spaces': 4,
tabs: '\t',
}
type SourcePanelProps = { type SourcePanelProps = {
lang: string, lang: string,
@@ -18,21 +26,34 @@ type SourcePanelProps = {
} }
export function SourcePanel({ lang, name, model, blockStates, doCopy, doDownload, doImport, onError }: SourcePanelProps) { export function SourcePanel({ lang, name, model, blockStates, doCopy, doDownload, doImport, onError }: SourcePanelProps) {
const loc = locale.bind(null, lang) const loc = locale.bind(null, lang)
const [indent, setIndent] = useState(Store.getIndent())
const source = useRef<HTMLTextAreaElement>(null) const source = useRef<HTMLTextAreaElement>(null)
const download = useRef<HTMLAnchorElement>(null) const download = useRef<HTMLAnchorElement>(null)
const retransform = useRef<Function>()
useModel(model, model => { useEffect(() => {
try { retransform.current = () => {
const props = { blockStates: blockStates ?? {} } if (!model || !blockStates) return
const data = model.schema.hook(transformOutput, new ModelPath(model), model.data, props) try {
source.current.value = JSON.stringify(data, null, 2) + '\n' const props = { blockStates: blockStates ?? {} }
} catch (e) { const data = model.schema.hook(transformOutput, new ModelPath(model), model.data, props)
onError(`Error getting JSON output: ${e.message}`) source.current.value = JSON.stringify(data, null, INDENT[indent]) + '\n'
console.error(e) } catch (e) {
source.current.value = '' onError(`Error getting JSON output: ${e.message}`)
console.error(e)
source.current.value = ''
}
} }
}) })
useModel(model, () => {
retransform.current()
})
useEffect(() => {
retransform.current()
}, [indent])
const onImport = () => { const onImport = () => {
try { try {
const data = JSON.parse(source.current.value) const data = JSON.parse(source.current.value)
@@ -65,7 +86,20 @@ export function SourcePanel({ lang, name, model, blockStates, doCopy, doDownload
} }
}, [doImport]) }, [doImport])
const changeIndent = (value: string) => {
Store.setIndent(value)
setIndent(value)
}
return <> return <>
<div class="controls">
<BtnMenu icon="gear">
{Object.entries(INDENT).map(([key]) =>
<Btn label={loc(`indentation.${key}`)} active={indent === key}
onClick={() => changeIndent(key)}/>
)}
</BtnMenu>
</div>
<textarea ref={source} class="source" onChange={onImport} spellcheck={false} autocorrect="off" placeholder={loc('source_placeholder')}></textarea> <textarea ref={source} class="source" onChange={onImport} spellcheck={false} autocorrect="off" placeholder={loc('source_placeholder')}></textarea>
<a ref={download} style="display: none;"></a> <a ref={download} style="display: none;"></a>
</> </>
+21 -21
View File
@@ -1,23 +1,23 @@
{ {
"worldgen/template-pool": "Vorlagenauswahl", "advancement": "Fortschritt",
"worldgen/surface-builder": "Oberflächengestalter", "copy": "Kopieren",
"worldgen/structure-feature": "Strukturmekrmal", "dimension-type": "Dimensionstyp",
"worldgen/processor-list": "Prozessorliste", "dimension": "Dimension",
"worldgen/noise-settings": "Rauscheinstellungen", "download": "Herunterladen",
"worldgen/feature": "Merkmal", "language": "Sprache",
"worldgen/carver": "Borer", "loot-table": "Beutetabelle",
"worldgen/biome": "Biom", "predicate": "Prädikat",
"preview": "Visualisieren", "preview": "Visualisieren",
"title.home": "Datenpaketgeneratoren", "reset": "Zurücksetzen",
"title.generator": "%0%-Generator", "share": "Teilen",
"share": "Teilen", "title.generator": "%0%-Generator",
"reset": "Zurücksetzen", "title.home": "Datenpaketgeneratoren",
"predicate": "Prädikat", "worldgen/biome": "Biom",
"loot-table": "Beutetabelle", "worldgen/carver": "Borer",
"language": "Sprache", "worldgen/feature": "Merkmal",
"download": "Herunterladen", "worldgen/noise-settings": "Rauscheinstellungen",
"dimension": "Dimension", "worldgen/processor-list": "Prozessorliste",
"dimension-type": "Dimensionstyp", "worldgen/structure-feature": "Strukturmekrmal",
"copy": "Kopieren", "worldgen/surface-builder": "Oberflächengestalter",
"advancement": "Fortschritt" "worldgen/template-pool": "Vorlagenauswahl"
} }
+3 -4
View File
@@ -8,16 +8,16 @@
"dimension-type": "Dimension Type", "dimension-type": "Dimension Type",
"dimension": "Dimension", "dimension": "Dimension",
"download": "Download", "download": "Download",
"error.block_state.missing_property": "Missing block property \"%0%\"",
"fields": "Fields", "fields": "Fields",
"github": "GitHub", "github": "GitHub",
"home": "Home", "home": "Home",
"import": "Import", "import": "Import",
"indentation.2_spaces": "2 spaces",
"indentation.4_spaces": "4 spaces",
"indentation.tabs": "Tabs",
"item-modifier": "Item Modifier", "item-modifier": "Item Modifier",
"language": "Language", "language": "Language",
"loot-table": "Loot Table", "loot-table": "Loot Table",
"maximize": "Maximize",
"minimize": "Minimize",
"not_found.description": "The page you were looking for does not exist.", "not_found.description": "The page you were looking for does not exist.",
"no_presets": "No presets", "no_presets": "No presets",
"predicate": "Predicate", "predicate": "Predicate",
@@ -36,7 +36,6 @@
"title.home": "Data Pack Generators", "title.home": "Data Pack Generators",
"presets": "Presets", "presets": "Presets",
"preview": "Visualize", "preview": "Visualize",
"preview.show_density": "Show Density",
"preview.scale": "Scale", "preview.scale": "Scale",
"preview.depth": "Depth", "preview.depth": "Depth",
"preview.width": "Width", "preview.width": "Width",
+3
View File
@@ -4,6 +4,9 @@
"dimension": "Dimension", "dimension": "Dimension",
"dimension-type": "Type de dimension", "dimension-type": "Type de dimension",
"download": "Télécharger", "download": "Télécharger",
"indentation.2_spaces": "2 espaces",
"indentation.4_spaces": "4 espaces",
"indentation.tabs": "Onglets",
"language": "Langage", "language": "Langage",
"loot-table": "Table de butin", "loot-table": "Table de butin",
"predicate": "Prédicat", "predicate": "Prédicat",
+8 -1
View File
@@ -1 +1,8 @@
{} {
"copy": "Copiar",
"indentation.2_spaces": "2 Espaços",
"indentation.4_spaces": "4 Espaços",
"indentation.tabs": "Guias",
"theme.dark": "Escuro",
"theme.light": "Claro"
}
+3 -1
View File
@@ -5,6 +5,9 @@
"dimension-type": "Тип измерения", "dimension-type": "Тип измерения",
"download": "Скачать", "download": "Скачать",
"fields": "Поля", "fields": "Поля",
"indentation.2_spaces": "2 пробела",
"indentation.4_spaces": "4 пробела",
"indentation.tabs": "Табуляция",
"item-modifier": "Модификатор предмета", "item-modifier": "Модификатор предмета",
"language": "Язык", "language": "Язык",
"loot-table": "Таблица добычи", "loot-table": "Таблица добычи",
@@ -12,7 +15,6 @@
"preview": "Визуализировать", "preview": "Визуализировать",
"preview.depth": "Глубина", "preview.depth": "Глубина",
"preview.scale": "Размер", "preview.scale": "Размер",
"preview.show_density": "Показать плотность",
"preview.width": "Ширина", "preview.width": "Ширина",
"redo": "Повторить", "redo": "Повторить",
"reset": "Сбросить", "reset": "Сбросить",
-4
View File
@@ -8,15 +8,12 @@
"dimension-type": "Typ dimenzie", "dimension-type": "Typ dimenzie",
"dimension": "Dimenzia", "dimension": "Dimenzia",
"download": "Stiahnuť", "download": "Stiahnuť",
"error.block_state.missing_property": "Chýba údaj kocky \"%0%\"",
"fields": "Polia", "fields": "Polia",
"github": "GitHub", "github": "GitHub",
"home": "Domov", "home": "Domov",
"item-modifier": "Úprava itemov", "item-modifier": "Úprava itemov",
"language": "Jazyk", "language": "Jazyk",
"loot-table": "Zoznam lupov", "loot-table": "Zoznam lupov",
"maximize": "Maximalizovať",
"minimize": "Minimalizovať",
"not_found.description": "Stránka ktorú ste vyhľadali neexistuje.", "not_found.description": "Stránka ktorú ste vyhľadali neexistuje.",
"predicate": "Predikát", "predicate": "Predikát",
"redo": "Znovu", "redo": "Znovu",
@@ -30,7 +27,6 @@
"title.home": "Data Packové Generátory", "title.home": "Data Packové Generátory",
"presets": "Prednastavenia", "presets": "Prednastavenia",
"preview": "Vizualizovať", "preview": "Vizualizovať",
"preview.show_density": "Zobraziť hustotu",
"preview.scale": "Veľkosť", "preview.scale": "Veľkosť",
"preview.depth": "Hĺbka", "preview.depth": "Hĺbka",
"preview.width": "Šírka", "preview.width": "Šírka",
+3
View File
@@ -5,6 +5,9 @@
"dimension-type": "维度类型", "dimension-type": "维度类型",
"download": "下载", "download": "下载",
"fields": "字段", "fields": "字段",
"indentation.2_spaces": "2 空格缩进",
"indentation.4_spaces": "4 空格缩进",
"indentation.tabs": "Tab 缩进",
"item-modifier": "物品修饰器", "item-modifier": "物品修饰器",
"language": "语言", "language": "语言",
"loot-table": "战利品表", "loot-table": "战利品表",