-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
34 changed files
with
1,579 additions
and
1,234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.