Notes/src/public/app/widgets/note_context_aware_widget.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-01-12 19:05:09 +01:00
import BasicWidget from "./basic_widget.js";
2022-12-01 13:07:23 +01:00
import appContext from "../components/app_context.js";
2020-01-12 19:05:09 +01:00
2021-05-22 12:35:41 +02:00
export default class NoteContextAwareWidget extends BasicWidget {
2021-05-22 12:42:34 +02:00
isNoteContext(ntxId) {
2021-05-22 12:26:45 +02:00
if (Array.isArray(ntxId)) {
return this.noteContext && ntxId.includes(this.noteContext.ntxId);
2021-05-20 23:13:34 +02:00
}
else {
2021-05-22 12:26:45 +02:00
return this.noteContext && this.noteContext.ntxId === ntxId;
2021-05-20 23:13:34 +02:00
}
}
isActiveNoteContext() {
return appContext.tabManager.getActiveContext() === this.noteContext;
}
isNote(noteId) {
2020-01-27 22:58:03 +01:00
return this.noteId === noteId;
}
get note() {
return this.noteContext?.note;
2020-01-27 22:58:03 +01:00
}
get noteId() {
return this.note?.noteId;
}
2020-01-28 21:54:28 +01:00
get notePath() {
2022-07-10 15:01:05 +02:00
return this.noteContext?.notePath;
2020-01-28 21:54:28 +01:00
}
get hoistedNoteId() {
return this.noteContext?.hoistedNoteId;
}
get ntxId() {
return this.noteContext?.ntxId;
}
isEnabled() {
2020-03-06 22:17:07 +01:00
return !!this.note;
2020-02-02 20:02:08 +01:00
}
async refresh() {
if (this.isEnabled()) {
2020-02-02 20:02:08 +01:00
const start = Date.now();
2020-03-06 22:17:07 +01:00
this.toggleInt(true);
await this.refreshWithNote(this.note);
2020-02-02 20:02:08 +01:00
const end = Date.now();
if (glob.PROFILING_LOG && end - start > 10) {
2020-02-02 20:02:08 +01:00
console.log(`Refresh of ${this.componentId} took ${end-start}ms`);
}
2020-01-19 11:37:24 +01:00
}
else {
2020-03-06 22:17:07 +01:00
this.toggleInt(false);
2020-01-19 11:37:24 +01:00
}
}
async refreshWithNote(note) {}
2020-01-12 19:05:09 +01:00
2021-05-22 12:35:41 +02:00
async noteSwitchedEvent({noteContext, notePath}) {
2022-07-10 15:01:05 +02:00
// if notePath does not match then the noteContext has been switched to another note in the meantime
2021-05-22 12:26:45 +02:00
if (noteContext.notePath === notePath) {
2020-03-06 22:17:07 +01:00
await this.noteSwitched();
}
}
async noteSwitched() {
await this.refresh();
}
2021-05-22 17:58:46 +02:00
async activeContextChangedEvent({noteContext}) {
2021-05-22 12:26:45 +02:00
this.noteContext = noteContext;
2020-01-12 19:05:09 +01:00
2021-05-22 17:58:46 +02:00
await this.activeContextChanged();
2020-03-06 22:17:07 +01:00
}
2021-05-22 17:58:46 +02:00
async activeContextChanged() {
2020-03-06 22:17:07 +01:00
await this.refresh();
2020-01-12 19:05:09 +01:00
}
2020-02-27 12:26:42 +01:00
// when note is both switched and activated, this should not produce double refresh
2021-05-22 12:35:41 +02:00
async noteSwitchedAndActivatedEvent({noteContext, notePath}) {
2021-05-22 12:26:45 +02:00
this.noteContext = noteContext;
2020-02-29 22:04:46 +01:00
2022-07-10 15:01:05 +02:00
// if notePath does not match then the noteContext has been switched to another note in the meantime
2020-03-06 22:17:07 +01:00
if (this.notePath === notePath) {
await this.refresh();
}
}
2021-05-22 12:26:45 +02:00
setNoteContextEvent({noteContext}) {
/** @var {NoteContext} */
this.noteContext = noteContext;
2020-03-06 22:17:07 +01:00
}
2020-02-28 11:46:35 +01:00
2020-03-06 22:17:07 +01:00
async noteTypeMimeChangedEvent({noteId}) {
if (this.isNote(noteId)) {
2020-02-28 00:31:12 +01:00
await this.refresh();
}
2020-02-27 12:26:42 +01:00
}
2021-04-16 22:57:37 +02:00
async frocaReloadedEvent() {
2020-02-27 12:41:15 +01:00
await this.refresh();
}
}