From c717e8e3ebce027ee3e78a2f773e834cdee5b57e Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 3 Feb 2025 19:58:35 -0500 Subject: [PATCH] Move collection description into dedicated tab (#8326) * Move collection description into dedicated tab * Dynamic default * refactor --- app/components/CollectionDescription.tsx | 216 ++------------ app/editor/components/MentionMenu.tsx | 3 + app/editor/extensions/index.ts | 30 ++ app/routes/authenticated.tsx | 3 +- app/scenes/Collection/index.tsx | 314 ++++++++++++--------- app/scenes/Document/components/Editor.tsx | 26 +- shared/editor/nodes/index.ts | 3 +- shared/i18n/locales/en_US/translation.json | 5 +- 8 files changed, 256 insertions(+), 344 deletions(-) create mode 100644 app/editor/extensions/index.ts diff --git a/app/components/CollectionDescription.tsx b/app/components/CollectionDescription.tsx index 0a45babb92..c3897dca0c 100644 --- a/app/components/CollectionDescription.tsx +++ b/app/components/CollectionDescription.tsx @@ -1,6 +1,5 @@ import debounce from "lodash/debounce"; import { observer } from "mobx-react"; -import { transparentize } from "polished"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; @@ -8,23 +7,14 @@ import styled from "styled-components"; import { richExtensions } from "@shared/editor/nodes"; import { s } from "@shared/styles"; import Collection from "~/models/Collection"; -import Arrow from "~/components/Arrow"; -import ButtonLink from "~/components/ButtonLink"; import Editor from "~/components/Editor"; import LoadingIndicator from "~/components/LoadingIndicator"; -import NudeButton from "~/components/NudeButton"; -import BlockMenuExtension from "~/editor/extensions/BlockMenu"; -import EmojiMenuExtension from "~/editor/extensions/EmojiMenu"; -import HoverPreviewsExtension from "~/editor/extensions/HoverPreviews"; +import { withUIExtensions } from "~/editor/extensions"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; +import Text from "./Text"; -const extensions = [ - ...richExtensions, - BlockMenuExtension, - EmojiMenuExtension, - HoverPreviewsExtension, -]; +const extensions = withUIExtensions(richExtensions); type Props = { collection: Collection; @@ -33,33 +23,8 @@ type Props = { function CollectionDescription({ collection }: Props) { const { collections } = useStores(); const { t } = useTranslation(); - const [isExpanded, setExpanded] = React.useState(false); - const [isEditing, setEditing] = React.useState(false); - const [isDirty, setDirty] = React.useState(false); const can = usePolicy(collection); - const handleStartEditing = React.useCallback(() => { - setEditing(true); - }, []); - - const handleStopEditing = React.useCallback(() => { - setEditing(false); - }, []); - - const handleClickDisclosure = React.useCallback( - (event) => { - event.preventDefault(); - - if (isExpanded && document.activeElement) { - // @ts-expect-error ts-migrate(2339) FIXME: Property 'blur' does not exist on type 'Element'. - document.activeElement.blur(); - } - - setExpanded(!isExpanded); - }, - [isExpanded] - ); - const handleSave = React.useMemo( () => debounce(async (getValue) => { @@ -67,7 +32,6 @@ function CollectionDescription({ collection }: Props) { await collection.save({ data: getValue(false), }); - setDirty(false); } catch (err) { toast.error(t("Sorry, an error occurred saving the collection")); throw err; @@ -76,162 +40,44 @@ function CollectionDescription({ collection }: Props) { [collection, t] ); - const handleChange = React.useCallback( - async (getValue) => { - setDirty(true); - await handleSave(getValue); - }, - [handleSave] + const childRef = React.useRef(null); + const childOffsetHeight = childRef.current?.offsetHeight || 0; + const editorStyle = React.useMemo( + () => ({ + padding: "0 32px", + margin: "0 -32px", + paddingBottom: `calc(50vh - ${childOffsetHeight}px)`, + }), + [childOffsetHeight] ); - React.useEffect(() => { - setEditing(false); - }, [collection.id]); - const placeholder = `${t("Add a description")}…`; - const key = isEditing || isDirty ? "draft" : collection.updatedAt; - return ( - - - - {collections.isSaving && } - {collection.hasDescription || isEditing || isDirty ? ( - { - // - }} - > - Loading… - - } - > - - - ) : ( - can.update && ( - { - // - }} - > - {placeholder} - - ) - )} - - - {!isEditing && ( - - - + <> + {collections.isSaving && } + {(collection.hasDescription || can.update) && ( + Loading…}> + +
+ )} - + ); } -const Disclosure = styled(NudeButton)` - opacity: 0; - color: ${s("divider")}; - position: absolute; - top: calc(25vh - 50px); - left: 50%; - z-index: 1; - transform: rotate(-90deg) translateX(-50%); - transition: opacity 100ms ease-in-out; - - &:focus, - &:hover { - opacity: 1; - } - - &:active { - color: ${s("sidebarText")}; - } -`; - -const Placeholder = styled(ButtonLink)` +const Placeholder = styled(Text)` color: ${s("placeholder")}; cursor: text; min-height: 27px; `; -const MaxHeight = styled.div` - position: relative; - max-height: 25vh; - overflow: hidden; - margin: 8px -8px -8px; - padding: 8px; - - &[data-editing="true"], - &[data-expanded="true"] { - max-height: initial; - overflow: initial; - - ${Disclosure} { - top: initial; - bottom: 0; - transform: rotate(90deg) translateX(-50%); - } - } - - &:hover ${Disclosure} { - opacity: 1; - } -`; - -const Input = styled.div` - margin: -8px; - padding: 8px; - border-radius: 8px; - - &:after { - content: ""; - position: absolute; - top: calc(25vh - 50px); - left: 0; - right: 0; - height: 50px; - pointer-events: none; - background: linear-gradient( - 180deg, - ${(props) => transparentize(1, props.theme.background)} 0%, - ${s("background")} 100% - ); - } - - &[data-editing="true"], - &[data-expanded="true"] { - &:after { - background: transparent; - } - } - - &[data-editing="true"] { - background: ${s("backgroundSecondary")}; - } - - .block-menu-trigger, - .heading-anchor { - display: none !important; - } -`; - export default observer(CollectionDescription); diff --git a/app/editor/components/MentionMenu.tsx b/app/editor/components/MentionMenu.tsx index dcb572900c..10df7df1a0 100644 --- a/app/editor/components/MentionMenu.tsx +++ b/app/editor/components/MentionMenu.tsx @@ -157,6 +157,9 @@ function MentionMenu({ search, isActive, ...rest }: Props) { if (item.attrs.type === MentionType.Document) { return; } + if (!documentId) { + return; + } // Check if the mentioned user has access to the document const res = await client.post("/documents.users", { id: documentId, diff --git a/app/editor/extensions/index.ts b/app/editor/extensions/index.ts new file mode 100644 index 0000000000..4ab69e40b5 --- /dev/null +++ b/app/editor/extensions/index.ts @@ -0,0 +1,30 @@ +import Extension from "@shared/editor/lib/Extension"; +import Mark from "@shared/editor/marks/Mark"; +import Node from "@shared/editor/nodes/Node"; +import BlockMenuExtension from "~/editor/extensions/BlockMenu"; +import ClipboardTextSerializer from "~/editor/extensions/ClipboardTextSerializer"; +import EmojiMenuExtension from "~/editor/extensions/EmojiMenu"; +import FindAndReplaceExtension from "~/editor/extensions/FindAndReplace"; +import HoverPreviewsExtension from "~/editor/extensions/HoverPreviews"; +import Keys from "~/editor/extensions/Keys"; +import MentionMenuExtension from "~/editor/extensions/MentionMenu"; +import PasteHandler from "~/editor/extensions/PasteHandler"; +import PreventTab from "~/editor/extensions/PreventTab"; +import SmartText from "~/editor/extensions/SmartText"; + +type Nodes = (typeof Node | typeof Mark | typeof Extension)[]; + +export const withUIExtensions = (nodes: Nodes) => [ + ...nodes, + SmartText, + PasteHandler, + ClipboardTextSerializer, + BlockMenuExtension, + EmojiMenuExtension, + MentionMenuExtension, + FindAndReplaceExtension, + HoverPreviewsExtension, + // Order these default key handlers last + PreventTab, + Keys, +]; diff --git a/app/routes/authenticated.tsx b/app/routes/authenticated.tsx index 7e52170da3..fe43440433 100644 --- a/app/routes/authenticated.tsx +++ b/app/routes/authenticated.tsx @@ -72,8 +72,7 @@ function AuthenticatedRoutes() { - - + import("~/components/IconPicker")); -function CollectionScene() { +enum CollectionPath { + Overview = "overview", + Recent = "recent", + Updated = "updated", + Published = "published", + Old = "old", + Alphabetical = "alphabetical", +} + +const CollectionScene = observer(function _CollectionScene() { const params = useParams<{ id?: string }>(); const history = useHistory(); const match = useRouteMatch(); @@ -68,11 +77,19 @@ function CollectionScene() { collections.getByUrl(id) || collections.get(id); const can = usePolicy(collection); const { pins, count } = usePinnedDocuments(id, collection?.id); + const [collectionTab, setCollectionTab] = usePersistedState( + `collection-tab:${collection?.id}`, + collection?.hasDescription + ? CollectionPath.Overview + : CollectionPath.Recent, + { + listen: false, + } + ); const handleIconChange = React.useCallback( - async (icon: string | null, color: string | null) => { - await collection?.save({ icon, color }); - }, + (icon: string | null, color: string | null) => + collection?.save({ icon, color }), [collection] ); @@ -122,6 +139,8 @@ function CollectionScene() { return ; } + const hasOverview = can.update || collection?.hasDescription; + const fallbackIcon = collection ? ( ) : null; + const tabProps = (path: CollectionPath) => ({ + exact: true, + onClick: () => setCollectionTab(path), + to: { + pathname: collectionPath(collection!.path, path), + state: { sidebarContext }, + }, + }); + return collection ? ( - - {!collection.isArchived && ( - - + {hasOverview && ( + + {t("Overview")} + + )} + {t("Documents")} + {!collection.isArchived && ( + <> + + {t("Recently updated")} + + + {t("Recently published")} + + + {t("Least recently updated")} + + + {t("A–Z")} + + + )} + + + + - {t("Documents")} - - - {t("Recently updated")} - - - {t("Recently published")} - - - {t("Least recently updated")} - - - {t("A–Z")} - - - )} - {collection.isEmpty ? ( - - ) : !collection.isArchived ? ( - - - + + + {hasOverview ? ( + + ) : ( + + )} + + {collection.isEmpty ? ( + + ) : !collection.isArchived ? ( + <> + - - - + + + + + + - - - - - - + + + - - - + + + - - - - - - ) : ( - - + exact + > + + + + ) : ( + {t("Documents")}} options={{ collectionId: collection.id, parentDocumentId: null, @@ -337,8 +383,8 @@ function CollectionScene() { showParentDocuments /> - - )} + )} + @@ -351,7 +397,15 @@ function CollectionScene() { ); -} +}); + +const KeyedCollection = () => { + const params = useParams<{ id?: string }>(); + + // Forced mount prevents animation of pinned documents when navigating + // _between_ collections, speeds up perceived performance. + return ; +}; const Documents = styled.div` position: relative; @@ -369,4 +423,4 @@ const CollectionHeading = styled(Heading)` `} `; -export default observer(CollectionScene); +export default KeyedCollection; diff --git a/app/scenes/Document/components/Editor.tsx b/app/scenes/Document/components/Editor.tsx index 7b112a2fb9..fe9674ec08 100644 --- a/app/scenes/Document/components/Editor.tsx +++ b/app/scenes/Document/components/Editor.tsx @@ -13,16 +13,7 @@ import { RefHandle } from "~/components/ContentEditable"; import { useDocumentContext } from "~/components/DocumentContext"; import Editor, { Props as EditorProps } from "~/components/Editor"; import Flex from "~/components/Flex"; -import BlockMenuExtension from "~/editor/extensions/BlockMenu"; -import ClipboardTextSerializer from "~/editor/extensions/ClipboardTextSerializer"; -import EmojiMenuExtension from "~/editor/extensions/EmojiMenu"; -import FindAndReplaceExtension from "~/editor/extensions/FindAndReplace"; -import HoverPreviewsExtension from "~/editor/extensions/HoverPreviews"; -import Keys from "~/editor/extensions/Keys"; -import MentionMenuExtension from "~/editor/extensions/MentionMenu"; -import PasteHandler from "~/editor/extensions/PasteHandler"; -import PreventTab from "~/editor/extensions/PreventTab"; -import SmartText from "~/editor/extensions/SmartText"; +import { withUIExtensions } from "~/editor/extensions"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import useCurrentUser from "~/hooks/useCurrentUser"; import useFocusedComment from "~/hooks/useFocusedComment"; @@ -40,20 +31,7 @@ import MultiplayerEditor from "./AsyncMultiplayerEditor"; import DocumentMeta from "./DocumentMeta"; import DocumentTitle from "./DocumentTitle"; -const extensions = [ - ...withComments(richExtensions), - SmartText, - PasteHandler, - ClipboardTextSerializer, - BlockMenuExtension, - EmojiMenuExtension, - MentionMenuExtension, - FindAndReplaceExtension, - HoverPreviewsExtension, - // Order these default key handlers last - PreventTab, - Keys, -]; +const extensions = withUIExtensions(withComments(richExtensions)); type Props = Omit & { onChangeTitle: (title: string) => void; diff --git a/shared/editor/nodes/index.ts b/shared/editor/nodes/index.ts index 7cafdd453d..a5ccf561e7 100644 --- a/shared/editor/nodes/index.ts +++ b/shared/editor/nodes/index.ts @@ -107,6 +107,7 @@ export const richExtensions: Nodes = [ TemplatePlaceholder, Math, MathBlock, + Mention, // Container type nodes should be last so that key handlers are registered for content inside // the container nodes first. ...listExtensions, @@ -116,4 +117,4 @@ export const richExtensions: Nodes = [ /** * Add commenting and mentions to a set of nodes */ -export const withComments = (nodes: Nodes) => [...nodes, Mention, Comment]; +export const withComments = (nodes: Nodes) => [...nodes, Comment]; diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 475905538e..907b6155a7 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -167,8 +167,6 @@ "Also, {{collectionName}} is being used as the start view – deleting it will reset the start view to the Home page.": "Also, {{collectionName}} is being used as the start view – deleting it will reset the start view to the Home page.", "Sorry, an error occurred saving the collection": "Sorry, an error occurred saving the collection", "Add a description": "Add a description", - "Collapse": "Collapse", - "Expand": "Expand", "Type a command or search": "Type a command or search", "Choose a template": "Choose a template", "Are you sure you want to permanently delete this entire comment thread?": "Are you sure you want to permanently delete this entire comment thread?", @@ -371,6 +369,8 @@ "You can't reorder documents in an alphabetically sorted collection": "You can't reorder documents in an alphabetically sorted collection", "Empty": "Empty", "Collections": "Collections", + "Collapse": "Collapse", + "Expand": "Expand", "Document not supported – try Markdown, Plain text, HTML, or Word": "Document not supported – try Markdown, Plain text, HTML, or Word", "Go back": "Go back", "Go forward": "Go forward", @@ -594,6 +594,7 @@ "{{ groupsCount }} groups with access_plural": "{{ groupsCount }} groups with access", "Archived by {{userName}}": "Archived by {{userName}}", "Share": "Share", + "Overview": "Overview", "Recently updated": "Recently updated", "Recently published": "Recently published", "Least recently updated": "Least recently updated",