Added editor instance event hooks

As per #1721
This commit is contained in:
Dan Brown 2019-10-16 18:01:35 +01:00
parent b24279cc12
commit 76bd0fdfa6
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 47 additions and 8 deletions

View File

@ -30,6 +30,12 @@ class MarkdownEditor {
this.displayDoc = this.display.contentDocument; this.displayDoc = this.display.contentDocument;
this.init(); this.init();
}); });
window.$events.emitPublic(elem, 'editor-markdown::setup', {
markdownIt: this.markdown,
displayEl: this.display,
codeMirrorInstance: this.cm,
});
} }
init() { init() {

View File

@ -412,6 +412,7 @@ class WysiwygEditor {
this.loadPlugins(); this.loadPlugins();
this.tinyMceConfig = this.getTinyMceConfig(); this.tinyMceConfig = this.getTinyMceConfig();
window.$events.emitPublic(elem, 'editor-tinymce::pre-init', {config: this.tinyMceConfig});
window.tinymce.init(this.tinyMceConfig); window.tinymce.init(this.tinyMceConfig);
} }
@ -654,6 +655,8 @@ class WysiwygEditor {
// Paste image-uploads // Paste image-uploads
editor.on('paste', event => editorPaste(event, editor, context)); editor.on('paste', event => editorPaste(event, editor, context));
// Custom handler hook
window.$events.emitPublic(context.elem, 'editor-tinymce::setup', {editor});
} }
}; };
} }

View File

@ -240,24 +240,27 @@ function setContent(cmInstance, codeContent) {
} }
/** /**
* Get a CodeMirror instace to use for the markdown editor. * Get a CodeMirror instance to use for the markdown editor.
* @param {HTMLElement} elem * @param {HTMLElement} elem
* @returns {*} * @returns {*}
*/ */
function markdownEditor(elem) { function markdownEditor(elem) {
let content = elem.textContent; const content = elem.textContent;
const config = {
return CodeMirror(function (elt) {
elem.parentNode.insertBefore(elt, elem);
elem.style.display = 'none';
}, {
value: content, value: content,
mode: "markdown", mode: "markdown",
lineNumbers: true, lineNumbers: true,
theme: getTheme(), theme: getTheme(),
lineWrapping: true, lineWrapping: true,
scrollPastEnd: true, scrollPastEnd: true,
}); };
window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {config});
return CodeMirror(function (elt) {
elem.parentNode.insertBefore(elt, elem);
elem.style.display = 'none';
}, config);
} }
/** /**

View File

@ -7,6 +7,12 @@ class Events {
this.stack = []; this.stack = [];
} }
/**
* Emit a custom event for any handlers to pick-up.
* @param {String} eventName
* @param {*} eventData
* @returns {Events}
*/
emit(eventName, eventData) { emit(eventName, eventData) {
this.stack.push({name: eventName, data: eventData}); this.stack.push({name: eventName, data: eventData});
if (typeof this.listeners[eventName] === 'undefined') return this; if (typeof this.listeners[eventName] === 'undefined') return this;
@ -18,11 +24,32 @@ class Events {
return this; return this;
} }
/**
* Listen to a custom event and run the given callback when that event occurs.
* @param {String} eventName
* @param {Function} callback
* @returns {Events}
*/
listen(eventName, callback) { listen(eventName, callback) {
if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = []; if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = [];
this.listeners[eventName].push(callback); this.listeners[eventName].push(callback);
return this; return this;
} }
/**
* Emit an event for public use.
* Sends the event via the native DOM event handling system.
* @param {Element} targetElement
* @param {String} eventName
* @param {Object} eventData
*/
emitPublic(targetElement, eventName, eventData) {
const event = new CustomEvent(eventName, {
detail: eventData,
bubbles: true
});
targetElement.dispatchEvent(event);
}
} }
export default Events; export default Events;