Skip to content

Build your personal knowledge base with Trilium Notes

refactor(book_properties): move rendering to book_properties

Elian Doran51b7955ccd5c
diff --git a/apps/client/src/widgets/ribbon_widgets/book_properties.ts b/apps/client/src/widgets/ribbon_widgets/book_properties.ts
index 9dd25c3ee2..39c4e92b20 100644
--- a/apps/client/src/widgets/ribbon_widgets/book_properties.ts
+++ b/apps/client/src/widgets/ribbon_widgets/book_properties.ts
@@ -3,7 +3,8 @@ import attributeService from "../../services/attributes.js";
import { t } from "../../services/i18n.js";
import type FNote from "../../entities/fnote.js";
import type { EventData } from "../../components/app_context.js";
-import { bookPropertiesConfig, renderBookProperty } from "./book_properties_config.js";
+import { bookPropertiesConfig, BookProperty } from "./book_properties_config.js";
+import attributes from "../../services/attributes.js";
const TPL = /*html*/`
<div class="book-properties-widget">
@@ -98,10 +99,7 @@ export default class BookPropertiesWidget extends NoteContextAwareWidget {
const bookPropertiesData = bookPropertiesConfig[viewType];
if (bookPropertiesData) {
for (const property of bookPropertiesData.properties) {
- this.$propertiesContainer.append(renderBookProperty(property, {
- note: this.note,
- triggerCommand: this.triggerCommand.bind(this)
- }));
+ this.$propertiesContainer.append(this.renderBookProperty(property));
this.labelsToWatch.push(property.bindToLabel);
}
}
@@ -126,4 +124,54 @@ export default class BookPropertiesWidget extends NoteContextAwareWidget {
this.refresh();
}
}
+
+ renderBookProperty(property: BookProperty) {
+ const $container = $("<div>");
+ const note = this.note;
+ if (!note) {
+ return $container;
+ }
+ switch (property.type) {
+ case "checkbox":
+ const $label = $("<label>").text(property.label);
+ const $checkbox = $("<input>", {
+ type: "checkbox",
+ class: "form-check-input",
+ });
+ $checkbox.on("change", () => {
+ if ($checkbox.prop("checked")) {
+ attributes.setLabel(note.noteId, property.bindToLabel);
+ } else {
+ attributes.removeOwnedLabelByName(note, property.bindToLabel);
+ }
+ });
+ $checkbox.prop("checked", note.hasOwnedLabel(property.bindToLabel));
+ $label.prepend($checkbox);
+ $container.append($label);
+ break;
+ case "button":
+ const $button = $("<button>", {
+ type: "button",
+ class: "btn btn-sm"
+ }).text(property.label);
+ if (property.title) {
+ $button.attr("title", property.title);
+ }
+ if (property.icon) {
+ $button.prepend($("<span>", { class: property.icon }));
+ }
+ $button.on("click", () => {
+ property.onClick({
+ note,
+ triggerCommand: this.triggerCommand.bind(this)
+ });
+ });
+ $container.append($button);
+ break;
+ }
+
+ return $container;
+ }
+
+
}
diff --git a/apps/client/src/widgets/ribbon_widgets/book_properties_config.ts b/apps/client/src/widgets/ribbon_widgets/book_properties_config.ts
index 30d8932688..97f400b6ad 100644
--- a/apps/client/src/widgets/ribbon_widgets/book_properties_config.ts
+++ b/apps/client/src/widgets/ribbon_widgets/book_properties_config.ts
@@ -4,7 +4,7 @@ import attributes from "../../services/attributes";
import { ViewTypeOptions } from "../../services/note_list_renderer"
import NoteContextAwareWidget from "../note_context_aware_widget";
-type BookProperty = CheckBoxProperty | ButtonProperty;
+export type BookProperty = CheckBoxProperty | ButtonProperty;
interface BookConfig {
properties: BookProperty[];
@@ -79,45 +79,3 @@ export const bookPropertiesConfig: Record<ViewTypeOptions, BookConfig> = {
]
}
};
-
-export function renderBookProperty(property: BookProperty, context: BookContext) {
- const $container = $("<div>");
- const { note } = context;
- switch (property.type) {
- case "checkbox":
- const $label = $("<label>").text(property.label);
- const $checkbox = $("<input>", {
- type: "checkbox",
- class: "form-check-input",
- });
- $checkbox.on("change", () => {
- if ($checkbox.prop("checked")) {
- attributes.setLabel(note.noteId, property.bindToLabel);
- } else {
- attributes.removeOwnedLabelByName(note, property.bindToLabel);
- }
- });
- $checkbox.prop("checked", note.hasOwnedLabel(property.bindToLabel));
- $label.prepend($checkbox);
- $container.append($label);
- break;
- case "button":
- const $button = $("<button>", {
- type: "button",
- class: "btn btn-sm"
- }).text(property.label);
- if (property.title) {
- $button.attr("title", property.title);
- }
- if (property.icon) {
- $button.prepend($("<span>", { class: property.icon }));
- }
- $button.on("click", () => {
- property.onClick(context);
- });
- $container.append($button);
- break;
- }
-
- return $container;
-}