Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(text): paste code copied from VS Code as code in Firefox too
Elian Doran06612ed15310
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.htmlindex 4ae97532f1..4b18355d51 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.html@@ -29,8 +29,8 @@<li>If that language is not in the list of languages, the code block uses <em>Auto-detected</em>.</li><li>The colors of the Visual Studio Code theme are removed.</li><li>A single line becomes inline code instead of a code block.</li>- <li>This works in the desktop application and in Chromium-based browsers (Chrome,- Edge), but not in Firefox.</li>+ <li>In Firefox, the code block always uses <em>Auto-detected</em>. Firefox+ can't read the language from Visual Studio Code.</li></ul></li></ul>diff --git a/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md b/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.mdindex 5894abf547..9a25c6f8a2 100644--- a/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md+++ b/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md@@ -17,7 +17,7 @@ Note that this feature is meant for generally small snippets of code. For larger* If that language is not in the list of languages, the code block uses _Auto-detected_.* The colors of the Visual Studio Code theme are removed.* A single line becomes inline code instead of a code block.- * This works in the desktop application and in Chromium-based browsers (Chrome, Edge), but not in Firefox.+ * In Firefox, the code block always uses _Auto-detected_. Firefox can't read the language from Visual Studio Code.## Exiting out of the code blockdiff --git a/packages/ckeditor5/src/plugins/paste_from_vscode.spec.ts b/packages/ckeditor5/src/plugins/paste_from_vscode.spec.tsindex 384f97e5bf..85a99fb73f 100644--- a/packages/ckeditor5/src/plugins/paste_from_vscode.spec.ts+++ b/packages/ckeditor5/src/plugins/paste_from_vscode.spec.ts@@ -2,7 +2,7 @@ import { ClassicEditor, Code, CodeBlock, Essentials, Paragraph, _getModelData, _import { beforeEach, describe, expect, it } from "vitest";import { createTestEditor } from "../../test/editor-kit.js";-import PasteFromVsCode, { codeBlockLanguage, VSCODE_EDITOR_DATA, vsCodePasteHtml } from "./paste_from_vscode.js";+import PasteFromVsCode, { codeBlockLanguage, isVsCodeHtml, VSCODE_EDITOR_DATA, vsCodePasteHtml } from "./paste_from_vscode.js";/** A CSS rule copied out of VS Code under a dark theme: the clipboard's HTML, verbatim. */const VSCODE_HTML = `<meta charset='utf-8'><div style="color: #bbbebf;background-color: #121314;font-family: Menlo, Monaco, 'Courier New', monospace;font-weight: normal;font-size: 12px;line-height: 18px;white-space: pre;"><div><span style="color: #d7ba7d;">#trilium-error-overlay</span><span style="color: #bbbebf;"> </span><span style="color: #d7ba7d;">.tn-eo-title</span><span style="color: #bbbebf;"> {</span></div><div><span style="color: #bbbebf;"> </span><span style="color: #9cdcfe;">margin</span><span style="color: #bbbebf;">: </span><span style="color: #b5cea8;">0</span><span style="color: #bbbebf;">;</span></div><div><span style="color: #bbbebf;"> </span><span style="color: #9cdcfe;">font-size</span><span style="color: #bbbebf;">: </span><span style="color: #b5cea8;">1.4em</span><span style="color: #bbbebf;">;</span></div><div><span style="color: #bbbebf;"> </span><span style="color: #9cdcfe;">font-weight</span><span style="color: #bbbebf;">: </span><span style="color: #b5cea8;">600</span><span style="color: #bbbebf;">;</span></div><div><span style="color: #bbbebf;">}</span></div></div>`;@@ -45,6 +45,23 @@ describe("PasteFromVsCode", () => {});});+ describe("isVsCodeHtml", () => {+ it("recognizes the HTML VS Code writes", () => {+ expect(isVsCodeHtml(VSCODE_HTML)).toBe(true);+ expect(isVsCodeHtml(`<div style="font-family: Consolas, monospace; white-space: pre;"><div>x</div></div>`)).toBe(true);+ });++ it("rejects HTML of any other shape", () => {+ expect(isVsCodeHtml("")).toBe(false);+ // Not preformatted, or not monospace.+ expect(isVsCodeHtml(`<div style="font-family: monospace;"><div>x</div></div>`)).toBe(false);+ expect(isVsCodeHtml(`<div style="font-family: Arial; white-space: pre;"><div>x</div></div>`)).toBe(false);+ // More than the one block, or a block that is not a div.+ expect(isVsCodeHtml(`${VSCODE_HTML}<p>after</p>`)).toBe(false);+ expect(isVsCodeHtml(`<pre style="font-family: monospace; white-space: pre;">x</pre>`)).toBe(false);+ });+ });+describe("vsCodePasteHtml", () => {it("builds a code block from the plain text of a multi-line copy", () => {expect(vsCodePasteHtml(clipboard(VSCODE_TEXT, VSCODE_CSS_DATA, VSCODE_HTML)))@@ -66,8 +83,13 @@ describe("PasteFromVsCode", () => {expect(vsCodePasteHtml(clipboard("a\nb", `{"mode":"makefile"}`))).toBe("<pre><code>a\nb</code></pre>");});+ it("falls back to the shape of the HTML when the editor data is missing, as in Firefox", () => {+ expect(vsCodePasteHtml(clipboard(VSCODE_TEXT, undefined, VSCODE_HTML))).toBe(`<pre><code>${VSCODE_TEXT}</code></pre>`);+ expect(vsCodePasteHtml(clipboard("margin: 0;", undefined, VSCODE_HTML))).toBe("<code>margin: 0;</code>");+ });+it("declines a clipboard that does not come from VS Code or holds no code", () => {- expect(vsCodePasteHtml(clipboard(VSCODE_TEXT, undefined, VSCODE_HTML))).toBeNull();+ expect(vsCodePasteHtml(clipboard(VSCODE_TEXT, undefined, "<p>text</p>"))).toBeNull();expect(vsCodePasteHtml(clipboard(VSCODE_TEXT, "not json"))).toBeNull();expect(vsCodePasteHtml(clipboard(VSCODE_TEXT, "null"))).toBeNull();expect(vsCodePasteHtml(clipboard(" \n ", VSCODE_CSS_DATA))).toBeNull();@@ -124,6 +146,16 @@ describe("PasteFromVsCode", () => {);});+ it("pastes VS Code's HTML as a code block in the default language when the editor data is missing", () => {+ _setModelData(editor.model, "<paragraph>[]</paragraph>");++ expect(paste(clipboard(VSCODE_TEXT, undefined, VSCODE_HTML))).toBe(+ "<codeBlock language=\"text-x-trilium-auto\">#trilium-error-overlay .tn-eo-title {<softBreak></softBreak>"+ + " margin: 0;<softBreak></softBreak> font-size: 1.4em;<softBreak></softBreak>"+ + " font-weight: 600;<softBreak></softBreak>}</codeBlock>"+ );+ });+it("pastes plain text into an existing code block", () => {_setModelData(editor.model, "<codeBlock language=\"text-css\">[]</codeBlock>");diff --git a/packages/ckeditor5/src/plugins/paste_from_vscode.ts b/packages/ckeditor5/src/plugins/paste_from_vscode.tsindex a7305da55d..a13712a0b8 100644--- a/packages/ckeditor5/src/plugins/paste_from_vscode.ts+++ b/packages/ckeditor5/src/plugins/paste_from_vscode.ts@@ -38,6 +38,10 @@ const VSCODE_LANGUAGE_MIMES: Record<string, string> = {* exact and the theme colors are dropped. A language that the editor does not offer falls back to* the code block's default language.*+ * Firefox cannot read {@link VSCODE_EDITOR_DATA}, which VS Code writes in Chromium's own clipboard+ * format. There the plugin recognizes VS Code's HTML instead (see {@link isVsCodeHtml}) and leaves+ * the language to the default.+ ** Pasting into an existing code block is left to `CodeBlockEditing`, whose normal-priority* listener inserts the plain text.*/@@ -68,7 +72,8 @@ export default class PasteFromVsCode extends Plugin {* from VS Code.*/export function vsCodePasteHtml(dataTransfer: { getData(type: string): string }): string | null {- const mode = readVsCodeMode(dataTransfer.getData(VSCODE_EDITOR_DATA));+ const mode = readVsCodeMode(dataTransfer.getData(VSCODE_EDITOR_DATA))+ ?? (isVsCodeHtml(dataTransfer.getData("text/html")) ? "" : null);if (mode === null) {return null;}@@ -96,6 +101,25 @@ export function codeBlockLanguage(mode: string): string | null {return mime ? normalizeMimeTypeForCKEditor(mime) : null;}+/**+ * Whether the clipboard HTML has the shape VS Code writes: a single `div` with `white-space: pre`+ * and a monospace font, holding a `div` per line.+ */+export function isVsCodeHtml(html: string): boolean {+ if (!html) {+ return false;+ }++ // A leading `<meta charset>` is parsed into the head, so it does not count as an element here.+ const body = new DOMParser().parseFromString(html, "text/html").body;+ const root = body.firstElementChild;+ if (!(root instanceof HTMLDivElement) || root !== body.lastElementChild) {+ return false;+ }++ return root.style.whiteSpace === "pre" && root.style.fontFamily.includes("monospace");+}+/*** The `mode` of the {@link VSCODE_EDITOR_DATA} JSON: the language ID, or an empty string if VS Code* named none. `null` if the data is absent or malformed.