Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: allow the grouping attribute to be listed in the promoted attributes list in Board Properties

Adorian Dorand9ddf45b4bb9
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index ba10db1bad..036b695b34 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1412,7 +1412,8 @@
"remove_value": "Remove this value",
"select_values_placeholder": "Select an option or create one",
"values_placeholder": "Type a value and press Enter",
- "add_value": "Add this value"
+ "add_value": "Add this value",
+ "drawn_by_collection": "The collection is arranged by this, so its items do not repeat it"
},
"script_executor": {
"query": "Query",
diff --git a/apps/client/src/widgets/collections/attribute_menu.spec.ts b/apps/client/src/widgets/collections/attribute_menu.spec.ts
index 7083b4358d..f8fba6aa75 100644
--- a/apps/client/src/widgets/collections/attribute_menu.spec.ts
+++ b/apps/client/src/widgets/collections/attribute_menu.spec.ts
@@ -244,6 +244,7 @@ function attribute(fields: Partial<PromotedAttribute> & { name: string }): Promo
type: "label",
title: fields.name,
hidden: false,
+ drawnByCollection: false,
definitionValue: "",
isOwned: true,
isInheritable: true,
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index c7a18b79a0..a61144f45a 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -952,12 +952,22 @@ export default class BoardApi {
return this.viewConfig?.promotedAttributes;
}
- /** The promoted attributes the board defines, in the order the reader put them. */
- getPromotedAttributes() {
+ /**
+ * Every promoted attribute the board defines, in the order the reader put them, the one it
+ * groups by included: the properties dialog lists that one so it keeps its place among the
+ * others for whenever the board is grouped by something else.
+ */
+ getAllPromotedAttributes() {
return resolvePromotedAttributes(
this.parentNote, this.viewConfig?.promotedAttributes, [ this.statusAttribute ]);
}
+ /** Those a card can show or a column sort by, which the columns themselves stand for. */
+ getPromotedAttributes() {
+ return this.getAllPromotedAttributes()
+ .filter((attribute) => !attribute.drawnByCollection);
+ }
+
/** Which of them a card draws, in order. */
getVisiblePromotedAttributeNames() {
return visiblePromotedAttributeNames(this.getPromotedAttributes());
diff --git a/apps/client/src/widgets/collections/board/data.spec.ts b/apps/client/src/widgets/collections/board/data.spec.ts
index b0206a8d8c..12a9bbde69 100644
--- a/apps/client/src/widgets/collections/board/data.spec.ts
+++ b/apps/client/src/widgets/collections/board/data.spec.ts
@@ -656,6 +656,7 @@ function definitions(name: string, type: "label" | "relation") {
type,
title: name,
hidden: false,
+ drawnByCollection: false,
definitionValue: "",
isOwned: true,
isInheritable: true
diff --git a/apps/client/src/widgets/collections/board/properties.tsx b/apps/client/src/widgets/collections/board/properties.tsx
index 6b28ad74aa..9197d26f5f 100644
--- a/apps/client/src/widgets/collections/board/properties.tsx
+++ b/apps/client/src/widgets/collections/board/properties.tsx
@@ -49,7 +49,7 @@ export default function BoardProperties({ api, note, shown, onClose }: {
instruction={t("board_view.promoted-attributes-hint")}
note={note}
settings={api.getStoredPromotedAttributes()}
- ignored={[ api.statusAttribute ]}
+ drawnByCollection={[ api.statusAttribute ]}
onChange={storeAttributes}
/>
diff --git a/apps/client/src/widgets/collections/promoted_attributes.spec.ts b/apps/client/src/widgets/collections/promoted_attributes.spec.ts
index f57377bad7..f9a1dd0e81 100644
--- a/apps/client/src/widgets/collections/promoted_attributes.spec.ts
+++ b/apps/client/src/widgets/collections/promoted_attributes.spec.ts
@@ -57,6 +57,7 @@ describe("resolvePromotedAttributes", () => {
title: "Due",
promotedAlias: "Due",
hidden: false,
+ drawnByCollection: false,
definitionValue: "promoted,single,text",
isOwned: true,
isInheritable: true
@@ -172,12 +173,20 @@ describe("resolvePromotedAttributes", () => {
expect(resolved.map((attribute) => attribute.name)).toEqual([ "dueDate" ]);
});
- /** What the collection draws itself is not the reader's to arrange. */
- it("leaves out what the caller names as its own", () => {
+ /**
+ * What the collection draws itself is listed and arranged with the rest, so it keeps its place
+ * among them, but it is marked, and nothing shows it on an item.
+ */
+ it("marks what the caller names as its own rather than leaving it out", () => {
const resolved = resolvePromotedAttributes(
collection(DEFINED), [ { name: "owner" } ], [ "owner" ]);
expect(resolved.map((attribute) => attribute.name))
+ .toEqual([ "owner", "dueDate", "requiresResearch" ]);
+ expect(resolved.map((attribute) => attribute.drawnByCollection))
+ .toEqual([ true, false, false ]);
+ // However it is stored, it is never among what an item draws.
+ expect(visiblePromotedAttributeNames(resolved))
.toEqual([ "dueDate", "requiresResearch" ]);
});
diff --git a/apps/client/src/widgets/collections/promoted_attributes.ts b/apps/client/src/widgets/collections/promoted_attributes.ts
index ec8144d921..cb6d1fcee0 100644
--- a/apps/client/src/widgets/collections/promoted_attributes.ts
+++ b/apps/client/src/widgets/collections/promoted_attributes.ts
@@ -28,6 +28,12 @@ export interface PromotedAttribute {
promotedAlias?: string;
/** Whether the attribute is kept off the items. */
hidden: boolean;
+ /**
+ * Whether the collection draws it itself, a board's grouping label being drawn as its columns.
+ * Such an attribute is listed and arranged like the rest, but never shown on an item and never
+ * offered to be: it is already on the screen, in the shape the collection gives it.
+ */
+ drawnByCollection: boolean;
/** The definition as stored, which the attribute editor is handed to edit. */
definitionValue: string;
/** What the field holds: `text`, `date`, `boolean` and the rest. Absent for a relation. */
@@ -61,14 +67,14 @@ export function resolvePromotedAttributes(
note: FNote | null | undefined,
settings: PromotedAttributeSetting[] | undefined,
/** Names the collection draws itself, such as the label a board groups by. */
- ignored: string[] = []
+ drawnByCollection: string[] = []
): PromotedAttribute[] {
const defined = new Map<string, PromotedAttribute>();
for (const definition of note?.getAttributeDefinitions() ?? []) {
const [ type, name ] = definition.name.split(":", 2);
if ((type !== "label" && type !== "relation") || !name || defined.has(name)
- || ignored.includes(name) || !definition.isInheritable) {
+ || !definition.isInheritable) {
continue;
}
@@ -82,6 +88,7 @@ export function resolvePromotedAttributes(
labelType: parsed?.labelType,
selectOptions: parsed?.selectOptions,
hidden: false,
+ drawnByCollection: drawnByCollection.includes(name),
definitionValue: definition.value,
isOwned: definition.noteId === note?.noteId,
isInheritable: definition.isInheritable
@@ -118,7 +125,9 @@ function defaultTitle(type: "label" | "relation", name: string) {
/** The attributes drawn on an item, in order, for a view showing their values. */
export function visiblePromotedAttributeNames(attributes: PromotedAttribute[]) {
- return attributes.filter((attribute) => !attribute.hidden).map((attribute) => attribute.name);
+ return attributes
+ .filter((attribute) => !attribute.hidden && !attribute.drawnByCollection)
+ .map((attribute) => attribute.name);
}
/** What a collection stores for the attributes it has resolved. */
diff --git a/apps/client/src/widgets/collections/sort_menu.spec.ts b/apps/client/src/widgets/collections/sort_menu.spec.ts
index a5efc4561f..fb3d54fda0 100644
--- a/apps/client/src/widgets/collections/sort_menu.spec.ts
+++ b/apps/client/src/widgets/collections/sort_menu.spec.ts
@@ -229,6 +229,7 @@ function attribute(fields: Partial<PromotedAttribute> & { name: string }): Promo
type: "label",
title: fields.name,
hidden: false,
+ drawnByCollection: false,
definitionValue: "",
isOwned: true,
isInheritable: true,
diff --git a/apps/client/src/widgets/collections/sorting.spec.ts b/apps/client/src/widgets/collections/sorting.spec.ts
index 1bc69d76c3..096ff00f6d 100644
--- a/apps/client/src/widgets/collections/sorting.spec.ts
+++ b/apps/client/src/widgets/collections/sorting.spec.ts
@@ -272,6 +272,7 @@ function sort(
type: isRelation ? "relation" : "label",
title: "Field",
hidden: false,
+ drawnByCollection: false,
definitionValue: "",
labelType: isRelation ? undefined : fieldType,
selectOptions,
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
index 4b42122ace..3498ae5819 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
@@ -82,11 +82,13 @@ describe("PromotedAttributesCard", () => {
let host: Component;
let stored: PromotedAttribute[][];
let settings: PromotedAttributeSetting[] | undefined;
+ let drawnByCollection: string[] | undefined;
beforeEach(() => {
vi.clearAllMocks();
stored = [];
settings = undefined;
+ drawnByCollection = undefined;
defined = [ definition("label:dueDate", { alias: "Due" }), definition("label:owner") ];
mocks.detail.opts = null;
mocks.confirm.mockResolvedValue(true);
@@ -126,6 +128,26 @@ describe("PromotedAttributesCard", () => {
]);
});
+ /**
+ * A collection draws one of them itself, a board's grouping label standing as its columns. It
+ * is listed and arranged with the rest, so it holds its place for whenever the collection is
+ * arranged by something else, but it is never shown on an item and its switch says so.
+ */
+ it("lists what the collection draws itself, with its switch held off", () => {
+ settings = [ { name: "owner" }, { name: "dueDate" } ];
+ drawnByCollection = [ "owner" ];
+ draw();
+
+ expect(names()).toEqual([ unnamed("owner"), "Due" ]);
+ expect(shown()).toEqual([ false, true ]);
+ expect(locked()).toEqual([ true, false ]);
+
+ // Pressing it changes nothing, so what the collection stores is left alone.
+ toggle(0);
+ expect(shown()).toEqual([ false, true ]);
+ expect(stored).toEqual([]);
+ });
+
it("follows the order the settings give, and turns off what they hide", () => {
settings = [ { name: "owner", hidden: true }, { name: "dueDate" } ];
draw();
@@ -365,6 +387,7 @@ describe("PromotedAttributesCard", () => {
instruction="Pick what the items show."
note={NOTE}
settings={settings}
+ drawnByCollection={drawnByCollection}
onChange={(attributes) => stored.push(attributes)}
/>
</ParentComponent.Provider>,
@@ -397,6 +420,12 @@ describe("PromotedAttributesCard", () => {
!!segment.querySelector(".switch-button")?.classList.contains("on"));
}
+ /** Which entries offer no say over being shown, their switch being held off. */
+ function locked() {
+ return segments().map((segment) =>
+ !!segment.querySelector<HTMLInputElement>(".switch-button input")?.disabled);
+ }
+
function toggle(index: number) {
act(() => { segments()[index].querySelector<HTMLElement>(".switch-button")?.click(); });
}
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
index c4a7b3e964..593c72cda1 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.tsx
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
@@ -45,8 +45,11 @@ export interface PromotedAttributesCardProps {
note: FNote;
/** The order and what is hidden, as the collection's view config stores it. */
settings: PromotedAttributeSetting[] | undefined;
- /** Attributes the collection draws itself, such as the label a board groups by. */
- ignored?: string[];
+ /**
+ * Attributes the collection draws itself, such as the label a board groups by. Listed and
+ * arranged with the rest, but their switch is held off: they are already on the screen.
+ */
+ drawnByCollection?: string[];
/** Called with the whole list after every change, for the caller to store. */
onChange: (attributes: PromotedAttribute[]) => void;
}
@@ -59,9 +62,10 @@ export interface PromotedAttributesCardProps {
* reports the order and what is hidden, which the caller stores.
*/
export default function PromotedAttributesCard({
- heading, instruction, note, settings, ignored, onChange
+ heading, instruction, note, settings, drawnByCollection, onChange
}: PromotedAttributesCardProps) {
- const [ shown, setShown ] = useState(() => resolvePromotedAttributes(note, settings, ignored));
+ const [ shown, setShown ] =
+ useState(() => resolvePromotedAttributes(note, settings, drawnByCollection));
const [ detail, setDetail ] = useState<AttributeDetailOpts | null>(null);
/** The definition the editor last reported, which `save` writes. */
const edited = useRef<Attribute>();
@@ -75,7 +79,7 @@ export default function PromotedAttributesCard({
.some((attribute) => attributes.isAffecting(attribute, note));
if (affects) {
setShown((was) =>
- resolvePromotedAttributes(note, storedPromotedAttributes(was), ignored));
+ resolvePromotedAttributes(note, storedPromotedAttributes(was), drawnByCollection));
}
});
@@ -234,9 +238,12 @@ export default function PromotedAttributesCard({
/>
<FormToggle
- currentValue={!attribute.hidden}
+ currentValue={!attribute.hidden && !attribute.drawnByCollection}
+ disabled={attribute.drawnByCollection}
switchOnTooltip={t("promoted_attributes.shown_on_items")}
- switchOffTooltip={t("promoted_attributes.hidden_from_items")}
+ switchOffTooltip={attribute.drawnByCollection
+ ? t("promoted_attributes.drawn_by_collection")
+ : t("promoted_attributes.hidden_from_items")}
onChange={(visible) => setHidden(attribute.name, !visible)}
/>
</>