Change: use all editorTypes in storageKeys, not only when post

This commit is contained in:
Raphael Beer 2020-04-13 14:32:07 +02:00
parent 89638beacb
commit 726a34a30c
No known key found for this signature in database
GPG Key ID: C1AC5E018B25EF11
3 changed files with 64 additions and 21 deletions

View File

@ -36,18 +36,7 @@ const users = [
storiesOf('Editor', module)
.addDecorator(withA11y)
.addDecorator(StoryRouter())
.addDecorator((storyFn) => {
const ctx = storyFn()
return {
components: { ctx },
template: `
<base-card style="width: 50%; min-width: 500px; margin: 0 auto;">
<ctx />
</base-card>
`,
}
})
.addDecorator(helpers.wrapInCard)
.addDecorator(helpers.layout)
.add('Empty', () => ({
components: { HcEditor },
@ -150,3 +139,43 @@ storiesOf('Editor', module)
}),
template: `<hc-editor :users="users" :value="content" />`,
}))
storiesOf('Editor/AutoSave', module)
.addDecorator(
StoryRouter(
{},
{
initialEntry: '/post/create',
},
),
)
.addDecorator(helpers.wrapInCard)
.addDecorator(helpers.layout)
.add('creating a post', () => ({
components: { HcEditor },
store: helpers.store,
data: () => ({
users,
}),
template: `<hc-editor :users="users" />`,
}))
storiesOf('Editor/AutoSave', module)
.addDecorator(
StoryRouter(
{},
{
initialEntry: '/post/f00-b00-1d/post-slug-I-comment-on',
},
),
)
.addDecorator(helpers.wrapInCard)
.addDecorator(helpers.layout)
.add('creating a comment', () => ({
components: { HcEditor },
store: helpers.store,
data: () => ({
users,
}),
template: `<hc-editor :users="users" />`,
}))

View File

@ -7,9 +7,10 @@ export default class AutoSave extends Extension {
super()
this.route = $route
const { id = hash(Date.now().toString(), 0xb0b).toString(16), editorType } = AutoSave.fromPath(
this.route.path,
)
const {
id = hash(Date.now().toString(), 0xb0b).toString(16),
editorType = this.route.path,
} = AutoSave.fromPath(this.route.path)
this.id = id
this.editorType = editorType
}
@ -35,9 +36,13 @@ export default class AutoSave extends Extension {
}
static load(path) {
const { id = localStorage.getItem('autosave:post:last'), editorType } = AutoSave.fromPath(path)
const key = AutoSave.getStorageKey(id, editorType)
return key ? localStorage[key] : null
const { id, editorType = path } = AutoSave.fromPath(path)
const _id = localStorage.getItem(`autosave:${editorType}:last`)
if (!_id) {
return null
}
const key = AutoSave.getStorageKey(_id, editorType)
return localStorage[key]
}
static getStorageKey(id, editorType) {
@ -62,9 +67,7 @@ export default class AutoSave extends Extension {
this.storageKey,
AutoSave.toHTML(tr.doc.content, editorState.config.schema),
)
if (this.editorType === 'post') {
localStorage.setItem('autosave:post:last', this.id)
}
localStorage.setItem(`autosave:${this.editorType}:last`, this.id)
}
return tr
},

View File

@ -57,6 +57,17 @@ const helpers = {
</layout>`,
}
},
wrapInCard(storyFn) {
const ctx = storyFn()
return {
components: { ctx },
template: `
<base-card style="width: 50%; min-width: 500px; margin: 0 auto;">
<ctx />
</base-card>
`,
}
}
}
export default helpers