import BasicWidget from "./basic_widget.js"; import appContext from "../components/app_context.js"; /** * This widget allows for changing and updating depending on the active note. * @extends {BasicWidget} */ class NoteContextAwareWidget extends BasicWidget { isNoteContext(ntxId) { if (Array.isArray(ntxId)) { return this.noteContext && ntxId.includes(this.noteContext.ntxId); } else { return this.noteContext && this.noteContext.ntxId === ntxId; } } isActiveNoteContext() { return appContext.tabManager.getActiveContext() === this.noteContext; } isNote(noteId) { return this.noteId === noteId; } /** @returns {FNote|undefined} */ get note() { return this.noteContext?.note; } /** @returns {string|undefined} */ get noteId() { return this.note?.noteId; } /** @returns {string|undefined} */ get notePath() { return this.noteContext?.notePath; } /** @returns {string} */ get hoistedNoteId() { return this.noteContext?.hoistedNoteId; } get ntxId() { return this.noteContext?.ntxId; } /** * @inheritdoc * *
* If the widget is not enabled, it will not receive `refreshWithNote` updates.
*
* @returns {boolean} true when an active note exists
*/
isEnabled() {
return !!this.note;
}
async refresh() {
if (this.isEnabled()) {
this.toggleInt(true);
try {
await this.refreshWithNote(this.note);
} catch (e) {
// Ignore errors when user is refreshing or navigating away.
if (e === "rejected by browser") {
return;
}
throw e;
}
}
else {
this.toggleInt(false);
}
}
/**
* Override this method to be able to refresh your
* widget with each note.
* @param {FNote} note
* @returns {Promise