Skip to content

Build your personal knowledge base with Trilium Notes

custom task states: show the state name in toolbar for undefined custom task states

Adorian Dorand7f736dd4f28
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index c6faa62caa..a61dd6636a 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2659,6 +2659,7 @@
},
"text-editor": {
"checkbox-tooltip": "Right click for more task states. Press Ctrl+Shift+Enter to cycle between states.",
- "edit-states-tooltip": "Edit task states"
+ "edit-states-tooltip": "Edit task states",
+ "unknown-task-state": "Undefined custom task state"
}
}
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.ts
index d932380e74..2cffe88fc2 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
@@ -49,8 +49,9 @@ export default class TodoListMultistateEditing extends Plugin {
scope: "item",
attributeName: TASK_STATE_ATTRIBUTE,
setAttributeOnDowncast(writer, value, element) {
- // Only customizable states carry `data-trilium-task-state`; none/done are native.
- if (typeof value === "string" && stateByName.has(value) && !isAnchorState(value)) {
+ // Customizable states carry `data-trilium-task-state`; none/done are native.
+ // Unrecognized states are preserved so they survive a state-config change.
+ if (typeof value === "string" && value !== "" && !isAnchorState(value)) {
writer.setAttribute("data-trilium-task-state", value, element);
} else {
writer.removeAttribute("data-trilium-task-state", element);
@@ -64,7 +65,7 @@ export default class TodoListMultistateEditing extends Plugin {
key: TASK_STATE_ATTRIBUTE,
value: (viewElement: ViewElement) => {
const value = viewElement.getAttribute("data-trilium-task-state");
- return typeof value === "string" && stateByName.has(value) && !isAnchorState(value)
+ return typeof value === "string" && value !== "" && !isAnchorState(value)
? value
: null;
}
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.ts
index 2b85ee72d5..44c802f5f7 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
@@ -6,11 +6,12 @@ import {
Plugin,
ToolbarSeparatorView,
ToolbarView,
+ View,
clickOutsideHandler,
type ModelElement,
type ViewElement
} from "ckeditor5";
-import { getConfiguredTaskStates } from "./todo_list_multistate_editing.js";
+import { getConfiguredTaskStates, TASK_STATE_ATTRIBUTE } from "./todo_list_multistate_editing.js";
import TodoListMultistateUI from "./todo_list_multistate_ui.js";
class TodoCheckboxContextMenuObserver extends DomEventObserver<"contextmenu"> {
@@ -31,6 +32,7 @@ export default class TodoListMultistateToolbar extends Plugin {
private _balloon!: ContextualBalloon;
private _toolbarView!: ToolbarView;
private _targetItemId: string | null = null;
+ private _unknownStateItems: View[] = [];
init() {
const editor = this.editor;
@@ -83,14 +85,17 @@ export default class TodoListMultistateToolbar extends Plugin {
return toolbar;
}
+ private _translate(key: string): string {
+ const translate = this.editor.config.get("translate") as ((key: string) => string) | undefined;
+ return translate ? translate(key) : key;
+ }
+
private _createEditButton(): ButtonView {
const editor = this.editor;
const button = new ButtonView(editor.locale);
- const translate = (editor.config.get("translate") as ((key: string) => string) | undefined)
- ?? ((key: string) => key);
-
+
button.set({
- label: translate("text-editor.edit-states-tooltip"),
+ label: this._translate("text-editor.edit-states-tooltip"),
withText: false,
tooltip: true,
class: "ck-task-state-edit bx bx-pencil"
@@ -123,6 +128,14 @@ export default class TodoListMultistateToolbar extends Plugin {
writer.setSelection(writer.createPositionAt(block, 0));
});
+ const knownStateNames = new Set(getConfiguredTaskStates(editor).map((state) => state.name));
+ const taskState = block.getAttribute(TASK_STATE_ATTRIBUTE);
+ this._updateUnknownStateLabel(
+ typeof taskState === "string" && taskState !== "" && !knownStateNames.has(taskState)
+ ? taskState
+ : null
+ );
+
const position = {
target: anchorDom,
positions: [
@@ -167,6 +180,50 @@ export default class TodoListMultistateToolbar extends Plugin {
return null;
}
+ /**
+ * Appends (or clears) a "Unknown state: …" label at the end of the toolbar
+ * when the current todo item carries a state that is not configured.
+ */
+ private _updateUnknownStateLabel(stateName: string | null) {
+ for (const item of this._unknownStateItems) {
+ if (this._toolbarView.items.has(item)) {
+ this._toolbarView.items.remove(item);
+ }
+ item.destroy();
+ }
+ this._unknownStateItems = [];
+
+ if (!stateName) {
+ return;
+ }
+
+ const locale = this.editor.locale;
+ const separator = new ToolbarSeparatorView(locale);
+ const label = new View(locale);
+ label.setTemplate({
+ tag: "span",
+ attributes: {
+ class: "ck tn-task-state-unknown"
+ },
+ children: [
+ { text: `${this._translate("text-editor.unknown-task-state")}: ` },
+ {
+ tag: "span",
+ attributes: {
+ class: "tn-task-state-unknown-name ck-reset_all-excluded"
+ },
+ children: [
+ { text: stateName }
+ ]
+ }
+ ]
+ });
+
+ this._toolbarView.items.add(separator);
+ this._toolbarView.items.add(label);
+ this._unknownStateItems = [separator, label];
+ }
+
private _hide() {
if (this._isVisible()) {
this._balloon.remove(this._toolbarView);