|
| 1 | +import {SearchQuery, type SearchResult, getSearchState, setSearchState} from 'prosemirror-search'; |
| 2 | + |
| 3 | +import type {Node} from '#pm/model'; |
| 4 | +import {type Command, Plugin, type PluginView, TextSelection} from '#pm/state'; |
| 5 | +import {findParentNodeClosestToPos} from '#pm/utils'; |
| 6 | +import type {EditorView} from '#pm/view'; |
| 7 | +import {type SearchCounter, type SearchState, renderSearchPopup} from 'src/modules/search'; |
| 8 | + |
| 9 | +import {getReactRendererFromState} from '../ReactRenderer'; |
| 10 | + |
| 11 | +import {closeSearch, findNext, findPrev, replaceAll, replaceNext} from './commands'; |
| 12 | +import {pluginKey} from './const'; |
| 13 | +import {searchKeyHandler} from './key-handler'; |
| 14 | +import type {SearchViewState} from './types'; |
| 15 | +import {startTracking} from './utils/connect-tracker'; |
| 16 | +import {FocusManager} from './utils/focus-manager'; |
| 17 | +import {getCounter} from './utils/search-counter'; |
| 18 | + |
| 19 | +import './search-plugin.scss'; |
| 20 | + |
| 21 | +export interface SearchViewPluginParams { |
| 22 | + anchorSelector: string; |
| 23 | +} |
| 24 | + |
| 25 | +export const searchViewPlugin = (params: SearchViewPluginParams) => { |
| 26 | + return new Plugin<SearchViewState>({ |
| 27 | + key: pluginKey, |
| 28 | + props: { |
| 29 | + handleKeyDown: searchKeyHandler, |
| 30 | + }, |
| 31 | + state: { |
| 32 | + init: () => ({open: false}), |
| 33 | + apply(tr, value, _oldState, _newState) { |
| 34 | + const newValue = tr.getMeta(pluginKey) as SearchViewState | undefined; |
| 35 | + if (typeof newValue === 'object') return newValue; |
| 36 | + return value; |
| 37 | + }, |
| 38 | + }, |
| 39 | + view(view) { |
| 40 | + return new SeachPluginView(view, params); |
| 41 | + }, |
| 42 | + }); |
| 43 | +}; |
| 44 | + |
| 45 | +class SeachPluginView implements PluginView { |
| 46 | + private readonly _view: EditorView; |
| 47 | + private readonly _renderer; |
| 48 | + private readonly _focusManager: FocusManager; |
| 49 | + private readonly _viewDomTrackerDispose; |
| 50 | + |
| 51 | + private _counter: SearchCounter; |
| 52 | + private _isDomConnected: boolean; |
| 53 | + private _viewState: SearchViewState | undefined; |
| 54 | + private _searchState: SearchQuery | undefined; |
| 55 | + |
| 56 | + constructor(view: EditorView, params: SearchViewPluginParams) { |
| 57 | + this._view = view; |
| 58 | + this._viewState = pluginKey.getState(view.state); |
| 59 | + this._searchState = getSearchState(view.state)?.query; |
| 60 | + this._counter = getCounter(view.state); |
| 61 | + this._isDomConnected = view.dom.ownerDocument.contains(view.dom); |
| 62 | + |
| 63 | + this._renderer = this._createRenderer(params); |
| 64 | + this._focusManager = new FocusManager(view.dom.ownerDocument); |
| 65 | + |
| 66 | + // uses MutationObserver to detect when view.dom is disconnected from the DOM tree |
| 67 | + // TODO: replace with eventBus (subscribe to change-editor-mode event) to track when to hide the search bar |
| 68 | + // see https://github.com/gravity-ui/markdown-editor/issues/884 |
| 69 | + this._viewDomTrackerDispose = startTracking(view.dom, { |
| 70 | + onConnect: this._onEditorViewDomConnected, |
| 71 | + onDisconnect: this._onEditorViewDomDisconnected, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + update() { |
| 76 | + const newCounter = getCounter(this._view.state); |
| 77 | + const newViewState = pluginKey.getState(this._view.state); |
| 78 | + const newSearchState = getSearchState(this._view.state)?.query; |
| 79 | + |
| 80 | + if ( |
| 81 | + newViewState !== this._viewState || |
| 82 | + newSearchState !== this._searchState || |
| 83 | + newCounter.total !== this._counter.total || |
| 84 | + newCounter.current !== this._counter.current |
| 85 | + ) { |
| 86 | + this._counter = newCounter; |
| 87 | + this._viewState = newViewState; |
| 88 | + this._searchState = newSearchState; |
| 89 | + this._renderer.rerender(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + destroy() { |
| 94 | + this._renderer.remove(); |
| 95 | + this._viewDomTrackerDispose(); |
| 96 | + } |
| 97 | + |
| 98 | + private _onEditorViewDomConnected = () => { |
| 99 | + this._isDomConnected = true; |
| 100 | + this._renderer.rerender(); |
| 101 | + }; |
| 102 | + |
| 103 | + private _onEditorViewDomDisconnected = () => { |
| 104 | + this._isDomConnected = false; |
| 105 | + this._onClose(); |
| 106 | + }; |
| 107 | + |
| 108 | + private _createRenderer(params: Pick<SearchViewPluginParams, 'anchorSelector'>) { |
| 109 | + return getReactRendererFromState(this._view.state).createItem('search-view', () => { |
| 110 | + const { |
| 111 | + _viewState: viewState, |
| 112 | + _searchState: searchState, |
| 113 | + _isDomConnected: domConnected, |
| 114 | + } = this; |
| 115 | + |
| 116 | + if (!domConnected || !viewState?.open || !searchState) return null; |
| 117 | + |
| 118 | + const anchor = this._view.dom.ownerDocument.querySelector(params.anchorSelector); |
| 119 | + |
| 120 | + return renderSearchPopup({ |
| 121 | + anchor, |
| 122 | + open: viewState.open, |
| 123 | + counter: this._counter, |
| 124 | + state: searchState, |
| 125 | + onClose: this._onClose, |
| 126 | + onChange: this._onChange, |
| 127 | + onSearchPrev: this._onSearchPrev, |
| 128 | + onSearchNext: this._onSearchNext, |
| 129 | + onReplaceNext: this._onReplaceNext, |
| 130 | + onReplaceAll: this._onReplaceAll, |
| 131 | + }); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private _onChange = (config: SearchState) => { |
| 136 | + const {state, dispatch} = this._view; |
| 137 | + |
| 138 | + const query = new SearchQuery({ |
| 139 | + search: config.search, |
| 140 | + replace: config.replace, |
| 141 | + caseSensitive: config.caseSensitive, |
| 142 | + wholeWord: config.wholeWord, |
| 143 | + }); |
| 144 | + |
| 145 | + const tr = setSearchState(state.tr, query); |
| 146 | + const {$from, $to} = tr.selection; |
| 147 | + const parent = findParentNodeClosestToPos($from, (node: Node) => node.type.isTextblock); |
| 148 | + |
| 149 | + let result: SearchResult | null = null; |
| 150 | + // find match in [sel.$from, parent.$end] |
| 151 | + if (parent) result = query.findNext(state, $from.pos, parent.pos + parent.node.nodeSize); |
| 152 | + // find match in [parent.$start or sel.$from, doc.$end] |
| 153 | + if (!result) result = query.findNext(state, parent?.pos || $from.pos); |
| 154 | + // find match in [doc.$start, parent.$start or sel.$to] |
| 155 | + if (!result) result = query.findPrev(state, parent?.pos || $to.pos); |
| 156 | + // update text selection |
| 157 | + if (result) tr.setSelection(TextSelection.create(tr.doc, result.from, result.to)); |
| 158 | + |
| 159 | + dispatch(tr); |
| 160 | + }; |
| 161 | + |
| 162 | + private _onClose = () => { |
| 163 | + closeSearch(this._view.state, this._view.dispatch); |
| 164 | + this._view.focus(); |
| 165 | + }; |
| 166 | + |
| 167 | + private _onSearchPrev = () => { |
| 168 | + this._preserveFocus(findPrev); |
| 169 | + }; |
| 170 | + |
| 171 | + private _onSearchNext = () => { |
| 172 | + this._preserveFocus(findNext); |
| 173 | + }; |
| 174 | + |
| 175 | + private _onReplaceNext = () => { |
| 176 | + this._preserveFocus(replaceNext); |
| 177 | + }; |
| 178 | + |
| 179 | + private _onReplaceAll = () => { |
| 180 | + this._preserveFocus(replaceAll); |
| 181 | + }; |
| 182 | + |
| 183 | + private _preserveFocus(command: Command) { |
| 184 | + this._focusManager.storeFocus(); |
| 185 | + this._view.focus(); |
| 186 | + command(this._view.state, this._view.dispatch, this._view); |
| 187 | + this._focusManager.restoreFocus({preventScroll: true}); |
| 188 | + } |
| 189 | +} |
0 commit comments