From ef3ba499c9a6d06b62ebcb9b22b6f7ced41601e9 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Thu, 20 Jun 2024 11:42:57 +0200 Subject: [PATCH] Change text entry from YArray to Y.Text --- javascript/src/ycell.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index 3b90a66..9257342 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -770,11 +770,16 @@ export class YCodeCell let _newOutput: { [id: string]: any }; const newOutput = new Y.Map(); if (output.output_type === 'stream') { - // Set the text field as a Y.Array + // Set the text field as a Y.Text const { text, ...outputWithoutText } = output; _newOutput = outputWithoutText; - const newText = new Y.Array(); - newText.push(text as string[]); + const newText = new Y.Text(); + var length = 0; + // text is a list of strings + for (const str of text as string[]) { + newText.insert(length, str); + length += str.length; + } _newOutput['text'] = newText; } else { _newOutput = output; @@ -799,13 +804,25 @@ export class YCodeCell } /** - * Push text to a stream output. + * Remove text from a stream output. + */ + removeStreamOutput(index: number, start: number): void { + this.transact(() => { + const output = this._youtputs.get(index); + const prevText = output.get('text') as Y.Text; + const length = prevText.length - start; + prevText.delete(start, length); + }, false); + } + + /** + * Append text to a stream output. */ - pushStreamOutput(index: number, text: string): void { + appendStreamOutput(index: number, text: string): void { this.transact(() => { const output = this._youtputs.get(index); - const prevText = output.get('text') as Y.Array; - prevText.push([text]); + const prevText = output.get('text') as Y.Text; + prevText.insert(prevText.length, text); }, false); }