From 08efb34c29efe24f0096e6090e65201a17d2e207 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 9 Jun 2024 22:39:36 -0400 Subject: [PATCH] poc --- app/editor/components/MentionMenu.tsx | 49 ++++++++++++++++++--------- server/routes/api/urls/urls.ts | 46 +++++++++++++------------ server/validation.ts | 9 +++-- shared/editor/components/Styles.ts | 6 +++- shared/editor/nodes/Mention.ts | 9 +++-- shared/types.ts | 1 + 6 files changed, 78 insertions(+), 42 deletions(-) diff --git a/app/editor/components/MentionMenu.tsx b/app/editor/components/MentionMenu.tsx index dc90d77fd1..20082b7b16 100644 --- a/app/editor/components/MentionMenu.tsx +++ b/app/editor/components/MentionMenu.tsx @@ -7,6 +7,7 @@ import { MenuItem } from "@shared/editor/types"; import { MentionType } from "@shared/types"; import parseDocumentSlug from "@shared/utils/parseDocumentSlug"; import User from "~/models/User"; +import Model from "~/models/base/Model"; import Avatar from "~/components/Avatar"; import { AvatarSize } from "~/components/Avatar/Avatar"; import Flex from "~/components/Flex"; @@ -19,7 +20,7 @@ import SuggestionsMenu, { interface MentionItem extends MenuItem { name: string; - user: User; + model: Model; appendSpace: boolean; attrs: { id: string; @@ -39,7 +40,7 @@ function MentionMenu({ search, isActive, ...rest }: Props) { const [loaded, setLoaded] = React.useState(false); const [items, setItems] = React.useState([]); const { t } = useTranslation(); - const { auth, users } = useStores(); + const { auth, documents, users } = useStores(); const location = useLocation(); const documentId = parseDocumentSlug(location.pathname); const { data, loading, request } = useRequest( @@ -60,19 +61,35 @@ function MentionMenu({ search, isActive, ...rest }: Props) { React.useEffect(() => { if (data && !loading) { - const items = data.map((user) => ({ - name: "mention", - user, - title: user.name, - appendSpace: true, - attrs: { - id: v4(), - type: MentionType.User, - modelId: user.id, - actorId: auth.currentUserId ?? undefined, - label: user.name, - }, - })); + const items = data + .map((user) => ({ + name: "mention", + model: user, + title: user.name, + appendSpace: true, + attrs: { + id: v4(), + type: MentionType.User, + modelId: user.id, + actorId: auth.currentUserId ?? undefined, + label: user.name, + }, + })) + .concat( + documents.orderedData.map((doc) => ({ + name: "mention", + model: doc, + title: doc.title, + appendSpace: true, + attrs: { + id: v4(), + type: MentionType.Document, + modelId: doc.id, + actorId: auth.currentUserId ?? undefined, + label: doc.title, + }, + })) + ); setItems(items); setLoaded(true); @@ -105,7 +122,7 @@ function MentionMenu({ search, isActive, ...rest }: Props) { style={{ width: 24, height: 24 }} > div { diff --git a/shared/editor/nodes/Mention.ts b/shared/editor/nodes/Mention.ts index 761fdabb53..3a9359377a 100644 --- a/shared/editor/nodes/Mention.ts +++ b/shared/editor/nodes/Mention.ts @@ -7,6 +7,7 @@ import { } from "prosemirror-model"; import { Command, TextSelection } from "prosemirror-state"; import { Primitive } from "utility-types"; +import { MentionType } from "@shared/types"; import Extension from "../lib/Extension"; import { MarkdownSerializerState } from "../lib/markdown/serializer"; import mentionRule from "../rules/mention"; @@ -50,10 +51,14 @@ export default class Mention extends Extension { }, ], toDOM: (node) => [ - "span", + "a", { - class: `${node.type.name} use-hover-preview`, + class: `${node.type.name} mention-${node.attrs.type} use-hover-preview`, id: node.attrs.id, + href: + node.attrs.type === MentionType.User + ? undefined + : `/doc/${node.attrs.modelId}`, "data-type": node.attrs.type, "data-id": node.attrs.modelId, "data-actorId": node.attrs.actorId, diff --git a/shared/types.ts b/shared/types.ts index 949043d6b4..28394e6f8c 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -47,6 +47,7 @@ export enum FileOperationState { export enum MentionType { User = "user", + Document = "document", } export type PublicEnv = {