mirror of
https://github.com/outline/outline.git
synced 2026-11-25 07:04:37 +03:00
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.
This commit is contained in:
4 files changed
+93
-19
No files matched your search
@@ -57,7 +57,7 @@ export function MediaDimension({ tooltip }: Props) {
|
||||
|
||||
// Only PDFs can be unlocked, other media always render at their natural aspect ratio.
|
||||
const canUnlock = nodeType === "attachment";
|
||||
const [isLocked, setIsLocked] = useState(true);
|
||||
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
|
||||
@@ -211,9 +211,9 @@ export function MediaDimension({ tooltip }: Props) {
|
||||
]);
|
||||
|
||||
const handleToggleLock = useCallback(() => {
|
||||
setIsLocked((prev) => !prev);
|
||||
commands["toggleAttachmentAspectRatioLock"]();
|
||||
setError({ width: false, height: false });
|
||||
}, []);
|
||||
}, [commands]);
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
@@ -261,7 +261,9 @@ export function MediaDimension({ tooltip }: Props) {
|
||||
|
||||
const dimensions =
|
||||
nodeType === "attachment"
|
||||
? scalePDFDimensions({ width, height }, newWidth)
|
||||
? isLocked
|
||||
? scalePDFDimensions({ width, height }, newWidth)
|
||||
: { width: newWidth, height: newHeight ?? height }
|
||||
: { width: newWidth, height: newHeight };
|
||||
|
||||
setLocalDimension({
|
||||
@@ -275,7 +277,7 @@ export function MediaDimension({ tooltip }: Props) {
|
||||
return () => {
|
||||
window.removeEventListener("media-drag-resize", handleDragResize);
|
||||
};
|
||||
}, [nodeType, width, height]);
|
||||
}, [nodeType, width, height, isLocked]);
|
||||
|
||||
// Debounce validation so errors are not shown while typing.
|
||||
useEffect(() => {
|
||||
|
||||
@@ -2,7 +2,14 @@ import React, { useCallback, useRef } from "react";
|
||||
import { useMergeRefs } from "react-merge-refs";
|
||||
import styled from "styled-components";
|
||||
import useDragResize from "./hooks/useDragResize";
|
||||
import { ResizeLeft, ResizeRight } from "./ResizeHandle";
|
||||
import {
|
||||
ResizeBottomLeft,
|
||||
ResizeBottomRight,
|
||||
ResizeLeft,
|
||||
ResizeRight,
|
||||
ResizeTopLeft,
|
||||
ResizeTopRight,
|
||||
} from "./ResizeHandle";
|
||||
import type { ComponentProps } from "../types";
|
||||
import { isFirefox } from "../../utils/browser";
|
||||
import { sanitizeUrl } from "../../utils/urls";
|
||||
@@ -35,11 +42,12 @@ export default function PdfViewer(props: Props) {
|
||||
const debounceTimerRef = useRef<NodeJS.Timeout | null>(null);
|
||||
const dimensions = resolvePDFDimensions(node.attrs.width, node.attrs.height);
|
||||
|
||||
const { width, handlePointerDown, dragging } = useDragResize({
|
||||
const { width, height, handlePointerDown, dragging } = useDragResize({
|
||||
width: node.attrs.width,
|
||||
height: node.attrs.height,
|
||||
height: dimensions.height,
|
||||
naturalWidth: pdfNaturalWidth,
|
||||
naturalHeight: pdfNaturalHeight,
|
||||
aspectRatio: dimensions.height / dimensions.width,
|
||||
onChangeSize,
|
||||
ref,
|
||||
});
|
||||
@@ -114,7 +122,10 @@ export default function PdfViewer(props: Props) {
|
||||
style={{
|
||||
width: "100%",
|
||||
height: "auto",
|
||||
aspectRatio: `${dimensions.width} / ${dimensions.height}`,
|
||||
aspectRatio:
|
||||
node.attrs.lockAspectRatio !== false
|
||||
? `${dimensions.width} / ${dimensions.height}`
|
||||
: `${width ?? dimensions.width} / ${height ?? dimensions.height}`,
|
||||
pointerEvents:
|
||||
!isEditable || (isSelected && !dragging) ? "initial" : "none",
|
||||
marginTop: 6,
|
||||
@@ -130,6 +141,22 @@ export default function PdfViewer(props: Props) {
|
||||
onPointerDown={handlePointerDown("right")}
|
||||
$dragging={!!(isSelected || dragging)}
|
||||
/>
|
||||
<ResizeTopLeft
|
||||
onPointerDown={handlePointerDown("topLeft")}
|
||||
$dragging={!!dragging}
|
||||
/>
|
||||
<ResizeTopRight
|
||||
onPointerDown={handlePointerDown("topRight")}
|
||||
$dragging={!!dragging}
|
||||
/>
|
||||
<ResizeBottomLeft
|
||||
onPointerDown={handlePointerDown("bottomLeft")}
|
||||
$dragging={!!dragging}
|
||||
/>
|
||||
<ResizeBottomRight
|
||||
onPointerDown={handlePointerDown("bottomRight")}
|
||||
$dragging={!!dragging}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</PDFWrapper>
|
||||
@@ -160,6 +187,23 @@ const PDFWrapper = styled.div<{ $dragging: boolean }>`
|
||||
will-change: ${(props) => (props.$dragging ? "width, height" : "auto")};
|
||||
}
|
||||
|
||||
/* The wrapper clips overflow, so corners sit inside it rather than on the edge. */
|
||||
${ResizeTopLeft}, ${ResizeTopRight} {
|
||||
top: 0;
|
||||
}
|
||||
|
||||
${ResizeBottomLeft}, ${ResizeBottomRight} {
|
||||
bottom: 0;
|
||||
}
|
||||
|
||||
${ResizeTopLeft}, ${ResizeBottomLeft} {
|
||||
left: 0;
|
||||
}
|
||||
|
||||
${ResizeTopRight}, ${ResizeBottomRight} {
|
||||
right: 0;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
${ResizeLeft}, ${ResizeRight} {
|
||||
opacity: 1;
|
||||
|
||||
@@ -74,6 +74,8 @@ type Params = {
|
||||
ref: React.RefObject<HTMLDivElement | null>;
|
||||
/** Whether the element should scale symmetrically from the center. Defaults to true. */
|
||||
isCentered?: boolean;
|
||||
/** The height to width ratio kept when resizing from a corner. Defaults to the natural ratio. */
|
||||
aspectRatio?: number;
|
||||
};
|
||||
|
||||
export default function useDragResize(props: Params): ReturnValue {
|
||||
@@ -86,6 +88,9 @@ export default function useDragResize(props: Params): ReturnValue {
|
||||
ref,
|
||||
isCentered = true,
|
||||
} = props;
|
||||
const aspectRatio =
|
||||
props.aspectRatio ??
|
||||
(naturalWidth ? naturalHeight / naturalWidth : undefined);
|
||||
|
||||
// The committed size passed in through props is the source of truth. `draft`
|
||||
// holds the size being previewed while resizing and is released once the
|
||||
@@ -166,8 +171,7 @@ export default function useDragResize(props: Params): ReturnValue {
|
||||
"bottomRight",
|
||||
].includes(dragging || "");
|
||||
|
||||
if (isCorner && naturalHeight && naturalWidth) {
|
||||
const aspectRatio = naturalHeight / naturalWidth;
|
||||
if (isCorner && aspectRatio) {
|
||||
const hFactor = isCentered ? 0.5 : 1;
|
||||
const factor = isCentered ? 2 : 1;
|
||||
const dW =
|
||||
@@ -180,7 +184,6 @@ export default function useDragResize(props: Params): ReturnValue {
|
||||
const factor = isCentered ? 2 : 1;
|
||||
const newWidth = sizeAtDragStart.width + diffX * factor;
|
||||
const constrainedWidth = constrainWidth(newWidth, maxWidth);
|
||||
const aspectRatio = naturalHeight / naturalWidth;
|
||||
|
||||
// When dragged to or beyond the editor edge, store the natural width as a
|
||||
// sentinel for "full width" so the element stays responsive. Only do this
|
||||
@@ -192,7 +195,7 @@ export default function useDragResize(props: Params): ReturnValue {
|
||||
? naturalWidth
|
||||
: constrainedWidth;
|
||||
const nextHeight = isCorner
|
||||
? naturalWidth
|
||||
? aspectRatio
|
||||
? Math.round(constrainedWidth * aspectRatio)
|
||||
: undefined
|
||||
: sizeAtDragStart.height;
|
||||
@@ -245,7 +248,7 @@ export default function useDragResize(props: Params): ReturnValue {
|
||||
maxWidth,
|
||||
gridHeightSnap,
|
||||
naturalWidth,
|
||||
naturalHeight,
|
||||
aspectRatio,
|
||||
minHeight,
|
||||
constrainWidth,
|
||||
isCentered,
|
||||
|
||||
@@ -54,6 +54,11 @@ export default class Attachment extends Node {
|
||||
height: {
|
||||
default: null,
|
||||
},
|
||||
// Unset means locked, so only unlocked nodes store a value.
|
||||
lockAspectRatio: {
|
||||
default: null,
|
||||
validate: "boolean|null",
|
||||
},
|
||||
contentType: {
|
||||
default: null,
|
||||
validate: "string|null",
|
||||
@@ -111,7 +116,7 @@ export default class Attachment extends Node {
|
||||
node: ProsemirrorNode;
|
||||
getPos: () => number | undefined;
|
||||
}) =>
|
||||
({ width }: { width: number; height?: number }) => {
|
||||
({ width, height }: { width: number; height?: number }) => {
|
||||
if (!node.attrs.preview) {
|
||||
return;
|
||||
}
|
||||
@@ -125,10 +130,11 @@ export default class Attachment extends Node {
|
||||
}
|
||||
|
||||
const $pos = doc.resolve(pos);
|
||||
const dimensions = scalePDFDimensions(
|
||||
resolvePDFDimensions(node.attrs.width, node.attrs.height),
|
||||
width
|
||||
);
|
||||
const current = resolvePDFDimensions(node.attrs.width, node.attrs.height);
|
||||
const dimensions =
|
||||
node.attrs.lockAspectRatio !== false
|
||||
? scalePDFDimensions(current, width)
|
||||
: { width, height: height ?? current.height };
|
||||
|
||||
view.dispatch(tr.setSelection(new NodeSelection($pos)));
|
||||
commands["resizeAttachment"](dimensions);
|
||||
@@ -271,6 +277,25 @@ export default class Attachment extends Node {
|
||||
dispatch?.(transaction.setSelection(new NodeSelection($pos)));
|
||||
return true;
|
||||
},
|
||||
toggleAttachmentAspectRatioLock: (): Command => (state, dispatch) => {
|
||||
if (
|
||||
!(state.selection instanceof NodeSelection) ||
|
||||
!state.selection.node.attrs.preview
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { attrs } = state.selection.node;
|
||||
const transaction = state.tr
|
||||
.setNodeMarkup(state.selection.from, undefined, {
|
||||
...attrs,
|
||||
lockAspectRatio: attrs.lockAspectRatio === false ? null : false,
|
||||
})
|
||||
.setMeta("addToHistory", true);
|
||||
const $pos = transaction.doc.resolve(state.selection.from);
|
||||
dispatch?.(transaction.setSelection(new NodeSelection($pos)));
|
||||
return true;
|
||||
},
|
||||
resizeAttachment:
|
||||
({ width, height }: { width: number; height?: number }): Command =>
|
||||
(state, dispatch) => {
|
||||
|
||||
Reference in new issue
Block a user