Skip to content

Commit

Permalink
Merge pull request #241 from davidbrochart/stream-output
Browse files Browse the repository at this point in the history
Add appendStreamOutput and removeStreamOutput methods
  • Loading branch information
Zsailer authored Jun 24, 2024
2 parents 8d7daaf + 3f461ac commit 137ca8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
3 changes: 2 additions & 1 deletion javascript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
} from '@lumino/coreutils';
import type { IObservableDisposable } from '@lumino/disposable';
import type { ISignal } from '@lumino/signaling';
import * as Y from 'yjs';

/**
* Changes on Sequence-like data are expressed as Quill-inspired deltas.
Expand Down Expand Up @@ -737,7 +738,7 @@ export type CellChange = SourceChange & {
/**
* Cell output changes
*/
outputsChange?: Delta<nbformat.IOutput[]>;
outputsChange?: Delta<Y.Map<any>>;
/**
* Cell execution count change
*/
Expand Down
40 changes: 34 additions & 6 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,17 +764,22 @@ export class YCodeCell
return JSONExt.deepCopy(this._youtputs.toJSON());
}

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

/**
* 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.
*/
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);
}

/**
* Replace content from `start' to `end` with `outputs`.
*
Expand Down Expand Up @@ -863,7 +891,7 @@ export class YCodeCell
return changes;
}

private _youtputs: Y.Array<nbformat.IOutput>;
private _youtputs: Y.Array<Y.Map<any>>;
}

class YAttachmentCell
Expand Down

0 comments on commit 137ca8e

Please sign in to comment.