Files
RuneStone/src/lib/features/pm/index.ts
T
Knotty Wyvern 956ead818b Translate Project Management and add its tests and guide
- Every interface string of Project Management goes through the catalog: 539 new rows with fr, ja, and zh-CN.
- Storage and model messages use a local message() marker and stay free of host imports. The interface translates them.
- A board renders again when the language changes. Dates use the interface language.
- Fix: the ribbon button waits for the feature switches at startup, and the settings code loads no feature module.
- Add six smoke tests: board state, cards, menus, drag and drop, views, and language.
- Add docs/guides/project-management.md and UI-060. Update the user, plugin, SDK, host, development, and interface docs and CLAUDE.md.
- Name the fake board view of tree/renameUndo "Board fixture", because the native Project Management button is now in the ribbon.
2026-10-29 21:53:09 -07:00

134 lines
6.3 KiB
TypeScript

// The Project Management feature. `loader.ts` imports this module at the first use: the ribbon button, a board tab,
// or a tab transfer from another window. The views are `planning` (All tasks) and `board-<hex of the board ID>`.
// The storage code (`storage/`) and the domain code (`model/`) use no host module, so the skill helper can bundle them.
import { invoke } from "@tauri-apps/api/core";
import { backgroundHub } from "../../plugins/backgrounds.ts";
import { onProjectManagementChange, readProjectManagement, readViewStates, writeProjectManagement, writeViewStates } from "../pmSettings.ts";
import type { FeatureContext, FeatureInstance } from "../types.ts";
import { boardKey, isManagedBoard, readCatalog } from "./storage/catalog.ts";
import { findLegacy, hasLegacy, type LegacyContent } from "./storage/legacy.ts";
import { isHiddenCard } from "./storage/persistence.ts";
import { message } from "./storage/message.ts";
import type { BoardReference } from "./storage/board.ts";
import { TaskStore } from "./model/store.ts";
import { hostMenus } from "./ui/host.ts";
import { mountPM, parseViewState, type PMViewState } from "./ui/ui.ts";
import { hostWorkspace } from "./workspace.ts";
import { boardOfView, boardViewId, PLANNING } from "./views.ts";
export function start(context: FeatureContext): FeatureInstance {
const workspace = hostWorkspace(context.onWorkspaceChange);
const preferences = readProjectManagement();
const stopPreferences = onProjectManagementChange(() => Object.assign(preferences, readProjectManagement()));
const states = new Map<string, PMViewState>(Object.entries(readViewStates()).map(([key, value]) => [key, parseViewState(value)]));
const dirty = new Set<string>();
let saveTimer: ReturnType<typeof setTimeout> | undefined;
let disposed = false;
const mounted = new Set<() => void>();
/** The legacy data of each workspace root. A migration reads it again. */
const legacy = new Map<string, Promise<LegacyContent>>();
const flush = () => {
clearTimeout(saveTimer);
if (!dirty.size) return;
writeViewStates(Object.fromEntries([...dirty].map(key => [key, structuredClone(states.get(key))])));
dirty.clear();
};
const persist = (id: string) => {
if (disposed) return;
dirty.add(id);
clearTimeout(saveTimer);
saveTimer = setTimeout(flush, 200);
};
const root = () => invoke<string | null>("workspace_last_path").catch(() => null);
const legacyContent = async (fresh = false) => {
const key = await root() ?? "";
if (fresh) legacy.delete(key);
let pending = legacy.get(key);
if (!pending) {
pending = findLegacy(workspace, () => { if (disposed) throw new Error(message("Project Management is off.")); });
pending.catch(() => legacy.delete(key));
legacy.set(key, pending);
}
return pending;
};
/** Copy the legacy boards, the root label file, and every legacy task note to `.runestone/pm/`, with backups. */
const migrate = async () => {
const store = new TaskStore(workspace, undefined, { legacy: true });
try {
await store.reload();
const cards = store.tasks.filter(task => !isHiddenCard(task.path)).map(task => task.id);
return await store.migrateStorage(cards);
} finally {
store.dispose();
void legacyContent(true).catch(() => undefined);
}
};
const openBoard = async (path: string, from?: string) => {
const snapshot = await readCatalog(workspace);
const board = isManagedBoard(path) ? snapshot.value.boards.find(item => boardKey(item.value.id) === path) : undefined;
if (!board) throw new Error(message("This board is unavailable. Refresh Project Management."));
const id = boardViewId(board.value.id);
context.setViewTitle(id, board.name);
if (from) context.replaceView(from, id); else context.openView(id);
};
function mountView(view: string, element: HTMLElement) {
const boardId = view === PLANNING ? null : boardOfView(view);
if (view !== PLANNING && !boardId) throw new Error(message("This view is unavailable."));
const reference: BoardReference | undefined = boardId ? { id: boardId, path: boardKey(boardId) } : undefined;
if (!states.has(view)) states.set(view, {});
const store = new TaskStore(workspace, reference, { legacy: false });
if (boardId) {
void readCatalog(workspace).then(snapshot => {
const board = snapshot.value.boards.find(item => item.value.id === boardId);
if (board && !disposed) context.setViewTitle(view, board.name);
}, () => undefined);
}
const cleanup = mountPM(element, store, {
ui: hostMenus,
backgrounds: backgroundHub,
state: states.get(view),
stateChanged: () => persist(view),
preferences,
savePreferences: value => writeProjectManagement(value),
openBoard: (path, replace) => openBoard(path, replace ? view : undefined),
openAll: replace => { if (replace) context.replaceView(view, PLANNING); else context.openView(PLANNING); },
boardRenamed: name => { if (boardId) context.setViewTitle(view, name); },
boardDeleted: () => { if (boardId) context.closeView(view); },
watch: workspace.watch,
legacy: { find: legacyContent, has: hasLegacy, migrate },
});
mounted.add(cleanup);
return () => { mounted.delete(cleanup); cleanup(); };
}
return {
mount: mountView,
async launch(view) {
if (view !== PLANNING) { context.openView(view); return; }
// The default board of the workspace is in the catalog. A missing, archived, or damaged default opens All tasks.
try {
const { value } = await readCatalog(workspace);
const board = value.boards.find(item => item.value.id === value.defaultBoardId && !item.archived);
if (board) { await openBoard(boardKey(board.value.id)); return; }
} catch { /* All tasks shows the storage problem. */ }
context.openView(PLANNING);
},
saveTabState: view => structuredClone(states.get(view) ?? {}),
restoreTabState(view, value) {
if (view !== PLANNING && !boardOfView(view)) throw new Error(message("This view is unavailable."));
states.set(view, parseViewState(value));
persist(view);
},
dispose() {
disposed = true;
flush();
stopPreferences();
for (const cleanup of [...mounted]) cleanup();
mounted.clear();
},
};
}