Skip to content

Commit

Permalink
Use keypress event to enter text
Browse files Browse the repository at this point in the history
Use EditState to maintain editor state

Move reading of activeSections off cursor, onto EditState

Ensure that cursorDidChange event fires when active sections change

Ensure cursor change callbacks do not fire before rendering
  • Loading branch information
bantic committed Mar 17, 2016
1 parent c529236 commit 2701e71
Show file tree
Hide file tree
Showing 34 changed files with 1,579 additions and 1,234 deletions.
59 changes: 59 additions & 0 deletions src/js/editor/edit-state.js
@@ -0,0 +1,59 @@
import { contains, isArrayEqual } from 'mobiledoc-kit/utils/array-utils';

export default class EditState {
constructor(editor) {
this.editor = editor;
this._activeMarkups = [];
this._activeSections = [];
}

get activeSections() {
let { editor: { range, post } } = this;
if (range.isBlank) {
return [];
} else {
return post.sections.readRange(range.head.section, range.tail.section);
}
}

get activeMarkups() {
let { editor: { cursor, post, range } } = this;

if (!cursor.hasCursor()) {
return [];
} else if (!this._activeMarkups) {
this._activeMarkups = post.markupsInRange(range);
}

return this._activeMarkups;
}

toggleMarkupState(markup) {
if (contains(this.activeMarkups, markup)) {
this._removeActiveMarkup(markup);
} else {
this._addActiveMarkup(markup);
}
}

/**
* @return {Boolean} Whether the markups after reset have changed
*/
resetActiveMarkups() {
let prevMarkups = this._activeMarkups || [];
delete this._activeMarkups;
let markups = this.activeMarkups || [];

let didChange = !isArrayEqual(prevMarkups, markups);
return didChange;
}

_removeActiveMarkup(markup) {
let index = this._activeMarkups.indexOf(markup);
this._activeMarkups.splice(index, 1);
}

_addActiveMarkup(markup) {
this._activeMarkups.push(markup);
}
}

0 comments on commit 2701e71

Please sign in to comment.