diff --git a/src/app/Store.ts b/src/app/Store.ts index 3f52b634..7388fc8b 100644 --- a/src/app/Store.ts +++ b/src/app/Store.ts @@ -9,6 +9,7 @@ export namespace Store { export const ID_VERSION = 'schema_version' export const ID_INDENT = 'indentation' export const ID_FORMAT = 'output_format' + export const ID_HIGHLIGHTING = 'output_highlighting' export const ID_SOUNDS_VERSION = 'minecraft_sounds_version' export const ID_PROJECTS = 'misode_projects' @@ -37,6 +38,10 @@ export namespace Store { return localStorage.getItem(ID_FORMAT) ?? 'json' } + export function getHighlighting() { + return localStorage.getItem(ID_HIGHLIGHTING) !== 'false' + } + export function getSoundsVersion() { return localStorage.getItem(ID_SOUNDS_VERSION) ?? 'latest' } @@ -69,6 +74,10 @@ export namespace Store { if (format) localStorage.setItem(ID_FORMAT, format) } + export function setHighlighting(highlighting: boolean | undefined) { + if (highlighting !== undefined) localStorage.setItem(ID_HIGHLIGHTING, highlighting.toString()) + } + export function setSoundsVersion(version: string | undefined) { if (version) localStorage.setItem(ID_SOUNDS_VERSION, version) } diff --git a/src/app/components/generator/SourcePanel.tsx b/src/app/components/generator/SourcePanel.tsx index b176e431..fd0f1b9c 100644 --- a/src/app/components/generator/SourcePanel.tsx +++ b/src/app/components/generator/SourcePanel.tsx @@ -37,6 +37,13 @@ const FORMATS: Record(null) const retransform = useRef() const onImport = useRef<(e: any) => any>() - const editor = useRef() + const textarea = useRef() + const editor = useRef() const getSerializedOutput = useCallback((model: DataModel, blockStates: BlockStateRegistry) => { const data = getOutput(model, blockStates) @@ -67,7 +76,7 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm if (!model || !blockStates) return try { const output = getSerializedOutput(model, blockStates) - editor.current.getSession().setValue(output) + editor.current.setValue(output) } catch (e) { onError(`Error getting JSON output: ${message(e)}`) console.error(e) @@ -86,19 +95,49 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm console.error(e) } } - }, [model, blockStates, indent, format]) + }, [model, blockStates, indent, format, highlighting]) useEffect(() => { - editor.current = brace.edit('editor') - editor.current.setOptions({ - fontSize: 14, - showFoldWidgets: false, - highlightSelectedWord: false, - }) - editor.current.$blockScrolling = Infinity - editor.current.on('blur', e => onImport.current(e)) - editor.current.getSession().setMode('ace/mode/json') - }, []) + if (highlighting) { + const braceEditor = brace.edit('editor') + braceEditor.setOptions({ + fontSize: 14, + showFoldWidgets: false, + highlightSelectedWord: false, + }) + braceEditor.$blockScrolling = Infinity + braceEditor.on('blur', e => onImport.current(e)) + braceEditor.getSession().setMode('ace/mode/json') + + editor.current = { + getValue() { + return braceEditor.getSession().getValue() + }, + setValue(value) { + braceEditor.getSession().setValue(value) + }, + configure(indent, format) { + braceEditor.setOption('useSoftTabs', indent !== 'tabs') + braceEditor.setOption('tabSize', indent === 'tabs' ? 4 : INDENT[indent]) + braceEditor.getSession().setMode(`ace/mode/${format}`) + }, + select() { + braceEditor.selectAll() + }, + } + } else { + editor.current = { + getValue() { + return textarea.current.value + }, + setValue(value: string) { + textarea.current.value = value + }, + configure() {}, + select() {}, + } + } + }, [highlighting]) useModel(model, () => { retransform.current() @@ -108,11 +147,9 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm }, [model]) useEffect(() => { - editor.current.setOption('useSoftTabs', indent !== 'tabs') - editor.current.setOption('tabSize', indent === 'tabs' ? 4 : INDENT[indent]) - editor.current.getSession().setMode(`ace/mode/${format}`) + editor.current.configure(indent, format) retransform.current() - }, [indent, format]) + }, [indent, format, highlighting]) useEffect(() => { if (doCopy && model && blockStates) { @@ -134,7 +171,7 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm useEffect(() => { if (doImport && editor.current) { editor.current.setValue('') - editor.current.selectAll() + editor.current.select() } }, [doImport]) @@ -148,6 +185,11 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm setFormat(value) } + const changeHighlighting = (value: boolean) => { + Store.setHighlighting(value) + setHighlighting(value) + } + return <>
@@ -159,9 +201,14 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm {Object.keys(FORMATS).map(key => changeFormat(key)} />)} +
+ changeHighlighting(!highlighting)} />
-

+		{highlighting
+			? 

+			: }
 		
 	
 }
diff --git a/src/locales/en.json b/src/locales/en.json
index b94879b3..2d2d447d 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -50,6 +50,7 @@
   "layer.offset": "Offset",
   "layer.factor": "Factor",
   "layer.jaggedness": "Jaggedness",
+  "highlighting": "Highlighting",
   "loot_table": "Loot Table",
   "model": "Model",
   "more": "More",