Files
genoffice/packages/electron-utils/src/context-menu.ts
T
merrick-2002andGenOffice 8f523289d6 Sync snapshot (2026-08-04) (#19)
- Docs: paragraph writing direction (LTR/RTL) toggle with auto-detection
- Docs: hardened file lifecycle, docx fidelity, and editor performance
- PDF: Export as Word via cloud conversion, non-destructive Save As, and an in-progress export dialog
- Slides: treat full-page background shapes as the page background
- Sheets: faster recalculation with a session-resident engine; better xlsx compatibility (builtin number formats, auto-fit row heights, sheet-name resolution)
- AI: surface stream and gateway errors instead of finishing empty turns; parse non-SSE JSON gateway replies; stall watchdogs
- Fixes from alpha feedback batches 3 and 4 (TOC levels, Windows menus, honest AI applies, login flow diagnostics, credits-exhausted notice)
- CI: sheets save round-trip E2E and xlsx compatibility corpus gate
- Start the 0.5 release train (v0.5.0/v0.5.1)

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-08-04 21:26:02 +08:00

117 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// Native right-click menu for surfaces without a self-drawn one (AI panel,
/// inputs, chrome). Renderer-drawn menus call preventDefault() on the DOM
/// contextmenu event, which suppresses this webContents event entirely
/// (verified on Electron 41), so the two never stack.
import type { App, ContextMenuParams, MenuItemConstructorOptions, WebContents } from 'electron'
export interface ContextMenuLabels {
cut: string
copy: string
paste: string
selectAll: string
}
const EN: ContextMenuLabels = { cut: 'Cut', copy: 'Copy', paste: 'Paste', selectAll: 'Select All' }
// One shared table instead of 4 keys × 19 languages duplicated into every
// app's main-process dictionary; strings match the docs Edit menu.
const LABELS: Record<string, ContextMenuLabels> = {
zh: { cut: '剪切', copy: '复制', paste: '粘贴', selectAll: '全选' },
en: EN,
ja: { cut: '切り取り', copy: 'コピー', paste: '貼り付け', selectAll: 'すべて選択' },
ko: { cut: '잘라내기', copy: '복사', paste: '붙여넣기', selectAll: '모두 선택' },
fr: { cut: 'Couper', copy: 'Copier', paste: 'Coller', selectAll: 'Tout sélectionner' },
de: { cut: 'Ausschneiden', copy: 'Kopieren', paste: 'Einfügen', selectAll: 'Alles auswählen' },
es: { cut: 'Cortar', copy: 'Copiar', paste: 'Pegar', selectAll: 'Seleccionar todo' },
th: { cut: 'ตัด', copy: 'คัดลอก', paste: 'วาง', selectAll: 'เลือกทั้งหมด' },
id: { cut: 'Potong', copy: 'Salin', paste: 'Tempel', selectAll: 'Pilih Semua' },
ru: { cut: 'Вырезать', copy: 'Копировать', paste: 'Вставить', selectAll: 'Выделить все' },
ar: { cut: 'قص', copy: 'نسخ', paste: 'لصق', selectAll: 'تحديد الكل' },
pt: { cut: 'Recortar', copy: 'Copiar', paste: 'Colar', selectAll: 'Selecionar Tudo' },
it: { cut: 'Taglia', copy: 'Copia', paste: 'Incolla', selectAll: 'Seleziona tutto' },
pl: { cut: 'Wytnij', copy: 'Kopiuj', paste: 'Wklej', selectAll: 'Zaznacz wszystko' },
nl: { cut: 'Knippen', copy: 'Kopiëren', paste: 'Plakken', selectAll: 'Alles selecteren' },
ms: { cut: 'Potong', copy: 'Salin', paste: 'Tampal', selectAll: 'Pilih Semua' },
he: { cut: 'גזור', copy: 'העתק', paste: 'הדבק', selectAll: 'בחר הכול' },
hi: { cut: 'काटें', copy: 'कॉपी करें', paste: 'चिपकाएँ', selectAll: 'सभी चुनें' },
'zh-TW': { cut: '剪下', copy: '複製', paste: '貼上', selectAll: '全選' },
}
export function contextMenuLabels(lang: string): ContextMenuLabels {
return LABELS[lang] ?? EN
}
export type ContextMenuItem =
| { type: 'separator' }
| { action: 'replaceMisspelling'; label: string }
| { action: 'cut' | 'copy' | 'paste' | 'selectAll'; label: string; enabled: boolean }
type BuildParams = Pick<
ContextMenuParams,
'isEditable' | 'selectionText' | 'misspelledWord' | 'dictionarySuggestions' | 'editFlags'
>
/** Empty result means: don't show a menu. */
export function buildContextMenuItems(
params: BuildParams,
labels: ContextMenuLabels,
): ContextMenuItem[] {
const flags = params.editFlags
if (params.isEditable) {
const items: ContextMenuItem[] = []
if (params.misspelledWord && params.dictionarySuggestions.length) {
for (const word of params.dictionarySuggestions)
items.push({ action: 'replaceMisspelling', label: word })
items.push({ type: 'separator' })
}
items.push(
{ action: 'cut', label: labels.cut, enabled: flags.canCut },
{ action: 'copy', label: labels.copy, enabled: flags.canCopy },
{ action: 'paste', label: labels.paste, enabled: flags.canPaste },
{ type: 'separator' },
{ action: 'selectAll', label: labels.selectAll, enabled: flags.canSelectAll },
)
return items
}
if (params.selectionText.trim()) {
return [
{ action: 'copy', label: labels.copy, enabled: flags.canCopy },
{ action: 'selectAll', label: labels.selectAll, enabled: flags.canSelectAll },
]
}
return []
}
// Symbol.for: survives multiple bundled copies (see navigation-guard.ts).
const INSTALLED = Symbol.for('genoffice.context-menu-installed')
export function installContextMenu(app: App, getLabels: () => ContextMenuLabels): void {
const holder = app as unknown as Record<symbol, boolean | undefined>
if (holder[INSTALLED]) return
holder[INSTALLED] = true
app.on('web-contents-created', (_event, contents) => {
contents.on('context-menu', (_e, params) => {
void popupMenu(contents, params, getLabels())
})
})
}
async function popupMenu(
contents: WebContents,
params: ContextMenuParams,
labels: ContextMenuLabels,
): Promise<void> {
const items = buildContextMenuItems(params, labels)
if (!items.length) return
// Dynamic import keeps this module loadable outside Electron (unit tests).
const { Menu } = await import('electron')
const template = items.map((item): MenuItemConstructorOptions => {
if ('type' in item) return { type: 'separator' }
if (item.action === 'replaceMisspelling')
return { label: item.label, click: () => contents.replaceMisspelling(item.label) }
const action = item.action
return { label: item.label, enabled: item.enabled, click: () => contents[action]() }
})
Menu.buildFromTemplate(template).popup()
}