From 3241ce124ba02e79a01231dbd2efd08e2d181b30 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Mon, 9 Sep 2024 15:00:20 +0800 Subject: feat: Update editor content by hash --- src/editor.mjs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/editor.mjs b/src/editor.mjs index 04b4125..31229db 100644 --- a/src/editor.mjs +++ b/src/editor.mjs @@ -7,16 +7,16 @@ import { defaultAliasesForRenderer, parseConfigsFromYaml } from 'mapclay' const HtmlContainer = document.querySelector(".result-html") const mdeElement = document.querySelector("#tinymde") -const getContentFromHash = () => { +const getContentFromHash = (cleanHash = false) => { const hashValue = location.hash.substring(1); - window.location.hash = '' + if (cleanHash) window.location.hash = '' return hashValue.startsWith('text=') ? decodeURIComponent(hashValue.substring(5)) : null } // Add Editor -const contentFromHash = getContentFromHash() +const contentFromHash = getContentFromHash(true) 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' @@ -26,6 +26,11 @@ const tinyEditor = new TinyMDE.Editor({ }); mdeElement.querySelectorAll('span').forEach(e => e.setAttribute('spellcheck', 'false')) +onhashchange = () => { + const contentFromHash = getContentFromHash() + if (contentFromHash) tinyEditor.setContent(contentFromHash) +} + // Add command bar for editor // Use this command to render maps and geoLinks const mapCommand = { -- cgit v1.2.3-70-g09d2