import { NodeSelection } from "prosemirror-state"; import * as React from "react"; import styled, { css } from "styled-components"; import { sanitizeUrl } from "../../utils/urls"; import { getDisplayUrl } from "../lib/uploadPlaceholder"; import { EditorStyleHelper } from "../styles/EditorStyleHelper"; import type { ComponentProps } from "../types"; import { ResizeHandles, ResizeLeft, ResizeRight } from "./ResizeHandle"; import useDragResize from "./hooks/useDragResize"; type Props = ComponentProps & { /** Callback triggered when the video is resized */ onChangeSize?: (props: { width: number; height?: number }) => void; children?: React.ReactElement; }; export default function Video(props: Props) { const { isSelected, node, isEditable, children, onChangeSize, view, getPos } = props; const [naturalWidth] = React.useState(node.attrs.width); const [naturalHeight] = React.useState(node.attrs.height); const ref = React.useRef(null); const videoRef = React.useRef(null); const isResizable = !!onChangeSize; // ProseMirror makes a selected node draggable on mousedown, which stops // Safari from releasing the native video controls on mouseup. React.useEffect(() => { const video = videoRef.current; if (!video) { return; } const handleMouseDown = (event: MouseEvent) => { const { selection } = view.state; if (selection instanceof NodeSelection && selection.from === getPos()) { event.stopPropagation(); } }; video.addEventListener("mousedown", handleMouseDown); return () => video.removeEventListener("mousedown", handleMouseDown); }, [view, getPos]); const { width, height, handlePointerDown, handleDoubleClick, dragging } = useDragResize({ width: node.attrs.width ?? naturalWidth, height: node.attrs.height ?? naturalHeight, naturalWidth, naturalHeight, onChangeSize, ref, }); const style: React.CSSProperties = { width: width || "auto", maxHeight: height || "auto", pointerEvents: dragging ? "none" : "all", }; return (
{isEditable && isResizable && ( )} {children}
); } export const videoStyle = css` max-width: 100%; height: auto; background: ${(props) => props.theme.background}; color: ${(props) => props.theme.text} !important; margin: -2px; padding: 2px; border-radius: ${EditorStyleHelper.mediaRadius}; box-shadow: 0 0 0 1px ${(props) => props.theme.divider}; `; const StyledVideo = styled.video` ${videoStyle} `; const VideoWrapper = styled.div<{ $dragging: boolean }>` line-height: 0; position: relative; margin-left: auto; margin-right: auto; white-space: nowrap; cursor: default; border-radius: ${EditorStyleHelper.mediaRadius}; user-select: none; max-width: 100%; overflow: hidden; transition-property: width, max-height; transition-duration: ${(props) => (props.$dragging ? "0ms" : "150ms")}; transition-timing-function: ease-in-out; video { transition-property: width, max-height; transition-duration: ${(props) => (props.$dragging ? "0ms" : "150ms")}; transition-timing-function: ease-in-out; } &.ProseMirror-selectednode, &.ProseMirror-selectednode video { border-radius: ${EditorStyleHelper.mediaSelectedRadius}; } &:hover { ${ResizeLeft}, ${ResizeRight} { opacity: 1; } } `;