From 649dd25e2d7185739780f06c2cd65d089d22ddc1 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Mon, 16 Sep 2024 21:39:25 +0800 Subject: fix: prevent autosave overrides content from hash --- src/editor.mjs | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/editor.mjs b/src/editor.mjs index 8ab2389..d7c26cf 100644 --- a/src/editor.mjs +++ b/src/editor.mjs @@ -21,35 +21,19 @@ const toggleEditing = () => { // Set up EasyMDE {{{ // Content values for editor -const getStateFromHash = (hash) => { - const hashValue = hash.substring(1); - const stateString = decodeURIComponent(hashValue) - try { return JSON.parse(stateString) ?? {} } - catch (_) { return {} } -} - -const getContentFromHash = (hash) => { - const state = getStateFromHash(hash) - return state.content -} -const initialState = getStateFromHash(window.location.hash) -window.location.hash = '' -const contentFromHash = initialState.content -const lastContent = localStorage.getItem('editorContent') const defaultContent = '## Links\n\n- [Go to marker](geo:24,121?id=foo,leaflet&text=normal "Link Test")\n\n```map\nid: foo\nuse: Maplibre\n```\n' - const editor = new EasyMDE({ element: textArea, - indentWithTabs: false, - initialValue: contentFromHash ?? lastContent ?? defaultContent, - lineNumbers: true, - promptURLs: true, - uploadImage: true, + initialValue: defaultContent, autosave: { enabled: true, uniqueId: 'dumbymap', }, + indentWithTabs: false, + lineNumbers: true, + promptURLs: true, + uploadImage: true, spellChecker: false, toolbarButtonClassPrefix: 'mde', status: false, @@ -80,6 +64,29 @@ const editor = new EasyMDE({ }); const cm = editor.codemirror + +const getStateFromHash = (hash) => { + const hashValue = hash.substring(1); + const stateString = decodeURIComponent(hashValue) + try { return JSON.parse(stateString) ?? {} } + catch (_) { return {} } +} + +const getContentFromHash = (hash) => { + const state = getStateFromHash(hash) + return state.content +} + +const initialState = getStateFromHash(window.location.hash) +window.location.hash = '' +const contentFromHash = initialState.content + +// Seems like autosave would overwrite initialValue, set content from hash here +if (contentFromHash) { + editor.cleanup() + editor.value(contentFromHash) +} + // }}} // Set up logic about editor content {{{ markdown2HTML(HtmlContainer, editor.value()) @@ -167,6 +174,7 @@ updateDumbyMap() // Re-render HTML by editor content cm.on("change", (_, change) => { + console.log('change', change.text) updateDumbyMap() addClassToCodeLines() completeForCodeBlock(change) -- cgit v1.2.3-70-g09d2