UNPKG

@atlaskit/editor-plugin-media-insert

Version:

Media Insert plugin for @atlaskit/editor-core

153 lines (152 loc) 7.28 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'; var PopupWithListeners = withOuterListeners(Popup); /** * A custom TabPanel that is non-focusable. */ var CustomTabPanel = function CustomTabPanel(_ref) { var children = _ref.children; var 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 var MediaInsertPicker = function MediaInsertPicker(_ref2) { var api = _ref2.api, editorView = _ref2.editorView, dispatchAnalyticsEvent = _ref2.dispatchAnalyticsEvent, popupsMountPoint = _ref2.popupsMountPoint, popupsBoundariesElement = _ref2.popupsBoundariesElement, popupsScrollableElement = _ref2.popupsScrollableElement, _closeMediaInsertPicker = _ref2.closeMediaInsertPicker, insertMediaSingle = _ref2.insertMediaSingle, insertExternalMediaSingle = _ref2.insertExternalMediaSingle, insertFile = _ref2.insertFile, _ref2$isOnlyExternalL = _ref2.isOnlyExternalLinks, isOnlyExternalLinks = _ref2$isOnlyExternalL === void 0 ? false : _ref2$isOnlyExternalL, customizedUrlValidation = _ref2.customizedUrlValidation, customizedHelperMessage = _ref2.customizedHelperMessage; var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['media', 'mediaInsert'], function (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 }; }), mediaProvider = _useSharedPluginState.mediaProvider, isOpen = _useSharedPluginState.isOpen, mountInfo = _useSharedPluginState.mountInfo; var targetRef; var 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 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions); mountPoint = popupsMountPoint; } var intl = useIntl(); var focusEditor = useFocusEditor({ editorView: editorView }); var _useUnholyAutofocus = useUnholyAutofocus(), autofocusRef = _useUnholyAutofocus.autofocusRef, onPositionCalculated = _useUnholyAutofocus.onPositionCalculated; if (!isOpen || !mediaProvider) { return null; } var handleClose = function handleClose(exitMethod) { return function (event) { event.preventDefault(); if (dispatchAnalyticsEvent) { var payload = { action: ACTION.CLOSED, actionSubject: ACTION_SUBJECT.PICKER, actionSubjectId: ACTION_SUBJECT_ID.PICKER_MEDIA, eventType: EVENT_TYPE.UI, attributes: { exitMethod: 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, function (setOutsideClickTargetRef) { return /*#__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: function 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: function closeMediaInsertPicker() { _closeMediaInsertPicker(); focusEditor(); }, insertMediaSingle: insertMediaSingle, insertExternalMediaSingle: insertExternalMediaSingle, isOnlyExternalLinks: isOnlyExternalLinks, customizedUrlValidation: customizedUrlValidation, customizedHelperMessage: customizedHelperMessage })))); })); };