@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
158 lines (155 loc) • 6.8 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React, { PureComponent, useLayoutEffect, useRef, useState } from 'react';
import { ResourcedMention } from '@atlaskit/mention/element';
import ResourcedMentionWithProfilecard from './mention-with-profilecard';
var GENERIC_USER_IDS = ['HipChat', 'all', 'here'];
var noop = function noop() {};
export var MentionWithProviders = function MentionWithProviders(_ref) {
var accessLevel = _ref.accessLevel,
eventHandlers = _ref.eventHandlers,
id = _ref.id,
mentionProvider = _ref.mentionProvider,
profilecardProviderResolver = _ref.profilecardProvider,
text = _ref.text,
localId = _ref.localId;
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
profilecardProvider = _useState2[0],
setProfilecardProvider = _useState2[1];
var mountedRef = useRef(true);
useLayoutEffect(function () {
mountedRef.current = true;
return function () {
mountedRef.current = false;
};
}, []);
useLayoutEffect(function () {
// We are not using async/await here to avoid having an intermediate Promise
// introduced by the transpiler.
// This will allow consumer to use a SynchronousPromise.resolve and avoid useless
// rerendering
profilecardProviderResolver === null || profilecardProviderResolver === void 0 || profilecardProviderResolver.then(function (result) {
if (mountedRef.current) {
setProfilecardProvider(result);
}
}).catch(function () {
if (mountedRef.current) {
setProfilecardProvider(null);
}
});
}, [profilecardProviderResolver]);
var MentionComponent = profilecardProvider && profilecardProviderResolver && GENERIC_USER_IDS.indexOf(id) === -1 ? ResourcedMentionWithProfilecard : ResourcedMention;
return /*#__PURE__*/React.createElement(MentionComponent, {
id: id,
text: text,
accessLevel: accessLevel,
localId: localId,
mentionProvider: mentionProvider
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
,
profilecardProvider: profilecardProvider,
onClick: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onClick,
onMouseEnter: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseEnter,
onMouseLeave: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseLeave
});
};
// eslint-disable-next-line @repo/internal/react/no-class-components
export var MentionWithProvidersOld = /*#__PURE__*/function (_PureComponent) {
function MentionWithProvidersOld() {
var _this;
_classCallCheck(this, MentionWithProvidersOld);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, MentionWithProvidersOld, [].concat(args));
_defineProperty(_this, "state", {
profilecardProvider: null
});
return _this;
}
_inherits(MentionWithProvidersOld, _PureComponent);
return _createClass(MentionWithProvidersOld, [{
key: "UNSAFE_componentWillMount",
value:
// Ignored via go/ees005
// eslint-disable-next-line react/no-unsafe
function UNSAFE_componentWillMount() {
this.updateProfilecardProvider(this.props);
}
// Ignored via go/ees005
// eslint-disable-next-line react/no-unsafe
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.profilecardProvider !== this.props.profilecardProvider) {
this.updateProfilecardProvider(nextProps);
}
}
}, {
key: "updateProfilecardProvider",
value: function updateProfilecardProvider(props) {
var _this2 = this;
// We are not using async/await here to avoid having an intermediate Promise
// introduced by the transpiler.
// This will allow consumer to use a SynchronousPromise.resolve and avoid useless
// rerendering
if (props.profilecardProvider) {
props.profilecardProvider.then(function (profilecardProvider) {
_this2.setState({
profilecardProvider: profilecardProvider
});
}).catch(function () {
_this2.setState({
profilecardProvider: null
});
});
} else {
this.setState({
profilecardProvider: null
});
}
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
accessLevel = _this$props.accessLevel,
eventHandlers = _this$props.eventHandlers,
id = _this$props.id,
mentionProvider = _this$props.mentionProvider,
text = _this$props.text,
localId = _this$props.localId;
var profilecardProvider = this.state.profilecardProvider;
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var actionHandlers = {};
['onClick', 'onMouseEnter', 'onMouseLeave'].forEach(function (handler) {
actionHandlers[handler] = eventHandlers && eventHandlers[handler] || noop;
});
var MentionComponent = profilecardProvider && GENERIC_USER_IDS.indexOf(id) === -1 ? ResourcedMentionWithProfilecard : ResourcedMention;
return /*#__PURE__*/React.createElement(MentionComponent, _extends({
id: id,
text: text,
accessLevel: accessLevel,
localId: localId,
mentionProvider: mentionProvider
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
,
profilecardProvider: profilecardProvider
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
}, actionHandlers));
}
}]);
}(PureComponent);