mirror of
https://github.com/outline/outline.git
synced 2026-11-26 07:33:19 +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
326 lines
9.3 KiB
TypeScript
326 lines
9.3 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import styled from "styled-components";
|
||
import { breakpoint } from "@shared/styles/breakpoint";
|
||
import * as Toolbar from "@radix-ui/react-toolbar";
|
||
import { closeHistory } from "@shared/editor/lib/closeHistory";
|
||
import { MenuItemGroup, type MenuItem } from "@shared/editor/types";
|
||
import { hideScrollbars, s } from "@shared/styles";
|
||
import { UndoIcon } from "~/components/Icons/UndoIcon";
|
||
import { TooltipProvider } from "~/components/TooltipContext";
|
||
import useMobile from "~/hooks/useMobile";
|
||
import type { MenuItem as TMenuItem } from "~/types";
|
||
import { mapMenuItems } from "../menus/mapMenuItems";
|
||
import { useEditor } from "./EditorContext";
|
||
import { MediaDimension } from "./MediaDimension";
|
||
import ToolbarButton from "./ToolbarButton";
|
||
import ToolbarSeparator from "./ToolbarSeparator";
|
||
import Tooltip from "./Tooltip";
|
||
import { toMenuItems } from "~/components/Menu/transformer";
|
||
import {
|
||
MenuFilter,
|
||
MenuFilterEmpty,
|
||
useMenuFilter,
|
||
} from "~/components/Menu/MenuFilter";
|
||
import { MenuContent } from "~/components/primitives/Menu";
|
||
import { MenuProvider } from "~/components/primitives/Menu/MenuContext";
|
||
import { Menu, MenuTrigger } from "~/components/primitives/Menu";
|
||
import { useTranslation } from "react-i18next";
|
||
import EventBoundary from "@shared/components/EventBoundary";
|
||
|
||
type Props = {
|
||
items: MenuItem[];
|
||
/** Whether to pin an undo button to the start of the toolbar. */
|
||
showUndo?: boolean;
|
||
};
|
||
|
||
type ToolbarDropdownProps = {
|
||
active: boolean;
|
||
item: MenuItem;
|
||
tooltip?: string;
|
||
shortcut?: string;
|
||
};
|
||
|
||
/**
|
||
* Renders a dropdown menu in the floating toolbar.
|
||
*/
|
||
function ToolbarDropdown(props: ToolbarDropdownProps) {
|
||
const { commands, view } = useEditor();
|
||
const { t } = useTranslation();
|
||
const { item, shortcut, tooltip } = props;
|
||
const { state } = view;
|
||
const [isOpen, setIsOpen] = useState<boolean>(false);
|
||
const {
|
||
query,
|
||
setQuery,
|
||
handleKeyDownCapture,
|
||
handleEscapeKeyDown,
|
||
reset: resetFilter,
|
||
} = useMenuFilter();
|
||
|
||
const handleOpenChange = useCallback(
|
||
(open: boolean) => {
|
||
setIsOpen(open);
|
||
if (open) {
|
||
resetFilter();
|
||
}
|
||
},
|
||
[resetFilter]
|
||
);
|
||
|
||
const items: TMenuItem[] = useMemo(() => {
|
||
if (!isOpen) {
|
||
return [];
|
||
}
|
||
|
||
const resolvedItemChildren =
|
||
typeof item.children === "function" ? item.children() : item.children;
|
||
return resolvedItemChildren
|
||
? mapMenuItems(resolvedItemChildren, commands, view, state)
|
||
: [];
|
||
// Menu items are resolved against the editor state at the moment the menu
|
||
// opens, recomputing on every transaction would rebuild the open menu.
|
||
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [isOpen, commands]);
|
||
|
||
const handleCloseAutoFocus = useCallback((ev: Event) => {
|
||
ev.stopImmediatePropagation();
|
||
}, []);
|
||
|
||
return (
|
||
<Tooltip shortcut={shortcut} content={tooltip} disabled={isOpen}>
|
||
<MenuProvider variant="dropdown">
|
||
<Menu open={isOpen} onOpenChange={handleOpenChange}>
|
||
<MenuTrigger>
|
||
<ToolbarButton
|
||
data-group={item.group}
|
||
aria-label={item.label ? undefined : item.tooltip}
|
||
disabled={item.disabled}
|
||
>
|
||
{item.label && <Label>{item.label}</Label>}
|
||
{item.icon}
|
||
</ToolbarButton>
|
||
</MenuTrigger>
|
||
<MenuContent
|
||
align="end"
|
||
aria-label={item.tooltip || t("More options")}
|
||
onCloseAutoFocus={handleCloseAutoFocus}
|
||
onEscapeKeyDown={handleEscapeKeyDown}
|
||
onKeyDownCapture={handleKeyDownCapture}
|
||
>
|
||
<MenuFilter query={query} onChange={setQuery} />
|
||
<EventBoundary>
|
||
{toMenuItems(items, query) ??
|
||
(query ? <MenuFilterEmpty /> : null)}
|
||
</EventBoundary>
|
||
</MenuContent>
|
||
</Menu>
|
||
</MenuProvider>
|
||
</Tooltip>
|
||
);
|
||
}
|
||
|
||
function ToolbarMenu(props: Props) {
|
||
const { commands, view } = useEditor();
|
||
const { t } = useTranslation();
|
||
const { items, showUndo } = props;
|
||
const { state } = view;
|
||
const isMobile = useMobile();
|
||
const wrapperRef = useRef<HTMLDivElement>(null);
|
||
const hasSelection = !state.selection.empty;
|
||
|
||
// On mobile the toolbar is a single bar holding both the text and the block
|
||
// controls, wider than the screen. It scrolls to the group that suits the
|
||
// selection – text controls once there is a selection, block controls for a
|
||
// bare cursor – leaving the other group one swipe away.
|
||
const didScroll = useRef(false);
|
||
useEffect(() => {
|
||
const wrapper = wrapperRef.current;
|
||
if (!isMobile || !wrapper) {
|
||
return;
|
||
}
|
||
|
||
const target = wrapper.querySelector(
|
||
`[data-group="${hasSelection ? MenuItemGroup.inline : MenuItemGroup.block}"]`
|
||
);
|
||
target?.scrollIntoView({
|
||
block: "nearest",
|
||
inline: "start",
|
||
behavior: didScroll.current ? "smooth" : "auto",
|
||
});
|
||
didScroll.current = true;
|
||
}, [isMobile, hasSelection]);
|
||
|
||
const handleClick = (item: MenuItem) => () => {
|
||
if (!item.name) {
|
||
return;
|
||
}
|
||
|
||
// if item has an associated onClick prop, run it
|
||
if (item.onClick) {
|
||
item.onClick();
|
||
return;
|
||
}
|
||
|
||
// otherwise, run the associated editor command
|
||
closeHistory(view);
|
||
commands[item.name](
|
||
typeof item.attrs === "function" ? item.attrs(state) : item.attrs
|
||
);
|
||
closeHistory(view);
|
||
};
|
||
|
||
const handleUndo = () => {
|
||
commands.undo();
|
||
};
|
||
|
||
const content = (
|
||
<FlexibleWrapper ref={wrapperRef} $pinned={showUndo}>
|
||
{items.map((item, index) => {
|
||
if (item.name === "separator" && item.visible !== false) {
|
||
// The mobile bar spaces its buttons evenly instead, as dividers
|
||
// would eat into the room for them.
|
||
return isMobile ? null : <ToolbarSeparator key={index} />;
|
||
}
|
||
if (item.visible === false || (!item.skipIcon && !item.icon)) {
|
||
return null;
|
||
}
|
||
const isActive = item.active ? item.active(state) : false;
|
||
|
||
if (item.children) {
|
||
return (
|
||
<ToolbarDropdown
|
||
key={index}
|
||
active={isActive && !item.label}
|
||
item={item}
|
||
tooltip={item.label === item.tooltip ? undefined : item.tooltip}
|
||
shortcut={item.shortcut}
|
||
/>
|
||
);
|
||
}
|
||
|
||
if (item.name === "dimensions") {
|
||
return <MediaDimension key={index} tooltip={item.tooltip} />;
|
||
}
|
||
|
||
return (
|
||
<Tooltip
|
||
key={index}
|
||
shortcut={item.shortcut}
|
||
content={item.label === item.tooltip ? undefined : item.tooltip}
|
||
>
|
||
<Toolbar.Button asChild>
|
||
<ToolbarButton
|
||
data-group={item.group}
|
||
onClick={handleClick(item)}
|
||
active={isActive && !item.label}
|
||
aria-label={item.label ? undefined : item.tooltip}
|
||
disabled={item.disabled}
|
||
>
|
||
{item.label && <Label>{item.label}</Label>}
|
||
{item.icon}
|
||
</ToolbarButton>
|
||
</Toolbar.Button>
|
||
</Tooltip>
|
||
);
|
||
})}
|
||
</FlexibleWrapper>
|
||
);
|
||
|
||
return (
|
||
<TooltipProvider>
|
||
<Toolbar.Root asChild>
|
||
{showUndo ? (
|
||
<PinnedWrapper>
|
||
<Toolbar.Button asChild>
|
||
<PinnedButton onClick={handleUndo} aria-label={t("Undo")}>
|
||
<UndoIcon />
|
||
</PinnedButton>
|
||
</Toolbar.Button>
|
||
<PinnedSeparator />
|
||
{content}
|
||
</PinnedWrapper>
|
||
) : (
|
||
content
|
||
)}
|
||
</Toolbar.Root>
|
||
</TooltipProvider>
|
||
);
|
||
}
|
||
|
||
const FlexibleWrapper = styled.div<{ $pinned?: boolean }>`
|
||
color: ${s("textSecondary")};
|
||
overflow: hidden;
|
||
display: flex;
|
||
gap: 6px;
|
||
padding: 6px;
|
||
|
||
${breakpoint("mobile", "tablet")`
|
||
justify-content: space-evenly;
|
||
align-items: center;
|
||
overflow-x: auto;
|
||
scroll-padding-inline-start: 4px;
|
||
padding: 0;
|
||
|
||
// Six buttons and half of the seventh fill the width – the half button is
|
||
// what tells the user that the bar scrolls. Whatever is left over becomes
|
||
// the space between them, so the first and last sit hard against the ends
|
||
// and the bar keeps an even margin all round.
|
||
gap: 0 calc((100% - 20px) / 6 - 40px);
|
||
|
||
> * {
|
||
display: flex;
|
||
justify-content: center;
|
||
flex: 0 0 auto;
|
||
min-width: 40px;
|
||
}
|
||
|
||
> * > button {
|
||
min-width: 40px;
|
||
height: 40px;
|
||
justify-content: center;
|
||
border-radius: 20px;
|
||
}
|
||
|
||
${hideScrollbars()}
|
||
`}
|
||
|
||
// A pinned button takes the place of one of the six. The space after the
|
||
// separator is padding so buttons scroll right up to the separator.
|
||
${(props) =>
|
||
props.$pinned &&
|
||
breakpoint("mobile", "tablet")`
|
||
flex: 1;
|
||
gap: 0 calc((100% - 20px) / 5 - 40px);
|
||
padding-inline-start: 8px;
|
||
scroll-padding-inline-start: 8px;
|
||
`}
|
||
`;
|
||
|
||
const PinnedWrapper = styled.div`
|
||
display: flex;
|
||
align-items: center;
|
||
min-width: 0;
|
||
`;
|
||
|
||
const PinnedButton = styled(ToolbarButton)`
|
||
flex-shrink: 0;
|
||
min-width: 40px;
|
||
height: 40px;
|
||
justify-content: center;
|
||
border-radius: 20px;
|
||
`;
|
||
|
||
const PinnedSeparator = styled(ToolbarSeparator)`
|
||
flex-shrink: 0;
|
||
height: 24px;
|
||
margin: 0 0 0 4px;
|
||
`;
|
||
|
||
const Label = styled.span`
|
||
font-size: 15px;
|
||
font-weight: 500;
|
||
color: ${s("text")};
|
||
`;
|
||
|
||
export default ToolbarMenu;
|