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>
This commit is contained in:
co-authored by
GenOffice
parent
4da673d4df
commit
8f523289d6
@@ -0,0 +1,418 @@
|
||||
/// Localized replacements for Electron role menus, whose built-in labels are
|
||||
/// English-only and (for role:'windowMenu' on Windows/Linux) follow macOS
|
||||
/// conventions (Zoom, Ctrl+M minimize, Bring All to Front).
|
||||
import type { MenuItemConstructorOptions } from 'electron'
|
||||
import { contextMenuLabels, type ContextMenuLabels } from './context-menu'
|
||||
|
||||
export interface AppMenuLabels extends ContextMenuLabels {
|
||||
window: string
|
||||
minimize: string
|
||||
closeWindow: string
|
||||
edit: string
|
||||
undo: string
|
||||
redo: string
|
||||
delete: string
|
||||
view: string
|
||||
reload: string
|
||||
forceReload: string
|
||||
toggleDevTools: string
|
||||
actualSize: string
|
||||
zoomIn: string
|
||||
zoomOut: string
|
||||
fullscreen: string
|
||||
}
|
||||
|
||||
type Labels = Omit<AppMenuLabels, keyof ContextMenuLabels>
|
||||
|
||||
const EN: Labels = {
|
||||
window: 'Window',
|
||||
minimize: 'Minimize',
|
||||
closeWindow: 'Close Window',
|
||||
edit: 'Edit',
|
||||
undo: 'Undo',
|
||||
redo: 'Redo',
|
||||
delete: 'Delete',
|
||||
view: 'View',
|
||||
reload: 'Reload',
|
||||
forceReload: 'Force Reload',
|
||||
toggleDevTools: 'Developer Tools',
|
||||
actualSize: 'Actual Size',
|
||||
zoomIn: 'Zoom In',
|
||||
zoomOut: 'Zoom Out',
|
||||
fullscreen: 'Full Screen',
|
||||
}
|
||||
|
||||
// Shared table, same rationale as context-menu.ts: one copy instead of
|
||||
// 15 keys × 19 languages per app dictionary.
|
||||
const LABELS: Record<string, Labels> = {
|
||||
zh: {
|
||||
window: '窗口',
|
||||
minimize: '最小化',
|
||||
closeWindow: '关闭窗口',
|
||||
edit: '编辑',
|
||||
undo: '撤销',
|
||||
redo: '重做',
|
||||
delete: '删除',
|
||||
view: '视图',
|
||||
reload: '重新加载',
|
||||
forceReload: '强制重新加载',
|
||||
toggleDevTools: '开发者工具',
|
||||
actualSize: '实际大小',
|
||||
zoomIn: '放大',
|
||||
zoomOut: '缩小',
|
||||
fullscreen: '全屏',
|
||||
},
|
||||
en: EN,
|
||||
ja: {
|
||||
window: 'ウィンドウ',
|
||||
minimize: '最小化',
|
||||
closeWindow: 'ウィンドウを閉じる',
|
||||
edit: '編集',
|
||||
undo: '元に戻す',
|
||||
redo: 'やり直す',
|
||||
delete: '削除',
|
||||
view: '表示',
|
||||
reload: '再読み込み',
|
||||
forceReload: '強制的に再読み込み',
|
||||
toggleDevTools: '開発者ツール',
|
||||
actualSize: '実際のサイズ',
|
||||
zoomIn: '拡大',
|
||||
zoomOut: '縮小',
|
||||
fullscreen: 'フルスクリーン',
|
||||
},
|
||||
ko: {
|
||||
window: '창',
|
||||
minimize: '최소화',
|
||||
closeWindow: '창 닫기',
|
||||
edit: '편집',
|
||||
undo: '실행 취소',
|
||||
redo: '다시 실행',
|
||||
delete: '삭제',
|
||||
view: '보기',
|
||||
reload: '새로고침',
|
||||
forceReload: '강제 새로고침',
|
||||
toggleDevTools: '개발자 도구',
|
||||
actualSize: '실제 크기',
|
||||
zoomIn: '확대',
|
||||
zoomOut: '축소',
|
||||
fullscreen: '전체 화면',
|
||||
},
|
||||
fr: {
|
||||
window: 'Fenêtre',
|
||||
minimize: 'Réduire',
|
||||
closeWindow: 'Fermer la fenêtre',
|
||||
edit: 'Édition',
|
||||
undo: 'Annuler',
|
||||
redo: 'Rétablir',
|
||||
delete: 'Supprimer',
|
||||
view: 'Affichage',
|
||||
reload: 'Recharger',
|
||||
forceReload: 'Forcer le rechargement',
|
||||
toggleDevTools: 'Outils de développement',
|
||||
actualSize: 'Taille réelle',
|
||||
zoomIn: 'Zoom avant',
|
||||
zoomOut: 'Zoom arrière',
|
||||
fullscreen: 'Plein écran',
|
||||
},
|
||||
de: {
|
||||
window: 'Fenster',
|
||||
minimize: 'Minimieren',
|
||||
closeWindow: 'Fenster schließen',
|
||||
edit: 'Bearbeiten',
|
||||
undo: 'Rückgängig',
|
||||
redo: 'Wiederholen',
|
||||
delete: 'Löschen',
|
||||
view: 'Ansicht',
|
||||
reload: 'Neu laden',
|
||||
forceReload: 'Erzwungenes Neuladen',
|
||||
toggleDevTools: 'Entwicklertools',
|
||||
actualSize: 'Originalgröße',
|
||||
zoomIn: 'Vergrößern',
|
||||
zoomOut: 'Verkleinern',
|
||||
fullscreen: 'Vollbild',
|
||||
},
|
||||
es: {
|
||||
window: 'Ventana',
|
||||
minimize: 'Minimizar',
|
||||
closeWindow: 'Cerrar ventana',
|
||||
edit: 'Edición',
|
||||
undo: 'Deshacer',
|
||||
redo: 'Rehacer',
|
||||
delete: 'Eliminar',
|
||||
view: 'Ver',
|
||||
reload: 'Recargar',
|
||||
forceReload: 'Forzar recarga',
|
||||
toggleDevTools: 'Herramientas de desarrollo',
|
||||
actualSize: 'Tamaño real',
|
||||
zoomIn: 'Acercar',
|
||||
zoomOut: 'Alejar',
|
||||
fullscreen: 'Pantalla completa',
|
||||
},
|
||||
th: {
|
||||
window: 'หน้าต่าง',
|
||||
minimize: 'ย่อเล็กสุด',
|
||||
closeWindow: 'ปิดหน้าต่าง',
|
||||
edit: 'แก้ไข',
|
||||
undo: 'เลิกทำ',
|
||||
redo: 'ทำซ้ำ',
|
||||
delete: 'ลบ',
|
||||
view: 'มุมมอง',
|
||||
reload: 'โหลดใหม่',
|
||||
forceReload: 'บังคับโหลดใหม่',
|
||||
toggleDevTools: 'เครื่องมือนักพัฒนา',
|
||||
actualSize: 'ขนาดจริง',
|
||||
zoomIn: 'ขยาย',
|
||||
zoomOut: 'ย่อ',
|
||||
fullscreen: 'เต็มหน้าจอ',
|
||||
},
|
||||
id: {
|
||||
window: 'Jendela',
|
||||
minimize: 'Minimalkan',
|
||||
closeWindow: 'Tutup Jendela',
|
||||
edit: 'Edit',
|
||||
undo: 'Urungkan',
|
||||
redo: 'Ulangi',
|
||||
delete: 'Hapus',
|
||||
view: 'Tampilan',
|
||||
reload: 'Muat Ulang',
|
||||
forceReload: 'Paksa Muat Ulang',
|
||||
toggleDevTools: 'Alat Pengembang',
|
||||
actualSize: 'Ukuran Sebenarnya',
|
||||
zoomIn: 'Perbesar',
|
||||
zoomOut: 'Perkecil',
|
||||
fullscreen: 'Layar Penuh',
|
||||
},
|
||||
ru: {
|
||||
window: 'Окно',
|
||||
minimize: 'Свернуть',
|
||||
closeWindow: 'Закрыть окно',
|
||||
edit: 'Правка',
|
||||
undo: 'Отменить',
|
||||
redo: 'Повторить',
|
||||
delete: 'Удалить',
|
||||
view: 'Вид',
|
||||
reload: 'Перезагрузить',
|
||||
forceReload: 'Принудительно перезагрузить',
|
||||
toggleDevTools: 'Инструменты разработчика',
|
||||
actualSize: 'Реальный размер',
|
||||
zoomIn: 'Увеличить',
|
||||
zoomOut: 'Уменьшить',
|
||||
fullscreen: 'Полноэкранный режим',
|
||||
},
|
||||
ar: {
|
||||
window: 'نافذة',
|
||||
minimize: 'تصغير',
|
||||
closeWindow: 'إغلاق النافذة',
|
||||
edit: 'تحرير',
|
||||
undo: 'تراجع',
|
||||
redo: 'إعادة',
|
||||
delete: 'حذف',
|
||||
view: 'عرض',
|
||||
reload: 'إعادة التحميل',
|
||||
forceReload: 'فرض إعادة التحميل',
|
||||
toggleDevTools: 'أدوات المطور',
|
||||
actualSize: 'الحجم الفعلي',
|
||||
zoomIn: 'تكبير',
|
||||
zoomOut: 'تصغير العرض',
|
||||
fullscreen: 'ملء الشاشة',
|
||||
},
|
||||
pt: {
|
||||
window: 'Janela',
|
||||
minimize: 'Minimizar',
|
||||
closeWindow: 'Fechar Janela',
|
||||
edit: 'Editar',
|
||||
undo: 'Desfazer',
|
||||
redo: 'Refazer',
|
||||
delete: 'Excluir',
|
||||
view: 'Exibir',
|
||||
reload: 'Recarregar',
|
||||
forceReload: 'Forçar Recarregamento',
|
||||
toggleDevTools: 'Ferramentas do Desenvolvedor',
|
||||
actualSize: 'Tamanho Real',
|
||||
zoomIn: 'Ampliar',
|
||||
zoomOut: 'Reduzir',
|
||||
fullscreen: 'Tela Cheia',
|
||||
},
|
||||
it: {
|
||||
window: 'Finestra',
|
||||
minimize: 'Riduci a icona',
|
||||
closeWindow: 'Chiudi finestra',
|
||||
edit: 'Modifica',
|
||||
undo: 'Annulla',
|
||||
redo: 'Ripeti',
|
||||
delete: 'Elimina',
|
||||
view: 'Visualizza',
|
||||
reload: 'Ricarica',
|
||||
forceReload: 'Forza ricarica',
|
||||
toggleDevTools: 'Strumenti di sviluppo',
|
||||
actualSize: 'Dimensioni effettive',
|
||||
zoomIn: 'Ingrandisci',
|
||||
zoomOut: 'Riduci',
|
||||
fullscreen: 'Schermo intero',
|
||||
},
|
||||
pl: {
|
||||
window: 'Okno',
|
||||
minimize: 'Minimalizuj',
|
||||
closeWindow: 'Zamknij okno',
|
||||
edit: 'Edycja',
|
||||
undo: 'Cofnij',
|
||||
redo: 'Ponów',
|
||||
delete: 'Usuń',
|
||||
view: 'Widok',
|
||||
reload: 'Załaduj ponownie',
|
||||
forceReload: 'Wymuś ponowne załadowanie',
|
||||
toggleDevTools: 'Narzędzia deweloperskie',
|
||||
actualSize: 'Rzeczywisty rozmiar',
|
||||
zoomIn: 'Powiększ',
|
||||
zoomOut: 'Pomniejsz',
|
||||
fullscreen: 'Pełny ekran',
|
||||
},
|
||||
nl: {
|
||||
window: 'Venster',
|
||||
minimize: 'Minimaliseren',
|
||||
closeWindow: 'Venster sluiten',
|
||||
edit: 'Bewerken',
|
||||
undo: 'Ongedaan maken',
|
||||
redo: 'Opnieuw',
|
||||
delete: 'Verwijderen',
|
||||
view: 'Beeld',
|
||||
reload: 'Opnieuw laden',
|
||||
forceReload: 'Geforceerd opnieuw laden',
|
||||
toggleDevTools: 'Ontwikkelaarstools',
|
||||
actualSize: 'Ware grootte',
|
||||
zoomIn: 'Inzoomen',
|
||||
zoomOut: 'Uitzoomen',
|
||||
fullscreen: 'Volledig scherm',
|
||||
},
|
||||
ms: {
|
||||
window: 'Tetingkap',
|
||||
minimize: 'Minimumkan',
|
||||
closeWindow: 'Tutup Tetingkap',
|
||||
edit: 'Edit',
|
||||
undo: 'Buat Asal',
|
||||
redo: 'Buat Semula',
|
||||
delete: 'Padam',
|
||||
view: 'Paparan',
|
||||
reload: 'Muat Semula',
|
||||
forceReload: 'Paksa Muat Semula',
|
||||
toggleDevTools: 'Alat Pembangun',
|
||||
actualSize: 'Saiz Sebenar',
|
||||
zoomIn: 'Zum Masuk',
|
||||
zoomOut: 'Zum Keluar',
|
||||
fullscreen: 'Skrin Penuh',
|
||||
},
|
||||
he: {
|
||||
window: 'חלון',
|
||||
minimize: 'מזער',
|
||||
closeWindow: 'סגור חלון',
|
||||
edit: 'עריכה',
|
||||
undo: 'בטל',
|
||||
redo: 'בצע שוב',
|
||||
delete: 'מחק',
|
||||
view: 'תצוגה',
|
||||
reload: 'טען מחדש',
|
||||
forceReload: 'טען מחדש בכפייה',
|
||||
toggleDevTools: 'כלי מפתחים',
|
||||
actualSize: 'גודל בפועל',
|
||||
zoomIn: 'הגדל',
|
||||
zoomOut: 'הקטן',
|
||||
fullscreen: 'מסך מלא',
|
||||
},
|
||||
hi: {
|
||||
window: 'विंडो',
|
||||
minimize: 'छोटा करें',
|
||||
closeWindow: 'विंडो बंद करें',
|
||||
edit: 'संपादन',
|
||||
undo: 'पूर्ववत करें',
|
||||
redo: 'फिर से करें',
|
||||
delete: 'हटाएँ',
|
||||
view: 'दृश्य',
|
||||
reload: 'पुनः लोड करें',
|
||||
forceReload: 'बलपूर्वक पुनः लोड करें',
|
||||
toggleDevTools: 'डेवलपर टूल',
|
||||
actualSize: 'वास्तविक आकार',
|
||||
zoomIn: 'ज़ूम इन',
|
||||
zoomOut: 'ज़ूम आउट',
|
||||
fullscreen: 'पूर्ण स्क्रीन',
|
||||
},
|
||||
'zh-TW': {
|
||||
window: '視窗',
|
||||
minimize: '最小化',
|
||||
closeWindow: '關閉視窗',
|
||||
edit: '編輯',
|
||||
undo: '復原',
|
||||
redo: '重做',
|
||||
delete: '刪除',
|
||||
view: '檢視',
|
||||
reload: '重新載入',
|
||||
forceReload: '強制重新載入',
|
||||
toggleDevTools: '開發人員工具',
|
||||
actualSize: '實際大小',
|
||||
zoomIn: '放大',
|
||||
zoomOut: '縮小',
|
||||
fullscreen: '全螢幕',
|
||||
},
|
||||
}
|
||||
|
||||
export function appMenuLabels(lang: string): AppMenuLabels {
|
||||
return { ...contextMenuLabels(lang), ...(LABELS[lang] ?? EN) }
|
||||
}
|
||||
|
||||
/** macOS keeps the native role (Minimize/Zoom/Front, window list); Windows/Linux
|
||||
* gets only conventional items — no Zoom/Front, and no Ctrl+M accelerator since
|
||||
* Windows has no menu shortcut for minimize. */
|
||||
export function windowMenuTemplate(
|
||||
platform: NodeJS.Platform,
|
||||
labels: AppMenuLabels,
|
||||
): MenuItemConstructorOptions {
|
||||
if (platform === 'darwin') return { role: 'windowMenu', label: labels.window }
|
||||
return {
|
||||
label: labels.window,
|
||||
submenu: [
|
||||
{ label: labels.minimize, click: (_item, win) => win?.minimize() },
|
||||
{ type: 'separator' },
|
||||
{ label: labels.closeWindow, click: (_item, win) => win?.close() },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/** macOS keeps role:'editMenu' (Speech/Substitutions submenus etc.); elsewhere
|
||||
* the same items Electron would generate, with localized labels. */
|
||||
export function editMenuTemplate(
|
||||
platform: NodeJS.Platform,
|
||||
labels: AppMenuLabels,
|
||||
): MenuItemConstructorOptions {
|
||||
if (platform === 'darwin') return { role: 'editMenu', label: labels.edit }
|
||||
return {
|
||||
label: labels.edit,
|
||||
submenu: [
|
||||
{ role: 'undo', label: labels.undo },
|
||||
{ role: 'redo', label: labels.redo },
|
||||
{ type: 'separator' },
|
||||
{ role: 'cut', label: labels.cut },
|
||||
{ role: 'copy', label: labels.copy },
|
||||
{ role: 'paste', label: labels.paste },
|
||||
{ role: 'delete', label: labels.delete },
|
||||
{ type: 'separator' },
|
||||
{ role: 'selectAll', label: labels.selectAll },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/** role:'viewMenu' expands identically on every platform, so no branch. */
|
||||
export function viewMenuTemplate(labels: AppMenuLabels): MenuItemConstructorOptions {
|
||||
return {
|
||||
label: labels.view,
|
||||
submenu: [
|
||||
{ role: 'reload', label: labels.reload },
|
||||
{ role: 'forceReload', label: labels.forceReload },
|
||||
{ role: 'toggleDevTools', label: labels.toggleDevTools },
|
||||
{ type: 'separator' },
|
||||
{ role: 'resetZoom', label: labels.actualSize },
|
||||
{ role: 'zoomIn', label: labels.zoomIn },
|
||||
{ role: 'zoomOut', label: labels.zoomOut },
|
||||
{ type: 'separator' },
|
||||
{ role: 'togglefullscreen', label: labels.fullscreen },
|
||||
],
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/// 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()
|
||||
}
|
||||
@@ -1,3 +1,17 @@
|
||||
export {
|
||||
buildContextMenuItems,
|
||||
contextMenuLabels,
|
||||
installContextMenu,
|
||||
type ContextMenuItem,
|
||||
type ContextMenuLabels,
|
||||
} from './context-menu'
|
||||
export {
|
||||
appMenuLabels,
|
||||
editMenuTemplate,
|
||||
viewMenuTemplate,
|
||||
windowMenuTemplate,
|
||||
type AppMenuLabels,
|
||||
} from './app-menu'
|
||||
export { installNavigationGuard } from './navigation-guard'
|
||||
export { safeExternalUrl, type SafeExternalUrlOptions } from './safe-external-url'
|
||||
export {
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import {
|
||||
appMenuLabels,
|
||||
editMenuTemplate,
|
||||
viewMenuTemplate,
|
||||
windowMenuTemplate,
|
||||
type AppMenuLabels,
|
||||
} from '../src/index'
|
||||
|
||||
const LANGS = [
|
||||
'zh',
|
||||
'en',
|
||||
'ja',
|
||||
'ko',
|
||||
'fr',
|
||||
'de',
|
||||
'es',
|
||||
'th',
|
||||
'id',
|
||||
'ru',
|
||||
'ar',
|
||||
'pt',
|
||||
'it',
|
||||
'pl',
|
||||
'nl',
|
||||
'ms',
|
||||
'he',
|
||||
'hi',
|
||||
'zh-TW',
|
||||
] as const
|
||||
|
||||
const en = appMenuLabels('en')
|
||||
|
||||
type Item = { role?: string; type?: string; label?: string; accelerator?: string }
|
||||
const submenuOf = (tpl: { submenu?: unknown }): Item[] => tpl.submenu as Item[]
|
||||
|
||||
describe('appMenuLabels', () => {
|
||||
it('covers all 19 languages with every key non-empty', () => {
|
||||
const keys = Object.keys(en) as (keyof AppMenuLabels)[]
|
||||
for (const lang of LANGS) {
|
||||
const labels = appMenuLabels(lang)
|
||||
for (const key of keys) expect(labels[key], `${lang}.${key}`).toBeTruthy()
|
||||
}
|
||||
})
|
||||
|
||||
it('falls back to English for unknown languages', () => {
|
||||
expect(appMenuLabels('xx')).toEqual(en)
|
||||
})
|
||||
|
||||
it('merges the context-menu clipboard labels', () => {
|
||||
const zh = appMenuLabels('zh')
|
||||
expect(zh.copy).toBe('复制')
|
||||
expect(zh.selectAll).toBe('全选')
|
||||
})
|
||||
})
|
||||
|
||||
describe('windowMenuTemplate', () => {
|
||||
it('keeps the native role on macOS', () => {
|
||||
expect(windowMenuTemplate('darwin', en)).toEqual({ role: 'windowMenu', label: 'Window' })
|
||||
})
|
||||
|
||||
it.each(['win32', 'linux'] as const)('builds Minimize/Close only on %s', (platform) => {
|
||||
const tpl = windowMenuTemplate(platform, en)
|
||||
expect(tpl.role).toBeUndefined()
|
||||
expect(tpl.label).toBe('Window')
|
||||
const items = submenuOf(tpl)
|
||||
expect(items.map((i) => i.label ?? i.type)).toEqual(['Minimize', 'separator', 'Close Window'])
|
||||
// no mac-only entries, no roles, no Ctrl+M
|
||||
for (const item of items) {
|
||||
expect(item.role).toBeUndefined()
|
||||
expect(item.accelerator).toBeUndefined()
|
||||
expect(item.label ?? '').not.toMatch(/zoom|front/i)
|
||||
}
|
||||
})
|
||||
|
||||
it('localizes the Windows submenu', () => {
|
||||
const items = submenuOf(windowMenuTemplate('win32', appMenuLabels('zh')))
|
||||
expect(items[0]!.label).toBe('最小化')
|
||||
expect(items[2]!.label).toBe('关闭窗口')
|
||||
})
|
||||
})
|
||||
|
||||
describe('editMenuTemplate', () => {
|
||||
it('keeps the native role on macOS', () => {
|
||||
expect(editMenuTemplate('darwin', en)).toEqual({ role: 'editMenu', label: 'Edit' })
|
||||
})
|
||||
|
||||
it('mirrors the non-mac role expansion with labeled roles on Windows', () => {
|
||||
const tpl = editMenuTemplate('win32', en)
|
||||
expect(tpl.label).toBe('Edit')
|
||||
expect(submenuOf(tpl).map((i) => i.role ?? i.type)).toEqual([
|
||||
'undo',
|
||||
'redo',
|
||||
'separator',
|
||||
'cut',
|
||||
'copy',
|
||||
'paste',
|
||||
'delete',
|
||||
'separator',
|
||||
'selectAll',
|
||||
])
|
||||
for (const item of submenuOf(tpl)) if (item.role) expect(item.label).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('viewMenuTemplate', () => {
|
||||
it('mirrors the viewMenu role expansion with labeled roles', () => {
|
||||
const tpl = viewMenuTemplate(en)
|
||||
expect(tpl.label).toBe('View')
|
||||
expect(submenuOf(tpl).map((i) => i.role ?? i.type)).toEqual([
|
||||
'reload',
|
||||
'forceReload',
|
||||
'toggleDevTools',
|
||||
'separator',
|
||||
'resetZoom',
|
||||
'zoomIn',
|
||||
'zoomOut',
|
||||
'separator',
|
||||
'togglefullscreen',
|
||||
])
|
||||
for (const item of submenuOf(tpl)) if (item.role) expect(item.label).toBeTruthy()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,95 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { buildContextMenuItems, contextMenuLabels } from '../src/index'
|
||||
|
||||
const labels = contextMenuLabels('en')
|
||||
|
||||
const flags = (
|
||||
overrides: Partial<{ canCut: boolean; canCopy: boolean; canPaste: boolean }> = {},
|
||||
) => ({
|
||||
canUndo: false,
|
||||
canRedo: false,
|
||||
canCut: true,
|
||||
canCopy: true,
|
||||
canPaste: true,
|
||||
canDelete: false,
|
||||
canSelectAll: true,
|
||||
canEditRichly: false,
|
||||
...overrides,
|
||||
})
|
||||
|
||||
const base = {
|
||||
isEditable: false,
|
||||
selectionText: '',
|
||||
misspelledWord: '',
|
||||
dictionarySuggestions: [] as string[],
|
||||
editFlags: flags(),
|
||||
}
|
||||
|
||||
describe('buildContextMenuItems', () => {
|
||||
it('builds the full edit set for editable targets', () => {
|
||||
const items = buildContextMenuItems({ ...base, isEditable: true }, labels)
|
||||
expect(items).toEqual([
|
||||
{ action: 'cut', label: 'Cut', enabled: true },
|
||||
{ action: 'copy', label: 'Copy', enabled: true },
|
||||
{ action: 'paste', label: 'Paste', enabled: true },
|
||||
{ type: 'separator' },
|
||||
{ action: 'selectAll', label: 'Select All', enabled: true },
|
||||
])
|
||||
})
|
||||
|
||||
it('disables items per editFlags', () => {
|
||||
const items = buildContextMenuItems(
|
||||
{ ...base, isEditable: true, editFlags: flags({ canCut: false, canPaste: false }) },
|
||||
labels,
|
||||
)
|
||||
expect(items).toContainEqual({ action: 'cut', label: 'Cut', enabled: false })
|
||||
expect(items).toContainEqual({ action: 'paste', label: 'Paste', enabled: false })
|
||||
})
|
||||
|
||||
it('prepends spelling suggestions when a word is misspelled', () => {
|
||||
const items = buildContextMenuItems(
|
||||
{
|
||||
...base,
|
||||
isEditable: true,
|
||||
misspelledWord: 'helo',
|
||||
dictionarySuggestions: ['hello', 'halo'],
|
||||
},
|
||||
labels,
|
||||
)
|
||||
expect(items.slice(0, 3)).toEqual([
|
||||
{ action: 'replaceMisspelling', label: 'hello' },
|
||||
{ action: 'replaceMisspelling', label: 'halo' },
|
||||
{ type: 'separator' },
|
||||
])
|
||||
})
|
||||
|
||||
it('skips the suggestion block when there are no suggestions', () => {
|
||||
const items = buildContextMenuItems(
|
||||
{ ...base, isEditable: true, misspelledWord: 'zqx' },
|
||||
labels,
|
||||
)
|
||||
expect(items[0]).toEqual({ action: 'cut', label: 'Cut', enabled: true })
|
||||
})
|
||||
|
||||
it('offers copy/select-all for non-editable selections', () => {
|
||||
const items = buildContextMenuItems({ ...base, selectionText: 'some text' }, labels)
|
||||
expect(items).toEqual([
|
||||
{ action: 'copy', label: 'Copy', enabled: true },
|
||||
{ action: 'selectAll', label: 'Select All', enabled: true },
|
||||
])
|
||||
})
|
||||
|
||||
it('returns nothing for non-editable targets without selection', () => {
|
||||
expect(buildContextMenuItems(base, labels)).toEqual([])
|
||||
expect(buildContextMenuItems({ ...base, selectionText: ' ' }, labels)).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('contextMenuLabels', () => {
|
||||
it('covers every language and falls back to English', () => {
|
||||
expect(contextMenuLabels('zh').copy).toBe('复制')
|
||||
expect(contextMenuLabels('zh-TW').paste).toBe('貼上')
|
||||
expect(contextMenuLabels('xx')).toEqual(contextMenuLabels('en'))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user