Files
apes-sidebar/src/client/cm-search.ts
T
suiyideali e2f53df484 feat(editor): 编辑器内查找——接入 @codemirror/search(Cmd/Ctrl+F)
- src/client/TextEditor.tsx: 共享扩展表新增 search({top:true}) + keymap.of(searchKeymap)
  (Mod-f 打开、面板固定编辑器顶部、Mod-g / Shift-Mod-g 下一个 / 上一个、Mod-Alt-g
  跳转到行、Escape 关闭)。搜索扩展与代码 / markdown 两个 viewer、只读「预览」与
  编辑模式共用同一份扩展表,不存在仅可编辑的分支;search 注册在编辑器自有 keymap
  之前,Escape 优先关面板(面板未开时 closeSearchPanel 返回 false,仍回落到
  simplifySelection),保存键 Mod-s 位置与优先级不变。
- src/client/cm-search.ts(新增): 面板 chrome 走 --dsw-* 令牌(CodeMirror 自带
  面板按 &light 上灰白配色,深色皮肤下会浮出一个白框);phrases facet 放进
  Compartment,语言切换就地 reconfigure(文档 / 撤销历史 / keymap / 滚动不动)。
- i18n: 新增 15 个 search* key——zh/en 在 locales.ts,其余 19 份词典全量补译
  (tests/locales.spec.ts 的 key-set 相等断言);locales.ts 新增 localeSignature(),
  TextEditor 同时订阅 DSH locale 与 better-locale override(tab-cell memo 不比较
  override,只靠父组件重渲染会漏掉 ja 切换)。
- README / README_EN: 快捷键表各加一行「文件内查找 / Find in file」。
- tests/editor-find.spec.tsx(新增): 扩展含 search keymap、Mod-f 开顶部面板、
  Escape 关闭、语言热切换后重开面板文案跟随、Mod-s 保存键仍生效。
- tests/e2e/mount.e2e.ts: 打开 seed 文件后补 Ctrl/Cmd+F → .cm-search 面板可见、
  Escape 关闭的真机断言(真实 Chromium + 懒加载 client-editor.js chunk)。

背景:package.json 里 @codemirror/search 一直是死依赖(src 零 import),编辑器
没有任何查找面板——Cmd+F 不被 CodeMirror 接管,长文件里浏览器原生查找只能命中
已渲染的行。本次只接入文件内查找,终端搜索(@xterm/addon-search)另开 PR。
2026-09-15 10:42:46 +08:00

142 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Find-in-file for the sidebar editor: CodeMirror's own search extension,
* wired into every TextEditor view (code and markdown alike, edit and
* preview mode — the extension lives in the shared base extension list, not
* in an editable-only branch).
*
* Three pieces:
* - `search({ top: true })` pins the panel to the TOP of the editor
* (CodeMirror's default is the bottom), so it reads like a browser/IDE
* find bar and never covers the caret's line.
* - `keymap.of(searchKeymap)` binds the upstream default keys: Mod-f opens
* the panel, Mod-g / Shift-Mod-g jump next/previous, Mod-Alt-g goes to a
* line, Escape closes. The editor's own keymap is registered AFTER this
* one, so upstream bindings never shadow it (the only shared key is
* Escape, and `closeSearchPanel` returns false when no panel is open —
* the editor's `simplifySelection` still runs then).
* - the panel's copy comes from the `phrases` facet, which CodeMirror reads
* at panel-build time. The facet is part of the EditorState, so a language
* switch must reconfigure it — {@link CmSearchPhrases} is that
* compartment (the document, history, and keymaps survive).
*
* The panel chrome is re-themed with the app's design tokens
* ({@link cmSearchTheme}): CodeMirror's stock panel paints its own light
* palette (`&light` selectors resolve as light because the editor theme does
* not declare `dark`), which would sit as a gray/white box inside a
* dark-mode sidebar. Only the token-driven chrome is overridden — the match
* highlight overlays stay CodeMirror's translucent defaults, which read on
* both schemes.
*/
import { Compartment, EditorState, type Extension, type StateEffect } from '@codemirror/state'
import { EditorView, keymap } from '@codemirror/view'
import { search, searchKeymap } from '@codemirror/search'
import { t } from './locales.ts'
/**
* CodeMirror's own phrase keys → this plugin's dictionary keys. The keys are
* the exact literals @codemirror/search looks up (see its SearchPanel /
* announceMatch / gotoLine panel); `$` inside a value is CodeMirror's
* placeholder, replaced with the caller's argument when the phrase is
* resolved (`EditorState.phrase`), so the dictionary entries keep it
* verbatim.
*/
function searchPhrases(): Record<string, string> {
return {
'Find': t('searchFind'),
'Replace': t('searchReplace'),
'next': t('searchNext'),
'previous': t('searchPrevious'),
'all': t('searchAll'),
'match case': t('searchMatchCase'),
'by word': t('searchWholeWord'),
'regexp': t('searchRegexp'),
'replace': t('searchReplace'),
'replace all': t('searchReplaceAll'),
// The close button is only an aria-label (the visible glyph is `×`) —
// the generic "close" key fits and needs no new dictionary entry.
'close': t('close'),
'current match': t('searchCurrentMatch'),
'on line': t('searchOnLine'),
'replaced $ matches': t('searchReplacedMatches'),
'replaced match on line $': t('searchReplacedMatchOnLine'),
'Go to line': t('searchGoToLine'),
'go': t('searchGo'),
}
}
/** The search panel chrome on design tokens (no hardcoded colors). */
export const cmSearchTheme = EditorView.theme({
'.cm-panels.cm-panels-top': {
backgroundColor: 'var(--dsw-alias-bg-layer-2)',
color: 'var(--dsw-alias-label-primary)',
borderBottom: '1px solid var(--dsw-alias-border-l1)',
},
'.cm-panel.cm-search': {
backgroundColor: 'transparent',
color: 'var(--dsw-alias-label-primary)',
},
'.cm-panel.cm-search input.cm-textfield': {
backgroundColor: 'var(--dsw-alias-bg-base)',
color: 'var(--dsw-alias-label-primary)',
border: '1px solid var(--dsw-alias-border-l1)',
borderRadius: '3px',
},
'.cm-panel.cm-search input.cm-textfield:focus': {
border: '1px solid var(--dsw-alias-brand-primary)',
outline: 'none',
},
'.cm-panel.cm-search button.cm-button': {
backgroundColor: 'var(--dsw-alias-interactive-bg-hover)',
backgroundImage: 'none',
color: 'var(--dsw-alias-label-primary)',
border: '1px solid var(--dsw-alias-border-l1)',
},
'.cm-panel.cm-search button.cm-button:hover': {
backgroundColor: 'var(--dsw-alias-interactive-bg-hover-accent)',
},
'.cm-panel.cm-search button.cm-button:active': {
backgroundColor: 'var(--dsw-alias-interactive-bg-active)',
backgroundImage: 'none',
},
'.cm-panel.cm-search [name=close]': {
color: 'var(--dsw-alias-label-secondary)',
cursor: 'pointer',
},
'.cm-panel.cm-search [name=close]:hover': {
color: 'var(--dsw-alias-label-primary)',
},
'.cm-panel.cm-search label': {
color: 'var(--dsw-alias-label-secondary)',
},
})
/**
* The scheme-independent part of the find-in-file wiring: the top-pinned
* panel, its theme, and the upstream search keymap. The localized phrases
* are added separately through {@link CmSearchPhrases} (they are the only
* piece a language switch reconfigures).
*/
export function cmSearchExtensions(): Extension[] {
return [search({ top: true }), cmSearchTheme, keymap.of(searchKeymap)]
}
/**
* A Compartment holding the localized `phrases` facet. Created once per
* editor view; a language switch dispatches `reconfigure()` on it, which
* rebuilds the panel copy on its next open without touching the document,
* undo history, or keymaps.
*/
export class CmSearchPhrases {
private readonly compartment = new Compartment()
/** `of(...)` payload for EditorState.create. */
of(): Extension {
return this.compartment.of(EditorState.phrases.of(searchPhrases()))
}
/** Re-resolve the panel copy for the current language. */
reconfigure(): StateEffect<unknown> {
return this.compartment.reconfigure(EditorState.phrases.of(searchPhrases()))
}
}