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

93 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-05-25 23:38:06 +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
import type Mark from "mark.js";
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";
import type { FindResult } from "./find.js";
2022-05-25 23:38:06 +02:00
const FIND_RESULT_SELECTED_CSS_CLASSNAME = "ck-find-result_selected";
const FIND_RESULT_CSS_CLASSNAME = "ck-find-result";
export default class FindInHtml {
2025-03-17 22:46:00 +02:00
private parent: FindWidget;
private currentIndex: number;
private $results: JQuery<HTMLElement> | null;
private mark?: Mark;
2025-03-17 22:46:00 +02:00
constructor(parent: FindWidget) {
2022-05-25 23:38:06 +02:00
this.parent = parent;
2022-05-26 16:29:54 +02:00
this.currentIndex = 0;
this.$results = null;
2022-05-25 23:38:06 +02:00
}
2025-03-17 22:46:00 +02:00
async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) {
const $content = await this.parent?.noteContext?.getContentElement();
if (!$content || !$content.length) {
return Promise.resolve({ totalFound: 0, currentFound: 0 });
}
if (!this.mark) {
this.mark = new (await import("mark.js")).default($content[0]);
}
2022-05-25 23:38:06 +02:00
2022-05-26 16:29:54 +02:00
const wholeWordChar = wholeWord ? "\\b" : "";
const regExp = new RegExp(wholeWordChar + utils.escapeRegExp(searchTerm) + wholeWordChar, matchCase ? "g" : "gi");
return new Promise<FindResult>((res) => {
this.mark!.unmark({
2022-05-26 16:29:54 +02:00
done: () => {
this.mark!.markRegExp(regExp, {
2022-05-26 16:29:54 +02:00
element: "span",
className: FIND_RESULT_CSS_CLASSNAME,
done: async () => {
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
const scrollingContainer = $content[0].closest('.scrolling-container');
const containerTop = scrollingContainer?.getBoundingClientRect().top ?? 0;
const closestIndex = this.$results.toArray().findIndex(el => el.getBoundingClientRect().top >= containerTop);
this.currentIndex = closestIndex >= 0 ? closestIndex : 0;
2022-05-26 16:29:54 +02:00
await this.jumpTo();
res({
totalFound: this.$results.length,
currentFound: this.$results.length > 0 ? this.currentIndex + 1 : 0
2022-05-26 16:29:54 +02:00
});
}
});
}
});
});
2022-05-25 23:38:06 +02:00
}
2025-03-17 22:46:00 +02:00
async findNext(direction: -1 | 1, currentFound: number, nextFound: number) {
if (this.$results?.length) {
2022-05-26 16:29:54 +02:00
this.currentIndex += direction;
if (this.currentIndex < 0) {
this.currentIndex = this.$results.length - 1;
}
if (this.currentIndex > this.$results.length - 1) {
this.currentIndex = 0;
}
await this.jumpTo();
}
2022-05-25 23:38:06 +02:00
}
2025-03-17 22:46:00 +02:00
async findBoxClosed(totalFound: number, currentFound: number) {
this.mark?.unmark();
2022-05-25 23:38:06 +02:00
}
2022-05-26 16:29:54 +02:00
async jumpTo() {
2025-03-17 22:46:00 +02:00
if (this.$results?.length) {
2022-05-26 16:29:54 +02:00
const $current = this.$results.eq(this.currentIndex);
this.$results.removeClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
2025-05-29 23:09:52 +08:00
$current[0].scrollIntoView({ block: 'center', inline: 'center'});
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
2022-05-26 16:29:54 +02:00
}
2022-05-25 23:38:06 +02:00
}
}