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
+152
View File
@@ -0,0 +1,152 @@
import type { Workspace, Task } from "../model/model.ts";
import { migrateCards, readCards, cardOrigin } from "./cards.ts";
import type { BoardDocument } from "./board.ts";
import {
backupFile,
readCatalog,
writeCatalog,
isManagedBoard,
type OriginalFile,
} from "./catalog.ts";
import { labelsPath, legacyLabelsPath, parseLabels } from "./labels.ts";
import {
readOptional,
saveDocument,
ensurePrivateStorage,
} from "./persistence.ts";
/** Retain originals until the user checks the boards and removes the listed files. */
export async function migrateStorage(
workspace: Workspace,
boards: BoardDocument[],
cards: Task[] = [],
) {
if (!workspace.readOptional || !workspace.create)
throw new Error("Update RuneStone before migrating Project Management storage.");
const snapshot = await readCatalog(workspace);
const legacy = boards.filter((board) => !isManagedBoard(board.path));
const legacyLabels = await readOptional(workspace, legacyLabelsPath);
const savedLabels = await readOptional(workspace, labelsPath);
if (legacyLabels !== null) parseLabels(legacyLabels);
if (savedLabels !== null) parseLabels(savedLabels);
// Validate every source before creating backups or publishing any changes.
for (const board of legacy) {
if (snapshot.value.deletedBoardIds?.includes(board.id))
throw new Error("This board was deleted. Refresh Project Management before migration.");
if ((await workspace.read(board.path)) !== board.source)
throw new Error(
`The board changed: ${board.path}. Refresh before migration.`,
);
const existing = snapshot.value.boards.find(
(item) => item.value.id === board.id,
);
if (
existing &&
(existing.original?.path !== board.path ||
existing.original.source !== board.source)
)
throw new Error(
`A different board already uses the ID from ${board.path}.`,
);
}
if (legacyLabels !== null && savedLabels !== null) {
const original = JSON.parse(savedLabels).original;
if (
original?.path !== legacyLabelsPath ||
original?.source !== legacyLabels
)
throw new Error(
"Hidden labels and legacy labels both exist. Resolve their differences before migration.",
);
}
await ensurePrivateStorage(workspace);
let changed = false;
for (const board of legacy) {
if (snapshot.value.boards.some((item) => item.value.id === board.id))
continue;
const original = await backupFile(workspace, board.path, board.source);
snapshot.value.boards.push({
name: board.name,
value: board.value,
original,
});
changed = true;
}
const labelBackup =
legacyLabels !== null && savedLabels === null
? await backupFile(workspace, legacyLabelsPath, legacyLabels)
: undefined;
for (const board of legacy)
if ((await workspace.read(board.path)) !== board.source)
throw new Error(
`The board changed: ${board.path}. Refresh before migration.`,
);
if (legacyLabels !== (await readOptional(workspace, legacyLabelsPath)))
throw new Error("Legacy labels changed. Refresh before migration.");
if (changed) await writeCatalog(workspace, snapshot.value, snapshot.source);
if (labelBackup) {
const source =
JSON.stringify(
{ ...JSON.parse(legacyLabels!), original: labelBackup },
null,
2,
) + "\n";
await saveDocument(workspace, labelsPath, source, null);
if ((await workspace.read(labelsPath)) !== source)
throw new Error("The saved label check failed. Retry migration.");
}
const saved = await readCatalog(workspace);
for (const board of legacy) {
const entry = saved.value.boards.find((item) => item.value.id === board.id);
const expected = snapshot.value.boards.find(
(item) => item.value.id === board.id,
);
if (
entry?.original?.source !== board.source ||
JSON.stringify(entry) !== JSON.stringify(expected)
)
throw new Error("The saved board check failed. Retry migration.");
}
await migrateCards(workspace, cards);
const files = await migrationFiles(workspace);
for (const file of files) {
if ((await workspace.read(file.backup)) !== file.source)
throw new Error(
`Backup check failed for ${file.path}. Keep the original.`,
);
const current = await readOptional(workspace, file.path);
if (current !== null && current !== file.source)
throw new Error(
`The original changed: ${file.path}. Keep both versions.`,
);
}
return files;
}
export async function migrationFiles(
workspace: Workspace,
): Promise<OriginalFile[]> {
const snapshot = await readCatalog(workspace);
const files = snapshot.value.boards.flatMap((board) =>
board.original ? [board.original] : [],
);
const labels = await readOptional(workspace, labelsPath);
if (labels !== null) {
parseLabels(labels);
const original = JSON.parse(labels).original;
if (
original &&
typeof original.path === "string" &&
typeof original.source === "string" &&
typeof original.backup === "string"
)
files.push(original);
}
const cards = await readCards(workspace);
if (cards.issues.length) throw new Error(cards.issues.join(" "));
for (const task of cards.tasks) {
const original = cardOrigin(task.source);
if (original) files.push(original);
}
return files;
}