Skip to content

Commit f0c45a0

Browse files
authored
feat: add context menu support for editable fields (#1870)
1 parent f5146c3 commit f0c45a0

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

apps/code/src/main/window.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import path from "node:path";
22
import { fileURLToPath } from "node:url";
33
import { createIPCHandler } from "@posthog/electron-trpc/main";
4-
import { BrowserWindow, screen, shell } from "electron";
4+
import {
5+
BrowserWindow,
6+
Menu,
7+
type MenuItemConstructorOptions,
8+
screen,
9+
shell,
10+
} from "electron";
511
import { container } from "./di/container";
612
import { MAIN_TOKENS } from "./di/tokens";
713
import { buildApplicationMenu } from "./menu";
@@ -97,6 +103,24 @@ function setupExternalLinkHandlers(window: BrowserWindow): void {
97103
});
98104
}
99105

106+
function setupEditableContextMenu(window: BrowserWindow): void {
107+
window.webContents.on("context-menu", (_event, params) => {
108+
if (!params.isEditable) return;
109+
const { editFlags } = params;
110+
const template: MenuItemConstructorOptions[] = [
111+
{ role: "undo", enabled: editFlags.canUndo },
112+
{ role: "redo", enabled: editFlags.canRedo },
113+
{ type: "separator" },
114+
{ role: "cut", enabled: editFlags.canCut },
115+
{ role: "copy", enabled: editFlags.canCopy },
116+
{ role: "paste", enabled: editFlags.canPaste },
117+
{ type: "separator" },
118+
{ role: "selectAll", enabled: editFlags.canSelectAll },
119+
];
120+
Menu.buildFromTemplate(template).popup({ window });
121+
});
122+
}
123+
100124
export function createWindow(): void {
101125
const isDev = isDevBuild();
102126
const savedState = getSavedWindowState();
@@ -187,6 +211,7 @@ export function createWindow(): void {
187211
});
188212

189213
setupExternalLinkHandlers(mainWindow);
214+
setupEditableContextMenu(mainWindow);
190215
buildApplicationMenu();
191216

192217
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {

apps/code/src/renderer/features/message-editor/components/PromptInput.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ export const PromptInput = forwardRef<EditorHandle, PromptInputProps>(
224224
[focus],
225225
);
226226

227+
const handleContextMenu = useCallback((e: React.MouseEvent) => {
228+
e.stopPropagation();
229+
}, []);
230+
227231
const handleSubmitClick = (e: React.MouseEvent) => {
228232
e.stopPropagation();
229233
if (onSubmitClick) {
@@ -271,6 +275,7 @@ export const PromptInput = forwardRef<EditorHandle, PromptInputProps>(
271275
<Flex direction="column" gap="1">
272276
<InputGroup
273277
onClick={handleContainerClick}
278+
onContextMenu={handleContextMenu}
274279
className={`h-auto bg-card ${isBashMode ? "ring-1 ring-blue-9" : ""}`}
275280
style={{ cursor: "text" }}
276281
{...(tourTarget && {

apps/code/src/renderer/features/sessions/components/SessionView.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ export function SessionView({
346346

347347
useAutoFocusOnTyping(editorRef, !isActiveSession);
348348

349+
const handleContextMenu = useCallback((e: React.MouseEvent) => {
350+
const target = e.target as HTMLElement;
351+
if (
352+
target.closest('input, textarea, [contenteditable="true"], .ProseMirror')
353+
) {
354+
e.stopPropagation();
355+
}
356+
}, []);
357+
349358
return (
350359
<ContextMenu.Root>
351360
<ContextMenu.Trigger>
@@ -354,6 +363,7 @@ export function SessionView({
354363
direction="column"
355364
height="100%"
356365
className="relative bg-background"
366+
onContextMenu={handleContextMenu}
357367
>
358368
<RawLogsView
359369
events={events}
@@ -366,6 +376,7 @@ export function SessionView({
366376
height="100%"
367377
className="relative bg-background"
368378
onClick={handlePaneClick}
379+
onContextMenu={handleContextMenu}
369380
onDragEnter={handleDragEnter}
370381
onDragLeave={handleDragLeave}
371382
onDragOver={handleDragOver}

0 commit comments

Comments
 (0)