mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
chore: Move link actions to their own module and inline link checks
This commit is contained in:
23 files changed
+226
-223
No files matched your search
@@ -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: <LinkIcon />,
|
||||
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",
|
||||
|
||||
@@ -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: <InsertAboveIcon />,
|
||||
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: <InsertBelowIcon />,
|
||||
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: <IndentIcon />,
|
||||
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: <LinkIcon />,
|
||||
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: <LinkIcon />,
|
||||
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",
|
||||
|
||||
@@ -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: <LinkIcon />,
|
||||
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: <InsertAboveIcon />,
|
||||
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: <InsertBelowIcon />,
|
||||
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: <IndentIcon />,
|
||||
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: <LinkIcon />,
|
||||
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: <LinkIcon />,
|
||||
keywords: "url bookmark",
|
||||
visible: ({ activeDocumentId, stores }) =>
|
||||
getLinkLocation(stores, activeDocumentId)?.collection.isManualSort ===
|
||||
false && canAddLinkInside(stores, activeDocumentId),
|
||||
perform: addLinkAt("inside"),
|
||||
});
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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({
|
||||
<React.Fragment key={item.key}>
|
||||
{gap > 0 && <div style={{ height: gap }} />}
|
||||
<div data-index={item.index} ref={virtualizer.measureElement}>
|
||||
{isLinkNode(row.node) ? (
|
||||
{row.node.type === NavigationNodeType.Link ? (
|
||||
<ExternalLink
|
||||
node={row.node}
|
||||
collection={collection}
|
||||
|
||||
@@ -2,8 +2,7 @@ import { observer } from "mobx-react";
|
||||
import * as React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import Icon from "@shared/components/Icon";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import { isLinkNode } from "@shared/utils/collections";
|
||||
import { NavigationNodeType, type NavigationNode } from "@shared/types";
|
||||
import type Collection from "~/models/Collection";
|
||||
import type Document from "~/models/Document";
|
||||
import useStores from "~/hooks/useStores";
|
||||
@@ -157,7 +156,7 @@ const ObservedDocumentLink = observer(DocumentLink);
|
||||
* A row in the sidebar of a public share, for a document or a link.
|
||||
*/
|
||||
export function SharedDocumentLink(props: Props) {
|
||||
return isLinkNode(props.node) ? (
|
||||
return props.node.type === NavigationNodeType.Link ? (
|
||||
<ExternalLink node={props.node} depth={props.depth} />
|
||||
) : (
|
||||
<ObservedDocumentLink {...props} />
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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)
|
||||
),
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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}`);
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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<NavigationNode, "type">): boolean =>
|
||||
node.type === NavigationNodeType.Link;
|
||||
|
||||
/**
|
||||
* Inserts a node into a navigation tree.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user