Files
outline/shared/editor/components/Video.tsx
T
Tom Moor ee3c88bc14 fix: Standardize media resizing, add aspect ratio lock control (#14677)
* fix: Allow PDF embed width and height to change independently

Adds an aspect ratio lock toggle to the media dimension toolbar for PDF
embeds, renders PDFs at their stored aspect ratio, and keeps that ratio
when drag resizing.

* feat: Add corner resize handles to PDF embeds and persist aspect ratio lock

Stores the lock as an optional attachment attribute, where unset means
locked, so side handles change only the width of unlocked PDFs.

* feat: Consistent resize handles for images, videos, PDFs and embeds

All media get side, bottom and corner handles driven by one lock rule.
Images and videos stay locked. PDFs and embeds can be unlocked to resize
each axis independently.

* feat: Show width, height and lock in the embed toolbar

* feat: Start embeds unlocked and step media dimensions with arrow keys

Embeds start with an unlocked aspect ratio so full-width embeds can still
be made taller, while PDFs start locked. Up and down arrows step the
dimension inputs by 1, or 10 with shift.

* fix: Align the aspect ratio padlock with the dimension digits

* fix: Move the aspect ratio lock closer to the dimension inputs

* fix: Show the aspect ratio lock state in its tooltip
2026-11-21 11:33:02 -05:00

144 lines
4.2 KiB
TypeScript

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<HTMLDivElement>(null);
const videoRef = React.useRef<HTMLVideoElement>(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 (
<div contentEditable={false} ref={ref}>
<VideoWrapper
className={
isSelected
? `${EditorStyleHelper.videoWrapper} ProseMirror-selectednode`
: EditorStyleHelper.videoWrapper
}
$dragging={!!dragging}
style={style}
>
<StyledVideo
ref={videoRef}
src={sanitizeUrl(getDisplayUrl(props.view.state, node.attrs.src))}
title={node.attrs.title}
// Reserves the aspect ratio of the video before its metadata loads.
width={width || undefined}
height={height || undefined}
style={style}
controls={!dragging}
/>
{isEditable && isResizable && (
<ResizeHandles
onPointerDown={handlePointerDown}
onDoubleClick={handleDoubleClick}
isVisible={!!dragging}
isInset
/>
)}
</VideoWrapper>
{children}
</div>
);
}
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;
}
}
`;