贡献者 PR #600(feat/editor-draft-protection,head a14c1e8)以 --no-ff 落到 dev 基线(9b97e6f)。贡献者的提交原样保留,冲突解决体现为本次合并结果。 保留(PR 独有): - 草稿保护:src/client/editor-dirty.ts(脏稿登记表 + confirmDiscardDraft)、EditorHost 的登记/卸载清理、Sidebar 的 closeTab 门与 beforeunload、closePathTabs 的确认参数。 - 保存冲突门:fs.read 回报 mtimeMs、fs.write 的 expectedMtimeMs(409 fs-conflict,在写 临时文件之前判定)、TextEditor 的 mtimeRef + 冲突横幅 + onReload、wire 的 fs-conflict 码,以及 fs-write-route / text-editor-conflict 两个新 spec。 - 查找/替换入口:TextEditor 工具栏放大镜按钮(打开 #587 的面板;可编辑视图的面板自带 替换行),文案用 #587 的 searchFind。 删除(与 #587 /2e22b90重复的部分): - TextEditor 里重复的那份查找接线:本地 searchPhrases()、EditorState.phrases.of(...)、 search({ top: true })、编辑器 keymap 里的 ...searchKeymap,以及 search / searchKeymap 两个 import——#587 的 src/client/cm-search.ts 已提供同一套(面板、keymap、phrases 隔间、 令牌主题)。 - 20 份词典里被 #587 取代的 10 个键:find / replace / findNext / findPrevious / findAll / findMatchCase / findRegexp / findByWord / replaceOne / replaceAll(命名以先落的 #587 为准: searchFind / searchReplace / searchNext / searchPrevious / searchAll / searchMatchCase / searchWholeWord / searchRegexp / searchReplaceAll)。 - src/index.ts 里已被2e22b90覆盖的临时文件改名(.dsh-write-<uuid>):保留 dev 的 .dsh-sidebar-tmp-<uuid>,tests/fs-write-route.spec.ts 的残留断言改指 dev 命名。 - tests/text-editor-search.spec.tsx 中与 editor-find.spec.tsx 重复的三个用例(en/zh 文案、 Mod-f),只留「工具栏按钮开面板并切到编辑态」这一个独有用例。 - README / README_EN / 指南里未实现的 Ctrl/Cmd + H 声明(PR 从未绑定 Mod-h,且浏览器保留 该组合键),改为如实描述「Ctrl/Cmd + F 的面板含替换行」,并补上 #587 已支持的 Ctrl/Cmd + G / Shift + Ctrl/Cmd + G / Ctrl/Cmd + D。 其它解冲突取舍(dev 上下文已漂移,不复活 PR 基线代码): - 图标:IconSearchOutline16 在宿主图标整族改名后已不存在,换为 IconSearchOutlineRegular size={16}(显式尺寸;旧 16 后缀已删,不传会按新默认尺寸)。 - 采纳 dev 侧:fs.read 的 ensureWorkspacePath 两参调用、truncation 横幅不带 mode 条件、 fs.write 的编码探测与 invalidateDirectoryCache、Sidebar 的 closeTab 里 PR 基线的终端 pty 释放块(dev 已移走)与 summaryCwd/pushedBottomHeight/collapsed 块(dev 已删)。 - 测试 harness 跟随 dev:ctx.sidebarRight.mounted、bottomSplits、先打开底部工作台、 apply() 需要的 ctx.on、api 模块改为部分 mock(保留 sessionPhase / sessionCwd)。 - 取舍记录写入 docs/plans/2026-09-09-editor-draft-protection-design.md。 Refs #600
104 lines
4.0 KiB
TypeScript
104 lines
4.0 KiB
TypeScript
/**
|
|
* The built-in text editor's find/replace TOOLBAR affordance: the header's
|
|
* magnifier button opens CodeMirror's search panel — the same panel the
|
|
* Ctrl/Cmd+F keymap opens (find-in-file wiring itself is pinned by
|
|
* editor-find.spec.tsx, which owns the extensions, the keymap and the panel
|
|
* copy) — and the panel carries the REPLACE row, so the button reaches both
|
|
* halves of the find/replace surface. The button also flips a markdown
|
|
* preview to edit first, since the panel lives in the hidden CodeMirror
|
|
* surface.
|
|
*/
|
|
// @vitest-environment jsdom
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
|
import { createElement } from 'react'
|
|
import { act } from 'react-dom/test-utils'
|
|
import { renderRoot, setupReactAct } from './test-utils.ts'
|
|
import { TextEditor } from '../src/client/TextEditor.tsx'
|
|
import { attachLocale } from '../src/client/locales.ts'
|
|
import { createSidebarStore } from '../src/client/state.ts'
|
|
import type { FileViewerProps } from '../src/client/service.ts'
|
|
|
|
setupReactAct()
|
|
|
|
/** Minimal structural fake of the DSH LocaleService face the sidebar uses. */
|
|
class FakeLocale {
|
|
active: string = 'en'
|
|
getSnapshot(): { active: string } {
|
|
return { active: this.active }
|
|
}
|
|
subscribe(_fn: () => void): () => void {
|
|
return () => {}
|
|
}
|
|
}
|
|
|
|
function viewerProps(overrides: Partial<FileViewerProps> = {}): FileViewerProps {
|
|
return {
|
|
ctx: {} as FileViewerProps['ctx'],
|
|
store: createSidebarStore(),
|
|
scope: { sessionId: 's1', cwd: '/p' },
|
|
path: '/p/a.md',
|
|
title: 'a.md',
|
|
viewerId: 'markdown',
|
|
content: '# Title\n\nneedle here\n\nneedle again\n',
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function click(element: HTMLElement): void {
|
|
act(() => { element.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
|
|
}
|
|
|
|
/** The toolbar's search button (locale-dependent aria-label). */
|
|
function searchButton(container: HTMLDivElement): HTMLButtonElement {
|
|
const button = container.querySelector('button[aria-label="Find"], button[aria-label="查找"]')
|
|
if (button === null) throw new Error('search button not found')
|
|
return button as HTMLButtonElement
|
|
}
|
|
|
|
/** The CodeMirror search panel, or null while closed. */
|
|
function searchPanel(container: HTMLDivElement): HTMLElement | null {
|
|
return container.querySelector('.cm-search')
|
|
}
|
|
|
|
/** The header's preview/edit toggle buttons, keyed by their label. */
|
|
function modeButton(container: HTMLDivElement, label: string): HTMLButtonElement {
|
|
const button = Array.from(container.querySelectorAll('button'))
|
|
.find(candidate => candidate.textContent === label)
|
|
if (button === undefined) throw new Error(`mode button "${label}" not found`)
|
|
return button
|
|
}
|
|
|
|
/** The active mode toggle carries one extra class (the "active" marker). */
|
|
function activeMode(container: HTMLDivElement): string {
|
|
const preview = modeButton(container, 'Preview')
|
|
const edit = modeButton(container, 'Edit')
|
|
return edit.classList.length > preview.classList.length ? 'edit' : 'preview'
|
|
}
|
|
|
|
afterEach(() => {
|
|
attachLocale(undefined)
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
describe('TextEditor find/replace toolbar button', () => {
|
|
it('opens the search panel, replace row included, from the toolbar button', () => {
|
|
attachLocale(new FakeLocale())
|
|
const mounted = renderRoot(createElement(TextEditor, viewerProps()))
|
|
try {
|
|
expect(searchPanel(mounted.container)).toBeNull()
|
|
// The markdown viewer starts in preview, where the CodeMirror surface
|
|
// (and therefore the panel) is hidden — the button must flip first.
|
|
expect(activeMode(mounted.container)).toBe('preview')
|
|
click(searchButton(mounted.container))
|
|
const panel = searchPanel(mounted.container)
|
|
expect(panel).not.toBeNull()
|
|
// The button reaches the REPLACE half too (an editable view's panel
|
|
// renders the second field; a read-only one would not).
|
|
expect(panel!.querySelector('input[name="replace"]')).not.toBeNull()
|
|
expect(activeMode(mounted.container)).toBe('edit')
|
|
} finally {
|
|
mounted.unmount()
|
|
}
|
|
})
|
|
})
|