* Fix extended task rendering in widgets
This patch addresses several issues.
1. Extended tasks like `[PLANNED]` or `[TODO]` were not rendering
correctly when generated via widgets. The HTML renderer was producing
plain text without brackets or proper styling, while simple `[x]`
tasks worked fine. The fix produces the same DOM structure as
CodeMirror producing coherent styling everywhere.
2. Widget checkboxes are not interactive now. Since widget tasks have no
backing state handler clicking them only caused a misleading visual
toggle with no effect. The `disabled` attribute prevents this
confusion and the dimmed appearanceit signals to the user that the
checkbox is read-only in this context which is the truth.
To see it in action try the following Markdown snippet in SilverBullet:
```
- [x] #foo [att: i]
- [PLANNED] bar #bar [att: i]
${'- [x] #foo [att: i]\n- [PLANNED] bar #bar [att: i]'}
```
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
* Revert and just prevent widget checkboxes from toggling visually
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
* Support custom task states in widgets and indexer
Custom task states like `[PLANNED]` or `[IN PROGRESS]` now render
correctly in *widgets*, matching the same DOM structure as CodeMirror
uses in the editor. Widget-rendered tasks that reference a real task
(via `page@pos`) are fully interactive — clicking an extended state
cycles it through the configured states and updates the source task.
Tasks without a reference are disabled to prevent misleading visual
feedback.
The task indexer now respects the `done` flag on custom states defined
via `taskState.define`, so queries like `where not t.done` correctly
exclude tasks in states marked as done. Previously only the built-in
simple tasks (`- [x]` or `- [X]`) states were treated as completed. The
`Task: Remove Completed` command already handled custom done states and
continues to work unchanged.
State cycling now preserves the definition order from `taskState.define`
calls instead of sorting alphabetically. A new `order` field allows
explicit control over the cycle sequence. Without it, states cycle in
definition order. With it, states are sorted by the `order` field value.
This gives users full control over the progression, for example:
PLANNED --> IN PROGRESS --> FINISHED
The `taskState.define` API now validates its input against a schema:
`name` (string, required), `done` (boolean, optional), and `order`
(number, optional).
The `expandMarkdown` no longer injects "fake" `page@pos` references into
tasks rendered via `widget.markdownBlock` or `widget.new` which was
causing broken links and unwanted interactivity on static task content.
The `task:stateChange` event is dispatched when toggling tasks from
widgets, ensuring Lua listeners (such as some custom completed timestamp
management) fire correctly regardless of where the toggle originates.
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
---------
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
153 lines
4.8 KiB
TypeScript
153 lines
4.8 KiB
TypeScript
import { parseToRef } from "@silverbulletmd/silverbullet/lib/ref";
|
|
import type { Client } from "../client.ts";
|
|
import type { EventPayLoad } from "./lua_widget.ts";
|
|
|
|
export function moveCursorToWidgetStart(
|
|
client: Client,
|
|
widgetDom: HTMLElement,
|
|
widgetText?: string,
|
|
) {
|
|
const view = client.editorView;
|
|
const pos = view.posAtDOM(widgetDom, 0);
|
|
|
|
let anchor = pos;
|
|
if (widgetText) {
|
|
// The widget decoration may be placed at the end of the source range
|
|
// (node.to). Search near posAtDOM for the actual text to find its start.
|
|
const searchFrom = Math.max(0, pos - widgetText.length);
|
|
const region = view.state.sliceDoc(searchFrom, pos + widgetText.length);
|
|
const idx = region.lastIndexOf(widgetText);
|
|
if (idx !== -1) {
|
|
anchor = searchFrom + idx;
|
|
}
|
|
}
|
|
|
|
view.dispatch({ selection: { anchor } });
|
|
client.focus();
|
|
}
|
|
|
|
export function attachWidgetEventHandlers(
|
|
div: HTMLElement,
|
|
client: Client,
|
|
widgetText?: string,
|
|
events?: Record<string, (event: EventPayLoad) => void>,
|
|
) {
|
|
if (!div.dataset.handlersAttached) {
|
|
div.dataset.handlersAttached = "true";
|
|
div.addEventListener("mousedown", (e) => {
|
|
if (e.altKey && widgetText) {
|
|
// Move cursor there
|
|
moveCursorToWidgetStart(client, div, widgetText);
|
|
e.preventDefault();
|
|
}
|
|
// CodeMirror overrides mousedown on parent elements to implement its own selection highlighting.
|
|
// That's nice, but not for markdown widgets, so let's not propagate the event to CodeMirror here.
|
|
e.stopPropagation();
|
|
});
|
|
|
|
div.addEventListener("mouseup", (e) => {
|
|
// Same as above
|
|
e.stopPropagation();
|
|
});
|
|
}
|
|
|
|
// Override wiki links with local navigate (faster)
|
|
div.querySelectorAll("a[data-ref]").forEach((el_) => {
|
|
const el = el_ as HTMLElement;
|
|
// Override default click behavior with a local navigate (faster)
|
|
el.addEventListener("click", (e) => {
|
|
if (e.ctrlKey || e.metaKey) {
|
|
// Don't do anything special for ctrl/meta clicks
|
|
return;
|
|
}
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
void client.navigate(
|
|
parseToRef(el.dataset.ref!),
|
|
false,
|
|
e.ctrlKey || e.metaKey,
|
|
);
|
|
});
|
|
});
|
|
|
|
div.querySelectorAll("button[data-onclick]").forEach((el_) => {
|
|
const el = el_ as HTMLElement;
|
|
const onclick = el.dataset.onclick!;
|
|
const parsedOnclick = JSON.parse(onclick);
|
|
if (parsedOnclick[0] === "command") {
|
|
const command = parsedOnclick[1];
|
|
el.addEventListener("click", (e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
console.info("Command link clicked in widget, running", parsedOnclick);
|
|
client.runCommandByName(command, parsedOnclick[2]).catch(console.error);
|
|
});
|
|
}
|
|
});
|
|
|
|
// Implement task toggling
|
|
div.querySelectorAll("span[data-external-task-ref]").forEach((el: any) => {
|
|
const taskRef = el.dataset.externalTaskRef;
|
|
const input = el.querySelector("input[type=checkbox]");
|
|
if (input) {
|
|
input.addEventListener("click", (e: any) => {
|
|
// Avoid triggering the click on the parent
|
|
e.stopPropagation();
|
|
});
|
|
input.addEventListener("change", (e: any) => {
|
|
e.stopPropagation();
|
|
const oldState = e.target.dataset.state;
|
|
const newState = oldState === " " ? "x" : " ";
|
|
// Update state in DOM as well for future toggles
|
|
e.target.dataset.state = newState;
|
|
console.log("Toggling task", taskRef);
|
|
client.clientSystem
|
|
.localSyscall("system.invokeFunction", [
|
|
"index.updateTaskState",
|
|
taskRef,
|
|
oldState,
|
|
newState,
|
|
])
|
|
.catch(console.error);
|
|
});
|
|
}
|
|
|
|
// Extended task states (e.g. [PLANNED], [TODO])
|
|
const taskStateSpan = el.querySelector("span.sb-task-state[data-state]");
|
|
if (taskStateSpan) {
|
|
taskStateSpan.style.cursor = "pointer";
|
|
taskStateSpan.addEventListener("click", (e: any) => {
|
|
e.stopPropagation();
|
|
const oldState = taskStateSpan.dataset.state;
|
|
console.log("Cycling extended task", taskRef, oldState);
|
|
client.clientSystem
|
|
.localSyscall("system.invokeFunction", [
|
|
"index.cycleTaskStateByRef",
|
|
taskRef,
|
|
oldState,
|
|
])
|
|
.then((newState: string) => {
|
|
taskStateSpan.dataset.state = newState;
|
|
taskStateSpan.textContent = newState;
|
|
})
|
|
.catch(console.error);
|
|
});
|
|
}
|
|
});
|
|
|
|
// Disable non-referenced task checkboxes
|
|
div.querySelectorAll("input[type=checkbox]").forEach((cb) => {
|
|
if (!cb.closest("span[data-external-task-ref]")) {
|
|
cb.setAttribute("disabled", "disabled");
|
|
}
|
|
});
|
|
|
|
if (events) {
|
|
for (const [eventName, event] of Object.entries(events)) {
|
|
div.addEventListener(eventName, (e) => {
|
|
event({ name: eventName, data: e });
|
|
});
|
|
}
|
|
}
|
|
}
|