From 8c67d3313bfec9f4aa5f8239e70ff1f6ccc821d2 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Sun, 13 Mar 2016 15:55:02 -0400 Subject: [PATCH] Extensibility hooks This closes upstream issue #24 by adding will/didCreateEditor hooks, plus it refactors the update and cursor hooks so they can be extended within the same run loop. --- .../components/mobiledoc-editor/component.js | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/addon/components/mobiledoc-editor/component.js b/addon/components/mobiledoc-editor/component.js index 73162c6..918f705 100644 --- a/addon/components/mobiledoc-editor/component.js +++ b/addon/components/mobiledoc-editor/component.js @@ -174,6 +174,8 @@ export default Component.extend({ this._upstreamMobiledoc = mobiledoc; this._localMobiledoc = null; + this.willCreateEditor(); + // Teardown any old editor that might be around. let editor = this.get('editor'); if (editor) { @@ -237,34 +239,20 @@ export default Component.extend({ }); editor.on('update', () => { Ember.run.join(() => { - let serializeVersion = this.get('serializeVersion'); - let updatedMobileDoc = editor.serialize(serializeVersion); - this._localMobiledoc = updatedMobileDoc; - this.sendAction('on-change', updatedMobileDoc); + this.editorUpdated(editor); }); }); editor.cursorDidChange(() => { if (this.isDestroyed) { return; } - Ember.run.join(() => { - const markupTags = arrayToMap(editor.markupsInSelection, 'tagName'); - const sectionTags = arrayToMap(editor.activeSections, 'tagName'); - - this.set('activeMarkupTagNames', markupTags); - this.set('activeSectionTagNames', sectionTags); - - let isCursorOffEditor = !this.get('editor').cursor.offsets.head.section; - if (!isCursorOffEditor && !this._ignoreCursorDidChange) { - this.set('linkOffsets', null); - } else { - this._ignoreCursorDidChange = false; - } + this.cursorDidChange(editor); }); }); if (this.get('isEditingDisabled')) { editor.disableEditing(); } this.set('editor', editor); + this.didCreateEditor(); }, didRender() { @@ -280,6 +268,31 @@ export default Component.extend({ editor.destroy(); }, + editorUpdated(editor) { + let serializeVersion = this.get('serializeVersion'); + let updatedMobileDoc = editor.serialize(serializeVersion); + this._localMobiledoc = updatedMobileDoc; + this.sendAction('on-change', updatedMobileDoc); + }, + + cursorDidChange(editor) { + const markupTags = arrayToMap(editor.markupsInSelection, 'tagName'); + const sectionTags = arrayToMap(editor.activeSections, 'tagName'); + + this.set('activeMarkupTagNames', markupTags); + this.set('activeSectionTagNames', sectionTags); + + let isCursorOffEditor = !this.get('editor').cursor.offsets.head.section; + if (!isCursorOffEditor && !this._ignoreCursorDidChange) { + this.set('linkOffsets', null); + } else { + this._ignoreCursorDidChange = false; + } + }, + + willCreateEditor: Ember.K, + didCreateEditor: Ember.K, + _addCard(cardName, payload, editMode=false) { let editor = this.get('editor'); let section = editor.activeSection;