-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(markup): added markup search (#295)
* feat(markup): added markup search
- Loading branch information
1 parent
fb97667
commit 48372a6
Showing
18 changed files
with
442 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"label_case-sensitive": "Case sensitive", | ||
"label_whole-word": "Whole word", | ||
"title": "Search in code" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {registerKeyset} from '../i18n'; | ||
|
||
import en from './en.json'; | ||
import ru from './ru.json'; | ||
|
||
const KEYSET = 'search'; | ||
|
||
export const i18n = registerKeyset(KEYSET, {en, ru}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"label_case-sensitive": "С учетом регистра", | ||
"label_whole-word": "Слово целиком", | ||
"title": "Найти в коде" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { | ||
SearchQuery, | ||
closeSearchPanel, | ||
findNext, | ||
findPrevious, | ||
search, | ||
searchKeymap, | ||
searchPanelOpen, | ||
setSearchQuery, | ||
} from '@codemirror/search'; | ||
import {EditorView, PluginValue, ViewPlugin, ViewUpdate, keymap} from '@codemirror/view'; | ||
|
||
import {EditorMode, EventMap} from '../../../bundle/Editor'; | ||
import type {RendererItem} from '../../../extensions'; | ||
import {debounce} from '../../../lodash'; | ||
import {Receiver} from '../../../utils'; | ||
import {ReactRendererFacet} from '../react-facet'; | ||
|
||
import {renderSearchPopup} from './view/SearchPopup'; | ||
|
||
interface SearchQueryParams { | ||
search: string; | ||
caseSensitive?: boolean; | ||
literal?: boolean; | ||
regexp?: boolean; | ||
replace?: string; | ||
valid?: boolean; | ||
wholeWord?: boolean; | ||
} | ||
|
||
const INPUT_DELAY = 200; | ||
|
||
export interface SearchPanelPluginParams { | ||
anchorSelector: string; | ||
inputDelay?: number; | ||
receiver?: Receiver<EventMap>; | ||
} | ||
|
||
export const SearchPanelPlugin = (params: SearchPanelPluginParams) => | ||
ViewPlugin.fromClass( | ||
class implements PluginValue { | ||
readonly view: EditorView; | ||
readonly params: SearchPanelPluginParams; | ||
|
||
anchor: HTMLElement | null; | ||
renderer: RendererItem | null; | ||
searchQuery: SearchQueryParams = { | ||
search: '', | ||
caseSensitive: false, | ||
wholeWord: false, | ||
}; | ||
receiver: Receiver<EventMap> | undefined; | ||
|
||
setViewSearchWithDelay: (config: Partial<SearchQueryParams>) => void; | ||
|
||
constructor(view: EditorView) { | ||
this.view = view; | ||
this.anchor = null; | ||
this.renderer = null; | ||
this.params = params; | ||
this.receiver = params.receiver; | ||
|
||
this.handleClose = this.handleClose.bind(this); | ||
this.handleChange = this.handleChange.bind(this); | ||
this.handleSearchNext = this.handleSearchNext.bind(this); | ||
this.handleSearchPrev = this.handleSearchPrev.bind(this); | ||
this.handleSearchConfigChange = this.handleSearchConfigChange.bind(this); | ||
this.handleEditorModeChange = this.handleEditorModeChange.bind(this); | ||
|
||
this.setViewSearchWithDelay = debounce( | ||
this.setViewSearch, | ||
this.params.inputDelay ?? INPUT_DELAY, | ||
); | ||
this.receiver?.on('change-editor-mode', this.handleEditorModeChange); | ||
} | ||
|
||
update(update: ViewUpdate): void { | ||
const isPanelOpen = searchPanelOpen(update.state); | ||
|
||
if (isPanelOpen && !this.renderer) { | ||
this.anchor = document.querySelector(this.params.anchorSelector); | ||
this.renderer = this.view.state | ||
.facet(ReactRendererFacet) | ||
.createItem('cm-search', () => | ||
renderSearchPopup({ | ||
open: true, | ||
anchor: this.anchor, | ||
onChange: this.handleChange, | ||
onClose: this.handleClose, | ||
onSearchNext: this.handleSearchNext, | ||
onSearchPrev: this.handleSearchPrev, | ||
onConfigChange: this.handleSearchConfigChange, | ||
}), | ||
); | ||
} else if (!isPanelOpen && this.renderer) { | ||
this.renderer?.remove(); | ||
this.renderer = null; | ||
} | ||
} | ||
|
||
destroy() { | ||
this.renderer?.remove(); | ||
this.renderer = null; | ||
this.receiver?.off('change-editor-mode', this.handleEditorModeChange); | ||
} | ||
|
||
setViewSearch(config: Partial<SearchQueryParams>) { | ||
this.searchQuery = { | ||
...this.searchQuery, | ||
...config, | ||
}; | ||
const searchQuery = new SearchQuery({ | ||
...this.searchQuery, | ||
}); | ||
|
||
this.view.dispatch({effects: setSearchQuery.of(searchQuery)}); | ||
} | ||
|
||
handleEditorModeChange({mode}: {mode: EditorMode}) { | ||
if (mode === 'wysiwyg') { | ||
closeSearchPanel(this.view); | ||
} | ||
} | ||
|
||
handleChange(search: string) { | ||
this.setViewSearchWithDelay({search}); | ||
} | ||
|
||
handleClose() { | ||
this.setViewSearch({search: ''}); | ||
closeSearchPanel(this.view); | ||
} | ||
|
||
handleSearchNext() { | ||
findNext(this.view); | ||
} | ||
|
||
handleSearchPrev() { | ||
findPrevious(this.view); | ||
} | ||
|
||
handleSearchConfigChange({ | ||
isCaseSensitive, | ||
isWholeWord, | ||
}: { | ||
isCaseSensitive?: boolean; | ||
isWholeWord?: boolean; | ||
}) { | ||
this.setViewSearch({ | ||
caseSensitive: isCaseSensitive, | ||
wholeWord: isWholeWord, | ||
}); | ||
} | ||
}, | ||
{ | ||
provide: () => [ | ||
keymap.of(searchKeymap), | ||
search({ | ||
createPanel: () => ({ | ||
// Create an empty search panel | ||
dom: document.createElement('div'), | ||
}), | ||
}), | ||
], | ||
}, | ||
); |
Oops, something went wrong.