diff --git a/app/components/ContextMenu/Template.tsx b/app/components/ContextMenu/Template.tsx
index 1050203c19..e2213662d1 100644
--- a/app/components/ContextMenu/Template.tsx
+++ b/app/components/ContextMenu/Template.tsx
@@ -200,7 +200,8 @@ function Template({ items, actions, context, showIcons, ...menu }: Props) {
}
if (item.type === "submenu") {
- return (
+ // Skip rendering empty submenus
+ return item.items.length > 0 ? (
- );
+ ) : null;
}
if (item.type === "separator") {
diff --git a/app/hooks/useTemplateMenuItems.tsx b/app/hooks/useTemplateMenuItems.tsx
new file mode 100644
index 0000000000..bf4ba99411
--- /dev/null
+++ b/app/hooks/useTemplateMenuItems.tsx
@@ -0,0 +1,88 @@
+import { DocumentIcon } from "outline-icons";
+import * as React from "react";
+import { useTranslation } from "react-i18next";
+import Icon from "@shared/components/Icon";
+import { TextHelper } from "@shared/utils/TextHelper";
+import Document from "~/models/Document";
+import useCurrentUser from "~/hooks/useCurrentUser";
+import useStores from "~/hooks/useStores";
+import { MenuItem } from "~/types";
+
+type Props = {
+ /** The document to which the templates will be applied */
+ document: Document;
+ /** Callback to handle when a template is selected */
+ onSelectTemplate?: (template: Document) => void;
+};
+
+/**
+ * This hook provides a memoized list of menu items for both collection-specific
+ * templates and workspace-wide templates. It filters templates based on whether
+ * they are published and organizes them into appropriate sections.
+ *
+ * Collection-specific templates are displayed first, followed by workspace templates
+ * with a separator in between (if both types exist).
+ *
+ * @returns An array of MenuItem objects representing templates that can be applied
+ * to the current document. Returns an empty array if no callback is provided.
+ */
+export function useTemplateMenuItems({ document, onSelectTemplate }: Props) {
+ const user = useCurrentUser();
+ const { documents } = useStores();
+ const { t } = useTranslation();
+
+ const templateToMenuItem = React.useCallback(
+ (template: Document): MenuItem => ({
+ type: "button",
+ title: TextHelper.replaceTemplateVariables(
+ template.titleWithDefault,
+ user
+ ),
+ icon: template.icon ? (
+
+ ) : (
+
+ ),
+ onClick: () => onSelectTemplate?.(template),
+ }),
+ [user, onSelectTemplate]
+ );
+
+ const templates = documents.templates.filter(
+ (template) => template.publishedAt
+ );
+
+ const collectionItems = templates
+ .filter(
+ (template) =>
+ !template.isWorkspaceTemplate &&
+ template.collectionId === document.collectionId
+ )
+ .map(templateToMenuItem);
+
+ const workspaceTemplates = templates
+ .filter((tmpl) => tmpl.isWorkspaceTemplate)
+ .map(templateToMenuItem);
+
+ const workspaceItems: MenuItem[] = React.useMemo(
+ () =>
+ workspaceTemplates.length
+ ? [{ type: "heading", title: t("Workspace") }, ...workspaceTemplates]
+ : [],
+ [t, workspaceTemplates]
+ );
+
+ if (!onSelectTemplate) {
+ return [];
+ }
+
+ return collectionItems
+ ? workspaceItems.length
+ ? [
+ ...collectionItems,
+ { type: "separator" } as MenuItem,
+ ...workspaceItems,
+ ]
+ : collectionItems
+ : workspaceItems;
+}
diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx
index 6e4d135c43..e085ea66e8 100644
--- a/app/menus/DocumentMenu.tsx
+++ b/app/menus/DocumentMenu.tsx
@@ -2,7 +2,13 @@ import capitalize from "lodash/capitalize";
import isEmpty from "lodash/isEmpty";
import noop from "lodash/noop";
import { observer } from "mobx-react";
-import { EditIcon, InputIcon, RestoreIcon, SearchIcon } from "outline-icons";
+import {
+ EditIcon,
+ InputIcon,
+ RestoreIcon,
+ SearchIcon,
+ ShapesIcon,
+} from "outline-icons";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
@@ -57,6 +63,7 @@ import useMobile from "~/hooks/useMobile";
import usePolicy from "~/hooks/usePolicy";
import useRequest from "~/hooks/useRequest";
import useStores from "~/hooks/useStores";
+import { useTemplateMenuItems } from "~/hooks/useTemplateMenuItems";
import { MenuItem, MenuItemButton } from "~/types";
import { documentEditPath } from "~/utils/routeHelpers";
import { MenuContext, useMenuContext } from "./MenuContext";
@@ -76,6 +83,7 @@ type Props = {
label?: (props: MenuButtonHTMLProps) => React.ReactNode;
/** Invoked when the "Find and replace" menu item is clicked */
onFindAndReplace?: () => void;
+ onSelectTemplate?: (template: Document) => void;
/** Invoked when the "Rename" menu item is clicked */
onRename?: () => void;
/** Invoked when menu is opened */
@@ -147,6 +155,7 @@ type MenuContentProps = {
onOpen?: () => void;
onClose?: () => void;
onFindAndReplace?: () => void;
+ onSelectTemplate?: (template: Document) => void;
onRename?: () => void;
showDisplayOptions?: boolean;
showToggleEmbeds?: boolean;
@@ -156,6 +165,7 @@ const MenuContent: React.FC = observer(function MenuContent_({
onOpen,
onClose,
onFindAndReplace,
+ onSelectTemplate,
onRename,
showDisplayOptions,
showToggleEmbeds,
@@ -218,6 +228,11 @@ const MenuContent: React.FC = observer(function MenuContent_({
[collections.orderedData, handleRestore, policies]
);
+ const templateMenuItems = useTemplateMenuItems({
+ document,
+ onSelectTemplate,
+ });
+
return !isEmpty(can) ? (
= observer(function MenuContent_({
actionToMenuItem(archiveDocument, context),
actionToMenuItem(moveDocument, context),
actionToMenuItem(moveTemplate, context),
+ {
+ type: "submenu",
+ title: t("Apply template"),
+ icon: ,
+ items: templateMenuItems,
+ },
actionToMenuItem(pinDocument, context),
actionToMenuItem(createDocumentFromTemplate, context),
{
@@ -383,6 +404,7 @@ function DocumentMenu({
modal = true,
showToggleEmbeds,
showDisplayOptions,
+ onSelectTemplate,
label,
onRename,
onOpen,
@@ -466,6 +488,7 @@ function DocumentMenu({
onOpen={onOpen}
onClose={onClose}
onRename={onRename}
+ onSelectTemplate={onSelectTemplate}
showDisplayOptions={showDisplayOptions}
showToggleEmbeds={showToggleEmbeds}
/>
diff --git a/app/menus/TemplatesMenu.tsx b/app/menus/TemplatesMenu.tsx
index 44e09c5a86..6faba9c175 100644
--- a/app/menus/TemplatesMenu.tsx
+++ b/app/menus/TemplatesMenu.tsx
@@ -1,17 +1,13 @@
import { observer } from "mobx-react";
-import { DocumentIcon, ShapesIcon } from "outline-icons";
+import { ShapesIcon } from "outline-icons";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { MenuButton, useMenuState } from "reakit/Menu";
-import Icon from "@shared/components/Icon";
-import { TextHelper } from "@shared/utils/TextHelper";
import Document from "~/models/Document";
import Button from "~/components/Button";
import ContextMenu from "~/components/ContextMenu";
import Template from "~/components/ContextMenu/Template";
-import useCurrentUser from "~/hooks/useCurrentUser";
-import useStores from "~/hooks/useStores";
-import { MenuItem } from "~/types";
+import { useTemplateMenuItems } from "~/hooks/useTemplateMenuItems";
type Props = {
/** The document to which the templates will be applied */
@@ -23,57 +19,12 @@ type Props = {
};
function TemplatesMenu({ isCompact, onSelectTemplate, document }: Props) {
+ const { t } = useTranslation();
const menu = useMenuState({
modal: true,
});
- const user = useCurrentUser();
- const { documents } = useStores();
- const { t } = useTranslation();
- const templateToMenuItem = React.useCallback(
- (tmpl: Document): MenuItem => ({
- type: "button",
- title: TextHelper.replaceTemplateVariables(tmpl.titleWithDefault, user),
- icon: tmpl.icon ? (
-
- ) : (
-
- ),
- onClick: () => onSelectTemplate(tmpl),
- }),
- [user, onSelectTemplate]
- );
-
- const templates = documents.templates.filter((tmpl) => tmpl.publishedAt);
-
- const collectionItems = templates
- .filter(
- (tmpl) =>
- !tmpl.isWorkspaceTemplate && tmpl.collectionId === document.collectionId
- )
- .map(templateToMenuItem);
-
- const workspaceTemplates = templates
- .filter((tmpl) => tmpl.isWorkspaceTemplate)
- .map(templateToMenuItem);
-
- const workspaceItems: MenuItem[] = React.useMemo(
- () =>
- workspaceTemplates.length
- ? [{ type: "heading", title: t("Workspace") }, ...workspaceTemplates]
- : [],
- [t, workspaceTemplates]
- );
-
- const items = collectionItems
- ? workspaceItems.length
- ? [
- ...collectionItems,
- { type: "separator" } as MenuItem,
- ...workspaceItems,
- ]
- : collectionItems
- : workspaceItems;
+ const items = useTemplateMenuItems({ onSelectTemplate, document });
if (!items.length) {
return null;
diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx
index fd61eca3a0..f3bda2a12e 100644
--- a/app/scenes/Document/components/Document.tsx
+++ b/app/scenes/Document/components/Document.tsx
@@ -4,7 +4,7 @@ import isEqual from "lodash/isEqual";
import { action, observable } from "mobx";
import { observer } from "mobx-react";
import { Node } from "prosemirror-model";
-import { AllSelection } from "prosemirror-state";
+import { AllSelection, TextSelection } from "prosemirror-state";
import * as React from "react";
import { WithTranslation, withTranslation } from "react-i18next";
import {
@@ -146,7 +146,17 @@ class DocumentScene extends React.Component {
}
}
- replaceDocument = (template: Document | Revision) => {
+ /**
+ * Replaces the given selection with a template, if no selection is provided
+ * then the template is inserted at the beginning of the document.
+ *
+ * @param template The template to use
+ * @param selection The selection to replace, if any
+ */
+ replaceSelection = (
+ template: Document | Revision,
+ selection?: TextSelection | AllSelection
+ ) => {
const editorRef = this.editor.current;
if (!editorRef) {
@@ -154,6 +164,7 @@ class DocumentScene extends React.Component {
}
const { view, schema } = editorRef;
+ const sel = selection ?? TextSelection.near(view.state.doc.resolve(0));
const doc = Node.fromJSON(
schema,
ProsemirrorHelper.replaceTemplateVariables(
@@ -163,11 +174,7 @@ class DocumentScene extends React.Component {
);
if (doc) {
- view.dispatch(
- view.state.tr
- .setSelection(new AllSelection(view.state.doc))
- .replaceSelectionWith(doc)
- );
+ view.dispatch(view.state.tr.setSelection(sel).replaceSelectionWith(doc));
}
this.isEditorDirty = true;
@@ -217,7 +224,10 @@ class DocumentScene extends React.Component {
});
if (response) {
- await this.replaceDocument(response.data);
+ await this.replaceSelection(
+ response.data,
+ new AllSelection(editorRef.view.state.doc)
+ );
toast.success(t("Document restored"));
history.replace(this.props.document.url, history.location.state);
}
@@ -518,7 +528,7 @@ class DocumentScene extends React.Component {
}
savingIsDisabled={document.isSaving || this.isEmpty}
sharedTree={this.props.sharedTree}
- onSelectTemplate={this.replaceDocument}
+ onSelectTemplate={this.replaceSelection}
onSave={this.onSave}
/>
diff --git a/app/scenes/Document/components/Header.tsx b/app/scenes/Document/components/Header.tsx
index 554ca2bbfa..fe04c2f9e4 100644
--- a/app/scenes/Document/components/Header.tsx
+++ b/app/scenes/Document/components/Header.tsx
@@ -387,6 +387,7 @@ function DocumentHeader({
neutral
/>
)}
+ onSelectTemplate={onSelectTemplate}
onFindAndReplace={editor?.commands.openFindAndReplace}
showToggleEmbeds={canToggleEmbeds}
showDisplayOptions
diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json
index f0a6679964..1a4a6ed3ea 100644
--- a/shared/i18n/locales/en_US/translation.json
+++ b/shared/i18n/locales/en_US/translation.json
@@ -547,6 +547,7 @@
"Document options": "Document options",
"Choose a collection": "Choose a collection",
"Subscription inherited from collection": "Subscription inherited from collection",
+ "Apply template": "Apply template",
"Enable embeds": "Enable embeds",
"Export options": "Export options",
"Group members": "Group members",