diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-08 12:35:26 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-08 12:38:13 +0800 |
| commit | 25867c2887776ecc52ddbec741faf7f1ae05d6dd (patch) | |
| tree | af6096271f6b0b0feeb76be4a2602a803de0c318 /src | |
| parent | 1ad10e44b140c3a58d26a50a2533ac7eb31d7d31 (diff) | |
feat: add search params 'content' for tutorial
Diffstat (limited to 'src')
| -rw-r--r-- | src/editor.mjs | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index 866237a..f0b4af0 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
| @@ -7,6 +7,7 @@ import { shiftByWindow } from './utils.mjs' | |||
| 7 | 7 | ||
| 8 | // Set up Containers {{{ | 8 | // Set up Containers {{{ |
| 9 | 9 | ||
| 10 | const url = new URL(window.location) | ||
| 10 | const context = document.querySelector('[data-mode]') | 11 | const context = document.querySelector('[data-mode]') |
| 11 | const dumbyContainer = document.querySelector('.DumbyMap') | 12 | const dumbyContainer = document.querySelector('.DumbyMap') |
| 12 | const textArea = document.querySelector('.editor textarea') | 13 | const textArea = document.querySelector('.editor textarea') |
| @@ -134,6 +135,7 @@ const editor = new EasyMDE({ | |||
| 134 | action: () => { | 135 | action: () => { |
| 135 | const state = { content: editor.value() } | 136 | const state = { content: editor.value() } |
| 136 | window.location.hash = encodeURIComponent(JSON.stringify(state)) | 137 | window.location.hash = encodeURIComponent(JSON.stringify(state)) |
| 138 | window.location.search = '' | ||
| 137 | navigator.clipboard.writeText(window.location.href) | 139 | navigator.clipboard.writeText(window.location.href) |
| 138 | window.alert('URL updated in address bar, you can save current page as bookmark') | 140 | window.alert('URL updated in address bar, you can save current page as bookmark') |
| 139 | } | 141 | } |
| @@ -239,17 +241,19 @@ const getContentFromHash = hash => { | |||
| 239 | const state = getStateFromHash(hash) | 241 | const state = getStateFromHash(hash) |
| 240 | return state.content | 242 | return state.content |
| 241 | } | 243 | } |
| 244 | const contentFromHash = getContentFromHash(window.location.hash) | ||
| 242 | 245 | ||
| 243 | const initialState = getStateFromHash(window.location.hash) | 246 | if (url.searchParams.get('content') === 'tutorial') { |
| 244 | window.location.hash = '' | 247 | console.log('tutorial') |
| 245 | const contentFromHash = initialState.content | 248 | editor.value(defaultContent) |
| 246 | 249 | } else if (contentFromHash) { | |
| 247 | // Seems like autosave would overwrite initialValue, set content from hash here | 250 | // Seems like autosave would overwrite initialValue, set content from hash here |
| 248 | if (contentFromHash) { | ||
| 249 | editor.cleanup() | 251 | editor.cleanup() |
| 250 | editor.value(contentFromHash) | 252 | editor.value(contentFromHash) |
| 251 | } | 253 | } |
| 252 | 254 | ||
| 255 | window.location.hash = '' | ||
| 256 | |||
| 253 | // }}} | 257 | // }}} |
| 254 | // Set up logic about editor content {{{ | 258 | // Set up logic about editor content {{{ |
| 255 | 259 | ||