Handle editor-driven input events.

The mdarea extension's handleKey method only applies when input is accompanied by an actual key press, and won't register programmatic inputs via `app.composer.editor` methods. We add an additional listener for custom events, which are dispatched in `setValue`.
This commit is contained in:
Alexander Skvortsov 2021-03-08 16:09:18 -05:00
parent 6cbd641ad5
commit 50da5ffd45

View File

@ -40,12 +40,6 @@ export default class MarkdownAreaEditorDriver extends BasicEditorDriver {
dom.append(this.el); dom.append(this.el);
// We can't bind shortcutHandler directly in case `build`
// runs before MarkdownToolbar's `oninit`.
this.el.addEventListener('keydown', function (e) {
return params.shortcutHandler(...arguments);
});
const callInputListeners = (e) => { const callInputListeners = (e) => {
params.inputListeners.forEach((listener) => { params.inputListeners.forEach((listener) => {
listener(); listener();
@ -54,6 +48,21 @@ export default class MarkdownAreaEditorDriver extends BasicEditorDriver {
e.redraw = false; e.redraw = false;
}; };
// We can't bind shortcutHandler directly in case `build`
// runs before MarkdownToolbar's `oninit`.
this.el.addEventListener('keydown', function (e) {
return params.shortcutHandler(...arguments);
});
// Our mdarea extension won't detect programmatic changes via
// the `app.composer.editor api.
this.el.addEventListener('input', function (e) {
if (e instanceof CustomEvent) {
params.oninput(e.target.value);
callInputListeners(e);
}
});
// This one can't be run through mdarea, but that doesn't matter // This one can't be run through mdarea, but that doesn't matter
// because mdarea doesn't change value in response to clicks. // because mdarea doesn't change value in response to clicks.
this.el.addEventListener('click', callInputListeners); this.el.addEventListener('click', callInputListeners);