From c98d6aa33a26da9fe4fa5ea4f47471ab9f586add Mon Sep 17 00:00:00 2001 From: infinite-persistence <64950861+infinite-persistence@users.noreply.github.com> Date: Fri, 29 Nov 2024 23:33:38 +0800 Subject: [PATCH] Allow user to select doc-copy destination (#8030) * DocumentExplorer: make style extensible * Allow user to select doc-copy dest The change in `documentDuplicator` essentially alters the fallback from "parent" to "top of collection". But there is only 1 place that uses it so far, so I think it's fine to support this PR. In the next commit, the caller side will restore the default to "parent". * Auto select parent as initial target (to retain existing behavior) Otherwise, user would need to always search/expand the tree. I have a feeling that people might want the last selection to be persistent, but ignoring that for now. The 50ms timeout feels dirty, but 0 was too fast, at least on my machine. I couldn't find anything in react-window for a "ready" flag. * Rename: DuplicateDialog -> DocumentCopy This begins the switch to DocumentCopy's look in the next few commits * Revert DocumentExplorer style override No longer needed since we won't be using it under a ConfirmationDialog anymore in the next commit. * Switch to DocumentMove's style * initialSelectionId -> defaultValue --------- Co-authored-by: Tom Moor --- app/actions/definitions/documents.tsx | 4 +- app/components/DocumentCopy.tsx | 149 +++++++++++++++++++++ app/components/DocumentExplorer.tsx | 32 ++++- app/components/DuplicateDialog.tsx | 97 -------------- app/models/Document.ts | 2 + app/scenes/DocumentMove.tsx | 6 +- server/commands/documentDuplicator.ts | 2 +- shared/i18n/locales/en_US/translation.json | 8 +- 8 files changed, 189 insertions(+), 111 deletions(-) create mode 100644 app/components/DocumentCopy.tsx delete mode 100644 app/components/DuplicateDialog.tsx diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index 4716e712c8..8954337a58 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -45,7 +45,7 @@ import DocumentPermanentDelete from "~/scenes/DocumentPermanentDelete"; import DocumentPublish from "~/scenes/DocumentPublish"; import DeleteDocumentsInTrash from "~/scenes/Trash/components/DeleteDocumentsInTrash"; import ConfirmationDialog from "~/components/ConfirmationDialog"; -import DuplicateDialog from "~/components/DuplicateDialog"; +import DocumentCopy from "~/components/DocumentCopy"; import Icon from "~/components/Icon"; import MarkdownIcon from "~/components/Icons/MarkdownIcon"; import SharePopover from "~/components/Sharing/Document"; @@ -562,7 +562,7 @@ export const duplicateDocument = createAction({ stores.dialogs.openModal({ title: t("Copy document"), content: ( - { stores.dialogs.closeAllModals(); diff --git a/app/components/DocumentCopy.tsx b/app/components/DocumentCopy.tsx new file mode 100644 index 0000000000..ba90f940b7 --- /dev/null +++ b/app/components/DocumentCopy.tsx @@ -0,0 +1,149 @@ +import flatten from "lodash/flatten"; +import { observer } from "mobx-react"; +import * as React from "react"; +import { Trans, useTranslation } from "react-i18next"; +import { toast } from "sonner"; +import styled from "styled-components"; +import { NavigationNode } from "@shared/types"; +import Document from "~/models/Document"; +import { FlexContainer, Footer, StyledText } from "~/scenes/DocumentMove"; +import Button from "~/components/Button"; +import DocumentExplorer from "~/components/DocumentExplorer"; +import useCollectionTrees from "~/hooks/useCollectionTrees"; +import useStores from "~/hooks/useStores"; +import { flattenTree } from "~/utils/tree"; +import Switch from "./Switch"; +import Text from "./Text"; + +type Props = { + /** The original document to duplicate */ + document: Document; + onSubmit: (documents: Document[]) => void; +}; + +function DocumentCopy({ document, onSubmit }: Props) { + const { t } = useTranslation(); + const { policies } = useStores(); + const collectionTrees = useCollectionTrees(); + const [publish, setPublish] = React.useState(!!document.publishedAt); + const [recursive, setRecursive] = React.useState(true); + const [selectedPath, selectPath] = React.useState( + null + ); + + const items = React.useMemo(() => { + const nodes = flatten(collectionTrees.map(flattenTree)).filter((node) => + node.collectionId + ? policies.get(node.collectionId)?.abilities.createDocument + : true + ); + + if (document.isTemplate) { + return nodes + .filter((node) => node.type === "collection") + .map((node) => ({ ...node, children: [] })); + } + return nodes; + }, [policies, collectionTrees, document.isTemplate]); + + const handlePublishChange = React.useCallback( + (ev: React.ChangeEvent) => { + setPublish(ev.target.checked); + }, + [] + ); + + const handleRecursiveChange = React.useCallback( + (ev: React.ChangeEvent) => { + setRecursive(ev.target.checked); + }, + [] + ); + + const copy = async () => { + if (!selectedPath) { + toast.message(t("Select a location to copy")); + return; + } + + try { + const result = await document.duplicate({ + publish, + recursive, + title: document.title, + collectionId: selectedPath.collectionId, + ...(selectedPath.type === "document" + ? { parentDocumentId: selectedPath.id } + : {}), + }); + + toast.success(t("Document copied")); + onSubmit(result); + } catch (err) { + toast.error(t("Couldn’t copy the document, try again?")); + } + }; + + return ( + + + + {!document.isTemplate && ( + <> + {document.collectionId && ( + + + + )} + {document.publishedAt && document.childDocuments.length > 0 && ( + + + + )} + + )} + +
+ + {selectedPath ? ( + }} + /> + ) : ( + t("Select a location to copy") + )} + + +
+
+ ); +} + +const OptionsContainer = styled.div` + margin: 16px 0 8px 0; + padding-left: 24px; + padding-right: 24px; +`; + +export default observer(DocumentCopy); diff --git a/app/components/DocumentExplorer.tsx b/app/components/DocumentExplorer.tsx index a09161d6df..323eaef947 100644 --- a/app/components/DocumentExplorer.tsx +++ b/app/components/DocumentExplorer.tsx @@ -31,15 +31,15 @@ import { ancestors, descendants } from "~/utils/tree"; type Props = { /** Action taken upon submission of selected item, could be publish, move etc. */ onSubmit: () => void; - /** A side-effect of item selection */ onSelect: (item: NavigationNode | null) => void; - /** Items to be shown in explorer */ items: NavigationNode[]; + /** Automatically expand to and select item with the given id */ + defaultValue?: string; }; -function DocumentExplorer({ onSubmit, onSelect, items }: Props) { +function DocumentExplorer({ onSubmit, onSelect, items, defaultValue }: Props) { const isMobile = useMobile(); const { collections, documents } = useStores(); const { t } = useTranslation(); @@ -47,12 +47,25 @@ function DocumentExplorer({ onSubmit, onSelect, items }: Props) { const [searchTerm, setSearchTerm] = React.useState(); const [selectedNode, selectNode] = React.useState( - null + () => { + const node = + defaultValue && items.find((item) => item.id === defaultValue); + return node || null; + } ); const [initialScrollOffset, setInitialScrollOffset] = React.useState(0); const [activeNode, setActiveNode] = React.useState(0); - const [expandedNodes, setExpandedNodes] = React.useState([]); + const [expandedNodes, setExpandedNodes] = React.useState(() => { + if (defaultValue) { + const node = items.find((item) => item.id === defaultValue); + if (node) { + return ancestors(node).map((node) => node.id); + } + } + return []; + }); + const [itemRefs, setItemRefs] = React.useState< React.RefObject[] >([]); @@ -94,6 +107,15 @@ function DocumentExplorer({ onSubmit, onSelect, items }: Props) { onSelect(selectedNode); }, [selectedNode, onSelect]); + React.useEffect(() => { + if (defaultValue && selectedNode && listRef) { + const index = nodes.findIndex((node) => node.id === selectedNode.id); + if (index > 0) { + setTimeout(() => listRef.current?.scrollToItem(index, "center"), 50); + } + } + }, []); + function getNodes() { function includeDescendants(item: NavigationNode): NavigationNode[] { return expandedNodes.includes(item.id) diff --git a/app/components/DuplicateDialog.tsx b/app/components/DuplicateDialog.tsx deleted file mode 100644 index e84191533c..0000000000 --- a/app/components/DuplicateDialog.tsx +++ /dev/null @@ -1,97 +0,0 @@ -import { observer } from "mobx-react"; -import * as React from "react"; -import { useTranslation } from "react-i18next"; -import { DocumentValidation } from "@shared/validations"; -import Document from "~/models/Document"; -import ConfirmationDialog from "~/components/ConfirmationDialog"; -import Input from "./Input"; -import Switch from "./Switch"; -import Text from "./Text"; - -type Props = { - /** The original document to duplicate */ - document: Document; - onSubmit: (documents: Document[]) => void; -}; - -function DuplicateDialog({ document, onSubmit }: Props) { - const { t } = useTranslation(); - const defaultTitle = t(`Copy of {{ documentName }}`, { - documentName: document.title, - }); - const [publish, setPublish] = React.useState(!!document.publishedAt); - const [recursive, setRecursive] = React.useState(true); - const [title, setTitle] = React.useState(defaultTitle); - - const handlePublishChange = React.useCallback( - (ev: React.ChangeEvent) => { - setPublish(ev.target.checked); - }, - [] - ); - - const handleRecursiveChange = React.useCallback( - (ev: React.ChangeEvent) => { - setRecursive(ev.target.checked); - }, - [] - ); - - const handleTitleChange = React.useCallback( - (ev: React.ChangeEvent) => { - setTitle(ev.target.value); - }, - [] - ); - - const handleSubmit = async () => { - const result = await document.duplicate({ - publish, - recursive, - title, - }); - onSubmit(result); - }; - - return ( - - - {!document.isTemplate && ( - <> - {document.collectionId && ( - - - - )} - {document.publishedAt && document.childDocuments.length > 0 && ( - - - - )} - - )} - - ); -} - -export default observer(DuplicateDialog); diff --git a/app/models/Document.ts b/app/models/Document.ts index ebeb697ced..6c0a9736a7 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -573,6 +573,8 @@ export default class Document extends ArchivableModel { title?: string; publish?: boolean; recursive?: boolean; + collectionId?: string | null; + parentDocumentId?: string; }) => this.store.duplicate(this, options); /** diff --git a/app/scenes/DocumentMove.tsx b/app/scenes/DocumentMove.tsx index 5d7993d2fa..73cec45b11 100644 --- a/app/scenes/DocumentMove.tsx +++ b/app/scenes/DocumentMove.tsx @@ -120,21 +120,21 @@ function DocumentMove({ document }: Props) { ); } -const FlexContainer = styled(Flex)` +export const FlexContainer = styled(Flex)` margin-left: -24px; margin-right: -24px; margin-bottom: -24px; outline: none; `; -const Footer = styled(Flex)` +export const Footer = styled(Flex)` height: 64px; border-top: 1px solid ${(props) => props.theme.horizontalRule}; padding-left: 24px; padding-right: 24px; `; -const StyledText = styled(Text)` +export const StyledText = styled(Text)` ${ellipsis()} margin-bottom: 0; `; diff --git a/server/commands/documentDuplicator.ts b/server/commands/documentDuplicator.ts index 301f375e39..803c9d7b75 100644 --- a/server/commands/documentDuplicator.ts +++ b/server/commands/documentDuplicator.ts @@ -43,7 +43,7 @@ export default async function documentDuplicator({ }; const duplicated = await documentCreator({ - parentDocumentId: parentDocumentId ?? document.parentDocumentId, + parentDocumentId, icon: document.icon, color: document.color, template: document.template, diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 9745451f83..13b4f33d1b 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -196,6 +196,11 @@ "Install now": "Install now", "Deleted Collection": "Deleted Collection", "Unpin": "Unpin", + "Select a location to copy": "Select a location to copy", + "Document copied": "Document copied", + "Couldn’t copy the document, try again?": "Couldn’t copy the document, try again?", + "Include nested documents": "Include nested documents", + "Copy to {{ location }}": "Copy to {{ location }}", "Search collections & documents": "Search collections & documents", "No results found": "No results found", "Untitled": "Untitled", @@ -227,9 +232,6 @@ "Currently editing": "Currently editing", "Currently viewing": "Currently viewing", "Viewed {{ timeAgo }}": "Viewed {{ timeAgo }}", - "Copy of {{ documentName }}": "Copy of {{ documentName }}", - "Title": "Title", - "Include nested documents": "Include nested documents", "Module failed to load": "Module failed to load", "Loading Failed": "Loading Failed", "Sorry, part of the application failed to load. This may be because it was updated since you opened the tab or because of a failed network request. Please try reloading.": "Sorry, part of the application failed to load. This may be because it was updated since you opened the tab or because of a failed network request. Please try reloading.",