@atlaskit/editor-plugin-extension
Version:
editor-plugin-extension plugin for @atlaskit/editor-core
268 lines • 10 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var _excluded = ["value"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
import React, { useEffect, useState } from 'react';
import { getUserFieldContextProvider } from '@atlaskit/editor-common/extensions';
import { Field } from '@atlaskit/form';
import SmartUserPicker, { hydrateDefaultValues } from '@atlaskit/smart-user-picker';
import FieldMessages from '../FieldMessages';
import { validate as _validate } from '../utils';
import UnhandledType from './UnhandledType';
function makeCompat(defaultValue) {
if (!defaultValue) {
return null;
}
if (Array.isArray(defaultValue)) {
return defaultValue.map(function (id) {
return {
type: 'user',
id: id
};
});
}
return {
type: 'user',
id: defaultValue
};
}
function makeSafe(value) {
if (!value) {
return null;
}
if (Array.isArray(value)) {
var ids = [];
var _iterator = _createForOfIteratorHelper(value),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var id = _step.value.id;
ids.push(id);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return ids;
}
return value.id;
}
var isOptionData = function isOptionData(value) {
if (!value) {
return false;
}
return (Array.isArray(value) ? value : [value]).every(function (item) {
return 'name' in item;
});
};
function SafeSmartUserPicker(_ref) {
var context = _ref.context,
field = _ref.field,
formFieldProps = _ref.formFieldProps,
autoFocus = _ref.autoFocus,
onBlur = _ref.onBlur,
onChange = _ref.onChange;
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
unsafeValue = _useState2[0],
setUnsafeValue = _useState2[1];
var siteId = context.siteId,
principalId = context.principalId,
fieldId = context.fieldId,
productKey = context.productKey,
containerId = context.containerId,
objectId = context.objectId,
childObjectId = context.childObjectId,
productAttributes = context.productAttributes,
_context$includeUsers = context.includeUsers,
includeUsers = _context$includeUsers === void 0 ? true : _context$includeUsers;
var safeValue = formFieldProps.value,
formFieldPropsRest = _objectWithoutProperties(formFieldProps, _excluded);
var isMultiple = field.isMultiple,
placeholder = field.placeholder;
function onChangeUnsafe(newValue) {
setUnsafeValue(newValue);
onChange(makeSafe(newValue));
}
useEffect(function () {
var cancel = false;
function hydrate() {
return _hydrate.apply(this, arguments);
}
function _hydrate() {
_hydrate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var hydrated;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return hydrateDefaultValues(undefined,
// no need to override baseUrl
makeCompat(safeValue), productKey, undefined,
// no need to override siteId
{
tenantId: siteId
});
case 2:
hydrated = _context.sent;
if (!(cancel || !isOptionData(hydrated))) {
_context.next = 5;
break;
}
return _context.abrupt("return");
case 5:
setUnsafeValue(hydrated);
case 6:
case "end":
return _context.stop();
}
}, _callee);
}));
return _hydrate.apply(this, arguments);
}
hydrate();
return function () {
cancel = true;
};
}, [safeValue, productKey, siteId]);
return /*#__PURE__*/React.createElement(SmartUserPicker
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
, _extends({}, formFieldPropsRest, {
onChange: onChangeUnsafe,
autoFocus: autoFocus,
onBlur: onBlur,
maxOptions: 10,
isClearable: true,
isMulti: isMultiple,
includeUsers: includeUsers,
includeGroups: false,
includeTeams: false,
fieldId: fieldId,
principalId: principalId,
siteId: siteId,
productKey: productKey,
objectId: objectId,
containerId: containerId,
childObjectId: childObjectId,
placeholder: placeholder,
productAttributes: productAttributes,
value: unsafeValue,
width: "100%"
}));
}
export default function UserSelect(_ref2) {
var name = _ref2.name,
autoFocus = _ref2.autoFocus,
extensionManifest = _ref2.extensionManifest,
field = _ref2.field,
onFieldChange = _ref2.onFieldChange;
var _useState3 = useState({}),
_useState4 = _slicedToArray(_useState3, 2),
context = _useState4[0],
setContext = _useState4[1];
var siteId = context.siteId,
principalId = context.principalId,
fieldId = context.fieldId,
productKey = context.productKey;
var label = field.label,
defaultValue = field.defaultValue,
description = field.description,
isRequired = field.isRequired,
options = field.options,
isDisabled = field.isDisabled;
var type = options.provider.type;
useEffect(function () {
var cancel = false;
function fetchContext() {
return _fetchContext.apply(this, arguments);
}
function _fetchContext() {
_fetchContext = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var _context2;
return _regeneratorRuntime.wrap(function _callee2$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
_context3.prev = 0;
_context3.next = 3;
return getUserFieldContextProvider(extensionManifest, field.options.provider)();
case 3:
_context2 = _context3.sent;
if (!cancel) {
_context3.next = 6;
break;
}
return _context3.abrupt("return");
case 6:
setContext(_context2);
_context3.next = 12;
break;
case 9:
_context3.prev = 9;
_context3.t0 = _context3["catch"](0);
// eslint-disable-next-line no-console
console.error(_context3.t0);
case 12:
case "end":
return _context3.stop();
}
}, _callee2, null, [[0, 9]]);
}));
return _fetchContext.apply(this, arguments);
}
fetchContext();
return function () {
cancel = true;
};
}, [extensionManifest, field.options.provider]);
return /*#__PURE__*/React.createElement(Field, {
name: name,
label: label,
isRequired: isRequired,
defaultValue: defaultValue
// eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
,
validate: function validate(value) {
return _validate(field, value);
},
testId: "config-panel-user-select-".concat(name),
isDisabled: isDisabled
}, function (_ref3) {
var fieldProps = _ref3.fieldProps,
error = _ref3.error,
meta = _ref3.meta;
// if any of these don't exists, the provider is missing
if (!siteId || !principalId || !fieldId || !productKey) {
return /*#__PURE__*/React.createElement(UnhandledType, {
key: name,
field: field,
errorMessage: "Field \"".concat(name, "\" can't be rendered. Missing provider for \"").concat(type, "\".")
});
}
function onChange(newValue) {
fieldProps.onChange(newValue);
onFieldChange(name, true);
}
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SafeSmartUserPicker, {
context: context,
field: field,
formFieldProps: fieldProps,
autoFocus: autoFocus || false
// eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
,
onBlur: function onBlur() {
return onFieldChange(name, meta.dirty);
},
onChange: onChange
}), /*#__PURE__*/React.createElement(FieldMessages, {
error: error,
description: description
}));
});
}