@@ -78,21 +78,22 @@ const CALLBACK_QUEUES = {
7878 DID_REPARSE : 'didReparse'
7979} ;
8080
81+ /**
82+ * The Editor is a core component of mobiledoc-kit. After instantiating
83+ * an editor, use {@link Editor#render} to display the editor on the web page.
84+ *
85+ * An editor uses a {@link Post} internally to represent the displayed document.
86+ * The post can be serialized as mobiledoc using {@link Editor#serialize}. Mobiledoc
87+ * is the transportable "over-the-wire" format (JSON) that is suited for persisting
88+ * and sharing between editors and renderers (for display, e.g.), whereas the Post
89+ * model is better suited for programmatic editing.
90+ *
91+ * The editor will call registered callbacks for certain state changes. These are:
92+ * * cursorDidChange
93+ * * didUpdate
94+ */
8195class Editor {
8296 /**
83- * The Editor is a core component of mobiledoc-kit. After instantiating
84- * an editor, use {@link Editor#render} to display the editor on the web page.
85- *
86- * An editor uses a {@link Post} internally to represent the displayed document.
87- * The post can be serialized as mobiledoc using {@link Editor#serialize}. Mobiledoc
88- * is the transportable "over-the-wire" format (JSON) that is suited for persisting
89- * and sharing between editors and renderers (for display, e.g.), whereas the Post
90- * model is better suited for programmatic editing.
91- *
92- * The editor will call registered callbacks for certain state changes. These are:
93- * * cursorDidChange
94- * * didUpdate
95- *
9697 * @param {Object } [options]
9798 * @param {Object } [options.mobiledoc] The mobiledoc to load into the editor.
9899 * Supersedes `options.html`.
@@ -142,10 +143,10 @@ class Editor {
142143 this . hasRendered = false ;
143144 }
144145
145- addView ( view ) {
146- this . _views . push ( view ) ;
147- }
148-
146+ /**
147+ * The editor's instance of a post node builder.
148+ * @type { PostNodeBuilder }
149+ */
149150 get builder ( ) {
150151 if ( ! this . _builder ) { this . _builder = new PostNodeBuilder ( ) ; }
151152 return this . _builder ;
@@ -446,7 +447,7 @@ class Editor {
446447 }
447448
448449 /**
449- * @return {array } The sections from the cursor's selection start to the selection end
450+ * @return {Section[] } The sections from the cursor's selection start to the selection end
450451 */
451452 get activeSections ( ) {
452453 return this . _editState . activeSections ;
@@ -468,6 +469,10 @@ class Editor {
468469 return this . _editState . activeMarkups ;
469470 }
470471
472+ /**
473+ * @param {Markup|String } markup A markup instance, or a string (e.g. "b")
474+ * @return {boolean }
475+ */
471476 hasActiveMarkup ( markup ) {
472477 markup = this . builder . _coerceMarkup ( markup ) ;
473478 return contains ( this . activeMarkups , markup ) ;
@@ -479,10 +484,9 @@ class Editor {
479484 }
480485
481486 /**
482- * @public
483- *
484- * @param {string } version The mobiledoc version to serialize to.
487+ * @param {String } version The mobiledoc version to serialize to.
485488 * @return {Mobiledoc } Serialized mobiledoc
489+ * @public
486490 */
487491 serialize ( version = MOBILEDOC_VERSION ) {
488492 return this . serializePost ( this . post , 'mobiledoc' , { version} ) ;
@@ -537,6 +541,10 @@ class Editor {
537541 }
538542 }
539543
544+ addView ( view ) {
545+ this . _views . push ( view ) ;
546+ }
547+
540548 removeAllViews ( ) {
541549 this . _views . forEach ( ( v ) => v . destroy ( ) ) ;
542550 this . _views = [ ] ;
@@ -704,7 +712,7 @@ class Editor {
704712 }
705713
706714 /**
707- * @param {Function } callback This callback will be called after the cursor
715+ * @param {Function } callback This callback will be called every time the cursor
708716 * position (or selection) changes.
709717 * @public
710718 */
@@ -772,6 +780,16 @@ class Editor {
772780 } ) ;
773781 }
774782
783+ /**
784+ * Toggles the given markup at the editor's current {@link Range}.
785+ * If the range is collapsed this changes the editor's state so that the
786+ * next characters typed will be affected. If there is text selected
787+ * (aka a non-collapsed range), the selections' markup will be toggled.
788+ * If the editor is not focused and has no active range, nothing happens.
789+ * @param {String } markup E.g. "b", "em", "a"
790+ * @public
791+ * @see PostEditor#toggleMarkup
792+ */
775793 toggleMarkup ( markup ) {
776794 markup = this . post . builder . createMarkup ( markup ) ;
777795 if ( this . range . isCollapsed ) {
@@ -822,6 +840,12 @@ class Editor {
822840 return false ;
823841 }
824842
843+ /**
844+ * Inserts the text at the current cursor position. If the editor has
845+ * no current cursor position, nothing will be inserted.
846+ * @param {String } text
847+ * @public
848+ */
825849 insertText ( text ) {
826850 let { activeMarkups, range, range : { head : position } } = this ;
827851
@@ -859,6 +883,22 @@ class Editor {
859883 triggerEvent ( context , eventName , event ) {
860884 this . _eventManager . _trigger ( context , eventName , event ) ;
861885 }
886+
887+ addCallback ( ...args ) {
888+ this . _callbacks . addCallback ( ...args ) ;
889+ }
890+
891+ addCallbackOnce ( ...args ) {
892+ this . _callbacks . addCallbackOnce ( ...args ) ;
893+ }
894+
895+ runCallbacks ( ...args ) {
896+ if ( this . _isDestroyed ) {
897+ // warn -- should not run after destroyed
898+ return ;
899+ }
900+ this . _callbacks . runCallbacks ( ...args ) ;
901+ }
862902}
863903
864904mixin ( Editor , EventEmitter ) ;
0 commit comments