Notes/apps/client/src/widgets/find_in_code.ts

155 lines
4.8 KiB
TypeScript
Raw Normal View History

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;
};
}
2022-05-16 23:56:43 +02:00
export default class FindInCode {
2025-03-17 22:46:00 +02:00
private parent: FindWidget;
private findResult?: Match[] | null;
constructor(parent: FindWidget) {
this.parent = parent;
}
async getCodeEditor() {
2025-03-17 22:46:00 +02:00
return this.parent.noteContext?.getCodeEditor();
}
2025-03-17 22:46:00 +02:00
async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) {
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-12 20:43:30 +03:00
const { totalFound, currentFound } = await codeEditor.performFind(searchTerm, matchCase, wholeWord);
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) {
const codeEditor = await this.getCodeEditor();
2025-03-17 22:46:00 +02:00
if (!codeEditor || !this.findResult) {
return;
}
2022-05-16 23:56:43 +02:00
const doc = codeEditor.doc;
//
// Dehighlight current, highlight & scrollIntoView next
//
let marker = this.findResult[currentFound];
let pos = marker.find();
marker.clear();
2025-01-09 18:07:02 +02:00
marker = doc.markText(pos.from, pos.to, { className: FIND_RESULT_CSS_CLASSNAME });
2022-05-16 23:56:43 +02:00
this.findResult[currentFound] = marker;
marker = this.findResult[nextFound];
pos = marker.find();
marker.clear();
2025-01-09 18:07:02 +02:00
marker = doc.markText(pos.from, pos.to, { className: FIND_RESULT_SELECTED_CSS_CLASSNAME });
2022-05-16 23:56:43 +02:00
this.findResult[nextFound] = marker;
codeEditor.scrollIntoView(pos.from);
}
2025-03-17 22:46:00 +02:00
async findBoxClosed(totalFound: number, currentFound: number) {
const codeEditor = await this.getCodeEditor();
2022-05-16 23:56:43 +02:00
2025-03-17 22:46:00 +02:00
if (codeEditor && totalFound > 0) {
2022-05-16 23:56:43 +02:00
const doc = codeEditor.doc;
2025-03-17 22:46:00 +02:00
const pos = this.findResult?.[currentFound].find();
2022-05-16 23:56:43 +02:00
// Note setting the selection sets the cursor to
// the end of the selection and scrolls it into
// view
2025-03-17 22:46:00 +02:00
if (pos) {
doc.setSelection(pos.from, pos.to);
}
2022-05-16 23:56:43 +02:00
// Clear all markers
codeEditor.operation(() => {
2025-03-17 22:46:00 +02:00
if (!this.findResult) {
return;
}
2022-05-16 23:56:43 +02:00
for (let i = 0; i < this.findResult.length; ++i) {
let marker = this.findResult[i];
marker.clear();
}
});
}
this.findResult = null;
2025-03-17 22:46:00 +02:00
codeEditor?.focus();
2022-05-16 23:56:43 +02:00
}
2025-03-17 22:46:00 +02:00
async replace(replaceText: string) {
2024-11-12 10:56:54 +08:00
// this.findResult may be undefined and null
2025-01-09 18:07:02 +02:00
if (!this.findResult || this.findResult.length === 0) {
2024-11-12 10:56:54 +08:00
return;
}
2024-11-11 22:57:24 +08:00
let currentFound = -1;
this.findResult.forEach((marker, index) => {
const pos = marker.find();
if (pos) {
if (marker.className === FIND_RESULT_SELECTED_CSS_CLASSNAME) {
currentFound = index;
return;
}
}
});
if (currentFound >= 0) {
let marker = this.findResult[currentFound];
let pos = marker.find();
const codeEditor = await this.getCodeEditor();
2025-03-17 22:46:00 +02:00
const doc = codeEditor?.doc;
if (doc) {
doc.replaceRange(replaceText, pos.from, pos.to);
}
2024-11-11 22:57:24 +08:00
marker.clear();
let nextFound;
if (currentFound === this.findResult.length - 1) {
nextFound = 0;
} else {
nextFound = currentFound;
}
this.findResult.splice(currentFound, 1);
if (this.findResult.length > 0) {
this.findNext(0, nextFound, nextFound);
}
}
}
2025-03-17 22:46:00 +02:00
async replaceAll(replaceText: string) {
2025-01-09 18:07:02 +02:00
if (!this.findResult || this.findResult.length === 0) {
2024-11-12 10:56:54 +08:00
return;
}
2024-11-11 22:57:24 +08:00
const codeEditor = await this.getCodeEditor();
2025-03-17 22:46:00 +02:00
const doc = codeEditor?.doc;
codeEditor?.operation(() => {
if (!this.findResult) {
return;
}
2024-11-11 22:57:24 +08:00
for (let currentFound = 0; currentFound < this.findResult.length; currentFound++) {
let marker = this.findResult[currentFound];
let pos = marker.find();
2025-03-17 22:46:00 +02:00
doc?.replaceRange(replaceText, pos.from, pos.to);
2024-11-11 22:57:24 +08:00
marker.clear();
}
});
this.findResult = [];
}
2022-05-16 23:56:43 +02:00
}