Files
outline/app/models/Template.ts
T
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

191 lines
4.0 KiB
TypeScript

import { addDays } from "date-fns";
import i18n from "i18next";
import { computed, observable } from "mobx";
import type { ProsemirrorData } from "@shared/types";
import { ProsemirrorDataHelper } from "@shared/utils/ProsemirrorDataHelper";
import { isRTL } from "@shared/utils/rtl";
import slugify from "@shared/utils/slugify";
import type TemplatesStore from "~/stores/TemplatesStore";
import User from "~/models/User";
import { settingsPath } from "~/utils/routeHelpers";
import Collection from "./Collection";
import ParanoidModel from "./base/ParanoidModel";
import Field from "./decorators/Field";
import Relation from "./decorators/Relation";
import type { Searchable } from "./interfaces/Searchable";
export default class Template extends ParanoidModel implements Searchable {
static modelName = "Template";
store: TemplatesStore;
@Field
@observable.shallow
data: ProsemirrorData;
@computed
get searchContent(): string {
return this.title;
}
@computed
get searchSuppressed(): boolean {
return this.isDeleted;
}
/**
* The id of the collection that this template belongs to, if any.
*/
@Field
@observable
collectionId?: string | null;
/**
* The collection that this template belongs to.
*/
@Relation(() => Collection, { onDelete: "cascade" })
collection?: Collection;
/**
* The title of the template.
*/
@Field
@observable
title: string;
/**
* An icon (or) emoji to use as the template icon.
*/
@Field
@observable
icon?: string | null;
/**
* The color to use for the template icon.
*/
@Field
@observable
color?: string | null;
/**
* Whether the template layout is displayed full page width.
*/
@Field
@observable
fullWidth: boolean;
/**
* The likely language of the template, in ISO 639-1 format.
*/
@Field
@observable
language: string | undefined;
@Relation(() => User)
createdBy: User | undefined;
@Relation(() => User)
updatedBy: User | undefined;
@observable
urlId: string;
/**
* The date the template was published, and so made available to other members
* of the workspace.
*/
@observable
publishedAt: string | undefined;
/**
* Publishes the template, making it available to other members of the
* workspace.
*
* @returns a promise that resolves when the template has been published.
*/
publish = () => this.save(undefined, { publish: true });
/**
* Returns the direction of the template text, either "rtl" or "ltr"
*/
@computed
get dir(): "rtl" | "ltr" {
return this.rtl ? "rtl" : "ltr";
}
/**
* Returns true if the template text is right-to-left
*/
@computed
get rtl() {
return isRTL(this.title);
}
@computed
get path(): string {
if (!this.title) {
return `${settingsPath("templates")}/untitled-${this.urlId}`;
}
const slugifiedTitle = slugify(this.title);
return `${settingsPath("templates")}/${slugifiedTitle}-${this.urlId}`;
}
@computed
get isDeleted(): boolean {
return !!this.deletedAt;
}
get hasEmptyTitle(): boolean {
return this.title === "";
}
/**
* Whether the template has neither a title nor any content.
*/
@computed
get isEmpty(): boolean {
return (
!this.title?.trim() &&
(!this.data || ProsemirrorDataHelper.isEmpty(this.data))
);
}
@computed
get isWorkspaceTemplate(): boolean {
return !this.collectionId;
}
/**
* Whether the template is an unpublished draft, only visible to the user that
* created it.
*/
@computed
get isDraft(): boolean {
return !this.publishedAt;
}
@computed
get permanentlyDeletedAt(): string | undefined {
if (!this.deletedAt) {
return undefined;
}
return addDays(new Date(this.deletedAt), 30).toString();
}
get titleWithDefault(): string {
return this.title || i18n.t("Untitled");
}
@computed
get initial(): string {
return (this.titleWithDefault?.charAt(0) ?? "?").toUpperCase();
}
@computed
get isActive(): boolean {
return !this.isDeleted;
}
}