Skip to content

Build your personal knowledge base with Trilium Notes

text editor/checkboxes: add class selection support

Adorian Doran71c6c22f8f5c
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index 5fef9821dd..4d4d665fb2 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -2545,6 +2545,14 @@ footer.webview-footer button {
opacity: 0.6;
}
+.ck-content ul.todo-list > li.todo-list-a {
+ background-color: rgba(255, 235, 130, 0.25);
+}
+
+.ck-content ul.todo-list > li.todo-list-b {
+ background-color: rgba(130, 200, 255, 0.25);
+}
+
.chat-options-container {
display: flex;
margin: 5px 0;
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index 939a284ea7..5d25ad8ee1 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -37,6 +37,7 @@ import AdmonitionTypeDropdown from "./plugins/admonition_type_dropdown.js";
import AdmonitionToolbar from "./plugins/admonition_toolbar.js";
import IncludeNoteBoxSizeDropdown from "./plugins/include_note_box_size_dropdown.js";
import IncludeNoteToolbar from "./plugins/include_note_toolbar.js";
+import TodoListClass from "./plugins/todo_list_class/todo_list_class.js";
/**
* Plugins that are specific to Trilium and not part of the CKEditor 5 core, included in both text editors but not in the attribute editor.
@@ -64,6 +65,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [
AdmonitionToolbar,
IncludeNoteBoxSizeDropdown,
IncludeNoteToolbar,
+ TodoListClass,
CopyAnchorLinkButton,
];
diff --git a/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class.ts b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class.ts
new file mode 100644
index 0000000000..173856964b
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class.ts
@@ -0,0 +1,12 @@
+import { Plugin } from "ckeditor5";
+import TodoListClassEditing from "./todo_list_class_editing.js";
+import TodoListClassToolbar from "./todo_list_class_toolbar.js";
+import TodoListClassUI from "./todo_list_class_ui.js";
+
+export default class TodoListClass extends Plugin {
+
+ static get requires() {
+ return [TodoListClassEditing, TodoListClassUI, TodoListClassToolbar] as const;
+ }
+
+}
diff --git a/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_editing.ts b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_editing.ts
new file mode 100644
index 0000000000..971db2c9f2
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_editing.ts
@@ -0,0 +1,96 @@
+import { Command, ListEditing, Plugin, TodoList, type ModelElement } from "ckeditor5";
+
+export const TODO_LIST_CLASSES = ["a", "b"] as const;
+export type TodoListClass = typeof TODO_LIST_CLASSES[number];
+export const TODO_LIST_CLASS_ATTRIBUTE = "todoListItemClass";
+
+function isTodoListClass(value: unknown): value is TodoListClass {
+ return typeof value === "string" && (TODO_LIST_CLASSES as readonly string[]).includes(value);
+}
+
+export default class TodoListClassEditing extends Plugin {
+
+ static get requires() {
+ return [TodoList, ListEditing] as const;
+ }
+
+ init() {
+ const editor = this.editor;
+
+ editor.model.schema.extend("$block", {allowAttributes: TODO_LIST_CLASS_ATTRIBUTE});
+
+ editor.commands.add("todoListItemClass", new TodoListClassCommand(editor));
+
+ const listEditing = editor.plugins.get(ListEditing);
+ listEditing.registerDowncastStrategy({
+ scope: "item",
+ attributeName: TODO_LIST_CLASS_ATTRIBUTE,
+ setAttributeOnDowncast(writer, value, element) {
+ for (const c of TODO_LIST_CLASSES) {
+ writer.removeClass(`todo-list-${c}`, element);
+ }
+ if (isTodoListClass(value)) {
+ writer.addClass(`todo-list-${value}`, element);
+ }
+ }
+ });
+
+ editor.conversion.for("upcast").attributeToAttribute({
+ view: {
+ key: "class",
+ value: new RegExp(`^todo-list-(${TODO_LIST_CLASSES.join("|")})$`)
+ },
+ model: {
+ key: TODO_LIST_CLASS_ATTRIBUTE,
+ value: (viewElement) => {
+ for (const c of TODO_LIST_CLASSES) {
+ if (viewElement.hasClass(`todo-list-${c}`)) {
+ return c;
+ }
+ }
+ return null;
+ }
+ }
+ });
+ }
+
+}
+
+class TodoListClassCommand extends Command {
+
+ declare public value: TodoListClass | null;
+
+ refresh() {
+ const block = this._getTodoBlock();
+ this.isEnabled = !!block;
+ this.value = block ? ((block.getAttribute(TODO_LIST_CLASS_ATTRIBUTE) as TodoListClass) ?? null) : null;
+ }
+
+ execute(options: {className: TodoListClass | null} = {className: null}) {
+ const model = this.editor.model;
+ const {className} = options;
+ model.change((writer) => {
+ for (const block of model.document.selection.getSelectedBlocks()) {
+ if (block.getAttribute("listType") !== "todo") {
+ continue;
+ }
+ const current = block.getAttribute(TODO_LIST_CLASS_ATTRIBUTE);
+ if (className === null || current === className) {
+ writer.removeAttribute(TODO_LIST_CLASS_ATTRIBUTE, block);
+ } else {
+ writer.setAttribute(TODO_LIST_CLASS_ATTRIBUTE, className, block);
+ }
+ }
+ });
+ }
+
+ private _getTodoBlock(): ModelElement | null {
+ const position = this.editor.model.document.selection.getFirstPosition();
+ const parent = position?.parent;
+ if (!parent || !parent.is("element")) {
+ return null;
+ }
+ return parent.getAttribute("listType") === "todo" ? (parent as ModelElement) : null;
+ }
+
+}
diff --git a/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_toolbar.ts b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_toolbar.ts
new file mode 100644
index 0000000000..3052b69fa8
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_toolbar.ts
@@ -0,0 +1,163 @@
+import {
+ BalloonPanelView,
+ ClickObserver,
+ ContextualBalloon,
+ Plugin,
+ ToolbarView,
+ clickOutsideHandler,
+ type ModelElement,
+ type ViewElement
+} from "ckeditor5";
+import { TODO_LIST_CLASSES } from "./todo_list_class_editing.js";
+import TodoListClassUI from "./todo_list_class_ui.js";
+
+export default class TodoListClassToolbar extends Plugin {
+
+ static get requires() {
+ return [ContextualBalloon, TodoListClassUI] as const;
+ }
+
+ private _balloon!: ContextualBalloon;
+ private _toolbarView!: ToolbarView;
+ private _targetItemId: string | null = null;
+
+ init() {
+ const editor = this.editor;
+ this._balloon = editor.plugins.get(ContextualBalloon);
+ editor.editing.view.addObserver(ClickObserver);
+ this._toolbarView = this._createToolbarView();
+
+ this.listenTo(editor.editing.view.document, "click", (_evt, data) => {
+ const target = (data as {target?: unknown}).target;
+ if (!isTodoCheckbox(target)) {
+ this._hide();
+ return;
+ }
+ this._show(target);
+ });
+
+ clickOutsideHandler({
+ emitter: this._toolbarView,
+ contextElements: () => (this._balloon.view.element ? [this._balloon.view.element] : []),
+ callback: () => this._hide(),
+ activator: () => this._isVisible()
+ });
+
+ this.listenTo(editor.model.document.selection, "change:range", () => {
+ if (!this._isVisible()) {
+ return;
+ }
+ const block = editor.model.document.selection.getFirstPosition()?.parent;
+ const currentId = block && (block as ModelElement).is?.("element") ? (block as ModelElement).getAttribute("listItemId") : null;
+ if (currentId !== this._targetItemId) {
+ this._hide();
+ }
+ });
+ }
+
+ override destroy() {
+ this._toolbarView?.destroy();
+ super.destroy();
+ }
+
+ private _createToolbarView(): ToolbarView {
+ const editor = this.editor;
+ const toolbar = new ToolbarView(editor.locale);
+ toolbar.class = "todo-list-class-toolbar";
+ toolbar.items.add(editor.ui.componentFactory.create("todoListClass:none"));
+ for (const className of TODO_LIST_CLASSES) {
+ toolbar.items.add(editor.ui.componentFactory.create(`todoListClass:${className}`));
+ }
+ return toolbar;
+ }
+
+ private _show(checkbox: ViewElement) {
+ const editor = this.editor;
+ const wrapper = checkbox.parent;
+ if (!wrapper || !wrapper.is("element")) {
+ return;
+ }
+ const anchorDom = editor.editing.view.domConverter.viewToDom(wrapper) as HTMLElement | null;
+ if (!anchorDom) {
+ return;
+ }
+
+ const block = this._findTodoBlock(checkbox);
+ if (!block) {
+ return;
+ }
+ this._targetItemId = (block.getAttribute("listItemId") as string | undefined) ?? null;
+ editor.model.change((writer) => {
+ writer.setSelection(writer.createPositionAt(block, 0));
+ });
+
+ const position = {
+ target: anchorDom,
+ positions: [
+ BalloonPanelView.defaultPositions.northArrowSouth,
+ BalloonPanelView.defaultPositions.southArrowNorth
+ ]
+ };
+
+ if (this._isVisible()) {
+ this._balloon.updatePosition(position);
+ } else {
+ this._balloon.add({
+ view: this._toolbarView,
+ position,
+ balloonClassName: "ck-toolbar-container todo-list-class-toolbar"
+ });
+ }
+ }
+
+ private _findTodoBlock(checkbox: ViewElement): ModelElement | null {
+ const editor = this.editor;
+ const li = checkbox.findAncestor("li");
+ if (!li) {
+ return null;
+ }
+ const domLi = editor.editing.view.domConverter.viewToDom(li) as HTMLElement | null;
+ const itemId = domLi?.getAttribute("data-list-item-id");
+ if (!itemId) {
+ return null;
+ }
+ const root = editor.model.document.getRoot();
+ if (!root) {
+ return null;
+ }
+ for (const item of editor.model.createRangeIn(root).getItems()) {
+ if (item.is("element")
+ && item.getAttribute("listItemId") === itemId
+ && item.getAttribute("listType") === "todo") {
+ return item as ModelElement;
+ }
+ }
+ return null;
+ }
+
+ private _hide() {
+ if (this._isVisible()) {
+ this._balloon.remove(this._toolbarView);
+ }
+ this._targetItemId = null;
+ }
+
+ private _isVisible(): boolean {
+ return this._balloon.hasView(this._toolbarView);
+ }
+
+}
+
+function isTodoCheckbox(el: unknown): el is ViewElement {
+ if (!el || typeof (el as ViewElement).is !== "function") {
+ return false;
+ }
+ const v = el as ViewElement;
+ if (!v.is("element", "input")) {
+ return false;
+ }
+ if (v.getAttribute("type") !== "checkbox") {
+ return false;
+ }
+ return !!v.findAncestor({classes: "todo-list__label"});
+}
diff --git a/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_ui.ts b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_ui.ts
new file mode 100644
index 0000000000..7ab3359211
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/todo_list_class/todo_list_class_ui.ts
@@ -0,0 +1,52 @@
+import { ButtonView, Plugin } from "ckeditor5";
+import TodoListClassEditing, { TODO_LIST_CLASSES, type TodoListClass } from "./todo_list_class_editing.js";
+
+export default class TodoListClassUI extends Plugin {
+
+ static get requires() {
+ return [TodoListClassEditing] as const;
+ }
+
+ init() {
+ const editor = this.editor;
+ const command = editor.commands.get("todoListItemClass")!;
+
+ editor.ui.componentFactory.add("todoListClass:none", (locale) => {
+ const button = new ButtonView(locale);
+ button.set({
+ label: "—",
+ withText: true,
+ tooltip: editor.t("Clear class"),
+ class: "ck-todo-list-class-button ck-todo-list-class-none"
+ });
+ button.bind("isOn").to(command, "value", (value) => value === null);
+ button.bind("isEnabled").to(command, "isEnabled");
+ button.on("execute", () => {
+ editor.execute("todoListItemClass", {className: null});
+ editor.editing.view.focus();
+ });
+ return button;
+ });
+
+ for (const className of TODO_LIST_CLASSES) {
+ const componentName = `todoListClass:${className}`;
+ editor.ui.componentFactory.add(componentName, (locale) => {
+ const button = new ButtonView(locale);
+ button.set({
+ label: className.toUpperCase(),
+ withText: true,
+ tooltip: editor.t("Apply class %0", [`todo-list-${className}`]),
+ class: `ck-todo-list-class-button ck-todo-list-class-${className}`
+ });
+ button.bind("isOn").to(command, "value", (value) => value === className);
+ button.bind("isEnabled").to(command, "isEnabled");
+ button.on("execute", () => {
+ editor.execute("todoListItemClass", {className: className as TodoListClass});
+ editor.editing.view.focus();
+ });
+ return button;
+ });
+ }
+ }
+
+}
diff --git a/packages/share-theme/src/styles/base.css b/packages/share-theme/src/styles/base.css
index 744ee87faa..cf0d55f74b 100644
--- a/packages/share-theme/src/styles/base.css
+++ b/packages/share-theme/src/styles/base.css
@@ -147,3 +147,11 @@ iframe.pdf-view {
background-color: var(--background-primary);
border-color: var(--text-primary) !important;
}
+
+.ck-content ul.todo-list > li.todo-list-a {
+ background-color: rgba(255, 235, 130, 0.25);
+}
+
+.ck-content ul.todo-list > li.todo-list-b {
+ background-color: rgba(130, 200, 255, 0.25);
+}