mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-03 02:11:07 +05:30
feat(renderer): register menu commands in AppShell (Phase 6 wiring)
- useRegisterMenuCommands: registers file.open, file.save, file.closeTab, tab.next, tab.prev, view.toggleSidebar, view.togglePreview in command store - useBridgeNativeMenu: useMenuAction for 13 native-menu channels (file-save, toggle-preview, load-template-menu, etc.) - AppShell calls both on mount - integration test mock extended to include ipc.menu.on - 145/145 tests pass (was 105)
This commit is contained in:
@@ -10,10 +10,13 @@ import { useAppStore } from '@/stores/app-store';
|
||||
import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from '@/components/ui/resizable';
|
||||
import { useFileShortcuts } from '@/hooks/use-file-shortcuts';
|
||||
import { useRestoreLastFolder } from '@/hooks/use-restore-last-folder';
|
||||
import { useRegisterMenuCommands, useBridgeNativeMenu } from '@/lib/commands/register-menu-commands';
|
||||
|
||||
export function AppShell() {
|
||||
useFileShortcuts();
|
||||
useRestoreLastFolder();
|
||||
useRegisterMenuCommands();
|
||||
useBridgeNativeMenu();
|
||||
const { sidebarVisible, previewVisible, paneSizes, setPaneSizes } = useAppStore();
|
||||
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useCommandStore } from '@/stores/command-store';
|
||||
import { useFileStore } from '@/stores/file-store';
|
||||
import { useAppStore } from '@/stores/app-store';
|
||||
import { useMenuAction } from '@/hooks/use-menu-action';
|
||||
|
||||
/**
|
||||
* Register all Phase 6 menu commands in the command store, and bridge
|
||||
* the native menu IPC channels to the matching command ids.
|
||||
*
|
||||
* Phase 6 scope: file/view/tab commands with direct store mappings.
|
||||
* Phase 7+ will add the dialog-driven commands (export, settings, etc.)
|
||||
* once the corresponding modals exist.
|
||||
*/
|
||||
export function useRegisterMenuCommands(): void {
|
||||
// Register handlers in the command store.
|
||||
useEffect(() => {
|
||||
const { register } = useCommandStore.getState();
|
||||
register('file.open', () => {
|
||||
void useFileStore.getState().openFileDialog();
|
||||
});
|
||||
register('file.openFolder', () => {
|
||||
void useFileStore.getState().openFolderDialog();
|
||||
});
|
||||
register('file.save', () => {
|
||||
void useFileStore.getState().saveActiveBuffer();
|
||||
});
|
||||
register('file.closeTab', () => {
|
||||
const { activeTabId, closeTab } = useFileStore.getState();
|
||||
if (activeTabId) closeTab(activeTabId);
|
||||
});
|
||||
register('tab.next', () => {
|
||||
const { openTabs, activeTabId, setActiveTab } = useFileStore.getState();
|
||||
if (openTabs.length === 0) return;
|
||||
const idx = activeTabId ? openTabs.findIndex((t) => t.id === activeTabId) : 0;
|
||||
const safeIdx = idx === -1 ? 0 : idx;
|
||||
const nextIdx = safeIdx >= openTabs.length - 1 ? 0 : safeIdx + 1;
|
||||
setActiveTab(openTabs[nextIdx].id);
|
||||
});
|
||||
register('tab.prev', () => {
|
||||
const { openTabs, activeTabId, setActiveTab } = useFileStore.getState();
|
||||
if (openTabs.length === 0) return;
|
||||
const idx = activeTabId ? openTabs.findIndex((t) => t.id === activeTabId) : 0;
|
||||
const safeIdx = idx === -1 ? 0 : idx;
|
||||
const nextIdx = safeIdx <= 0 ? openTabs.length - 1 : safeIdx - 1;
|
||||
setActiveTab(openTabs[nextIdx].id);
|
||||
});
|
||||
register('view.toggleSidebar', () => useAppStore.getState().toggleSidebar());
|
||||
register('view.togglePreview', () => useAppStore.getState().togglePreview());
|
||||
}, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wire native-menu IPC channels to the command store.
|
||||
* Channel names match what main.js dispatches via webContents.send.
|
||||
*/
|
||||
export function useBridgeNativeMenu(): void {
|
||||
useMenuAction('file-save', 'file.save');
|
||||
useMenuAction('toggle-preview', 'view.togglePreview');
|
||||
useMenuAction('toggle-command-palette', 'command.palette');
|
||||
useMenuAction('toggle-sidebar-panel', 'view.sidebarPanel', (panel) => panel as string);
|
||||
useMenuAction('toggle-bottom-panel', 'view.bottomPanel');
|
||||
useMenuAction('toggle-find', 'find.toggle');
|
||||
useMenuAction('undo', 'editor.undo');
|
||||
useMenuAction('redo', 'editor.redo');
|
||||
useMenuAction('adjust-font-size', 'font.size', (direction) => direction as string);
|
||||
useMenuAction('load-custom-css', 'theme.loadCustomCss');
|
||||
useMenuAction('clear-custom-css', 'theme.clearCustomCss');
|
||||
useMenuAction('load-template-menu', 'template.load', (name) => name as string);
|
||||
useMenuAction('print-preview', 'print.preview');
|
||||
useMenuAction('print-preview-styled', 'print.previewStyled');
|
||||
useMenuAction('file-opened', 'file.opened', (payload) => payload);
|
||||
useMenuAction('clear-recent-files', 'file.clearRecent');
|
||||
}
|
||||
Reference in New Issue
Block a user