mirror of
https://github.com/zadam/trilium.git
synced 2025-11-08 22:35:50 +01:00
Make the find function for read-only code scroll correctly.
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
// uses for highlighting matches, use the same one on CodeMirror
|
||||
// for consistency
|
||||
import utils from "../services/utils.js";
|
||||
import appContext from "../components/app_context.js";
|
||||
import type FindWidget from "./find.js";
|
||||
import type { FindResult } from "./find.js";
|
||||
|
||||
@@ -39,12 +38,25 @@ export default class FindInHtml {
|
||||
caseSensitive: matchCase,
|
||||
done: async () => {
|
||||
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
|
||||
this.currentIndex = 0;
|
||||
let closestIndex = 0;
|
||||
let minTop = Infinity;
|
||||
|
||||
this.$results.each((i, el) => {
|
||||
const rect = el.getBoundingClientRect();
|
||||
const top = rect.top;
|
||||
|
||||
if (top >= 0 && top < minTop) {
|
||||
minTop = top;
|
||||
closestIndex = i;
|
||||
}
|
||||
});
|
||||
|
||||
this.currentIndex = closestIndex;
|
||||
await this.jumpTo();
|
||||
|
||||
res({
|
||||
totalFound: this.$results.length,
|
||||
currentFound: Math.min(1, this.$results.length)
|
||||
currentFound: this.$results.length > 0 ? closestIndex + 1 : 0
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -78,20 +90,10 @@ export default class FindInHtml {
|
||||
|
||||
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();
|
||||
if ($content) {
|
||||
const $contentWidget = appContext.getComponentByEl($content[0]);
|
||||
$contentWidget.triggerCommand("scrollContainerTo", { position });
|
||||
}
|
||||
}
|
||||
$current[0].scrollIntoView();
|
||||
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user