Files
Trilium/apps/client/src/widgets/buttons/button_from_note.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-11-25 15:29:57 +01:00
import froca from "../../services/froca.js";
import attributeService from "../../services/attributes.js";
2022-12-02 16:46:14 +01:00
import CommandButtonWidget from "./command_button.js";
import type { EventData } from "../../components/app_context.js";
export type ButtonNoteIdProvider = () => string;
2022-11-25 15:29:57 +01:00
2022-12-02 16:46:14 +01:00
export default class ButtonFromNoteWidget extends CommandButtonWidget {
2022-11-25 15:29:57 +01:00
constructor() {
super();
2022-11-29 16:16:57 +01:00
this.settings.buttonNoteIdProvider = null;
2022-11-25 15:29:57 +01:00
}
buttonNoteIdProvider(provider: ButtonNoteIdProvider) {
2022-11-29 16:16:57 +01:00
this.settings.buttonNoteIdProvider = provider;
return this;
}
2022-11-25 15:29:57 +01:00
doRender() {
super.doRender();
this.updateIcon();
}
updateIcon() {
if (!this.settings.buttonNoteIdProvider) {
console.error(`buttonNoteId for '${this.componentId}' is not defined.`);
return;
}
2022-11-29 16:16:57 +01:00
const buttonNoteId = this.settings.buttonNoteIdProvider();
2022-11-30 16:57:51 +01:00
if (!buttonNoteId) {
console.error(`buttonNoteId for '${this.componentId}' is not defined.`);
return;
}
2025-01-09 18:07:02 +02:00
froca.getNote(buttonNoteId).then((note) => {
const icon = note?.getIcon();
if (icon) {
this.settings.icon = icon;
}
2022-11-25 15:29:57 +01:00
this.refreshIcon();
2022-11-30 16:57:51 +01:00
});
2022-11-25 15:29:57 +01:00
}
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
// TODO: this seems incorrect
//@ts-ignore
2022-11-29 16:16:57 +01:00
const buttonNote = froca.getNoteFromCache(this.buttonNoteIdProvider());
if (!buttonNote) {
return;
}
2025-01-09 18:07:02 +02:00
if (loadResults.getAttributeRows(this.componentId).find((attr) => attr.type === "label" && attr.name === "iconClass" && attributeService.isAffecting(attr, buttonNote))) {
2022-11-25 15:29:57 +01:00
this.updateIcon();
}
}
2025-01-09 18:07:02 +02:00
}