UNPKG

tldraw

Version:

A tiny little drawing editor.

193 lines (192 loc) • 9.01 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var OverflowingToolbar_exports = {}; __export(OverflowingToolbar_exports, { IsInOverflowContext: () => IsInOverflowContext, OverflowingToolbar: () => OverflowingToolbar, isActiveTLUiToolItem: () => isActiveTLUiToolItem }); module.exports = __toCommonJS(OverflowingToolbar_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_editor = require("@tldraw/editor"); var import_classnames = __toESM(require("classnames")); var import_react = require("react"); var import_constants = require("../../constants"); var import_breakpoints = require("../../context/breakpoints"); var import_useKeyboardShortcuts = require("../../hooks/useKeyboardShortcuts"); var import_useTranslation = require("../../hooks/useTranslation/useTranslation"); var import_TldrawUiButtonIcon = require("../primitives/Button/TldrawUiButtonIcon"); var import_TldrawUiPopover = require("../primitives/TldrawUiPopover"); var import_TldrawUiToolbar = require("../primitives/TldrawUiToolbar"); var import_TldrawUiMenuContext = require("../primitives/menus/TldrawUiMenuContext"); const IsInOverflowContext = (0, import_react.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 }) { const editor = (0, import_editor.useEditor)(); const id = (0, import_editor.useUniqueSafeId)(); const breakpoint = (0, import_breakpoints.useBreakpoint)(); const msg = (0, import_useTranslation.useTranslation)(); const rButtons = (0, import_react.useRef)([]); const [isOpen, setIsOpen] = (0, import_react.useState)(false); const overflowIndex = Math.min(8, 5 + breakpoint); const [totalItems, setTotalItems] = (0, import_react.useState)(0); const mainToolsRef = (0, import_react.useRef)(null); const [lastActiveOverflowItem, setLastActiveOverflowItem] = (0, import_react.useState)(null); const css = (0, import_react.useMemo)(() => { const activeCss = lastActiveOverflowItem ? `:not([data-value="${lastActiveOverflowItem}"])` : ""; return ` #${id}_main > *:nth-child(n + ${overflowIndex + (lastActiveOverflowItem ? 1 : 2)})${activeCss} { display: none; } #${id}_more > *:nth-child(-n + ${overflowIndex}) { display: none; } #${id}_more > *:nth-child(-n + ${overflowIndex + 4}) { margin-top: 0; } `; }, [lastActiveOverflowItem, id, overflowIndex]); const onDomUpdate = (0, import_editor.useEvent)(() => { if (!mainToolsRef.current) return; const children2 = Array.from(mainToolsRef.current.children); setTotalItems(children2.length); const lastActiveElementIdx = children2.findIndex( (el) => el.getAttribute("data-value") === lastActiveOverflowItem ); if (lastActiveElementIdx <= overflowIndex) { setLastActiveOverflowItem(null); } const activeElementIdx = Array.from(mainToolsRef.current.children).findIndex( (el) => el.getAttribute("aria-pressed") === "true" ); if (activeElementIdx === -1) return; if (activeElementIdx >= overflowIndex) { setLastActiveOverflowItem(children2[activeElementIdx].getAttribute("data-value")); } rButtons.current = Array.from(mainToolsRef.current?.children ?? []).filter( (el) => { if (!(el instanceof HTMLElement)) return false; if (el.tagName.toLowerCase() !== "button") return false; return !!(el.offsetWidth || el.offsetHeight); } ); }); (0, import_react.useLayoutEffect)(() => { onDomUpdate(); }); (0, import_react.useLayoutEffect)(() => { if (!mainToolsRef.current) return; const mutationObserver = new MutationObserver(onDomUpdate); mutationObserver.observe(mainToolsRef.current, { childList: true, subtree: true, attributeFilter: ["data-value", "aria-pressed"] }); return () => { mutationObserver.disconnect(); }; }, [onDomUpdate]); (0, import_react.useEffect)(() => { if (!editor.options.enableToolbarKeyboardShortcuts) return; function handleKeyDown(event) { if ((0, import_useKeyboardShortcuts.areShortcutsDisabled)(editor) || (0, import_editor.activeElementShouldCaptureKeys)( true /* allow buttons */ )) return; if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return; const index = NUMBERED_SHORTCUT_KEYS[event.key]; if (typeof index === "number") { (0, import_editor.preventDefault)(event); rButtons.current[index]?.click(); } } document.addEventListener("keydown", handleKeyDown); return () => { document.removeEventListener("keydown", handleKeyDown); }; }, [editor]); const popoverId = "toolbar overflow"; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { nonce: editor.options.nonce, children: css }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_TldrawUiToolbar.TldrawUiToolbar, { className: (0, import_classnames.default)("tlui-toolbar__tools", { "tlui-toolbar__tools__mobile": breakpoint < import_constants.PORTRAIT_BREAKPOINT.TABLET_SM }), label: msg("tool-panel.title"), children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { id: `${id}_main`, ref: mainToolsRef, className: "tlui-toolbar__tools__list", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiMenuContext.TldrawUiMenuContextProvider, { type: "toolbar", sourceId: "toolbar", children }) }), totalItems > overflowIndex + 1 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(IsInOverflowContext.Provider, { value: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_TldrawUiPopover.TldrawUiPopover, { id: popoverId, open: isOpen, onOpenChange: setIsOpen, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiPopover.TldrawUiPopoverTrigger, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiToolbar.TldrawUiToolbarButton, { title: msg("tool-panel.more"), type: "tool", className: "tlui-toolbar__overflow", "data-testid": "tools.more-button", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiButtonIcon.TldrawUiButtonIcon, { icon: "chevron-up" }) } ) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiPopover.TldrawUiPopoverContent, { side: "top", align: "center", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiToolbar.TldrawUiToolbar, { className: "tlui-buttons__grid", "data-testid": "tools.more-content", label: msg("tool-panel.more"), id: `${id}_more`, onClick: () => { import_editor.tlmenus.deleteOpenMenu(popoverId, editor.contextId); setIsOpen(false); }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiMenuContext.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; }; //# sourceMappingURL=OverflowingToolbar.js.map