mirror of
https://github.com/outline/outline.git
synced 2026-12-05 02:44:34 +03:00
Stores the lock as an optional attachment attribute, where unset means locked, so side handles change only the width of unlocked PDFs.
213 lines
6.0 KiB
TypeScript
213 lines
6.0 KiB
TypeScript
import React, { useCallback, useRef } from "react";
|
|
import { useMergeRefs } from "react-merge-refs";
|
|
import styled from "styled-components";
|
|
import useDragResize from "./hooks/useDragResize";
|
|
import {
|
|
ResizeBottomLeft,
|
|
ResizeBottomRight,
|
|
ResizeLeft,
|
|
ResizeRight,
|
|
ResizeTopLeft,
|
|
ResizeTopRight,
|
|
} from "./ResizeHandle";
|
|
import type { ComponentProps } from "../types";
|
|
import { isFirefox } from "../../utils/browser";
|
|
import { sanitizeUrl } from "../../utils/urls";
|
|
import Flex from "../../components/Flex";
|
|
import { s } from "../../styles";
|
|
import { Preview, Subtitle, Title } from "./Widget";
|
|
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
|
|
import {
|
|
pdfNaturalHeight,
|
|
pdfNaturalWidth,
|
|
resolvePDFDimensions,
|
|
} from "../lib/pdf";
|
|
|
|
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 dimensions = resolvePDFDimensions(node.attrs.width, node.attrs.height);
|
|
|
|
const { width, height, handlePointerDown, dragging } = useDragResize({
|
|
width: node.attrs.width,
|
|
height: dimensions.height,
|
|
naturalWidth: pdfNaturalWidth,
|
|
naturalHeight: pdfNaturalHeight,
|
|
aspectRatio: dimensions.height / dimensions.width,
|
|
onChangeSize,
|
|
ref,
|
|
});
|
|
|
|
// force embed to reload, so the content fits the new size.
|
|
const observeRef = useCallback(
|
|
(element: HTMLDivElement | null) => {
|
|
// firefox handles resizing on its own
|
|
// and forced reload causes the parent to collapse while resizing
|
|
if (isFirefox || !element) {
|
|
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 = sanitizeUrl(href) ?? "";
|
|
}
|
|
});
|
|
}
|
|
}, 250);
|
|
});
|
|
|
|
observer.observe(element);
|
|
|
|
return () => {
|
|
observer.disconnect();
|
|
if (debounceTimerRef.current) {
|
|
clearTimeout(debounceTimerRef.current);
|
|
}
|
|
};
|
|
},
|
|
[dragging, href]
|
|
);
|
|
|
|
const mergedRef = useMergeRefs([ref, observeRef]);
|
|
|
|
return (
|
|
<PDFWrapper
|
|
contentEditable={false}
|
|
ref={mergedRef}
|
|
className={
|
|
isSelected || dragging
|
|
? `${EditorStyleHelper.pdfWrapper} ProseMirror-selectednode`
|
|
: EditorStyleHelper.pdfWrapper
|
|
}
|
|
style={{ width: width ?? "100%" }}
|
|
$dragging={!!dragging}
|
|
>
|
|
<Flex gap={6} align="center">
|
|
{props.icon}
|
|
<Preview>
|
|
<Title>{props.title}</Title>
|
|
<Subtitle>{props.context}</Subtitle>
|
|
</Preview>
|
|
</Flex>
|
|
<embed
|
|
title={name}
|
|
src={sanitizeUrl(href)}
|
|
ref={embedRef}
|
|
style={{
|
|
width: "100%",
|
|
height: "auto",
|
|
aspectRatio:
|
|
node.attrs.lockAspectRatio !== false
|
|
? `${dimensions.width} / ${dimensions.height}`
|
|
: `${width ?? dimensions.width} / ${height ?? dimensions.height}`,
|
|
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)}
|
|
/>
|
|
<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>
|
|
);
|
|
}
|
|
|
|
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 {
|
|
display: block;
|
|
max-width: 100%;
|
|
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")};
|
|
}
|
|
|
|
/* 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;
|
|
}
|
|
}
|
|
`;
|