UNPKG

tldraw

Version:

A tiny little drawing editor.

285 lines (284 loc) • 10.8 kB
import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import { activeElementShouldCaptureKeys, assert, modulate, preventDefault, tlmenus, useEditor, useEvent, useUniqueSafeId } from "@tldraw/editor"; import classNames from "classnames"; import { createContext, useEffect, useLayoutEffect, useRef, useState } from "react"; import { PORTRAIT_BREAKPOINT } from "../../constants.mjs"; import { useBreakpoint } from "../../context/breakpoints.mjs"; import { areShortcutsDisabled } from "../../hooks/useKeyboardShortcuts.mjs"; import { useTranslation } from "../../hooks/useTranslation/useTranslation.mjs"; import { TldrawUiButtonIcon } from "../primitives/Button/TldrawUiButtonIcon.mjs"; import { TldrawUiPopover, TldrawUiPopoverContent, TldrawUiPopoverTrigger } from "../primitives/TldrawUiPopover.mjs"; import { TldrawUiToolbar, TldrawUiToolbarButton } from "../primitives/TldrawUiToolbar.mjs"; import { TldrawUiColumn, TldrawUiRow } from "../primitives/layout.mjs"; import { TldrawUiMenuContextProvider } from "../primitives/menus/TldrawUiMenuContext.mjs"; const IsInOverflowContext = createContext(false); const NUMBERED_SHORTCUT_KEYS = { "1": 0, "2": 1, "3": 2, "4": 3, "5": 4, "6": 5, "7": 6, "8": 7, "9": 8, "0": 9 }; function OverflowingToolbar({ children, orientation, sizingParentClassName, minItems, minSizePx, maxItems, maxSizePx }) { const editor = useEditor(); const id = useUniqueSafeId(); const breakpoint = useBreakpoint(); const msg = useTranslation(); const rButtons = useRef([]); const [isOpen, setIsOpen] = useState(false); const mainToolsRef = useRef(null); const [overflowTools, setOverflowTools] = useState(null); const [lastActiveOverflowItem, setLastActiveOverflowItem] = useState(null); const [shouldShowOverflow, setShouldShowOverflow] = useState(false); const onDomUpdate = useEvent(() => { if (!mainToolsRef.current) return; const sizeProp = orientation === "horizontal" ? "offsetWidth" : "offsetHeight"; const mainItems = collectItems(mainToolsRef.current.children); const overflowItems = overflowTools ? collectItems(overflowTools.children) : null; function collectItems(collection) { const items = []; for (const child of collection) { if (child.classList.contains("tlui-main-toolbar__group")) { items.push({ type: "group", items: collectItems(child.children), element: child }); } else if (!child.hasAttribute("data-radix-popper-content-wrapper")) { items.push({ type: "item", element: child }); } } return items; } const sizingParent = findParentWithClassName(mainToolsRef.current, sizingParentClassName); const size = sizingParent[sizeProp]; const itemsToShow = Math.floor( modulate(size, [minSizePx, maxSizePx], [minItems, maxItems], true) ); let mainItemCount = 0; let newActiveOverflowItem = null; let shouldInvalidateLastActiveOverflowItem = false; const numberedButtons = []; function visitItems(mainItems2, overflowItems2) { if (overflowItems2) assert(mainItems2.length === overflowItems2.length); let didShowAnyInMain = false; let didShowAnyInOverflow2 = false; for (let i = 0; i < mainItems2.length; i++) { const mainItem = mainItems2[i]; const overflowItem = overflowItems2?.[i]; if (mainItem.type === "item") { const isLastActiveOverflowItem = mainItem.element.getAttribute("data-value") === lastActiveOverflowItem; let shouldShowInMain; if (lastActiveOverflowItem) { shouldShowInMain = mainItemCount < itemsToShow || isLastActiveOverflowItem; } else { shouldShowInMain = mainItemCount <= itemsToShow; } const shouldShowInOverflow = mainItemCount >= itemsToShow; didShowAnyInMain ||= shouldShowInMain; didShowAnyInOverflow2 ||= shouldShowInOverflow; setAttribute( mainItem.element, "data-toolbar-visible", shouldShowInMain ? "true" : "false" ); if (overflowItem) { assert(overflowItem.type === "item"); setAttribute( overflowItem.element, "data-toolbar-visible", shouldShowInOverflow ? "true" : "false" ); } if (shouldShowInOverflow && mainItem.element.getAttribute("aria-pressed") === "true") { newActiveOverflowItem = mainItem.element.getAttribute("data-value"); } if (shouldShowInMain && mainItem.element.tagName === "BUTTON") { numberedButtons.push(mainItem.element); } if (!shouldShowInOverflow && isLastActiveOverflowItem) { shouldInvalidateLastActiveOverflowItem = true; } mainItemCount++; } else { let result, overflowGroup; if (overflowItem) { assert(overflowItem.type === "group"); overflowGroup = overflowItem; result = visitItems(mainItem.items, overflowGroup.items); } else { result = visitItems(mainItem.items, null); } didShowAnyInMain ||= result.didShowAnyInMain; didShowAnyInOverflow2 ||= result.didShowAnyInOverflow; setAttribute( mainItem.element, "data-toolbar-visible", result.didShowAnyInMain ? "true" : "false" ); if (overflowGroup) { setAttribute( overflowGroup.element, "data-toolbar-visible", result.didShowAnyInOverflow ? "true" : "false" ); } } } return { didShowAnyInMain, didShowAnyInOverflow: didShowAnyInOverflow2 }; } const { didShowAnyInOverflow } = visitItems(mainItems, overflowItems); setShouldShowOverflow(didShowAnyInOverflow); if (newActiveOverflowItem) { setLastActiveOverflowItem(newActiveOverflowItem); } else if (shouldInvalidateLastActiveOverflowItem) { setLastActiveOverflowItem(null); } rButtons.current = numberedButtons; }); useLayoutEffect(() => { onDomUpdate(); }); useLayoutEffect(() => { if (!mainToolsRef.current) return; const mutationObserver = new MutationObserver(onDomUpdate); mutationObserver.observe(mainToolsRef.current, { childList: true, subtree: true, attributes: true, characterData: true }); const sizingParent = findParentWithClassName(mainToolsRef.current, sizingParentClassName); const resizeObserver = new ResizeObserver(onDomUpdate); resizeObserver.observe(sizingParent); return () => { mutationObserver.disconnect(); resizeObserver.disconnect(); }; }, [onDomUpdate, sizingParentClassName]); useEffect(() => { if (!editor.options.enableToolbarKeyboardShortcuts) return; function handleKeyDown(event) { if (areShortcutsDisabled(editor) || activeElementShouldCaptureKeys( false /* includeButton */ )) { return; } if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return; const index = NUMBERED_SHORTCUT_KEYS[event.key]; if (typeof index === "number") { preventDefault(event); rButtons.current[index]?.click(); } } document.addEventListener("keydown", handleKeyDown); return () => { document.removeEventListener("keydown", handleKeyDown); }; }, [editor]); const popoverId = "toolbar overflow"; const Layout = orientation === "horizontal" ? TldrawUiRow : TldrawUiColumn; return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs( TldrawUiToolbar, { orientation, className: classNames("tlui-main-toolbar__tools", { "tlui-main-toolbar__tools__mobile": breakpoint < PORTRAIT_BREAKPOINT.TABLET_SM }), label: msg("tool-panel.title"), children: [ /* @__PURE__ */ jsx(Layout, { id: `${id}_main`, ref: mainToolsRef, children: /* @__PURE__ */ jsx(TldrawUiMenuContextProvider, { type: "toolbar", sourceId: "toolbar", children }) }), shouldShowOverflow && /* @__PURE__ */ jsx(IsInOverflowContext.Provider, { value: true, children: /* @__PURE__ */ jsxs(TldrawUiPopover, { id: popoverId, open: isOpen, onOpenChange: setIsOpen, children: [ /* @__PURE__ */ jsx(TldrawUiPopoverTrigger, { children: /* @__PURE__ */ jsx( TldrawUiToolbarButton, { title: msg("tool-panel.more"), type: "tool", className: "tlui-main-toolbar__overflow", "data-testid": "tools.more-button", children: /* @__PURE__ */ jsx( TldrawUiButtonIcon, { icon: orientation === "horizontal" ? "chevron-up" : "chevron-right" } ) } ) }), /* @__PURE__ */ jsx( TldrawUiPopoverContent, { side: orientation === "horizontal" ? "top" : "right", align: orientation === "horizontal" ? "center" : "end", children: /* @__PURE__ */ jsx( TldrawUiToolbar, { orientation: "grid", className: "tlui-main-toolbar__overflow-content", ref: setOverflowTools, "data-testid": "tools.more-content", label: msg("tool-panel.more"), id: `${id}_more`, onClick: () => { tlmenus.deleteOpenMenu(popoverId, editor.contextId); setIsOpen(false); }, children: /* @__PURE__ */ jsx(TldrawUiMenuContextProvider, { type: "toolbar-overflow", sourceId: "toolbar", children }) } ) } ) ] }) }) ] } ) }); } const isActiveTLUiToolItem = (item, activeToolId, geoState) => { return item.meta?.geo ? activeToolId === "geo" && geoState === item.meta?.geo : activeToolId === item.id; }; function findParentWithClassName(startingElement, className) { let element = startingElement; while (element) { if (element.classList.contains(className)) { return element; } element = element.parentElement; } throw new Error("Could not find parent with class name " + className); } function setAttribute(element, name, value) { if (element.getAttribute(name) === value) return; element.setAttribute(name, value); } export { IsInOverflowContext, OverflowingToolbar, isActiveTLUiToolItem }; //# sourceMappingURL=OverflowingToolbar.mjs.map