From c71f1382a2fd51d1990cbd6c407e426b94acc511 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 16 Mar 2025 14:45:16 -0400 Subject: [PATCH] wip --- app/actions/definitions/documents.tsx | 24 +-- .../CommandBar/useTemplatesAction.tsx | 10 +- app/hooks/useSettingsConfig.ts | 2 +- app/menus/DocumentMenu.tsx | 9 +- app/models/Document.ts | 28 +-- app/models/Template.ts | 145 ++++++++++++++ app/scenes/Document/components/Header.tsx | 4 +- app/scenes/Settings/Templates.tsx | 15 +- app/stores/DocumentsStore.ts | 31 --- app/stores/RootStore.ts | 3 + app/stores/TemplatesStore.ts | 16 ++ server/models/Document.ts | 8 +- server/models/Template.ts | 184 ++++++++++++++++++ server/models/helpers/DocumentHelper.tsx | 8 +- server/models/index.ts | 2 + server/policies/document.ts | 99 +++------- server/policies/team.test.ts | 2 +- server/policies/team.ts | 32 +-- server/policies/template.ts | 67 +++++++ server/presenters/index.ts | 2 + server/presenters/template.ts | 32 +++ server/routes/api/documents/documents.ts | 41 +--- server/routes/api/documents/schema.ts | 3 - server/routes/api/index.ts | 2 + server/routes/api/templates/index.ts | 1 + server/routes/api/templates/schema.ts | 27 +++ server/routes/api/templates/templates.ts | 67 +++++++ server/storage/database.ts | 2 +- 28 files changed, 610 insertions(+), 256 deletions(-) create mode 100644 app/models/Template.ts create mode 100644 app/stores/TemplatesStore.ts create mode 100644 server/models/Template.ts create mode 100644 server/policies/template.ts create mode 100644 server/presenters/template.ts create mode 100644 server/routes/api/templates/index.ts create mode 100644 server/routes/api/templates/schema.ts create mode 100644 server/routes/api/templates/templates.ts diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index 911a466ed5..388fbf2669 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -155,12 +155,7 @@ export const createDocumentFromTemplate = createAction({ ? stores.documents.get(activeDocumentId) : undefined; - if ( - !currentTeamId || - !document?.isTemplate || - !!document?.isDraft || - !!document?.isDeleted - ) { + if (!currentTeamId || !!document?.isDraft || !!document?.isDeleted) { return false; } @@ -766,7 +761,7 @@ export const createTemplateFromDocument = createAction({ const document = activeDocumentId ? stores.documents.get(activeDocumentId) : undefined; - if (document?.isTemplate || !document?.isActive) { + if (!document?.isActive) { return false; } return !!( @@ -829,7 +824,7 @@ export const moveTemplateToWorkspace = createAction({ return false; } const document = stores.documents.get(activeDocumentId); - if (!document || !document.template || document.isWorkspaceTemplate) { + if (!document) { return false; } return !!stores.policies.abilities(activeDocumentId).move; @@ -895,8 +890,7 @@ export const moveDocument = createAction({ return false; } const document = stores.documents.get(activeDocumentId); - // Don't show the button if this is a non-workspace template. - if (!document || (document.template && !document.isWorkspaceTemplate)) { + if (!document) { return false; } return !!stores.policies.abilities(activeDocumentId).move; @@ -914,8 +908,7 @@ export const moveTemplate = createAction({ return false; } const document = stores.documents.get(activeDocumentId); - // Don't show the menu if this is not a template (or) a workspace template. - if (!document || !document.template || document.isWorkspaceTemplate) { + if (!document) { return false; } return !!stores.policies.abilities(activeDocumentId).move; @@ -1106,12 +1099,7 @@ export const openDocumentInsights = createAction({ ? stores.documents.get(activeDocumentId) : undefined; - return ( - !!activeDocumentId && - can.listViews && - !document?.isTemplate && - !document?.isDeleted - ); + return !!activeDocumentId && can.listViews && !document?.isDeleted; }, perform: ({ activeDocumentId, stores }) => { if (!activeDocumentId) { diff --git a/app/components/CommandBar/useTemplatesAction.tsx b/app/components/CommandBar/useTemplatesAction.tsx index 0ceacbf087..293bcdaf6c 100644 --- a/app/components/CommandBar/useTemplatesAction.tsx +++ b/app/components/CommandBar/useTemplatesAction.tsx @@ -12,15 +12,15 @@ import history from "~/utils/history"; import { newDocumentPath } from "~/utils/routeHelpers"; const useTemplatesAction = () => { - const { documents } = useStores(); + const { templates } = useStores(); React.useEffect(() => { - void documents.fetchAllTemplates(); - }, [documents]); + void templates.fetchAll(); + }, [templates]); const actions = React.useMemo( () => - documents.templatesAlphabetical.map((template) => + templates.orderedData.map((template) => createAction({ name: template.titleWithDefault, analyticsName: "New document", @@ -58,7 +58,7 @@ const useTemplatesAction = () => { ), }) ), - [documents.templatesAlphabetical] + [templates.orderedData] ); const newFromTemplate = React.useMemo( diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index 89935f8c80..faa4c99bea 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -140,7 +140,7 @@ const useSettingsConfig = () => { name: t("Templates"), path: settingsPath("templates"), component: Templates, - enabled: can.readTemplate, + enabled: can.createDocument, group: t("Workspace"), icon: ShapesIcon, }, diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx index 6e4d135c43..b4afa1e801 100644 --- a/app/menus/DocumentMenu.tsx +++ b/app/menus/DocumentMenu.tsx @@ -231,9 +231,7 @@ const MenuContent: React.FC = observer(function MenuContent_({ { type: "button", title: t("Restore"), - visible: - !!(document.isWorkspaceTemplate || collection?.isActive) && - !!(can.restore || can.unarchive), + visible: !!collection?.isActive && !!(can.restore || can.unarchive), onClick: (ev) => handleRestore(ev), icon: , }, @@ -241,7 +239,7 @@ const MenuContent: React.FC = observer(function MenuContent_({ type: "submenu", title: t("Restore"), visible: - !(document.isWorkspaceTemplate || collection?.isActive) && + !collection?.isActive && !!(can.restore || can.unarchive) && restoreItems.length !== 0, style: { @@ -289,8 +287,7 @@ const MenuContent: React.FC = observer(function MenuContent_({ type: "route", title: t("Edit"), to: documentEditPath(document), - visible: - !!can.update && user.separateEditMode && !document.template, + visible: !!can.update && user.separateEditMode, icon: , }, { diff --git a/app/models/Document.ts b/app/models/Document.ts index 57a530a93a..a54c50cf80 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -24,7 +24,6 @@ import DocumentsStore from "~/stores/DocumentsStore"; import User from "~/models/User"; import type { Properties } from "~/types"; import { client } from "~/utils/ApiClient"; -import { settingsPath } from "~/utils/routeHelpers"; import Collection from "./Collection"; import Notification from "./Notification"; import Pin from "./Pin"; @@ -146,12 +145,6 @@ export default class Document extends ArchivableModel implements Searchable { @observable color?: string | null; - /** - * Whether this is a template. - */ - @observable - template: boolean; - /** * Whether the document layout is displayed full page width. */ @@ -258,20 +251,17 @@ export default class Document extends ArchivableModel implements Searchable { @computed get path(): string { - const prefix = - this.template && !this.isDeleted ? settingsPath("templates") : "/doc"; - if (!this.title) { - return `${prefix}/untitled-${this.urlId}`; + return `/doc/untitled-${this.urlId}`; } const slugifiedTitle = slugify(this.title); - return `${prefix}/${slugifiedTitle}-${this.urlId}`; + return `/doc/${slugifiedTitle}-${this.urlId}`; } @computed get noun(): string { - return this.template ? t("template") : t("document"); + return t("document"); } @computed @@ -353,11 +343,6 @@ export default class Document extends ArchivableModel implements Searchable { return !!this.deletedAt; } - @computed - get isTemplate(): boolean { - return !!this.template; - } - @computed get isDraft(): boolean { return !this.publishedAt; @@ -423,11 +408,6 @@ export default class Document extends ArchivableModel implements Searchable { return path.map((item) => item.asNavigationNode); } - @computed - get isWorkspaceTemplate() { - return this.template && !this.collectionId; - } - get titleWithDefault(): string { return this.title || i18n.t("Untitled"); } @@ -620,7 +600,7 @@ export default class Document extends ArchivableModel implements Searchable { @computed get isActive(): boolean { - return !this.isDeleted && !this.isTemplate && !this.isArchived; + return !this.isDeleted && !this.isArchived; } @computed diff --git a/app/models/Template.ts b/app/models/Template.ts new file mode 100644 index 0000000000..b0e90c0009 --- /dev/null +++ b/app/models/Template.ts @@ -0,0 +1,145 @@ +import type { ProsemirrorData } from "@shared/types"; +import { isRTL } from "@shared/utils/rtl"; +import slugify from "@shared/utils/slugify"; +import { addDays } from "date-fns"; +import i18n from "i18next"; +import { computed, observable } from "mobx"; +import TemplatesStore from "~/stores/TemplatesStore"; +import User from "~/models/User"; +import Collection from "./Collection"; +import ParanoidModel from "./base/ParanoidModel"; +import Field from "./decorators/Field"; +import Relation from "./decorators/Relation"; +import { Searchable } from "./interfaces/Searchable"; + +export default class Template extends ParanoidModel implements Searchable { + static modelName = "Template"; + + store: TemplatesStore; + + @observable.shallow + data: ProsemirrorData; + + @computed + get searchContent(): string { + return this.title; + } + + /** + * The id of the collection that this document belongs to, if any. + */ + @Field + @observable + collectionId?: string | null; + + /** + * The collection that this document belongs to. + */ + @Relation(() => Collection, { onDelete: "cascade" }) + collection?: Collection; + + /** + * The title of the document. + */ + @Field + @observable + title: string; + + /** + * An icon (or) emoji to use as the document icon. + */ + @Field + @observable + icon?: string | null; + + /** + * The color to use for the document icon. + */ + @Field + @observable + color?: string | null; + + /** + * Whether the document layout is displayed full page width. + */ + @Field + @observable + fullWidth: boolean; + + @Relation(() => User) + createdBy: User | undefined; + + @Relation(() => User) + @observable + updatedBy: User | undefined; + + @observable + publishedAt: string | undefined; + + @observable + urlId: string; + + /** + * 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 `/settings/templates/untitled-${this.urlId}`; + } + + const slugifiedTitle = slugify(this.title); + return `/settings/templates/${slugifiedTitle}-${this.urlId}`; + } + + @computed + get isDeleted(): boolean { + return !!this.deletedAt; + } + + @computed + get isDraft(): boolean { + return !this.publishedAt; + } + + @computed + get hasEmptyTitle(): boolean { + return this.title === ""; + } + + @computed + get isWorkspaceTemplate(): boolean { + return !this.collectionId; + } + + @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 isActive(): boolean { + return !this.isDeleted; + } +} diff --git a/app/scenes/Document/components/Header.tsx b/app/scenes/Document/components/Header.tsx index 554ca2bbfa..6011148036 100644 --- a/app/scenes/Document/components/Header.tsx +++ b/app/scenes/Document/components/Header.tsx @@ -368,9 +368,7 @@ function DocumentHeader({ hideOnActionDisabled hideIcon > - {document.collectionId || document.isWorkspaceTemplate - ? t("Publish") - : `${t("Publish")}…`} + {document.collectionId ? t("Publish") : `${t("Publish")}…`} )} diff --git a/app/scenes/Settings/Templates.tsx b/app/scenes/Settings/Templates.tsx index d78311ac4f..c3736a74c5 100644 --- a/app/scenes/Settings/Templates.tsx +++ b/app/scenes/Settings/Templates.tsx @@ -17,16 +17,11 @@ import NewTemplateMenu from "~/menus/NewTemplateMenu"; import { settingsPath } from "~/utils/routeHelpers"; function Templates() { - const { documents } = useStores(); + const { templates } = useStores(); const { t } = useTranslation(); const param = useQuery(); - const { fetchTemplates, templates, templatesAlphabetical } = documents; const sort = param.get("sort") || "recent"; - React.useEffect(() => { - void documents.fetchDrafts(); - }, [documents]); - return ( } @@ -65,8 +60,12 @@ function Templates() { } empty={{t("There are no templates just yet.")}} - fetch={fetchTemplates} - documents={sort === "alphabetical" ? templatesAlphabetical : templates} + fetch={templates.fetchAll} + documents={ + sort === "alphabetical" + ? templates.alphabetical + : templates.orderedData + } showCollection showDraft /> diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts index 32ede115a2..165bfcaad7 100644 --- a/app/stores/DocumentsStore.ts +++ b/app/stores/DocumentsStore.ts @@ -99,17 +99,6 @@ export default class DocumentsStore extends Store { return orderBy(this.all, "updatedAt", "desc"); } - get templates(): Document[] { - return orderBy( - filter( - this.orderedData, - (d) => !d.archivedAt && !d.deletedAt && d.template - ), - "updatedAt", - "desc" - ); - } - createdByUser(userId: string): Document[] { return orderBy( filter(this.all, (d) => d.createdBy?.id === userId), @@ -152,21 +141,6 @@ export default class DocumentsStore extends Store { ); } - templatesInCollection(collectionId: string): Document[] { - return orderBy( - filter( - this.orderedData, - (d) => - !d.archivedAt && - !d.deletedAt && - d.template === true && - d.collectionId === collectionId - ), - "updatedAt", - "desc" - ); - } - publishedInCollection(collectionId: string): Document[] { return filter( this.all, @@ -231,11 +205,6 @@ export default class DocumentsStore extends Store { ); } - @computed - get templatesAlphabetical(): Document[] { - return naturalSort(this.templates, "title"); - } - @computed get totalDrafts(): number { return this.drafts().length; diff --git a/app/stores/RootStore.ts b/app/stores/RootStore.ts index 3182f5618f..91bc8bd67a 100644 --- a/app/stores/RootStore.ts +++ b/app/stores/RootStore.ts @@ -24,6 +24,7 @@ import SearchesStore from "./SearchesStore"; import SharesStore from "./SharesStore"; import StarsStore from "./StarsStore"; import SubscriptionsStore from "./SubscriptionsStore"; +import TemplatesStore from "./TemplatesStore"; import UiStore from "./UiStore"; import UserMembershipsStore from "./UserMembershipsStore"; import UsersStore from "./UsersStore"; @@ -55,6 +56,7 @@ export default class RootStore { ui: UiStore; stars: StarsStore; subscriptions: SubscriptionsStore; + templates: TemplatesStore; users: UsersStore; views: ViewsStore; fileOperations: FileOperationsStore; @@ -82,6 +84,7 @@ export default class RootStore { this.registerStore(SharesStore); this.registerStore(StarsStore); this.registerStore(SubscriptionsStore); + this.registerStore(TemplatesStore); this.registerStore(UsersStore); this.registerStore(ViewsStore); this.registerStore(FileOperationsStore); diff --git a/app/stores/TemplatesStore.ts b/app/stores/TemplatesStore.ts new file mode 100644 index 0000000000..b3588186a1 --- /dev/null +++ b/app/stores/TemplatesStore.ts @@ -0,0 +1,16 @@ +import naturalSort from "@shared/utils/naturalSort"; +import { computed } from "mobx"; +import Template from "~/models/Template"; +import RootStore from "./RootStore"; +import Store from "./base/Store"; + +export default class TemplatesStore extends Store