feat(composer): fold large pastes into text attachments (#11442)

This commit is contained in:
Chris Deeming authored and GitHub committed 2026-09-12 17:28:28 -07:00
1 parent 36caf200c2
commit 68c2277f50
77 files changed
+3278 -465

No files matched your search

@@ -40,6 +40,7 @@ import {
openExternal,
openSystemSettings,
checkSystemPermission,
pasteAsText,
probeRemoteEditors,
pickFolder,
pickProjectFavicon,
@@ -124,6 +125,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
yield* ipc.handle(openExternal);
yield* ipc.handle(openSystemSettings);
yield* ipc.handle(checkSystemPermission);
yield* ipc.handle(pasteAsText);
yield* ipc.handle(probeRemoteEditors);
yield* ipc.handle(getUpdateState);
yield* ipc.handle(setUpdateChannel);
+1
View File
@@ -7,6 +7,7 @@ export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
export const OPEN_SYSTEM_SETTINGS_CHANNEL = "desktop:open-system-settings";
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
export const PASTE_AS_TEXT_CHANNEL = "desktop:paste-as-text";
export const SNAP_SHOT_EVENT_CHANNEL = "desktop:snap-shot-event";
export const QUIT_SHORTCUT_CHANNEL = "desktop:quit-shortcut";
export const GET_WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:get-window-fullscreen-state";
@@ -6,6 +6,15 @@ import { vi } from "vite-plus/test";
import type * as Electron from "electron";
const { focusedWebContents, ownerWindow } = vi.hoisted(() => ({
focusedWebContents: vi.fn(),
ownerWindow: vi.fn(),
}));
vi.mock("electron", () => ({
webContents: { getFocusedWebContents: focusedWebContents },
BrowserWindow: { fromWebContents: ownerWindow },
}));
import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
import * as ElectronDialog from "../../electron/ElectronDialog.ts";
@@ -13,6 +22,7 @@ import * as ElectronWindow from "../../electron/ElectronWindow.ts";
import {
getLocalEnvironmentBootstraps,
getWindowFullscreenState,
pasteAsText,
pickProjectFavicon,
} from "./window.ts";
@@ -153,6 +163,50 @@ describe("getWindowFullscreenState", () => {
});
});
describe("pasteAsText", () => {
it.effect(
"pastes into the focused guest only after the main renderer acknowledges the menu action",
() => {
const paste = vi.fn();
const mainPaste = vi.fn();
const window = {
webContents: { id: 42, paste: mainPaste },
isDestroyed: () => false,
} as unknown as Electron.BrowserWindow;
focusedWebContents.mockReturnValue({ paste, isDestroyed: () => false });
ownerWindow.mockReturnValue(window);
return Effect.gen(function* () {
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
assert.equal(paste.mock.calls.length, 1);
assert.equal(mainPaste.mock.calls.length, 0);
yield* pasteAsText.handler(undefined, { sender: { id: 99 } });
assert.equal(paste.mock.calls.length, 1);
ownerWindow.mockReturnValue({}); // A focused PiP/other BrowserWindow.
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
assert.equal(paste.mock.calls.length, 1);
ownerWindow.mockReturnValue(null); // Detached contents.
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
assert.equal(paste.mock.calls.length, 1);
ownerWindow.mockReturnValue(window);
focusedWebContents.mockReturnValue({ paste, isDestroyed: () => true });
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
assert.equal(paste.mock.calls.length, 1);
focusedWebContents.mockReturnValue(null);
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
assert.equal(paste.mock.calls.length, 1);
}).pipe(
Effect.provide(
Layer.mock(ElectronWindow.ElectronWindow)({
main: Effect.succeed(Option.some(window)),
}),
),
);
},
);
});
describe("pickProjectFavicon", () => {
it.effect("opens a single-image picker from the project directory", () =>
Effect.gen(function* () {
+26
View File
@@ -345,6 +345,32 @@ export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
}),
});
export const pasteAsText = DesktopIpc.makeIpcMethod({
channel: IpcChannels.PASTE_AS_TEXT_CHANNEL,
payload: Schema.Undefined,
result: Schema.Void,
handler: Effect.fn("desktop.ipc.window.pasteAsText")(function* (_input, event) {
const electronWindow = yield* ElectronWindow.ElectronWindow;
const window = yield* electronWindow.main;
if (
event === undefined ||
Option.isNone(window) ||
window.value.isDestroyed() ||
window.value.webContents.id !== event.sender.id
) {
return;
}
const focused = Electron.webContents.getFocusedWebContents();
if (
focused &&
!focused.isDestroyed() &&
Electron.BrowserWindow.fromWebContents(focused) === window.value
) {
focused.paste();
}
}),
});
/** Theme files are a few KB; anything larger returns empty text and lets the
* renderer reject it by size without the contents ever crossing the bridge. */
const PICKED_THEME_FILE_MAX_BYTES = 256 * 1024;
+1
View File
@@ -157,6 +157,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
openSystemSettings: (pane: string) =>
ipcRenderer.invoke(IpcChannels.OPEN_SYSTEM_SETTINGS_CHANNEL, pane),
probeRemoteEditors: () => ipcRenderer.invoke(IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL, undefined),
pasteAsText: () => ipcRenderer.invoke(IpcChannels.PASTE_AS_TEXT_CHANNEL, undefined),
onMenuAction: (listener) => {
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
if (typeof action !== "string") return;
@@ -153,6 +153,36 @@ describe("DesktopApplicationMenu", () => {
}),
);
it.effect("owns Paste as Text and routes it through the renderer", () =>
Effect.gen(function* () {
const selectedAction = yield* Deferred.make<string>();
const applicationMenuTemplate =
yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
yield* configureMenu(selectedAction, applicationMenuTemplate);
const template = yield* Deferred.await(applicationMenuTemplate);
const editMenu = template.find((item) => item.label === "Edit");
assert.isDefined(editMenu);
if (!Array.isArray(editMenu.submenu)) {
throw new Error("Expected Edit menu submenu to be an array.");
}
const pasteAsTextItem = editMenu.submenu.find((item) => item.label === "Paste as Text");
assert.isDefined(pasteAsTextItem);
assert.equal(pasteAsTextItem.accelerator, "CmdOrCtrl+Shift+V");
if (typeof pasteAsTextItem.click !== "function") {
throw new Error("Expected Paste as Text menu item to have a click handler.");
}
pasteAsTextItem.click(
{} as Electron.MenuItem,
{} as Electron.BrowserWindow,
{} as KeyboardEvent,
);
assert.equal(yield* Deferred.await(selectedAction), "paste-as-text");
}),
);
// Zoom must route through DesktopWindow.zoomMain instead of the Electron
// zoom roles: the roles zoom whichever webContents has focus, which breaks
// app zoom while an embedded preview WebContentsView holds focus.
@@ -46,7 +46,9 @@ const dispatchMenuAction = Effect.fn("desktop.menu.dispatchMenuAction")(function
action: string,
): Effect.fn.Return<void, DesktopWindow.DesktopWindowError, DesktopWindow.DesktopWindow> {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.dispatchMenuAction(action);
yield* desktopWindow.dispatchMenuAction(action, {
reveal: action !== "paste-as-text",
});
});
const zoomMainWindow = Effect.fn("desktop.menu.zoomMainWindow")(function* (
@@ -135,6 +137,9 @@ export const make = Effect.gen(function* () {
const settingsClick = () => {
runMenuEffect("open-settings", dispatchMenuAction("open-settings"));
};
const pasteAsTextClick = () => {
runMenuEffect("paste-as-text", dispatchMenuAction("paste-as-text"));
};
const zoomClick = (direction: DesktopWindow.MainWindowZoomDirection) => () => {
runMenuEffect(`zoom-${direction}`, zoomMainWindow(direction));
};
@@ -184,7 +189,34 @@ export const make = Effect.gen(function* () {
{ role: environment.platform === "darwin" ? "close" : "quit" },
],
},
{ role: "editMenu" },
{
label: "Edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{
label: "Paste as Text",
accelerator: "CmdOrCtrl+Shift+V",
click: pasteAsTextClick,
},
{ role: "delete" },
{ type: "separator" },
{ role: "selectAll" },
...(environment.platform === "darwin"
? [
{ type: "separator" as const },
{
label: "Speech",
submenu: [{ role: "startSpeaking" as const }, { role: "stopSpeaking" as const }],
},
]
: []),
],
},
{
label: "View",
submenu: [