Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix: preserve rich clipboard formats on restore (#1020)

Dustin Persek4b6258548995
diff --git a/src/helpers/clipboard.js b/src/helpers/clipboard.js
index 11b31fdae..87f097a2c 100644
--- a/src/helpers/clipboard.js
+++ b/src/helpers/clipboard.js
@@ -611,21 +611,44 @@ class ClipboardManager {
_saveClipboard() {
const formats = clipboard.availableFormats();
+ const data = {};
+
+ const text = clipboard.readText();
+ if (text) data.text = text;
+
+ if (formats.includes("text/html")) {
+ const html = clipboard.readHTML();
+ if (html) data.html = html;
+ }
+
+ if (formats.includes("text/rtf") || formats.includes("public.rtf")) {
+ const rtf = clipboard.readRTF();
+ if (rtf) data.rtf = rtf;
+ }
+
if (formats.some((f) => f.startsWith("image/"))) {
- return { type: "image", data: clipboard.readImage() };
- } else if (formats.includes("text/html")) {
- return { type: "html", text: clipboard.readText(), html: clipboard.readHTML() };
- } else {
- return { type: "text", data: clipboard.readText() };
+ const image = clipboard.readImage();
+ if (image && !image.isEmpty()) data.image = image;
+ }
+
+ const keys = Object.keys(data);
+ if (keys.length === 1 && keys[0] === "image") {
+ return { type: "image", data: data.image };
}
+ if (keys.length === 1 && keys[0] === "text") {
+ return { type: "text", data: data.text };
+ }
+ if (keys.length > 0) return { type: "formats", data };
+
+ return { type: "text", data: text };
}
_restoreClipboard(original) {
if (!original) return;
- if (original.type === "image") {
- if (!original.data.isEmpty()) clipboard.writeImage(original.data);
- } else if (original.type === "html") {
- clipboard.write({ text: original.text, html: original.html });
+ if (original.type === "formats") {
+ clipboard.write(original.data);
+ } else if (original.type === "image") {
+ clipboard.writeImage(original.data);
} else {
clipboard.writeText(original.data);
}
diff --git a/test/helpers/clipboardRestore.test.js b/test/helpers/clipboardRestore.test.js
index c558cc038..45c7c6b22 100644
--- a/test/helpers/clipboardRestore.test.js
+++ b/test/helpers/clipboardRestore.test.js
@@ -5,7 +5,10 @@ const Module = require("node:module");
const fakeClipboard = {
text: "",
html: "",
+ rtf: "",
+ image: null,
formats: ["text/plain"],
+ writes: [],
availableFormats() {
return this.formats;
},
@@ -14,20 +17,46 @@ const fakeClipboard = {
},
writeText(text) {
this.text = text;
+ this.html = "";
+ this.rtf = "";
+ this.image = null;
+ this.formats = ["text/plain"];
+ this.writes.push(["writeText", text]);
},
readHTML() {
return this.html;
},
+ readRTF() {
+ return this.rtf;
+ },
write(payload) {
- this.text = payload.text;
- this.html = payload.html;
+ this.text = payload.text || "";
+ this.html = payload.html || "";
+ this.rtf = payload.rtf || "";
+ this.image = payload.image || null;
+ this.formats = [];
+ if (Object.hasOwn(payload, "text")) this.formats.push("text/plain");
+ if (Object.hasOwn(payload, "html")) this.formats.push("text/html");
+ if (Object.hasOwn(payload, "rtf")) this.formats.push("text/rtf");
+ if (Object.hasOwn(payload, "image")) this.formats.push("image/png");
+ this.writes.push(["write", payload]);
},
readImage() {
- return { isEmpty: () => true };
+ return this.image || emptyImage;
+ },
+ writeImage(image) {
+ this.text = "";
+ this.html = "";
+ this.rtf = "";
+ this.image = image;
+ this.formats = image && !image.isEmpty() ? ["image/png"] : [];
+ this.writes.push(["writeImage", image]);
},
- writeImage() {},
};
+const emptyImage = { isEmpty: () => true };
+const nonEmptyImage = { isEmpty: () => false };
+
const originalLoad = Module._load;
Module._load = function loadWithElectronMock(request, parent, isMain) {
if (request === "electron") {
@@ -44,12 +73,48 @@ Module._load = function loadWithElectronMock(request, parent, isMain) {
const ClipboardManager = require("../../src/helpers/clipboard");
Module._load = originalLoad;
-function resetClipboard() {
- fakeClipboard.text = "";
- fakeClipboard.html = "";
- fakeClipboard.formats = ["text/plain"];
+function resetClipboard({
+ text = "",
+ html = "",
+ rtf = "",
+ image = null,
+ formats = ["text/plain"],
+} = {}) {
+ fakeClipboard.text = text;
+ fakeClipboard.html = html;
+ fakeClipboard.rtf = rtf;
+ fakeClipboard.image = image;
+ fakeClipboard.formats = formats;
+ fakeClipboard.writes = [];
}
+test("restore preserves rich clipboard formats atomically", () => {
+ resetClipboard({
+ formats: ["text/html", "text/rtf", "text/plain", "image/png"],
+ text: "plain before",
+ html: "<b>html before</b>",
+ rtf: "{\\rtf1 before}",
+ image: nonEmptyImage,
+ });
+ const manager = new ClipboardManager();
+
+ const snapshot = manager._saveClipboard();
+ fakeClipboard.writeText("dictated text");
+ manager._restoreClipboard(snapshot);
+
+ assert.deepEqual([...fakeClipboard.availableFormats()].sort(), [
+ "image/png",
+ "text/html",
+ "text/plain",
+ "text/rtf",
+ ]);
+ assert.equal(fakeClipboard.text, "plain before");
+ assert.equal(fakeClipboard.html, "<b>html before</b>");
+ assert.equal(fakeClipboard.rtf, "{\\rtf1 before}");
+ assert.equal(fakeClipboard.image, nonEmptyImage);
+ assert.equal(fakeClipboard.writes.at(-1)[0], "write");
+});
+
test("restore runs when clipboard still contains the pasted text", async () => {
resetClipboard();
fakeClipboard.text = "dictated text";