mirror of
https://github.com/outline/outline.git
synced 2026-11-27 16:08:58 +03:00
* fix: Remove unused grid snapping from element resizing Horizontal resizing snapped widths to a 5% grid, which is no longer desired. Replace the only remaining use of the gridSnap prop (the minimum-width clamp) with a named constant and drop the prop entirely. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix: Remove resize lag by disabling size transition while dragging The width/height CSS transition on resizable elements existed to smooth the discrete jumps from grid snapping. With pixel-by-pixel resizing the element perpetually animates toward a target ~150ms in the future, so it visibly trails the cursor. Disable the transition while actively dragging and restore it afterwards so snap-back and collaborative size changes still animate. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix: Constrain image resizing to editor edge instead of snapping to natural size When dragging an element past the editor bounds, the full-width sentinel forced the width to the natural size. For images narrower than the editor this snapped them back to their (smaller) natural width at the boundary. Only use the natural-width sentinel when the image is genuinely wider than the editor; otherwise constrain to the editor edge. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * PR feedback --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
168 lines
4.6 KiB
TypeScript
168 lines
4.6 KiB
TypeScript
import React, { useEffect, useRef } from "react";
|
|
import styled from "styled-components";
|
|
import useDragResize from "./hooks/useDragResize";
|
|
import { ResizeLeft, ResizeRight } from "./ResizeHandle";
|
|
import type { ComponentProps } from "../types";
|
|
import { isFirefox } from "../../utils/browser";
|
|
import Flex from "../../components/Flex";
|
|
import { s } from "../../styles";
|
|
import { Preview, Subtitle, Title } from "./Widget";
|
|
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
|
|
|
|
type Props = ComponentProps & {
|
|
/** Icon to display on the left side of the widget */
|
|
icon: React.ReactNode;
|
|
/** Title of the widget */
|
|
title: React.ReactNode;
|
|
/** Context, displayed to right of title */
|
|
context?: React.ReactNode;
|
|
/** Callback triggered when the pdf is resized */
|
|
onChangeSize?: (props: { width: number; height?: number }) => void;
|
|
};
|
|
|
|
export default function PdfViewer(props: Props) {
|
|
const { node, isEditable, onChangeSize, isSelected } = props;
|
|
const { href, name } = node.attrs;
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
const embedRef = useRef<HTMLEmbedElement>(null);
|
|
const debounceTimerRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
const { width, height, setSize, handlePointerDown, dragging } = useDragResize(
|
|
{
|
|
width: node.attrs.width,
|
|
height: node.attrs.height,
|
|
naturalWidth: 300,
|
|
naturalHeight: 424,
|
|
onChangeSize,
|
|
ref,
|
|
}
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (node.attrs.width && node.attrs.width !== width) {
|
|
setSize({
|
|
width: node.attrs.width,
|
|
height: node.attrs.height,
|
|
});
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [node.attrs.width]);
|
|
|
|
// force embed to reload, so the content fits the new size.
|
|
useEffect(() => {
|
|
// firefox handles resizing on its own
|
|
// and forced reload causes the parent to collapse while resizing
|
|
if (isFirefox || !ref.current) {
|
|
return;
|
|
}
|
|
|
|
const observer = new ResizeObserver(() => {
|
|
if (dragging) {
|
|
return;
|
|
}
|
|
|
|
if (debounceTimerRef.current) {
|
|
clearTimeout(debounceTimerRef.current);
|
|
}
|
|
|
|
debounceTimerRef.current = setTimeout(() => {
|
|
if (embedRef.current) {
|
|
embedRef.current.src = "";
|
|
requestAnimationFrame(() => {
|
|
if (embedRef.current) {
|
|
embedRef.current.src = href;
|
|
}
|
|
});
|
|
}
|
|
}, 250);
|
|
});
|
|
|
|
observer.observe(ref.current);
|
|
|
|
return () => {
|
|
observer.disconnect();
|
|
if (debounceTimerRef.current) {
|
|
clearTimeout(debounceTimerRef.current);
|
|
}
|
|
};
|
|
}, [dragging, href]);
|
|
|
|
return (
|
|
<PDFWrapper
|
|
contentEditable={false}
|
|
ref={ref}
|
|
className={
|
|
isSelected || dragging
|
|
? "pdf-wrapper ProseMirror-selectednode"
|
|
: "pdf-wrapper"
|
|
}
|
|
style={{ width: width ?? "auto" }}
|
|
$dragging={dragging}
|
|
>
|
|
<Flex gap={6} align="center">
|
|
{props.icon}
|
|
<Preview>
|
|
<Title>{props.title}</Title>
|
|
<Subtitle>{props.context}</Subtitle>
|
|
</Preview>
|
|
</Flex>
|
|
<embed
|
|
title={name}
|
|
src={href}
|
|
ref={embedRef}
|
|
width={
|
|
// subtract padding and borders from width
|
|
width - 24
|
|
}
|
|
height={height}
|
|
style={{
|
|
pointerEvents:
|
|
!isEditable || (isSelected && !dragging) ? "initial" : "none",
|
|
marginTop: 6,
|
|
}}
|
|
/>
|
|
{isEditable && !!props.onChangeSize && (
|
|
<>
|
|
<ResizeLeft
|
|
onPointerDown={handlePointerDown("left")}
|
|
$dragging={isSelected || dragging}
|
|
/>
|
|
<ResizeRight
|
|
onPointerDown={handlePointerDown("right")}
|
|
$dragging={isSelected || dragging}
|
|
/>
|
|
</>
|
|
)}
|
|
</PDFWrapper>
|
|
);
|
|
}
|
|
|
|
const PDFWrapper = styled.div<{ $dragging: boolean }>`
|
|
line-height: 0;
|
|
position: relative;
|
|
margin-left: auto;
|
|
margin-right: auto;
|
|
max-width: 100%;
|
|
transition-property: width, height;
|
|
transition-duration: ${(props) => (props.$dragging ? "0ms" : "120ms")};
|
|
transition-timing-function: ease-in-out;
|
|
overflow: hidden;
|
|
will-change: ${(props) => (props.$dragging ? "width, height" : "auto")};
|
|
box-shadow: 0 0 0 1px ${s("divider")};
|
|
border-radius: ${EditorStyleHelper.blockRadius};
|
|
padding: ${EditorStyleHelper.blockRadius};
|
|
|
|
embed {
|
|
transition-property: width, height;
|
|
transition-duration: ${(props) => (props.$dragging ? "0ms" : "120ms")};
|
|
transition-timing-function: ease-in-out;
|
|
will-change: ${(props) => (props.$dragging ? "width, height" : "auto")};
|
|
}
|
|
|
|
&:hover {
|
|
${ResizeLeft}, ${ResizeRight} {
|
|
opacity: 1;
|
|
}
|
|
}
|
|
`;
|