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
+249
View File
@@ -0,0 +1,249 @@
export const statuses = ["todo", "doing", "done"] as const;
export type Status = (typeof statuses)[number];
export const priorities = ["highest", "high", "medium", "low", "lowest"] as const;
export type PriorityLevels = 2 | 3 | 5;
export function availablePriorities(levels: PriorityLevels): readonly Priority[] {
return levels === 2 ? ["high", "low"] : levels === 3 ? ["high", "medium", "low"] : priorities;
}
export type Priority = (typeof priorities)[number];
export const priorityNames: Record<Priority, string> = { highest: "Highest", high: "High", medium: "Medium", low: "Low", lowest: "Lowest" };
export const statusNames: Record<Status, string> = {
todo: "To do",
doing: "In progress",
done: "Done",
};
export const cardColors = {
red: "#df7373",
orange: "#d99554",
amber: "#dda83e",
yellow: "#c5ae4f",
lime: "#a4bd58",
green: "#63ad84",
teal: "#4cafa6",
cyan: "#53b8cf",
blue: "#729fe0",
indigo: "#8790df",
purple: "#ac89d8",
magenta: "#cb75c8",
pink: "#d784b0",
brown: "#b48b68",
slate: "#8398ae",
gray: "#a4a6ad",
} as const;
export interface Plan {
start: string;
end: string;
}
export interface Session {
id: string;
start: string;
end: string;
timeZone: string;
}
export interface TaskFields {
createdAt?: string;
title?: string;
order?: string;
color?: string;
assignees?: string[];
customFields?: { name: string; value: string }[];
listId?: string | null;
placements?: Record<string, { listId: string | null; order?: string }>;
status: Status;
tags: string[];
labels?: string[];
priority?: Priority | null;
coverImage?: string | null;
attachments?: { id: string; name: string }[];
subtasks?: { id: string; title: string; done: boolean; taskId?: string }[];
links?: { taskId: string; relation: "relates" | "blocks" | "blocked-by" }[];
due?: { date: string };
plan?: Plan;
sessions: Session[];
archived: boolean;
}
export interface Task extends TaskFields {
id: string;
path: string;
title: string;
body: string;
source: string;
}
export interface Entry {
name: string;
rel_path: string;
kind: string;
}
export interface Workspace {
list(path?: string): Promise<Entry[]>;
listMarkdown?(): Promise<string[]>;
read(path: string): Promise<string>;
readMany?(paths: string[]): Promise<{ path: string; source?: string; error?: string }[]>;
readOptional?(path: string): Promise<string | null>;
write(path: string, contents: string, expected: string): Promise<void>;
create?(path: string, contents: string): Promise<void>;
subscribe?(listener: () => void): () => void;
}
export function validDate(value: unknown): value is string {
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(value))
return false;
const date = new Date(`${value}T00:00:00Z`);
return !isNaN(date.getTime()) && date.toISOString().slice(0, 10) === value;
}
export function addDays(value: string, days: number): string {
if (!validDate(value)) throw new Error("Enter a valid calendar date.");
const date = new Date(`${value}T12:00:00Z`);
date.setUTCDate(date.getUTCDate() + days);
return date.toISOString().slice(0, 10);
}
export function dayDistance(start: string, end: string): number {
return Math.round(
(Date.parse(`${end}T12:00:00Z`) - Date.parse(`${start}T12:00:00Z`)) /
86_400_000,
);
}
export function localDay(date = new Date()): string {
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
}
export function localTime(instant: string): string {
const date = new Date(instant);
return `${localDay(date)}T${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
}
export function localInstant(value: string): string {
const date = new Date(value);
if (isNaN(date.getTime()) || localTime(date.toISOString()) !== value)
throw new Error("Enter an existing local date and time.");
return date.toISOString();
}
export function validateFields(fields: TaskFields): void {
if (fields.createdAt !== undefined && (typeof fields.createdAt !== "string" ||
!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/.test(fields.createdAt) ||
!validDate(fields.createdAt.slice(0, 10)) || !Number.isFinite(Date.parse(fields.createdAt))))
throw new Error("The card creation timestamp is invalid.");
if (fields.attachments !== undefined && (!Array.isArray(fields.attachments) || fields.attachments.some(item => !item || typeof item.id !== "string" || !/^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(item.id) || typeof item.name !== "string" || !item.name.trim() || item.name.length > 255) || new Set(fields.attachments.map(item => item.id)).size !== fields.attachments.length))
throw new Error("Each image attachment needs a unique image ID and a name of 1 to 255 characters.");
if (fields.priority !== undefined && fields.priority !== null && !priorities.includes(fields.priority))
throw new Error("Choose a supported priority or No priority.");
if (fields.coverImage != null && !/^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(fields.coverImage))
throw new Error("Choose an image from this card.");
if (fields.subtasks !== undefined && (!Array.isArray(fields.subtasks) || fields.subtasks.some(item => !item || typeof item.id !== "string" || !item.id || typeof item.title !== "string" || !item.title.trim() || item.title.length > 200 || typeof item.done !== "boolean" || (item.taskId !== undefined && (typeof item.taskId !== "string" || !item.taskId))) || new Set(fields.subtasks.map(item => item.id)).size !== fields.subtasks.length))
throw new Error("Each subtask needs a unique ID, a title, and a completion state.");
if (fields.links !== undefined && (!Array.isArray(fields.links) || fields.links.some(item => !item || typeof item.taskId !== "string" || !item.taskId || !["relates", "blocks", "blocked-by"].includes(item.relation)) || new Set(fields.links.map(item => item.taskId)).size !== fields.links.length))
throw new Error("Choose each linked card once and select its relationship.");
if (
fields.title !== undefined &&
(typeof fields.title !== "string" ||
!fields.title.trim() ||
fields.title.length > 100 ||
/[\x00-\x1f]/.test(fields.title))
)
throw new Error("Enter a card title of 1 to 100 characters.");
const validOrder = (value: unknown) =>
value === undefined ||
(typeof value === "string" && /^[0-9a-z]*[1-9a-z]$/.test(value));
if (!validOrder(fields.order))
throw new Error("The card order is invalid. Refresh the board.");
if (fields.placements !== undefined) {
if (
!fields.placements ||
typeof fields.placements !== "object" ||
Array.isArray(fields.placements)
)
throw new Error("Board placements must be a mapping.");
for (const [id, placement] of Object.entries(fields.placements)) {
if (
!id.trim() ||
!placement ||
typeof placement !== "object" ||
Array.isArray(placement) ||
(placement.listId !== null &&
(typeof placement.listId !== "string" || !placement.listId)) ||
!validOrder(placement.order)
)
throw new Error(
"Each board placement needs a board ID and a valid list ID.",
);
}
}
if (
fields.color !== undefined &&
(typeof fields.color !== "string" ||
!Object.hasOwn(cardColors, fields.color))
)
throw new Error("Choose a supported card color.");
if (
fields.assignees !== undefined &&
(!Array.isArray(fields.assignees) ||
fields.assignees.some((name) => typeof name !== "string" || !name.trim()))
)
throw new Error("Assignments must contain names.");
if (fields.customFields !== undefined) {
if (
!Array.isArray(fields.customFields) ||
fields.customFields.some(
(field) =>
!field ||
typeof field.name !== "string" ||
!field.name.trim() ||
typeof field.value !== "string",
)
)
throw new Error("Each custom field needs a name and a text value.");
const names = fields.customFields.map((field) =>
field.name.trim().toLocaleLowerCase(),
);
if (new Set(names).size !== names.length)
throw new Error("Use a different name for each custom field.");
}
if (
fields.listId !== undefined &&
fields.listId !== null &&
(typeof fields.listId !== "string" || !fields.listId)
)
throw new Error("The task has an invalid list ID.");
if (!statuses.includes(fields.status))
throw new Error("Choose a supported task status.");
if (
!Array.isArray(fields.tags) ||
fields.tags.some((tag) => typeof tag !== "string")
)
throw new Error("Tags must be text.");
if (
fields.labels !== undefined &&
(!Array.isArray(fields.labels) ||
fields.labels.some(
(id) => typeof id !== "string" || !/^[a-zA-Z0-9_-]{1,100}$/.test(id),
) ||
new Set(fields.labels).size !== fields.labels.length)
)
throw new Error("Labels must contain unique saved label IDs.");
if (fields.due && !validDate(fields.due.date))
throw new Error("Enter a valid deadline date.");
if (
fields.plan &&
(!validDate(fields.plan.start) ||
!validDate(fields.plan.end) ||
fields.plan.end <= fields.plan.start)
) {
throw new Error("The planned end must follow the start.");
}
const ids = new Set<string>();
for (const session of fields.sessions) {
const instant =
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
if (
!session.id ||
ids.has(session.id) ||
!instant.test(session.start) ||
!instant.test(session.end) ||
!validDate(session.start.slice(0, 10)) ||
!validDate(session.end.slice(0, 10)) ||
!Number.isFinite(Date.parse(session.start)) ||
!Number.isFinite(Date.parse(session.end)) ||
Date.parse(session.end) <= Date.parse(session.start)
)
throw new Error("Each session needs a unique ID and a valid time range.");
new Intl.DateTimeFormat("en", { timeZone: session.timeZone }).format();
ids.add(session.id);
}
}