@@ -91,16 +91,18 @@ export default class EditorModel {
9191 }
9292
9393 public clone ( ) : EditorModel {
94- const clonedParts = this . parts . map ( ( p ) => this . partCreator . deserializePart ( p . serialize ( ) ) ) ;
94+ const clonedParts = this . parts
95+ . map ( ( p ) => this . partCreator . deserializePart ( p . serialize ( ) ) )
96+ . filter ( ( p ) : p is Part => Boolean ( p ) ) ;
9597 return new EditorModel ( clonedParts , this . _partCreator , this . updateCallback ) ;
9698 }
9799
98100 private insertPart ( index : number , part : Part ) : void {
99101 this . _parts . splice ( index , 0 , part ) ;
100- if ( this . activePartIdx >= index ) {
102+ if ( this . activePartIdx && this . activePartIdx >= index ) {
101103 ++ this . activePartIdx ;
102104 }
103- if ( this . autoCompletePartIdx >= index ) {
105+ if ( this . autoCompletePartIdx && this . autoCompletePartIdx >= index ) {
104106 ++ this . autoCompletePartIdx ;
105107 }
106108 }
@@ -109,12 +111,12 @@ export default class EditorModel {
109111 this . _parts . splice ( index , 1 ) ;
110112 if ( index === this . activePartIdx ) {
111113 this . activePartIdx = null ;
112- } else if ( this . activePartIdx > index ) {
114+ } else if ( this . activePartIdx && this . activePartIdx > index ) {
113115 -- this . activePartIdx ;
114116 }
115117 if ( index === this . autoCompletePartIdx ) {
116118 this . autoCompletePartIdx = null ;
117- } else if ( this . autoCompletePartIdx > index ) {
119+ } else if ( this . autoCompletePartIdx && this . autoCompletePartIdx > index ) {
118120 -- this . autoCompletePartIdx ;
119121 }
120122 }
@@ -160,7 +162,9 @@ export default class EditorModel {
160162 }
161163
162164 public reset ( serializedParts : SerializedPart [ ] , caret ?: Caret , inputType ?: string ) : void {
163- this . _parts = serializedParts . map ( ( p ) => this . _partCreator . deserializePart ( p ) ) ;
165+ this . _parts = serializedParts
166+ . map ( ( p ) => this . _partCreator . deserializePart ( p ) )
167+ . filter ( ( p ) : p is Part => Boolean ( p ) ) ;
164168 if ( ! caret ) {
165169 caret = this . getPositionAtEnd ( ) ;
166170 }
@@ -194,7 +198,7 @@ export default class EditorModel {
194198
195199 public update ( newValue : string , inputType : string , caret : DocumentOffset ) : Promise < void > {
196200 const diff = this . diff ( newValue , inputType , caret ) ;
197- const position = this . positionForOffset ( diff . at , caret . atNodeEnd ) ;
201+ const position = this . positionForOffset ( diff . at || 0 , caret . atNodeEnd ) ;
198202 let removedOffsetDecrease = 0 ;
199203 if ( diff . removed ) {
200204 removedOffsetDecrease = this . removeText ( position , diff . removed . length ) ;
@@ -204,7 +208,7 @@ export default class EditorModel {
204208 addedLen = this . addText ( position , diff . added , inputType ) ;
205209 }
206210 this . mergeAdjacentParts ( ) ;
207- const caretOffset = diff . at - removedOffsetDecrease + addedLen ;
211+ const caretOffset = ( diff . at || 0 ) - removedOffsetDecrease + addedLen ;
208212 let newPosition = this . positionForOffset ( caretOffset , true ) ;
209213 const canOpenAutoComplete = inputType !== "insertFromPaste" && inputType !== "insertFromDrop" ;
210214 const acPromise = this . setActivePart ( newPosition , canOpenAutoComplete ) ;
@@ -254,10 +258,11 @@ export default class EditorModel {
254258 private onAutoComplete = ( { replaceParts, close } : ICallback ) : void => {
255259 let pos : DocumentPosition | undefined ;
256260 if ( replaceParts ) {
257- this . _parts . splice ( this . autoCompletePartIdx , this . autoCompletePartCount , ...replaceParts ) ;
261+ const autoCompletePartIdx = this . autoCompletePartIdx || 0 ;
262+ this . _parts . splice ( autoCompletePartIdx , this . autoCompletePartCount , ...replaceParts ) ;
258263 this . autoCompletePartCount = replaceParts . length ;
259264 const lastPart = replaceParts [ replaceParts . length - 1 ] ;
260- const lastPartIndex = this . autoCompletePartIdx + replaceParts . length - 1 ;
265+ const lastPartIndex = autoCompletePartIdx + replaceParts . length - 1 ;
261266 pos = new DocumentPosition ( lastPartIndex , lastPart . text . length ) ;
262267 }
263268 if ( close ) {
@@ -360,10 +365,13 @@ export default class EditorModel {
360365 const { offset } = pos ;
361366 let addLen = str . length ;
362367 const part = this . _parts [ index ] ;
368+
369+ let it : string | undefined = str ;
370+
363371 if ( part ) {
364372 if ( part . canEdit ) {
365373 if ( part . validateAndInsert ( offset , str , inputType ) ) {
366- str = null ;
374+ it = undefined ;
367375 } else {
368376 const splitPart = part . split ( offset ) ;
369377 index += 1 ;
@@ -381,7 +389,6 @@ export default class EditorModel {
381389 index = 0 ;
382390 }
383391
384- let it : string | undefined = str ;
385392 while ( it ) {
386393 const newPart = this . _partCreator . createPartForInput ( it , index , inputType ) ;
387394 const oldStr = it ;
0 commit comments