|
| 1 | +import { |
| 2 | + SearchQuery, |
| 3 | + findNext, |
| 4 | + findPrev, |
| 5 | + getSearchState, |
| 6 | + replaceAll, |
| 7 | + replaceNext, |
| 8 | + setSearchState, |
| 9 | +} from 'prosemirror-search'; |
| 10 | + |
| 11 | +import {keydownHandler} from '#pm/keymap'; |
| 12 | +import {type Command, Plugin, PluginKey, type PluginView} from '#pm/state'; |
| 13 | +import type {EditorView} from '#pm/view'; |
| 14 | +import {debounce} from 'src/lodash'; |
| 15 | +import {type SearchPopupProps, renderSearchPopup} from 'src/search/SearchPopup'; |
| 16 | + |
| 17 | +import {getReactRendererFromState} from '../ReactRenderer'; |
| 18 | + |
| 19 | +import 'prosemirror-search/style/search.css'; |
| 20 | + |
| 21 | +const pluginKey = new PluginKey<SearchViewState>('search-view'); |
| 22 | + |
| 23 | +type TrMeta = {open: false} | {open: true; search: string}; |
| 24 | +type SearchQueryConfig = ConstructorParameters<typeof SearchQuery>[0]; |
| 25 | + |
| 26 | +const openSearch: Command = (state, dispatch) => { |
| 27 | + const pluginState = pluginKey.getState(state); |
| 28 | + if (!pluginState?.open && dispatch) { |
| 29 | + const search = state.doc.textBetween(state.selection.from, state.selection.to, ' '); |
| 30 | + const meta: TrMeta = {open: true, search}; |
| 31 | + dispatch(setSearchState(state.tr.setMeta(pluginKey, meta), new SearchQuery({search}))); |
| 32 | + } |
| 33 | + return true; |
| 34 | +}; |
| 35 | + |
| 36 | +const hideSearch: Command = (state, dispatch) => { |
| 37 | + if (dispatch) { |
| 38 | + const meta: TrMeta = {open: false}; |
| 39 | + dispatch(setSearchState(state.tr.setMeta(pluginKey, meta), new SearchQuery({search: ''}))); |
| 40 | + } |
| 41 | + return true; |
| 42 | +}; |
| 43 | + |
| 44 | +type SearchState = { |
| 45 | + query: string; |
| 46 | + replacement?: string; |
| 47 | +}; |
| 48 | + |
| 49 | +type SearchViewState = |
| 50 | + | { |
| 51 | + open: false; |
| 52 | + } |
| 53 | + | { |
| 54 | + open: true; |
| 55 | + state: SearchState; |
| 56 | + }; |
| 57 | + |
| 58 | +export interface SearchViewPluginParams { |
| 59 | + anchorSelector: string; |
| 60 | +} |
| 61 | + |
| 62 | +export const searchViewPlugin = (params: SearchViewPluginParams) => { |
| 63 | + return new Plugin<SearchViewState>({ |
| 64 | + key: pluginKey, |
| 65 | + props: { |
| 66 | + handleKeyDown: keydownHandler({ |
| 67 | + 'Mod-f': openSearch, |
| 68 | + }), |
| 69 | + }, |
| 70 | + state: { |
| 71 | + init: () => ({open: false}), |
| 72 | + apply(tr, value, _oldState, _newState) { |
| 73 | + const newValue = tr.getMeta(pluginKey); |
| 74 | + if (typeof newValue === 'object') return newValue; |
| 75 | + return value; |
| 76 | + }, |
| 77 | + }, |
| 78 | + view(view) { |
| 79 | + return new SeachPluginView(view, params); |
| 80 | + }, |
| 81 | + }); |
| 82 | +}; |
| 83 | + |
| 84 | +class SeachPluginView implements PluginView { |
| 85 | + private readonly _view: EditorView; |
| 86 | + private readonly _renderer; |
| 87 | + |
| 88 | + private _viewState: SearchViewState | undefined; |
| 89 | + private _searchState: ReturnType<typeof getSearchState>; |
| 90 | + |
| 91 | + private readonly _onSearchChangeDebounced; |
| 92 | + private readonly _onReplacementChangeDebounced; |
| 93 | + |
| 94 | + constructor(view: EditorView, params: SearchViewPluginParams) { |
| 95 | + this._view = view; |
| 96 | + this._viewState = pluginKey.getState(view.state); |
| 97 | + this._searchState = getSearchState(view.state); |
| 98 | + |
| 99 | + this._renderer = this._createRenderer(params); |
| 100 | + |
| 101 | + this._onSearchChangeDebounced = debounce(this._handleSearchChange.bind(this)); |
| 102 | + this._onReplacementChangeDebounced = debounce(this._handleReplacementChange.bind(this)); |
| 103 | + } |
| 104 | + |
| 105 | + update() { |
| 106 | + const newViewState = pluginKey.getState(this._view.state); |
| 107 | + console.log('searchViewPlugin view update', {newViewState}); |
| 108 | + // const newSearchState = getSearchState(view.state); |
| 109 | + if (newViewState !== this._viewState) { |
| 110 | + this._viewState = newViewState; |
| 111 | + this._searchState = getSearchState(this._view.state); |
| 112 | + console.log('searchViewPlugin view update rerender', { |
| 113 | + viewState: this._viewState, |
| 114 | + searchState: this._searchState, |
| 115 | + }); |
| 116 | + this._renderer.rerender(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + destroy() { |
| 121 | + this._onSearchChangeDebounced.cancel(); |
| 122 | + this._onReplacementChangeDebounced.cancel(); |
| 123 | + this._renderer.remove(); |
| 124 | + } |
| 125 | + |
| 126 | + private _createRenderer(params: Pick<SearchViewPluginParams, 'anchorSelector'>) { |
| 127 | + return getReactRendererFromState(this._view.state).createItem('search-view', () => { |
| 128 | + const {_viewState: viewState, _searchState: searchState} = this; |
| 129 | + |
| 130 | + console.log('searchViewPlugin render', {viewState, searchState}); |
| 131 | + if (!viewState?.open || !searchState) return null; |
| 132 | + |
| 133 | + const anchor = this._view.dom.ownerDocument.querySelector(params.anchorSelector); |
| 134 | + console.log('searchViewPlugin render anchor', { |
| 135 | + anchor, |
| 136 | + anchorSelector: params.anchorSelector, |
| 137 | + }); |
| 138 | + |
| 139 | + return renderSearchPopup({ |
| 140 | + anchor, |
| 141 | + open: viewState.open, |
| 142 | + initial: searchState.query, |
| 143 | + onClose: this._onClose, |
| 144 | + onSearchPrev: this._onSearchPrev, |
| 145 | + onSearchNext: this._onSearchNext, |
| 146 | + onReplaceNext: this._onReplaceNext, |
| 147 | + onReplaceAll: this._onReplaceAll, |
| 148 | + onConfigChange: this._onConfigChange, |
| 149 | + onQueryChange: this._onSearchChangeDebounced, |
| 150 | + onReplacementChange: this._onReplacementChangeDebounced, |
| 151 | + }); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + private _handleSearchChange(value: string) { |
| 156 | + this._updateSearchState({search: value}); |
| 157 | + } |
| 158 | + |
| 159 | + private _handleReplacementChange(value: string) { |
| 160 | + this._updateSearchState({replace: value}); |
| 161 | + } |
| 162 | + |
| 163 | + private _updateSearchState(config: Partial<SearchQueryConfig>) { |
| 164 | + const {state, dispatch} = this._view; |
| 165 | + let prevConfig: SearchQueryConfig = {search: ''}; |
| 166 | + |
| 167 | + const prevSearchState = getSearchState(state); |
| 168 | + if (prevSearchState) { |
| 169 | + prevConfig = { |
| 170 | + search: prevSearchState.query.search, |
| 171 | + caseSensitive: prevSearchState.query.caseSensitive, |
| 172 | + wholeWord: prevSearchState.query.wholeWord, |
| 173 | + regexp: prevSearchState.query.regexp, |
| 174 | + replace: prevSearchState.query.replace, |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + console.log('searchViewPlugin updateSearchState', {prevSearchState, prevConfig, config}); |
| 179 | + |
| 180 | + dispatch( |
| 181 | + setSearchState( |
| 182 | + state.tr, |
| 183 | + new SearchQuery({ |
| 184 | + ...prevConfig, |
| 185 | + ...config, |
| 186 | + }), |
| 187 | + ), |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + private _onClose = () => { |
| 192 | + this._onSearchChangeDebounced.cancel(); |
| 193 | + this._onReplacementChangeDebounced.cancel(); |
| 194 | + hideSearch(this._view.state, this._view.dispatch); |
| 195 | + this._view.focus(); |
| 196 | + }; |
| 197 | + |
| 198 | + private _onSearchPrev = () => { |
| 199 | + // this._view.focus(); |
| 200 | + this._onSearchChangeDebounced.flush(); |
| 201 | + findPrev(this._view.state, this._view.dispatch); |
| 202 | + // this._scrollToSelection(); |
| 203 | + }; |
| 204 | + |
| 205 | + private _onSearchNext = () => { |
| 206 | + // this._view.focus(); |
| 207 | + this._onSearchChangeDebounced.flush(); |
| 208 | + findNext(this._view.state, this._view.dispatch); |
| 209 | + // this._scrollToSelection(); |
| 210 | + }; |
| 211 | + |
| 212 | + private _onReplaceNext = () => { |
| 213 | + this._onSearchChangeDebounced.flush(); |
| 214 | + this._onReplacementChangeDebounced.flush(); |
| 215 | + replaceNext(this._view.state, this._view.dispatch); |
| 216 | + }; |
| 217 | + |
| 218 | + private _onReplaceAll = () => { |
| 219 | + this._onSearchChangeDebounced.flush(); |
| 220 | + this._onReplacementChangeDebounced.flush(); |
| 221 | + replaceAll(this._view.state, this._view.dispatch); |
| 222 | + }; |
| 223 | + |
| 224 | + private _onConfigChange: SearchPopupProps['onConfigChange'] = (config) => { |
| 225 | + console.log('searchViewPlugin onConfigChange', config); |
| 226 | + this._updateSearchState({ |
| 227 | + wholeWord: config.wholeWord, |
| 228 | + caseSensitive: config.caseSensitive, |
| 229 | + }); |
| 230 | + }; |
| 231 | + |
| 232 | + // private _scrollToSelection() { |
| 233 | + // this._view.dispatch(this._view.state.tr.scrollIntoView()); |
| 234 | + // } |
| 235 | +} |
0 commit comments