@contentful/field-editor-reference 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ import isNumber from 'lodash-es/isNumber';
15
15
  import arrayMove from 'array-move';
16
16
  import { SortableContainer, SortableHandle, SortableElement } from 'react-sortable-hoc';
17
17
  import mimetype from '@contentful/mimetype';
18
+ import { useInView } from 'react-intersection-observer';
18
19
 
19
20
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
20
21
  try {
@@ -1933,6 +1934,9 @@ var UnsupportedError = /*#__PURE__*/function (_Error) {
1933
1934
 
1934
1935
  return UnsupportedError;
1935
1936
  }( /*#__PURE__*/_wrapNativeSuper(Error));
1937
+ function isUnsupportedError(value) {
1938
+ return typeof value === 'object' && (value == null ? void 0 : value.isUnsupportedError) === true;
1939
+ }
1936
1940
 
1937
1941
  var isEntityQueryKey = function isEntityQueryKey(queryKey) {
1938
1942
  return Array.isArray(queryKey) && (queryKey[0] === 'Entry' || queryKey[0] === 'Asset') && queryKey.length === 4;
@@ -2273,8 +2277,7 @@ function useEntity(entityType, entityId, options) {
2273
2277
  var _useQuery = useQuery(queryKey, function () {
2274
2278
  return getEntity(entityType, entityId, options);
2275
2279
  }, {
2276
- enabled: options == null ? void 0 : options.enabled,
2277
- useErrorBoundary: false
2280
+ enabled: options == null ? void 0 : options.enabled
2278
2281
  }),
2279
2282
  status = _useQuery.status,
2280
2283
  data = _useQuery.data;
@@ -2293,9 +2296,7 @@ function useResource(resourceType, urn, options) {
2293
2296
  var _useQuery2 = useQuery(queryKey, function () {
2294
2297
  return getResource(resourceType, urn, options);
2295
2298
  }, {
2296
- enabled: options == null ? void 0 : options.enabled,
2297
- // TODO: check if this is ok for all errors
2298
- useErrorBoundary: false
2299
+ enabled: options == null ? void 0 : options.enabled
2299
2300
  }),
2300
2301
  status = _useQuery2.status,
2301
2302
  data = _useQuery2.data,
@@ -2318,7 +2319,7 @@ function EntityProvider(_ref8) {
2318
2319
  queryCache: queryCache,
2319
2320
  defaultOptions: {
2320
2321
  queries: {
2321
- useErrorBoundary: true,
2322
+ useErrorBoundary: false,
2322
2323
  refetchOnWindowFocus: false,
2323
2324
  refetchOnReconnect: true,
2324
2325
  refetchOnMount: false,
@@ -3812,5 +3813,447 @@ MultipleMediaEditor.defaultProps = {
3812
3813
  isInitiallyDisabled: true
3813
3814
  };
3814
3815
 
3815
- export { AssetThumbnail, CombinedLinkActions, CreateEntryLinkButton, CreateEntryMenuTrigger, EntityProvider, MissingEntityCard, MultipleEntryReferenceEditor, MultipleMediaEditor, ScheduledIconWithTooltip, SingleEntryReferenceEditor, SingleMediaEditor, SortableLinkList, WrappedAssetCard, WrappedEntryCard, getScheduleTooltipContent, useEntity, useEntityLoader, useResource };
3816
+ var resolveAsset = function resolveAsset() {
3817
+ return Promise.resolve();
3818
+ }; // we don't want to show scheduled actions for resources
3819
+
3820
+
3821
+ var resolveScheduledActions = function resolveScheduledActions() {
3822
+ return Promise.resolve([]);
3823
+ };
3824
+
3825
+ function ContentfulEntryCard(_ref) {
3826
+ var info = _ref.info,
3827
+ isDisabled = _ref.isDisabled,
3828
+ renderDragHandle = _ref.renderDragHandle,
3829
+ onRemove = _ref.onRemove,
3830
+ onMoveTop = _ref.onMoveTop,
3831
+ onMoveBottom = _ref.onMoveBottom,
3832
+ getEntryRouteHref = _ref.getEntryRouteHref;
3833
+ var resourceSys = info.resource.sys;
3834
+ var spaceId = resourceSys.space.sys.id;
3835
+ var environmentId = resourceSys.environment.sys.id;
3836
+ var entryId = resourceSys.id;
3837
+ var resourceHref = getEntryRouteHref({
3838
+ spaceId: spaceId,
3839
+ environmentId: environmentId,
3840
+ entryId: entryId
3841
+ }); // TODO: move this into `sdk.navigator.openEntry()`, note that it's signature only include the entry id (not a space or environment)
3842
+
3843
+ var openEntryDetail = function openEntryDetail() {
3844
+ window.open(resourceHref, '_blank', 'noopener,noreferrer');
3845
+ };
3846
+
3847
+ return createElement(WrappedEntryCard, {
3848
+ entry: info.resource,
3849
+ isDisabled: isDisabled,
3850
+ hasCardEditActions: false,
3851
+ contentType: info.contentType,
3852
+ // we use the default locale from the space the entry belongs to
3853
+ // as we assume this gives a more consistent behaviour.
3854
+ // locales will inevitably differ from space to space, so it's likely
3855
+ // that the current locale does not exist in the "remote" space
3856
+ localeCode: info.defaultLocaleCode,
3857
+ defaultLocaleCode: info.defaultLocaleCode,
3858
+ size: "small",
3859
+ getAsset: resolveAsset,
3860
+ getEntityScheduledActions: resolveScheduledActions,
3861
+ spaceName: info.space.name,
3862
+ renderDragHandle: renderDragHandle,
3863
+ isClickable: true,
3864
+ onEdit: openEntryDetail,
3865
+ hasCardRemoveActions: Boolean(onRemove),
3866
+ onRemove: onRemove,
3867
+ onMoveBottom: onMoveBottom,
3868
+ onMoveTop: onMoveTop,
3869
+ entryUrl: resourceHref
3870
+ });
3871
+ }
3872
+
3873
+ var styles$7 = {
3874
+ card: /*#__PURE__*/css({
3875
+ position: 'relative'
3876
+ })
3877
+ };
3878
+ function UnsupportedEntityCard(props) {
3879
+ return React__default.createElement(Card, {
3880
+ className: styles$7.card
3881
+ }, React__default.createElement(SectionHeading, {
3882
+ marginBottom: "none"
3883
+ }, "Resource type ", props.entityType, " is currently not supported"));
3884
+ }
3885
+
3886
+ function ResourceCardSkeleton() {
3887
+ return createElement(EntryCard, {
3888
+ size: "small",
3889
+ isLoading: true
3890
+ });
3891
+ }
3892
+
3893
+ function ExistingResourceCard(props) {
3894
+ var resourceLink = props.resourceLink,
3895
+ inView = props.inView,
3896
+ _props$index = props.index,
3897
+ index = _props$index === void 0 ? 0 : _props$index;
3898
+ var resourceOptions = {
3899
+ priority: index * -1,
3900
+ enabled: inView
3901
+ };
3902
+
3903
+ var _useResource = useResource(resourceLink.sys.linkType, resourceLink.sys.urn, resourceOptions),
3904
+ data = _useResource.data,
3905
+ error = _useResource.error;
3906
+
3907
+ if (!data && !error) {
3908
+ return createElement(ResourceCardSkeleton, null);
3909
+ }
3910
+
3911
+ if (data) {
3912
+ return createElement(ContentfulEntryCard, _extends({
3913
+ info: data
3914
+ }, props));
3915
+ }
3916
+
3917
+ if (isUnsupportedError(error)) {
3918
+ return createElement(UnsupportedEntityCard, {
3919
+ entityType: resourceLink.sys.linkType
3920
+ });
3921
+ }
3922
+
3923
+ return createElement(MissingEntityCard, {
3924
+ entityType: "Entry",
3925
+ isDisabled: props.isDisabled,
3926
+ onRemove: props.onRemove
3927
+ });
3928
+ }
3929
+
3930
+ function ResourceCardWrapper(props) {
3931
+ if (!props.resourceLink) {
3932
+ return null;
3933
+ }
3934
+
3935
+ return createElement(ExistingResourceCard, _extends({}, props, {
3936
+ resourceLink: props.resourceLink,
3937
+ getEntryRouteHref: props.getEntryRouteHref
3938
+ }));
3939
+ }
3940
+
3941
+ function ResourceCard(props) {
3942
+ var _useInView = useInView({
3943
+ triggerOnce: true,
3944
+ rootMargin: '300px 0px 0px 300px'
3945
+ }),
3946
+ ref = _useInView.ref,
3947
+ inView = _useInView.inView; // Forma does not offer us to pass refs, so we need an additional wrapper here
3948
+
3949
+
3950
+ return createElement("div", {
3951
+ ref: ref
3952
+ }, createElement(ResourceCardWrapper, _extends({}, props, {
3953
+ inView: inView
3954
+ })));
3955
+ }
3956
+
3957
+ function useResourceLinkActions(_ref) {
3958
+ var apiUrl = _ref.apiUrl,
3959
+ dialogs = _ref.dialogs,
3960
+ field = _ref.field,
3961
+ onAfterLink = _ref.onAfterLink;
3962
+ var handleAfterLink = useCallback(function (entries) {
3963
+ if (!onAfterLink) {
3964
+ return;
3965
+ }
3966
+
3967
+ entries.forEach(onAfterLink);
3968
+ }, [onAfterLink]);
3969
+ var multiple = field.type === 'Array';
3970
+ var toLinkItem = useMemo(function () {
3971
+ function toUrn(entry) {
3972
+ return "crn:" + apiUrl + ":::content:spaces/" + entry.sys.space.sys.id + "/entries/" + entry.sys.id;
3973
+ }
3974
+
3975
+ return function (entry) {
3976
+ return {
3977
+ sys: {
3978
+ type: 'ResourceLink',
3979
+ linkType: 'Contentful:Entry',
3980
+ urn: toUrn(entry)
3981
+ }
3982
+ };
3983
+ };
3984
+ }, [apiUrl]);
3985
+ var onLinkedExisting = useMemo(function () {
3986
+ if (multiple) {
3987
+ return function (entries) {
3988
+ var linkItems = entries.map(toLinkItem);
3989
+ var prevValue = field.getValue() || [];
3990
+ var updatedValue = [].concat(prevValue, linkItems);
3991
+ field.setValue(updatedValue);
3992
+ handleAfterLink(entries);
3993
+ };
3994
+ } else {
3995
+ return function (entries) {
3996
+ var entry = entries[0];
3997
+ field.setValue(toLinkItem(entry));
3998
+ handleAfterLink([entry]);
3999
+ };
4000
+ }
4001
+ }, [field, handleAfterLink, multiple, toLinkItem]);
4002
+ var onLinkExisting = useMemo(function () {
4003
+ var promptSelection = multiple ? /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
4004
+ return runtime_1.wrap(function _callee$(_context) {
4005
+ while (1) {
4006
+ switch (_context.prev = _context.next) {
4007
+ case 0:
4008
+ _context.next = 2;
4009
+ return dialogs.selectMultipleResourceEntries({
4010
+ // @ts-expect-error wait for update of app-sdk version
4011
+ allowedResources: field.allowedResources
4012
+ });
4013
+
4014
+ case 2:
4015
+ return _context.abrupt("return", _context.sent);
4016
+
4017
+ case 3:
4018
+ case "end":
4019
+ return _context.stop();
4020
+ }
4021
+ }
4022
+ }, _callee);
4023
+ })) : /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
4024
+ return runtime_1.wrap(function _callee2$(_context2) {
4025
+ while (1) {
4026
+ switch (_context2.prev = _context2.next) {
4027
+ case 0:
4028
+ _context2.next = 2;
4029
+ return dialogs.selectSingleResourceEntry({
4030
+ // @ts-expect-error wait for update of app-sdk version
4031
+ allowedResources: field.allowedResources
4032
+ });
4033
+
4034
+ case 2:
4035
+ _context2.t0 = _context2.sent;
4036
+ return _context2.abrupt("return", [_context2.t0]);
4037
+
4038
+ case 4:
4039
+ case "end":
4040
+ return _context2.stop();
4041
+ }
4042
+ }
4043
+ }, _callee2);
4044
+ }));
4045
+ return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3() {
4046
+ var res;
4047
+ return runtime_1.wrap(function _callee3$(_context3) {
4048
+ while (1) {
4049
+ switch (_context3.prev = _context3.next) {
4050
+ case 0:
4051
+ _context3.next = 2;
4052
+ return promptSelection();
4053
+
4054
+ case 2:
4055
+ res = _context3.sent;
4056
+
4057
+ if (res) {
4058
+ _context3.next = 5;
4059
+ break;
4060
+ }
4061
+
4062
+ return _context3.abrupt("return");
4063
+
4064
+ case 5:
4065
+ onLinkedExisting(res);
4066
+
4067
+ case 6:
4068
+ case "end":
4069
+ return _context3.stop();
4070
+ }
4071
+ }
4072
+ }, _callee3);
4073
+ })); // @ts-expect-error wait for update of app-sdk version
4074
+ }, [dialogs, field.allowedResources, multiple, onLinkedExisting]);
4075
+ return {
4076
+ onLinkExisting: onLinkExisting,
4077
+ onLinkedExisting: onLinkedExisting,
4078
+ // hardcoded values to match interface for standard reference field actions
4079
+ entityType: 'Entry',
4080
+ contentTypes: [],
4081
+ canCreateEntity: false,
4082
+ canLinkMultiple: multiple,
4083
+ canLinkEntity: true,
4084
+ isDisabled: false,
4085
+ isEmpty: false,
4086
+ isFull: false,
4087
+ // eslint-disable-next-line -- hardcoded values to match interface for standard reference field actions
4088
+ onCreate: function () {
4089
+ var _onCreate = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4() {
4090
+ return runtime_1.wrap(function _callee4$(_context4) {
4091
+ while (1) {
4092
+ switch (_context4.prev = _context4.next) {
4093
+ case 0:
4094
+ case "end":
4095
+ return _context4.stop();
4096
+ }
4097
+ }
4098
+ }, _callee4);
4099
+ }));
4100
+
4101
+ function onCreate() {
4102
+ return _onCreate.apply(this, arguments);
4103
+ }
4104
+
4105
+ return onCreate;
4106
+ }(),
4107
+ // eslint-disable-next-line -- hardcoded values to match interface for standard reference field actions
4108
+ onCreated: function onCreated() {}
4109
+ };
4110
+ }
4111
+
4112
+ function ResourceEditor(props) {
4113
+ var setValue = props.setValue,
4114
+ items = props.items,
4115
+ apiUrl = props.apiUrl;
4116
+ var onSortStart = useCallback(function (_, event) {
4117
+ return event.preventDefault();
4118
+ }, []);
4119
+ var onSortEnd = useCallback(function (_ref) {
4120
+ var oldIndex = _ref.oldIndex,
4121
+ newIndex = _ref.newIndex;
4122
+ var newItems = arrayMove(items, oldIndex, newIndex);
4123
+ setValue(newItems);
4124
+ }, [items, setValue]);
4125
+ var onMove = useCallback(function (oldIndex, newIndex) {
4126
+ var newItems = arrayMove(items, oldIndex, newIndex);
4127
+ setValue(newItems);
4128
+ }, [items, setValue]);
4129
+ var onRemoteItemAtIndex = useCallback(function (index) {
4130
+ setValue(items.filter(function (_v, i) {
4131
+ return i !== index;
4132
+ }));
4133
+ }, [items, setValue]);
4134
+ var _props$sdk = props.sdk,
4135
+ dialogs = _props$sdk.dialogs,
4136
+ field = _props$sdk.field;
4137
+ var linkActionsProps = useResourceLinkActions({
4138
+ dialogs: dialogs,
4139
+ field: field,
4140
+ apiUrl: apiUrl
4141
+ });
4142
+ return createElement(Fragment, null, props.children(_extends({}, props, {
4143
+ onSortStart: onSortStart,
4144
+ onSortEnd: onSortEnd,
4145
+ onMove: onMove,
4146
+ onRemoteItemAtIndex: onRemoteItemAtIndex
4147
+ })), createElement(CombinedLinkActions, _extends({}, linkActionsProps)));
4148
+ } // provides memoized callbacks bound to a given item
4149
+
4150
+
4151
+ function WithPerItemCallbacks(_ref2) {
4152
+ var listLength = _ref2.listLength,
4153
+ index = _ref2.index,
4154
+ onMove = _ref2.onMove,
4155
+ onRemoteItemAtIndex = _ref2.onRemoteItemAtIndex,
4156
+ children = _ref2.children;
4157
+ var handleMoveTop = useMemo(function () {
4158
+ return index > 0 ? function () {
4159
+ return onMove(index, 0);
4160
+ } : undefined;
4161
+ }, [index, onMove]);
4162
+ var handleMoveBottom = useMemo(function () {
4163
+ return index < listLength - 1 ? function () {
4164
+ return onMove(index, listLength - 1);
4165
+ } : undefined;
4166
+ }, [index, onMove, listLength]);
4167
+ var handleRemove = useCallback(function () {
4168
+ return onRemoteItemAtIndex(index);
4169
+ }, [index, onRemoteItemAtIndex]);
4170
+ return createElement(Fragment, null, children({
4171
+ onMoveBottom: handleMoveBottom,
4172
+ onMoveTop: handleMoveTop,
4173
+ onRemove: handleRemove
4174
+ }));
4175
+ }
4176
+
4177
+ var EMPTY_ARRAY = [];
4178
+ function MultipleResourceReferenceEditor(props) {
4179
+ return createElement(EntityProvider, {
4180
+ sdk: props.sdk
4181
+ }, createElement(FieldConnector, {
4182
+ throttle: 0,
4183
+ field: props.sdk.field,
4184
+ isInitiallyDisabled: props.isInitiallyDisabled,
4185
+ isEqualValues: deepEqual
4186
+ }, function (_ref3) {
4187
+ var value = _ref3.value,
4188
+ disabled = _ref3.disabled,
4189
+ setValue = _ref3.setValue,
4190
+ externalReset = _ref3.externalReset;
4191
+ return createElement(ResourceEditor, _extends({}, props, {
4192
+ items: value || EMPTY_ARRAY,
4193
+ isDisabled: disabled,
4194
+ setValue: setValue,
4195
+ key: externalReset + "-list"
4196
+ }), function (editorProps) {
4197
+ return createElement(SortableLinkList, _extends({}, editorProps), function (_ref4) {
4198
+ var item = _ref4.item,
4199
+ isDisabled = _ref4.isDisabled,
4200
+ DragHandle = _ref4.DragHandle,
4201
+ index = _ref4.index;
4202
+ return createElement(WithPerItemCallbacks, {
4203
+ index: index,
4204
+ onMove: editorProps.onMove,
4205
+ onRemoteItemAtIndex: editorProps.onRemoteItemAtIndex,
4206
+ listLength: (value == null ? void 0 : value.length) || 0
4207
+ }, function (_ref5) {
4208
+ var onMoveBottom = _ref5.onMoveBottom,
4209
+ onMoveTop = _ref5.onMoveTop,
4210
+ onRemove = _ref5.onRemove;
4211
+ return createElement(ResourceCard, {
4212
+ index: index,
4213
+ resourceLink: item,
4214
+ isDisabled: isDisabled,
4215
+ renderDragHandle: DragHandle,
4216
+ onMoveTop: onMoveTop,
4217
+ onMoveBottom: onMoveBottom,
4218
+ onRemove: onRemove,
4219
+ getEntryRouteHref: props.getEntryRouteHref
4220
+ });
4221
+ });
4222
+ });
4223
+ });
4224
+ }));
4225
+ }
4226
+
4227
+ function SingleResourceReferenceEditor(props) {
4228
+ var _props$sdk = props.sdk,
4229
+ dialogs = _props$sdk.dialogs,
4230
+ field = _props$sdk.field;
4231
+ var linkActionsProps = useResourceLinkActions({
4232
+ dialogs: dialogs,
4233
+ field: field,
4234
+ apiUrl: props.apiUrl
4235
+ });
4236
+ return createElement(EntityProvider, {
4237
+ sdk: props.sdk
4238
+ }, createElement(FieldConnector, {
4239
+ throttle: 0,
4240
+ field: props.sdk.field,
4241
+ isInitiallyDisabled: props.isInitiallyDisabled,
4242
+ isEqualValues: deepEqual
4243
+ }, function (_ref) {
4244
+ var value = _ref.value,
4245
+ disabled = _ref.disabled;
4246
+ return value ? createElement(ResourceCard, {
4247
+ onRemove: function onRemove() {
4248
+ return props.sdk.field.removeValue();
4249
+ },
4250
+ resourceLink: value,
4251
+ isDisabled: disabled,
4252
+ getEntryRouteHref: props.getEntryRouteHref
4253
+ }) : // TODO: support custom actions once publicly available
4254
+ createElement(CombinedLinkActions, _extends({}, linkActionsProps));
4255
+ }));
4256
+ }
4257
+
4258
+ export { AssetThumbnail, CombinedLinkActions, CreateEntryLinkButton, CreateEntryMenuTrigger, EntityProvider, MissingEntityCard, MultipleEntryReferenceEditor, MultipleMediaEditor, MultipleResourceReferenceEditor, ScheduledIconWithTooltip, SingleEntryReferenceEditor, SingleMediaEditor, SingleResourceReferenceEditor, SortableLinkList, WrappedAssetCard, WrappedEntryCard, getScheduleTooltipContent, useEntity, useEntityLoader, useResource };
3816
4259
  //# sourceMappingURL=field-editor-reference.esm.js.map