find widget readonly notes

This commit is contained in:
zadam
2022-05-26 16:29:54 +02:00
parent f250b72563
commit ade77e5fb8
6 changed files with 78 additions and 18 deletions

View File

@@ -3,6 +3,7 @@
// for consistency
import libraryLoader from "../services/library_loader.js";
import utils from "../services/utils.js";
import appContext from "../services/app_context.js";
const FIND_RESULT_SELECTED_CSS_CLASSNAME = "ck-find-result_selected";
const FIND_RESULT_CSS_CLASSNAME = "ck-find-result";
@@ -11,6 +12,8 @@ export default class FindInHtml {
constructor(parent) {
/** @property {FindWidget} */
this.parent = parent;
this.currentIndex = 0;
this.$results = null;
}
async getInitialSearchTerm() {
@@ -22,15 +25,69 @@ export default class FindInHtml {
const $content = await this.parent.noteContext.getContentElement();
$content.markRegExp(new RegExp(utils.escapeRegExp(searchTerm), "gi"));
const wholeWordChar = wholeWord ? "\\b" : "";
const regExp = new RegExp(wholeWordChar + utils.escapeRegExp(searchTerm) + wholeWordChar, matchCase ? "g" : "gi");
return new Promise(res => {
$content.unmark({
done: () => {
$content.markRegExp(regExp, {
element: "span",
className: FIND_RESULT_CSS_CLASSNAME,
separateWordSearch: false,
caseSensitive: matchCase,
done: async () => {
this.$results = $content.find("." + FIND_RESULT_CSS_CLASSNAME);
this.currentIndex = 0;
await this.jumpTo();
res({
totalFound: this.$results.length,
currentFound: 1
});
}
});
}
});
});
}
async findNext(direction, currentFound, nextFound) {
if (this.$results.length) {
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();
}
}
async cleanup(totalFound, currentFound) {
async findBoxClosed(totalFound, currentFound) {
const $content = await this.parent.noteContext.getContentElement();
$content.unmark();
}
async close() {
async jumpTo() {
if (this.$results.length) {
const offsetTop = 100;
const $current = this.$results.eq(this.currentIndex);
this.$results.removeClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
if ($current.length) {
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
const position = $current.position().top - offsetTop;
const $content = await this.parent.noteContext.getContentElement();
const $contentWiget = appContext.getComponentByEl($content);
$contentWiget.triggerCommand("scrollContainerTo", {position});
}
}
}
}