import type { Task } from "../model/model.ts"; const digits = "0123456789abcdefghijklmnopqrstuvwxyz"; const encode = (text: string) => text .split("") .map((char) => char.charCodeAt(0).toString(16).padStart(4, "0")) .join(""); export function cardOrder(task: Task, boardId?: string): string { const saved = boardId ? task.placements?.[boardId]?.order : task.order; // Old notes keep filename order. Title edits do not change their positions. const position = saved ?? legacyPosition(task.path); // The ID breaks ties when separate windows choose the same position. return position + "i" + encode(task.id) + "8"; } export function legacyPosition(path: string): string { return encode(path.split("/").at(-1)!.toLowerCase()) + "0000" + encode(path); } export function compareCards(a: Task, b: Task, boardId?: string): number { const left = cardOrder(a, boardId), right = cardOrder(b, boardId); return left < right ? -1 : left > right ? 1 : 0; } export function orderBetween(lower?: string, upper?: string): string { if (lower !== undefined && upper !== undefined && lower >= upper) throw new Error("The card order changed. Refresh the board."); let result = ""; for (let index = 0; ; index++) { const low = index < (lower?.length ?? 0) ? digits.indexOf(lower![index]) : 0; const high = upper === undefined ? digits.length : digits.indexOf(upper[index]); if (high - low > 1) return result + digits[Math.floor((low + high) / 2)]; result += digits[low]; if (low < high) upper = undefined; } }