import type { Workspace } from "../model/model.ts"; import { readOptional, saveDocument } from "./persistence.ts"; import { message } from "./message.ts"; import { imagePath, parseImage } from "./images.ts"; const invalidDraft = () => new Error(message("The description draft is invalid. Preserve it before recovery.")); const sameImages = (a: ReadonlyMap, b: ReadonlyMap) => a.size === b.size && [...a].every(([id, data]) => b.get(id) === data); /** * A draft is separate from the saved card and uses compare-and-write protection. * A second file keeps the data of the images that only the draft uses. Thus a text change does not write the images again. * The image folder gets an image only from a save of the description or the card. A discarded draft keeps an empty image record, so no image file stays behind. */ export class DescriptionDraft { private source: string | null = null; private imageSource: string | null = null; private images: ReadonlyMap = new Map(); private queue: Promise = Promise.resolve(); readonly path: string; readonly imagesPath: string; private workspace: Workspace; constructor(workspace: Workspace, id: string) { this.workspace = workspace; const key = Array.from(new TextEncoder().encode(id), byte => byte.toString(16).padStart(2, "0")).join(""); this.path = `.runestone/pm/drafts/${key}.json`; this.imagesPath = `.runestone/pm/drafts/${key}.images.json`; } async load(): Promise { this.source = await readOptional(this.workspace, this.path); if (this.source === null) return null; const value = JSON.parse(this.source); if (value?.schema !== 1 || (value.body !== null && typeof value.body !== "string")) throw invalidDraft(); return value.body; } /** The image data of the draft, by image ID. A draft of an earlier version has no image record. Call this before `save`. */ async loadImages(): Promise> { this.images = new Map(); this.imageSource = await readOptional(this.workspace, this.imagesPath); if (this.imageSource === null) return new Map(); const value = JSON.parse(this.imageSource); if (value?.schema !== 1 || typeof value.images !== "object" || value.images === null || Array.isArray(value.images)) throw invalidDraft(); const images = new Map(); for (const [id, data] of Object.entries(value.images)) { try { imagePath(id); images.set(id, parseImage(JSON.stringify({ schema: 1, data }))); } catch { throw invalidDraft(); } } this.images = images; return new Map(images); } /** Save the text of the draft and the data of the images that only the draft uses. A `null` body discards the draft and its images. */ save(body: string | null, images: ReadonlyMap = new Map()): Promise { const write = this.queue.then(async () => { const next = body === null ? new Map() : images; // Keep the old and the new image data until the text is saved, so that each saved reference has its data. await this.saveImages(new Map([...this.images, ...next])); const source = JSON.stringify({ schema: 1, body }) + "\n"; if (source !== this.source && !(body === null && this.source === null)) { await saveDocument(this.workspace, this.path, source, this.source); this.source = source; } await this.saveImages(next); }); this.queue = write.catch(() => {}); return write; } private async saveImages(images: ReadonlyMap) { if (sameImages(images, this.images) || (!images.size && this.imageSource === null)) return; const sorted = [...images].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0); const source = JSON.stringify({ schema: 1, images: Object.fromEntries(sorted) }) + "\n"; await saveDocument(this.workspace, this.imagesPath, source, this.imageSource); this.imageSource = source; this.images = new Map(images); } }