guardian / scribe

DEPRECATED: A rich text editor framework for the web platform
http://guardian.github.io/scribe/
Apache License 2.0
3.5k stars 245 forks source link

Overriding core commands/plugins? #240

Closed chanon closed 9 years ago

chanon commented 10 years ago

What can I do if I want to override or change the behavior of a core plugin?

For example, I want to change how the undo/redo commands work, but in the core undo/redo commands' code it adds a keydown event listener to detect ctrl-z to the editor element which I don't know how to remove.

Looking at the Scribe constructor, I don't see a way to make it not use the core undo/redo commands either (other than manually commenting the code lines in it).

bradvogel commented 10 years ago

Here's a rather hacky way to override ctrl-z:

  document.addEventListener('keydown', function(event) {
    if (event.target === scribe.el && !event.shiftKey && (event.metaKey || event.ctrlKey) && event.keyCode === 90) {
      event.stopImmediatePropagation(); // Stop other listeners from taking the event.
      event.preventDefault();
    }
  }, true /* Capture */ );
bregenspan commented 9 years ago

I ran into a similar case recently, which is that (sadly) I need to support IFRAMEs within the editor. For that to work nicely, the ensure-selectable-containers formatter needs to have IFRAME in its list of html5VoidElements to ignore. It might or might not make sense for Scribe to have IFRAME in that list by default (it's not technically a void element, though it should be treated as such for the purposes of an editor), so making this kind of thing configurable seems like a smart solution.

One proposal for adding configurability of core plugins: plugins could optionally register a name for themselves, and that could be used as a key for passing in configuration options at the time Scribe is instantiated. For example:

// Formatter

return function (options) {
  return function (scribe) {
    scribe.registerHTMLFormatter('normalize', 'ensureSelectableContainers', function (html) {
      ...
    });
  }
}
// Scribe instantiation (with options)

var scribe = new Scribe(document.querySelector('.rte'), {
  plugins: {
    ensureSelectableContainers: {
      voidElements: ['AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT', 'IFRAME', 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR']
    }
  }
});
hmgibson23 commented 9 years ago

This https://github.com/guardian/scribe/pull/275 take some steps towards doing exactly this. It makes sense to be able to override the core commands to me.

\cc @rrees

rrees commented 9 years ago

Yes this makes sense to me. Feel free to submit PRs if urgent for you otherwise I'll try to look at it when I can.

rrees commented 9 years ago

Version 1.2.0 includes this change #321 that allows you to choose the command patches that run. You can then use your own plugins.