mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
Merge pull request #1876 from TriliumNext/find_replace
Make the find function for read-only code scroll correctly.
This commit is contained in:
commit
307d94a5df
@ -438,6 +438,14 @@ body .CodeMirror {
|
|||||||
background-color: #eeeeee;
|
background-color: #eeeeee;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.cm-matchhighlight.ck-find-result{
|
||||||
|
background: var(--ck-color-highlight-background);
|
||||||
|
}
|
||||||
|
|
||||||
|
.cm-matchhighlight.ck-find-result_selected {
|
||||||
|
background-color: #ff9633;
|
||||||
|
}
|
||||||
|
|
||||||
.CodeMirror pre.CodeMirror-placeholder {
|
.CodeMirror pre.CodeMirror-placeholder {
|
||||||
color: #999 !important;
|
color: #999 !important;
|
||||||
}
|
}
|
||||||
|
@ -143,9 +143,9 @@ export default class FindWidget extends NoteContextAwareWidget {
|
|||||||
this.$currentFound = this.$widget.find(".find-widget-current-found");
|
this.$currentFound = this.$widget.find(".find-widget-current-found");
|
||||||
this.$totalFound = this.$widget.find(".find-widget-total-found");
|
this.$totalFound = this.$widget.find(".find-widget-total-found");
|
||||||
this.$caseSensitiveCheckbox = this.$widget.find(".find-widget-case-sensitive-checkbox");
|
this.$caseSensitiveCheckbox = this.$widget.find(".find-widget-case-sensitive-checkbox");
|
||||||
this.$caseSensitiveCheckbox.change(() => this.performFind());
|
this.$caseSensitiveCheckbox.on("change", () => this.performFind());
|
||||||
this.$matchWordsCheckbox = this.$widget.find(".find-widget-match-words-checkbox");
|
this.$matchWordsCheckbox = this.$widget.find(".find-widget-match-words-checkbox");
|
||||||
this.$matchWordsCheckbox.change(() => this.performFind());
|
this.$matchWordsCheckbox.on("change", () => this.performFind());
|
||||||
this.$previousButton = this.$widget.find(".find-widget-previous-button");
|
this.$previousButton = this.$widget.find(".find-widget-previous-button");
|
||||||
this.$previousButton.on("click", () => this.findNext(-1));
|
this.$previousButton.on("click", () => this.findNext(-1));
|
||||||
this.$nextButton = this.$widget.find(".find-widget-next-button");
|
this.$nextButton = this.$widget.find(".find-widget-next-button");
|
||||||
@ -160,7 +160,7 @@ export default class FindWidget extends NoteContextAwareWidget {
|
|||||||
this.$replaceButton = this.$widget.find(".replace-widget-replace-button");
|
this.$replaceButton = this.$widget.find(".replace-widget-replace-button");
|
||||||
this.$replaceButton.on("click", () => this.replace());
|
this.$replaceButton.on("click", () => this.replace());
|
||||||
|
|
||||||
this.$input.keydown(async (e) => {
|
this.$input.on("keydown", async (e) => {
|
||||||
if ((e.metaKey || e.ctrlKey) && (e.key === "F" || e.key === "f")) {
|
if ((e.metaKey || e.ctrlKey) && (e.key === "F" || e.key === "f")) {
|
||||||
// If ctrl+f is pressed when the findbox is shown, select the
|
// If ctrl+f is pressed when the findbox is shown, select the
|
||||||
// whole input to find
|
// whole input to find
|
||||||
@ -172,7 +172,7 @@ export default class FindWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.$widget.keydown(async (e) => {
|
this.$widget.on("keydown", async (e) => {
|
||||||
if (e.key === "Escape") {
|
if (e.key === "Escape") {
|
||||||
await this.closeSearch();
|
await this.closeSearch();
|
||||||
}
|
}
|
||||||
@ -197,9 +197,14 @@ export default class FindWidget extends NoteContextAwareWidget {
|
|||||||
const isReadOnly = await this.noteContext?.isReadOnly();
|
const isReadOnly = await this.noteContext?.isReadOnly();
|
||||||
|
|
||||||
let selectedText = "";
|
let selectedText = "";
|
||||||
if (this.note?.type === "code" && !isReadOnly && this.noteContext) {
|
if (this.note?.type === "code" && this.noteContext) {
|
||||||
|
if (isReadOnly){
|
||||||
|
const $content = await this.noteContext.getContentElement();
|
||||||
|
selectedText = $content.find('.cm-matchhighlight').first().text();
|
||||||
|
} else {
|
||||||
const codeEditor = await this.noteContext.getCodeEditor();
|
const codeEditor = await this.noteContext.getCodeEditor();
|
||||||
selectedText = codeEditor.getSelection();
|
selectedText = codeEditor.getSelection();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
selectedText = window.getSelection()?.toString() || "";
|
selectedText = window.getSelection()?.toString() || "";
|
||||||
}
|
}
|
||||||
@ -235,6 +240,12 @@ export default class FindWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async readOnlyTemporarilyDisabledEvent({ noteContext }: EventData<"readOnlyTemporarilyDisabled">) {
|
||||||
|
if (this.isNoteContext(noteContext.ntxId)) {
|
||||||
|
await this.closeSearch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async getHandler() {
|
async getHandler() {
|
||||||
if (this.note?.type === "render") {
|
if (this.note?.type === "render") {
|
||||||
return this.htmlHandler;
|
return this.htmlHandler;
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
// 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 utils from "../services/utils.js";
|
import utils from "../services/utils.js";
|
||||||
import appContext from "../components/app_context.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";
|
||||||
|
|
||||||
@ -39,12 +38,16 @@ export default class FindInHtml {
|
|||||||
caseSensitive: matchCase,
|
caseSensitive: matchCase,
|
||||||
done: async () => {
|
done: async () => {
|
||||||
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
|
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
|
||||||
this.currentIndex = 0;
|
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;
|
||||||
|
|
||||||
await this.jumpTo();
|
await this.jumpTo();
|
||||||
|
|
||||||
res({
|
res({
|
||||||
totalFound: this.$results.length,
|
totalFound: this.$results.length,
|
||||||
currentFound: Math.min(1, this.$results.length)
|
currentFound: this.$results.length > 0 ? this.currentIndex + 1 : 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -71,27 +74,17 @@ export default class FindInHtml {
|
|||||||
|
|
||||||
async findBoxClosed(totalFound: number, currentFound: number) {
|
async findBoxClosed(totalFound: number, currentFound: number) {
|
||||||
const $content = await this.parent?.noteContext?.getContentElement();
|
const $content = await this.parent?.noteContext?.getContentElement();
|
||||||
if ($content) {
|
if (typeof $content?.unmark === 'function') {
|
||||||
$content.unmark();
|
$content.unmark();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async jumpTo() {
|
async jumpTo() {
|
||||||
if (this.$results?.length) {
|
if (this.$results?.length) {
|
||||||
const offsetTop = 100;
|
|
||||||
const $current = this.$results.eq(this.currentIndex);
|
const $current = this.$results.eq(this.currentIndex);
|
||||||
this.$results.removeClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
|
this.$results.removeClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
|
||||||
|
$current[0].scrollIntoView();
|
||||||
if ($current.length) {
|
|
||||||
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
|
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
|
||||||
const position = $current.position().top - offsetTop;
|
|
||||||
|
|
||||||
const $content = await this.parent.noteContext?.getContentElement();
|
|
||||||
if ($content) {
|
|
||||||
const $contentWidget = appContext.getComponentByEl($content[0]);
|
|
||||||
$contentWidget.triggerCommand("scrollContainerTo", { position });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,16 +55,27 @@ export default class FindInText {
|
|||||||
const options = { matchCase: matchCase, wholeWords: wholeWord };
|
const options = { matchCase: matchCase, wholeWords: wholeWord };
|
||||||
findResult = textEditor.execute("find", searchTerm, options);
|
findResult = textEditor.execute("find", searchTerm, options);
|
||||||
totalFound = findResult.results.length;
|
totalFound = findResult.results.length;
|
||||||
// Find the result beyond the cursor
|
const selection = model.document.selection;
|
||||||
const cursorPos = model.document.selection.getLastPosition();
|
// If text is selected, highlight the corresponding result;
|
||||||
|
// otherwise, highlight the first visible result in the scrolling container.
|
||||||
|
if (!selection.isCollapsed) {
|
||||||
|
const cursorPos = selection.getFirstPosition();
|
||||||
for (let i = 0; i < findResult.results.length; ++i) {
|
for (let i = 0; i < findResult.results.length; ++i) {
|
||||||
const marker = findResult.results.get(i)?.marker;
|
const marker = findResult.results.get(i)?.marker;
|
||||||
const fromPos = marker?.getStart();
|
const fromPos = marker?.getStart();
|
||||||
if (cursorPos && fromPos && fromPos.compareWith(cursorPos) !== "before") {
|
if (cursorPos && fromPos?.compareWith(cursorPos) !== "before") {
|
||||||
currentFound = i;
|
currentFound = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
const editorEl = textEditor?.sourceElement;
|
||||||
|
const findResultElement = editorEl?.querySelectorAll(".ck-find-result");
|
||||||
|
const scrollingContainer = editorEl?.closest('.scrolling-container');
|
||||||
|
const containerTop = scrollingContainer?.getBoundingClientRect().top ?? 0;
|
||||||
|
const closestIndex = Array.from(findResultElement ?? []).findIndex((el) => el.getBoundingClientRect().top >= containerTop);
|
||||||
|
currentFound = closestIndex >= 0 ? closestIndex : 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.findResult = findResult;
|
this.findResult = findResult;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user