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:
@@ -0,0 +1,132 @@
|
||||
// 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 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("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("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("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("This view is unavailable.");
|
||||
states.set(view, parseViewState(value));
|
||||
persist(view);
|
||||
},
|
||||
dispose() {
|
||||
disposed = true;
|
||||
flush();
|
||||
stopPreferences();
|
||||
for (const cleanup of [...mounted]) cleanup();
|
||||
mounted.clear();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user