MarkdownEditorDriver should be its own driver.

Splitting it out instead of hacking it onto BasicEditorDriver gives us more flexibility in customizing it for markdown area.
This commit is contained in:
Alexander Skvortsov 2021-02-27 15:37:19 -05:00
parent 9579871378
commit aa62e69dab
2 changed files with 34 additions and 25 deletions

View File

@ -9,11 +9,10 @@
import { extend, override } from 'flarum/extend';
import TextEditor from 'flarum/components/TextEditor';
import BasicEditorDriver from 'flarum/utils/BasicEditorDriver';
import MarkdownArea from 'mdarea';
import MarkdownToolbar from './components/MarkdownToolbar';
import MarkdownButton from './components/MarkdownButton';
import MarkdownAreaEditorDriver from './util/MarkdownAreaEditorDriver';
let shortcutHandler = () => { };
@ -24,31 +23,13 @@ app.initializers.add('flarum-markdown', function (app) {
this.textareaId = 'textarea' + (index++);
});
override(TextEditor.prototype, 'buildEditor', function (_, dom) {
return new MarkdownAreaEditorDriver(dom, this.buildEditorParams());
});
extend(TextEditor.prototype, 'buildEditorParams', function (params) {
params.textareaId = this.textareaId;
});
extend(BasicEditorDriver.prototype, 'build', function (_, dom, params) {
this.el.id = params.textareaId;
// We can't bind shortcutHandler directly in case `build`
// runs before MarkdownToolbar's `oninit`.
this.el.addEventListener('keydown', function (e) {
return shortcutHandler(...arguments);
});
this.mdarea = new MarkdownArea(this.el, {
keyMap: {
indent: ['Ctrl+m'],
outdent: ['Ctrl+M'],
inline: []
}
});
});
override(BasicEditorDriver.prototype, 'destroy', function (original) {
this.mdarea.destroy();
original();
params.shortcutHandler = shortcutHandler;
});
extend(TextEditor.prototype, 'toolbarItems', function (items) {

View File

@ -0,0 +1,28 @@
import MarkdownArea from 'mdarea';
import BasicEditorDriver from 'flarum/utils/BasicEditorDriver';
export default class MarkdownAreaEditorDriver extends BasicEditorDriver {
build(dom, params) {
super.build(dom, params);
this.el.id = params.textareaId;
// We can't bind shortcutHandler directly in case `build`
// runs before MarkdownToolbar's `oninit`.
this.el.addEventListener('keydown', function (e) {
return params.shortcutHandler(...arguments);
});
this.mdarea = new MarkdownArea(this.el, {
keyMap: {
indent: ['Ctrl+m'],
outdent: ['Ctrl+M'],
inline: []
}
});
}
destroy() {
this.mdarea.destroy();
super.destroy();
}
}