mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
* feat: POC * fix: cleanup * fix: cleanup * fix: can use existing `UiStore` in favor of `LightboxStore` * fix: style dialog overlay and content * fix: style and position action buttons * fix: keyboard nav * fix: display caption * fix: cleanup * fix: force `Lightbox` to unmount if `DocumentScene` re-renders * fix: cleanup * fix: images going out of bounds–trying `object-fit` for first pass * fix: making `Figure` a flexbox and setting its size to img dimensions, gets rid of the `object-fit` "letterboxing" effect * feat: image transition * fix: match animation to that of `ImageZoom` * fix: `fade-in` overlay * fix: match overlay background to main background * fix: fade out nav if idle for 3s * fix: width & height transition in firefox * fix: move nav buttons sideways * fix: button sizes * fix: don't let nav buttons become invisible due to image in backdrop * feat: download btn * feat: handle swipe left, right & down on mobile, also double tap to open lightbox * fix: off-by-one * fix: for img to be visible, if not animating * fix: cleanup * just a showcase commit, it's broken and will be reverted * Revert "just a showcase commit, it's broken and will be reverted" This reverts commit 63d7cbda9895f92a530cfb3a8402eaacb2414947. * feat: `animateOnClose` * fix: prevent body from shifting horizontally * Revert "fix: prevent body from shifting horizontally" This reverts commit fcc2d0d1daaab458325ece880d13903b42560dfd. * fix: lint * fix: make lightbox resilient to doc changes * fix: `VisuallyHidden` `Dialog.Title` * fix: fade in action and nav buttons too * fix: `fadeOut` overlay, action & nav buttons upon close * fix: `dom.querySelector` is not a function * fix: let `objectFit` compute the img dimensions * fix: flashing caption * fix: hide nav keys based on whether it's a first or last img * fix: tooltip for action buttons * Fixes: 1. `Space` with the image selected should trigger the lightbox 2. There should be a "Zoom in" cursor on the image when selected so you know you can click to zoom * fix: fade out on close if there's img loading err * fix: pull animation duration into a const * fix: upscaled editor image animating unreliably * fix: lint * fixes: 1. Model as state machine 2. Replace `transition` with `animation` * fix: cleanup * fix: DRY * fix: cleanup * fix: img loading indicator * fix: swipe * fix: types * fix: Incorrect transitions when image nodes are duplicated * Tweaks, translations, aria * fix: Incorrect transition if image is added while lightbox is open * fix: review * Remove ImageZoom, react-medium-image-zoom * Tweak styling * fix: Quick fix for multiple open editors * refactor: Move active lightbox state into editor * Apply graphite suggestions --------- Co-authored-by: Tom Moor <tom@getoutline.com>
278 lines
8.0 KiB
TypeScript
278 lines
8.0 KiB
TypeScript
import { CrossIcon, DownloadIcon, GlobeIcon } from "outline-icons";
|
|
import type { EditorView } from "prosemirror-view";
|
|
import * as React from "react";
|
|
import styled from "styled-components";
|
|
import Flex from "../../components/Flex";
|
|
import { s } from "../../styles";
|
|
import { isExternalUrl, sanitizeUrl } from "../../utils/urls";
|
|
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
|
|
import { ComponentProps } from "../types";
|
|
import { ResizeLeft, ResizeRight } from "./ResizeHandle";
|
|
import useDragResize from "./hooks/useDragResize";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
type Props = ComponentProps & {
|
|
/** Callback triggered when the image is clicked */
|
|
onClick: () => void;
|
|
/** Callback triggered when the download button is clicked */
|
|
onDownload?: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
|
/** Callback triggered when the image is resized */
|
|
onChangeSize?: (props: { width: number; height?: number }) => void;
|
|
/** The editor view */
|
|
view: EditorView;
|
|
children?: React.ReactElement;
|
|
};
|
|
|
|
const Image = (props: Props) => {
|
|
const { isSelected, node, isEditable, onChangeSize, onClick } = props;
|
|
const { src, layoutClass } = node.attrs;
|
|
const { t } = useTranslation();
|
|
const className = layoutClass ? `image image-${layoutClass}` : "image";
|
|
const [loaded, setLoaded] = React.useState(false);
|
|
const [error, setError] = React.useState(false);
|
|
const [naturalWidth, setNaturalWidth] = React.useState(node.attrs.width);
|
|
const [naturalHeight, setNaturalHeight] = React.useState(node.attrs.height);
|
|
const lastTapTimeRef = React.useRef(0);
|
|
const ref = React.useRef<HTMLDivElement>(null);
|
|
const { width, height, setSize, handlePointerDown, dragging } = useDragResize(
|
|
{
|
|
width: node.attrs.width ?? naturalWidth,
|
|
height: node.attrs.height ?? naturalHeight,
|
|
naturalWidth,
|
|
naturalHeight,
|
|
gridSnap: 5,
|
|
onChangeSize,
|
|
ref,
|
|
}
|
|
);
|
|
|
|
const isFullWidth = layoutClass === "full-width";
|
|
const isResizable = !!props.onChangeSize && !error;
|
|
const isDownloadable = !!props.onDownload && !error;
|
|
|
|
React.useEffect(() => {
|
|
if (node.attrs.width && node.attrs.width !== width) {
|
|
setSize({
|
|
width: node.attrs.width,
|
|
height: node.attrs.height,
|
|
});
|
|
}
|
|
}, [node.attrs.width]);
|
|
|
|
const sanitizedSrc = sanitizeUrl(src);
|
|
|
|
const handleOpen = React.useCallback(() => {
|
|
window.open(sanitizedSrc, "_blank");
|
|
}, [sanitizedSrc]);
|
|
|
|
const widthStyle = isFullWidth
|
|
? { width: "var(--container-width)" }
|
|
: { width: width || "auto" };
|
|
|
|
const handleImageTouchStart = (ev: React.TouchEvent<HTMLDivElement>) => {
|
|
const currentTime = Date.now();
|
|
const timeSinceLastTap = currentTime - lastTapTimeRef.current;
|
|
|
|
if (timeSinceLastTap < 300 && isSelected) {
|
|
ev.preventDefault();
|
|
onClick();
|
|
}
|
|
|
|
lastTapTimeRef.current = currentTime;
|
|
};
|
|
|
|
const handleImageClick = (ev: React.MouseEvent<HTMLDivElement>) => {
|
|
if (!isEditable || isSelected) {
|
|
ev.preventDefault();
|
|
onClick();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div contentEditable={false} className={className} ref={ref}>
|
|
<ImageWrapper
|
|
isFullWidth={isFullWidth}
|
|
className={isSelected || dragging ? "ProseMirror-selectednode" : ""}
|
|
style={widthStyle}
|
|
>
|
|
{!dragging && width > 60 && isDownloadable && (
|
|
<Actions>
|
|
{isExternalUrl(src) && (
|
|
<Button onClick={handleOpen} aria-label={t("Open")}>
|
|
<GlobeIcon />
|
|
</Button>
|
|
)}
|
|
<Button onClick={props.onDownload} aria-label={t("Download")}>
|
|
<DownloadIcon />
|
|
</Button>
|
|
</Actions>
|
|
)}
|
|
{error ? (
|
|
<Error style={widthStyle} className={EditorStyleHelper.imageHandle}>
|
|
<CrossIcon size={16} /> Image failed to load
|
|
</Error>
|
|
) : (
|
|
<>
|
|
<img
|
|
className={EditorStyleHelper.imageHandle}
|
|
style={{
|
|
...widthStyle,
|
|
display: loaded ? "block" : "none",
|
|
pointerEvents:
|
|
dragging || (!props.isSelected && props.isEditable)
|
|
? "none"
|
|
: "all",
|
|
}}
|
|
src={sanitizedSrc}
|
|
alt={node.attrs.alt || ""}
|
|
onError={() => {
|
|
setError(true);
|
|
setLoaded(true);
|
|
}}
|
|
onLoad={(ev: React.SyntheticEvent<HTMLImageElement>) => {
|
|
// For some SVG's Firefox does not provide the naturalWidth, in this
|
|
// rare case we need to provide a default so that the image can be
|
|
// seen and is not sized to 0px
|
|
const nw = (ev.target as HTMLImageElement).naturalWidth || 300;
|
|
const nh = (ev.target as HTMLImageElement).naturalHeight;
|
|
setNaturalWidth(nw);
|
|
setNaturalHeight(nh);
|
|
setLoaded(true);
|
|
|
|
if (!node.attrs.width) {
|
|
setSize((state) => ({
|
|
...state,
|
|
width: nw,
|
|
}));
|
|
}
|
|
}}
|
|
onClick={handleImageClick}
|
|
onTouchStart={handleImageTouchStart}
|
|
/>
|
|
{!loaded && width && height && (
|
|
<img
|
|
style={{
|
|
...widthStyle,
|
|
display: "block",
|
|
}}
|
|
src={`data:image/svg+xml;charset=UTF-8,${encodeURIComponent(
|
|
getPlaceholder(width, height)
|
|
)}`}
|
|
/>
|
|
)}
|
|
</>
|
|
)}
|
|
{isEditable && !isFullWidth && isResizable && (
|
|
<>
|
|
<ResizeLeft
|
|
onPointerDown={handlePointerDown("left")}
|
|
$dragging={!!dragging}
|
|
/>
|
|
<ResizeRight
|
|
onPointerDown={handlePointerDown("right")}
|
|
$dragging={!!dragging}
|
|
/>
|
|
</>
|
|
)}
|
|
</ImageWrapper>
|
|
{isFullWidth && props.children
|
|
? React.cloneElement(props.children, { style: widthStyle })
|
|
: props.children}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
function getPlaceholder(width: number, height: number) {
|
|
return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" />`;
|
|
}
|
|
|
|
export const Error = styled(Flex)`
|
|
max-width: 100%;
|
|
color: ${s("textTertiary")};
|
|
font-size: 14px;
|
|
background: ${s("backgroundSecondary")};
|
|
border-radius: 4px;
|
|
min-width: 33vw;
|
|
height: 80px;
|
|
align-items: center;
|
|
justify-content: center;
|
|
user-select: none;
|
|
`;
|
|
|
|
const Actions = styled.div`
|
|
display: flex;
|
|
align-items: center;
|
|
position: absolute;
|
|
gap: 1px;
|
|
top: 8px;
|
|
right: 8px;
|
|
opacity: 0;
|
|
transition: opacity 150ms ease-in-out;
|
|
|
|
&:hover {
|
|
opacity: 1;
|
|
}
|
|
`;
|
|
|
|
const Button = styled.button`
|
|
border: 0;
|
|
margin: 0;
|
|
padding: 0;
|
|
border-radius: 4px;
|
|
background: ${s("background")};
|
|
color: ${s("textSecondary")};
|
|
width: 24px;
|
|
height: 24px;
|
|
display: inline-block;
|
|
cursor: var(--pointer) !important;
|
|
transition: opacity 150ms ease-in-out;
|
|
|
|
&:first-child:not(:last-child) {
|
|
border-top-right-radius: 0;
|
|
border-bottom-right-radius: 0;
|
|
}
|
|
|
|
&:last-child:not(:first-child) {
|
|
border-top-left-radius: 0;
|
|
border-bottom-left-radius: 0;
|
|
}
|
|
|
|
&:active {
|
|
transform: scale(0.98);
|
|
}
|
|
|
|
&:hover {
|
|
color: ${s("text")};
|
|
}
|
|
`;
|
|
|
|
const ImageWrapper = styled.div<{ isFullWidth: boolean }>`
|
|
line-height: 0;
|
|
position: relative;
|
|
margin-left: auto;
|
|
margin-right: auto;
|
|
max-width: ${(props) => (props.isFullWidth ? "initial" : "100%")};
|
|
transition-property: width, height;
|
|
transition-duration: ${(props) => (props.isFullWidth ? "0ms" : "150ms")};
|
|
transition-timing-function: ease-in-out;
|
|
overflow: hidden;
|
|
|
|
img {
|
|
transition-property: width, height;
|
|
transition-duration: ${(props) => (props.isFullWidth ? "0ms" : "150ms")};
|
|
transition-timing-function: ease-in-out;
|
|
}
|
|
|
|
&:hover {
|
|
${Actions} {
|
|
opacity: 0.9;
|
|
}
|
|
|
|
${ResizeLeft}, ${ResizeRight} {
|
|
opacity: 1;
|
|
}
|
|
}
|
|
`;
|
|
|
|
export default Image;
|