Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Desktop: Resolves #9927: Beta editor: Fix search results not highligh…
…ted (#9928)
- Loading branch information
1 parent
5e4c35a
commit 20f8bb7
Showing
12 changed files
with
295 additions
and
67 deletions.
There are no files selected for viewing
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
66 changes: 66 additions & 0 deletions
66
packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useEditorSearchHandler.ts
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,66 @@ | ||
import { RefObject, useEffect } from 'react'; | ||
import usePrevious from '../../../../hooks/usePrevious'; | ||
import { RenderedBody } from './types'; | ||
const debounce = require('debounce'); | ||
|
||
interface Props { | ||
setLocalSearchResultCount(count: number): void; | ||
searchMarkers: any; | ||
webviewRef: RefObject<any>; | ||
editorRef: RefObject<any>; | ||
|
||
noteContent: string; | ||
renderedBody: RenderedBody; | ||
} | ||
|
||
const useEditorSearchHandler = (props: Props) => { | ||
const { webviewRef, editorRef, renderedBody, noteContent, searchMarkers } = props; | ||
|
||
const previousContent = usePrevious(noteContent); | ||
const previousRenderedBody = usePrevious(renderedBody); | ||
const previousSearchMarkers = usePrevious(searchMarkers); | ||
|
||
useEffect(() => { | ||
if (!searchMarkers) return () => {}; | ||
|
||
// If there is a currently active search, it's important to re-search the text as the user | ||
// types. However this is slow for performance so we ONLY want it to happen when there is | ||
// a search | ||
|
||
// Note that since the CodeMirror component also needs to handle the viewer pane, we need | ||
// to check if the rendered body has changed too (it will be changed with a delay after | ||
// props.content has been updated). | ||
const textChanged = searchMarkers.keywords.length > 0 && (noteContent !== previousContent || renderedBody !== previousRenderedBody); | ||
|
||
if (webviewRef.current && (searchMarkers !== previousSearchMarkers || textChanged)) { | ||
webviewRef.current.send('setMarkers', searchMarkers.keywords, searchMarkers.options); | ||
|
||
if (editorRef.current) { | ||
// Fixes https://github.com/laurent22/joplin/issues/7565 | ||
const debouncedMarkers = debounce(() => { | ||
const matches = editorRef.current.setMarkers(searchMarkers.keywords, searchMarkers.options); | ||
|
||
props.setLocalSearchResultCount(matches); | ||
}, 50); | ||
debouncedMarkers(); | ||
return () => { | ||
debouncedMarkers.clear(); | ||
}; | ||
} | ||
} | ||
return () => {}; | ||
}, [ | ||
editorRef, | ||
webviewRef, | ||
searchMarkers, | ||
previousSearchMarkers, | ||
props.setLocalSearchResultCount, | ||
noteContent, | ||
previousContent, | ||
previousRenderedBody, | ||
renderedBody, | ||
]); | ||
|
||
}; | ||
|
||
export default useEditorSearchHandler; |
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
Oops, something went wrong.