import { difference } from "es-toolkit/compat"; import { observer } from "mobx-react"; import * as React from "react"; import { useMergeRefs } from "react-merge-refs"; import type { Optional } from "utility-types"; import EditorContainer from "@shared/editor/components/Styles"; import { AttachmentPreset } from "@shared/types"; import { ProsemirrorHelper } from "~/models/helpers/ProsemirrorHelper"; import AsyncEditor from "~/components/AsyncEditor"; import ClickablePadding from "~/components/ClickablePadding"; import ErrorBoundary from "~/components/ErrorBoundary"; import PlaceholderDocument from "~/components/PlaceholderDocument"; import type { Props as EditorProps, Editor as SharedEditor } from "~/editor"; import useCurrentUser from "~/hooks/useCurrentUser"; import useEditorClickHandlers from "~/hooks/useEditorClickHandlers"; import useEmbeds from "~/hooks/useEmbeds"; import useStores from "~/hooks/useStores"; import { uploadFile, uploadFileFromUrl } from "~/utils/files"; import useShare from "@shared/hooks/useShare"; export type Props = Optional< EditorProps, "placeholder" | "defaultValue" | "onClickLink" | "embeds" | "extensions" > & { embedsDisabled?: boolean; onSynced?: () => Promise; onPublish?: (event: React.MouseEvent) => void; editorStyle?: React.CSSProperties; }; function Editor({ ref, ...props }: Props & { ref?: React.Ref }) { const { id, onChange, onCreateCommentMark, onDeleteCommentMark } = props; const { comments } = useStores(); const { shareId } = useShare(); const embeds = useEmbeds(!shareId); const localRef = React.useRef(null); const preferences = useCurrentUser({ rejectOnEmpty: false })?.preferences; const previousCommentIds = React.useRef(undefined); const handleUploadFile = React.useCallback( async ( file: File | string, uploadOptions?: { id?: string; onProgress?: (fractionComplete: number) => void; } ) => { const options = { id: uploadOptions?.id, documentId: id, preset: AttachmentPreset.DocumentAttachment, onProgress: uploadOptions?.onProgress, }; const result = file instanceof File ? await uploadFile(file, options) : await uploadFileFromUrl(file, options); return result.url; }, [id] ); const { handleClickLink } = useEditorClickHandlers({ shareId }); const focusAtEnd = React.useCallback(() => { localRef?.current?.focusAtEnd(); }, [localRef]); const handleDrop = React.useCallback( (event: React.DragEvent) => localRef.current?.insertDroppedContent(event), [] ); // see: https://stackoverflow.com/a/50233827/192065 const handleDragOver = React.useCallback( (event: React.DragEvent) => { event.stopPropagation(); event.preventDefault(); }, [] ); const updateComments = React.useCallback( (event?: { remote: boolean }) => { if (onCreateCommentMark && onDeleteCommentMark && localRef.current) { const commentMarks = localRef.current.getComments(); const commentIds = comments.orderedData.map((c) => c.id); const commentMarkIds = commentMarks?.map((c) => c.id); // Only marks created by a local change should steal focus. Marks that // arrive through a remote or sync transaction, or that are discovered // by a rescan outside of a change event – such as the initial load of // a document containing a draft comment – should not. const focus = previousCommentIds.current !== undefined && !!event && !event.remote; const newCommentIds = difference( commentMarkIds, previousCommentIds.current ?? [], commentIds ); newCommentIds.forEach((commentId) => { const mark = commentMarks.find((c) => c.id === commentId); if (mark) { onCreateCommentMark(mark.id, mark.userId, { focus }); } }); const removedCommentIds = difference( previousCommentIds.current ?? [], commentMarkIds ?? [] ); removedCommentIds.forEach((commentId) => { onDeleteCommentMark(commentId); }); previousCommentIds.current = commentMarkIds; } }, [onCreateCommentMark, onDeleteCommentMark, comments.orderedData] ); const handleChange = React.useCallback>( (value, event) => { onChange?.(value, event); updateComments(event); }, [onChange, updateComments] ); const handleRefChanged = React.useCallback( (node: SharedEditor | null) => { if (node) { updateComments(); } }, [updateComments] ); const mergedRef = useMergeRefs([ref, localRef, handleRefChanged]); // Read-only content that needs no node views or plugins is rendered from // the schema alone, avoiding an editor view per instance. const staticHTML = React.useMemo(() => { if (props.readOnly && typeof props.value === "object") { const node = ProsemirrorHelper.toStaticNode(props.value); return node ? ProsemirrorHelper.toHTML(node) : undefined; } return undefined; }, [props.readOnly, props.value]); return ( <> {staticHTML !== undefined ? (
) : ( // The boundary must live between the lazy editor and any ancestor // with effects or refs. If the suspension reached an outer boundary // React 18 would hide mounted ancestors and destroy their effects, // re-running provider setup and ref callbacks in a loop. }> )} {props.editorStyle?.paddingBottom && !props.readOnly && ( )} ); } export default observer(Editor);