aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-09-11 19:12:17 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-09-11 19:12:17 +0800
commit722b799b22198ff9a25e6be8d3a685afcbaba4f3 (patch)
tree3bc02cf51caae2035a5bafdc52ecc8a9ea27ad10 /src
parent449f2aacc7a89df06465e9b30c4df9eb39c19205 (diff)
feat: Use object to store content in hash
Maybe can put more metadata inside object
Diffstat (limited to 'src')
-rw-r--r--src/editor.mjs24
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
22const getContentFromHash = (cleanHash = false) => { 22const 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}
29const contentFromHash = getContentFromHash(true) 28
29const getContentFromHash = (hash) => {
30 const state = getStateFromHash(hash)
31 return state.content
32}
33
34const initialState = getStateFromHash(window.location.hash)
35window.location.hash = ''
36const contentFromHash = initialState.content
30const lastContent = localStorage.getItem('editorContent') 37const lastContent = localStorage.getItem('editorContent')
31const 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' 38const 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 },