mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2026-02-06 09:56:03 +00:00
56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
import { Extension, Plugin, PluginKey } from 'tiptap'
|
|
import { DOMSerializer } from 'prosemirror-model'
|
|
|
|
export default class AutoSave extends Extension {
|
|
constructor({ $route }) {
|
|
super()
|
|
this.route = $route
|
|
this._postId = 'randomIdForPosts'
|
|
}
|
|
|
|
static toHTML(content, schema) {
|
|
const container = document.createElement('div')
|
|
const fragment = DOMSerializer.fromSchema(schema).serializeFragment(content)
|
|
container.appendChild(fragment)
|
|
return container.innerHTML
|
|
}
|
|
|
|
get name() {
|
|
return 'auto_save'
|
|
}
|
|
|
|
get storageKey() {
|
|
if (this.route.path === '/post/create') {
|
|
return `draft:post:${this._postId}`
|
|
}
|
|
return null
|
|
}
|
|
|
|
get plugins() {
|
|
return [
|
|
new Plugin({
|
|
key: new PluginKey('auto_save'),
|
|
filterTransaction: (tr, editorState) => {
|
|
if (tr.docChanged) {
|
|
localStorage.setItem(
|
|
this.storageKey,
|
|
AutoSave.toHTML(tr.doc.content, editorState.config.schema),
|
|
)
|
|
}
|
|
return tr
|
|
},
|
|
state: {
|
|
init() {
|
|
return {
|
|
saveNextUpdate: false,
|
|
}
|
|
},
|
|
apply(_, prev) {
|
|
return { ...prev }
|
|
},
|
|
},
|
|
}),
|
|
]
|
|
}
|
|
}
|