From f98ee6a1f6dccd644c9ba77a353e3450c67d4d40 Mon Sep 17 00:00:00 2001 From: Raphael Beer Date: Sun, 12 Apr 2020 06:07:10 +0200 Subject: [PATCH] Add: load last autosave for /post/create --- webapp/components/Editor/Editor.spec.js | 6 ++++++ webapp/components/Editor/plugins/autoSave.js | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/webapp/components/Editor/Editor.spec.js b/webapp/components/Editor/Editor.spec.js index ceb6ddc3e..332af9035 100644 --- a/webapp/components/Editor/Editor.spec.js +++ b/webapp/components/Editor/Editor.spec.js @@ -226,6 +226,12 @@ describe('Editor.vue', () => { const lastEditedId = localStorage.getItem('autosave:post:last') expect(lastEditedId).toMatch(/^[a-f\d]{8}$/) }) + + it('loads last edited autosave', () => { + const wrapper = Wrapper() + const { value: autosaveHTML } = getFirstInStorage() + expect(wrapper.vm.editor.getHTML()).toBe(autosaveHTML) + }) }) describe('when editing a comment', () => { diff --git a/webapp/components/Editor/plugins/autoSave.js b/webapp/components/Editor/plugins/autoSave.js index 409638e5f..a6077f3ed 100644 --- a/webapp/components/Editor/plugins/autoSave.js +++ b/webapp/components/Editor/plugins/autoSave.js @@ -7,14 +7,16 @@ export default class AutoSave extends Extension { super() this.route = $route - const { id, editorType } = AutoSave.for(this.route.path) + const { id = hash(Date.now().toString(), 0xb0b).toString(16), editorType } = AutoSave.for( + this.route.path, + ) this.id = id this.editorType = editorType } static for(path) { if (path === '/post/create') { - return { editorType: 'post', id: hash(Date.now().toString(), 0xb0b).toString(16) } + return { editorType: 'post' } } const commentMatch = path.match(/^\/post\/([0-9a-f-]*)\/[\w-]*$/) @@ -33,7 +35,7 @@ export default class AutoSave extends Extension { } static load(path) { - const { id, editorType } = AutoSave.for(path) + const { id = localStorage.getItem('autosave:post:last'), editorType } = AutoSave.for(path) const key = AutoSave.getStorageKey(id, editorType) return key ? localStorage[key] : null }