Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ export function pad2(n: number): string {
return n.toString().padStart(2, "0");
}

// 年(数値)・月・日(各2桁ゼロ埋め文字列)を取り出す。日付スタンプ生成の共通部分。
function dateParts(now: Date): { year: number; month: string; day: string } {
return { year: now.getFullYear(), month: pad2(now.getMonth() + 1), day: pad2(now.getDate()) };
}

export function todayStamp(): string {
const now = new Date();
return `${now.getFullYear()}-${pad2(now.getMonth() + 1)}-${pad2(now.getDate())}`;
const { year, month, day } = dateParts(new Date());
return `${year}-${month}-${day}`;
}

export function fileStamp(): string {
const now = new Date();
return `${now.getFullYear()}${pad2(now.getMonth() + 1)}${pad2(now.getDate())}-${pad2(now.getHours())}${pad2(now.getMinutes())}`;
const { year, month, day } = dateParts(now);
return `${year}${month}${day}-${pad2(now.getHours())}${pad2(now.getMinutes())}`;
}

export function randomOf<T>(arr: readonly T[]): T | undefined {
Expand Down
17 changes: 11 additions & 6 deletions src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,18 @@ function hexColor(v: unknown, fallback: string): string {
return typeof v === "string" && HEX_COLOR.test(v) ? v : fallback;
}

// 既存の id を採用し、無ければ新規発番する(正規化対象すべてで共通の id 決定ロジック)。
function idOf(r: Record<string, unknown>): string {
return str(r.id) || newId();
}

function normalizeStep(raw: unknown): Step {
const r = rec(raw);
if (r.type === "if") {
const branches = rec(r.branches);
const labels = rec(r.labels);
return {
id: str(r.id) || newId(),
id: idOf(r),
type: "if",
text: str(r.text),
done: bool(r.done),
Expand All @@ -213,13 +218,13 @@ function normalizeStep(raw: unknown): Step {
},
};
}
return { id: str(r.id) || newId(), type: "step", text: str(r.text), done: bool(r.done) };
return { id: idOf(r), type: "step", text: str(r.text), done: bool(r.done) };
}

// ステッカー・シェイプ・写真に共通する配置情報(id と座標・回転)を正規化する。
function normalizePlacement(r: Record<string, unknown>): Placement {
return {
id: str(r.id) || newId(),
id: idOf(r),
x: clampNum(r.x, 0, 0, MAX_POSITION),
y: clampNum(r.y, 0, 0, MAX_POSITION),
rot: num(r.rot),
Expand Down Expand Up @@ -262,7 +267,7 @@ function normalizeArrow(raw: unknown): Arrow | null {
const from = str(r.from);
const to = str(r.to);
if (!from || !to) return null;
const id = str(r.id) || newId();
const id = idOf(r);
const mx = num(r.mx);
const my = num(r.my);
const length = num(r.length);
Expand All @@ -283,7 +288,7 @@ export function normalizePage(raw: unknown): Page {
const shapes = list(r.shapes).map(normalizeShape);
const shapeIds = new Set(shapes.map((s) => s.id));
return {
id: str(r.id) || newId(),
id: idOf(r),
type: oneOf<PageType>(r.type, ["flowchart", "notebook"], "flowchart"),
title: str(r.title),
frame: oneOf<Frame>(r.frame, FRAMES, "aurora"),
Expand Down Expand Up @@ -325,7 +330,7 @@ export function normalizeNotebook(raw: unknown): Notebook | null {
}

return {
id: str(r.id) || newId(),
id: idOf(r),
name: str(r.name),
type,
color: hexColor(r.color, "#C9B6E4"),
Expand Down
Loading