2022-05-16 23:56:43 +02:00
|
|
|
// ck-find-result and ck-find-result_selected are the styles ck-editor
|
|
|
|
// uses for highlighting matches, use the same one on CodeMirror
|
|
|
|
// for consistency
|
2022-05-25 23:38:06 +02:00
|
|
|
import utils from "../services/utils.js";
|
2025-03-17 22:46:00 +02:00
|
|
|
import type FindWidget from "./find.js";
|
2022-05-25 23:38:06 +02:00
|
|
|
|
2022-05-16 23:56:43 +02:00
|
|
|
const FIND_RESULT_SELECTED_CSS_CLASSNAME = "ck-find-result_selected";
|
|
|
|
const FIND_RESULT_CSS_CLASSNAME = "ck-find-result";
|
|
|
|
|
2025-03-17 22:46:00 +02:00
|
|
|
// TODO: Deduplicate.
|
|
|
|
interface Match {
|
|
|
|
className: string;
|
|
|
|
clear(): void;
|
|
|
|
find(): {
|
|
|
|
from: number;
|
|
|
|
to: number;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2025-05-13 15:27:50 +03:00
|
|
|
interface SearchParameters {
|
|
|
|
searchTerm: string;
|
|
|
|
matchCase: boolean;
|
|
|
|
wholeWord: boolean;
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:56:43 +02:00
|
|
|
export default class FindInCode {
|
2025-03-17 22:46:00 +02:00
|
|
|
|
|
|
|
private parent: FindWidget;
|
2025-05-13 15:27:50 +03:00
|
|
|
private searchParameters: SearchParameters | null = null;
|
2025-03-17 22:46:00 +02:00
|
|
|
|
|
|
|
constructor(parent: FindWidget) {
|
2022-05-17 23:53:35 +02:00
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getCodeEditor() {
|
2025-03-17 22:46:00 +02:00
|
|
|
return this.parent.noteContext?.getCodeEditor();
|
2022-05-17 23:53:35 +02:00
|
|
|
}
|
|
|
|
|
2025-03-17 22:46:00 +02:00
|
|
|
async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) {
|
2022-05-17 23:53:35 +02:00
|
|
|
const codeEditor = await this.getCodeEditor();
|
2025-03-17 22:46:00 +02:00
|
|
|
if (!codeEditor) {
|
2025-05-12 20:43:30 +03:00
|
|
|
return { totalFound: 0, currentFound: 0 };
|
2025-03-17 22:46:00 +02:00
|
|
|
}
|
|
|
|
|
2025-05-13 15:27:50 +03:00
|
|
|
this.searchParameters = {
|
|
|
|
searchTerm,
|
|
|
|
matchCase,
|
|
|
|
wholeWord,
|
|
|
|
};
|
2025-05-12 20:43:30 +03:00
|
|
|
const { totalFound, currentFound } = await codeEditor.performFind(searchTerm, matchCase, wholeWord);
|
2025-05-12 18:47:50 +03:00
|
|
|
return { totalFound, currentFound };
|
2022-05-16 23:56:43 +02:00
|
|
|
}
|
|
|
|
|
2025-03-17 22:46:00 +02:00
|
|
|
async findNext(direction: number, currentFound: number, nextFound: number) {
|
2022-05-17 23:53:35 +02:00
|
|
|
const codeEditor = await this.getCodeEditor();
|
2025-05-12 21:21:46 +03:00
|
|
|
if (!codeEditor) {
|
2025-03-17 22:46:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-12 21:21:46 +03:00
|
|
|
codeEditor.findNext(direction, currentFound, nextFound);
|
2022-05-16 23:56:43 +02:00
|
|
|
}
|
|
|
|
|
2025-03-17 22:46:00 +02:00
|
|
|
async findBoxClosed(totalFound: number, currentFound: number) {
|
2022-05-17 23:53:35 +02:00
|
|
|
const codeEditor = await this.getCodeEditor();
|
2025-05-12 22:17:10 +03:00
|
|
|
codeEditor?.cleanSearch();
|
2025-03-17 22:46:00 +02:00
|
|
|
codeEditor?.focus();
|
2022-05-16 23:56:43 +02:00
|
|
|
}
|
2024-11-11 22:57:24 +08:00
|
|
|
|
2025-05-12 23:52:41 +03:00
|
|
|
async replace(replaceText: string) {
|
|
|
|
const codeEditor = await this.getCodeEditor();
|
2025-05-13 15:27:50 +03:00
|
|
|
await codeEditor?.replace(replaceText);
|
|
|
|
this.rerunSearch();
|
2024-11-11 22:57:24 +08:00
|
|
|
}
|
2025-05-12 23:52:41 +03:00
|
|
|
|
2025-03-17 22:46:00 +02:00
|
|
|
async replaceAll(replaceText: string) {
|
2024-11-11 22:57:24 +08:00
|
|
|
const codeEditor = await this.getCodeEditor();
|
2025-05-13 15:27:50 +03:00
|
|
|
await codeEditor?.replaceAll(replaceText);
|
|
|
|
this.rerunSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
private rerunSearch() {
|
|
|
|
if (this.searchParameters) {
|
|
|
|
this.performFind(
|
|
|
|
this.searchParameters.searchTerm,
|
|
|
|
this.searchParameters.matchCase,
|
|
|
|
this.searchParameters.wholeWord);
|
|
|
|
}
|
2024-11-11 22:57:24 +08:00
|
|
|
}
|
2025-05-13 15:27:50 +03:00
|
|
|
|
2022-05-16 23:56:43 +02:00
|
|
|
}
|