mirror of
https://github.com/zadam/trilium.git
synced 2025-11-13 16:55:50 +01:00
chore(client/ts): port note_context_aware_widget
This commit is contained in:
136
src/public/app/widgets/note_context_aware_widget.ts
Normal file
136
src/public/app/widgets/note_context_aware_widget.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import BasicWidget from "./basic_widget.js";
|
||||
import appContext, { EventData } from "../components/app_context.js";
|
||||
import FNote from "../entities/fnote.js";
|
||||
import NoteContext from "../components/note_context.js";
|
||||
|
||||
/**
|
||||
* This widget allows for changing and updating depending on the active note.
|
||||
* @extends {BasicWidget}
|
||||
*/
|
||||
class NoteContextAwareWidget extends BasicWidget {
|
||||
|
||||
private noteContext?: NoteContext;
|
||||
|
||||
isNoteContext(ntxId: string) {
|
||||
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: string) {
|
||||
return this.noteId === noteId;
|
||||
}
|
||||
|
||||
get note() {
|
||||
return this.noteContext?.note;
|
||||
}
|
||||
|
||||
get noteId() {
|
||||
return this.note?.noteId;
|
||||
}
|
||||
|
||||
get notePath() {
|
||||
return this.noteContext?.notePath;
|
||||
}
|
||||
|
||||
get hoistedNoteId() {
|
||||
return this.noteContext?.hoistedNoteId;
|
||||
}
|
||||
|
||||
get ntxId() {
|
||||
return this.noteContext?.ntxId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if the widget is enabled. Widgets are enabled by default. Generally setting this to `false` will cause the widget not to be displayed, however it will still be available on the DOM but hidden.
|
||||
*
|
||||
* <p>
|
||||
* If the widget is not enabled, it will not receive `refreshWithNote` updates.
|
||||
*
|
||||
* @returns 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.
|
||||
*/
|
||||
async refreshWithNote(note: FNote | null | undefined) {}
|
||||
|
||||
async noteSwitchedEvent({noteContext, notePath}: EventData<"noteSwitched">) {
|
||||
this.noteContext = noteContext;
|
||||
// if notePath does not match, then the noteContext has been switched to another note in the meantime
|
||||
if (noteContext.notePath === notePath) {
|
||||
await this.noteSwitched();
|
||||
}
|
||||
}
|
||||
|
||||
async noteSwitched() {
|
||||
await this.refresh();
|
||||
}
|
||||
|
||||
async activeContextChangedEvent({noteContext}: EventData<"activeContextChanged">) {
|
||||
this.noteContext = noteContext;
|
||||
|
||||
await this.activeContextChanged();
|
||||
}
|
||||
|
||||
async activeContextChanged() {
|
||||
await this.refresh();
|
||||
}
|
||||
|
||||
// when note is both switched and activated, this should not produce a double refresh
|
||||
async noteSwitchedAndActivatedEvent({noteContext, notePath}: EventData<"noteSwitchedAndActivatedEvent">) {
|
||||
this.noteContext = noteContext;
|
||||
|
||||
// if notePath does not match, then the noteContext has been switched to another note in the meantime
|
||||
if (this.notePath === notePath) {
|
||||
await this.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
setNoteContextEvent({noteContext}: EventData<"setNoteContext">) {
|
||||
/** @var {NoteContext} */
|
||||
this.noteContext = noteContext;
|
||||
}
|
||||
|
||||
async noteTypeMimeChangedEvent({noteId}: EventData<"noteTypeMimeChangedEvent">) {
|
||||
if (this.isNote(noteId)) {
|
||||
await this.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
async frocaReloadedEvent() {
|
||||
await this.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
export default NoteContextAwareWidget;
|
||||
Reference in New Issue
Block a user