Skip to content

Commit 4c2df56

Browse files
committed
append html
1 parent c5511d5 commit 4c2df56

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

extensions/notebook-renderers/src/index.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import type { ActivationFunction, OutputItem, RendererContext } from 'vscode-notebook-renderer';
7-
import { createOutputContent, scrollableClass } from './textHelper';
7+
import { appendOutput, createOutputContent, scrollableClass } from './textHelper';
88
import { HtmlRenderingHook, IDisposable, IRichRenderContext, JavaScriptRenderingHook, RenderOptions } from './rendererTypes';
99
import { ttPolicy } from './htmlHelper';
1010

@@ -172,7 +172,7 @@ function renderError(
172172
outputElement.classList.add('traceback');
173173

174174
const outputScrolling = scrollingEnabled(outputInfo, ctx.settings);
175-
const content = createOutputContent(outputInfo.id, [err.stack ?? ''], ctx.settings.lineLimit, outputScrolling, trustHTML);
175+
const content = createOutputContent(outputInfo.id, err.stack ?? '', ctx.settings.lineLimit, outputScrolling, trustHTML);
176176
const contentParent = document.createElement('div');
177177
contentParent.classList.toggle('word-wrap', ctx.settings.outputWordWrap);
178178
disposableStore.push(ctx.onDidChangeSettings(e => {
@@ -276,26 +276,19 @@ interface OutputWithAppend extends OutputItem {
276276
// div output-item-id="{guid}" <-- content from outputItem parameter
277277
function renderStream(outputInfo: OutputWithAppend, outputElement: HTMLElement, error: boolean, ctx: IRichRenderContext): IDisposable {
278278
const appendedText = outputInfo.appendedText?.();
279-
if (appendedText) {
280-
console.log(`appending output version ${appendedText}`);
281-
}
282279
const disposableStore = createDisposableStore();
283280
const outputScrolling = scrollingEnabled(outputInfo, ctx.settings);
284281

285282
outputElement.classList.add('output-stream');
286283

287-
const text = outputInfo.text();
288-
const newContent = createOutputContent(outputInfo.id, [text], ctx.settings.lineLimit, outputScrolling, false);
289-
newContent.setAttribute('output-item-id', outputInfo.id);
290-
if (error) {
291-
newContent.classList.add('error');
292-
}
284+
293285

294286
const scrollTop = outputScrolling ? findScrolledHeight(outputElement) : undefined;
295287

296288
const previousOutputParent = getPreviousMatchingContentGroup(outputElement);
297289
// If the previous output item for the same cell was also a stream, append this output to the previous
298290
if (previousOutputParent) {
291+
const newContent = createContent(outputInfo, ctx, outputScrolling, error);
299292
const existingContent = previousOutputParent.querySelector(`[output-item-id="${outputInfo.id}"]`) as HTMLElement | null;
300293
if (existingContent) {
301294
existingContent.replaceWith(newContent);
@@ -309,15 +302,19 @@ function renderStream(outputInfo: OutputWithAppend, outputElement: HTMLElement,
309302
const existingContent = outputElement.querySelector(`[output-item-id="${outputInfo.id}"]`) as HTMLElement | null;
310303
let contentParent = existingContent?.parentElement;
311304
if (existingContent && contentParent) {
312-
if (appendedText){
313-
existingContent
305+
if (appendedText) {
306+
appendOutput(existingContent, outputInfo.id, appendedText, ctx.settings.lineLimit, outputScrolling, false);
314307
}
315-
existingContent.replaceWith(newContent);
316-
while (newContent.nextSibling) {
317-
// clear out any stale content if we had previously combined streaming outputs into this one
318-
newContent.nextSibling.remove();
308+
else {
309+
const newContent = createContent(outputInfo, ctx, outputScrolling, error);
310+
existingContent.replaceWith(newContent);
311+
while (newContent.nextSibling) {
312+
// clear out any stale content if we had previously combined streaming outputs into this one
313+
newContent.nextSibling.remove();
314+
}
319315
}
320316
} else {
317+
const newContent = createContent(outputInfo, ctx, outputScrolling, error);
321318
contentParent = document.createElement('div');
322319
contentParent.appendChild(newContent);
323320
while (outputElement.firstChild) {
@@ -338,13 +335,23 @@ function renderStream(outputInfo: OutputWithAppend, outputElement: HTMLElement,
338335
return disposableStore;
339336
}
340337

338+
function createContent(outputInfo: OutputWithAppend, ctx: IRichRenderContext, outputScrolling: boolean, error: boolean) {
339+
const text = outputInfo.text();
340+
const newContent = createOutputContent(outputInfo.id, text, ctx.settings.lineLimit, outputScrolling, false);
341+
newContent.setAttribute('output-item-id', outputInfo.id);
342+
if (error) {
343+
newContent.classList.add('error');
344+
}
345+
return newContent;
346+
}
347+
341348
function renderText(outputInfo: OutputItem, outputElement: HTMLElement, ctx: IRichRenderContext): IDisposable {
342349
const disposableStore = createDisposableStore();
343350
clearContainer(outputElement);
344351

345352
const text = outputInfo.text();
346353
const outputScrolling = scrollingEnabled(outputInfo, ctx.settings);
347-
const content = createOutputContent(outputInfo.id, [text], ctx.settings.lineLimit, outputScrolling, false);
354+
const content = createOutputContent(outputInfo.id, text, ctx.settings.lineLimit, outputScrolling, false);
348355
content.classList.add('output-plaintext');
349356
if (ctx.settings.outputWordWrap) {
350357
content.classList.add('word-wrap');

extensions/notebook-renderers/src/textHelper.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,43 @@ function scrollableArrayOfString(id: string, buffer: string[], trustHtml: boolea
100100
return element;
101101
}
102102

103-
export function createOutputContent(id: string, outputs: string[], linesLimit: number, scrollable: boolean, trustHtml: boolean): HTMLElement {
103+
export function createOutputContent(id: string, outputText: string, linesLimit: number, scrollable: boolean, trustHtml: boolean): HTMLElement {
104104

105-
const buffer = outputs.join('\n').split(/\r\n|\r|\n/g);
105+
const buffer = outputText.split(/\r\n|\r|\n/g);
106106

107107
if (scrollable) {
108108
return scrollableArrayOfString(id, buffer, trustHtml);
109109
} else {
110110
return truncatedArrayOfString(id, buffer, linesLimit, trustHtml);
111111
}
112112
}
113+
114+
export function appendOutput(element: HTMLElement, id: string, outputText: string, linesLimit: number, scrollable: boolean, trustHtml: boolean) {
115+
const buffer = outputText.split(/\r\n|\r|\n/g);
116+
let newContent: HTMLDivElement;
117+
if (scrollable) {
118+
newContent = scrollableArrayOfString(id, buffer, trustHtml);
119+
element.appendChild(newContent);
120+
// contains 1 span per line
121+
// const innerContainer = element.childNodes[0].childNodes[0];
122+
// const linesToAdd = newContent.childNodes[0].childNodes[0].childNodes;
123+
124+
// linesToAdd.forEach(span => {
125+
// innerContainer.appendChild(span);
126+
// while (innerContainer.childNodes.length > 5000) {
127+
// innerContainer.firstChild?.remove();
128+
// }
129+
// });
130+
} else {
131+
newContent = truncatedArrayOfString(id, buffer, linesLimit, trustHtml);
132+
133+
// contains 1 span per line
134+
const innerContainer = element.childNodes[0].childNodes[0];
135+
const linesToAdd = newContent.childNodes[0].childNodes[0].childNodes;
136+
137+
linesToAdd.forEach(span => {
138+
innerContainer.appendChild(span);
139+
140+
});
141+
}
142+
}

0 commit comments

Comments
 (0)