Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change registerPlugin to add plugin after Extensions plugins #658

Merged
merged 2 commits into from Apr 12, 2020

Conversation

BrianHung
Copy link
Contributor

@BrianHung BrianHung commented Apr 8, 2020

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.

Screen Shot 2020-04-08 at 3 27 19 AM

By placing registered 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. A use case of this defining a floating Editor Menu that's navigable with arrow and enter keys.

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`.
Comment on lines 495 to 497
this.plugins.push(plugin);
this.state.plugins.splice(this.plugins.length, 0, plugin);
const newState = this.state.reconfigure({plugins: this.state.plugins});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the same with [plugin, ...this.state.plugins], right? 🤔

Copy link
Contributor Author

@BrianHung BrianHung Apr 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushing new plugins to the front of this.state.plugins could also work, as long as they're in-front of the default ProseMirror plugins.

An alternative to accommodate both of these solutions would be to modify parameters of registerPlugin, and to allow a helper function to be passed in, e.g.

  registerPlugin(plugin, helper) {
    let plugins = typeof helper == 'function' ? helper(plugin, this.state.plugins) : [...plugin, this.state.plugins];
    const newState = this.state.reconfigure({plugins});
    this.view.updateState(newState)
  }

This would allow custom behavior for where new plugins should be registered within this.state.plugins. An example is if plugins had to be registered in a certain order. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Lets do this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've updated the PR to allow a function to be passed in. :)

@philippkuehn philippkuehn merged commit 9bf8be4 into ueberdosis:master Apr 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants