@@ -358,15 +358,20 @@ export class OutputAreaModel implements IOutputAreaModel {
358
358
const curText = prev . streamText ! ;
359
359
const newText =
360
360
typeof value . text === 'string' ? value . text : value . text . join ( '' ) ;
361
- Private . addText ( curText , newText ) ;
361
+ this . _streamIndex = Private . addText ( this . _streamIndex , curText , newText ) ;
362
362
return this . length ;
363
363
}
364
364
365
365
if ( nbformat . isStream ( value ) ) {
366
366
if ( typeof value . text !== 'string' ) {
367
367
value . text = value . text . join ( '' ) ;
368
368
}
369
- value . text = Private . processText ( value . text ) ;
369
+ const { text, index } = Private . processText (
370
+ this . _streamIndex ,
371
+ value . text
372
+ ) ;
373
+ this . _streamIndex = index ;
374
+ value . text = text ;
370
375
}
371
376
372
377
// Create the new item.
@@ -480,6 +485,7 @@ export class OutputAreaModel implements IOutputAreaModel {
480
485
private _changed = new Signal < OutputAreaModel , IOutputAreaModel . ChangedArgs > (
481
486
this
482
487
) ;
488
+ private _streamIndex = 0 ;
483
489
}
484
490
485
491
/**
@@ -530,14 +536,20 @@ namespace Private {
530
536
/*
531
537
* Handle backspaces in `newText` and concatenates to `text`, if any.
532
538
*/
533
- export function processText ( newText : string , text ?: string ) : string {
539
+ export function processText (
540
+ index : number ,
541
+ newText : string ,
542
+ text ?: string
543
+ ) : { text : string ; index : number } {
534
544
if ( text === undefined ) {
535
545
text = '' ;
536
546
}
537
547
if ( ! ( newText . includes ( '\b' ) || newText . includes ( '\r' ) ) ) {
538
- return text + newText ;
548
+ text =
549
+ text . slice ( 0 , index ) + newText + text . slice ( index + newText . length ) ;
550
+ return { text, index : index + newText . length } ;
539
551
}
540
- let idx0 = text . length ;
552
+ let idx0 = index ;
541
553
let idx1 : number = - 1 ;
542
554
let lastEnd : number = 0 ;
543
555
const regex = / [ \n \b \r ] / ;
@@ -587,14 +599,18 @@ namespace Private {
587
599
throw Error ( `This should not happen` ) ;
588
600
}
589
601
}
590
- return text ;
602
+ return { text, index : idx0 } ;
591
603
}
592
604
593
605
/*
594
606
* Concatenate a string to an observable string, handling backspaces.
595
607
*/
596
- export function addText ( curText : IObservableString , newText : string ) : void {
597
- const text = processText ( newText , curText . text ) ;
608
+ export function addText (
609
+ prevIndex : number ,
610
+ curText : IObservableString ,
611
+ newText : string
612
+ ) : number {
613
+ const { text, index } = processText ( prevIndex , newText , curText . text ) ;
598
614
// Compute the difference between current text and new text.
599
615
let done = false ;
600
616
let idx = 0 ;
@@ -619,5 +635,6 @@ namespace Private {
619
635
idx ++ ;
620
636
}
621
637
}
638
+ return index ;
622
639
}
623
640
}
0 commit comments