Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(desktop): support Electron 44 clipboard API

tianma-if3ba075a7244f
diff --git a/apps/desktop/src/main/clipboard-write.mjs b/apps/desktop/src/main/clipboard-write.mjs
index 591022a1..e640fc66 100644
--- a/apps/desktop/src/main/clipboard-write.mjs
+++ b/apps/desktop/src/main/clipboard-write.mjs
@@ -1,10 +1,26 @@
-export const writeRichClipboard = (clipboard, input) => {
+export const writeTextClipboard = async (clipboard, value) => {
+ if (typeof value !== "string") {
+ throw new TypeError("Clipboard value must be a string");
+ }
+
+ await clipboard.writeText(value);
+ return await clipboard.readText() === value;
+};
+
+export const writeRichClipboard = async (clipboard, ClipboardItem, input) => {
const html = input?.html;
const plainText = input?.plainText;
if (typeof html !== "string" || typeof plainText !== "string") {
throw new TypeError("Clipboard HTML and plain text must be strings");
}
- clipboard.write({ html, text: plainText });
- return clipboard.readText() === plainText && clipboard.readHTML().trim().length > 0;
+ await clipboard.write([new ClipboardItem({
+ "text/html": html,
+ "text/plain": plainText,
+ })]);
+ const [copiedText, hasHtml] = await Promise.all([
+ clipboard.readText(),
+ clipboard.has("text/html"),
+ ]);
+ return copiedText === plainText && hasHtml;
};
diff --git a/apps/desktop/src/main/clipboard-write.test.mjs b/apps/desktop/src/main/clipboard-write.test.mjs
index 3342a9c8..7b15171a 100644
--- a/apps/desktop/src/main/clipboard-write.test.mjs
+++ b/apps/desktop/src/main/clipboard-write.test.mjs
@@ -1,41 +1,84 @@
import { describe, expect, test } from "bun:test";
-import { writeRichClipboard } from "./clipboard-write.mjs";
+import { writeRichClipboard, writeTextClipboard } from "./clipboard-write.mjs";
+
+class TestClipboardItem {
+ constructor(data) {
+ this.data = data;
+ }
+}
+
+describe("writeTextClipboard", () => {
+ test("awaits and verifies the Electron 44 text clipboard API", async () => {
+ const calls = [];
+ const clipboard = {
+ writeText: async (value) => { calls.push(["writeText", value]); },
+ readText: async () => {
+ calls.push(["readText"]);
+ return "Hello";
+ },
+ };
+
+ await expect(writeTextClipboard(clipboard, "Hello")).resolves.toBe(true);
+ expect(calls).toEqual([
+ ["writeText", "Hello"],
+ ["readText"],
+ ]);
+ });
+
+ test("does not report success when the written text cannot be read back", async () => {
+ const clipboard = {
+ writeText: async () => {},
+ readText: async () => "",
+ };
+
+ await expect(writeTextClipboard(clipboard, "Hello")).resolves.toBe(false);
+ });
+
+ test("rejects malformed text input", async () => {
+ const clipboard = { writeText: async () => {}, readText: async () => "" };
+ await expect(writeTextClipboard(clipboard, null)).rejects.toThrow(
+ "Clipboard value must be a string",
+ );
+ });
+});
describe("writeRichClipboard", () => {
- test("writes and verifies both rich HTML and plain text", () => {
+ test("writes and verifies both rich HTML and plain text with the Electron 44 API", async () => {
const writes = [];
const clipboard = {
- write: (value) => writes.push(value),
- readText: () => "Hello",
- readHTML: () => '<meta charset="utf-8"><strong>Hello</strong>',
+ write: async (value) => writes.push(value),
+ readText: async () => "Hello",
+ has: async (type) => type === "text/html",
};
- expect(writeRichClipboard(clipboard, {
+ await expect(writeRichClipboard(clipboard, TestClipboardItem, {
html: "<strong>Hello</strong>",
plainText: "Hello",
- })).toBe(true);
- expect(writes).toEqual([{
- html: "<strong>Hello</strong>",
- text: "Hello",
- }]);
+ })).resolves.toBe(true);
+ expect(writes).toEqual([[
+ new TestClipboardItem({
+ "text/html": "<strong>Hello</strong>",
+ "text/plain": "Hello",
+ }),
+ ]]);
});
- test("does not report success when rich HTML is missing after the write", () => {
+ test("does not report success when rich HTML is missing after the write", async () => {
const clipboard = {
- write: () => {},
- readText: () => "Hello",
- readHTML: () => "",
+ write: async () => {},
+ readText: async () => "Hello",
+ has: async () => false,
};
- expect(writeRichClipboard(clipboard, {
+ await expect(writeRichClipboard(clipboard, TestClipboardItem, {
html: "<strong>Hello</strong>",
plainText: "Hello",
- })).toBe(false);
+ })).resolves.toBe(false);
});
- test("rejects malformed renderer input", () => {
- const clipboard = { write: () => {}, readText: () => "", readHTML: () => "" };
- expect(() => writeRichClipboard(clipboard, { html: "<p>Hello</p>" })).toThrow(
+ test("rejects malformed renderer input", async () => {
+ const clipboard = { write: async () => {}, readText: async () => "", has: async () => false };
+ await expect(writeRichClipboard(clipboard, TestClipboardItem, { html: "<p>Hello</p>" })).rejects.toThrow(
"Clipboard HTML and plain text must be strings",
);
});
diff --git a/apps/desktop/src/main/index.mjs b/apps/desktop/src/main/index.mjs
index 9748a202..3bb9b371 100644
--- a/apps/desktop/src/main/index.mjs
+++ b/apps/desktop/src/main/index.mjs
@@ -1,4 +1,4 @@
-import { app, BrowserWindow, Menu, Tray, nativeImage, ipcMain, session, net, protocol, shell, dialog, safeStorage, clipboard, powerMonitor, desktopCapturer, screen } from "electron";
+import { app, BrowserWindow, Menu, Tray, nativeImage, ipcMain, session, net, protocol, shell, dialog, safeStorage, clipboard, ClipboardItem, powerMonitor, desktopCapturer, screen } from "electron";
import { createReadStream, existsSync } from "node:fs";
import { appendFile, mkdir, open, readdir, readFile, rename, rm, stat, unlink, writeFile } from "node:fs/promises";
import { basename, join } from "node:path";
@@ -28,7 +28,7 @@ import { userDataDirectoryFromArguments } from "./user-data-directory.mjs";
import { isAllowedPrintPreviewUrl } from "./window-open-policy.mjs";
import { showWindow } from "./window-visibility.mjs";
import { trayIconPath } from "./tray-icon.mjs";
-import { writeRichClipboard } from "./clipboard-write.mjs";
+import { writeRichClipboard, writeTextClipboard } from "./clipboard-write.mjs";
import { captureScreenToNote, createScreenshotCaptureGuard, screenshotImportIpcPayload, writeScreenshotTempPath } from "./screenshot-capture.mjs";
import { LocalDataResetError, scheduleMacLocalDataReset } from "./local-data-reset.mjs";
import { buildDesktopDiagnosticIssueUrl, normalizeDesktopDiagnostic } from "./desktop-diagnostics.mjs";
@@ -1489,12 +1489,8 @@ const startApplication = async () => {
ipcMain.on("desktop:api-base-url-sync", (event) => { event.returnValue = configuredApiBaseUrl; });
ipcMain.on("desktop:session-token-sync", (event) => { event.returnValue = desktopSessionToken; });
ipcMain.on("desktop:recovered-after-abnormal-exit-sync", (event) => { event.returnValue = recoveredAfterAbnormalExit; });
- ipcMain.handle("desktop:copy-text", (_event, value) => {
- if (typeof value !== "string") throw new Error("Clipboard value must be a string");
- clipboard.writeText(value);
- return clipboard.readText() === value;
- });
- ipcMain.handle("desktop:copy-html", (_event, input) => writeRichClipboard(clipboard, input));
+ ipcMain.handle("desktop:copy-text", (_event, value) => writeTextClipboard(clipboard, value));
+ ipcMain.handle("desktop:copy-html", (_event, input) => writeRichClipboard(clipboard, ClipboardItem, input));
ipcMain.handle("desktop:set-session-token", async (_event, value) => {
await saveDesktopSessionToken(value);
return { stored: Boolean(desktopSessionToken) };