2020-01-12 19:05:09 +01:00
|
|
|
import BasicWidget from "./basic_widget.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import appContext, { type EventData } from "../components/app_context.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type FNote from "../entities/fnote.js";
|
|
|
|
import type NoteContext from "../components/note_context.js";
|
2020-01-12 19:05:09 +01:00
|
|
|
|
2023-08-21 04:15:53 -04:00
|
|
|
/**
|
|
|
|
* This widget allows for changing and updating depending on the active note.
|
|
|
|
* @extends {BasicWidget}
|
|
|
|
*/
|
|
|
|
class NoteContextAwareWidget extends BasicWidget {
|
2025-01-07 12:34:10 +02:00
|
|
|
protected noteContext?: NoteContext;
|
2025-01-07 11:55:20 +02:00
|
|
|
|
2025-01-28 14:07:56 +02:00
|
|
|
isNoteContext(ntxId: string | string[] | null | undefined) {
|
2021-05-22 12:26:45 +02:00
|
|
|
if (Array.isArray(ntxId)) {
|
2025-01-28 14:07:56 +02:00
|
|
|
return this.noteContext && this.noteContext.ntxId && ntxId.includes(this.noteContext.ntxId);
|
2025-01-09 18:07:02 +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
|
|
|
}
|
2020-01-19 20:18:02 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:53:57 +02:00
|
|
|
isActiveNoteContext() {
|
|
|
|
return appContext.tabManager.getActiveContext() === this.noteContext;
|
|
|
|
}
|
|
|
|
|
2025-01-07 11:55:20 +02:00
|
|
|
isNote(noteId: string) {
|
2020-01-27 22:58:03 +01:00
|
|
|
return this.noteId === noteId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get note() {
|
2022-07-03 23:10:13 +02:00
|
|
|
return this.noteContext?.note;
|
2020-01-27 22:58:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get noteId() {
|
2022-07-03 23:10:13 +02:00
|
|
|
return this.note?.noteId;
|
2020-01-19 20:18:02 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-03 21:49:57 +01:00
|
|
|
get hoistedNoteId() {
|
2022-07-03 23:10:13 +02:00
|
|
|
return this.noteContext?.hoistedNoteId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get ntxId() {
|
|
|
|
return this.noteContext?.ntxId;
|
2021-03-03 21:49:57 +01:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:53 -04:00
|
|
|
/**
|
2024-12-11 18:31:29 +02:00
|
|
|
* 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.
|
2025-01-04 21:03:03 +02:00
|
|
|
*
|
2024-10-27 21:03:13 +02:00
|
|
|
* <p>
|
|
|
|
* If the widget is not enabled, it will not receive `refreshWithNote` updates.
|
2025-01-04 21:03:03 +02:00
|
|
|
*
|
2025-01-07 11:55:20 +02:00
|
|
|
* @returns true when an active note exists
|
2023-08-21 04:15:53 -04:00
|
|
|
*/
|
2025-01-28 14:13:21 +02:00
|
|
|
isEnabled(): boolean | null | undefined {
|
2020-03-06 22:17:07 +01:00
|
|
|
return !!this.note;
|
2020-02-02 20:02:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async refresh() {
|
2020-02-08 21:54:39 +01:00
|
|
|
if (this.isEnabled()) {
|
2020-03-06 22:17:07 +01:00
|
|
|
this.toggleInt(true);
|
2024-08-14 22:10:54 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2020-03-06 22:17:07 +01:00
|
|
|
this.toggleInt(false);
|
2020-01-19 11:37:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 23:47:24 +02:00
|
|
|
/**
|
2025-01-07 11:55:20 +02:00
|
|
|
* Override this method to be able to refresh your widget with each note.
|
2023-04-03 23:47:24 +02:00
|
|
|
*/
|
2025-01-07 11:55:20 +02:00
|
|
|
async refreshWithNote(note: FNote | null | undefined) {}
|
2020-01-12 19:05:09 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async noteSwitchedEvent({ noteContext, notePath }: EventData<"noteSwitched">) {
|
2025-01-04 21:03:03 +02:00
|
|
|
this.noteContext = noteContext;
|
2023-05-05 23:41:11 +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();
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async activeContextChangedEvent({ noteContext }: EventData<"activeContextChanged">) {
|
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-01 22:29:32 +01:00
|
|
|
|
2023-05-05 23:41:11 +02:00
|
|
|
// when note is both switched and activated, this should not produce a double refresh
|
2025-01-09 18:07:02 +02:00
|
|
|
async noteSwitchedAndActivatedEvent({ noteContext, notePath }: EventData<"noteSwitchedAndActivatedEvent">) {
|
2021-05-22 12:26:45 +02:00
|
|
|
this.noteContext = noteContext;
|
2020-02-29 22:04:46 +01:00
|
|
|
|
2023-05-05 23:41:11 +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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
setNoteContextEvent({ noteContext }: EventData<"setNoteContext">) {
|
2021-05-22 12:26:45 +02:00
|
|
|
/** @var {NoteContext} */
|
|
|
|
this.noteContext = noteContext;
|
2020-03-06 22:17:07 +01:00
|
|
|
}
|
2020-02-28 11:46:35 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async noteTypeMimeChangedEvent({ noteId }: EventData<"noteTypeMimeChangedEvent">) {
|
2020-03-06 22:17:07 +01:00
|
|
|
if (this.isNote(noteId)) {
|
2020-02-28 00:31:12 +01:00
|
|
|
await this.refresh();
|
|
|
|
}
|
2020-02-27 12:26:42 +01:00
|
|
|
}
|
|
|
|
|
2025-01-28 15:44:15 +02:00
|
|
|
async frocaReloadedEvent(): Promise<void> {
|
2020-02-27 12:41:15 +01:00
|
|
|
await this.refresh();
|
2020-02-01 22:29:32 +01:00
|
|
|
}
|
2020-08-13 23:23:57 +02:00
|
|
|
}
|
2023-08-21 04:15:53 -04:00
|
|
|
|
|
|
|
export default NoteContextAwareWidget;
|