From c8603b3392e18f92aa6c0a60d38de43d70877838 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Wed, 21 Aug 2024 18:25:29 +0200 Subject: [PATCH 1/2] Add streamOutputChange --- javascript/src/api.ts | 4 ++++ javascript/src/ycell.ts | 44 +++++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/javascript/src/api.ts b/javascript/src/api.ts index 3eaf995..d929a46 100644 --- a/javascript/src/api.ts +++ b/javascript/src/api.ts @@ -746,6 +746,10 @@ export type CellChange = SourceChange & { * Cell output changes */ outputsChange?: Delta>; + /** + * Cell stream output text changes + */ + streamOutputChange?: Delta; /** * Cell execution count change */ diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index a4df55c..05ae3dc 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -827,24 +827,32 @@ export class YCodeCell /** * 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); + removeStreamOutput(index: number, start: number, origin: any = null): 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, + origin + ); } /** * Append text to a stream output. */ - appendStreamOutput(index: number, text: string): void { - this.transact(() => { - const output = this._youtputs.get(index); - const prevText = output.get('text') as Y.Text; - prevText.insert(prevText.length, text); - }, false); + appendStreamOutput(index: number, text: string, origin: any = null): void { + this.transact( + () => { + const output = this._youtputs.get(index); + const prevText = output.get('text') as Y.Text; + prevText.insert(prevText.length, text); + }, + false, + origin + ); } /** @@ -895,6 +903,16 @@ export class YCodeCell protected getChanges(events: Y.YEvent[]): Partial { const changes = super.getChanges(events); + const streamOutputEvent = events.find( + event => + event.path.length === 3 && + event.path[0] === 'outputs' && + event.path[2] === 'text' + ); + if (streamOutputEvent) { + changes.streamOutputChange = streamOutputEvent.changes.delta as any; + } + const outputEvent = events.find( event => event.target === this.ymodel.get('outputs') ); From ce0135f44ef594204a2e915f4d4c5dcb570c2203 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Thu, 22 Aug 2024 10:23:17 +0200 Subject: [PATCH 2/2] Review --- javascript/src/ycell.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index 05ae3dc..4bee750 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -904,6 +904,9 @@ export class YCodeCell const changes = super.getChanges(events); const streamOutputEvent = events.find( + // Changes to the 'text' of a cell's stream output can be accessed like so: + // ycell['outputs'][output_idx]['text'] + // This translates to an event path of: ['outputs', output_idx, 'text] event => event.path.length === 3 && event.path[0] === 'outputs' &&