@atlaskit/editor-plugin-toolbar
Version:
Toolbar plugin for @atlaskit/editor-core
188 lines (185 loc) • 6.8 kB
JavaScript
/* ElementToolbarPopup.tsx generated by @compiled/babel-plugin v3.0.4 */
import "./ElementToolbarPopup.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
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 const ELEMENT_TOOLBAR_TEST_ID = 'editor-element-toolbar';
const styles = {
scrollWrapper: "_kqswh2mm _1e0c1txw _4cvr1h6o _1ul9idpf",
scrollContainer: "_zulp1b66 _1e0c1txw _4cvr1h6o _1reo1wug _18m915vq _1fjgglyw"
};
const PopperContent = ({
activeElement,
children,
editorDom,
popperRef,
scrollable,
style,
target,
update,
zIndex
}) => {
const contentRef = useRef(null);
const setRefs = useCallback(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(() => {
var _target$ownerDocument;
const ResizeObserverConstructor = (_target$ownerDocument = target.ownerDocument.defaultView) === null || _target$ownerDocument === void 0 ? void 0 : _target$ownerDocument.ResizeObserver;
if (!ResizeObserverConstructor) {
return;
}
const scheduleUpdate = rafSchedule(update);
const observer = new ResizeObserverConstructor(() => scheduleUpdate());
[target, editorDom, contentRef.current].forEach(element => {
if (element) {
observer.observe(element);
}
});
return () => {
observer.disconnect();
scheduleUpdate.cancel();
};
}, [target, editorDom, update]);
// Reposition when the element changes, e.g. a table gets a new row
const isFirstRender = useRef(true);
useEffect(() => {
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: {
...style,
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 const ElementToolbarPopup = ({
actionSubjectId,
children,
getDomRef,
label,
offset = [0, 12],
scrollable,
zIndex
}) => {
var _getDomRef, _getDocument;
const intl = useIntl();
const {
editorView
} = useEditorToolbar();
const activeElement = useElementToolbarContext();
const {
popupsMountPoint,
popupsBoundariesElement,
fireAnalyticsEvent
} = useToolbarUI();
const scrollContainerRef = useRef(null);
const modifiers = useMemo(() => {
const boundary = popupsBoundariesElement !== null && popupsBoundariesElement !== void 0 ? popupsBoundariesElement : 'clippingParents';
return [{
name: 'flip',
options: {
boundary
}
},
// Keep the toolbar in view while the element scrolls, like the legacy `stick` option
{
name: 'preventOverflow',
options: {
boundary,
altAxis: true,
tether: false
}
}];
}, [popupsBoundariesElement]);
const target = editorView && ((_getDomRef = getDomRef === null || getDomRef === void 0 ? void 0 : getDomRef(editorView)) !== null && _getDomRef !== void 0 ? _getDomRef : getDomRefFromSelection(editorView));
const mountPoint = popupsMountPoint !== null && popupsMountPoint !== void 0 ? popupsMountPoint : (_getDocument = getDocument()) === null || _getDocument === void 0 ? void 0 : _getDocument.body;
if (!activeElement || !editorView || !target || !mountPoint) {
return null;
}
const scrollButton = side => /*#__PURE__*/React.createElement(ScrollButton, {
intl: intl,
scrollContainerRef: scrollContainerRef,
node: activeElement.node,
disabled: false,
side: side
});
const 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
}, ({
ref,
style,
update
}) => /*#__PURE__*/React.createElement(PopperContent, {
popperRef: ref,
style: style,
update: update,
activeElement: activeElement,
editorDom: editorView.dom,
target: target,
scrollable: scrollable,
zIndex: zIndex
}, toolbar)), mountPoint));
};