Skip to content

Commit a8b6520

Browse files
feat: Add inline match highlighting in Editor for cross-page search (resolves #189) (#305)
Co-authored-by: Junhao Liao <[email protected]>
1 parent 1dae6aa commit a8b6520

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/components/Editor/index.tsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/* eslint max-lines: ["error", 350] */
1+
/* eslint max-lines: ["error", 400] */
2+
/* eslint max-lines-per-function: ["error", 180] */
3+
/* eslint max-statements: ["error", 25] */
24
import {
35
useCallback,
46
useEffect,
@@ -9,6 +11,7 @@ import {
911
import {useColorScheme} from "@mui/joy";
1012
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
1113

14+
import useQueryStore from "../../stores/queryStore";
1215
import useViewStore from "../../stores/viewStore";
1316
import {Nullable} from "../../typings/common";
1417
import {
@@ -187,6 +190,9 @@ const Editor = () => {
187190
const beginLineNumToLogEventNum = useViewStore((state) => state.beginLineNumToLogEventNum);
188191
const logData = useViewStore((state) => state.logData);
189192
const logEventNum = useViewStore((state) => state.logEventNum);
193+
const queryString = useQueryStore((state) => state.queryString);
194+
const queryIsCaseSensitive = useQueryStore((state) => state.queryIsCaseSensitive);
195+
const queryIsRegex = useQueryStore((state) => state.queryIsRegex);
190196

191197
const [lineNum, setLineNum] = useState<number>(1);
192198
const beginLineNumToLogEventNumRef = useRef<BeginLineNumToLogEventNumMap>(
@@ -195,6 +201,9 @@ const Editor = () => {
195201
const editorRef = useRef<Nullable<monaco.editor.IStandaloneCodeEditor>>(null);
196202
const isMouseDownRef = useRef<boolean>(false);
197203
const pageSizeRef = useRef(getConfig(CONFIG_KEY.PAGE_SIZE));
204+
const searchDecorationsCollectionRef = useRef<
205+
Nullable<monaco.editor.IEditorDecorationsCollection>
206+
>(null);
198207

199208
/**
200209
* Sets `editorRef` and configures callbacks for mouse down detection.
@@ -271,6 +280,44 @@ const Editor = () => {
271280
beginLineNumToLogEventNumRef.current = beginLineNumToLogEventNum;
272281
}, [beginLineNumToLogEventNum]);
273282

283+
// On `logData`, `queryString`, `queryIsCaseSensitive`, or `queryIsRegex` update, highlight any
284+
// matches.
285+
useEffect(() => {
286+
if (null === editorRef.current) {
287+
return;
288+
}
289+
searchDecorationsCollectionRef.current?.clear();
290+
291+
const matches = editorRef.current
292+
.getModel()
293+
?.findMatches(
294+
queryString,
295+
false,
296+
queryIsRegex,
297+
queryIsCaseSensitive,
298+
null,
299+
false,
300+
Infinity
301+
);
302+
303+
if ("undefined" === typeof matches || 0 === matches.length) {
304+
return;
305+
}
306+
searchDecorationsCollectionRef.current = editorRef.current.createDecorationsCollection(
307+
matches.map(({range}) => ({
308+
range: range,
309+
options: {
310+
className: "findMatch",
311+
},
312+
}))
313+
);
314+
}, [
315+
logData,
316+
queryString,
317+
queryIsCaseSensitive,
318+
queryIsRegex,
319+
]);
320+
274321
// On `logEventNum` update, update line number in the editor.
275322
useEffect(() => {
276323
if (null === editorRef.current || isMouseDownRef.current) {

src/stores/queryStore/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {QueryResults} from "../../typings/query";
22

33

44
interface QueryConfigValues {
5-
queryString: string;
65
queryIsCaseSensitive: boolean;
76
queryIsRegex: boolean;
7+
queryString: string;
88
}
99

1010
interface QueryConfigActions {
11-
setQueryString: (newQueryString: string) => void;
1211
setQueryIsCaseSensitive: (newQueryIsCaseSensitive: boolean) => void;
1312
setQueryIsRegex: (newQueryIsRegex: boolean) => void;
13+
setQueryString: (newQueryString: string) => void;
1414
}
1515

1616
type QueryConfigSlice = QueryConfigValues & QueryConfigActions;

0 commit comments

Comments
 (0)