Skip to content

Commit

Permalink
Extensibility hooks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ef4 committed Mar 13, 2016
1 parent 0086ed7 commit 8c67d33
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions addon/components/mobiledoc-editor/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
Expand Down

0 comments on commit 8c67d33

Please sign in to comment.