trilium/src/public/app/widgets/tab_aware_widget.js
2021-05-22 12:26:45 +02:00

116 lines
2.9 KiB
JavaScript

import BasicWidget from "./basic_widget.js";
import appContext from "../services/app_context.js";
export default class TabAwareWidget extends BasicWidget {
isTab(ntxId) {
if (Array.isArray(ntxId)) {
return this.noteContext && ntxId.includes(this.noteContext.ntxId);
}
else {
return this.noteContext && this.noteContext.ntxId === ntxId;
}
}
isTabOrParent(ntxId) {
return this.noteContext && (this.noteContext.ntxId === ntxId || this.noteContext.mainNtxId === ntxId);
}
isNote(noteId) {
return this.noteId === noteId;
}
get note() {
return this.noteContext && this.noteContext.note;
}
get noteId() {
return this.note && this.note.noteId;
}
get notePath() {
return this.noteContext && this.noteContext.notePath;
}
get hoistedNoteId() {
return this.noteContext && this.noteContext.hoistedNoteId;
}
isEnabled() {
return !!this.note;
}
async refresh() {
if (this.isEnabled()) {
const start = Date.now();
this.toggleInt(true);
await this.refreshWithNote(this.note);
const end = Date.now();
if (glob.PROFILING_LOG && end - start > 10) {
console.log(`Refresh of ${this.componentId} took ${end-start}ms`);
}
}
else {
this.toggleInt(false);
}
}
async refreshWithNote(note) {}
async tabNoteSwitchedEvent({noteContext, notePath}) {
// if notePath does not match then the noteContext has been switched to another note in the mean time
if (noteContext.notePath === notePath) {
await this.noteSwitched();
}
}
async noteSwitched() {
await this.refresh();
}
async activeTabChangedEvent({noteContext}) {
this.noteContext = noteContext;
await this.activeTabChanged();
}
async activeTabChanged() {
await this.refresh();
}
// when note is both switched and activated, this should not produce double refresh
async tabNoteSwitchedAndActivatedEvent({noteContext, notePath}) {
this.noteContext = noteContext;
// if notePath does not match then the noteContext has been switched to another note in the mean time
if (this.notePath === notePath) {
await this.refresh();
}
}
setNoteContextEvent({noteContext}) {
/** @var {NoteContext} */
this.noteContext = noteContext;
}
async noteTypeMimeChangedEvent({noteId}) {
if (this.isNote(noteId)) {
await this.refresh();
}
}
async frocaReloadedEvent() {
await this.refresh();
}
async lazyLoadedEvent() {
if (!this.noteContext) { // has not been loaded yet
this.noteContext = appContext.tabManager.getActiveNoteContext();
}
await this.refresh();
}
}