Skip to content

Commit e97181c

Browse files
Add streamOutputChange attribute to cell change object (#264)
* Add streamOutputChange * Review
1 parent 06dc63d commit e97181c

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

javascript/src/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,10 @@ export type CellChange = SourceChange & {
746746
* Cell output changes
747747
*/
748748
outputsChange?: Delta<Y.Map<any>>;
749+
/**
750+
* Cell stream output text changes
751+
*/
752+
streamOutputChange?: Delta<Y.Text>;
749753
/**
750754
* Cell execution count change
751755
*/

javascript/src/ycell.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -827,24 +827,32 @@ export class YCodeCell
827827
/**
828828
* Remove text from a stream output.
829829
*/
830-
removeStreamOutput(index: number, start: number): void {
831-
this.transact(() => {
832-
const output = this._youtputs.get(index);
833-
const prevText = output.get('text') as Y.Text;
834-
const length = prevText.length - start;
835-
prevText.delete(start, length);
836-
}, false);
830+
removeStreamOutput(index: number, start: number, origin: any = null): void {
831+
this.transact(
832+
() => {
833+
const output = this._youtputs.get(index);
834+
const prevText = output.get('text') as Y.Text;
835+
const length = prevText.length - start;
836+
prevText.delete(start, length);
837+
},
838+
false,
839+
origin
840+
);
837841
}
838842

839843
/**
840844
* Append text to a stream output.
841845
*/
842-
appendStreamOutput(index: number, text: string): void {
843-
this.transact(() => {
844-
const output = this._youtputs.get(index);
845-
const prevText = output.get('text') as Y.Text;
846-
prevText.insert(prevText.length, text);
847-
}, false);
846+
appendStreamOutput(index: number, text: string, origin: any = null): void {
847+
this.transact(
848+
() => {
849+
const output = this._youtputs.get(index);
850+
const prevText = output.get('text') as Y.Text;
851+
prevText.insert(prevText.length, text);
852+
},
853+
false,
854+
origin
855+
);
848856
}
849857

850858
/**
@@ -895,6 +903,19 @@ export class YCodeCell
895903
protected getChanges(events: Y.YEvent<any>[]): Partial<CellChange> {
896904
const changes = super.getChanges(events);
897905

906+
const streamOutputEvent = events.find(
907+
// Changes to the 'text' of a cell's stream output can be accessed like so:
908+
// ycell['outputs'][output_idx]['text']
909+
// This translates to an event path of: ['outputs', output_idx, 'text]
910+
event =>
911+
event.path.length === 3 &&
912+
event.path[0] === 'outputs' &&
913+
event.path[2] === 'text'
914+
);
915+
if (streamOutputEvent) {
916+
changes.streamOutputChange = streamOutputEvent.changes.delta as any;
917+
}
918+
898919
const outputEvent = events.find(
899920
event => event.target === this.ymodel.get('outputs')
900921
);

0 commit comments

Comments
 (0)