Skip to content

Commit 137ca8e

Browse files
authored
Merge pull request #241 from davidbrochart/stream-output
Add appendStreamOutput and removeStreamOutput methods
2 parents 8d7daaf + 3f461ac commit 137ca8e

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

javascript/src/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
} from '@lumino/coreutils';
2323
import type { IObservableDisposable } from '@lumino/disposable';
2424
import type { ISignal } from '@lumino/signaling';
25+
import * as Y from 'yjs';
2526

2627
/**
2728
* Changes on Sequence-like data are expressed as Quill-inspired deltas.
@@ -737,7 +738,7 @@ export type CellChange = SourceChange & {
737738
/**
738739
* Cell output changes
739740
*/
740-
outputsChange?: Delta<nbformat.IOutput[]>;
741+
outputsChange?: Delta<Y.Map<any>>;
741742
/**
742743
* Cell execution count change
743744
*/

javascript/src/ycell.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,17 +764,22 @@ export class YCodeCell
764764
return JSONExt.deepCopy(this._youtputs.toJSON());
765765
}
766766

767-
createOutputs(outputs: Array<nbformat.IOutput>): Array<any> {
768-
const newOutputs: Array<any> = [];
767+
createOutputs(outputs: Array<nbformat.IOutput>): Array<Y.Map<any>> {
768+
const newOutputs: Array<Y.Map<any>> = [];
769769
for (const output of outputs) {
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;
@@ -798,6 +803,29 @@ export class YCodeCell
798803
}, false);
799804
}
800805

806+
/**
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.
820+
*/
821+
appendStreamOutput(index: number, text: string): void {
822+
this.transact(() => {
823+
const output = this._youtputs.get(index);
824+
const prevText = output.get('text') as Y.Text;
825+
prevText.insert(prevText.length, text);
826+
}, false);
827+
}
828+
801829
/**
802830
* Replace content from `start' to `end` with `outputs`.
803831
*
@@ -863,7 +891,7 @@ export class YCodeCell
863891
return changes;
864892
}
865893

866-
private _youtputs: Y.Array<nbformat.IOutput>;
894+
private _youtputs: Y.Array<Y.Map<any>>;
867895
}
868896

869897
class YAttachmentCell

0 commit comments

Comments
 (0)