feat: Improved placeholder and upload handling for videos

This commit is contained in:
Arek Nawo
2026-01-19 20:14:48 +01:00
parent 5bda5623f2
commit 0c5c83a17a
6 changed files with 173 additions and 130 deletions
@@ -205,8 +205,12 @@ const CommandGroups: SlashMenuGroupedItemsType = {
if (input.files?.length) { if (input.files?.length) {
const file = input.files[0]; const file = input.files[0];
const pos = editor.view.state.selection.from; const pos = editor.view.state.selection.from;
uploadVideoAction(file, editor.view, pos, pageId); uploadVideoAction(file, editor.view, pos, pageId);
} }
// Reset the input value to allow uploading the same file again if needed
input.value = "";
}; };
input.click(); input.click();
}, },
@@ -20,7 +20,7 @@ export function VideoMenu({ editor }: EditorMenuProps) {
const editorState = useEditorState({ const editorState = useEditorState({
editor, editor,
selector: ctx => { selector: (ctx) => {
if (!ctx.editor) { if (!ctx.editor) {
return null; return null;
} }
@@ -43,7 +43,7 @@ export function VideoMenu({ editor }: EditorMenuProps) {
return false; return false;
} }
return editor.isActive("video"); return editor.isActive("video") && editor.getAttributes("video").src;
}, },
[editor], [editor],
); );
@@ -0,0 +1,15 @@
.videoWrapper {
border-radius: 8px;
@mixin light {
background-color: var(--mantine-color-gray-0);
}
@mixin dark {
background-color: var(--mantine-color-dark-7);
}
}
.video {
display: block;
width: 100%;
border-radius: 8px;
}
@@ -2,11 +2,11 @@ import { NodeViewProps, NodeViewWrapper } from "@tiptap/react";
import { useMemo } from "react"; import { useMemo } from "react";
import { getFileUrl } from "@/lib/config.ts"; import { getFileUrl } from "@/lib/config.ts";
import clsx from "clsx"; import clsx from "clsx";
import classes from "./video-view.module.css";
export default function VideoView(props: NodeViewProps) { export default function VideoView(props: NodeViewProps) {
const { node, selected } = props; const { node, selected } = props;
const { src, width, align } = node.attrs; const { src, width, align, aspectRatio } = node.attrs;
const alignClass = useMemo(() => { const alignClass = useMemo(() => {
if (align === "left") return "alignLeft"; if (align === "left") return "alignLeft";
if (align === "right") return "alignRight"; if (align === "right") return "alignRight";
@@ -16,14 +16,26 @@ export default function VideoView(props: NodeViewProps) {
return ( return (
<NodeViewWrapper data-drag-handle> <NodeViewWrapper data-drag-handle>
<video <div
preload="metadata" className={clsx(
width={width} selected && "ProseMirror-selectednode",
controls classes.videoWrapper,
src={getFileUrl(src)} alignClass,
className={clsx(selected ? "ProseMirror-selectednode" : "", alignClass)} )}
style={{ display: "block" }} style={{
/> aspectRatio: aspectRatio ? aspectRatio : src ? undefined : "16 / 9",
width,
}}
>
{src && (
<video
className={classes.video}
preload="metadata"
controls
src={getFileUrl(src)}
/>
)}
</div>
</NodeViewWrapper> </NodeViewWrapper>
); );
} }
+115 -107
View File
@@ -1,132 +1,140 @@
import { type EditorState, Plugin, PluginKey } from "@tiptap/pm/state"; import { MediaUploadOptions, UploadFn } from "../media-utils";
import { Decoration, DecorationSet } from "@tiptap/pm/view";
import {
insertTrailingNode,
MediaUploadOptions,
UploadFn,
} from "../media-utils";
import { IAttachment } from "../types"; import { IAttachment } from "../types";
import { generateNodeId } from "../utils";
import { Node } from "@tiptap/pm/model";
const uploadKey = new PluginKey("video-upload"); const findVideoNodeByPlaceholderId = (
doc: Node,
placeholderId: string,
): { node: Node; pos: number } | null => {
let result: { node: Node; pos: number } | null = null;
export const VideoUploadPlugin = ({ doc.descendants((node, pos) => {
placeholderClass, if (result) return false;
}: {
placeholderClass: string;
}) =>
new Plugin({
key: uploadKey,
state: {
init() {
return DecorationSet.empty;
},
apply(tr, set) {
set = set.map(tr.mapping, tr.doc);
// See if the transaction adds or removes any placeholders
//@-ts-expect-error - not yet sure what the type I need here
const action = tr.getMeta(this);
if (action?.add) {
const { id, pos, src } = action.add;
const placeholder = document.createElement("div"); if (
placeholder.setAttribute("class", "video-placeholder"); node.type.name === "video" &&
const video = document.createElement("video"); node.attrs.placeholderId === placeholderId
video.setAttribute("class", placeholderClass); ) {
video.src = src; result = { node, pos };
placeholder.appendChild(video); return false;
const deco = Decoration.widget(pos + 1, placeholder, { }
id,
}); return true;
set = set.add(tr.doc, [deco]);
} else if (action?.remove) {
set = set.remove(
set.find(
undefined,
undefined,
(spec) => spec.id == action.remove.id,
),
);
}
return set;
},
},
props: {
decorations(state) {
return this.getState(state);
},
},
}); });
function findPlaceholder(state: EditorState, id: {}) { return result;
const decos = uploadKey.getState(state) as DecorationSet; };
const found = decos.find(undefined, undefined, (spec) => spec.id == id); const getVideoDimensions = (
return found.length ? found[0]?.from : null; url: string,
} ): Promise<
{ width: number; height: number; aspectRatio: number } | undefined
> => {
return new Promise<
{ width: number; height: number; aspectRatio: number } | undefined
>((resolve) => {
const video = document.createElement("video");
export const handleVideoUpload = video.preload = "metadata";
video.onloadedmetadata = () => {
const width = video.videoWidth;
const height = video.videoHeight;
const aspectRatio = height > 0 ? width / height : 1;
resolve({ width, height, aspectRatio });
};
video.onerror = () => {
resolve(undefined);
};
video.src = url;
});
};
const handleVideoUpload =
({ validateFn, onUpload }: MediaUploadOptions): UploadFn => ({ validateFn, onUpload }: MediaUploadOptions): UploadFn =>
async (file, view, pos, pageId) => { async (file, view, pos, pageId) => {
// check if the file is an image // check if the file is valid
const validated = validateFn?.(file); const validated = validateFn?.(file);
// @ts-ignore // @ts-ignore
if (!validated) return; if (!validated) return;
// A fresh object to act as the ID for this upload
const id = {};
// Replace the selection with a placeholder const objectUrl = URL.createObjectURL(file);
const videoDimensions = await getVideoDimensions(objectUrl);
const placeholderId = generateNodeId();
const aspectRatio = videoDimensions.aspectRatio;
const initialPlaceholderNode = view.state.schema.nodes.video?.create({
placeholderId,
aspectRatio,
});
const reader = new FileReader(); let placeholderShown = false;
reader.readAsDataURL(file); let tr = view.state.tr;
reader.onload = () => {
const tr = view.state.tr;
if (!tr.selection.empty) tr.deleteSelection();
tr.setMeta(uploadKey, { if (!initialPlaceholderNode) {
add: { URL.revokeObjectURL(objectUrl);
id, return;
pos, }
src: reader.result,
},
});
insertTrailingNode(tr, pos, view); const { parent } = tr.doc.resolve(pos);
const isEmptyTextBlock = parent.isTextblock && !parent.childCount;
if (isEmptyTextBlock) {
// Replace e.g. empty paragraph with the video
tr.replaceRangeWith(pos - 1, pos + 1, initialPlaceholderNode);
} else {
tr.insert(pos, initialPlaceholderNode);
}
// Only show the placeholder if the upload takes more than 250ms
const displayPlaceholderTimeout = setTimeout(() => {
view.dispatch(tr); view.dispatch(tr);
}; placeholderShown = true;
tr = view.state.tr;
}, 250);
await onUpload(file, pageId).then( try {
(attachment: IAttachment) => { const attachment: IAttachment = await onUpload(file, pageId);
const { schema } = view.state; const { pos: currentPos = null } =
findVideoNodeByPlaceholderId(tr.doc, placeholderId) || {};
const pos = findPlaceholder(view.state, id); // If the placeholder is not found or attachment is missing, abort the process
if (currentPos === null || !attachment) return;
// If the content around the placeholder has been deleted, drop // Update the placeholder node with the actual video data
// the image tr.setNodeMarkup(currentPos, undefined, {
if (pos == null) return; src: `/api/files/${attachment.id}/${attachment.fileName}`,
attachmentId: attachment.id,
title: attachment.fileName,
size: attachment.fileSize,
aspectRatio,
});
} catch (error) {
const { pos: currentPos = null } =
findVideoNodeByPlaceholderId(tr.doc, placeholderId) || {};
// Otherwise, insert it at the placeholder's position, and remove if (currentPos === null) return;
// the placeholder
if (!attachment) return; // Delete the video placeholder on error
tr.delete(
currentPos,
currentPos + (initialPlaceholderNode.nodeSize ?? 2),
);
} finally {
clearTimeout(displayPlaceholderTimeout);
const node = schema.nodes.video?.create({ const dispatchFinal = () => {
src: `/api/files/${attachment.id}/${attachment.fileName}`, view.dispatch(tr);
attachmentId: attachment.id, URL.revokeObjectURL(objectUrl);
title: attachment.fileName, };
size: attachment.fileSize,
});
if (!node) return;
const transaction = view.state.tr // If the placeholder was shown, delay showing the video to avoid flicker
.replaceWith(pos, pos, node) if (placeholderShown) {
.setMeta(uploadKey, { remove: { id } }); setTimeout(() => {
view.dispatch(transaction); dispatchFinal();
}, }, 100);
() => { } else {
// Deletes the image placeholder on error dispatchFinal();
const transaction = view.state.tr }
.delete(pos, pos) }
.setMeta(uploadKey, { remove: { id } });
view.dispatch(transaction);
},
);
}; };
export { handleVideoUpload };
+15 -11
View File
@@ -1,6 +1,5 @@
import { ReactNodeViewRenderer } from "@tiptap/react"; import { ReactNodeViewRenderer } from "@tiptap/react";
import { VideoUploadPlugin } from "./video-upload"; import { Range, Node } from "@tiptap/core";
import { mergeAttributes, Range, Node, nodeInputRule } from "@tiptap/core";
export interface VideoOptions { export interface VideoOptions {
view: any; view: any;
@@ -13,6 +12,8 @@ export interface VideoAttributes {
attachmentId?: string; attachmentId?: string;
size?: number; size?: number;
width?: number; width?: number;
aspectRatio?: number;
placeholderId?: string;
} }
declare module "@tiptap/core" { declare module "@tiptap/core" {
@@ -20,7 +21,7 @@ declare module "@tiptap/core" {
videoBlock: { videoBlock: {
setVideo: (attributes: VideoAttributes) => ReturnType; setVideo: (attributes: VideoAttributes) => ReturnType;
setVideoAt: ( setVideoAt: (
attributes: VideoAttributes & { pos: number | Range } attributes: VideoAttributes & { pos: number | Range },
) => ReturnType; ) => ReturnType;
setVideoAlign: (align: "left" | "center" | "right") => ReturnType; setVideoAlign: (align: "left" | "center" | "right") => ReturnType;
setVideoWidth: (width: number) => ReturnType; setVideoWidth: (width: number) => ReturnType;
@@ -81,6 +82,17 @@ export const TiptapVideo = Node.create<VideoOptions>({
"data-align": attributes.align, "data-align": attributes.align,
}), }),
}, },
aspectRatio: {
default: null,
parseHTML: (element) => element.getAttribute("data-aspect-ratio"),
renderHTML: (attributes: VideoAttributes) => ({
"data-aspect-ratio": attributes.aspectRatio,
}),
},
placeholderId: {
default: null,
rendered: false,
},
}; };
}, },
@@ -131,12 +143,4 @@ export const TiptapVideo = Node.create<VideoOptions>({
return ReactNodeViewRenderer(this.options.view); return ReactNodeViewRenderer(this.options.view);
}, },
addProseMirrorPlugins() {
return [
VideoUploadPlugin({
placeholderClass: "video-upload",
}),
];
},
}); });