add custom event emitter
This commit is contained in:
@@ -15,7 +15,6 @@
|
|||||||
"@types/prosemirror-dropcursor": "^1.0.0",
|
"@types/prosemirror-dropcursor": "^1.0.0",
|
||||||
"@types/prosemirror-gapcursor": "^1.0.1",
|
"@types/prosemirror-gapcursor": "^1.0.1",
|
||||||
"collect.js": "^4.20.3",
|
"collect.js": "^4.20.3",
|
||||||
"events": "^3.1.0",
|
|
||||||
"prosemirror-commands": "^1.1.3",
|
"prosemirror-commands": "^1.1.3",
|
||||||
"prosemirror-dropcursor": "^1.3.2",
|
"prosemirror-dropcursor": "^1.3.2",
|
||||||
"prosemirror-gapcursor": "^1.1.4",
|
"prosemirror-gapcursor": "^1.1.4",
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { EventEmitter } from 'events'
|
|
||||||
import { EditorState, TextSelection } from 'prosemirror-state'
|
import { EditorState, TextSelection } from 'prosemirror-state'
|
||||||
import { EditorView} from 'prosemirror-view'
|
import { EditorView} from 'prosemirror-view'
|
||||||
import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model'
|
import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model'
|
||||||
@@ -19,6 +18,7 @@ import getSchemaTypeByName from './utils/getSchemaTypeByName'
|
|||||||
import ExtensionManager from './ExtensionManager'
|
import ExtensionManager from './ExtensionManager'
|
||||||
import Extension from './Extension'
|
import Extension from './Extension'
|
||||||
import Node from './Node'
|
import Node from './Node'
|
||||||
|
import EventEmitter from './EventEmitter'
|
||||||
|
|
||||||
type EditorContent = string | JSON | null
|
type EditorContent = string | JSON | null
|
||||||
type Command = (next: Function, editor: Editor, ...args: any) => any
|
type Command = (next: Function, editor: Editor, ...args: any) => any
|
||||||
@@ -73,7 +73,9 @@ export class Editor extends EventEmitter {
|
|||||||
const command = this.commands[name]
|
const command = this.commands[name]
|
||||||
|
|
||||||
if (!command) {
|
if (!command) {
|
||||||
throw new Error(`tiptap: command '${name}' not found.`)
|
// TODO: prevent vue devtools to throw error
|
||||||
|
// throw new Error(`tiptap: command '${name}' not found.`)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return (...args: any) => command(...args)
|
return (...args: any) => command(...args)
|
||||||
|
|||||||
42
packages/core/src/EventEmitter.ts
Normal file
42
packages/core/src/EventEmitter.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
export default class EventEmitter {
|
||||||
|
|
||||||
|
_callbacks: { [key: string]: Function[] } = {}
|
||||||
|
|
||||||
|
on(event: string, fn: Function) {
|
||||||
|
if (!this._callbacks[event]) {
|
||||||
|
this._callbacks[event] = []
|
||||||
|
}
|
||||||
|
|
||||||
|
this._callbacks[event].push(fn)
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(event: string, ...args: any) {
|
||||||
|
const callbacks = this._callbacks[event]
|
||||||
|
|
||||||
|
if (callbacks) {
|
||||||
|
callbacks.forEach(callback => callback.apply(this, args))
|
||||||
|
}
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
off(event: string, fn?: Function) {
|
||||||
|
const callbacks = this._callbacks[event]
|
||||||
|
|
||||||
|
if (callbacks) {
|
||||||
|
if (fn) {
|
||||||
|
this._callbacks[event] = callbacks.filter(callback => callback !== fn)
|
||||||
|
} else {
|
||||||
|
delete this._callbacks[event]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
removeAllListeners() {
|
||||||
|
this._callbacks = {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5541,7 +5541,7 @@ eventemitter3@^3.1.0:
|
|||||||
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
|
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
|
||||||
integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==
|
integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==
|
||||||
|
|
||||||
events@^3.0.0, events@^3.1.0:
|
events@^3.0.0:
|
||||||
version "3.1.0"
|
version "3.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/events/-/events-3.1.0.tgz#84279af1b34cb75aa88bf5ff291f6d0bd9b31a59"
|
resolved "https://registry.yarnpkg.com/events/-/events-3.1.0.tgz#84279af1b34cb75aa88bf5ff291f6d0bd9b31a59"
|
||||||
integrity sha512-Rv+u8MLHNOdMjTAFeT3nCjHn2aGlx435FP/sDHNaRhDEMwyI/aB22Kj2qIN8R0cw3z28psEQLYwxVKLsKrMgWg==
|
integrity sha512-Rv+u8MLHNOdMjTAFeT3nCjHn2aGlx435FP/sDHNaRhDEMwyI/aB22Kj2qIN8R0cw3z28psEQLYwxVKLsKrMgWg==
|
||||||
|
|||||||
Reference in New Issue
Block a user