diff --git a/app/actions/definitions/collections.tsx b/app/actions/definitions/collections.tsx index 96737b9b52..a99d197bf7 100644 --- a/app/actions/definitions/collections.tsx +++ b/app/actions/definitions/collections.tsx @@ -7,7 +7,6 @@ import { EditIcon, ExportIcon, ImportIcon, - LinkIcon, SortManualIcon, NewDocumentIcon, PadlockIcon, @@ -32,7 +31,6 @@ import ConfirmationDialog from "~/components/ConfirmationDialog"; import { DialogTitle } from "~/components/DialogTitle"; import DynamicCollectionIcon from "~/components/Icons/CollectionIcon"; import { ImportDocumentDialog } from "~/components/ImportDocumentDialog"; -import { openAddLinkDialog } from "~/components/Sidebar/components/LinkDialog"; import { getHeaderExpandedKey } from "~/components/Sidebar/components/Header"; import { createAction, @@ -560,25 +558,6 @@ export const createDocument = createInternalLinkAction({ }, }); -export const addLinkToCollection = createAction({ - name: ({ t }) => `${t("Add link")}…`, - analyticsName: "Add link", - section: ActiveCollectionSection, - icon: , - keywords: "url bookmark", - visible: ({ getActiveModel, getActivePolicies }) => - !!getActiveModel(Collection)?.documents && - getActivePolicies(Collection).some( - (policy) => policy.abilities.createDocument - ), - perform: ({ getActiveModel, stores, t }) => { - const collection = getActiveModel(Collection); - if (collection) { - openAddLinkDialog({ stores, t, collection, index: 0 }); - } - }, -}); - export const createTemplate = createInternalLinkAction({ name: ({ t }) => t("New template"), analyticsName: "New template", diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index a91921a37d..408601af5b 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -42,7 +42,6 @@ import { IndentIcon, InsertAboveIcon, InsertBelowIcon, - LinkIcon, } from "outline-icons"; import { toast } from "sonner"; import { showPublicLinkCopiedToast } from "~/components/Sharing/components/toasts"; @@ -56,9 +55,7 @@ import { } from "@shared/types"; import { isMobile } from "@shared/utils/browser"; import { Week } from "@shared/utils/time"; -import type Collection from "~/models/Collection"; import type UserMembership from "~/models/UserMembership"; -import { openAddLinkDialog } from "~/components/Sidebar/components/LinkDialog"; import Document from "~/models/Document"; import { client } from "~/utils/ApiClient"; import DocumentDelete from "~/scenes/DocumentDelete"; @@ -261,7 +258,7 @@ export const createDraftDocument = createInternalLinkAction({ * @param document - the document to find the index of. * @returns the index of the document among its siblings, or -1 if not found. */ -function findDocumentSiblingIndex( +export function findDocumentSiblingIndex( stores: ActionContext["stores"], document: { id: string; @@ -294,7 +291,7 @@ function findDocumentSiblingIndex( * @param document - the document to create a sibling of. * @returns true if the user can create a sibling. */ -function canCreateSiblingDocument( +export function canCreateSiblingDocument( stores: ActionContext["stores"], document: Document ): boolean { @@ -510,142 +507,6 @@ export const starDocument = createAction({ }, }); -/** - * Returns the active document and its collection when a link can be added - * next to or inside it. Links live in the collection structure, so the - * document must be published in a collection whose structure is loaded. - * - * @param stores - the root stores. - * @param activeDocumentId - the id of the active document. - * @returns the document and collection, or undefined. - */ -function getLinkLocation( - stores: ActionContext["stores"], - activeDocumentId: string | undefined -): { document: Document; collection: Collection } | undefined { - const document = activeDocumentId - ? stores.documents.get(activeDocumentId) - : undefined; - if (!document?.collectionId || document.isDraft || document.isDeleted) { - return undefined; - } - const collection = stores.collections.get(document.collectionId); - return collection?.documents ? { document, collection } : undefined; -} - -const canAddLinkBeside = ( - stores: ActionContext["stores"], - activeDocumentId: string | undefined -) => { - const location = getLinkLocation(stores, activeDocumentId); - return ( - !!location?.collection.isManualSort && - canCreateSiblingDocument(stores, location.document) - ); -}; - -const canAddLinkInside = ( - stores: ActionContext["stores"], - activeDocumentId: string | undefined -) => { - const location = getLinkLocation(stores, activeDocumentId); - return ( - !!location && - stores.policies.abilities(location.document.id).createChildDocument - ); -}; - -/** - * Returns an action handler that opens the dialog to add a link at the given - * position relative to the active document. - * - * @param position - where to add the link. - * @returns the action handler. - */ -const addLinkAt = - (position: "above" | "below" | "inside") => - ({ activeDocumentId, stores, t }: ActionContext) => { - const location = getLinkLocation(stores, activeDocumentId); - if (!location) { - return; - } - const { collection, document } = location; - const index = findDocumentSiblingIndex(stores, document); - - openAddLinkDialog({ - stores, - t, - collection, - ...(position === "inside" - ? { parentDocumentId: document.id } - : { - parentDocumentId: document.parentDocumentId ?? undefined, - index: position === "above" ? Math.max(0, index) : index + 1, - }), - }); - }; - -const addLinkAbove = createAction({ - name: ({ t, isMenu }) => - isMenu ? `${t("Above")}…` : `${t("Add link above")}…`, - analyticsName: "Add link above", - section: ActiveDocumentSection, - icon: , - keywords: "url bookmark", - visible: ({ activeDocumentId, stores }) => - canAddLinkBeside(stores, activeDocumentId), - perform: addLinkAt("above"), -}); - -const addLinkBelow = createAction({ - name: ({ t, isMenu }) => - isMenu ? `${t("Below")}…` : `${t("Add link below")}…`, - analyticsName: "Add link below", - section: ActiveDocumentSection, - icon: , - keywords: "url bookmark", - visible: ({ activeDocumentId, stores }) => - canAddLinkBeside(stores, activeDocumentId), - perform: addLinkAt("below"), -}); - -const addNestedLink = createAction({ - name: ({ t, isMenu }) => - isMenu ? `${t("Inside")}…` : `${t("Add nested link")}…`, - analyticsName: "Add nested link", - section: ActiveDocumentSection, - icon: , - keywords: "url bookmark", - visible: ({ activeDocumentId, stores }) => - canAddLinkInside(stores, activeDocumentId), - perform: addLinkAt("inside"), -}); - -export const addLink = createActionWithChildren({ - name: ({ t }) => t("Add link"), - analyticsName: "Add link", - section: ActiveDocumentSection, - icon: , - keywords: "url bookmark", - visible: ({ activeDocumentId, stores }) => - !!getLinkLocation(stores, activeDocumentId)?.collection.isManualSort && - (canAddLinkBeside(stores, activeDocumentId) || - canAddLinkInside(stores, activeDocumentId)), - children: [addLinkAbove, addLinkBelow, addNestedLink], -}); - -export const addLinkInAlphabeticalCollection = createAction({ - name: ({ t }) => `${t("Add link")}…`, - analyticsName: "Add nested link", - section: ActiveDocumentSection, - icon: , - keywords: "url bookmark", - visible: ({ activeDocumentId, stores }) => - getLinkLocation(stores, activeDocumentId)?.collection.isManualSort === - false && canAddLinkInside(stores, activeDocumentId), - perform: addLinkAt("inside"), -}); - export const unstarDocument = createAction({ name: ({ t }) => t("Unstar"), analyticsName: "Unstar document", diff --git a/app/actions/definitions/links.tsx b/app/actions/definitions/links.tsx new file mode 100644 index 0000000000..a83c2138ec --- /dev/null +++ b/app/actions/definitions/links.tsx @@ -0,0 +1,174 @@ +import { + IndentIcon, + InsertAboveIcon, + InsertBelowIcon, + LinkIcon, +} from "outline-icons"; +import { createAction, createActionWithChildren } from "~/actions"; +import { + ActiveCollectionSection, + ActiveDocumentSection, +} from "~/actions/sections"; +import { openAddLinkDialog } from "~/components/Sidebar/components/LinkDialog"; +import Collection from "~/models/Collection"; +import type Document from "~/models/Document"; +import type { ActionContext } from "~/types"; +import { + canCreateSiblingDocument, + findDocumentSiblingIndex, +} from "./documents"; + +export const addLinkToCollection = createAction({ + name: ({ t }) => `${t("Add link")}…`, + analyticsName: "Add link", + section: ActiveCollectionSection, + icon: , + keywords: "url bookmark", + visible: ({ getActiveModel, getActivePolicies }) => + !!getActiveModel(Collection)?.documents && + getActivePolicies(Collection).some( + (policy) => policy.abilities.createDocument + ), + perform: ({ getActiveModel, stores, t }) => { + const collection = getActiveModel(Collection); + if (collection) { + openAddLinkDialog({ stores, t, collection, index: 0 }); + } + }, +}); + +/** + * Returns the active document and its collection when a link can be added + * next to or inside it. Links live in the collection structure, so the + * document must be published in a collection whose structure is loaded. + * + * @param stores - the root stores. + * @param activeDocumentId - the id of the active document. + * @returns the document and collection, or undefined. + */ +function getLinkLocation( + stores: ActionContext["stores"], + activeDocumentId: string | undefined +): { document: Document; collection: Collection } | undefined { + const document = activeDocumentId + ? stores.documents.get(activeDocumentId) + : undefined; + if (!document?.collectionId || document.isDraft || document.isDeleted) { + return undefined; + } + const collection = stores.collections.get(document.collectionId); + return collection?.documents ? { document, collection } : undefined; +} + +const canAddLinkBeside = ( + stores: ActionContext["stores"], + activeDocumentId: string | undefined +) => { + const location = getLinkLocation(stores, activeDocumentId); + return ( + !!location?.collection.isManualSort && + canCreateSiblingDocument(stores, location.document) + ); +}; + +const canAddLinkInside = ( + stores: ActionContext["stores"], + activeDocumentId: string | undefined +) => { + const location = getLinkLocation(stores, activeDocumentId); + return ( + !!location && + stores.policies.abilities(location.document.id).createChildDocument + ); +}; + +/** + * Returns an action handler that opens the dialog to add a link at the given + * position relative to the active document. + * + * @param position - where to add the link. + * @returns the action handler. + */ +const addLinkAt = + (position: "above" | "below" | "inside") => + ({ activeDocumentId, stores, t }: ActionContext) => { + const location = getLinkLocation(stores, activeDocumentId); + if (!location) { + return; + } + const { collection, document } = location; + const index = findDocumentSiblingIndex(stores, document); + + openAddLinkDialog({ + stores, + t, + collection, + ...(position === "inside" + ? { parentDocumentId: document.id } + : { + parentDocumentId: document.parentDocumentId ?? undefined, + index: position === "above" ? Math.max(0, index) : index + 1, + }), + }); + }; + +const addLinkAbove = createAction({ + name: ({ t, isMenu }) => + isMenu ? `${t("Above")}…` : `${t("Add link above")}…`, + analyticsName: "Add link above", + section: ActiveDocumentSection, + icon: , + keywords: "url bookmark", + visible: ({ activeDocumentId, stores }) => + canAddLinkBeside(stores, activeDocumentId), + perform: addLinkAt("above"), +}); + +const addLinkBelow = createAction({ + name: ({ t, isMenu }) => + isMenu ? `${t("Below")}…` : `${t("Add link below")}…`, + analyticsName: "Add link below", + section: ActiveDocumentSection, + icon: , + keywords: "url bookmark", + visible: ({ activeDocumentId, stores }) => + canAddLinkBeside(stores, activeDocumentId), + perform: addLinkAt("below"), +}); + +const addNestedLink = createAction({ + name: ({ t, isMenu }) => + isMenu ? `${t("Inside")}…` : `${t("Add nested link")}…`, + analyticsName: "Add nested link", + section: ActiveDocumentSection, + icon: , + keywords: "url bookmark", + visible: ({ activeDocumentId, stores }) => + canAddLinkInside(stores, activeDocumentId), + perform: addLinkAt("inside"), +}); + +export const addLink = createActionWithChildren({ + name: ({ t }) => t("Add link"), + analyticsName: "Add link", + section: ActiveDocumentSection, + icon: , + keywords: "url bookmark", + visible: ({ activeDocumentId, stores }) => + !!getLinkLocation(stores, activeDocumentId)?.collection.isManualSort && + (canAddLinkBeside(stores, activeDocumentId) || + canAddLinkInside(stores, activeDocumentId)), + children: [addLinkAbove, addLinkBelow, addNestedLink], +}); + +export const addLinkInAlphabeticalCollection = createAction({ + name: ({ t }) => `${t("Add link")}…`, + analyticsName: "Add nested link", + section: ActiveDocumentSection, + icon: , + keywords: "url bookmark", + visible: ({ activeDocumentId, stores }) => + getLinkLocation(stores, activeDocumentId)?.collection.isManualSort === + false && canAddLinkInside(stores, activeDocumentId), + perform: addLinkAt("inside"), +}); diff --git a/app/components/CommandBar/SharedSearchActions.tsx b/app/components/CommandBar/SharedSearchActions.tsx index bf95420342..87152f2924 100644 --- a/app/components/CommandBar/SharedSearchActions.tsx +++ b/app/components/CommandBar/SharedSearchActions.tsx @@ -5,7 +5,6 @@ import * as React from "react"; import { useTranslation } from "react-i18next"; import useShare from "@shared/hooks/useShare"; import { NavigationNodeType, type NavigationNode } from "@shared/types"; -import { isLinkNode } from "@shared/utils/collections"; import { flattenTree } from "@shared/utils/tree"; import { createAction } from "~/actions"; import { RecentSection, SearchResultsSection } from "~/actions/sections"; @@ -29,7 +28,8 @@ const serverSearchDelay = 350; * document for document shares – only the latter are searchable. */ const isDocumentNode = (node: NavigationNode) => - node.type !== NavigationNodeType.Collection && !isLinkNode(node); + node.type !== NavigationNodeType.Collection && + node.type !== NavigationNodeType.Link; /** * Registers search result actions in the command bar scoped to a public share. diff --git a/app/components/EditableTitle.tsx b/app/components/EditableTitle.tsx index 1497b60555..e4bb41284b 100644 --- a/app/components/EditableTitle.tsx +++ b/app/components/EditableTitle.tsx @@ -116,8 +116,6 @@ function EditableTitle({ setValue(value); setIsEditing(true); - // The error is shown here, callers are event handlers that cannot - // handle it. toast.error(errToString(error)); } finally { setIsSubmitting(false); diff --git a/app/components/Sidebar/components/DocumentTree.tsx b/app/components/Sidebar/components/DocumentTree.tsx index 1edcbe2eaa..2f3bce7ff2 100644 --- a/app/components/Sidebar/components/DocumentTree.tsx +++ b/app/components/Sidebar/components/DocumentTree.tsx @@ -4,8 +4,8 @@ import { noop } from "es-toolkit/compat"; import { observer } from "mobx-react"; import * as React from "react"; import scrollIntoView from "scroll-into-view-if-needed"; -import type { NavigationNode } from "@shared/types"; -import { isLinkNode, sortNavigationNodes } from "@shared/utils/collections"; +import { NavigationNodeType, type NavigationNode } from "@shared/types"; +import { sortNavigationNodes } from "@shared/utils/collections"; import { flattenExpandedTree, type FlattenedTreeNode, @@ -214,7 +214,7 @@ export const DocumentTree = observer(function DocumentTree({ const getNavigationItems = React.useCallback( () => rows - .filter((row) => !isLinkNode(row.node)) + .filter((row) => row.node.type !== NavigationNodeType.Link) .map((row) => ({ id: row.node.id, url: row.node.url, @@ -255,7 +255,7 @@ export const DocumentTree = observer(function DocumentTree({ {gap > 0 &&
}
- {isLinkNode(row.node) ? ( + {row.node.type === NavigationNodeType.Link ? ( ) : ( diff --git a/app/components/Sidebar/components/SidebarNavigation.tsx b/app/components/Sidebar/components/SidebarNavigation.tsx index d9b5eefb85..d7dd85d6ac 100644 --- a/app/components/Sidebar/components/SidebarNavigation.tsx +++ b/app/components/Sidebar/components/SidebarNavigation.tsx @@ -1,7 +1,6 @@ import { observer } from "mobx-react"; import * as React from "react"; -import type { NavigationNode } from "@shared/types"; -import { isLinkNode } from "@shared/utils/collections"; +import { NavigationNodeType, type NavigationNode } from "@shared/types"; import { useActiveSidebarContext } from "~/hooks/useActiveSidebarContext"; import useKeyDown from "~/hooks/useKeyDown"; import useStores from "~/hooks/useStores"; @@ -169,7 +168,7 @@ function lastDocumentDescendant( ): NavigationNode | undefined { for (let i = nodes.length - 1; i >= 0; i--) { const node = nodes[i]; - if (!isLinkNode(node)) { + if (node.type !== NavigationNodeType.Link) { return lastDocumentDescendant(node.children) ?? node; } } diff --git a/app/hooks/useCollectionMenuAction.tsx b/app/hooks/useCollectionMenuAction.tsx index be361c3e27..45a2d8243a 100644 --- a/app/hooks/useCollectionMenuAction.tsx +++ b/app/hooks/useCollectionMenuAction.tsx @@ -15,13 +15,13 @@ import { subscribeCollection, unsubscribeCollection, createDocument, - addLinkToCollection, exportCollection, importDocument, openCollectionInSplit, sortCollection, } from "~/actions/definitions/collections"; import { renameActionFactory } from "~/actions/definitions/common"; +import { addLinkToCollection } from "~/actions/definitions/links"; import { ActiveCollectionSection } from "~/actions/sections"; type Props = { diff --git a/app/hooks/useCollectionTrees.ts b/app/hooks/useCollectionTrees.ts index 2c9d723938..9dc5479e1b 100644 --- a/app/hooks/useCollectionTrees.ts +++ b/app/hooks/useCollectionTrees.ts @@ -40,7 +40,6 @@ function getCollectionTree(collection: Collection): NavigationNode { }; root.children = annotateNodes( - // Links are not documents, so they cannot be a destination or be selected. collection.documentNodes ?? [], collection.sort, root diff --git a/app/hooks/useDocumentMenuAction.tsx b/app/hooks/useDocumentMenuAction.tsx index e943e46c70..eefc1e0928 100644 --- a/app/hooks/useDocumentMenuAction.tsx +++ b/app/hooks/useDocumentMenuAction.tsx @@ -13,8 +13,6 @@ import { shareDocument, createNewDocument, createNewDocumentInAlphabeticalCollection, - addLink, - addLinkInAlphabeticalCollection, importDocument, createTemplateFromDocument, duplicateDocument, @@ -39,6 +37,10 @@ import { toggleDocumentStats, } from "~/actions/definitions/documents"; import { renameActionFactory } from "~/actions/definitions/common"; +import { + addLink, + addLinkInAlphabeticalCollection, +} from "~/actions/definitions/links"; import { ActiveDocumentSection } from "~/actions/sections"; import useMobile from "./useMobile"; import type Template from "~/models/Template"; diff --git a/app/models/Collection.ts b/app/models/Collection.ts index 9d55acd325..3ba5ccb3db 100644 --- a/app/models/Collection.ts +++ b/app/models/Collection.ts @@ -18,7 +18,6 @@ import { import { filterNavigationNodes, insertNavigationNode, - isLinkNode, moveNavigationNode, sortNavigationNodes, updateNavigationNode, @@ -206,7 +205,10 @@ export default class Collection extends ParanoidModel { @computed get documentNodes(): NavigationNode[] | undefined { return this.documents - ? filterNavigationNodes(this.documents, (node) => !isLinkNode(node)) + ? filterNavigationNodes( + this.documents, + (node) => node.type !== NavigationNodeType.Link + ) : undefined; } @@ -534,7 +536,7 @@ export default class Collection extends ParanoidModel { */ getChildDocumentsForDocument(documentId: string) { return this.getChildrenForDocument(documentId).filter( - (node) => !isLinkNode(node) + (node) => node.type !== NavigationNodeType.Link ); } diff --git a/app/models/Document.ts b/app/models/Document.ts index 50cdd647a4..1bf115d44d 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -26,7 +26,6 @@ import { import { DocumentPreferenceDefaults } from "@shared/constants"; import { ProsemirrorDataHelper } from "@shared/utils/ProsemirrorDataHelper"; import Storage from "@shared/utils/Storage"; -import { isLinkNode } from "@shared/utils/collections"; import { isRTL } from "@shared/utils/rtl"; import type DocumentsStore from "~/stores/DocumentsStore"; import User from "~/models/User"; @@ -737,7 +736,7 @@ export default class Document extends ArchivableModel implements Searchable { collection?.getChildrenForDocument(this.id) ?? membership?.getChildrenForDocument(this.id) ?? [] - ).filter((node) => !isLinkNode(node)); + ).filter((node) => node.type !== NavigationNodeType.Link); } /** diff --git a/app/scenes/Document/components/References.tsx b/app/scenes/Document/components/References.tsx index ffe9acd20c..61ffe0294f 100644 --- a/app/scenes/Document/components/References.tsx +++ b/app/scenes/Document/components/References.tsx @@ -16,8 +16,8 @@ import ReferenceListItem, { NewChildReferenceListItem, } from "./ReferenceListItem"; import useShare from "@shared/hooks/useShare"; -import type { NavigationNode } from "@shared/types"; -import { findNavigationNode, isLinkNode } from "@shared/utils/collections"; +import { NavigationNodeType, type NavigationNode } from "@shared/types"; +import { findNavigationNode } from "@shared/utils/collections"; import { flattenTree } from "@shared/utils/tree"; type Props = { @@ -171,7 +171,7 @@ function useChildren( const children = findNavigationNode([sharedTree], document.id)?.node.children ?? []; - return children.filter((node) => !isLinkNode(node)); + return children.filter((node) => node.type !== NavigationNodeType.Link); }, [document.id, document.children, sharedTree]); } diff --git a/server/models/Collection.ts b/server/models/Collection.ts index 55dfb2a231..c014fdc530 100644 --- a/server/models/Collection.ts +++ b/server/models/Collection.ts @@ -55,7 +55,6 @@ import { UrlHelper } from "@shared/utils/UrlHelper"; import { findNavigationNode, insertNavigationNode, - isLinkNode, moveNavigationNode, removeNavigationNode, updateNavigationNode, @@ -1258,7 +1257,7 @@ class Collection extends ParanoidModel< const allDocumentIds: string[] = []; const loopChildren = (node: NavigationNode) => { - if (isLinkNode(node)) { + if (node.type === NavigationNodeType.Link) { return; } allDocumentIds.push(node.id); @@ -1281,7 +1280,7 @@ class Collection extends ParanoidModel< linkId: string ): { node: NavigationNode; parentDocumentId?: string } | undefined => { const result = findNavigationNode(this.documentStructure ?? [], linkId); - return result && isLinkNode(result.node) + return result && result.node.type === NavigationNodeType.Link ? { node: result.node, parentDocumentId: result.parentId } : undefined; }; diff --git a/server/presenters/navigationNode.ts b/server/presenters/navigationNode.ts index 96517fd477..3e37a1c7dd 100644 --- a/server/presenters/navigationNode.ts +++ b/server/presenters/navigationNode.ts @@ -1,7 +1,6 @@ import type { Team } from "@server/models"; import type { NavigationNode } from "@shared/types"; import { NavigationNodeType } from "@shared/types"; -import { isLinkNode } from "@shared/utils/collections"; export interface PresentedNavigationNode { id: string; @@ -30,7 +29,10 @@ export default function presentNavigationNode( url: /^https?:\/\//.test(node.url) ? node.url : new URL(node.url, team.url).href, - type: isLinkNode(node) ? NavigationNodeType.Link : undefined, + type: + node.type === NavigationNodeType.Link + ? NavigationNodeType.Link + : undefined, children: (node.children ?? []).map((child) => presentNavigationNode(team, child) ), diff --git a/server/queues/tasks/ExportDocumentTreeTask.ts b/server/queues/tasks/ExportDocumentTreeTask.ts index 3bb134b09a..a97a962170 100644 --- a/server/queues/tasks/ExportDocumentTreeTask.ts +++ b/server/queues/tasks/ExportDocumentTreeTask.ts @@ -3,8 +3,7 @@ import { escapeRegExp } from "es-toolkit/compat"; import type { ZipFile } from "yazl"; import { errToString } from "@shared/utils/error"; import type { NavigationNode } from "@shared/types"; -import { FileOperationFormat } from "@shared/types"; -import { isLinkNode } from "@shared/utils/collections"; +import { FileOperationFormat, NavigationNodeType } from "@shared/types"; import Logger from "@server/logging/Logger"; import type { Collection } from "@server/models"; import Attachment from "@server/models/Attachment"; @@ -440,7 +439,7 @@ export default abstract class ExportDocumentTreeTask extends ExportTask { format: FileOperationFormat ) { for (const node of nodes) { - if (isLinkNode(node)) { + if (node.type === NavigationNodeType.Link) { continue; } diff --git a/server/queues/tasks/ExportJSONTask.ts b/server/queues/tasks/ExportJSONTask.ts index ba8b0b7662..9189c7c486 100644 --- a/server/queues/tasks/ExportJSONTask.ts +++ b/server/queues/tasks/ExportJSONTask.ts @@ -1,7 +1,6 @@ import type { ZipFile } from "yazl"; import { omit } from "es-toolkit/compat"; -import type { NavigationNode } from "@shared/types"; -import { isLinkNode } from "@shared/utils/collections"; +import { NavigationNodeType, type NavigationNode } from "@shared/types"; import env from "@server/env"; import type { Collection, FileOperation } from "@server/models"; import { Attachment, Document } from "@server/models"; @@ -99,7 +98,7 @@ export default class ExportJSONTask extends ExportTask { const addDocumentTree = async (nodes: NavigationNode[]) => { for (const node of nodes) { - if (isLinkNode(node)) { + if (node.type === NavigationNodeType.Link) { continue; } diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 49c08b48ea..ad40cfcb62 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -11,7 +11,7 @@ import type { Filter } from "@shared/helpers/FilterHelper"; import { Op, Sequelize } from "sequelize"; import { randomUUID } from "node:crypto"; import type { DirectionFilter, SortFilter } from "@shared/types"; -import { type NavigationNode } from "@shared/types"; +import { NavigationNodeType, type NavigationNode } from "@shared/types"; import { DocumentPermission, ExportContentType, @@ -19,7 +19,6 @@ import { StatusFilter, UserRole, } from "@shared/types"; -import { isLinkNode } from "@shared/utils/collections"; import { subtractDate } from "@shared/utils/date"; import slugify from "@server/utils/slugify"; import { Day } from "@shared/utils/time"; @@ -291,7 +290,7 @@ router.post( }); authorize(user, "readDocument", collection); documentIds = (collection.documentStructure || []) - .filter((node) => !isLinkNode(node)) + .filter((node) => node.type !== NavigationNodeType.Link) .slice(offset, offset + limit) .map((node) => node.id); where[Op.and].push({ id: documentIds }); diff --git a/server/utils/sitemap.ts b/server/utils/sitemap.ts index bf6b8b804a..cccb886715 100644 --- a/server/utils/sitemap.ts +++ b/server/utils/sitemap.ts @@ -1,6 +1,5 @@ import { escape } from "es-toolkit/compat"; -import type { NavigationNode } from "@shared/types"; -import { isLinkNode } from "@shared/utils/collections"; +import { NavigationNodeType, type NavigationNode } from "@shared/types"; /** * Converts a navigation tree to a sitemap XML string, by traversing the nodes. @@ -17,7 +16,7 @@ export function navigationNodeToSitemap( function collectUrls(node: NavigationNode, urls: string[]) { // Links point to other pages, which are not part of the share. - if (isLinkNode(node)) { + if (node.type === NavigationNodeType.Link) { return; } urls.push(`${baseUrl}${node.url}`); diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 3381718bc6..673e5a741e 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -40,7 +40,6 @@ "Export": "Export", "Export collection": "Export collection", "New document": "New document", - "Add link": "Add link", "New template": "New template", "Delete comment": "Delete comment", "Mark as resolved": "Mark as resolved", @@ -70,9 +69,6 @@ "New document below": "New document below", "{{ count }} document starred": "{{ count }} document starred", "{{ count }} document starred_plural": "{{ count }} documents starred", - "Add link above": "Add link above", - "Add link below": "Add link below", - "Add nested link": "Add nested link", "{{ count }} document unstarred": "{{ count }} document unstarred", "{{ count }} document unstarred_plural": "{{ count }} documents unstarred", "Publish": "Publish", @@ -182,6 +178,10 @@ "{{userName}} was removed from the group": "{{userName}} was removed from the group", "Disconnect": "Disconnect", "Disconnect analytics": "Disconnect analytics", + "Add link": "Add link", + "Add link above": "Add link above", + "Add link below": "Add link below", + "Add nested link": "Add nested link", "Home": "Home", "Drafts": "Drafts", "Search": "Search", diff --git a/shared/utils/collections.test.ts b/shared/utils/collections.test.ts index ca12ca034b..72fa2b2de4 100644 --- a/shared/utils/collections.test.ts +++ b/shared/utils/collections.test.ts @@ -3,7 +3,6 @@ import { filterNavigationNodes, findNavigationNode, insertNavigationNode, - isLinkNode, moveNavigationNode, removeNavigationNode, updateNavigationNode, @@ -143,13 +142,18 @@ describe("removeNavigationNode", () => { describe("filterNavigationNodes", () => { it("should remove links at every depth and keep unchanged nodes", () => { const tree = [node("a", [node("a1"), link("l2")]), link("l1"), node("b")]; - const result = filterNavigationNodes(tree, (n) => !isLinkNode(n)); + const result = filterNavigationNodes( + tree, + (n) => n.type !== NavigationNodeType.Link + ); expect(ids(result)).toEqual([["a", ["a1"]], "b"]); expect(result[1]).toBe(tree[2]); }); it("should return the same array when nothing is removed", () => { const tree = [node("a", [node("a1")])]; - expect(filterNavigationNodes(tree, (n) => !isLinkNode(n))).toBe(tree); + expect( + filterNavigationNodes(tree, (n) => n.type !== NavigationNodeType.Link) + ).toBe(tree); }); }); diff --git a/shared/utils/collections.ts b/shared/utils/collections.ts index 78d631ee16..b1928b301b 100644 --- a/shared/utils/collections.ts +++ b/shared/utils/collections.ts @@ -1,4 +1,4 @@ -import { NavigationNodeType, type NavigationNode } from "../types"; +import type { NavigationNode } from "../types"; import shallowEqual from "./shallowEqual"; import naturalSort from "./naturalSort"; @@ -7,16 +7,6 @@ type Sort = { direction: "asc" | "desc"; }; -/** - * Returns whether the node is a link to an external location, rather than a - * document. - * - * @param node The navigation node to check. - * @returns true if the node is a link. - */ -export const isLinkNode = (node: Pick): boolean => - node.type === NavigationNodeType.Link; - /** * Inserts a node into a navigation tree. *