From fe3e71255511517bd1be2007fb4a4f7806605afe Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 8 Feb 2025 16:54:54 -0500 Subject: [PATCH] Add inline creation for collection --- .../Sidebar/components/CollectionLink.tsx | 150 +++++++++++------- .../Sidebar/components/DocumentLink.tsx | 2 +- .../Sidebar/components/EditableTitle.tsx | 20 ++- app/models/Collection.ts | 5 + 4 files changed, 114 insertions(+), 63 deletions(-) diff --git a/app/components/Sidebar/components/CollectionLink.tsx b/app/components/Sidebar/components/CollectionLink.tsx index 41b6217bf3..e6ce104207 100644 --- a/app/components/Sidebar/components/CollectionLink.tsx +++ b/app/components/Sidebar/components/CollectionLink.tsx @@ -4,17 +4,19 @@ import { PlusIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { mergeRefs } from "react-merge-refs"; -import { CollectionValidation } from "@shared/validations"; +import { useHistory } from "react-router-dom"; +import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; +import { CollectionValidation, DocumentValidation } from "@shared/validations"; import Collection from "~/models/Collection"; import Document from "~/models/Document"; import Fade from "~/components/Fade"; import CollectionIcon from "~/components/Icons/CollectionIcon"; import NudeButton from "~/components/NudeButton"; -import { createDocument } from "~/actions/definitions/documents"; -import useActionContext from "~/hooks/useActionContext"; import useBoolean from "~/hooks/useBoolean"; import usePolicy from "~/hooks/usePolicy"; +import useStores from "~/hooks/useStores"; import CollectionMenu from "~/menus/CollectionMenu"; +import { documentEditPath } from "~/utils/routeHelpers"; import { useDropToChangeCollection } from "../hooks/useDragAndDrop"; import DropToImport from "./DropToImport"; import EditableTitle, { RefHandle } from "./EditableTitle"; @@ -42,6 +44,8 @@ const CollectionLink: React.FC = ({ }: Props) => { const [menuOpen, handleMenuOpen, handleMenuClose] = useBoolean(); const [isEditing, setIsEditing] = React.useState(false); + const { documents } = useStores(); + const history = useHistory(); const can = usePolicy(collection); const { t } = useTranslation(); const sidebarContext = useSidebarContext(); @@ -73,70 +77,104 @@ const CollectionLink: React.FC = ({ void collection.fetchDocuments(); }, [collection]); - const context = useActionContext({ - activeCollectionId: collection.id, - sidebarContext, - }); - const handleRename = React.useCallback(() => { editableTitleRef.current?.setIsEditing(true); }, [editableTitleRef]); + const [isAddingNewChild, setIsAddingNewChild, closeAddingNewChild] = + useBoolean(); + return ( - - + <> + + + + } + showActions={menuOpen} + isActiveDrop={isOver && canDrop} + isActive={( + match, + location: Location<{ sidebarContext?: SidebarContextType }> + ) => !!match && location.state?.sidebarContext === sidebarContext} + label={ + + } + exact={false} + depth={depth ? depth : 0} + menu={ + !isEditing && + !isDraggingAnyCollection && ( + + { + ev.preventDefault(); + setIsAddingNewChild(); + handleExpand(); + }} + > + + + + + ) + } + /> + + + {isAddingNewChild && ( } - showActions={menuOpen} - isActiveDrop={isOver && canDrop} - isActive={( - match, - location: Location<{ sidebarContext?: SidebarContextType }> - ) => !!match && location.state?.sidebarContext === sidebarContext} + depth={(depth ?? 0) + 2} + isActive={() => true} label={ { + const newDocument = await documents.create( + { + collectionId: collection.id, + title: input, + data: ProsemirrorHelper.getEmptyDocument(), + }, + { publish: true } + ); + collection?.addDocument(newDocument); + + closeAddingNewChild(); + history.replace(documentEditPath(newDocument)); + }} + maxLength={DocumentValidation.maxTitleLength} /> } - exact={false} - depth={depth ? depth : 0} - menu={ - !isEditing && - !isDraggingAnyCollection && ( - - - - - - - ) - } /> - - + )} + ); }; diff --git a/app/components/Sidebar/components/DocumentLink.tsx b/app/components/Sidebar/components/DocumentLink.tsx index 67de8c7113..6b68aa11d5 100644 --- a/app/components/Sidebar/components/DocumentLink.tsx +++ b/app/components/Sidebar/components/DocumentLink.tsx @@ -326,7 +326,7 @@ function InnerDocumentLink( canUpdate isEditing placeholder={`${t("New doc")}…`} - onEscape={closeAddingNewChild} + onCancel={closeAddingNewChild} onSubmit={async (input) => { const newDocument = await documents.create( { diff --git a/app/components/Sidebar/components/EditableTitle.tsx b/app/components/Sidebar/components/EditableTitle.tsx index b69477e5f9..2eed914256 100644 --- a/app/components/Sidebar/components/EditableTitle.tsx +++ b/app/components/Sidebar/components/EditableTitle.tsx @@ -4,21 +4,29 @@ import styled from "styled-components"; import { s } from "@shared/styles"; type Props = Omit, "onSubmit"> & { + /** A callback when the title is submitted. */ onSubmit: (title: string) => Promise | void; + /** A callback when the editing status changes. */ onEditing?: (isEditing: boolean) => void; - onEscape?: () => void; + /** A callback when editing is canceled. */ + onCancel?: () => void; + /** The default title. */ title: string; + /** Whether the user can update the title. */ canUpdate: boolean; + /** The maximum length of the title. */ maxLength?: number; + /** The default editing state. */ isEditing?: boolean; }; export type RefHandle = { + /** A function to set the editing state. */ setIsEditing: (isEditing: boolean) => void; }; function EditableTitle( - { title, onSubmit, canUpdate, onEditing, onEscape, ...rest }: Props, + { title, onSubmit, canUpdate, onEditing, onCancel, ...rest }: Props, ref: React.RefObject ) { const [isEditing, setIsEditing] = React.useState(rest.isEditing || false); @@ -61,7 +69,7 @@ function EditableTitle( if (trimmedValue === originalValue || trimmedValue.length === 0) { setValue(originalValue); - onEscape?.(); + onCancel?.(); return; } @@ -76,7 +84,7 @@ function EditableTitle( } } }, - [originalValue, value, onSubmit] + [originalValue, value, onCancel, onSubmit] ); const handleKeyDown = React.useCallback( @@ -86,14 +94,14 @@ function EditableTitle( } if (ev.key === "Escape") { setIsEditing(false); - onEscape?.(); + onCancel?.(); setValue(originalValue); } if (ev.key === "Enter") { await handleSave(ev); } }, - [handleSave, originalValue] + [handleSave, onCancel, originalValue] ); React.useEffect(() => { diff --git a/app/models/Collection.ts b/app/models/Collection.ts index 0a8c962ecc..fd8b50c9b4 100644 --- a/app/models/Collection.ts +++ b/app/models/Collection.ts @@ -269,6 +269,11 @@ export default class Collection extends ParanoidModel { return; } + if (!parentDocumentId) { + this.documents.unshift(document.asNavigationNode); + return; + } + const travelNodes = (nodes: NavigationNode[]) => nodes.forEach((node) => { if (node.id === parentDocumentId) {