Skip to content

Commit 2701e71

Browse files
committed
Use keypress event to enter text
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
1 parent c529236 commit 2701e71

34 files changed

+1579
-1234
lines changed

src/js/editor/edit-state.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { contains, isArrayEqual } from 'mobiledoc-kit/utils/array-utils';
2+
3+
export default class EditState {
4+
constructor(editor) {
5+
this.editor = editor;
6+
this._activeMarkups = [];
7+
this._activeSections = [];
8+
}
9+
10+
get activeSections() {
11+
let { editor: { range, post } } = this;
12+
if (range.isBlank) {
13+
return [];
14+
} else {
15+
return post.sections.readRange(range.head.section, range.tail.section);
16+
}
17+
}
18+
19+
get activeMarkups() {
20+
let { editor: { cursor, post, range } } = this;
21+
22+
if (!cursor.hasCursor()) {
23+
return [];
24+
} else if (!this._activeMarkups) {
25+
this._activeMarkups = post.markupsInRange(range);
26+
}
27+
28+
return this._activeMarkups;
29+
}
30+
31+
toggleMarkupState(markup) {
32+
if (contains(this.activeMarkups, markup)) {
33+
this._removeActiveMarkup(markup);
34+
} else {
35+
this._addActiveMarkup(markup);
36+
}
37+
}
38+
39+
/**
40+
* @return {Boolean} Whether the markups after reset have changed
41+
*/
42+
resetActiveMarkups() {
43+
let prevMarkups = this._activeMarkups || [];
44+
delete this._activeMarkups;
45+
let markups = this.activeMarkups || [];
46+
47+
let didChange = !isArrayEqual(prevMarkups, markups);
48+
return didChange;
49+
}
50+
51+
_removeActiveMarkup(markup) {
52+
let index = this._activeMarkups.indexOf(markup);
53+
this._activeMarkups.splice(index, 1);
54+
}
55+
56+
_addActiveMarkup(markup) {
57+
this._activeMarkups.push(markup);
58+
}
59+
}

0 commit comments

Comments
 (0)