diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index ccafbf8..1383393 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -761,13 +761,38 @@ export class YCodeCell return JSONExt.deepCopy(this._youtputs.toArray()); } + createOutputs(outputs: Array): Array { + const newOutputs: Array = []; + 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 + const { text, ...outputWithoutText } = output; + _newOutput = outputWithoutText; + const newText = new Y.Array(); + newText.push(text as string[]); + _newOutput['text'] = newText; + } + else { + _newOutput = output; + } + for (const [key, value] of Object.entries(_newOutput)) { + newOutput.set(key, value); + } + newOutputs.push(newOutput); + } + return newOutputs; + } + /** * Replace all outputs. */ setOutputs(outputs: Array): void { this.transact(() => { this._youtputs.delete(0, this._youtputs.length); - this._youtputs.insert(0, outputs); + const newOutputs = this.createOutputs(outputs); + this._youtputs.insert(0, newOutputs); }, false); } @@ -789,7 +814,8 @@ export class YCodeCell end < this._youtputs.length ? end - start : this._youtputs.length - start; this.transact(() => { this._youtputs.delete(start, fin); - this._youtputs.insert(start, outputs); + const newOutputs = this.createOutputs(outputs); + this._youtputs.insert(start, newOutputs); }, false); } diff --git a/jupyter_ydoc/ynotebook.py b/jupyter_ydoc/ynotebook.py index 5166af1..89afdf6 100644 --- a/jupyter_ydoc/ynotebook.py +++ b/jupyter_ydoc/ynotebook.py @@ -157,7 +157,12 @@ def create_ycell(self, value: Dict[str, Any]) -> Map: if "attachments" in cell and not cell["attachments"]: del cell["attachments"] elif cell_type == "code": - cell["outputs"] = Array(cell.get("outputs", [])) + outputs = cell.get("outputs", []) + for idx, output in enumerate(outputs): + if output.get("output_type") == "stream": + output["text"] = Array(output.get("text", [])) + outputs[idx] = Map(output) + cell["outputs"] = Array(outputs) return Map(cell)