Add: load last autosave for /post/create

This commit is contained in:
Raphael Beer 2020-04-12 06:07:10 +02:00
parent d07d2ba5b3
commit f98ee6a1f6
No known key found for this signature in database
GPG Key ID: C1AC5E018B25EF11
2 changed files with 11 additions and 3 deletions

View File

@ -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', () => {

View File

@ -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
}