UNPKG

@atlaskit/editor-plugin-paste

Version:

Paste plugin for @atlaskit/editor-core

98 lines 4.24 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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 from 'react'; import { useIntl } from 'react-intl'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks'; import AkFlag, { AutoDismissFlag, FlagGroup } from '@atlaskit/flag'; import StatusErrorIcon from '@atlaskit/icon/core/status-error'; import StatusSuccessIcon from '@atlaskit/icon/core/status-success'; import StatusWarningIcon from '@atlaskit/icon/core/status-warning'; import { PastePluginActionTypes } from '../editor-actions/actions'; import { FLAG_TYPE } from '../pastePluginType'; import { pluginKey } from '../pm-plugins/plugin-factory'; export var Flag = function Flag(_ref) { var api = _ref.api; var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['paste'], function (states) { var _states$pasteState; return { activeFlag: (_states$pasteState = states.pasteState) === null || _states$pasteState === void 0 ? void 0 : _states$pasteState.activeFlag }; }), activeFlag = _useSharedPluginState.activeFlag; var _useIntl = useIntl(), formatMessage = _useIntl.formatMessage; if (!activeFlag) { return; } var type = activeFlag.type, onDismissedCallback = activeFlag.onDismissed, flagDescription = activeFlag.description, flagTitle = activeFlag.title, flagUrlText = activeFlag.urlText, flagUrlHref = activeFlag.urlHref; var description = flagDescription ? formatMessage(flagDescription) : undefined; var title = flagTitle ? formatMessage(flagTitle) : undefined; var urlText = flagUrlText ? formatMessage(flagUrlText) : undefined; var onDismissed = function onDismissed() { api === null || api === void 0 || api.core.actions.execute(function (_ref2) { var tr = _ref2.tr; onDismissedCallback === null || onDismissedCallback === void 0 || onDismissedCallback(tr); var oldMeta = tr.getMeta(pluginKey); tr.setMeta(pluginKey, _objectSpread(_objectSpread({}, oldMeta), {}, { type: PastePluginActionTypes.SET_ACTIVE_FLAG, activeFlag: false })); return tr; }); api === null || api === void 0 || api.core.actions.focus(); }; var getActions = function getActions() { var action = urlText && flagUrlHref ? { content: urlText, href: flagUrlHref } : undefined; if (action) { return [{ content: action.content, href: action.href, target: '_blank' }]; } return undefined; }; var getFlagComponent = function getFlagComponent() { if (type === FLAG_TYPE.INFO || type === FLAG_TYPE.SUCCESS) { return AutoDismissFlag; } return AkFlag; }; var FlagComponent = getFlagComponent(); return /*#__PURE__*/React.createElement(FlagGroup, null, /*#__PURE__*/React.createElement(FlagComponent, { onDismissed: onDismissed, title: title, description: description, id: activeFlag.id, testId: activeFlag.id, icon: typeToIcon(type), actions: getActions() })); }; var typeToIcon = function typeToIcon(type) { if (type === 'error') { return /*#__PURE__*/React.createElement(StatusErrorIcon, { label: "", color: "var(--ds-icon-danger, #C9372C)" }); } if (type === 'warning') { return /*#__PURE__*/React.createElement(StatusWarningIcon, { label: "", color: "var(--ds-icon-warning, #E06C00)" }); } return /*#__PURE__*/React.createElement(StatusSuccessIcon, { label: "", color: "var(--ds-icon-success, #6A9A23)" }); };