UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

240 lines 10.2 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import React, { useEffect, useRef, useState } from 'react'; import { appearancePropsMap } from '@atlaskit/editor-common/card'; import { areToolbarFlagsEnabled } from '@atlaskit/editor-common/toolbar-flag-check'; import { FloatingToolbarButton as Button, FloatingToolbarSeparator as Separator } from '@atlaskit/editor-common/ui'; import { ArrowKeyNavigationType, DropdownContainer as UiDropdown } from '@atlaskit/editor-common/ui-menu'; import ChevronDownIcon from '@atlaskit/icon/core/chevron-down'; import { Flex } from '@atlaskit/primitives/compiled'; import { LinkAppearanceMenu } from './LinkToolbarAppearanceDropdown'; var CustomHyperlinkDropdown = function CustomHyperlinkDropdown(props) { var _props$editorPluginAp, _props$cardOptions3, _props$editorPluginAp3, _props$editorPluginAp4; var _useState = useState(new Map()), _useState2 = _slicedToArray(_useState, 2), supportedUrlsMap = _useState2[0], setSupportedUrlsMap = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isOpen = _useState4[0], setIsOpen = _useState4[1]; var cardProvider = useRef(undefined); var containerRef = useRef(undefined); var areAnyNewToolbarFlagsEnabled = areToolbarFlagsEnabled(Boolean((_props$editorPluginAp = props.editorPluginApi) === null || _props$editorPluginAp === void 0 ? void 0 : _props$editorPluginAp.toolbar)); var url = props.url, intl = props.intl, editorView = props.editorView, cardOptions = props.cardOptions, editorAnalyticsApi = props.editorAnalyticsApi, allowDatasource = props.allowDatasource, isDatasourceView = props.isDatasourceView, settingsConfig = props.settingsConfig; // Ignored via go/ees005 // eslint-disable-next-line require-await var getProvider = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _props$cardOptions; var _props$cardOptions2; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!((_props$cardOptions = props.cardOptions) !== null && _props$cardOptions !== void 0 && _props$cardOptions.provider)) { _context.next = 2; break; } return _context.abrupt("return", (_props$cardOptions2 = props.cardOptions) === null || _props$cardOptions2 === void 0 ? void 0 : _props$cardOptions2.provider); case 2: if (!cardProvider.current) { _context.next = 4; break; } return _context.abrupt("return", cardProvider.current); case 4: return _context.abrupt("return", new Promise(function (resolve) { var _props$editorPluginAp2; var cardProvider = (_props$editorPluginAp2 = props.editorPluginApi) === null || _props$editorPluginAp2 === void 0 || (_props$editorPluginAp2 = _props$editorPluginAp2.card) === null || _props$editorPluginAp2 === void 0 || (_props$editorPluginAp2 = _props$editorPluginAp2.sharedState) === null || _props$editorPluginAp2 === void 0 || (_props$editorPluginAp2 = _props$editorPluginAp2.currentState()) === null || _props$editorPluginAp2 === void 0 ? void 0 : _props$editorPluginAp2.provider; if (cardProvider) { resolve(cardProvider); } })); case 5: case "end": return _context.stop(); } }, _callee); })); return function getProvider() { return _ref.apply(this, arguments); }; }(); var resolveUrl = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(url) { var isUrlSupported, _yield$provider$findP, provider, newMap; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: if (!supportedUrlsMap.has(url)) { _context2.next = 2; break; } return _context2.abrupt("return"); case 2: isUrlSupported = false; _context2.prev = 3; _context2.next = 6; return getProvider(); case 6: provider = _context2.sent; _context2.next = 9; return provider === null || provider === void 0 ? void 0 : provider.findPattern(url); case 9: _context2.t1 = _yield$provider$findP = _context2.sent; _context2.t0 = _context2.t1 !== null; if (!_context2.t0) { _context2.next = 13; break; } _context2.t0 = _yield$provider$findP !== void 0; case 13: if (!_context2.t0) { _context2.next = 17; break; } _context2.t2 = _yield$provider$findP; _context2.next = 18; break; case 17: _context2.t2 = false; case 18: isUrlSupported = _context2.t2; _context2.next = 24; break; case 21: _context2.prev = 21; _context2.t3 = _context2["catch"](3); // eslint-disable-line no-unused-vars isUrlSupported = false; case 24: newMap = new Map(supportedUrlsMap); newMap.set(url, isUrlSupported); setSupportedUrlsMap(newMap); case 27: case "end": return _context2.stop(); } }, _callee2, null, [[3, 21]]); })); return function resolveUrl(_x) { return _ref2.apply(this, arguments); }; }(); useEffect(function () { resolveUrl(url); // before migrating from a class to a functional component, we were only reacting to changes in the url // eslint-disable-next-line react-hooks/exhaustive-deps }, [url, (_props$cardOptions3 = props.cardOptions) === null || _props$cardOptions3 === void 0 ? void 0 : _props$cardOptions3.provider, props.editorPluginApi]); if (!supportedUrlsMap.get(url)) { return null; } if (!editorView) { return null; } var dispatchCommand = function dispatchCommand(fn) { fn && fn(editorView && editorView.state, editorView && editorView.dispatch, editorView); // Refocus the view to ensure the editor has focus if (editorView && !editorView.hasFocus()) { editorView.focus(); } }; var toggleOpen = function toggleOpen() { return setIsOpen(function (open) { return !open; }); }; var close = function close() { return setIsOpen(false); }; var currentAppearanceDisplayInformation = appearancePropsMap['url']; var title = intl.formatMessage(currentAppearanceDisplayInformation.title); var trigger = /*#__PURE__*/React.createElement(Button, { selected: isOpen, title: title, "aria-label": title, "aria-expanded": isOpen, "aria-haspopup": true, onClick: toggleOpen, icon: currentAppearanceDisplayInformation.icon({ label: '' }), iconAfter: /*#__PURE__*/React.createElement(ChevronDownIcon, { label: "", spacing: "compact", size: "small" }), areAnyNewToolbarFlagsEnabled: areAnyNewToolbarFlagsEnabled }, title); return /*#__PURE__*/React.createElement(Flex, { ref: containerRef, gap: "space.075" }, /*#__PURE__*/React.createElement(UiDropdown, { mountTo: containerRef.current, isOpen: isOpen, handleClickOutside: close, handleEscapeKeydown: close, trigger: trigger, scrollableElement: containerRef.current // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , arrowKeyNavigationProviderOptions: { type: ArrowKeyNavigationType.MENU }, fitHeight: 400, fitWidth: 200 }, /*#__PURE__*/React.createElement(LinkAppearanceMenu, { url: url, intl: intl, currentAppearance: undefined, editorState: editorView.state, allowEmbeds: cardOptions === null || cardOptions === void 0 ? void 0 : cardOptions.allowEmbeds, allowBlockCards: cardOptions === null || cardOptions === void 0 ? void 0 : cardOptions.allowBlockCards, editorAnalyticsApi: editorAnalyticsApi, isDatasourceView: isDatasourceView, allowDatasource: allowDatasource, dispatchCommand: dispatchCommand, settingsConfig: settingsConfig, areAnyNewToolbarFlagsEnabled: areAnyNewToolbarFlagsEnabled })), !areToolbarFlagsEnabled(Boolean((_props$editorPluginAp3 = props.editorPluginApi) === null || _props$editorPluginAp3 === void 0 ? void 0 : _props$editorPluginAp3.toolbar)) && /*#__PURE__*/React.createElement(Separator, { areAnyNewToolbarFlagsEnabled: areToolbarFlagsEnabled(Boolean((_props$editorPluginAp4 = props.editorPluginApi) === null || _props$editorPluginAp4 === void 0 ? void 0 : _props$editorPluginAp4.toolbar)) })); }; export var getCustomHyperlinkAppearanceDropdown = function getCustomHyperlinkAppearanceDropdown(_ref3) { var url = _ref3.url, intl = _ref3.intl, editorAnalyticsApi = _ref3.editorAnalyticsApi, editorPluginApi = _ref3.editorPluginApi, settingsConfig = _ref3.settingsConfig, cardOptions = _ref3.cardOptions, allowDatasource = _ref3.allowDatasource, isDatasourceView = _ref3.isDatasourceView; return { type: 'custom', fallback: [], render: function render(editorView) { if (!editorView) { return; } return /*#__PURE__*/React.createElement(CustomHyperlinkDropdown, { intl: intl, url: url, editorAnalyticsApi: editorAnalyticsApi, editorPluginApi: editorPluginApi, editorView: editorView, settingsConfig: settingsConfig, cardOptions: cardOptions, allowDatasource: allowDatasource, isDatasourceView: isDatasourceView }); } }; };