Skip to content

Commit 76e4073

Browse files
committed
Change text entry from YArray to Y.Text
1 parent 527be8c commit 76e4073

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

javascript/src/ycell.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -770,11 +770,16 @@ export class YCodeCell
770770
let _newOutput: { [id: string]: any };
771771
const newOutput = new Y.Map();
772772
if (output.output_type === 'stream') {
773-
// Set the text field as a Y.Array
773+
// Set the text field as a Y.Text
774774
const { text, ...outputWithoutText } = output;
775775
_newOutput = outputWithoutText;
776-
const newText = new Y.Array();
777-
newText.push(text as string[]);
776+
const newText = new Y.Text();
777+
let length = 0;
778+
// text is a list of strings
779+
for (const str of text as string[]) {
780+
newText.insert(length, str);
781+
length += str.length;
782+
}
778783
_newOutput['text'] = newText;
779784
} else {
780785
_newOutput = output;
@@ -799,13 +804,25 @@ export class YCodeCell
799804
}
800805

801806
/**
802-
* Push text to a stream output.
807+
* Remove text from a stream output.
808+
*/
809+
removeStreamOutput(index: number, start: number): void {
810+
this.transact(() => {
811+
const output = this._youtputs.get(index);
812+
const prevText = output.get('text') as Y.Text;
813+
const length = prevText.length - start;
814+
prevText.delete(start, length);
815+
}, false);
816+
}
817+
818+
/**
819+
* Append text to a stream output.
803820
*/
804-
pushStreamOutput(index: number, text: string): void {
821+
appendStreamOutput(index: number, text: string): void {
805822
this.transact(() => {
806823
const output = this._youtputs.get(index);
807-
const prevText = output.get('text') as Y.Array<string>;
808-
prevText.push([text]);
824+
const prevText = output.get('text') as Y.Text;
825+
prevText.insert(prevText.length, text);
809826
}, false);
810827
}
811828

0 commit comments

Comments
 (0)