add destroy event

This commit is contained in:
Philipp Kühn
2020-11-30 13:50:06 +01:00
parent 6f3517a5cf
commit 6c39aea432
7 changed files with 33 additions and 5 deletions

View File

@@ -114,7 +114,6 @@ export default {
return { return {
name: this.getRandomName(), name: this.getRandomName(),
color: this.getRandomColor(), color: this.getRandomColor(),
provider: null,
indexdb: null, indexdb: null,
editor: null, editor: null,
users: [], users: [],
@@ -123,17 +122,17 @@ export default {
mounted() { mounted() {
const ydoc = new Y.Doc() const ydoc = new Y.Doc()
this.provider = new WebrtcProvider('tiptap-collaboration-example', ydoc) const provider = new WebrtcProvider('tiptap-collaboration-example', ydoc)
this.indexdb = new IndexeddbPersistence('tiptap-collaboration-example', ydoc) this.indexdb = new IndexeddbPersistence('tiptap-collaboration-example', ydoc)
this.editor = new Editor({ this.editor = new Editor({
extensions: [ extensions: [
...defaultExtensions(), ...defaultExtensions(),
Collaboration.configure({ Collaboration.configure({
provider: this.provider, provider,
}), }),
CollaborationCursor.configure({ CollaborationCursor.configure({
provider: this.provider, provider,
user: { user: {
name: this.name, name: this.name,
color: this.color, color: this.color,
@@ -201,7 +200,6 @@ export default {
beforeDestroy() { beforeDestroy() {
this.editor.destroy() this.editor.destroy()
this.provider.destroy()
}, },
} }
</script> </script>

View File

@@ -385,6 +385,8 @@ export class Editor extends EventEmitter {
* Destroy the editor. * Destroy the editor.
*/ */
public destroy() { public destroy() {
this.emit('destroy')
if (this.view) { if (this.view) {
this.view.destroy() this.view.destroy()
} }

View File

@@ -62,6 +62,11 @@ export interface ExtensionConfig<Options = any, Commands = {}> {
options: Options, options: Options,
editor: Editor, editor: Editor,
}) => Plugin[], }) => Plugin[],
onDestroy?: ((this: {
options: Options,
editor: Editor,
}) => void) | null,
} }
export class Extension<Options = any, Commands = any> { export class Extension<Options = any, Commands = any> {
@@ -76,6 +81,7 @@ export class Extension<Options = any, Commands = any> {
addInputRules: () => [], addInputRules: () => [],
addPasteRules: () => [], addPasteRules: () => [],
addProseMirrorPlugins: () => [], addProseMirrorPlugins: () => [],
onDestroy: null,
} }
options!: Options options!: Options

View File

@@ -33,6 +33,10 @@ export default class ExtensionManager {
const commands = extension.config.addCommands.bind(context)() const commands = extension.config.addCommands.bind(context)()
editor.registerCommands(commands) editor.registerCommands(commands)
if (typeof extension.config.onDestroy === 'function') {
this.editor.on('destroy', extension.config.onDestroy.bind(context))
}
}) })
} }

View File

@@ -108,6 +108,12 @@ export interface MarkConfig<Options = any, Commands = {}> extends Overwrite<Exte
editor: Editor, editor: Editor,
type: MarkType, type: MarkType,
}) => Plugin[], }) => Plugin[],
onDestroy?: ((this: {
options: Options,
editor: Editor,
type: MarkType,
}) => void) | null,
}> {} }> {}
export class Mark<Options = any, Commands = {}> { export class Mark<Options = any, Commands = {}> {
@@ -129,6 +135,7 @@ export class Mark<Options = any, Commands = {}> {
parseHTML: () => null, parseHTML: () => null,
renderHTML: null, renderHTML: null,
addAttributes: () => ({}), addAttributes: () => ({}),
onDestroy: null,
} }
options!: Options options!: Options

View File

@@ -152,6 +152,12 @@ export interface NodeConfig<Options = any, Commands = {}> extends Overwrite<Exte
editor: Editor, editor: Editor,
type: NodeType, type: NodeType,
}) => NodeViewRenderer) | null, }) => NodeViewRenderer) | null,
onDestroy?: ((this: {
options: Options,
editor: Editor,
type: NodeType,
}) => void) | null,
}> {} }> {}
export class Node<Options = any, Commands = {}> { export class Node<Options = any, Commands = {}> {
@@ -181,6 +187,7 @@ export class Node<Options = any, Commands = {}> {
renderHTML: null, renderHTML: null,
addAttributes: () => ({}), addAttributes: () => ({}),
addNodeView: null, addNodeView: null,
onDestroy: null,
} }
options!: Options options!: Options

View File

@@ -31,6 +31,10 @@ const Collaboration = Extension.create({
'Mod-Shift-z': redo, 'Mod-Shift-z': redo,
} }
}, },
onDestroy() {
this.options.provider?.destroy()
},
}) })
export default Collaboration export default Collaboration