diff --git a/javascript/src/api.ts b/javascript/src/api.ts index 9a44536..16b6f38 100644 --- a/javascript/src/api.ts +++ b/javascript/src/api.ts @@ -572,6 +572,11 @@ export interface ISharedCodeCell outputs: Array ): void; + /** + * Clear all outputs from the cell. + */ + clearOutputs(origin: any): void; + /** * Serialize the model to JSON. */ diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index e5a23ec..81c915f 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -867,6 +867,19 @@ export class YCodeCell ); } + /** + * Clear all outputs from the cell. + */ + clearOutputs(origin: any = null): void { + this.transact( + () => { + this._youtputs.delete(0, this._youtputs.length); + }, + false, + origin + ); + } + /** * Serialize the model to JSON. */ diff --git a/javascript/test/ycell.spec.ts b/javascript/test/ycell.spec.ts index 3f63825..4cfbdf0 100644 --- a/javascript/test/ycell.spec.ts +++ b/javascript/test/ycell.spec.ts @@ -333,3 +333,22 @@ describe('@jupyter/ydoc', () => { }); }); }); + +test('should clear outputs from a code cell', () => { + const codeCell = YCodeCell.create(); + const outputs = [ + { + data: { + 'text/plain': ['Hello, world!'] + }, + metadata: {}, + output_type: 'execute_result' + } + ]; + + codeCell.setOutputs(outputs); + expect(codeCell.getOutputs()).toEqual(outputs); + + codeCell.clearOutputs(); + expect(codeCell.getOutputs()).toEqual([]); +});