Files
plainleaf/plugs/index/complete.ts
T
d2e24ebae0 Add a dropdown picker for custom task states (#1900)
* Add a dropdown picker for custom task states

Add a dropdown picker for custom task states that appears as a small
triangle next to the `[state]` allowing to quickly switch between
defined states with autocomplete.

States marked as done receive strikethrough styling on the task text.

Each state can be styled individually using the `data-task-state`
attribute, for example:

```space-style
[data-task-state=PLANNED] .sb-task-state {
  color: black;
  background-color: orange;
}
```

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>

* Keep the current task state visible while the dropdown picker is open

Keep the current task state visible while the dropdown picker is open by
disabling built-in filtering and handling it manually — when the state
text exactly matches a known state all options are shown, otherwise
results are filtered by what the user has typed so far.

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>

* Added on-hover completion styling

* Fix: Show all defined task states in the dropdown when clicking the picker on an unrecognized state instead of showing an empty menu

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>

---------

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
Co-authored-by: Zef Hemel <zef@zef.me>
2026-03-20 15:09:12 +01:00

83 lines
2.1 KiB
TypeScript

import { config } from "@silverbulletmd/silverbullet/syscalls";
import type { CompleteEvent } from "@silverbulletmd/silverbullet/type/client";
import type { Completion } from "@codemirror/autocomplete";
export async function attributeCompletion(
tags: string[],
): Promise<Completion[]> {
const completions: Completion[] = [];
for (const tag of tags) {
const schema = await config.get<any>(["tags", tag, "schema"], null);
if (!schema) {
continue;
}
for (const [attr, val] of Object.entries(schema.properties)) {
const def = val as any;
if (def.readOnly) {
continue;
}
completions.push({
label: attr,
type: `attribute`,
detail: `for #${tag} (type: ${humanReadableSchemaType(def)})${
def.description ? `: ${def.description}` : ""
}`,
apply: `${attr}: `,
});
}
}
return completions;
}
function humanReadableSchemaType(type: any): string {
if (!type) {
return "any";
}
if (type.type) {
return type.type;
}
if (type.anyOf) {
return type.anyOf.map(humanReadableSchemaType).join(" | ");
}
return "any";
}
/**
* Task state completion
*/
export async function completeTaskState(completeEvent: CompleteEvent) {
const taskMatch = /([-*]\s+\[)([^[\]]*)$/.exec(completeEvent.linePrefix);
if (!taskMatch) {
return null;
}
const allStates = Object.keys(await config.get("taskStates", {}));
const typed = taskMatch[2];
let options: string[];
if (!typed) {
// Nothing typed — show all
options = allStates;
} else if (allStates.includes(typed)) {
// Exact match (dropdown click on existing state) — show all
options = allStates;
} else {
// Partial typing — filter by prefix, fall back to all if nothing matches
const filtered = allStates.filter((s) =>
s.toLowerCase().startsWith(typed.toLowerCase()),
);
options = filtered.length > 0 ? filtered : allStates;
}
return {
from: completeEvent.pos - typed.length,
filter: false,
options: options.map((state) => ({
label: state,
})),
};
}