Skip to content

Add streamOutputChange attribute to cell change object #264

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

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions javascript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ export type CellChange = SourceChange & {
* Cell output changes
*/
outputsChange?: Delta<Y.Map<any>>;
/**
* Cell stream output text changes
*/
streamOutputChange?: Delta<Y.Text>;
/**
* Cell execution count change
*/
Expand Down
44 changes: 31 additions & 13 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What are valid types/values for origin?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In Yjs an origin can be of any type.

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
);
}

/**
Expand Down Expand Up @@ -895,6 +903,16 @@ export class YCodeCell
protected getChanges(events: Y.YEvent<any>[]): Partial<CellChange> {
const changes = super.getChanges(events);

const streamOutputEvent = events.find(
event =>
event.path.length === 3 &&
event.path[0] === 'outputs' &&
event.path[2] === 'text'
Comment on lines +910 to +913
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's hard to understand this without some concrete example. I guess a comment or a test could help here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The event path is an array leading to the changed property, so to access the text property of a cell's stream output, one would do cell['outputs'][output_idx]['text'] and the event path would then be ['outputs', output_idx, 'text']. I'll add a comment 👍

);
if (streamOutputEvent) {
changes.streamOutputChange = streamOutputEvent.changes.delta as any;
}

const outputEvent = events.find(
event => event.target === this.ymodel.get('outputs')
);
Expand Down
Loading