@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
249 lines (248 loc) • 11.5 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCustomHyperlinkAppearanceDropdown = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _card = require("@atlaskit/editor-common/card");
var _toolbarFlagCheck = require("@atlaskit/editor-common/toolbar-flag-check");
var _ui = require("@atlaskit/editor-common/ui");
var _uiMenu = require("@atlaskit/editor-common/ui-menu");
var _chevronDown = _interopRequireDefault(require("@atlaskit/icon/core/chevron-down"));
var _compiled = require("@atlaskit/primitives/compiled");
var _LinkToolbarAppearanceDropdown = require("./LinkToolbarAppearanceDropdown");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var CustomHyperlinkDropdown = function CustomHyperlinkDropdown(props) {
var _props$editorPluginAp, _props$cardOptions3, _props$editorPluginAp3, _props$editorPluginAp4;
var _useState = (0, _react.useState)(new Map()),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
supportedUrlsMap = _useState2[0],
setSupportedUrlsMap = _useState2[1];
var _useState3 = (0, _react.useState)(false),
_useState4 = (0, _slicedToArray2.default)(_useState3, 2),
isOpen = _useState4[0],
setIsOpen = _useState4[1];
var cardProvider = (0, _react.useRef)(undefined);
var containerRef = (0, _react.useRef)(undefined);
var areAnyNewToolbarFlagsEnabled = (0, _toolbarFlagCheck.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 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
var _props$cardOptions;
var _props$cardOptions2;
return _regenerator.default.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 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(url) {
var isUrlSupported, _yield$provider$findP, provider, newMap;
return _regenerator.default.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);
};
}();
(0, _react.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 = _card.appearancePropsMap['url'];
var title = intl.formatMessage(currentAppearanceDisplayInformation.title);
var trigger = /*#__PURE__*/_react.default.createElement(_ui.FloatingToolbarButton, {
selected: isOpen,
title: title,
"aria-label": title,
"aria-expanded": isOpen,
"aria-haspopup": true,
onClick: toggleOpen,
icon: currentAppearanceDisplayInformation.icon({
label: ''
}),
iconAfter: /*#__PURE__*/_react.default.createElement(_chevronDown.default, {
label: "",
spacing: "compact",
size: "small"
}),
areAnyNewToolbarFlagsEnabled: areAnyNewToolbarFlagsEnabled
}, title);
return /*#__PURE__*/_react.default.createElement(_compiled.Flex, {
ref: containerRef,
gap: "space.075"
}, /*#__PURE__*/_react.default.createElement(_uiMenu.DropdownContainer, {
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: _uiMenu.ArrowKeyNavigationType.MENU
},
fitHeight: 400,
fitWidth: 200
}, /*#__PURE__*/_react.default.createElement(_LinkToolbarAppearanceDropdown.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
})), !(0, _toolbarFlagCheck.areToolbarFlagsEnabled)(Boolean((_props$editorPluginAp3 = props.editorPluginApi) === null || _props$editorPluginAp3 === void 0 ? void 0 : _props$editorPluginAp3.toolbar)) && /*#__PURE__*/_react.default.createElement(_ui.FloatingToolbarSeparator, {
areAnyNewToolbarFlagsEnabled: (0, _toolbarFlagCheck.areToolbarFlagsEnabled)(Boolean((_props$editorPluginAp4 = props.editorPluginApi) === null || _props$editorPluginAp4 === void 0 ? void 0 : _props$editorPluginAp4.toolbar))
}));
};
var getCustomHyperlinkAppearanceDropdown = exports.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.default.createElement(CustomHyperlinkDropdown, {
intl: intl,
url: url,
editorAnalyticsApi: editorAnalyticsApi,
editorPluginApi: editorPluginApi,
editorView: editorView,
settingsConfig: settingsConfig,
cardOptions: cardOptions,
allowDatasource: allowDatasource,
isDatasourceView: isDatasourceView
});
}
};
};