mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-10-27 01:51:34 +08:00
fix(client/search): broken search in read-only text
This commit is contained in:
parent
b4df8f75b9
commit
2f406aea83
@ -248,10 +248,10 @@ export default class FindWidget extends NoteContextAwareWidget {
|
|||||||
case "code":
|
case "code":
|
||||||
return this.codeHandler;
|
return this.codeHandler;
|
||||||
case "text":
|
case "text":
|
||||||
return this.textHandler;
|
|
||||||
default:
|
|
||||||
const readOnly = await this.noteContext?.isReadOnly();
|
const readOnly = await this.noteContext?.isReadOnly();
|
||||||
return readOnly ? this.htmlHandler : this.textHandler;
|
return readOnly ? this.htmlHandler : this.textHandler;
|
||||||
|
default:
|
||||||
|
console.warn("FindWidget: Unsupported note type for find widget", this.note?.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
// ck-find-result and ck-find-result_selected are the styles ck-editor
|
// ck-find-result and ck-find-result_selected are the styles ck-editor
|
||||||
// uses for highlighting matches, use the same one on CodeMirror
|
// uses for highlighting matches, use the same one on CodeMirror
|
||||||
// for consistency
|
// for consistency
|
||||||
|
import type Mark from "mark.js";
|
||||||
import utils from "../services/utils.js";
|
import utils from "../services/utils.js";
|
||||||
import type FindWidget from "./find.js";
|
import type FindWidget from "./find.js";
|
||||||
import type { FindResult } from "./find.js";
|
import type { FindResult } from "./find.js";
|
||||||
@ -13,6 +14,7 @@ export default class FindInHtml {
|
|||||||
private parent: FindWidget;
|
private parent: FindWidget;
|
||||||
private currentIndex: number;
|
private currentIndex: number;
|
||||||
private $results: JQuery<HTMLElement> | null;
|
private $results: JQuery<HTMLElement> | null;
|
||||||
|
private mark?: Mark;
|
||||||
|
|
||||||
constructor(parent: FindWidget) {
|
constructor(parent: FindWidget) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
@ -21,21 +23,24 @@ export default class FindInHtml {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) {
|
async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) {
|
||||||
await import("mark.js");
|
|
||||||
|
|
||||||
const $content = await this.parent?.noteContext?.getContentElement();
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
const wholeWordChar = wholeWord ? "\\b" : "";
|
const wholeWordChar = wholeWord ? "\\b" : "";
|
||||||
const regExp = new RegExp(wholeWordChar + utils.escapeRegExp(searchTerm) + wholeWordChar, matchCase ? "g" : "gi");
|
const regExp = new RegExp(wholeWordChar + utils.escapeRegExp(searchTerm) + wholeWordChar, matchCase ? "g" : "gi");
|
||||||
|
|
||||||
return new Promise<FindResult>((res) => {
|
return new Promise<FindResult>((res) => {
|
||||||
$content?.unmark({
|
this.mark!.unmark({
|
||||||
done: () => {
|
done: () => {
|
||||||
$content.markRegExp(regExp, {
|
this.mark!.markRegExp(regExp, {
|
||||||
element: "span",
|
element: "span",
|
||||||
className: FIND_RESULT_CSS_CLASSNAME,
|
className: FIND_RESULT_CSS_CLASSNAME,
|
||||||
separateWordSearch: false,
|
|
||||||
caseSensitive: matchCase,
|
|
||||||
done: async () => {
|
done: async () => {
|
||||||
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
|
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
|
||||||
const scrollingContainer = $content[0].closest('.scrolling-container');
|
const scrollingContainer = $content[0].closest('.scrolling-container');
|
||||||
@ -73,10 +78,7 @@ export default class FindInHtml {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findBoxClosed(totalFound: number, currentFound: number) {
|
async findBoxClosed(totalFound: number, currentFound: number) {
|
||||||
const $content = await this.parent?.noteContext?.getContentElement();
|
this.mark?.unmark();
|
||||||
if (typeof $content?.unmark === 'function') {
|
|
||||||
$content.unmark();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async jumpTo() {
|
async jumpTo() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user