Skip to content

Commit

Permalink
Refactor inputModeDidChange to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemelia committed Jun 5, 2020
1 parent 93653e2 commit 118b1d1
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions addon/components/mobiledoc-editor/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,42 @@ export default Component.extend({
},

inputModeDidChange(editor) {
const markupTags = arrayToMap(editor.activeMarkups.map(m => m.tagName));
let activeMarkupTagNames = this.getActiveMarkupTagNames(editor);
let activeSectionTagNames = this.getActiveSectionTagNames(editor);
let activeSectionAttributes = this.getActiveSectionAttributes(editor);

let setEditorProps = () => {
this.setProperties({
activeMarkupTagNames,
activeSectionTagNames,
activeSectionAttributes
});
}
// Avoid updating this component's properties synchronously while
// rendering the editor (after rendering the component) because it
// causes Ember to display deprecation warnings
if (this._isRenderingEditor) {
schedule('afterRender', setEditorProps);
} else {
setEditorProps();
}
},

getActiveMarkupTagNames(editor) {
return arrayToMap(editor.activeMarkups.map(m => m.tagName));
},

getActiveSectionTagNames(editor) {
// editor.activeSections are leaf sections.
// Map parent section tag names (e.g. 'p', 'ul', 'ol') so that list buttons
// can be bound.
// Also build a map of section attributes for the same reason.
let sectionParentTagNames = editor.activeSections.map(s => {
return s.isNested ? s.parent.tagName : s.tagName;
});
const sectionTags = arrayToMap(sectionParentTagNames);
return arrayToMap(sectionParentTagNames);
},

getActiveSectionAttributes(editor) {
const sectionAttributes = {};
editor.activeSections.forEach(s => {
let attributes = s.isNested ? s.parent.attributes : s.attributes;
Expand All @@ -359,22 +386,7 @@ export default Component.extend({
}
});
});

let setEditorProps = () => {
this.setProperties({
activeMarkupTagNames: markupTags,
activeSectionTagNames: sectionTags,
activeSectionAttributes: sectionAttributes
});
}
// Avoid updating this component's properties synchronously while
// rendering the editor (after rendering the component) because it
// causes Ember to display deprecation warnings
if (this._isRenderingEditor) {
schedule('afterRender', setEditorProps);
} else {
setEditorProps();
}
return sectionAttributes;
},

cursorDidChange(/*editor*/) {
Expand Down

0 comments on commit 118b1d1

Please sign in to comment.