Files
outline/server/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

299 lines
6.6 KiB
TypeScript

import { isUUID } from "class-validator";
import type {
Identifier,
InferAttributes,
InferCreationAttributes,
NonNullFindOptions,
FindOptions,
} from "sequelize";
import { EmptyResultError } from "sequelize";
import {
Column,
DataType,
BelongsTo,
ForeignKey,
Table,
Length as SimpleLength,
DefaultScope,
Default,
HasMany,
Unique,
Scopes,
BeforeValidate,
IsDate,
} from "sequelize-typescript";
import slugify from "slugify";
import type { ProsemirrorData } from "@shared/types";
import { UrlHelper } from "@shared/utils/UrlHelper";
import { DocumentValidation } from "@shared/validations";
import { generateUrlId } from "@server/utils/url";
import Collection from "./Collection";
import Revision from "./Revision";
import Team from "./Team";
import User from "./User";
import ParanoidModel from "./base/ParanoidModel";
import IsHexColor from "./validators/IsHexColor";
import Length from "./validators/Length";
type AdditionalFindOptions = {
userId?: string;
includeState?: boolean;
rejectOnEmpty?: boolean | Error;
};
@DefaultScope(() => ({
include: [
{
association: "createdBy",
paranoid: false,
},
{
association: "updatedBy",
paranoid: false,
},
],
where: {
template: true,
},
attributes: {
exclude: ["state"],
},
}))
@Scopes(() => ({
withMembership: (userId: string, paranoid = true) => ({
include: [
{
model: userId
? Collection.scope([
"defaultScope",
{
method: ["withMembership", userId],
},
])
: Collection,
as: "collection",
paranoid,
},
],
}),
withCollection: {
include: [
{
model: Collection,
as: "collection",
},
],
},
}))
@Table({ tableName: "documents", modelName: "template" })
class Template extends ParanoidModel<
InferAttributes<Template>,
Partial<InferCreationAttributes<Template>>
> {
/** The namespace to use for events. */
static eventNamespace = "templates";
@SimpleLength({
min: 10,
max: 10,
msg: `urlId must be 10 characters`,
})
@Unique
@Column(DataType.STRING)
urlId: string;
@Length({
max: DocumentValidation.maxTitleLength,
msg: `Template title must be ${DocumentValidation.maxTitleLength} characters or less`,
})
@Column(DataType.STRING)
title: string;
@Default(false)
@Column(DataType.BOOLEAN)
fullWidth: boolean;
@Default(true)
@Column(DataType.BOOLEAN)
template: boolean;
/** The version of the editor last used to edit this template. */
@SimpleLength({
max: 255,
msg: `editorVersion must be 255 characters or less`,
})
@Column(DataType.STRING)
editorVersion: string | null;
/** An icon to use as the template icon. */
@Length({
max: 50,
msg: `icon must be 50 characters or less`,
})
@Column(DataType.STRING)
icon: string | null;
/** The color of the icon. */
@IsHexColor
@Column(DataType.STRING)
color: string | null;
/** The likely language of the template, in ISO 639-1 format. */
@Column(DataType.STRING)
language: string | null;
/**
* The content of the template as JSON, this is a snapshot at the last time the state was saved.
*/
@Column(DataType.JSONB)
content: ProsemirrorData | null;
// associations
@BelongsTo(() => User, "lastModifiedById")
updatedBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
lastModifiedById: string;
@BelongsTo(() => User, "createdById")
createdBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
createdById: string;
@BelongsTo(() => Team, "teamId")
team: Team;
@ForeignKey(() => Team)
@Column(DataType.UUID)
teamId: string;
@BelongsTo(() => Collection, "collectionId")
collection: Collection | null;
@ForeignKey(() => Collection)
@Column(DataType.UUID)
collectionId?: string | null;
@HasMany(() => Revision, "documentId")
revisions: Revision[];
/** Whether the template is published, and if so when. */
@IsDate
@Column(DataType.DATE)
publishedAt: Date | null;
// getters
/** The frontend path to this template. */
get path() {
if (!this.title) {
return `/settings/templates/untitled-${this.urlId}`;
}
const slugifiedTitle = slugify(this.title);
return `/settings/templates/${slugifiedTitle}-${this.urlId}`;
}
/**
* Returns whether this is a workspace template.
*
* @returns boolean
*/
get isWorkspaceTemplate() {
return !this.collectionId;
}
/**
* Returns whether this template is an unpublished draft, only visible to the
* user that created it.
*
* @returns boolean
*/
get isDraft() {
return !this.publishedAt;
}
@BeforeValidate
static createUrlId(model: Template) {
return (model.urlId = model.urlId || generateUrlId());
}
/**
* Overrides the standard findByPk behavior to allow also querying by urlId
*
* @param id uuid or urlId
* @param options FindOptions
* @returns A promise resolving to a template instance or null
*/
static async findByPk(
id: Identifier,
options?: NonNullFindOptions<Template> & AdditionalFindOptions
): Promise<Template>;
static async findByPk(
id: Identifier,
options?: FindOptions<Template> & AdditionalFindOptions
): Promise<Template | null>;
static async findByPk(
id: Identifier,
options: (NonNullFindOptions<Template> | FindOptions<Template>) &
AdditionalFindOptions = {}
): Promise<Template | null> {
if (typeof id !== "string") {
return null;
}
const { includeState, userId, ...rest } = options;
// allow default preloading of collection membership if `userId` is passed in find options
// almost every endpoint needs the collection membership to determine policy permissions.
const scope = this.scope([
"defaultScope",
{
method: ["withMembership", userId, rest.paranoid],
},
]);
if (isUUID(id)) {
const template = await scope.findOne({
where: {
id,
},
...rest,
rejectOnEmpty: false,
});
if (!template && rest.rejectOnEmpty) {
throw new EmptyResultError(`Template doesn't exist with id: ${id}`);
}
return template;
}
const match = id.match(UrlHelper.SLUG_URL_REGEX);
if (match) {
const template = await scope.findOne({
where: {
urlId: match[1],
},
...rest,
rejectOnEmpty: false,
});
if (!template && rest.rejectOnEmpty) {
throw new EmptyResultError(`Template doesn't exist with id: ${id}`);
}
return template;
}
return null;
}
}
export default Template;