Files
Tom MoorandClaude Opus 5 e4d07b4f24 Persist template drafts as they are written (#13357)
* fix: Warn before discarding unsaved template changes

Template content is only persisted when the form is explicitly submitted,
so navigating away or closing the tab silently lost everything typed.

Adds a navigation Prompt and a beforeunload handler to both template
scenes, and reverts the model on unmount so abandoned edits no longer
leak into other views rendering the same record.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Persist templates from the first keystroke

New templates were held only in browser memory until Save was pressed, so
a refresh, crash, or stray navigation lost everything written.

Templates are now created up front as an unpublished draft and autosaved,
mirroring how documents behave. Drafts are visible only to their author
until published, and are cleaned up if abandoned while empty.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Preserve icon edits made during an in-flight template save

applyEdits only restored title and content, so an icon or color changed
while a save was in flight was overwritten by the response and never
re-saved.

Pending edits are now tracked as a single record covering every field the
form can change, rather than one ref per field.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feedback

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 00:09:20 +00:00

66 lines
1.7 KiB
TypeScript

import stores from "~/stores";
import Template from "./Template";
describe("Template model", () => {
const templates = stores.templates;
describe("isDraft", () => {
it("should be true when the template has not been published", () => {
const template = new Template(
{ id: "123", title: "Untitled" },
templates
);
expect(template.isDraft).toBe(true);
});
it("should be false once the template has been published", () => {
const template = new Template(
{
id: "124",
title: "Untitled",
publishedAt: "2026-08-08T00:00:00.000Z",
},
templates
);
expect(template.isDraft).toBe(false);
});
});
describe("isEmpty", () => {
it("should be true without a title or content", () => {
const template = new Template({ id: "125", title: "" }, templates);
expect(template.isEmpty).toBe(true);
});
it("should be true when the title is only whitespace", () => {
const template = new Template({ id: "126", title: " " }, templates);
expect(template.isEmpty).toBe(true);
});
it("should be false with a title", () => {
const template = new Template({ id: "127", title: "Weekly" }, templates);
expect(template.isEmpty).toBe(false);
});
it("should be false with content", () => {
const template = new Template(
{
id: "128",
title: "",
data: {
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "hello" }],
},
],
},
},
templates
);
expect(template.isEmpty).toBe(false);
});
});
});