feat(menus): 编辑区自绘右键菜单 + 全局屏蔽 WebView 自带菜单(docs/26,含 docs/21 勘误)

This commit is contained in:
Muse
2026-11-14 16:46:39 +08:00
parent bd6fcf3d45
commit bfacdd5fbb
7 changed files with 215 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
// 编辑区右键菜单:条目随模式/选区变化,点击回传动作并关闭。
import { describe, it, expect, vi } from 'vitest'
import { createRoot } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import EditorContextMenu from '../src/components/EditorContextMenu'
async function renderMenu(props: Record<string, unknown> = {}) {
const container = document.createElement('div')
document.body.appendChild(container)
const root = createRoot(container)
const actions: string[] = []
let closed = 0
await act(async () => {
root.render(
<EditorContextMenu
x={10} y={10} hasSelection={false} sourceMode={false}
onAction={(a) => actions.push(a)}
onClose={() => { closed += 1 }}
{...props}
/>,
)
})
return { container, root, actions, closedCount: () => closed }
}
const btn = (c: HTMLElement, text: string) => [...c.querySelectorAll('button')].find((b) => b.textContent === text)
describe('编辑区右键菜单', () => {
it('无选区时剪切/复制禁用,点加粗回传 bold 并关闭', async () => {
const { container, root, actions, closedCount } = await renderMenu()
expect(btn(container, '剪切')?.disabled).toBe(true)
expect(btn(container, '复制')?.disabled).toBe(true)
expect(btn(container, '粘贴')?.disabled).toBe(false)
await act(async () => { btn(container, '加粗')?.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
expect(actions).toEqual(['bold'])
expect(closedCount()).toBe(1)
await act(async () => { root.unmount() })
container.remove()
})
it('源码模式只出编辑命令与「即时渲染」', async () => {
const { container, root } = await renderMenu({ sourceMode: true, hasSelection: true })
expect(btn(container, '加粗')).toBeUndefined()
expect(btn(container, '即时渲染')).toBeTruthy()
expect(btn(container, '剪切')?.disabled).toBe(false)
await act(async () => { root.unmount() })
container.remove()
})
})