From fd1c10ec7bd9b6dee8f72576f5ca3d8d3191e414 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 20 Nov 2026 15:52:54 -0500 Subject: [PATCH] feat: Show width, height and lock in the embed toolbar --- app/editor/components/MediaDimension.tsx | 36 +++++++++++++++------ app/editor/components/MediaLinkEditor.tsx | 31 ++++++++++-------- shared/editor/components/Embed.tsx | 22 ++++--------- shared/editor/nodes/Embed.tsx | 39 ++++++++++++++++++++++- 4 files changed, 87 insertions(+), 41 deletions(-) diff --git a/app/editor/components/MediaDimension.tsx b/app/editor/components/MediaDimension.tsx index a2d63407ec..1824a97bba 100644 --- a/app/editor/components/MediaDimension.tsx +++ b/app/editor/components/MediaDimension.tsx @@ -4,6 +4,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import styled from "styled-components"; import Flex from "@shared/components/Flex"; import Text from "@shared/components/Text"; +import { embedDefaultHeight } from "@shared/editor/components/Embed"; import { contentWidth } from "@shared/editor/lib/contentWidth"; import { pdfNaturalHeight, @@ -46,20 +47,26 @@ export function MediaDimension({ tooltip }: Props) { const nodeType = node.type.name; const nodeWidth = node.attrs.width as number | null | undefined; const nodeHeight = node.attrs.height as number | null | undefined; + const [maxWidth] = useState(() => contentWidth(view.dom)); + + // Embeds without a stored size fill the editor width at their default height. const { width, height } = nodeType === "attachment" ? resolvePDFDimensions(nodeWidth, nodeHeight) - : { width: nodeWidth ?? 0, height: nodeHeight ?? 0 }; + : nodeType === "embed" + ? { + width: nodeWidth || maxWidth, + height: nodeHeight ?? embedDefaultHeight, + } + : { width: nodeWidth ?? 0, height: nodeHeight ?? 0 }; - // Only PDFs can be unlocked, other media always render at their natural aspect ratio. - const canUnlock = nodeType === "attachment"; + // Only PDFs and embeds can be unlocked, images and videos keep their natural aspect ratio. + const canUnlock = nodeType === "attachment" || nodeType === "embed"; const isLocked = !canUnlock || node.attrs.lockAspectRatio !== false; - const [maxWidth] = useState(() => contentWidth(view.dom)); // Full-width images store their natural width, so the editor width gives their - // aspect ratio. PDFs always store dimensions with a matching ratio. - const ratioWidth = - nodeType === "attachment" ? width : Math.min(width, maxWidth); + // aspect ratio. PDFs and embeds always store dimensions with a matching ratio. + const ratioWidth = canUnlock ? width : Math.min(width, maxWidth); const [localDimension, setLocalDimension] = useState(() => ({ width: width ? String(width) : "", @@ -72,7 +79,7 @@ export function MediaDimension({ tooltip }: Props) { }); const bounds = useMemo(() => { - // Unlocked height does not change the width, so limit it with the default page ratio. + // Unlocked height does not change the width, so limit it to a full-width page. const aspectRatio = isLocked ? height / ratioWidth : pdfNaturalHeight / pdfNaturalWidth; @@ -191,6 +198,11 @@ export function MediaDimension({ tooltip }: Props) { width: finalWidth, height: finalHeight, }); + } else if (nodeType === "embed") { + commands["resizeEmbed"]({ + width: finalWidth, + height: finalHeight, + }); } }, [ commands, @@ -207,9 +219,13 @@ export function MediaDimension({ tooltip }: Props) { ]); const handleToggleLock = useCallback(() => { - commands["toggleAttachmentAspectRatioLock"](); + commands[ + nodeType === "embed" + ? "toggleEmbedAspectRatioLock" + : "toggleAttachmentAspectRatioLock" + ](); setError({ width: false, height: false }); - }, [commands]); + }, [commands, nodeType]); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { diff --git a/app/editor/components/MediaLinkEditor.tsx b/app/editor/components/MediaLinkEditor.tsx index 57bd5df2a2..1908eab9e2 100644 --- a/app/editor/components/MediaLinkEditor.tsx +++ b/app/editor/components/MediaLinkEditor.tsx @@ -7,9 +7,10 @@ import { useTranslation } from "react-i18next"; import styled from "styled-components"; import Flex from "~/components/Flex"; import Tooltip from "~/components/Tooltip"; -import { toggleAspectRatioLock } from "@shared/editor/commands/toggleAspectRatioLock"; +import * as Toolbar from "@radix-ui/react-toolbar"; import Input from "~/editor/components/Input"; -import { AspectRatioLockButton } from "./AspectRatioLockButton"; +import { MediaDimension } from "./MediaDimension"; +import ToolbarSeparator from "./ToolbarSeparator"; import ToolbarButton from "./ToolbarButton"; import useOnClickOutside from "~/hooks/useOnClickOutside"; @@ -54,12 +55,6 @@ export function MediaLinkEditor({ view.focus(); }, [view]); - const handleToggleLock = useCallback(() => { - if (node) { - toggleAspectRatioLock(node.type)(view.state, view.dispatch); - } - }, [node, view]); - const openLink = useCallback(() => { window.open(url, "_blank"); }, [url]); @@ -124,12 +119,15 @@ export function MediaLinkEditor({ readOnly={!view.editable} /> {view.editable && node.type.name === "embed" && ( - - - + <> + + + + + + + + )} @@ -155,6 +153,11 @@ export function MediaLinkEditor({ ); } +const DimensionGroup = styled.div` + display: flex; + align-items: center; +`; + const Wrapper = styled(Flex)` pointer-events: all; gap: 6px; diff --git a/shared/editor/components/Embed.tsx b/shared/editor/components/Embed.tsx index 297e153939..b2cac9a22c 100644 --- a/shared/editor/components/Embed.tsx +++ b/shared/editor/components/Embed.tsx @@ -5,10 +5,12 @@ import { getMatchingEmbed } from "../lib/embeds"; import type { ComponentProps } from "../types"; import DisabledEmbed from "./DisabledEmbed"; import Frame from "./Frame"; -import { contentWidth } from "../lib/contentWidth"; import { ResizeHandles, ResizeLeft, ResizeRight } from "./ResizeHandle"; import useDragResize from "./hooks/useDragResize"; +/** The height of an embed that has not been resized. */ +export const embedDefaultHeight = 400; + type Props = ComponentProps & { embeds: EmbedDescriptor[]; embedsDisabled?: boolean; @@ -20,21 +22,9 @@ const Embed = (props: Props) => { const ref = React.useRef(null); const { node, isEditable, embedsDisabled, onChangeSize } = props; const naturalWidth = 0; - const naturalHeight = 400; + const naturalHeight = embedDefaultHeight; const isResizable = !!onChangeSize && !embedsDisabled; - // A width at the editor edge is stored as unset, so the embed stays full width. - const handleChangeSize = React.useCallback( - (size: { width: number; height?: number }) => { - const max = ref.current ? contentWidth(ref.current) : Infinity; - onChangeSize?.({ - ...size, - width: size.width >= max ? naturalWidth : size.width, - }); - }, - [onChangeSize] - ); - const { width, height, handlePointerDown, handleDoubleClick, dragging } = useDragResize({ width: node.attrs.width ?? naturalWidth, @@ -42,13 +32,13 @@ const Embed = (props: Props) => { naturalWidth, naturalHeight, isLocked: node.attrs.lockAspectRatio !== false, - onChangeSize: onChangeSize ? handleChangeSize : undefined, + onChangeSize, ref, }); const style: React.CSSProperties = { width: "100%", - height: height || 400, + height: height || embedDefaultHeight, pointerEvents: dragging ? "none" : "all", }; diff --git a/shared/editor/nodes/Embed.tsx b/shared/editor/nodes/Embed.tsx index ee03e75289..475d7cd0cc 100644 --- a/shared/editor/nodes/Embed.tsx +++ b/shared/editor/nodes/Embed.tsx @@ -11,6 +11,7 @@ import { NodeSelection, TextSelection } from "prosemirror-state"; import * as React from "react"; import type { Primitive } from "utility-types"; import { sanitizeUrl } from "../../utils/urls"; +import { contentWidth } from "../lib/contentWidth"; import { toggleAspectRatioLock } from "../commands/toggleAspectRatioLock"; import EmbedComponent from "../components/Embed"; import defaultEmbeds from "../embeds"; @@ -126,7 +127,7 @@ export default class Embed extends Node { const transaction = tr .setNodeMarkup(pos, undefined, { ...node.attrs, - width, + width: this.storedWidth(width), height, }) .setMeta("addToHistory", true); @@ -150,6 +151,31 @@ export default class Embed extends Node { commands({ type }: { type: NodeType }) { return { toggleEmbedAspectRatioLock: (): Command => toggleAspectRatioLock(type), + resizeEmbed: + ({ width, height }: { width: number; height: number }): Command => + (state, dispatch) => { + const { selection } = state; + if ( + !(selection instanceof NodeSelection) || + selection.node.type !== type + ) { + return false; + } + + const transaction = state.tr + .setNodeMarkup(selection.from, undefined, { + ...selection.node.attrs, + width: this.storedWidth(width), + height, + }) + .setMeta("addToHistory", true); + dispatch?.( + transaction.setSelection( + NodeSelection.create(transaction.doc, selection.from) + ) + ); + return true; + }, embed: (attrs: Record): Command => (state, dispatch) => { @@ -223,4 +249,15 @@ export default class Embed extends Node { }), }; } + + /** + * Returns the width to store, where a width at the editor edge is stored as + * unset so the embed stays full width. + * + * @param width - the resized width. + * @returns the width to store. + */ + private storedWidth(width: number) { + return width >= contentWidth(this.editor.view.dom) ? null : width; + } }