Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add appendStreamOutput and removeStreamOutput methods #241

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should improve typing here, any is not a great public API.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #269

/**
* 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
Loading