From 90f04982651605043e4cf09754572a19214bc7ec Mon Sep 17 00:00:00 2001 From: BrianHung Date: Wed, 8 Apr 2020 03:17:59 -0700 Subject: [PATCH 1/2] Change registerPlugin to add plugin after Extensions plugins This commit changes `registerPlugin` by 1. adding the new plugin to `this.plugins` and 2. updating `this.plugins` within `this.state.plugins` by splicing with the new `this.plugins` length. Previously, new plugins were simply added to the end of `this.state.plugins` and were not appended to `this.plugins`. By placing new plugins at this new location within `this.state.plugins`, we prioritize registered plugins over default ProseMirror plugins such as `keymap(baseKeymap)`. This will allow new plugins to have precedence over props such as `handleKeyDown`. --- packages/tiptap/src/Editor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/tiptap/src/Editor.js b/packages/tiptap/src/Editor.js index 1e138b68..fe31a117 100644 --- a/packages/tiptap/src/Editor.js +++ b/packages/tiptap/src/Editor.js @@ -492,9 +492,9 @@ export default class Editor extends Emitter { return } - const newState = this.state.reconfigure({ - plugins: this.state.plugins.concat([plugin]), - }) + this.plugins.push(plugin); + this.state.plugins.splice(this.plugins.length, 0, plugin); + const newState = this.state.reconfigure({plugins: this.state.plugins}); this.view.updateState(newState) } From f9d29865b71e61c4b92ca1f67b9450f7e8dff4d5 Mon Sep 17 00:00:00 2001 From: Brian Hung Date: Sat, 11 Apr 2020 15:29:53 -0700 Subject: [PATCH 2/2] Updated registerPlugin and README with handlePlugins argument. --- README.md | 2 +- packages/tiptap/src/Editor.js | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8365e795..16449e36 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ export default { | `setContent` | `content, emitUpdate, parseOptions` | Replace the current content. You can pass an HTML string or a JSON document. `emitUpdate` defaults to `false`. `parseOptions` defaults to those provided in constructor. | | `clearContent` | `emitUpdate` | Clears the current content. `emitUpdate` defaults to `false`. | | `setOptions` | `options` | Overwrites the current editor properties. | -| `registerPlugin` | `plugin` | Register a Prosemirror plugin. | +| `registerPlugin` | `plugin`, `handlePlugins` | Register a Prosemirror plugin. You can pass a function `handlePlugins` with parameters `(plugin, oldPlugins)` to define an order in which `newPlugins` will be called. `handlePlugins` defaults to pushing `plugin` to front of `oldPlugins`. | | `getJSON` | – | Get the current content as JSON. | | `getHTML` | – | Get the current content as HTML. | | `focus` | – | Focus the editor. | diff --git a/packages/tiptap/src/Editor.js b/packages/tiptap/src/Editor.js index fe31a117..120f4c3a 100644 --- a/packages/tiptap/src/Editor.js +++ b/packages/tiptap/src/Editor.js @@ -487,14 +487,10 @@ export default class Editor extends Emitter { }), {}) } - registerPlugin(plugin = null) { - if (!plugin) { - return - } - - this.plugins.push(plugin); - this.state.plugins.splice(this.plugins.length, 0, plugin); - const newState = this.state.reconfigure({plugins: this.state.plugins}); + registerPlugin(plugin = null, handlePlugins) { + const plugins = typeof handlePlugins === 'function' + ? handlePlugins(plugin, this.state.plugins) : [...plugin, this.state.plugins] + const newState = this.state.reconfigure({ plugins }) this.view.updateState(newState) }