UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

192 lines (189 loc) • 8.36 kB
/* ElementToolbarPopup.tsx generated by @compiled/babel-plugin v3.0.4 */ import _defineProperty from "@babel/runtime/helpers/defineProperty"; import "./ElementToolbarPopup.compiled.css"; import { ax, ix } from "@compiled/react/runtime"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { useCallback, useEffect, useMemo, useRef } from 'react'; import { createPortal } from 'react-dom'; import camelCase from 'lodash/camelCase'; import rafSchedule from 'raf-schd'; import { useIntl } from 'react-intl'; import { getDocument } from '@atlaskit/browser-apis'; import { ACTION_SUBJECT } from '@atlaskit/editor-common/analytics/types/enums'; import Announcer from '@atlaskit/editor-common/announcer'; import { ErrorBoundary } from '@atlaskit/editor-common/error-boundary'; import messages from '@atlaskit/editor-common/floating-toolbar/messages'; import { useEditorToolbar } from '@atlaskit/editor-common/toolbar/context'; import { Toolbar } from '@atlaskit/editor-toolbar/toolbar'; import { useToolbarUI } from '@atlaskit/editor-toolbar/ui-context'; import { Popper } from '@atlaskit/popper/main'; import { Box } from '@atlaskit/primitives/compiled/box'; import { ScrollButton } from '../ScrollButton'; import { getDomRefFromSelection } from '../SelectionToolbar/utils'; import { useElementToolbarContext } from './element-toolbar-context'; export var ELEMENT_TOOLBAR_TEST_ID = 'editor-element-toolbar'; var styles = { scrollWrapper: "_kqswh2mm _1e0c1txw _4cvr1h6o _1ul9idpf", scrollContainer: "_zulp1b66 _1e0c1txw _4cvr1h6o _1reo1wug _18m915vq _1fjgglyw" }; var PopperContent = function PopperContent(_ref) { var activeElement = _ref.activeElement, children = _ref.children, editorDom = _ref.editorDom, popperRef = _ref.popperRef, scrollable = _ref.scrollable, style = _ref.style, target = _ref.target, update = _ref.update, zIndex = _ref.zIndex; var contentRef = useRef(null); var setRefs = useCallback(function (element) { contentRef.current = element; if (typeof popperRef === 'function') { popperRef(element); } else if (popperRef) { popperRef.current = element; } }, [popperRef]); // Popper only repositions on scroll and window resize. Like the legacy Popup, also reposition // when the element, the editor or the toolbar changes size, e.g. when the editor gets narrower. useEffect(function () { var _target$ownerDocument; var ResizeObserverConstructor = (_target$ownerDocument = target.ownerDocument.defaultView) === null || _target$ownerDocument === void 0 ? void 0 : _target$ownerDocument.ResizeObserver; if (!ResizeObserverConstructor) { return; } var scheduleUpdate = rafSchedule(update); var observer = new ResizeObserverConstructor(function () { return scheduleUpdate(); }); [target, editorDom, contentRef.current].forEach(function (element) { if (element) { observer.observe(element); } }); return function () { observer.disconnect(); scheduleUpdate.cancel(); }; }, [target, editorDom, update]); // Reposition when the element changes, e.g. a table gets a new row var isFirstRender = useRef(true); useEffect(function () { if (isFirstRender.current) { isFirstRender.current = false; return; } update(); }, [activeElement, update]); return /*#__PURE__*/React.createElement("div", { ref: setRefs // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Popper positions with inline styles , style: _objectSpread(_objectSpread({}, style), {}, { zIndex: zIndex, maxWidth: scrollable ? '100%' : undefined }) }, children); }; /** * Renders an element toolbar next to its node. Use it as the `component` of a toolbar root * registered with `element` options. It only renders inside the element toolbar host. * * Uses `@atlaskit/popper`, the positioning layer of `@atlaskit/popup`, because `@atlaskit/popup` * needs a trigger element and the toolbar is anchored to an editor node instead. */ export var ElementToolbarPopup = function ElementToolbarPopup(_ref2) { var _getDomRef, _getDocument; var actionSubjectId = _ref2.actionSubjectId, children = _ref2.children, getDomRef = _ref2.getDomRef, label = _ref2.label, _ref2$offset = _ref2.offset, offset = _ref2$offset === void 0 ? [0, 12] : _ref2$offset, scrollable = _ref2.scrollable, zIndex = _ref2.zIndex; var intl = useIntl(); var _useEditorToolbar = useEditorToolbar(), editorView = _useEditorToolbar.editorView; var activeElement = useElementToolbarContext(); var _useToolbarUI = useToolbarUI(), popupsMountPoint = _useToolbarUI.popupsMountPoint, popupsBoundariesElement = _useToolbarUI.popupsBoundariesElement, fireAnalyticsEvent = _useToolbarUI.fireAnalyticsEvent; var scrollContainerRef = useRef(null); var modifiers = useMemo(function () { var boundary = popupsBoundariesElement !== null && popupsBoundariesElement !== void 0 ? popupsBoundariesElement : 'clippingParents'; return [{ name: 'flip', options: { boundary: boundary } }, // Keep the toolbar in view while the element scrolls, like the legacy `stick` option { name: 'preventOverflow', options: { boundary: boundary, altAxis: true, tether: false } }]; }, [popupsBoundariesElement]); var target = editorView && ((_getDomRef = getDomRef === null || getDomRef === void 0 ? void 0 : getDomRef(editorView)) !== null && _getDomRef !== void 0 ? _getDomRef : getDomRefFromSelection(editorView)); var mountPoint = popupsMountPoint !== null && popupsMountPoint !== void 0 ? popupsMountPoint : (_getDocument = getDocument()) === null || _getDocument === void 0 ? void 0 : _getDocument.body; if (!activeElement || !editorView || !target || !mountPoint) { return null; } var scrollButton = function scrollButton(side) { return /*#__PURE__*/React.createElement(ScrollButton, { intl: intl, scrollContainerRef: scrollContainerRef, node: activeElement.node, disabled: false, side: side }); }; var toolbar = /*#__PURE__*/React.createElement(Toolbar, { label: label, testId: ELEMENT_TOOLBAR_TEST_ID, actionSubjectId: actionSubjectId }, /*#__PURE__*/React.createElement(Announcer, { text: intl.formatMessage(messages.floatingToolbarAnnouncer), delay: 250 }), scrollable ? /*#__PURE__*/React.createElement(Box, { xcss: styles.scrollWrapper }, scrollButton('left'), /*#__PURE__*/React.createElement(Box, { ref: scrollContainerRef, xcss: styles.scrollContainer }, children), scrollButton('right')) : children); return /*#__PURE__*/React.createElement(ErrorBoundary, { component: ACTION_SUBJECT.FLOATING_TOOLBAR_PLUGIN, componentId: camelCase(label) // The toolbar UI context types payloads loosely. The host forwards them to the analytics plugin. , dispatchAnalyticsEvent: fireAnalyticsEvent, fallbackComponent: null }, /*#__PURE__*/createPortal( /*#__PURE__*/React.createElement(Popper, { referenceElement: target, placement: "bottom", offset: offset, strategy: popupsMountPoint ? 'absolute' : 'fixed', modifiers: modifiers }, function (_ref3) { var ref = _ref3.ref, style = _ref3.style, update = _ref3.update; return /*#__PURE__*/React.createElement(PopperContent, { popperRef: ref, style: style, update: update, activeElement: activeElement, editorDom: editorView.dom, target: target, scrollable: scrollable, zIndex: zIndex }, toolbar); }), mountPoint)); };