diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-05-11 16:20:43 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-05-11 16:20:43 +0800 |
| commit | 4e223650daa789b6f6851004e6e58970ef9999b9 (patch) | |
| tree | 80ff0cf7ae99a37304217e750134f3ce78acb968 | |
| parent | 3fe6988354df463c7274f7efaf9d39931669c91c (diff) | |
| download | yuescript-4e223650daa789b6f6851004e6e58970ef9999b9.tar.gz yuescript-4e223650daa789b6f6851004e6e58970ef9999b9.tar.bz2 yuescript-4e223650daa789b6f6851004e6e58970ef9999b9.zip | |
Persist try page code drafts
| -rwxr-xr-x | doc/docs/.vitepress/theme/components/YueCompiler.vue | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/doc/docs/.vitepress/theme/components/YueCompiler.vue b/doc/docs/.vitepress/theme/components/YueCompiler.vue index 4477f22..081e7c2 100755 --- a/doc/docs/.vitepress/theme/components/YueCompiler.vue +++ b/doc/docs/.vitepress/theme/components/YueCompiler.vue | |||
| @@ -41,6 +41,9 @@ import { history, indentWithTab } from "@codemirror/commands"; | |||
| 41 | import { defaultKeymap, historyKeymap } from "@codemirror/commands"; | 41 | import { defaultKeymap, historyKeymap } from "@codemirror/commands"; |
| 42 | import { simpleMode } from "@codemirror/legacy-modes/mode/simple-mode"; | 42 | import { simpleMode } from "@codemirror/legacy-modes/mode/simple-mode"; |
| 43 | 43 | ||
| 44 | const TRY_PAGE_DRAFT_KEY = "yuescript.try.code"; | ||
| 45 | const TRY_PAGE_DRAFT_SAVE_DELAY = 1000; | ||
| 46 | |||
| 44 | function createEditorTheme({ bg, fg, gutterColor, selectionBg, cursorColor, matchingBracketBg, dark }) { | 47 | function createEditorTheme({ bg, fg, gutterColor, selectionBg, cursorColor, matchingBracketBg, dark }) { |
| 45 | return EditorView.theme( | 48 | return EditorView.theme( |
| 46 | { | 49 | { |
| @@ -161,6 +164,7 @@ export default { | |||
| 161 | highlightCompartment: null, | 164 | highlightCompartment: null, |
| 162 | themeObserver: null, | 165 | themeObserver: null, |
| 163 | _onYueReady: null, | 166 | _onYueReady: null, |
| 167 | draftSaveTimer: null, | ||
| 164 | }; | 168 | }; |
| 165 | }, | 169 | }, |
| 166 | computed: { | 170 | computed: { |
| @@ -170,13 +174,17 @@ export default { | |||
| 170 | highlightedLua() { | 174 | highlightedLua() { |
| 171 | return highlight(this.compiled || "", languages.lua); | 175 | return highlight(this.compiled || "", languages.lua); |
| 172 | }, | 176 | }, |
| 177 | shouldPersistDraft() { | ||
| 178 | return !this.compileronly && !this.displayonly && this.text === ""; | ||
| 179 | }, | ||
| 173 | }, | 180 | }, |
| 174 | mounted() { | 181 | mounted() { |
| 175 | this.windowWidth = window.innerWidth; | 182 | this.windowWidth = window.innerWidth; |
| 176 | window.addEventListener("resize", this.handleResize); | 183 | window.addEventListener("resize", this.handleResize); |
| 184 | window.addEventListener("beforeunload", this.saveDraftNow); | ||
| 177 | this.observeTheme(); | 185 | this.observeTheme(); |
| 178 | 186 | ||
| 179 | const initialCode = this.text; | 187 | const initialCode = this.loadDraftCode(); |
| 180 | this.code = initialCode; | 188 | this.code = initialCode; |
| 181 | this.codeChanged(initialCode); | 189 | this.codeChanged(initialCode); |
| 182 | this.initEditor(initialCode); | 190 | this.initEditor(initialCode); |
| @@ -199,11 +207,17 @@ export default { | |||
| 199 | } | 207 | } |
| 200 | }, | 208 | }, |
| 201 | beforeUnmount() { | 209 | beforeUnmount() { |
| 210 | this.saveDraftNow(); | ||
| 202 | window.removeEventListener("resize", this.handleResize); | 211 | window.removeEventListener("resize", this.handleResize); |
| 212 | window.removeEventListener("beforeunload", this.saveDraftNow); | ||
| 203 | if (this._onYueReady) { | 213 | if (this._onYueReady) { |
| 204 | window.removeEventListener("yue:ready", this._onYueReady); | 214 | window.removeEventListener("yue:ready", this._onYueReady); |
| 205 | this._onYueReady = null; | 215 | this._onYueReady = null; |
| 206 | } | 216 | } |
| 217 | if (this.draftSaveTimer) { | ||
| 218 | clearTimeout(this.draftSaveTimer); | ||
| 219 | this.draftSaveTimer = null; | ||
| 220 | } | ||
| 207 | if (this.editorView) { | 221 | if (this.editorView) { |
| 208 | this.editorView.destroy(); | 222 | this.editorView.destroy(); |
| 209 | this.editorView = null; | 223 | this.editorView = null; |
| @@ -214,6 +228,46 @@ export default { | |||
| 214 | } | 228 | } |
| 215 | }, | 229 | }, |
| 216 | methods: { | 230 | methods: { |
| 231 | loadDraftCode() { | ||
| 232 | if (!this.shouldPersistDraft) { | ||
| 233 | return this.text; | ||
| 234 | } | ||
| 235 | try { | ||
| 236 | const draft = window.localStorage.getItem(TRY_PAGE_DRAFT_KEY); | ||
| 237 | return draft === null ? this.text : draft; | ||
| 238 | } catch (error) { | ||
| 239 | return this.text; | ||
| 240 | } | ||
| 241 | }, | ||
| 242 | scheduleDraftSave(text) { | ||
| 243 | if (!this.shouldPersistDraft) { | ||
| 244 | return; | ||
| 245 | } | ||
| 246 | if (this.draftSaveTimer) { | ||
| 247 | clearTimeout(this.draftSaveTimer); | ||
| 248 | } | ||
| 249 | this.draftSaveTimer = window.setTimeout(() => { | ||
| 250 | this.draftSaveTimer = null; | ||
| 251 | this.saveDraft(text); | ||
| 252 | }, TRY_PAGE_DRAFT_SAVE_DELAY); | ||
| 253 | }, | ||
| 254 | saveDraftNow() { | ||
| 255 | if (!this.shouldPersistDraft) { | ||
| 256 | return; | ||
| 257 | } | ||
| 258 | if (this.draftSaveTimer) { | ||
| 259 | clearTimeout(this.draftSaveTimer); | ||
| 260 | this.draftSaveTimer = null; | ||
| 261 | } | ||
| 262 | this.saveDraft(this.code); | ||
| 263 | }, | ||
| 264 | saveDraft(text) { | ||
| 265 | try { | ||
| 266 | window.localStorage.setItem(TRY_PAGE_DRAFT_KEY, text); | ||
| 267 | } catch (error) { | ||
| 268 | // Ignore unavailable or full localStorage; compiling should still work. | ||
| 269 | } | ||
| 270 | }, | ||
| 217 | focusEditorToEnd() { | 271 | focusEditorToEnd() { |
| 218 | if (!this.editorView) { | 272 | if (!this.editorView) { |
| 219 | return; | 273 | return; |
| @@ -506,6 +560,7 @@ export default { | |||
| 506 | } | 560 | } |
| 507 | const nextCode = update.state.doc.toString(); | 561 | const nextCode = update.state.doc.toString(); |
| 508 | this.code = nextCode; | 562 | this.code = nextCode; |
| 563 | this.scheduleDraftSave(nextCode); | ||
| 509 | this.codeChanged(nextCode); | 564 | this.codeChanged(nextCode); |
| 510 | }); | 565 | }); |
| 511 | 566 | ||
