|
| 1 | +import React, { forwardRef } from "react"; |
| 2 | +// plane imports |
| 3 | +import { DocumentEditorWithRef, EditorRefApi, IDocumentEditorProps, TFileHandler } from "@plane/editor"; |
| 4 | +import { MakeOptional, TSearchEntityRequestPayload, TSearchResponse } from "@plane/types"; |
| 5 | +import { cn } from "@plane/utils"; |
| 6 | +// components |
| 7 | +import { EditorMentionsRoot } from "@/components/editor"; |
| 8 | +// hooks |
| 9 | +import { useEditorConfig, useEditorMention } from "@/hooks/editor"; |
| 10 | +import { useMember } from "@/hooks/store"; |
| 11 | +// plane web hooks |
| 12 | +import { useEditorFlagging } from "@/plane-web/hooks/use-editor-flagging"; |
| 13 | +import { useIssueEmbed } from "@/plane-web/hooks/use-issue-embed"; |
| 14 | + |
| 15 | +type DocumentEditorWrapperProps = MakeOptional< |
| 16 | + Omit<IDocumentEditorProps, "fileHandler" | "mentionHandler" | "embedHandler" | "user">, |
| 17 | + "disabledExtensions" | "editable" | "flaggedExtensions" |
| 18 | +> & { |
| 19 | + embedHandler?: Partial<IDocumentEditorProps["embedHandler"]>; |
| 20 | + workspaceSlug: string; |
| 21 | + workspaceId: string; |
| 22 | + projectId?: string; |
| 23 | +} & ( |
| 24 | + | { |
| 25 | + editable: false; |
| 26 | + } |
| 27 | + | { |
| 28 | + editable: true; |
| 29 | + searchMentionCallback: (payload: TSearchEntityRequestPayload) => Promise<TSearchResponse>; |
| 30 | + uploadFile: TFileHandler["upload"]; |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | +export const DocumentEditor = forwardRef<EditorRefApi, DocumentEditorWrapperProps>((props, ref) => { |
| 35 | + const { |
| 36 | + containerClassName, |
| 37 | + editable, |
| 38 | + embedHandler, |
| 39 | + workspaceSlug, |
| 40 | + workspaceId, |
| 41 | + projectId, |
| 42 | + disabledExtensions: additionalDisabledExtensions = [], |
| 43 | + ...rest |
| 44 | + } = props; |
| 45 | + // store hooks |
| 46 | + const { getUserDetails } = useMember(); |
| 47 | + // editor flaggings |
| 48 | + const { document: documentEditorExtensions } = useEditorFlagging(workspaceSlug); |
| 49 | + // use editor mention |
| 50 | + const { fetchMentions } = useEditorMention({ |
| 51 | + searchEntity: editable ? async (payload) => await props.searchMentionCallback(payload) : async () => ({}), |
| 52 | + }); |
| 53 | + // editor config |
| 54 | + const { getEditorFileHandlers } = useEditorConfig(); |
| 55 | + // issue-embed |
| 56 | + const { issueEmbedProps } = useIssueEmbed({ |
| 57 | + projectId, |
| 58 | + workspaceSlug, |
| 59 | + }); |
| 60 | + |
| 61 | + return ( |
| 62 | + <DocumentEditorWithRef |
| 63 | + ref={ref} |
| 64 | + disabledExtensions={[...documentEditorExtensions.disabled, ...(additionalDisabledExtensions ?? [])]} |
| 65 | + editable={editable} |
| 66 | + flaggedExtensions={documentEditorExtensions.flagged} |
| 67 | + fileHandler={getEditorFileHandlers({ |
| 68 | + projectId, |
| 69 | + uploadFile: editable ? props.uploadFile : async () => "", |
| 70 | + workspaceId, |
| 71 | + workspaceSlug, |
| 72 | + })} |
| 73 | + mentionHandler={{ |
| 74 | + searchCallback: async (query) => { |
| 75 | + const res = await fetchMentions(query); |
| 76 | + if (!res) throw new Error("Failed in fetching mentions"); |
| 77 | + return res; |
| 78 | + }, |
| 79 | + renderComponent: EditorMentionsRoot, |
| 80 | + getMentionedEntityDetails: (id: string) => ({ display_name: getUserDetails(id)?.display_name ?? "" }), |
| 81 | + }} |
| 82 | + embedHandler={{ |
| 83 | + issue: issueEmbedProps, |
| 84 | + ...embedHandler, |
| 85 | + }} |
| 86 | + {...rest} |
| 87 | + containerClassName={cn("relative pl-3 pb-3", containerClassName)} |
| 88 | + /> |
| 89 | + ); |
| 90 | +}); |
| 91 | + |
| 92 | +DocumentEditor.displayName = "DocumentEditor"; |
0 commit comments