Files
Trilium/apps/client/src/widgets/bookmark_switch.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-12-20 17:30:47 +01:00
import SwitchWidget from "./switch.js";
2022-12-04 13:16:05 +01:00
import server from "../services/server.js";
import toastService from "../services/toast.js";
import { t } from "../services/i18n.js";
import type FNote from "../entities/fnote.js";
import type { EventData } from "../components/app_context.js";
// TODO: Deduplicate
type Response = {
success: true;
} | {
success: false;
message: string;
}
2021-10-05 22:08:02 +02:00
2021-12-20 17:30:47 +01:00
export default class BookmarkSwitchWidget extends SwitchWidget {
2022-12-04 13:16:05 +01:00
isEnabled() {
2025-01-09 18:07:02 +02:00
return (
super.isEnabled() &&
2022-12-04 13:16:05 +01:00
// it's not possible to bookmark root because that would clone it under bookmarks and thus create a cycle
!["root", "_hidden"].includes(this.noteId ?? "")
2025-01-09 18:07:02 +02:00
);
2022-12-04 13:16:05 +01:00
}
2021-12-20 17:30:47 +01:00
doRender() {
super.doRender();
2021-10-05 22:08:02 +02:00
this.switchOnName = t("bookmark_switch.bookmark");
this.switchOnTooltip = t("bookmark_switch.bookmark_this_note");
2021-10-05 22:08:02 +02:00
this.switchOffName = t("bookmark_switch.bookmark");
this.switchOffTooltip = t("bookmark_switch.remove_bookmark");
2021-12-20 17:30:47 +01:00
}
2021-10-05 22:08:02 +02:00
async toggle(state: boolean | null | undefined) {
const resp = await server.put<Response>(`notes/${this.noteId}/toggle-in-parent/_lbBookmarks/${!!state}`);
2021-10-05 22:08:02 +02:00
if (!resp.success && "message" in resp) {
2022-12-04 13:16:05 +01:00
toastService.showError(resp.message);
}
2021-10-05 22:08:02 +02:00
}
async refreshWithNote(note: FNote) {
2025-01-09 18:07:02 +02:00
const isBookmarked = !!note.getParentBranches().find((b) => b.parentNoteId === "_lbBookmarks");
2021-10-05 22:08:02 +02:00
this.isToggled = isBookmarked;
2021-10-05 22:08:02 +02:00
}
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
2025-01-09 18:07:02 +02:00
if (loadResults.getBranchRows().find((b) => b.noteId === this.noteId)) {
2022-12-04 13:16:05 +01:00
this.refresh();
2021-10-05 22:08:02 +02:00
}
}
}