Files
Tom Moor 128c240405 Render uploads with the same component as the uploaded node (#14117)
* feat: Render uploading images and videos with their node component

* fix: Insert uploaded block nodes next to the paragraph instead of inside it

* fix: Insert dropped block nodes on the nearest side of the paragraph

* feat: Fade in uploaded images and videos from their placeholder

* feat: Preview the new image while it replaces an existing one

* fix: Keep the placeholder size and preload pasted images after upload

* fix: Move the selection off an image while an upload replaces it

* feat: Play uploaded videos from the local file in the uploading editor

* feat: Display uploaded images from the local file in the uploading editor

* feat: Soften image and video corners, round them fully when selected

* feat: Render uploading attachments with the attachment component

* feat: Show the file size of an attachment while it uploads

* feat: Show upload progress on the placeholder instead of a toast

* fix: Fade the content of an uploading file but not its progress bar

* fix: Keep the local file of an upload when its placeholder is mapped away

* chore: Simplify upload placeholder state and remove unused progress callback

* Remove test

* fix: Address review feedback on upload placeholders

Expose upload progress to assistive technology outside the inert placeholder, free local files when the editor state is replaced, reuse the fadeIn animation and move the video wrapper class to EditorStyleHelper.

* chore: Move image and PDF wrapper class names to EditorStyleHelper

* feat: Scale in an upload placeholder when it appears

* chore: Make the upload placeholder scale-in more subtle
2026-10-17 12:12:25 -04:00

205 lines
7.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { difference } from "es-toolkit/compat";
import { observer } from "mobx-react";
import * as React from "react";
import { mergeRefs } 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<void>;
onPublish?: (event: React.MouseEvent) => void;
editorStyle?: React.CSSProperties;
};
function Editor({ ref, ...props }: Props & { ref?: React.Ref<SharedEditor> }) {
const { id, onChange, onCreateCommentMark, onDeleteCommentMark } = props;
const { comments } = useStores();
const { shareId } = useShare();
const embeds = useEmbeds(!shareId);
const localRef = React.useRef<SharedEditor | null>(null);
const preferences = useCurrentUser({ rejectOnEmpty: false })?.preferences;
const previousCommentIds = React.useRef<string[] | undefined>(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<HTMLDivElement>) =>
localRef.current?.insertDroppedContent(event),
[]
);
// see: https://stackoverflow.com/a/50233827/192065
const handleDragOver = React.useCallback(
(event: React.DragEvent<HTMLDivElement>) => {
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<NonNullable<EditorProps["onChange"]>>(
(value, event) => {
onChange?.(value, event);
updateComments(event);
},
[onChange, updateComments]
);
const handleRefChanged = React.useCallback(
(node: SharedEditor | null) => {
if (node) {
updateComments();
}
},
[updateComments]
);
// 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 (
<ErrorBoundary component="div" reloadOnChunkMissing>
<>
{staticHTML !== undefined ? (
<EditorContainer
$rtl={props.dir === "rtl"}
grow={props.grow}
style={props.style}
className={props.className}
editorStyle={props.editorStyle}
commenting={!!props.onClickCommentMark}
lang={props.lang}
readOnly
>
<div
className="ProseMirror"
dangerouslySetInnerHTML={{ __html: staticHTML }}
/>
</EditorContainer>
) : (
// 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.
<React.Suspense fallback={<PlaceholderDocument delay={500} />}>
<AsyncEditor
key={props.extensions?.length || 0}
ref={mergeRefs([ref, localRef, handleRefChanged])}
uploadFile={handleUploadFile}
embeds={embeds}
userPreferences={preferences}
{...props}
onClickLink={handleClickLink}
onChange={handleChange}
placeholder={props.placeholder || ""}
defaultValue={props.defaultValue || ""}
/>
</React.Suspense>
)}
{props.editorStyle?.paddingBottom && !props.readOnly && (
<ClickablePadding
onClick={props.readOnly ? undefined : focusAtEnd}
onDrop={props.readOnly ? undefined : handleDrop}
onDragOver={props.readOnly ? undefined : handleDragOver}
minHeight={props.editorStyle.paddingBottom}
/>
)}
</>
</ErrorBoundary>
);
}
export default observer(Editor);