mirror of
https://github.com/outline/outline.git
synced 2026-11-25 07:04:37 +03:00
* 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>
149 lines
4.2 KiB
TypeScript
149 lines
4.2 KiB
TypeScript
import { DocumentIcon, ShapesIcon } from "outline-icons";
|
|
import { cloneDeep } from "es-toolkit/compat";
|
|
import { observer } from "mobx-react";
|
|
import { useCallback, useMemo } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import Icon from "@shared/components/Icon";
|
|
import type { MenuItem } from "@shared/editor/types";
|
|
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
|
|
import { TextHelper } from "@shared/utils/TextHelper";
|
|
import useCurrentUser from "~/hooks/useCurrentUser";
|
|
import useStores from "~/hooks/useStores";
|
|
import getMenuItems from "../menus/block";
|
|
import { useEditor } from "./EditorContext";
|
|
import type { Props as SuggestionsMenuProps } from "./SuggestionsMenu";
|
|
import SuggestionsMenu from "./SuggestionsMenu";
|
|
import SuggestionsMenuItem from "./SuggestionsMenuItem";
|
|
|
|
/**
|
|
* Hook that returns a template menu item with children for inserting template
|
|
* content into the editor, or undefined if no templates are available.
|
|
*/
|
|
function useTemplateMenuItem(): MenuItem | undefined {
|
|
const { t } = useTranslation();
|
|
const user = useCurrentUser({ rejectOnEmpty: false });
|
|
const { documents, templates: templatesStore } = useStores();
|
|
const editor = useEditor();
|
|
const documentId = editor.props.id;
|
|
const document = documentId ? documents.get(documentId) : undefined;
|
|
const collectionId = document?.collectionId;
|
|
|
|
return useMemo(() => {
|
|
if (!user) {
|
|
return undefined;
|
|
}
|
|
|
|
const allTemplates = templatesStore.published.filter(
|
|
(template) => !!template.data
|
|
);
|
|
const hasTemplates = allTemplates.some(
|
|
(template) =>
|
|
template.isWorkspaceTemplate || template.collectionId === collectionId
|
|
);
|
|
|
|
if (!hasTemplates) {
|
|
return undefined;
|
|
}
|
|
|
|
const toMenuItem = (template: (typeof allTemplates)[0]): MenuItem => ({
|
|
name: "noop",
|
|
title: TextHelper.replaceTemplateVariables(
|
|
template.titleWithDefault,
|
|
user
|
|
),
|
|
icon: template.icon ? (
|
|
<Icon
|
|
value={template.icon}
|
|
initial={template.initial}
|
|
color={template.color ?? undefined}
|
|
/>
|
|
) : (
|
|
<DocumentIcon />
|
|
),
|
|
keywords: template.titleWithDefault,
|
|
onClick: () => {
|
|
const data = cloneDeep(template.data);
|
|
ProsemirrorHelper.replaceTemplateVariables(data, user);
|
|
editor.insertContent(data);
|
|
},
|
|
});
|
|
|
|
const children = (): MenuItem[] => {
|
|
const collectionTemplates = allTemplates.filter(
|
|
(template) =>
|
|
!template.isWorkspaceTemplate &&
|
|
template.collectionId === collectionId
|
|
);
|
|
const workspaceTemplates = allTemplates.filter(
|
|
(tmpl) => tmpl.isWorkspaceTemplate
|
|
);
|
|
|
|
const items: MenuItem[] = collectionTemplates.map(toMenuItem);
|
|
|
|
if (collectionTemplates.length && workspaceTemplates.length) {
|
|
items.push({ name: "separator" });
|
|
}
|
|
|
|
if (workspaceTemplates.length) {
|
|
for (const template of workspaceTemplates) {
|
|
items.push(toMenuItem(template));
|
|
}
|
|
}
|
|
|
|
return items;
|
|
};
|
|
|
|
return {
|
|
name: "noop",
|
|
title: t("Templates"),
|
|
icon: <ShapesIcon />,
|
|
keywords: "template",
|
|
children,
|
|
} satisfies MenuItem;
|
|
}, [user, templatesStore.published, collectionId, editor, t]);
|
|
}
|
|
|
|
type Props = Omit<SuggestionsMenuProps, "renderMenuItem" | "items"> &
|
|
Required<Pick<SuggestionsMenuProps, "embeds">>;
|
|
|
|
function BlockMenu(props: Props) {
|
|
const { t } = useTranslation();
|
|
const { elementRef } = useEditor();
|
|
const templateMenuItem = useTemplateMenuItem();
|
|
|
|
const items = useMemo(() => {
|
|
const baseItems = getMenuItems(t, elementRef);
|
|
|
|
if (!templateMenuItem) {
|
|
return baseItems;
|
|
}
|
|
|
|
return [...baseItems, { name: "separator" } as MenuItem, templateMenuItem];
|
|
}, [t, elementRef, templateMenuItem]);
|
|
|
|
const renderMenuItem = useCallback(
|
|
(item, _index, options) => (
|
|
<SuggestionsMenuItem
|
|
{...options}
|
|
icon={item.icon}
|
|
title={item.title}
|
|
shortcut={item.shortcut}
|
|
disclosure={options.disclosure}
|
|
/>
|
|
),
|
|
[]
|
|
);
|
|
|
|
return (
|
|
<SuggestionsMenu
|
|
{...props}
|
|
filterable
|
|
trigger="/"
|
|
renderMenuItem={renderMenuItem}
|
|
items={items}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export default observer(BlockMenu);
|