mirror of
https://github.com/outline/outline.git
synced 2026-12-01 09:22:14 +03:00
* fix: Allow PDF embed width and height to change independently Adds an aspect ratio lock toggle to the media dimension toolbar for PDF embeds, renders PDFs at their stored aspect ratio, and keeps that ratio when drag resizing. * 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. * feat: Consistent resize handles for images, videos, PDFs and embeds All media get side, bottom and corner handles driven by one lock rule. Images and videos stay locked. PDFs and embeds can be unlocked to resize each axis independently. * feat: Show width, height and lock in the embed toolbar * feat: Start embeds unlocked and step media dimensions with arrow keys Embeds start with an unlocked aspect ratio so full-width embeds can still be made taller, while PDFs start locked. Up and down arrows step the dimension inputs by 1, or 10 with shift. * fix: Align the aspect ratio padlock with the dimension digits * fix: Move the aspect ratio lock closer to the dimension inputs * fix: Show the aspect ratio lock state in its tooltip
167 lines
4.5 KiB
TypeScript
167 lines
4.5 KiB
TypeScript
import { OpenIcon, TrashIcon } from "outline-icons";
|
||
import type { Node } from "prosemirror-model";
|
||
import { Selection, TextSelection } from "prosemirror-state";
|
||
import type { EditorView } from "prosemirror-view";
|
||
import { useCallback, useRef, useState } from "react";
|
||
import { useTranslation } from "react-i18next";
|
||
import styled from "styled-components";
|
||
import Flex from "~/components/Flex";
|
||
import Tooltip from "~/components/Tooltip";
|
||
import * as Toolbar from "@radix-ui/react-toolbar";
|
||
import Input from "~/editor/components/Input";
|
||
import { MediaDimension } from "./MediaDimension";
|
||
import ToolbarSeparator from "./ToolbarSeparator";
|
||
import ToolbarButton from "./ToolbarButton";
|
||
import useOnClickOutside from "~/hooks/useOnClickOutside";
|
||
|
||
type Props = {
|
||
node?: Node;
|
||
view: EditorView;
|
||
autoFocus?: boolean;
|
||
onLinkUpdate: () => void;
|
||
onLinkRemove: () => void;
|
||
onEscape: () => void;
|
||
onClickOutside: (ev: MouseEvent | TouchEvent) => void;
|
||
};
|
||
|
||
export function MediaLinkEditor({
|
||
node,
|
||
view,
|
||
onLinkUpdate,
|
||
onLinkRemove,
|
||
onEscape,
|
||
onClickOutside,
|
||
}: Props) {
|
||
const { t } = useTranslation();
|
||
const url = (node?.attrs.href ?? node?.attrs.src) as string;
|
||
const [localUrl, setLocalUrl] = useState(url);
|
||
const wrapperRef = useRef<HTMLDivElement>(null);
|
||
|
||
// Image and video nodes store their URL in `src` and autofocus the input.
|
||
// Embeds use `href` and do not autofocus, as it made the editor scroll to
|
||
// top unexpectedly–leaving that out for now
|
||
const isSrcNode = node?.type.name === "image" || node?.type.name === "video";
|
||
|
||
const moveSelectionToEnd = useCallback(() => {
|
||
const { state, dispatch } = view;
|
||
const nextSelection = Selection.findFrom(
|
||
state.tr.doc.resolve(state.selection.from),
|
||
1,
|
||
true
|
||
);
|
||
|
||
const selection = nextSelection ?? TextSelection.create(state.tr.doc, 0);
|
||
dispatch(state.tr.setSelection(selection));
|
||
view.focus();
|
||
}, [view]);
|
||
|
||
const openLink = useCallback(() => {
|
||
window.open(url, "_blank");
|
||
}, [url]);
|
||
|
||
const remove = useCallback(() => {
|
||
const { state, dispatch } = view;
|
||
dispatch(state.tr.deleteSelection());
|
||
onLinkRemove();
|
||
}, [view, onLinkRemove]);
|
||
|
||
const update = useCallback(() => {
|
||
const { state } = view;
|
||
const hrefType = isSrcNode ? "src" : "href";
|
||
const tr = state.tr.setNodeMarkup(state.selection.from, undefined, {
|
||
...node?.attrs,
|
||
[hrefType]: localUrl,
|
||
});
|
||
|
||
view.dispatch(tr);
|
||
moveSelectionToEnd();
|
||
onLinkUpdate();
|
||
}, [localUrl, node, isSrcNode, view, moveSelectionToEnd, onLinkUpdate]);
|
||
|
||
useOnClickOutside(wrapperRef, onClickOutside);
|
||
|
||
const handleKeyDown = useCallback(
|
||
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
||
if (event.nativeEvent.isComposing) {
|
||
return;
|
||
}
|
||
|
||
switch (event.key) {
|
||
case "Enter": {
|
||
event.preventDefault();
|
||
update();
|
||
return;
|
||
}
|
||
|
||
case "Escape": {
|
||
event.preventDefault();
|
||
moveSelectionToEnd();
|
||
onEscape();
|
||
return;
|
||
}
|
||
}
|
||
},
|
||
[update, moveSelectionToEnd, onEscape]
|
||
);
|
||
|
||
if (!node) {
|
||
return null;
|
||
}
|
||
|
||
return (
|
||
<Wrapper ref={wrapperRef}>
|
||
<Input
|
||
autoFocus={isSrcNode}
|
||
value={localUrl}
|
||
placeholder={`${t("Paste a link")}…`}
|
||
onChange={(e) => setLocalUrl(e.target.value)}
|
||
onKeyDown={handleKeyDown}
|
||
readOnly={!view.editable}
|
||
/>
|
||
{view.editable && node.type.name === "embed" && (
|
||
<>
|
||
<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}>
|
||
<OpenIcon />
|
||
</ToolbarButton>
|
||
</Tooltip>
|
||
{view.editable && (
|
||
<Tooltip
|
||
content={
|
||
node.type.name === "embed"
|
||
? t("Delete embed")
|
||
: node.type.name === "video"
|
||
? t("Delete video")
|
||
: t("Delete image")
|
||
}
|
||
>
|
||
<ToolbarButton onClick={remove}>
|
||
<TrashIcon />
|
||
</ToolbarButton>
|
||
</Tooltip>
|
||
)}
|
||
</Wrapper>
|
||
);
|
||
}
|
||
|
||
const DimensionGroup = styled.div`
|
||
display: flex;
|
||
align-items: center;
|
||
`;
|
||
|
||
const Wrapper = styled(Flex)`
|
||
pointer-events: all;
|
||
gap: 6px;
|
||
padding: 6px;
|
||
min-width: 350px;
|
||
`;
|