Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
style(sort): keep the branch's lines under 100 and sort the dialog's imports
BeatLinkdf6ee7b989bc
diff --git a/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsx b/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsxindex d52f7cac85..e5d76cc668 100644--- a/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsx+++ b/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsx@@ -36,7 +36,9 @@ describe("SortChildNotesDialog", () => {</ParentComponent.Provider>);await act(async () => {- void host.handleEventInChildren("sortChildNotes", { noteId: "parent", selectedOrActiveBranchIds: [] });+ void host.handleEventInChildren("sortChildNotes", {+ noteId: "parent", selectedOrActiveBranchIds: []+ });});});diff --git a/apps/client/src/widgets/dialogs/sort_child_notes.tsx b/apps/client/src/widgets/dialogs/sort_child_notes.tsxindex fb1f84ea0d..13fcbeccac 100644--- a/apps/client/src/widgets/dialogs/sort_child_notes.tsx+++ b/apps/client/src/widgets/dialogs/sort_child_notes.tsx@@ -1,17 +1,18 @@import "./sort_child_notes.css";import { useState } from "preact/hooks";+import { t } from "../../services/i18n";+import server from "../../services/server";import ActionButton from "../react/ActionButton";import Button from "../react/Button";import FormCheckbox from "../react/FormCheckbox";+import FormGroup from "../react/FormGroup";import FormSelect from "../react/FormSelect";import FormTextBox from "../react/FormTextBox";+import { useTriliumEvent } from "../react/hooks";import Modal from "../react/Modal";import SegmentedChoice from "../react/SegmentedChoice";-import server from "../../services/server";-import FormGroup from "../react/FormGroup";-import { useTriliumEvent } from "../react/hooks";type SortKind = "title" | "dateCreated" | "dateModified" | "label";@@ -45,10 +46,22 @@ export default function SortChildNotesDialog() {function moveLevel(index: number, offset: -1 | 1) {const reordered = [ ...levels ];- [ reordered[index], reordered[index + offset] ] = [ reordered[index + offset], reordered[index] ];+ const other = index + offset;+ [ reordered[index], reordered[other] ] = [ reordered[other], reordered[index] ];setLevels(reordered);}+ const kindOptions = [+ { kind: "title", title: t("sort_child_notes.title") },+ { kind: "dateCreated", title: t("sort_child_notes.date_created") },+ { kind: "dateModified", title: t("sort_child_notes.date_modified") },+ { kind: "label", title: t("sort_child_notes.label") }+ ];+ const directionOptions = [+ { value: "asc", icon: "bx-sort-up", title: t("sort_child_notes.ascending") },+ { value: "desc", icon: "bx-sort-down", title: t("sort_child_notes.descending") }+ ];+async function onSubmit() {await server.put(`notes/${parentNoteId}/sort-children`, {sortBy: serializeSortLevels(levels),@@ -76,19 +89,16 @@ export default function SortChildNotesDialog() {><div className="sort-criteria"><h5>{t("sort_child_notes.sorting_criteria")}</h5>- <p className="sort-levels-description">{t("sort_child_notes.sorting_criteria_description")}</p>+ <p className="sort-levels-description">+ {t("sort_child_notes.sorting_criteria_description")}+ </p><div className="sort-levels">{levels.map((level, index) => (<div className="sort-level" key={index}><FormSelectclassName="sort-level-kind"- values={[- { key: "title", title: t("sort_child_notes.title") },- { key: "dateCreated", title: t("sort_child_notes.date_created") },- { key: "dateModified", title: t("sort_child_notes.date_modified") },- { key: "label", title: t("sort_child_notes.label") }- ]}- keyProperty="key"+ values={kindOptions}+ keyProperty="kind"titleProperty="title"currentValue={level.kind}onChange={(kind) => updateLevel(index, { kind: kind as SortKind })}@@ -103,12 +113,11 @@ export default function SortChildNotesDialog() {/>)}<SegmentedChoice- options={[- { value: "asc", icon: "bx-sort-up", title: t("sort_child_notes.ascending") },- { value: "desc", icon: "bx-sort-down", title: t("sort_child_notes.descending") }- ]}+ options={directionOptions}currentValue={level.descending ? "desc" : "asc"}- onChange={(direction) => updateLevel(index, { descending: direction === "desc" })}+ onChange={(direction) => {+ updateLevel(index, { descending: direction === "desc" });+ }}/><ActionButtonclassName="sort-level-up"@@ -167,7 +176,10 @@ export default function SortChildNotesDialog() {/** Writes the levels in the `#sorted` format the server parses, e.g. `priority:desc,title:asc`. */export function serializeSortLevels(levels: SortLevel[]) {return levels- .map((level) => ({ ...level, key: level.kind === "label" ? level.labelName.trim() : level.kind }))+ .map((level) => ({+ ...level,+ key: level.kind === "label" ? level.labelName.trim() : level.kind+ })).filter((level) => level.key).map((level) => `${level.key}:${level.descending ? "desc" : "asc"}`).join(",");diff --git a/packages/trilium-core/src/services/backend_script_api.ts b/packages/trilium-core/src/services/backend_script_api.tsindex 797bd8609e..a6a64ec942 100644--- a/packages/trilium-core/src/services/backend_script_api.ts+++ b/packages/trilium-core/src/services/backend_script_api.ts@@ -344,9 +344,9 @@ export interface Api {sortNotes(parentNoteId: string,sortConfig: {- /** Comma-separated levels of 'title', 'dateCreated', 'dateModified' or a label name, each with an- * optional ':asc' or ':desc' flag, e.g. 'priority:desc,dueDate'. The format is that of the- * #sorted label, see the "Sorting Notes" page of the User Guide. */+ /** Comma-separated levels of 'title', 'dateCreated', 'dateModified' or a label name,+ * each with an optional ':asc' or ':desc' flag, e.g. 'priority:desc,dueDate'. The+ * format is that of the #sorted label, see "Sorting Notes" in the User Guide. */sortBy?: string;/** Sorts the levels without a flag of their own descending. */reverse?: boolean;diff --git a/packages/trilium-core/src/services/handlers.spec.ts b/packages/trilium-core/src/services/handlers.spec.tsindex 8a4ec524e7..16777649f6 100644--- a/packages/trilium-core/src/services/handlers.spec.ts+++ b/packages/trilium-core/src/services/handlers.spec.ts@@ -116,11 +116,15 @@ describe("handlers", () => {addAttribute("ml-par", "label", "sorted", "priority:desc,dueDate");const unrelated = addAttribute("ml-chld", "label", "priorityNote", "x");- eventService.emit(eventService.ENTITY_CHANGED, { entityName: "attributes", entity: unrelated });+ eventService.emit(eventService.ENTITY_CHANGED, {+ entityName: "attributes", entity: unrelated+ });expect(sortNotesIfNeeded).not.toHaveBeenCalledWith("ml-par");const dueDate = addAttribute("ml-chld", "label", "dueDate", "2026-01-01");- eventService.emit(eventService.ENTITY_CHANGED, { entityName: "attributes", entity: dueDate });+ eventService.emit(eventService.ENTITY_CHANGED, {+ entityName: "attributes", entity: dueDate+ });expect(sortNotesIfNeeded).toHaveBeenCalledWith("ml-par");});diff --git a/packages/trilium-core/src/services/tree.spec.ts b/packages/trilium-core/src/services/tree.spec.tsindex 16cd1dd860..639b808800 100644--- a/packages/trilium-core/src/services/tree.spec.ts+++ b/packages/trilium-core/src/services/tree.spec.ts@@ -195,7 +195,7 @@ describe("Tree", () => {expect(orderedTitles).toStrictEqual(["a", "b", "e", "c", "d"]);});- it("applies #sortDirection to the levels without a direction of their own and to the title tiebreak", () => {+ it("applies #sortDirection to the levels without their own and to the title tiebreak", () => {const note = buildNote({children: [{title: "a", "#priority": "1"},diff --git a/packages/trilium-core/src/services/tree.ts b/packages/trilium-core/src/services/tree.tsindex 5ad4fe6fb2..512c567585 100644--- a/packages/trilium-core/src/services/tree.ts+++ b/packages/trilium-core/src/services/tree.ts@@ -80,18 +80,18 @@ function wouldAddingBranchCreateCycle(parentNoteId: string, childNoteId: string)export interface SortCriterion {/** `title`, `dateCreated`, `dateModified` or the name of a label on the child notes. */key: string;- /** Set by an explicit `:asc`/`:desc` flag; `undefined` follows the parent's `#sortDirection`. */+ /** Set by an explicit `:asc`/`:desc` flag; `undefined` follows `#sortDirection`. */descending?: boolean;}/**- * Parses a `#sorted` value such as `priority:desc,dueDate,title` into its levels: comma-separated sort- * keys, each optionally followed by `:asc` or `:desc`. An empty value sorts by title.+ * Parses a `#sorted` value such as `priority:desc,dueDate,title` into its levels: comma-separated+ * sort keys, each optionally followed by `:asc` or `:desc`. An empty value sorts by title.*/export function parseSortCriteria(value: string | null | undefined): SortCriterion[] {const criteria: SortCriterion[] = [];for (const level of (value ?? "").split(",")) {- // Only a trailing flag is a direction, so a label name that contains a colon still works as a key.+ // Only a trailing flag is a direction, so a label name with a colon still works as a key.const separator = level.lastIndexOf(":");const flag = separator === -1 ? "" : level.slice(separator + 1).trim().toLowerCase();const isDirection = flag === "asc" || flag === "desc";@@ -104,11 +104,18 @@ export function parseSortCriteria(value: string | null | undefined): SortCriteri}/**- * Sorts the children of `parentNoteId` by the levels of `sortBy` (see {@link parseSortCriteria}); a level- * without its own direction and the final title tiebreak follow `reverse`. `#top`, `#bottom` and folders- * take precedence over every level and ignore the direction.+ * Sorts the children of `parentNoteId` by the levels of `sortBy` (see {@link parseSortCriteria});+ * a level without its own direction and the final title tiebreak follow `reverse`. `#top`,+ * `#bottom` and folders take precedence over every level and ignore the direction.*/-function sortNotes(parentNoteId: string, sortBy: string = "title", reverse = false, foldersFirst = false, sortNatural = false, _sortLocale?: string | null) {+function sortNotes(+ parentNoteId: string,+ sortBy: string = "title",+ reverse = false,+ foldersFirst = false,+ sortNatural = false,+ _sortLocale?: string | null+) {const criteria = parseSortCriteria(sortBy);// sortLocale can not be empty string or null value, default value must be set to undefined.@@ -127,11 +134,14 @@ function sortNotes(parentNoteId: string, sortBy: string = "title", reverse = fallet rawValue: string | null;if (key === "title") {- const branch = note.getParentBranches().find((branch) => branch.parentNoteId === parentNoteId);+ const branch = note.getParentBranches()+ .find((branch) => branch.parentNoteId === parentNoteId);const prefix = branch?.prefix;rawValue = prefix ? `${prefix} - ${note.title}` : note.title;} else {- rawValue = ["dateCreated", "dateModified"].includes(key) ? (note as any)[key] : note.getLabelValue(key);+ rawValue = ["dateCreated", "dateModified"].includes(key)+ ? (note as any)[key]+ : note.getLabelValue(key);}return typeof rawValue === "string" ? rawValue.toLowerCase() : rawValue;