mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-30 21:41:46 +01:00
Unify code highlight
This commit is contained in:
@@ -1145,8 +1145,7 @@ async function webviewPreloads(ctx: PreloadContext) {
|
|||||||
}
|
}
|
||||||
case 'tokenizedCodeBlock': {
|
case 'tokenizedCodeBlock': {
|
||||||
const { codeBlockId, html } = event.data;
|
const { codeBlockId, html } = event.data;
|
||||||
MarkupCell.highlightCodeBlock(codeBlockId, html);
|
MarkdownCodeBlock.highlightCodeBlock(codeBlockId, html);
|
||||||
OutputElement.highlightCodeBlock(codeBlockId, html);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'tokenizedStylesChanged': {
|
case 'tokenizedStylesChanged': {
|
||||||
@@ -1563,12 +1562,11 @@ async function webviewPreloads(ctx: PreloadContext) {
|
|||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
|
|
||||||
class MarkupCell {
|
class MarkdownCodeBlock {
|
||||||
|
|
||||||
private static pendingCodeBlocksToHighlight = new Map<string, HTMLElement>();
|
private static pendingCodeBlocksToHighlight = new Map<string, HTMLElement>();
|
||||||
|
|
||||||
public static highlightCodeBlock(id: string, html: string) {
|
public static highlightCodeBlock(id: string, html: string) {
|
||||||
const el = MarkupCell.pendingCodeBlocksToHighlight.get(id);
|
const el = MarkdownCodeBlock.pendingCodeBlocksToHighlight.get(id);
|
||||||
if (!el) {
|
if (!el) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1579,6 +1577,24 @@ async function webviewPreloads(ctx: PreloadContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static requestHighlightCodeBlock(root: HTMLElement | ShadowRoot) {
|
||||||
|
const codeBlocks: Array<{ value: string; lang: string; id: string }> = [];
|
||||||
|
let i = 0;
|
||||||
|
for (const el of root.querySelectorAll('.vscode-code-block')) {
|
||||||
|
const lang = el.getAttribute('data-vscode-code-block-lang');
|
||||||
|
if (el.textContent && lang) {
|
||||||
|
const id = `${Date.now()}-${i++}`;
|
||||||
|
codeBlocks.push({ value: el.textContent, lang: lang, id });
|
||||||
|
MarkdownCodeBlock.pendingCodeBlocksToHighlight.set(id, el as HTMLElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return codeBlocks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MarkupCell {
|
||||||
|
|
||||||
public readonly ready: Promise<void>;
|
public readonly ready: Promise<void>;
|
||||||
|
|
||||||
public readonly id: string;
|
public readonly id: string;
|
||||||
@@ -1713,16 +1729,7 @@ async function webviewPreloads(ctx: PreloadContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const codeBlocks: Array<{ value: string; lang: string; id: string }> = [];
|
const codeBlocks: Array<{ value: string; lang: string; id: string }> = MarkdownCodeBlock.requestHighlightCodeBlock(root);
|
||||||
let i = 0;
|
|
||||||
for (const el of root.querySelectorAll('.vscode-code-block')) {
|
|
||||||
const lang = el.getAttribute('data-vscode-code-block-lang');
|
|
||||||
if (el.textContent && lang) {
|
|
||||||
const id = `${Date.now()}-${i++}`;
|
|
||||||
codeBlocks.push({ value: el.textContent, lang: lang, id });
|
|
||||||
MarkupCell.pendingCodeBlocksToHighlight.set(id, el as HTMLElement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
postNotebookMessage<webviewMessages.IRenderedMarkupMessage>('renderedMarkup', {
|
postNotebookMessage<webviewMessages.IRenderedMarkupMessage>('renderedMarkup', {
|
||||||
cellId: this.id,
|
cellId: this.id,
|
||||||
@@ -1926,20 +1933,6 @@ async function webviewPreloads(ctx: PreloadContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class OutputElement {
|
class OutputElement {
|
||||||
private static pendingCodeBlocksToHighlight = new Map<string, HTMLElement>();
|
|
||||||
|
|
||||||
public static highlightCodeBlock(id: string, html: string) {
|
|
||||||
const el = OutputElement.pendingCodeBlocksToHighlight.get(id);
|
|
||||||
if (!el) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const trustedHtml = ttPolicy?.createHTML(html) ?? html;
|
|
||||||
el.innerHTML = trustedHtml as string;
|
|
||||||
if (tokenizationStyleElement) {
|
|
||||||
el.insertAdjacentElement('beforebegin', tokenizationStyleElement.cloneNode(true) as HTMLElement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly element: HTMLElement;
|
public readonly element: HTMLElement;
|
||||||
|
|
||||||
private _content?: { content: webviewMessages.ICreationContent; preloadsAndErrors: unknown[] };
|
private _content?: { content: webviewMessages.ICreationContent; preloadsAndErrors: unknown[] };
|
||||||
@@ -2002,17 +1995,8 @@ async function webviewPreloads(ctx: PreloadContext) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const codeBlocks: Array<{ value: string; lang: string; id: string }> = [];
|
|
||||||
let i = 0;
|
|
||||||
const root = this.element.shadowRoot ?? this.element;
|
const root = this.element.shadowRoot ?? this.element;
|
||||||
for (const el of root.querySelectorAll('.vscode-code-block')) {
|
const codeBlocks: Array<{ value: string; lang: string; id: string }> = MarkdownCodeBlock.requestHighlightCodeBlock(root);
|
||||||
const lang = el.getAttribute('data-vscode-code-block-lang');
|
|
||||||
if (el.textContent && lang) {
|
|
||||||
const id = `${Date.now()}-${i++}`;
|
|
||||||
codeBlocks.push({ value: el.textContent, lang: lang, id });
|
|
||||||
OutputElement.pendingCodeBlocksToHighlight.set(id, el as HTMLElement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (codeBlocks.length > 0) {
|
if (codeBlocks.length > 0) {
|
||||||
postNotebookMessage<webviewMessages.IRenderedCellOutputMessage>('renderedCellOutput', {
|
postNotebookMessage<webviewMessages.IRenderedCellOutputMessage>('renderedCellOutput', {
|
||||||
|
|||||||
Reference in New Issue
Block a user