Make Project Management a native feature

- Move the storage and model of the plugin into src/lib/features/pm/ as pure TypeScript, so the skill can still bundle them.
- Move the DOM interface onto the host menus, the shared tooltip, and the host icons.
- A ribbon button opens Project Management. Board tabs keep the owner runestone.pm and the board-<hex> view IDs through view patterns.
- Read only .runestone/pm/. Offer the plugin migration, with backups, for legacy notes.
- A save, move, or delete reads only that card again. Watcher paths reach the feature.
- Add Settings → Project Management and the projectManagement section. View states move to localStorage.
- Background reads the boards from workspace.json. Remove api.backgrounds and the boards service.
- Add yaml 2.9.1. Use the host markdown-it.
- Move 18 plugin test files to tests/unit/features/pm/ and add native tests.
This commit is contained in:
Knotty Wyvern
2026-10-29 20:54:29 -07:00
parent e6deecc092
commit e66f089176
100 changed files with 14412 additions and 290 deletions
+163
View File
@@ -0,0 +1,163 @@
import { readTask, newTask, split } from "./notes.ts";
import type { Task, TaskFields, Workspace } from "../model/model.ts";
import { backupFile, type OriginalFile } from "./catalog.ts";
import { legacyPosition } from "./order.ts";
import { mapConcurrent, readSources } from "./scan.ts";
import {
cardsFolder,
cardsMarker,
isHiddenCard,
readOptional,
saveDocument,
} from "./persistence.ts";
export function cardPath(id: string) {
const key = Array.from(new TextEncoder().encode(id), (byte) =>
byte.toString(16).padStart(2, "0"),
).join("");
return `${cardsFolder}/${key}.md`;
}
export function cardOrigin(source: string): OriginalFile | undefined {
const origin = split(source).doc.toJS({
maxAliasCount: 50,
}).runestone_pm_origin;
if (origin === undefined) return;
if (
!origin ||
typeof origin.path !== "string" ||
typeof origin.source !== "string" ||
typeof origin.backup !== "string" ||
origin.path.startsWith(".") ||
origin.path.includes("\\") ||
origin.path
.split("/")
.some((part: string) => !part || part === "." || part === "..") ||
origin.backup !== `.runestone/pm/backups/${origin.path}`
)
throw new Error(
"The card migration record is invalid. Keep the original note.",
);
return origin;
}
/** Read one hidden card file. It throws when the file is invalid or when its name does not match the card ID. */
export function parseCard(path: string, source: string): Task {
cardOrigin(source);
const task = readTask(path, source);
if (!task || cardPath(task.id) !== path)
throw new Error("The hidden card ID does not match its storage path.");
return task;
}
export async function prepareCards(workspace: Workspace) {
const marker = await readOptional(workspace, cardsMarker);
if (marker !== null) return;
try {
await saveDocument(workspace, cardsMarker, "*\n", null);
} catch (error) {
if ((await readOptional(workspace, cardsMarker)) !== "*\n") throw error;
}
}
export async function readCards(
workspace: Workspace,
previous: ReadonlyMap<string, Task> = new Map(),
): Promise<{ tasks: Task[]; issues: string[] }> {
const tasks: Task[] = [],
issues: string[] = [];
// Direct reads are necessary because the host hides .runestone from root listings.
if (
!workspace.readOptional ||
(await workspace.readOptional(cardsMarker)) === null
)
return { tasks, issues };
const entries = (await workspace.list(cardsFolder)).filter(
(entry) => entry.kind !== "folder" && isHiddenCard(entry.rel_path),
);
const sources = await readSources(workspace, entries.map(entry => entry.rel_path));
const documents = await mapConcurrent(entries, async (entry) => {
try {
const result = sources.get(entry.rel_path)!;
if (result.error || result.source === undefined) throw new Error(result.error ?? "The card is unavailable.");
const source = result.source;
const cached = previous.get(entry.rel_path);
if (cached?.source === source) return { task: cached };
return { task: parseCard(entry.rel_path, source) };
} catch (error) {
return {
issue: `${entry.rel_path}: ${error instanceof Error ? error.message : String(error)}`,
};
}
});
for (const document of documents) {
if (document.task) tasks.push(document.task);
if (document.issue) issues.push(document.issue);
}
return { tasks, issues };
}
export async function createCard(
workspace: Workspace,
title: string,
fields: TaskFields,
body: string,
id: string = crypto.randomUUID(),
) {
// The storage name is independent of the title, so punctuation and duplicate titles are safe.
const note = newTask(id, { ...fields, title: title.trim() }, body, id);
const path = cardPath(id);
await prepareCards(workspace);
await saveDocument(workspace, path, note.source, null);
return { path, source: note.source };
}
export async function migrateCards(workspace: Workspace, tasks: Task[]) {
for (const task of tasks) {
if (isHiddenCard(task.path)) continue;
if ((await workspace.read(task.path)) !== task.source)
throw new Error(
`The card changed: ${task.path}. Refresh before migration.`,
);
const destination = await readOptional(workspace, cardPath(task.id));
if (destination !== null) {
const origin = cardOrigin(destination);
if (origin?.path !== task.path || origin.source !== task.source)
throw new Error(
`A different hidden card already uses the ID from ${task.path}.`,
);
}
if (split(task.source).doc.has("runestone_pm_origin"))
throw new Error(
`The note already contains a migration record: ${task.path}. Review it before migration.`,
);
}
for (const task of tasks) {
if (isHiddenCard(task.path)) continue;
const original = await backupFile(workspace, task.path, task.source);
const path = cardPath(task.id);
if ((await readOptional(workspace, path)) !== null) continue;
const { doc, body, newline, match } = split(task.source);
doc.set("runestone_pm_origin", original);
if (task.order === undefined)
doc.setIn(["runestone_pm", "order"], legacyPosition(task.path));
for (const [id, placement] of Object.entries(task.placements ?? {}))
if (placement.order === undefined)
doc.setIn(
["runestone_pm", "placements", id, "order"],
legacyPosition(task.path),
);
const source = `${match?.[1] ?? ""}---${newline}${doc.toString({ lineWidth: 0 }).replaceAll("\n", newline)}---${newline}${body}`;
const copied = readTask(path, source)!;
if (
copied.id !== task.id ||
copied.title !== task.title ||
copied.body !== task.body
)
throw new Error(
`The card copy differs from ${task.path}. Keep the original.`,
);
if ((await workspace.read(task.path)) !== task.source)
throw new Error(`The card changed: ${task.path}. Retry migration.`);
await prepareCards(workspace);
await saveDocument(workspace, path, source, null);
if ((await workspace.read(path)) !== source)
throw new Error(
`The saved card check failed: ${task.path}. Keep the original.`,
);
}
}