UNPKG

@atlaskit/editor-plugin-media-insert

Version:

Media Insert plugin for @atlaskit/editor-core

152 lines (151 loc) 6.29 kB
import _extends from "@babel/runtime/helpers/extends"; import React from 'react'; import { useIntl } from 'react-intl'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { getDomRefFromSelection } from '@atlaskit/editor-common/get-dom-ref-from-selection'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks'; import { mediaInsertMessages } from '@atlaskit/editor-common/messages'; import { PlainOutsideClickTargetRefContext, Popup, withOuterListeners } from '@atlaskit/editor-common/ui'; import { akEditorFloatingDialogZIndex } from '@atlaskit/editor-shared-styles'; import { Box } from '@atlaskit/primitives/compiled'; import Tabs, { Tab, TabList, useTabPanel } from '@atlaskit/tabs'; import { useFocusEditor } from './hooks/use-focus-editor'; import { useUnholyAutofocus } from './hooks/use-unholy-autofocus'; import { LocalMedia } from './LocalMedia'; import { MediaFromURL } from './MediaFromURL'; import { MediaInsertWrapper } from './MediaInsertWrapper'; const PopupWithListeners = withOuterListeners(Popup); /** * A custom TabPanel that is non-focusable. */ const CustomTabPanel = ({ children }) => { const tabPanelAttributes = useTabPanel(); return ( /*#__PURE__*/ // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading React.createElement(Box, _extends({ paddingBlockEnd: "space.150" }, tabPanelAttributes, { tabIndex: -1 }), children) ); }; export const MediaInsertPicker = ({ api, editorView, dispatchAnalyticsEvent, popupsMountPoint, popupsBoundariesElement, popupsScrollableElement, closeMediaInsertPicker, insertMediaSingle, insertExternalMediaSingle, insertFile, isOnlyExternalLinks = false, customizedUrlValidation, customizedHelperMessage }) => { const { mediaProvider, isOpen, mountInfo } = useSharedPluginStateWithSelector(api, ['media', 'mediaInsert'], states => { var _states$mediaState, _states$mediaInsertSt, _states$mediaInsertSt2; return { mediaProvider: (_states$mediaState = states.mediaState) === null || _states$mediaState === void 0 ? void 0 : _states$mediaState.mediaProvider, isOpen: (_states$mediaInsertSt = states.mediaInsertState) === null || _states$mediaInsertSt === void 0 ? void 0 : _states$mediaInsertSt.isOpen, mountInfo: (_states$mediaInsertSt2 = states.mediaInsertState) === null || _states$mediaInsertSt2 === void 0 ? void 0 : _states$mediaInsertSt2.mountInfo }; }); let targetRef; let mountPoint; if (mountInfo) { targetRef = mountInfo.ref; mountPoint = mountInfo.mountPoint; } else { var _api$analytics; // If targetRef is undefined, target the selection in the editor targetRef = getDomRefFromSelection(editorView, ACTION_SUBJECT_ID.PICKER_MEDIA, api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions); mountPoint = popupsMountPoint; } const intl = useIntl(); const focusEditor = useFocusEditor({ editorView }); const { autofocusRef, onPositionCalculated } = useUnholyAutofocus(); if (!isOpen || !mediaProvider) { return null; } const handleClose = exitMethod => event => { event.preventDefault(); if (dispatchAnalyticsEvent) { const payload = { action: ACTION.CLOSED, actionSubject: ACTION_SUBJECT.PICKER, actionSubjectId: ACTION_SUBJECT_ID.PICKER_MEDIA, eventType: EVENT_TYPE.UI, attributes: { exitMethod } }; dispatchAnalyticsEvent(payload); } closeMediaInsertPicker(); focusEditor(); }; return /*#__PURE__*/React.createElement(PopupWithListeners, { ariaLabel: intl.formatMessage(mediaInsertMessages.mediaPickerPopupAriaLabel) // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , offset: [0, 12], target: targetRef, zIndex: akEditorFloatingDialogZIndex, fitHeight: 390, fitWidth: 340, mountTo: mountPoint, boundariesElement: popupsBoundariesElement, handleClickOutside: handleClose(INPUT_METHOD.MOUSE), handleEscapeKeydown: handleClose(INPUT_METHOD.KEYBOARD), scrollableElement: popupsScrollableElement, preventOverflow: true, onPositionCalculated: onPositionCalculated, focusTrap: true }, /*#__PURE__*/React.createElement(PlainOutsideClickTargetRefContext.Consumer, null, setOutsideClickTargetRef => /*#__PURE__*/React.createElement(MediaInsertWrapper, { ref: setOutsideClickTargetRef }, /*#__PURE__*/React.createElement(Tabs, { id: "media-insert-tab-navigation" }, /*#__PURE__*/React.createElement(Box, { paddingBlockEnd: "space.150" }, /*#__PURE__*/React.createElement(TabList, null, !isOnlyExternalLinks && /*#__PURE__*/React.createElement(Tab, null, intl.formatMessage(mediaInsertMessages.fileTabTitle)), /*#__PURE__*/React.createElement(Tab, null, intl.formatMessage(mediaInsertMessages.linkTabTitle)))), !isOnlyExternalLinks && /*#__PURE__*/React.createElement(CustomTabPanel, null, /*#__PURE__*/React.createElement(LocalMedia, { ref: autofocusRef, mediaProvider: mediaProvider // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , closeMediaInsertPicker: () => { closeMediaInsertPicker(); focusEditor(); }, dispatchAnalyticsEvent: dispatchAnalyticsEvent, insertFile: insertFile })), /*#__PURE__*/React.createElement(CustomTabPanel, null, /*#__PURE__*/React.createElement(MediaFromURL, { mediaProvider: mediaProvider, dispatchAnalyticsEvent: dispatchAnalyticsEvent // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , closeMediaInsertPicker: () => { closeMediaInsertPicker(); focusEditor(); }, insertMediaSingle: insertMediaSingle, insertExternalMediaSingle: insertExternalMediaSingle, isOnlyExternalLinks: isOnlyExternalLinks, customizedUrlValidation: customizedUrlValidation, customizedHelperMessage: customizedHelperMessage })))))); };