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
text editor/checkboxes: add system states in hidden tree for ordering purposes
Adorian Doran6b6ed0cdc51d
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.tsindex 3fa32486d1..f5c93438da 100644--- a/apps/client/src/widgets/type_widgets/text/config.ts+++ b/apps/client/src/widgets/type_widgets/text/config.ts@@ -1,6 +1,6 @@import { buildExtraCommands, type EditorConfig, getCkLocale, loadPremiumPlugins, TemplateDefinition } from "@triliumnext/ckeditor5";import emojiDefinitionsUrl from "@triliumnext/ckeditor5/src/emoji_definitions/en.json?url";-import { ALLOWED_PROTOCOLS, DEFAULT_TASK_STATES, DISPLAYABLE_LOCALE_IDS, MIME_TYPE_AUTO, normalizeMimeTypeForCKEditor, type TaskStateDef } from "@triliumnext/commons";+import { ALLOWED_PROTOCOLS, DEFAULT_TASK_STATES, DISPLAYABLE_LOCALE_IDS, DONE_TASK_STATE, MIME_TYPE_AUTO, NONE_TASK_STATE, normalizeMimeTypeForCKEditor, type TaskStateDef } from "@triliumnext/commons";import froca from "../../../services/froca.js";import { copyTextWithToast } from "../../../services/clipboard_ext.js";@@ -237,15 +237,27 @@ async function fetchTaskStates(): Promise<TaskStateDef[]> {}const states = (await container.getChildNotes())- .map((note): TaskStateDef => ({- name: note.getLabelValue("stateName") ?? "",- title: note.title,- markdownSymbol: note.getLabelValue("markdownSymbol") ?? "",- checkboxValue: note.getLabelValue("checkboxValue") === "true",- color: note.getLabelValue("color") ?? "",- icon: note.getLabelValue("iconClass") ?? ""- }))- .filter((state) => state.name);+ .map((note): TaskStateDef | null => {+ if (note.noteId === "_taskStateNone") {+ return NONE_TASK_STATE;+ }+ if (note.noteId === "_taskStateDone") {+ return DONE_TASK_STATE;+ }+ const name = note.getLabelValue("stateName");+ if (!name) {+ return null;+ }+ return {+ name,+ title: note.title,+ markdownSymbol: note.getLabelValue("markdownSymbol") ?? "",+ checkboxValue: note.getLabelValue("checkboxValue") === "true",+ color: note.getLabelValue("color") ?? "",+ icon: note.getLabelValue("iconClass") ?? ""+ };+ })+ .filter((state): state is TaskStateDef => state !== null);return states.length ? states : DEFAULT_TASK_STATES;}diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.jsonindex 02fd7656d9..f3d74bcb39 100644--- a/apps/server/src/assets/translations/en/server.json+++ b/apps/server/src/assets/translations/en/server.json@@ -279,7 +279,9 @@"shared-notes-title": "Shared Notes","bulk-action-title": "Bulk Action","task-states-title": "Task States",+ "task-state-none": "None","task-state-doing": "Doing",+ "task-state-done": "Done","task-state-maybe": "Maybe","task-state-cancelled": "Cancelled","backend-log-title": "Backend Log",diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.tsindex 7d088c5dbc..ec82eba0a5 100644--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts@@ -1,4 +1,4 @@-import { DEFAULT_TASK_STATES, type TaskStateDef } from "@triliumnext/commons";+import { DEFAULT_TASK_STATES, DONE_STATE_NAME, isAnchorState, NONE_STATE_NAME, type TaskStateDef } from "@triliumnext/commons";import { Tooltip } from "bootstrap";import { Command, ListEditing, Plugin, TodoList, type Editor, type ModelElement, type ViewElement } from "ckeditor5";@@ -6,8 +6,8 @@ export const TASK_STATE_ATTRIBUTE = "taskState";const TODO_LIST_CHECKED_ATTRIBUTE = "todoListChecked";/**- * The configured non-binary task states. `none` (unchecked) and `done` (checked)- * are CKEditor's native checkbox behaviour and are not part of this list.+ * The ordered task states. Includes the built-in `none`/`done` anchors — those+ * are never written as `data-task-state`; they map to the native checkbox.*/export function getConfiguredTaskStates(editor: Editor): TaskStateDef[] {const states = editor.config.get("taskStates") as TaskStateDef[] | undefined;@@ -34,8 +34,8 @@ export default class TodoListMultistateEditing extends Plugin {if (!command?.isEnabled) {return;}- const cycle: (string | null)[] = [null, ...states.map((state) => state.name)];- const current = (command.value as string | null) ?? null;+ const cycle = states.map((state) => state.name);+ const current = (command.value as string | null) ?? NONE_STATE_NAME;const idx = cycle.indexOf(current);const next = cycle[(idx + 1) % cycle.length];editor.execute("setTaskState", {state: next});@@ -47,7 +47,8 @@ export default class TodoListMultistateEditing extends Plugin {scope: "item",attributeName: TASK_STATE_ATTRIBUTE,setAttributeOnDowncast(writer, value, element) {- if (typeof value === "string" && stateByName.has(value)) {+ // Only customizable states carry `data-task-state`; none/done are native.+ if (typeof value === "string" && stateByName.has(value) && !isAnchorState(value)) {writer.setAttribute("data-task-state", value, element);} else {writer.removeAttribute("data-task-state", element);@@ -61,7 +62,9 @@ export default class TodoListMultistateEditing extends Plugin {key: TASK_STATE_ATTRIBUTE,value: (viewElement: ViewElement) => {const value = viewElement.getAttribute("data-task-state");- return typeof value === "string" && stateByName.has(value) ? value : null;+ return typeof value === "string" && stateByName.has(value) && !isAnchorState(value)+ ? value+ : null;}}});@@ -103,12 +106,12 @@ export default class TodoListMultistateEditing extends Plugin {let changed = false;- // A configured state forces the checkbox to its `checkboxValue`.+ // A customizable state forces the checkbox to its `checkboxValue`.for (const el of stateChanged) {const stateName = el.getAttribute(TASK_STATE_ATTRIBUTE);const state = typeof stateName === "string" ? stateByName.get(stateName) : undefined;if (!state) {- // State cleared — leave the native checkbox (none/done) untouched.+ // State cleared — the command already set the native checkbox.continue;}if (!!el.getAttribute(TODO_LIST_CHECKED_ATTRIBUTE) !== state.checkboxValue) {@@ -146,19 +149,27 @@ class SetTaskStateCommand extends Command {return;}const stored = block.getAttribute(TASK_STATE_ATTRIBUTE);- this.value = typeof stored === "string" ? stored : null;+ if (typeof stored === "string") {+ this.value = stored;+ } else {+ this.value = block.getAttribute(TODO_LIST_CHECKED_ATTRIBUTE) ? DONE_STATE_NAME : NONE_STATE_NAME;+ }}execute(options: {state: string | null}) {const model = this.editor.model;- const {state} = options;+ const state = options.state ?? NONE_STATE_NAME;model.change((writer) => {for (const block of model.document.selection.getSelectedBlocks()) {if (block.getAttribute("listType") !== "todo") {continue;}- if (!state || state === "none") {+ if (state === NONE_STATE_NAME) {+ writer.removeAttribute(TASK_STATE_ATTRIBUTE, block);+ writer.setAttribute(TODO_LIST_CHECKED_ATTRIBUTE, false, block);+ } else if (state === DONE_STATE_NAME) {writer.removeAttribute(TASK_STATE_ATTRIBUTE, block);+ writer.setAttribute(TODO_LIST_CHECKED_ATTRIBUTE, true, block);} else {writer.setAttribute(TASK_STATE_ATTRIBUTE, state, block);}diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.tsindex a8279ae801..b616943944 100644--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.ts+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.ts@@ -73,7 +73,6 @@ export default class TodoListMultistateToolbar extends Plugin {const editor = this.editor;const toolbar = new ToolbarView(editor.locale);toolbar.class = "task-state-toolbar";- toolbar.items.add(editor.ui.componentFactory.create("taskState:none"));for (const state of getConfiguredTaskStates(editor)) {toolbar.items.add(editor.ui.componentFactory.create(`taskState:${state.name}`));}diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.tsindex df8201a18d..091ed0ee02 100644--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.ts+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.ts@@ -11,24 +11,6 @@ export default class TodoListMultistateUI extends Plugin {const editor = this.editor;const command = editor.commands.get("setTaskState")!;- // Clear button — drops back to the native none/done checkbox.- editor.ui.componentFactory.add("taskState:none", (locale) => {- const button = new ButtonView(locale);- button.set({- label: editor.t("Clear task state"),- withText: false,- tooltip: true,- class: "ck-task-state-button ck-task-state-none"- });- button.bind("isOn").to(command, "value", (value) => value === null);- button.bind("isEnabled").to(command, "isEnabled");- button.on("execute", () => {- editor.execute("setTaskState", {state: null});- editor.editing.view.focus();- });- return button;- });-for (const state of getConfiguredTaskStates(editor)) {editor.ui.componentFactory.add(`taskState:${state.name}`, (locale) => {const button = new ButtonView(locale);diff --git a/packages/commons/src/lib/task_states.ts b/packages/commons/src/lib/task_states.tsindex 5c9626c53d..4a3b553815 100644--- a/packages/commons/src/lib/task_states.ts+++ b/packages/commons/src/lib/task_states.ts@@ -1,9 +1,10 @@/**- * Definition of a configurable todo-list task state.+ * Definition of a todo-list task state.*- * The built-in `none` (unchecked) and `done` (checked) states are NOT task- * states — they are CKEditor's native checkbox behaviour. Task states only- * cover the non-binary states (e.g. "doing", "maybe", "cancelled").+ * States are ordered. Two of them are built-in, non-customizable **anchors**:+ * `none` (unchecked) and `done` (checked) — CKEditor's native checkbox states.+ * They exist as notes only so the user can position them relative to the+ * customizable states for toolbar order and keyboard cycling.*/export interface TaskStateDef {/** The `stateName` label value — used verbatim as the `data-task-state` attribute. */@@ -20,13 +21,44 @@ export interface TaskStateDef {icon: string;}+/** Reserved name of the built-in unchecked anchor state. */+export const NONE_STATE_NAME = "none";+/** Reserved name of the built-in checked anchor state. */+export const DONE_STATE_NAME = "done";++/** Whether `name` is a built-in anchor state (`none`/`done`) rather than a customizable one. */+export function isAnchorState(name: string): boolean {+ return name === NONE_STATE_NAME || name === DONE_STATE_NAME;+}++/** The built-in unchecked anchor state. */+export const NONE_TASK_STATE: TaskStateDef = {+ name: NONE_STATE_NAME, title: "None", markdownSymbol: " ", checkboxValue: false, color: "", icon: "bx bx-checkbox"+};++/** The built-in checked anchor state. */+export const DONE_TASK_STATE: TaskStateDef = {+ name: DONE_STATE_NAME, title: "Done", markdownSymbol: "x", checkboxValue: true, color: "", icon: "bx bx-checkbox-checked"+};+/**- * Fallback task states, used wherever the user-defined states (stored as notes- * in the `_taskStates` hidden subtree) are not reachable, and seeded as the- * defaults on a fresh installation.+ * The user-customizable default states, seeded under `_taskStates` on a fresh+ * installation.*/-export const DEFAULT_TASK_STATES: TaskStateDef[] = [+export const DEFAULT_CUSTOM_TASK_STATES: TaskStateDef[] = [{name: "doing", title: "Doing", markdownSymbol: "/", checkboxValue: false, color: "#e6a23c", icon: "bx bx-loader"},{name: "maybe", title: "Maybe", markdownSymbol: "?", checkboxValue: false, color: "#a06cd5", icon: "bx bx-help-circle"},{name: "cancelled", title: "Cancelled", markdownSymbol: "-", checkboxValue: false, color: "#909399", icon: "bx bx-x"}];++/**+ * The full default ordered sequence — `None, Doing, Done, Maybe, Cancelled`.+ * Used as the fallback wherever the user-defined states are not reachable.+ */+export const DEFAULT_TASK_STATES: TaskStateDef[] = [+ NONE_TASK_STATE,+ DEFAULT_CUSTOM_TASK_STATES[0],+ DONE_TASK_STATE,+ DEFAULT_CUSTOM_TASK_STATES[1],+ DEFAULT_CUSTOM_TASK_STATES[2]+];diff --git a/packages/trilium-core/src/services/hidden_subtree.ts b/packages/trilium-core/src/services/hidden_subtree.tsindex d1181adb59..33fa476292 100644--- a/packages/trilium-core/src/services/hidden_subtree.ts+++ b/packages/trilium-core/src/services/hidden_subtree.ts@@ -96,6 +96,24 @@ function buildHiddenSubtreeDefinition(helpSubtree: HiddenSubtreeItem[]): HiddenS{ type: "label", name: "child:label:markdownSymbol", value: "promoted,single,text" },{ type: "label", name: "child:label:checkboxValue", value: "promoted,single,bool" },{ type: "label", name: "child:label:color", value: "promoted,single,text" }+ ],+ // Non-customizable anchor states — recreated if missing; they only+ // determine where `none`/`done` sit in the toolbar/cycling order.+ children: [+ {+ id: "_taskStateNone",+ title: t("hidden-subtree.task-state-none"),+ type: "doc",+ icon: "bx-checkbox",+ attributes: [{ type: "label", name: "hidePromotedAttributes" }]+ },+ {+ id: "_taskStateDone",+ title: t("hidden-subtree.task-state-done"),+ type: "doc",+ icon: "bx-checkbox-checked",+ attributes: [{ type: "label", name: "hidePromotedAttributes" }]+ }]},{diff --git a/packages/trilium-core/src/services/task_states.ts b/packages/trilium-core/src/services/task_states.tsindex d86cb26eb0..b871258642 100644--- a/packages/trilium-core/src/services/task_states.ts+++ b/packages/trilium-core/src/services/task_states.ts@@ -1,4 +1,4 @@-import { DEFAULT_TASK_STATES, type TaskStateDef } from "@triliumnext/commons";+import { DEFAULT_CUSTOM_TASK_STATES, DEFAULT_TASK_STATES, DONE_TASK_STATE, NONE_TASK_STATE, type TaskStateDef } from "@triliumnext/commons";import { t } from "i18next";import becca from "../becca/becca.js";@@ -6,11 +6,18 @@ import BAttribute from "../becca/entities/battribute.js";import noteService from "./notes.js";export const TASK_STATES_CONTAINER_ID = "_taskStates";+const NONE_NOTE_ID = "_taskStateNone";+const DONE_NOTE_ID = "_taskStateDone";++function noteIdForState(name: string): string {+ return `_taskState${name.charAt(0).toUpperCase()}${name.slice(1)}`;+}/**- * Returns the user-defined task states from the `_taskStates` hidden subtree,- * in note order. Falls back to {@link DEFAULT_TASK_STATES} when the container- * is missing or empty.+ * Returns the task states from the `_taskStates` hidden subtree, in note order.+ * The `none`/`done` anchor notes map to their fixed built-in definitions; other+ * children are read from their promoted attributes. Falls back to+ * {@link DEFAULT_TASK_STATES} when the container is missing or empty.*/export function getTaskStates(): TaskStateDef[] {const container = becca.notes[TASK_STATES_CONTAINER_ID];@@ -19,34 +26,47 @@ export function getTaskStates(): TaskStateDef[] {}const states = container.getChildNotes()- .map((note): TaskStateDef => ({- name: note.getLabelValue("stateName") ?? "",- title: note.title,- markdownSymbol: note.getLabelValue("markdownSymbol") ?? "",- checkboxValue: note.getLabelValue("checkboxValue") === "true",- color: note.getLabelValue("color") ?? "",- icon: note.getLabelValue("iconClass") ?? ""- }))- .filter((state) => state.name);+ .map((note): TaskStateDef | null => {+ if (note.noteId === NONE_NOTE_ID) {+ return NONE_TASK_STATE;+ }+ if (note.noteId === DONE_NOTE_ID) {+ return DONE_TASK_STATE;+ }+ const name = note.getLabelValue("stateName");+ if (!name) {+ return null;+ }+ return {+ name,+ title: note.title,+ markdownSymbol: note.getLabelValue("markdownSymbol") ?? "",+ checkboxValue: note.getLabelValue("checkboxValue") === "true",+ color: note.getLabelValue("color") ?? "",+ icon: note.getLabelValue("iconClass") ?? ""+ };+ })+ .filter((state): state is TaskStateDef => state !== null);return states.length ? states : DEFAULT_TASK_STATES;}/**- * Seeds the default task-state notes under the `_taskStates` container. Called- * exactly once — when the container is first created — so user deletions stick.+ * Seeds the customizable default task states under the `_taskStates` container+ * and applies the default ordering across all states (anchors included). Called+ * exactly once — when the container is first created — so user changes stick.*/export function seedDefaultTaskStates() {- const seeds: Array<{noteId: string; title: string; state: TaskStateDef}> = [- {noteId: "_taskStateDoing", title: t("hidden-subtree.task-state-doing"), state: DEFAULT_TASK_STATES[0]},- {noteId: "_taskStateMaybe", title: t("hidden-subtree.task-state-maybe"), state: DEFAULT_TASK_STATES[1]},- {noteId: "_taskStateCancelled", title: t("hidden-subtree.task-state-cancelled"), state: DEFAULT_TASK_STATES[2]}- ];+ const titleKeys: Record<string, string> = {+ doing: "hidden-subtree.task-state-doing",+ maybe: "hidden-subtree.task-state-maybe",+ cancelled: "hidden-subtree.task-state-cancelled"+ };- for (const seed of seeds) {+ for (const state of DEFAULT_CUSTOM_TASK_STATES) {const {note} = noteService.createNewNote({- noteId: seed.noteId,- title: seed.title,+ noteId: noteIdForState(state.name),+ title: t(titleKeys[state.name]),type: "text",parentNoteId: TASK_STATES_CONTAINER_ID,content: "",@@ -54,11 +74,11 @@ export function seedDefaultTaskStates() {});const labels: Record<string, string> = {- iconClass: seed.state.icon,- stateName: seed.state.name,- markdownSymbol: seed.state.markdownSymbol,- checkboxValue: String(seed.state.checkboxValue),- color: seed.state.color+ iconClass: state.icon,+ stateName: state.name,+ markdownSymbol: state.markdownSymbol,+ checkboxValue: String(state.checkboxValue),+ color: state.color};for (const [name, value] of Object.entries(labels)) {@@ -71,4 +91,14 @@ export function seedDefaultTaskStates() {}).save();}}++ // Apply the default ordering (None, Doing, Done, Maybe, Cancelled) across all states.+ DEFAULT_TASK_STATES.forEach((state, index) => {+ const note = becca.notes[noteIdForState(state.name)];+ const branch = note?.getParentBranches().find((b) => b.parentNoteId === TASK_STATES_CONTAINER_ID);+ if (branch) {+ branch.notePosition = index * 10;+ branch.save();+ }+ });}