diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/editor.mjs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index 783e654..9b245d8 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
@@ -19,14 +19,21 @@ const toggleMaps = (container) => { | |||
19 | } | 19 | } |
20 | 20 | ||
21 | // Content values for editor | 21 | // Content values for editor |
22 | const getContentFromHash = (cleanHash = false) => { | 22 | const getStateFromHash = (hash) => { |
23 | const hashValue = location.hash.substring(1); | 23 | const hashValue = hash.substring(1); |
24 | if (cleanHash) window.location.hash = '' | 24 | const stateString = decodeURIComponent(hashValue) |
25 | return hashValue.startsWith('text=') | 25 | try { return JSON.parse(stateString) ?? {} } |
26 | ? decodeURIComponent(hashValue.substring(5)) | 26 | catch (_) { return {} } |
27 | : null | ||
28 | } | 27 | } |
29 | const contentFromHash = getContentFromHash(true) | 28 | |
29 | const getContentFromHash = (hash) => { | ||
30 | const state = getStateFromHash(hash) | ||
31 | return state.content | ||
32 | } | ||
33 | |||
34 | const initialState = getStateFromHash(window.location.hash) | ||
35 | window.location.hash = '' | ||
36 | const contentFromHash = initialState.content | ||
30 | const lastContent = localStorage.getItem('editorContent') | 37 | const lastContent = localStorage.getItem('editorContent') |
31 | 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' | 38 | 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' |
32 | 39 | ||
@@ -62,7 +69,8 @@ const editor = new EasyMDE({ | |||
62 | title: 'Save content as URL', | 69 | title: 'Save content as URL', |
63 | text: "🤔", | 70 | text: "🤔", |
64 | action: () => { | 71 | action: () => { |
65 | window.location.hash = '#text=' + encodeURIComponent(editor.value()) | 72 | const state = { content: editor.value() } |
73 | window.location.hash = encodeURIComponent(JSON.stringify(state)) | ||
66 | navigator.clipboard.writeText(window.location.href) | 74 | navigator.clipboard.writeText(window.location.href) |
67 | alert('URL copied to clipboard') | 75 | alert('URL copied to clipboard') |
68 | }, | 76 | }, |