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

This commit is contained in:
Tom Moor committed 2026-11-20 15:52:54 -05:00
1 parent 97c780fd04
commit fd1c10ec7b
4 files changed
+87 -41

No files matched your search

+26 -10
View File
@@ -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<Dimension>(() => ({
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<HTMLInputElement>) => {
+17 -14
View File
@@ -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" && (
<Tooltip content={t("Lock aspect ratio")}>
<AspectRatioLockButton
isLocked={node.attrs.lockAspectRatio !== false}
onClick={handleToggleLock}
/>
</Tooltip>
<>
<ToolbarSeparator />
<Toolbar.Root asChild>
<DimensionGroup>
<MediaDimension tooltip={`${t("Width")} × ${t("Height")}`} />
</DimensionGroup>
</Toolbar.Root>
<ToolbarSeparator />
</>
)}
<Tooltip content={t("Open link")}>
<ToolbarButton onClick={openLink} disabled={!localUrl}>
@@ -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;
+6 -16
View File
@@ -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<HTMLDivElement>(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",
};
+38 -1
View File
@@ -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<string, Primitive>): 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;
}
}