Skip to content

Commit

Permalink
Also freeze markup cell output items
Browse files Browse the repository at this point in the history
This ensures we don't hand out internal impl details
  • Loading branch information
mjbvz committed Feb 9, 2022
1 parent dbb563c commit e155fca
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ async function webviewPreloads(ctx: PreloadContext) {
}
}();

class MarkupCell implements rendererApi.OutputItem {
class MarkupCell {

private static pendingCodeBlocksToHighlight = new Map<string, HTMLElement>();

Expand All @@ -1574,19 +1574,50 @@ async function webviewPreloads(ctx: PreloadContext) {

public readonly ready: Promise<void>;

public readonly id: string;
public readonly element: HTMLElement;

private readonly outputItem: rendererApi.OutputItem;

/// Internal field that holds text content
private _content: { readonly value: string; readonly version: number };

constructor(id: string, mime: string, content: string, top: number) {
this.id = id;
this.mime = mime;
this._content = { value: content, version: 0 };

let resolveReady: () => void;
this.ready = new Promise<void>(r => resolveReady = r);

let cachedData: { readonly version: number; readonly value: Uint8Array } | undefined;
this.outputItem = Object.freeze(<rendererApi.OutputItem>{
id,
mime,
metadata: undefined,

text: (): string => {
return this._content.value;
},

json: () => {
return undefined;
},

data: (): Uint8Array => {
if (cachedData?.version === this._content.version) {
return cachedData.value;
}

const data = textEncoder.encode(this._content.value);
cachedData = { version: this._content.version, value: data };
return data;
},

blob(): Blob {
return new Blob([this.data()], { type: this.mime });
}
});

const root = document.getElementById('container')!;
const markupCell = document.createElement('div');
markupCell.className = 'markup';
Expand All @@ -1610,31 +1641,6 @@ async function webviewPreloads(ctx: PreloadContext) {
});
}

//#region IOutputItem
public readonly id: string;
public readonly mime: string;
public readonly metadata = undefined;

text(): string { return this._content.value; }

json() { return undefined; }

bytes(): Uint8Array { return this.data(); }

#cachedData?: { readonly version: number; readonly value: Uint8Array };
data(): Uint8Array {
if (this.#cachedData?.version === this._content.version) {
return this.#cachedData.value;
}

const data = textEncoder.encode(this._content.value);
this.#cachedData = { version: this._content.version, value: data };
return data;
}

blob() { return new Blob([this.data()], { type: this.mime }); }
//#endregion

private addEventListeners() {
this.element.addEventListener('dblclick', () => {
postNotebookMessage<webviewMessages.IToggleMarkupPreviewMessage>('toggleMarkupPreview', { cellId: this.id });
Expand Down Expand Up @@ -1682,7 +1688,7 @@ async function webviewPreloads(ctx: PreloadContext) {
public async updateContentAndRender(newContent: string): Promise<void> {
this._content = { value: newContent, version: this._content.version + 1 };

await renderers.render(this, this.element);
await renderers.render(this.outputItem, this.element);

const root = (this.element.shadowRoot ?? this.element);
const html = [];
Expand Down

0 comments on commit e155fca

Please sign in to comment.