* 更新搜索高亮(将 ProseMirror 搜索状态映射到 CodeMirror 装饰)
()
| 1361 | * 更新搜索高亮(将 ProseMirror 搜索状态映射到 CodeMirror 装饰) |
| 1362 | */ |
| 1363 | private updateSearchHighlights(): void { |
| 1364 | const pos = this.getPos(); |
| 1365 | if (pos === undefined) return; |
| 1366 | |
| 1367 | const searchState = searchPluginKey.getState(this.view.state); |
| 1368 | if (!searchState || !searchState.query || searchState.matches.length === 0) { |
| 1369 | this.cm.dispatch({ |
| 1370 | effects: this.searchHighlightCompartment.reconfigure([]), |
| 1371 | }); |
| 1372 | return; |
| 1373 | } |
| 1374 | |
| 1375 | const nodeStart = pos + 1; |
| 1376 | const nodeEnd = pos + 1 + this.node.content.size; |
| 1377 | |
| 1378 | const cmRanges = searchState.matches |
| 1379 | .map((m, i) => ({ ...m, index: i })) |
| 1380 | .filter((m) => m.from >= nodeStart && m.to <= nodeEnd) |
| 1381 | .map((m) => { |
| 1382 | const cls = |
| 1383 | m.index === searchState.currentIndex |
| 1384 | ? "milkup-search-match milkup-search-match-current" |
| 1385 | : "milkup-search-match"; |
| 1386 | return CMDecoration.mark({ class: cls }).range(m.from - nodeStart, m.to - nodeStart); |
| 1387 | }); |
| 1388 | |
| 1389 | const decoSet = cmRanges.length > 0 ? CMDecoration.set(cmRanges, true) : CMDecoration.none; |
| 1390 | |
| 1391 | this.cm.dispatch({ |
| 1392 | effects: this.searchHighlightCompartment.reconfigure(EditorView.decorations.of(decoSet)), |
| 1393 | }); |
| 1394 | } |
| 1395 | |
| 1396 | /** |
| 1397 | * 设置语言 |