82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
import { StrictMode } from 'react'
|
|
import { createRoot } from 'react-dom/client'
|
|
import { TooltipProvider } from '@/components/ui/tooltip'
|
|
import './index.css'
|
|
import App from './App.tsx'
|
|
import {
|
|
APP_COMMAND_EVENT_NAME,
|
|
isAppCommandId,
|
|
isNativeMenuCommandId,
|
|
} from './hooks/appCommandDispatcher'
|
|
import {
|
|
getShortcutEventInit,
|
|
type AppCommandShortcutEventInit,
|
|
type AppCommandShortcutEventOptions,
|
|
} from './hooks/appCommandCatalog'
|
|
|
|
// Disable native WebKit context menu in Tauri (WKWebView intercepts right-click
|
|
// at native level before React's synthetic events can call preventDefault).
|
|
// Capture phase fires first → prevents native menu; React bubble phase still fires
|
|
// → our custom context menus (e.g. sidebar right-click) work correctly.
|
|
if ('__TAURI__' in window || '__TAURI_INTERNALS__' in window) {
|
|
document.addEventListener('contextmenu', (e) => e.preventDefault(), true)
|
|
}
|
|
|
|
function dispatchDeterministicShortcutEvent(init: AppCommandShortcutEventInit) {
|
|
const target =
|
|
document.activeElement instanceof HTMLElement
|
|
? document.activeElement
|
|
: document.body ?? window
|
|
|
|
target.dispatchEvent(new KeyboardEvent('keydown', init))
|
|
}
|
|
|
|
window.__laputaTest = {
|
|
dispatchAppCommand(id: string) {
|
|
if (!isAppCommandId(id)) {
|
|
throw new Error(`Unknown app command: ${id}`)
|
|
}
|
|
window.dispatchEvent(new CustomEvent(APP_COMMAND_EVENT_NAME, { detail: id }))
|
|
},
|
|
dispatchShortcutEvent(init: AppCommandShortcutEventInit) {
|
|
dispatchDeterministicShortcutEvent(init)
|
|
},
|
|
async triggerMenuCommand(id: string) {
|
|
if (!isNativeMenuCommandId(id)) {
|
|
throw new Error(`Unknown native menu command: ${id}`)
|
|
}
|
|
|
|
if ('__TAURI__' in window || '__TAURI_INTERNALS__' in window) {
|
|
const { invoke } = await import('@tauri-apps/api/core')
|
|
return invoke('trigger_menu_command', { id })
|
|
}
|
|
|
|
if (!window.__laputaTest?.dispatchBrowserMenuCommand) {
|
|
throw new Error('Tolaria test bridge is missing dispatchBrowserMenuCommand')
|
|
}
|
|
|
|
window.__laputaTest.dispatchBrowserMenuCommand(id)
|
|
return undefined
|
|
},
|
|
triggerShortcutCommand(id: string, options?: AppCommandShortcutEventOptions) {
|
|
if (!isAppCommandId(id)) {
|
|
throw new Error(`Unknown app command: ${id}`)
|
|
}
|
|
|
|
const init = getShortcutEventInit(id, options)
|
|
if (!init) {
|
|
throw new Error(`Command ${id} does not define a keyboard shortcut`)
|
|
}
|
|
|
|
dispatchDeterministicShortcutEvent(init)
|
|
},
|
|
}
|
|
|
|
createRoot(document.getElementById('root')!).render(
|
|
<StrictMode>
|
|
<TooltipProvider>
|
|
<App />
|
|
</TooltipProvider>
|
|
</StrictMode>,
|
|
)
|