@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
153 lines (152 loc) • 6.91 kB
JavaScript
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 { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { areToolbarFlagsEnabled } from '@atlaskit/editor-common/toolbar-flag-check';
import { FloatingToolbarSeparator as Separator } from '@atlaskit/editor-common/ui';
import { Flex } from '@atlaskit/primitives/compiled';
import { DatasourceAppearanceButton } from './DatasourceAppearanceButton';
import { LinkToolbarAppearance } from './LinkToolbarAppearance';
export function HyperlinkToolbarAppearance(props) {
var _props$cardOptions3, _props$editorPluginAp2;
var _useState = useState(new Map()),
_useState2 = _slicedToArray(_useState, 2),
supportedUrlsMap = _useState2[0],
setSupportedUrlsMap = _useState2[1];
var cardProvider = useRef(undefined);
var url = props.url,
intl = props.intl,
editorView = props.editorView,
editorState = props.editorState,
cardOptions = props.cardOptions,
editorAnalyticsApi = props.editorAnalyticsApi;
// 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$editorPluginAp;
var cardProvider = (_props$editorPluginAp = props.editorPluginApi) === null || _props$editorPluginAp === void 0 || (_props$editorPluginAp = _props$editorPluginAp.card) === null || _props$editorPluginAp === void 0 || (_props$editorPluginAp = _props$editorPluginAp.sharedState) === null || _props$editorPluginAp === void 0 || (_props$editorPluginAp = _props$editorPluginAp.currentState()) === null || _props$editorPluginAp === void 0 ? void 0 : _props$editorPluginAp.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);
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;
}
var areAnyNewToolbarFlagsEnabled = areToolbarFlagsEnabled(Boolean((_props$editorPluginAp2 = props.editorPluginApi) === null || _props$editorPluginAp2 === void 0 ? void 0 : _props$editorPluginAp2.toolbar));
return /*#__PURE__*/React.createElement(Flex, null, /*#__PURE__*/React.createElement(LinkToolbarAppearance, {
key: "link-appearance",
url: url,
intl: intl,
editorView: editorView,
editorState: editorState,
allowEmbeds: cardOptions === null || cardOptions === void 0 ? void 0 : cardOptions.allowEmbeds,
allowBlockCards: cardOptions === null || cardOptions === void 0 ? void 0 : cardOptions.allowBlockCards,
editorAnalyticsApi: editorAnalyticsApi,
areAnyNewToolbarFlagsEnabled: areAnyNewToolbarFlagsEnabled
}), (cardOptions === null || cardOptions === void 0 ? void 0 : cardOptions.allowDatasource) && /*#__PURE__*/React.createElement(DatasourceAppearanceButton, {
intl: intl,
url: url,
editorState: editorState,
editorView: editorView,
editorAnalyticsApi: editorAnalyticsApi,
inputMethod: INPUT_METHOD.FLOATING_TB,
areAnyNewToolbarFlagsEnabled: areAnyNewToolbarFlagsEnabled
}), /*#__PURE__*/React.createElement(Separator, {
areAnyNewToolbarFlagsEnabled: areAnyNewToolbarFlagsEnabled
}));
}