249 lines
8.9 KiB
JavaScript
Raw Normal View History

/**
* (c) Antonio Tejada 2022
2022-05-14 22:33:45 +02:00
* https://github.com/antoniotejada/Trilium-FindWidget
*/
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import appContext from "../services/app_context.js";
2022-05-16 23:56:43 +02:00
import FindInText from "./find_in_text.js";
import FindInCode from "./find_in_code.js";
const findWidgetDelayMillis = 200;
const waitForEnter = (findWidgetDelayMillis < 0);
2022-05-14 21:06:14 +02:00
// tabIndex=-1 on the checkbox labels is necessary so when clicking on the label
// the focusout handler is called with relatedTarget equal to the label instead
2022-05-14 22:33:45 +02:00
// of undefined. It's -1 instead of > 0, so they don't tabstop
2022-05-15 12:09:30 +02:00
const TPL = `
2022-05-14 22:33:45 +02:00
<div style="contain: none;">
2022-05-15 22:51:26 +02:00
<style>
.find-widget-box {
padding: 10px;
border-top: 1px solid var(--main-border-color);
align-items: center;
}
.find-widget-box > * {
margin-right: 15px;
}
.find-widget-box {
display: flex;
}
.find-widget-found-wrapper {
font-weight: bold;
}
</style>
<div class="find-widget-box">
<input type="text" class="font-control find-widget-search-term-input">
<div class="form-check">
<label tabIndex="-1" class="form-check-label">
<input type="checkbox" class="form-check-input find-widget-case-sensitive-checkbox">
case sensitive
</label>
</div>
<div class="form-check">
<label tabIndex="-1" class="form-check-label">
<input type="checkbox" class="form-check-input find-widget-match-words-checkbox">
match words
</label>
</div>
<div class="find-widget-found-wrapper">
<span class="find-widget-current-found">0</span>
/
<span class="find-widget-total-found">0</span>
</div>
2022-05-14 22:33:45 +02:00
</div>
</div>`;
2022-05-16 23:56:43 +02:00
export default class FindWidget extends NoteContextAwareWidget {
constructor() {
super();
2022-05-16 23:56:43 +02:00
this.textHandler = new FindInText();
this.codeHandler = new FindInCode();
}
2022-05-15 12:09:30 +02:00
doRender() {
this.$widget = $(TPL);
2022-05-15 22:51:26 +02:00
this.$findBox = this.$widget.find('.find-widget-box');
2022-05-15 12:09:30 +02:00
this.$findBox.hide();
2022-05-15 22:51:26 +02:00
this.$input = this.$widget.find('.find-widget-search-term-input');
this.$currentFound = this.$widget.find('.find-widget-current-found');
this.$totalFound = this.$widget.find('.find-widget-total-found');
this.$caseSensitiveCheckbox = this.$widget.find(".find-widget-case-sensitive-checkbox");
2022-05-16 23:56:43 +02:00
this.$caseSensitiveCheckbox.change(() => this.performFind());
2022-05-15 22:51:26 +02:00
this.$matchWordsCheckbox = this.$widget.find(".find-widget-match-words-checkbox");
2022-05-16 23:56:43 +02:00
this.$matchWordsCheckbox.change(() => this.performFind());
2022-05-15 22:51:26 +02:00
this.searchTerm = null;
2022-05-15 12:09:30 +02:00
this.$input.keydown(async e => {
2022-05-15 22:51:26 +02:00
if ((e.metaKey || e.ctrlKey) && (e.key === 'F' || e.key === 'f')) {
// If ctrl+f is pressed when the findbox is shown, select the
// whole input to find
2022-05-15 12:09:30 +02:00
this.$input.select();
2022-05-15 22:51:26 +02:00
} else if (e.key === 'Enter' || e.key === 'F3') {
2022-05-16 23:56:43 +02:00
await this.findNext(e);
e.preventDefault();
return false;
2022-05-14 22:33:45 +02:00
} else if (e.key === 'Escape') {
2022-05-16 23:56:43 +02:00
await this.getHandler().close();
}
});
2022-05-16 23:56:43 +02:00
this.$input.on('input', () => this.startSearch());
2022-05-14 21:06:14 +02:00
// Note blur doesn't bubble to parent div, but the parent div needs to
// detect when any of the children are not focused and hide. Use
// focusout instead which does bubble to the parent div.
2022-05-16 23:56:43 +02:00
this.$findBox.on('focusout', async e => {
2022-05-14 21:06:14 +02:00
// e.relatedTarget is the new focused element, note it can be null
// if nothing is being focused
2022-05-15 12:09:30 +02:00
if (this.$findBox[0].contains(e.relatedTarget)) {
2022-05-14 21:06:14 +02:00
// The focused element is inside this div, ignore
return;
}
2022-05-16 23:56:43 +02:00
await this.closeSearch();
});
2022-05-15 12:09:30 +02:00
return this.$widget;
}
2022-05-16 23:56:43 +02:00
startSearch() {
// XXX This should clear the previous search immediately in all cases
// (the search is stale when waitforenter but also while the
// delay is running for non waitforenter case)
if (!waitForEnter) {
// Clear the previous timeout if any, it's ok if timeoutId is
// null or undefined
clearTimeout(this.timeoutId);
// Defer the search a few millis so the search doesn't start
// immediately, as this can cause search word typing lag with
// one or two-char searchwords and long notes
// See https://github.com/antoniotejada/Trilium-FindWidget/issues/1
const searchTerm = this.$input.val();
const matchCase = this.$caseSensitiveCheckbox.prop("checked");
const wholeWord = this.$matchWordsCheckbox.prop("checked");
this.timeoutId = setTimeout(async () => {
this.timeoutId = null;
await this.performFind(searchTerm, matchCase, wholeWord);
}, findWidgetDelayMillis);
}
}
async findNext(e) {
const searchTerm = this.$input.val();
if (waitForEnter && this.searchTerm !== searchTerm) {
await this.performFind(searchTerm);
}
const totalFound = parseInt(this.$totalFound.text());
const currentFound = parseInt(this.$currentFound.text()) - 1;
if (totalFound > 0) {
const direction = e.shiftKey ? -1 : 1;
let nextFound = currentFound + direction;
// Wrap around
if (nextFound > totalFound - 1) {
nextFound = 0;
} else if (nextFound < 0) {
nextFound = totalFound - 1;
}
this.$currentFound.text(nextFound + 1);
await this.getHandler().findNext(direction, currentFound, nextFound);
}
}
2022-05-15 21:03:51 +02:00
async findInTextEvent() {
const note = appContext.tabManager.getActiveContextNote();
// Only writeable text and code supported
const readOnly = note.getAttribute("label", "readOnly");
if (!readOnly && (note.type === "code" || note.type === "text")) {
if (this.$findBox.is(":hidden")) {
this.$findBox.show();
this.$input.focus();
2022-05-15 22:51:26 +02:00
this.$totalFound.text(0);
this.$currentFound.text(0);
2022-05-15 21:03:51 +02:00
2022-05-16 23:56:43 +02:00
const searchTerm = await this.getHandler().getInitialSearchTerm();
2022-05-15 21:03:51 +02:00
2022-05-16 23:56:43 +02:00
this.$input.val(searchTerm || "");
2022-05-16 23:56:43 +02:00
// Directly perform the search if there's some text to
// find, without delaying or waiting for enter
if (searchTerm !== "") {
this.$input.select();
await this.performFind(searchTerm);
}
2022-05-16 23:56:43 +02:00
}
}
}
2022-05-14 21:06:14 +02:00
/**
* Perform the find and highlight the find results.
*
2022-05-16 23:56:43 +02:00
* @param [searchTerm] {string} taken from the input box if missing.
* @param [matchCase] {boolean} taken from the checkbox state if missing.
* @param [wholeWord] {boolean} taken from the checkbox state if missing.
2022-05-14 21:06:14 +02:00
*/
2022-05-15 22:51:26 +02:00
async performFind(searchTerm, matchCase, wholeWord) {
searchTerm = (searchTerm === undefined) ? this.$input.val() : searchTerm;
matchCase = (matchCase === undefined) ? this.$caseSensitiveCheckbox.prop("checked") : matchCase;
wholeWord = (wholeWord === undefined) ? this.$matchWordsCheckbox.prop("checked") : wholeWord;
2022-05-16 23:56:43 +02:00
const {totalFound, currentFound} = await this.getHandler().performFind(searchTerm, matchCase, wholeWord);
this.$totalFound.text(totalFound);
this.$currentFound.text(currentFound);
this.searchTerm = searchTerm;
}
async closeSearch() {
this.$findBox.hide();
// Restore any state, if there's a current occurrence clear markers
// and scroll to and select the last occurrence
// XXX Switching to a different tab with crl+tab doesn't invoke
// blur and leaves a stale search which then breaks when
// navigating it
const totalFound = parseInt(this.$totalFound.text());
const currentFound = parseInt(this.$currentFound.text()) - 1;
if (totalFound > 0) {
await this.getHandler().cleanup(totalFound, currentFound);
}
2022-05-16 23:56:43 +02:00
this.searchTerm = null;
}
isEnabled() {
2022-05-15 21:03:51 +02:00
return super.isEnabled() && (this.note.type === 'text' || this.note.type === 'code');
}
async entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteContentReloaded(this.noteId)) {
this.refresh();
}
}
2022-05-16 23:56:43 +02:00
getHandler() {
const note = appContext.tabManager.getActiveContextNote();
if (note.type === "code") {
return this.codeHandler;
} else {
return this.textHandler;
}
}
}