@contentful/field-editor-reference 4.6.9 → 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.
@@ -1,18 +1,21 @@
1
- import React__default, { useState, useRef, useEffect, createElement, Fragment, useMemo, useCallback, useReducer } from 'react';
1
+ import React__default, { useState, useRef, useEffect, createElement, Fragment, useMemo, useCallback } from 'react';
2
2
  import { css, cx } from 'emotion';
3
3
  import tokens from '@contentful/f36-tokens';
4
4
  import get from 'lodash-es/get';
5
5
  import { Menu, TextInput, Button, Paragraph, Card, Flex, SectionHeading, IconButton, Tooltip, Text, EntryCard, MenuItem, MenuDivider, AssetCard } from '@contentful/f36-components';
6
6
  import { SearchIcon, PlusIcon, ChevronDownIcon, LinkIcon, CloseIcon, FolderOpenTrimmedIcon, ClockIcon } from '@contentful/f36-icons';
7
7
  import moment from 'moment';
8
- import deepEqual from 'deep-equal';
9
8
  import { FieldConnector, isValidImage, entityHelpers, shortenStorageUnit } from '@contentful/field-editor-shared';
9
+ import deepEqual from 'deep-equal';
10
+ import { useQuery, QueryCache, QueryClient, QueryClientProvider, useQueryClient } from '@tanstack/react-query';
10
11
  import constate from 'constate';
11
12
  import { createClient } from 'contentful-management';
13
+ import PQueue from 'p-queue';
12
14
  import isNumber from 'lodash-es/isNumber';
13
15
  import arrayMove from 'array-move';
14
16
  import { SortableContainer, SortableHandle, SortableElement } from 'react-sortable-hoc';
15
17
  import mimetype from '@contentful/mimetype';
18
+ import { useInView } from 'react-intersection-observer';
16
19
 
17
20
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
18
21
  try {
@@ -68,57 +71,110 @@ function _extends() {
68
71
  return _extends.apply(this, arguments);
69
72
  }
70
73
 
71
- function _objectWithoutPropertiesLoose(source, excluded) {
72
- if (source == null) return {};
73
- var target = {};
74
- var sourceKeys = Object.keys(source);
75
- var key, i;
74
+ function _inheritsLoose(subClass, superClass) {
75
+ subClass.prototype = Object.create(superClass.prototype);
76
+ subClass.prototype.constructor = subClass;
76
77
 
77
- for (i = 0; i < sourceKeys.length; i++) {
78
- key = sourceKeys[i];
79
- if (excluded.indexOf(key) >= 0) continue;
80
- target[key] = source[key];
78
+ _setPrototypeOf(subClass, superClass);
79
+ }
80
+
81
+ function _getPrototypeOf(o) {
82
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
83
+ return o.__proto__ || Object.getPrototypeOf(o);
84
+ };
85
+ return _getPrototypeOf(o);
86
+ }
87
+
88
+ function _setPrototypeOf(o, p) {
89
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
90
+ o.__proto__ = p;
91
+ return o;
92
+ };
93
+
94
+ return _setPrototypeOf(o, p);
95
+ }
96
+
97
+ function _isNativeReflectConstruct() {
98
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
99
+ if (Reflect.construct.sham) return false;
100
+ if (typeof Proxy === "function") return true;
101
+
102
+ try {
103
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
104
+ return true;
105
+ } catch (e) {
106
+ return false;
81
107
  }
108
+ }
82
109
 
83
- return target;
110
+ function _construct(Parent, args, Class) {
111
+ if (_isNativeReflectConstruct()) {
112
+ _construct = Reflect.construct;
113
+ } else {
114
+ _construct = function _construct(Parent, args, Class) {
115
+ var a = [null];
116
+ a.push.apply(a, args);
117
+ var Constructor = Function.bind.apply(Parent, a);
118
+ var instance = new Constructor();
119
+ if (Class) _setPrototypeOf(instance, Class.prototype);
120
+ return instance;
121
+ };
122
+ }
123
+
124
+ return _construct.apply(null, arguments);
84
125
  }
85
126
 
86
- function _unsupportedIterableToArray(o, minLen) {
87
- if (!o) return;
88
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
89
- var n = Object.prototype.toString.call(o).slice(8, -1);
90
- if (n === "Object" && o.constructor) n = o.constructor.name;
91
- if (n === "Map" || n === "Set") return Array.from(o);
92
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
127
+ function _isNativeFunction(fn) {
128
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
93
129
  }
94
130
 
95
- function _arrayLikeToArray(arr, len) {
96
- if (len == null || len > arr.length) len = arr.length;
131
+ function _wrapNativeSuper(Class) {
132
+ var _cache = typeof Map === "function" ? new Map() : undefined;
133
+
134
+ _wrapNativeSuper = function _wrapNativeSuper(Class) {
135
+ if (Class === null || !_isNativeFunction(Class)) return Class;
136
+
137
+ if (typeof Class !== "function") {
138
+ throw new TypeError("Super expression must either be null or a function");
139
+ }
140
+
141
+ if (typeof _cache !== "undefined") {
142
+ if (_cache.has(Class)) return _cache.get(Class);
97
143
 
98
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
144
+ _cache.set(Class, Wrapper);
145
+ }
146
+
147
+ function Wrapper() {
148
+ return _construct(Class, arguments, _getPrototypeOf(this).constructor);
149
+ }
150
+
151
+ Wrapper.prototype = Object.create(Class.prototype, {
152
+ constructor: {
153
+ value: Wrapper,
154
+ enumerable: false,
155
+ writable: true,
156
+ configurable: true
157
+ }
158
+ });
159
+ return _setPrototypeOf(Wrapper, Class);
160
+ };
99
161
 
100
- return arr2;
162
+ return _wrapNativeSuper(Class);
101
163
  }
102
164
 
103
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
104
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
105
- if (it) return (it = it.call(o)).next.bind(it);
165
+ function _objectWithoutPropertiesLoose(source, excluded) {
166
+ if (source == null) return {};
167
+ var target = {};
168
+ var sourceKeys = Object.keys(source);
169
+ var key, i;
106
170
 
107
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
108
- if (it) o = it;
109
- var i = 0;
110
- return function () {
111
- if (i >= o.length) return {
112
- done: true
113
- };
114
- return {
115
- done: false,
116
- value: o[i++]
117
- };
118
- };
171
+ for (i = 0; i < sourceKeys.length; i++) {
172
+ key = sourceKeys[i];
173
+ if (excluded.indexOf(key) >= 0) continue;
174
+ target[key] = source[key];
119
175
  }
120
176
 
121
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
177
+ return target;
122
178
  }
123
179
 
124
180
  var container = /*#__PURE__*/css({
@@ -498,7 +554,7 @@ function CombinedLinkActions(props) {
498
554
  var hideEmptyCard = props.entityType === 'Asset' && !props.isEmpty;
499
555
  return createElement("div", {
500
556
  className: hideEmptyCard ? '' : container
501
- }, !props.canCreateEntity && !props.canLinkEntity && createElement(NoLinkPermissionsInfo, null), props.entityType === 'Entry' && createElement(CombinedEntryLinkActions, Object.assign({}, props)), props.entityType === 'Asset' && createElement(CombinedAssetLinkActions, Object.assign({}, props)));
557
+ }, !props.canCreateEntity && !props.canLinkEntity && createElement(NoLinkPermissionsInfo, null), props.entityType === 'Entry' && createElement(CombinedEntryLinkActions, _extends({}, props)), props.entityType === 'Asset' && createElement(CombinedAssetLinkActions, _extends({}, props)));
502
558
  }
503
559
 
504
560
  function CombinedEntryLinkActions(props) {
@@ -1628,9 +1684,8 @@ function useLinkActionsProps(props) {
1628
1684
  return function (_x, _x2) {
1629
1685
  return _ref.apply(this, arguments);
1630
1686
  };
1631
- }(), [sdk, entityType, onCreated]); // Wrapping these two with useCallback caused a bug [ZEND-2154] where CTs were not propagated to the UI at all
1632
-
1633
- var onLinkExisting = /*#__PURE__*/function () {
1687
+ }(), [sdk, entityType, onCreated]);
1688
+ var onLinkExisting = useCallback( /*#__PURE__*/function () {
1634
1689
  var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(index) {
1635
1690
  var entity;
1636
1691
  return runtime_1.wrap(function _callee2$(_context2) {
@@ -1665,12 +1720,12 @@ function useLinkActionsProps(props) {
1665
1720
  }, _callee2);
1666
1721
  }));
1667
1722
 
1668
- return function onLinkExisting(_x3) {
1723
+ return function (_x3) {
1669
1724
  return _ref2.apply(this, arguments);
1670
1725
  };
1671
- }();
1672
-
1673
- var onLinkSeveralExisting = /*#__PURE__*/function () {
1726
+ }(), // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: Evaluate the dependencies
1727
+ [sdk, entityType, onLinkedExisting]);
1728
+ var onLinkSeveralExisting = useCallback( /*#__PURE__*/function () {
1674
1729
  var _ref3 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(index) {
1675
1730
  var entities;
1676
1731
  return runtime_1.wrap(function _callee3$(_context3) {
@@ -1705,11 +1760,11 @@ function useLinkActionsProps(props) {
1705
1760
  }, _callee3);
1706
1761
  }));
1707
1762
 
1708
- return function onLinkSeveralExisting(_x4) {
1763
+ return function (_x4) {
1709
1764
  return _ref3.apply(this, arguments);
1710
1765
  };
1711
- }(); // FIXME: The memoization might rerun every time due to the always changing callback identities above
1712
-
1766
+ }(), // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: Evaluate the dependencies
1767
+ [sdk, entityType, onLinkedExisting]); // FIXME: The memoization might rerun every time due to the always changing callback identities above
1713
1768
 
1714
1769
  return useMemo(function () {
1715
1770
  return {
@@ -1739,7 +1794,7 @@ function LinkEntityActions(_ref4) {
1739
1794
  props = _objectWithoutPropertiesLoose(_ref4, _excluded);
1740
1795
 
1741
1796
  var renderLinkActions = renderCustomActions ? renderCustomActions : function (props) {
1742
- return createElement(LinkActions, Object.assign({}, props));
1797
+ return createElement(LinkActions, _extends({}, props));
1743
1798
  };
1744
1799
  return renderLinkActions(props);
1745
1800
  }
@@ -1800,6 +1855,8 @@ var ScheduleTooltip = function ScheduleTooltip(_ref2) {
1800
1855
  };
1801
1856
 
1802
1857
  var ScheduledIconWithTooltip = function ScheduledIconWithTooltip(_ref) {
1858
+ var _status$jobs;
1859
+
1803
1860
  var entityType = _ref.entityType,
1804
1861
  entityId = _ref.entityId,
1805
1862
  getEntityScheduledActions = _ref.getEntityScheduledActions,
@@ -1831,7 +1888,7 @@ var ScheduledIconWithTooltip = function ScheduledIconWithTooltip(_ref) {
1831
1888
  return null;
1832
1889
  }
1833
1890
 
1834
- var jobs = status.jobs ? status.jobs : [];
1891
+ var jobs = (_status$jobs = status.jobs) != null ? _status$jobs : [];
1835
1892
 
1836
1893
  if (jobs.length === 0) {
1837
1894
  return null;
@@ -1857,502 +1914,426 @@ function AssetThumbnail(props) {
1857
1914
  });
1858
1915
  }
1859
1916
 
1860
- function reducer(state, action) {
1861
- var _extends2, _extends3, _extends4, _extends5, _extends6, _extends7, _extends8;
1917
+ var _excluded$1 = ["priority"],
1918
+ _excluded2 = ["children"];
1862
1919
 
1863
- switch (action.type) {
1864
- case 'set_entry':
1865
- return _extends({}, state, {
1866
- entries: _extends({}, state.entries, (_extends2 = {}, _extends2[action.id] = action.entry, _extends2))
1867
- });
1920
+ var globalQueue = /*#__PURE__*/new PQueue({
1921
+ concurrency: 20
1922
+ });
1923
+ var UnsupportedError = /*#__PURE__*/function (_Error) {
1924
+ _inheritsLoose(UnsupportedError, _Error);
1868
1925
 
1869
- case 'set_entry_failed':
1870
- return _extends({}, state, {
1871
- entries: _extends({}, state.entries, (_extends3 = {}, _extends3[action.id] = 'failed', _extends3))
1872
- });
1926
+ function UnsupportedError(message) {
1927
+ var _this;
1873
1928
 
1874
- case 'set_asset':
1875
- return _extends({}, state, {
1876
- assets: _extends({}, state.assets, (_extends4 = {}, _extends4[action.id] = action.asset, _extends4))
1877
- });
1929
+ _this = _Error.call(this, message) || this;
1930
+ _this.isUnsupportedError = void 0;
1931
+ _this.isUnsupportedError = true;
1932
+ return _this;
1933
+ }
1878
1934
 
1879
- case 'set_asset_failed':
1880
- return _extends({}, state, {
1881
- assets: _extends({}, state.assets, (_extends5 = {}, _extends5[action.id] = 'failed', _extends5))
1882
- });
1935
+ return UnsupportedError;
1936
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
1937
+ function isUnsupportedError(value) {
1938
+ return typeof value === 'object' && (value == null ? void 0 : value.isUnsupportedError) === true;
1939
+ }
1883
1940
 
1884
- case 'set_scheduled_actions':
1885
- return _extends({}, state, {
1886
- scheduledActions: _extends({}, state.scheduledActions, (_extends6 = {}, _extends6[action.key] = action.actions, _extends6))
1887
- });
1941
+ var isEntityQueryKey = function isEntityQueryKey(queryKey) {
1942
+ return Array.isArray(queryKey) && (queryKey[0] === 'Entry' || queryKey[0] === 'Asset') && queryKey.length === 4;
1943
+ };
1888
1944
 
1889
- case 'set_resource':
1890
- return _extends({}, state, {
1891
- resources: _extends({}, state.resources, (_extends7 = {}, _extends7[action.resourceType + "." + action.urn] = action.resourceInfo, _extends7))
1892
- });
1945
+ function fetchContentfulEntry(_x) {
1946
+ return _fetchContentfulEntry.apply(this, arguments);
1947
+ }
1893
1948
 
1894
- case 'set_resource_failed':
1895
- return _extends({}, state, {
1896
- resources: _extends({}, state.resources, (_extends8 = {}, _extends8[action.resourceType + "." + action.urn] = 'failed', _extends8))
1897
- });
1949
+ function _fetchContentfulEntry() {
1950
+ _fetchContentfulEntry = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(params) {
1951
+ var urn, fetch, options, resourceId, _resourceId$split, spaceId, entryId, environmentId, _yield$Promise$all, space, entry, contentTypeId, _yield$Promise$all2, contentType, defaultLocaleCode;
1898
1952
 
1899
- default:
1900
- return state;
1901
- }
1902
- }
1953
+ return runtime_1.wrap(function _callee3$(_context3) {
1954
+ while (1) {
1955
+ switch (_context3.prev = _context3.next) {
1956
+ case 0:
1957
+ urn = params.urn, fetch = params.fetch, options = params.options;
1958
+ resourceId = urn.split(':', 6)[5];
1959
+ _resourceId$split = resourceId.split('/'), spaceId = _resourceId$split[1], entryId = _resourceId$split[3];
1960
+ environmentId = 'master';
1961
+ _context3.next = 6;
1962
+ return Promise.all([fetch(['space', spaceId], function (_ref9) {
1963
+ var cmaClient = _ref9.cmaClient;
1964
+ return cmaClient.space.get({
1965
+ spaceId: spaceId
1966
+ });
1967
+ }, options), fetch(['entry', spaceId, environmentId, entryId], function (_ref10) {
1968
+ var cmaClient = _ref10.cmaClient;
1969
+ return cmaClient.entry.get({
1970
+ spaceId: spaceId,
1971
+ environmentId: environmentId,
1972
+ entryId: entryId
1973
+ });
1974
+ }, options)]);
1903
1975
 
1904
- var initialState = {
1905
- entries: {},
1906
- assets: {},
1907
- scheduledActions: {},
1908
- resources: {}
1909
- };
1976
+ case 6:
1977
+ _yield$Promise$all = _context3.sent;
1978
+ space = _yield$Promise$all[0];
1979
+ entry = _yield$Promise$all[1];
1980
+ contentTypeId = entry.sys.contentType.sys.id;
1981
+ _context3.next = 12;
1982
+ return Promise.all([fetch(['contentType', spaceId, environmentId, contentTypeId], function (_ref11) {
1983
+ var cmaClient = _ref11.cmaClient;
1984
+ return cmaClient.contentType.get({
1985
+ contentTypeId: contentTypeId,
1986
+ spaceId: spaceId,
1987
+ environmentId: environmentId
1988
+ });
1989
+ }, options), fetch(['defaultLocale', spaceId, environmentId], /*#__PURE__*/function () {
1990
+ var _ref13 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(_ref12) {
1991
+ var _locales$items$find;
1910
1992
 
1911
- var isNotNil = function isNotNil(entity) {
1912
- return Boolean(entity && entity !== 'failed');
1913
- };
1993
+ var cmaClient, locales, defaultLocaleCode;
1994
+ return runtime_1.wrap(function _callee2$(_context2) {
1995
+ while (1) {
1996
+ switch (_context2.prev = _context2.next) {
1997
+ case 0:
1998
+ cmaClient = _ref12.cmaClient;
1999
+ _context2.next = 3;
2000
+ return cmaClient.locale.getMany({
2001
+ spaceId: spaceId,
2002
+ environmentId: environmentId,
2003
+ query: {
2004
+ limit: 100
2005
+ }
2006
+ });
2007
+
2008
+ case 3:
2009
+ locales = _context2.sent;
2010
+ defaultLocaleCode = (_locales$items$find = locales.items.find(function (locale) {
2011
+ return locale["default"];
2012
+ })) == null ? void 0 : _locales$items$find.code;
2013
+ return _context2.abrupt("return", defaultLocaleCode);
1914
2014
 
1915
- var nonNilResources = function nonNilResources(map) {
1916
- return Object.entries(map).filter(function (_ref) {
1917
- var value = _ref[1];
1918
- return isNotNil(value);
1919
- });
1920
- };
2015
+ case 6:
2016
+ case "end":
2017
+ return _context2.stop();
2018
+ }
2019
+ }
2020
+ }, _callee2);
2021
+ }));
2022
+
2023
+ return function (_x3) {
2024
+ return _ref13.apply(this, arguments);
2025
+ };
2026
+ }(), options)]);
2027
+
2028
+ case 12:
2029
+ _yield$Promise$all2 = _context3.sent;
2030
+ contentType = _yield$Promise$all2[0];
2031
+ defaultLocaleCode = _yield$Promise$all2[1];
2032
+ return _context3.abrupt("return", {
2033
+ defaultLocaleCode: defaultLocaleCode,
2034
+ resource: entry,
2035
+ space: space,
2036
+ contentType: contentType
2037
+ });
1921
2038
 
1922
- function useEntitiesStore(props) {
1923
- var spaceId = props.sdk.ids.space;
1924
- var environmentId = props.sdk.ids.environmentAlias || props.sdk.ids.environment;
2039
+ case 16:
2040
+ case "end":
2041
+ return _context3.stop();
2042
+ }
2043
+ }
2044
+ }, _callee3);
2045
+ }));
2046
+ return _fetchContentfulEntry.apply(this, arguments);
2047
+ }
1925
2048
 
1926
- var _React$useState = useState(function () {
2049
+ var _constate = /*#__PURE__*/constate(function useInitServices(props) {
2050
+ var currentSpaceId = props.sdk.ids.space;
2051
+ var currentEnvironmentId = props.sdk.ids.environment;
2052
+ var queryClient = useQueryClient();
2053
+ var queryCache = queryClient.getQueryCache();
2054
+ var entityChangeUnsubscribers = useRef({});
2055
+ var cmaClient = useMemo(function () {
1927
2056
  return createClient({
1928
2057
  apiAdapter: props.sdk.cmaAdapter
1929
2058
  }, {
1930
2059
  type: 'plain'
1931
2060
  });
1932
- }),
1933
- cmaClient = _React$useState[0];
1934
-
1935
- var _React$useReducer = useReducer(reducer, initialState),
1936
- state = _React$useReducer[0],
1937
- dispatch = _React$useReducer[1];
1938
-
1939
- var loadEntityScheduledActions = useCallback(function (entityType, id) {
1940
- var key = entityType + ":" + id;
2061
+ }, [props.sdk.cmaAdapter]);
2062
+ var queryQueue = useMemo(function () {
2063
+ if (props.queryConcurrency) {
2064
+ return new PQueue({
2065
+ concurrency: props.queryConcurrency
2066
+ });
2067
+ }
1941
2068
 
1942
- if (state.scheduledActions[key]) {
1943
- return Promise.resolve(state.scheduledActions[key]);
2069
+ return globalQueue;
2070
+ }, [props.queryConcurrency]);
2071
+ var fetch = useCallback(function fetch(queryKey, fn, options) {
2072
+ if (options === void 0) {
2073
+ options = {};
1944
2074
  }
1945
2075
 
1946
- return props.sdk.space.getEntityScheduledActions(entityType, id).then(function (data) {
1947
- dispatch({
1948
- type: 'set_scheduled_actions',
1949
- key: key,
1950
- actions: data
1951
- });
1952
- return data;
1953
- })["catch"](function () {
1954
- dispatch({
1955
- type: 'set_scheduled_actions',
1956
- key: key,
1957
- actions: undefined
2076
+ var _options = options,
2077
+ priority = _options.priority,
2078
+ queryOptions = _objectWithoutPropertiesLoose(_options, _excluded$1);
2079
+
2080
+ return queryClient.fetchQuery(queryKey, function () {
2081
+ return queryQueue.add(function () {
2082
+ return fn({
2083
+ cmaClient: cmaClient
2084
+ });
2085
+ }, {
2086
+ priority: priority
1958
2087
  });
1959
- return [];
1960
- });
1961
- }, [props.sdk.space, state.scheduledActions]);
1962
- var loadEntry = useCallback( /*#__PURE__*/function () {
1963
- var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(entryId) {
1964
- var entry;
1965
- return runtime_1.wrap(function _callee$(_context) {
1966
- while (1) {
1967
- switch (_context.prev = _context.next) {
1968
- case 0:
1969
- _context.prev = 0;
1970
- _context.next = 3;
1971
- return cmaClient.entry.get({
1972
- spaceId: spaceId,
1973
- environmentId: environmentId,
1974
- entryId: entryId
1975
- });
2088
+ }, queryOptions);
2089
+ }, [queryClient, queryQueue, cmaClient]);
2090
+ var getEntity = useCallback(function getEntity(entityType, entityId, options) {
2091
+ var _options$spaceId, _options$environmentI;
2092
+
2093
+ var spaceId = (_options$spaceId = options == null ? void 0 : options.spaceId) != null ? _options$spaceId : currentSpaceId;
2094
+ var environmentId = (_options$environmentI = options == null ? void 0 : options.environmentId) != null ? _options$environmentI : currentEnvironmentId;
2095
+ var queryKey = [entityType, entityId, spaceId, environmentId];
2096
+ return fetch(queryKey, function (_ref) {
2097
+ var cmaClient = _ref.cmaClient;
2098
+
2099
+ if (entityType === 'Entry') {
2100
+ return cmaClient.entry.get({
2101
+ entryId: entityId,
2102
+ spaceId: spaceId,
2103
+ environmentId: environmentId
2104
+ });
2105
+ }
1976
2106
 
1977
- case 3:
1978
- entry = _context.sent;
1979
- dispatch({
1980
- type: 'set_entry',
1981
- id: entryId,
1982
- entry: entry
1983
- });
1984
- return _context.abrupt("return", entry);
2107
+ if (entityType === 'Asset') {
2108
+ return cmaClient.asset.get({
2109
+ assetId: entityId,
2110
+ spaceId: spaceId,
2111
+ environmentId: environmentId
2112
+ });
2113
+ }
1985
2114
 
1986
- case 8:
1987
- _context.prev = 8;
1988
- _context.t0 = _context["catch"](0);
1989
- dispatch({
1990
- type: 'set_entry_failed',
1991
- id: entryId
1992
- });
1993
- return _context.abrupt("return");
2115
+ throw new UnsupportedError('Unsupported entity type');
2116
+ }, options);
2117
+ }, [fetch, currentSpaceId, currentEnvironmentId]);
2118
+ var getEntityScheduledActions = useCallback(function getEntityScheduledActions(entityType, entityId, options) {
2119
+ var _options$spaceId2, _options$environmentI2;
2120
+
2121
+ var spaceId = (_options$spaceId2 = options == null ? void 0 : options.spaceId) != null ? _options$spaceId2 : currentSpaceId;
2122
+ var environmentId = (_options$environmentI2 = options == null ? void 0 : options.environmentId) != null ? _options$environmentI2 : currentEnvironmentId;
2123
+ var queryKey = ['scheduled-actions', entityType, entityId, spaceId, environmentId];
2124
+ return fetch(queryKey, /*#__PURE__*/function () {
2125
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref2) {
2126
+ var cmaClient, response;
2127
+ return runtime_1.wrap(function _callee$(_context) {
2128
+ while (1) {
2129
+ switch (_context.prev = _context.next) {
2130
+ case 0:
2131
+ cmaClient = _ref2.cmaClient;
2132
+ _context.next = 3;
2133
+ return cmaClient.scheduledActions.getMany({
2134
+ spaceId: spaceId,
2135
+ query: {
2136
+ 'environment.sys.id': environmentId,
2137
+ 'entity.sys.id': entityId,
2138
+ 'sys.status[in]': 'scheduled',
2139
+ order: 'scheduledFor.datetime'
2140
+ }
2141
+ });
1994
2142
 
1995
- case 12:
1996
- case "end":
1997
- return _context.stop();
1998
- }
1999
- }
2000
- }, _callee, null, [[0, 8]]);
2001
- }));
2143
+ case 3:
2144
+ response = _context.sent;
2145
+ return _context.abrupt("return", response.items);
2002
2146
 
2003
- return function (_x) {
2004
- return _ref2.apply(this, arguments);
2005
- };
2006
- }(), [cmaClient, spaceId, environmentId]);
2007
- var getEntry = useCallback( /*#__PURE__*/function () {
2008
- var _ref3 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(entryId) {
2009
- var cachedEntry;
2010
- return runtime_1.wrap(function _callee2$(_context2) {
2011
- while (1) {
2012
- switch (_context2.prev = _context2.next) {
2013
- case 0:
2014
- cachedEntry = state.entries[entryId];
2147
+ case 5:
2148
+ case "end":
2149
+ return _context.stop();
2150
+ }
2151
+ }
2152
+ }, _callee);
2153
+ }));
2015
2154
 
2016
- if (!isNotNil(cachedEntry)) {
2017
- _context2.next = 3;
2018
- break;
2019
- }
2155
+ return function (_x2) {
2156
+ return _ref3.apply(this, arguments);
2157
+ };
2158
+ }(), options);
2159
+ }, [fetch, currentSpaceId, currentEnvironmentId]);
2160
+ var getResource = useCallback(function getResource(resourceType, urn, options) {
2161
+ var queryKey = ['Resource', resourceType, urn];
2162
+ return fetch(queryKey, function () {
2163
+ if (resourceType === 'Contentful:Entry') {
2164
+ return fetchContentfulEntry({
2165
+ fetch: fetch,
2166
+ urn: urn,
2167
+ options: options
2168
+ });
2169
+ }
2020
2170
 
2021
- return _context2.abrupt("return", cachedEntry);
2171
+ throw new UnsupportedError('Unsupported resource type');
2172
+ }, options);
2173
+ }, [fetch]);
2174
+ var isSameSpaceEntityQueryKey = useCallback(function (queryKey) {
2175
+ var isEntityKey = isEntityQueryKey(queryKey);
2176
+ var isSameSpaceEntityKey = isEntityKey && queryKey[2] === currentSpaceId && queryKey[3] === currentEnvironmentId;
2177
+ return isSameSpaceEntityKey;
2178
+ }, [currentSpaceId, currentEnvironmentId]); // @ts-expect-error ...
2179
+
2180
+ // @ts-expect-error ...
2181
+ var onEntityChanged = props.sdk.space.onEntityChanged;
2182
+ useEffect(function () {
2183
+ if (typeof onEntityChanged !== 'function') {
2184
+ return;
2185
+ }
2022
2186
 
2023
- case 3:
2024
- return _context2.abrupt("return", loadEntry(entryId));
2187
+ var subscribeQuery = function subscribeQuery(_ref4) {
2188
+ var queryKey = _ref4.queryKey,
2189
+ queryHash = _ref4.queryHash;
2190
+ var entityType = queryKey[0],
2191
+ entityId = queryKey[1];
2192
+ entityChangeUnsubscribers.current[queryHash] = onEntityChanged(entityType, entityId, function (data) {
2193
+ queryClient.setQueryData(queryKey, data);
2194
+ });
2195
+ };
2025
2196
 
2026
- case 4:
2027
- case "end":
2028
- return _context2.stop();
2029
- }
2030
- }
2031
- }, _callee2);
2032
- }));
2197
+ var queries = queryCache.findAll({
2198
+ type: 'active',
2199
+ predicate: function predicate(query) {
2200
+ return isSameSpaceEntityQueryKey(query.queryKey);
2201
+ }
2202
+ });
2203
+ queries.forEach(subscribeQuery);
2204
+ var unsubscribe = queryCache.subscribe(function (event) {
2205
+ if (!event) {
2206
+ return;
2207
+ }
2033
2208
 
2034
- return function (_x2) {
2035
- return _ref3.apply(this, arguments);
2036
- };
2037
- }(), [loadEntry, state.entries]);
2038
- var loadAsset = useCallback( /*#__PURE__*/function () {
2039
- var _ref4 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(assetId) {
2040
- var asset;
2041
- return runtime_1.wrap(function _callee3$(_context3) {
2042
- while (1) {
2043
- switch (_context3.prev = _context3.next) {
2044
- case 0:
2045
- _context3.prev = 0;
2046
- _context3.next = 3;
2047
- return cmaClient.asset.get({
2048
- spaceId: spaceId,
2049
- environmentId: environmentId,
2050
- assetId: assetId
2051
- });
2209
+ var type = event.type,
2210
+ query = event.query;
2211
+ var queryKey = query.queryKey,
2212
+ queryHash = query.queryHash;
2052
2213
 
2053
- case 3:
2054
- asset = _context3.sent;
2055
- dispatch({
2056
- type: 'set_asset',
2057
- id: assetId,
2058
- asset: asset
2059
- });
2060
- return _context3.abrupt("return", asset);
2214
+ if (!isSameSpaceEntityQueryKey(queryKey)) {
2215
+ return;
2216
+ }
2061
2217
 
2062
- case 8:
2063
- _context3.prev = 8;
2064
- _context3.t0 = _context3["catch"](0);
2065
- dispatch({
2066
- type: 'set_asset_failed',
2067
- id: assetId
2068
- });
2069
- return _context3.abrupt("return");
2218
+ if (type === 'added') {
2219
+ subscribeQuery(query);
2220
+ }
2070
2221
 
2071
- case 12:
2072
- case "end":
2073
- return _context3.stop();
2074
- }
2075
- }
2076
- }, _callee3, null, [[0, 8]]);
2077
- }));
2222
+ if (type === 'removed') {
2223
+ var _entityChangeUnsubscr, _entityChangeUnsubscr2;
2078
2224
 
2079
- return function (_x3) {
2080
- return _ref4.apply(this, arguments);
2225
+ // calling unsubscribe
2226
+ (_entityChangeUnsubscr = (_entityChangeUnsubscr2 = entityChangeUnsubscribers.current)[queryHash]) == null ? void 0 : _entityChangeUnsubscr.call(_entityChangeUnsubscr2);
2227
+ }
2228
+ });
2229
+ return function () {
2230
+ unsubscribe();
2231
+ Object.values(entityChangeUnsubscribers.current).forEach(function (off) {
2232
+ return off();
2233
+ });
2234
+ entityChangeUnsubscribers.current = {};
2081
2235
  };
2082
- }(), [cmaClient, spaceId, environmentId]);
2083
- var getAsset = useCallback( /*#__PURE__*/function () {
2084
- var _ref5 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(assetId) {
2085
- var cachedAsset;
2086
- return runtime_1.wrap(function _callee4$(_context4) {
2087
- while (1) {
2088
- switch (_context4.prev = _context4.next) {
2089
- case 0:
2090
- cachedAsset = state.assets[assetId];
2091
-
2092
- if (!isNotNil(cachedAsset)) {
2093
- _context4.next = 3;
2094
- break;
2095
- }
2236
+ }, [onEntityChanged, queryCache, isSameSpaceEntityQueryKey, queryClient]);
2237
+ return {
2238
+ cmaClient: cmaClient,
2239
+ fetch: fetch,
2240
+ getResource: getResource,
2241
+ getEntity: getEntity,
2242
+ ids: props.sdk.ids,
2243
+ getEntityScheduledActions: getEntityScheduledActions
2244
+ };
2245
+ }, function (_ref5) {
2246
+ var fetch = _ref5.fetch;
2247
+ return fetch;
2248
+ }, function (_ref6) {
2249
+ var getResource = _ref6.getResource,
2250
+ getEntity = _ref6.getEntity,
2251
+ getEntityScheduledActions = _ref6.getEntityScheduledActions;
2252
+ return {
2253
+ getResource: getResource,
2254
+ getEntity: getEntity,
2255
+ getEntityScheduledActions: getEntityScheduledActions
2256
+ };
2257
+ }, function (_ref7) {
2258
+ var ids = _ref7.ids;
2259
+ return ids;
2260
+ }),
2261
+ InternalServiceProvider = _constate[0],
2262
+ useEntityLoader = _constate[2],
2263
+ useCurrentIds = _constate[3];
2264
+
2265
+ function useEntity(entityType, entityId, options) {
2266
+ var _options$spaceId3, _options$environmentI3;
2267
+
2268
+ var _useCurrentIds = useCurrentIds(),
2269
+ space = _useCurrentIds.space,
2270
+ environment = _useCurrentIds.environment;
2271
+
2272
+ var _useEntityLoader = useEntityLoader(),
2273
+ getEntity = _useEntityLoader.getEntity;
2274
+
2275
+ var queryKey = [entityType, entityId, (_options$spaceId3 = options == null ? void 0 : options.spaceId) != null ? _options$spaceId3 : space, (_options$environmentI3 = options == null ? void 0 : options.environmentId) != null ? _options$environmentI3 : environment];
2276
+
2277
+ var _useQuery = useQuery(queryKey, function () {
2278
+ return getEntity(entityType, entityId, options);
2279
+ }, {
2280
+ enabled: options == null ? void 0 : options.enabled
2281
+ }),
2282
+ status = _useQuery.status,
2283
+ data = _useQuery.data;
2096
2284
 
2097
- return _context4.abrupt("return", cachedAsset);
2285
+ return {
2286
+ status: status,
2287
+ data: data
2288
+ };
2289
+ }
2290
+ function useResource(resourceType, urn, options) {
2291
+ var queryKey = ['Resource', resourceType, urn];
2098
2292
 
2099
- case 3:
2100
- return _context4.abrupt("return", loadAsset(assetId));
2293
+ var _useEntityLoader2 = useEntityLoader(),
2294
+ getResource = _useEntityLoader2.getResource;
2101
2295
 
2102
- case 4:
2103
- case "end":
2104
- return _context4.stop();
2105
- }
2106
- }
2107
- }, _callee4);
2108
- }));
2296
+ var _useQuery2 = useQuery(queryKey, function () {
2297
+ return getResource(resourceType, urn, options);
2298
+ }, {
2299
+ enabled: options == null ? void 0 : options.enabled
2300
+ }),
2301
+ status = _useQuery2.status,
2302
+ data = _useQuery2.data,
2303
+ error = _useQuery2.error;
2109
2304
 
2110
- return function (_x4) {
2111
- return _ref5.apply(this, arguments);
2112
- };
2113
- }(), [loadAsset, state.assets]);
2114
- var loadContentfulEntry = useCallback( /*#__PURE__*/function () {
2115
- var _ref6 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(urn) {
2116
- var _locales$items$find;
2305
+ return {
2306
+ status: status,
2307
+ data: data,
2308
+ error: error
2309
+ };
2310
+ }
2117
2311
 
2118
- var resourceId, _resourceId$split, spaceId, entryId, environmentId, _yield$Promise$all, space, entry, contentTypeId, _yield$Promise$all2, contentType, locales, defaultLocaleCode;
2119
-
2120
- return runtime_1.wrap(function _callee5$(_context5) {
2121
- while (1) {
2122
- switch (_context5.prev = _context5.next) {
2123
- case 0:
2124
- resourceId = urn.split(':', 6)[5];
2125
- _resourceId$split = resourceId.split('/'), spaceId = _resourceId$split[1], entryId = _resourceId$split[3];
2126
- environmentId = 'master';
2127
- _context5.t0 = Promise;
2128
- _context5.next = 6;
2129
- return cmaClient.space.get({
2130
- spaceId: spaceId
2131
- });
2132
-
2133
- case 6:
2134
- _context5.t1 = _context5.sent;
2135
- _context5.next = 9;
2136
- return cmaClient.entry.get({
2137
- spaceId: spaceId,
2138
- environmentId: environmentId,
2139
- entryId: entryId
2140
- });
2141
-
2142
- case 9:
2143
- _context5.t2 = _context5.sent;
2144
- _context5.t3 = [_context5.t1, _context5.t2];
2145
- _context5.next = 13;
2146
- return _context5.t0.all.call(_context5.t0, _context5.t3);
2147
-
2148
- case 13:
2149
- _yield$Promise$all = _context5.sent;
2150
- space = _yield$Promise$all[0];
2151
- entry = _yield$Promise$all[1];
2152
- contentTypeId = entry.sys.contentType.sys.id;
2153
- _context5.t4 = Promise;
2154
- _context5.next = 20;
2155
- return cmaClient.contentType.get({
2156
- contentTypeId: contentTypeId,
2157
- spaceId: spaceId,
2158
- environmentId: environmentId
2159
- });
2160
-
2161
- case 20:
2162
- _context5.t5 = _context5.sent;
2163
- _context5.next = 23;
2164
- return cmaClient.locale.getMany({
2165
- spaceId: spaceId,
2166
- environmentId: environmentId,
2167
- query: {
2168
- limit: 100
2169
- }
2170
- });
2171
-
2172
- case 23:
2173
- _context5.t6 = _context5.sent;
2174
- _context5.t7 = [_context5.t5, _context5.t6];
2175
- _context5.next = 27;
2176
- return _context5.t4.all.call(_context5.t4, _context5.t7);
2177
-
2178
- case 27:
2179
- _yield$Promise$all2 = _context5.sent;
2180
- contentType = _yield$Promise$all2[0];
2181
- locales = _yield$Promise$all2[1];
2182
- defaultLocaleCode = (_locales$items$find = locales.items.find(function (locale) {
2183
- return locale["default"];
2184
- })) == null ? void 0 : _locales$items$find.code;
2185
- return _context5.abrupt("return", {
2186
- resource: entry,
2187
- defaultLocaleCode: defaultLocaleCode,
2188
- space: space,
2189
- contentType: contentType
2190
- });
2191
-
2192
- case 32:
2193
- case "end":
2194
- return _context5.stop();
2195
- }
2196
- }
2197
- }, _callee5);
2198
- }));
2199
-
2200
- return function (_x5) {
2201
- return _ref6.apply(this, arguments);
2202
- };
2203
- }(), [cmaClient]);
2204
- var getResource = useCallback( /*#__PURE__*/function () {
2205
- var _ref7 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee6(resourceType, urn) {
2206
- var cachedResource, resourceInfo;
2207
- return runtime_1.wrap(function _callee6$(_context6) {
2208
- while (1) {
2209
- switch (_context6.prev = _context6.next) {
2210
- case 0:
2211
- cachedResource = state.resources[resourceType + "." + urn];
2212
-
2213
- if (!isNotNil(cachedResource)) {
2214
- _context6.next = 3;
2215
- break;
2216
- }
2217
-
2218
- return _context6.abrupt("return", cachedResource);
2219
-
2220
- case 3:
2221
- _context6.prev = 3;
2222
-
2223
- if (!(resourceType === 'Contentful:Entry')) {
2224
- _context6.next = 8;
2225
- break;
2226
- }
2227
-
2228
- _context6.next = 7;
2229
- return loadContentfulEntry(urn);
2230
-
2231
- case 7:
2232
- resourceInfo = _context6.sent;
2233
-
2234
- case 8:
2235
- dispatch({
2236
- type: 'set_resource',
2237
- resourceType: resourceType,
2238
- urn: urn,
2239
- resourceInfo: resourceInfo
2240
- });
2241
- return _context6.abrupt("return", resourceInfo);
2242
-
2243
- case 12:
2244
- _context6.prev = 12;
2245
- _context6.t0 = _context6["catch"](3);
2246
- dispatch({
2247
- type: 'set_resource_failed',
2248
- resourceType: resourceType,
2249
- urn: urn
2250
- });
2251
- return _context6.abrupt("return");
2252
-
2253
- case 16:
2254
- case "end":
2255
- return _context6.stop();
2256
- }
2257
- }
2258
- }, _callee6, null, [[3, 12]]);
2259
- }));
2260
-
2261
- return function (_x6, _x7) {
2262
- return _ref7.apply(this, arguments);
2263
- };
2264
- }(), [loadContentfulEntry, state.resources]);
2265
- useEffect(function () {
2266
- // @ts-expect-error
2267
- if (typeof props.sdk.space.onEntityChanged !== 'undefined') {
2268
- // @ts-expect-error
2269
- var onEntityChanged = props.sdk.space.onEntityChanged;
2270
- var listeners = [];
2271
-
2272
- var _loop = function _loop() {
2273
- var _step$value = _step.value,
2274
- id = _step$value[0];
2275
- listeners.push(onEntityChanged('Entry', id, function (entry) {
2276
- return dispatch({
2277
- type: 'set_entry',
2278
- id: id,
2279
- entry: entry
2280
- });
2281
- }));
2282
- };
2283
-
2284
- for (var _iterator = _createForOfIteratorHelperLoose(nonNilResources(state.entries)), _step; !(_step = _iterator()).done;) {
2285
- _loop();
2286
- }
2287
-
2288
- var _loop2 = function _loop2() {
2289
- var _step2$value = _step2.value,
2290
- id = _step2$value[0];
2291
- listeners.push(onEntityChanged('Asset', id, function (asset) {
2292
- return dispatch({
2293
- type: 'set_asset',
2294
- id: id,
2295
- asset: asset
2296
- });
2297
- }));
2298
- };
2299
-
2300
- for (var _iterator2 = _createForOfIteratorHelperLoose(nonNilResources(state.assets)), _step2; !(_step2 = _iterator2()).done;) {
2301
- _loop2();
2302
- }
2303
-
2304
- return function () {
2305
- return listeners.forEach(function (off) {
2306
- return off();
2307
- });
2308
- };
2309
- }
2310
-
2311
- return props.sdk.navigator.onSlideInNavigation(function (_ref8) {
2312
- var oldSlideLevel = _ref8.oldSlideLevel,
2313
- newSlideLevel = _ref8.newSlideLevel;
2314
-
2315
- if (oldSlideLevel > newSlideLevel) {
2316
- for (var _iterator3 = _createForOfIteratorHelperLoose(nonNilResources(state.entries)), _step3; !(_step3 = _iterator3()).done;) {
2317
- var _step3$value = _step3.value,
2318
- id = _step3$value[0];
2319
- loadEntry(id);
2320
- }
2321
-
2322
- for (var _iterator4 = _createForOfIteratorHelperLoose(nonNilResources(state.assets)), _step4; !(_step4 = _iterator4()).done;) {
2323
- var _step4$value = _step4.value,
2324
- _id = _step4$value[0];
2325
- loadAsset(_id);
2326
- }
2327
- }
2328
- }); // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: Evaluate the dependencies
2329
- }, [props.sdk, state.assets, state.entries]);
2330
- return useMemo(function () {
2331
- return {
2332
- /**
2333
- * @deprecated use `getEntry` instead
2334
- */
2335
- getOrLoadEntry: getEntry,
2336
-
2337
- /**
2338
- * @deprecated use `getAsset` instead
2339
- */
2340
- getOrLoadAsset: getAsset,
2341
- getResource: getResource,
2342
- getEntry: getEntry,
2343
- getAsset: getAsset,
2344
- loadEntityScheduledActions: loadEntityScheduledActions,
2345
- entries: state.entries,
2346
- assets: state.assets,
2347
- scheduledActions: state.scheduledActions,
2348
- resources: state.resources
2349
- };
2350
- }, [getResource, getEntry, getAsset, loadEntityScheduledActions, state.entries, state.assets, state.scheduledActions, state.resources]);
2351
- }
2352
-
2353
- var _constate = /*#__PURE__*/constate(useEntitiesStore),
2354
- EntityProvider = _constate[0],
2355
- useEntities = _constate[1];
2312
+ function EntityProvider(_ref8) {
2313
+ var children = _ref8.children,
2314
+ props = _objectWithoutPropertiesLoose(_ref8, _excluded2);
2315
+
2316
+ var reactQueryClient = useMemo(function () {
2317
+ var queryCache = new QueryCache();
2318
+ var queryClient = new QueryClient({
2319
+ queryCache: queryCache,
2320
+ defaultOptions: {
2321
+ queries: {
2322
+ useErrorBoundary: false,
2323
+ refetchOnWindowFocus: false,
2324
+ refetchOnReconnect: true,
2325
+ refetchOnMount: false,
2326
+ staleTime: Infinity,
2327
+ retry: false
2328
+ }
2329
+ }
2330
+ });
2331
+ return queryClient;
2332
+ }, []);
2333
+ return React__default.createElement(QueryClientProvider, {
2334
+ client: reactQueryClient
2335
+ }, React__default.createElement(InternalServiceProvider, _extends({}, props), children));
2336
+ }
2356
2337
 
2357
2338
  function ReferenceEditor(props) {
2358
2339
  return createElement(EntityProvider, {
@@ -2574,7 +2555,10 @@ function fromFieldValidations(field) {
2574
2555
  var result = {
2575
2556
  contentTypes: (_linkContentTypeValid = linkContentTypeValidations == null ? void 0 : linkContentTypeValidations.linkContentType) != null ? _linkContentTypeValid : undefined,
2576
2557
  mimetypeGroups: (_linkMimetypeGroupVal = linkMimetypeGroupValidations == null ? void 0 : linkMimetypeGroupValidations.linkMimetypeGroup) != null ? _linkMimetypeGroupVal : undefined,
2577
- numberOfLinks: numberOfLinks
2558
+ numberOfLinks: numberOfLinks // todo: there are multiple BE problems that need to be solved first, for now we don't want to apply size constraints
2559
+ // linkedFileSize: findValidation(field, 'assetFileSize', {}),
2560
+ // linkedImageDimensions: findValidation(field, 'assetImageDimensions', {})
2561
+
2578
2562
  };
2579
2563
  return result;
2580
2564
  }
@@ -2773,7 +2757,7 @@ function Editor(props) {
2773
2757
  [linkActionsProps]);
2774
2758
 
2775
2759
  if (!props.entityId) {
2776
- return createElement(LinkEntityActions, Object.assign({
2760
+ return createElement(LinkEntityActions, _extends({
2777
2761
  renderCustomActions: props.renderCustomActions
2778
2762
  }, linkActionsProps));
2779
2763
  }
@@ -2785,12 +2769,12 @@ function Editor(props) {
2785
2769
 
2786
2770
  function SingleReferenceEditor(props) {
2787
2771
  var allContentTypes = props.sdk.space.getCachedContentTypes();
2788
- return createElement(ReferenceEditor, Object.assign({}, props), function (_ref) {
2772
+ return createElement(ReferenceEditor, _extends({}, props), function (_ref) {
2789
2773
  var value = _ref.value,
2790
2774
  setValue = _ref.setValue,
2791
2775
  disabled = _ref.disabled,
2792
2776
  externalReset = _ref.externalReset;
2793
- return createElement(Editor, Object.assign({}, props, {
2777
+ return createElement(Editor, _extends({}, props, {
2794
2778
  key: externalReset + "-reference",
2795
2779
  entityId: value ? value.sys.id : '',
2796
2780
  isDisabled: disabled,
@@ -3020,18 +3004,24 @@ function _openEntry() {
3020
3004
  }
3021
3005
 
3022
3006
  function FetchingWrappedEntryCard(props) {
3023
- var _useEntities = useEntities(),
3024
- getEntry = _useEntities.getEntry,
3025
- getAsset = _useEntities.getAsset,
3026
- loadEntityScheduledActions = _useEntities.loadEntityScheduledActions,
3027
- entries = _useEntities.entries;
3007
+ var _useEntity = useEntity('Entry', props.entryId),
3008
+ entry = _useEntity.data,
3009
+ status = _useEntity.status;
3028
3010
 
3029
- useEffect(function () {
3030
- getEntry(props.entryId); // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: Evaluate the dependencies
3031
- }, [props.entryId]);
3011
+ var _useEntityLoader = useEntityLoader(),
3012
+ getEntityScheduledActions = _useEntityLoader.getEntityScheduledActions;
3013
+
3014
+ var loadEntityScheduledActions = useCallback(function () {
3015
+ return getEntityScheduledActions('Entry', props.entryId);
3016
+ }, [getEntityScheduledActions, props.entryId]);
3032
3017
  var size = props.viewType === 'link' ? 'small' : 'default';
3033
- var entry = entries[props.entryId];
3034
- var entityKey = entry === 'failed' ? 'failed' : entry === undefined ? 'undefined' : ":" + entry.sys.id + ":" + entry.sys.version;
3018
+
3019
+ var _useEntityLoader2 = useEntityLoader(),
3020
+ getEntity = _useEntityLoader2.getEntity;
3021
+
3022
+ var getAsset = function getAsset(assetId) {
3023
+ return getEntity('Asset', assetId);
3024
+ };
3035
3025
 
3036
3026
  var onEdit = /*#__PURE__*/function () {
3037
3027
  var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
@@ -3089,7 +3079,7 @@ function FetchingWrappedEntryCard(props) {
3089
3079
 
3090
3080
  }, [entry]);
3091
3081
  return useMemo(function () {
3092
- if (entry === 'failed') {
3082
+ if (status === 'error') {
3093
3083
  var card = createElement(MissingEntityCard, {
3094
3084
  entityType: "Entry",
3095
3085
  isDisabled: props.isDisabled,
@@ -3109,7 +3099,7 @@ function FetchingWrappedEntryCard(props) {
3109
3099
  return card;
3110
3100
  }
3111
3101
 
3112
- if (entry === undefined) {
3102
+ if (status === 'loading') {
3113
3103
  return createElement(EntryCard, {
3114
3104
  size: size,
3115
3105
  isLoading: true
@@ -3148,7 +3138,7 @@ function FetchingWrappedEntryCard(props) {
3148
3138
  entryUrl: (props == null ? void 0 : props.entityUrl) || sharedCardProps.entityUrl
3149
3139
  });
3150
3140
 
3151
- return createElement(WrappedEntryCard, Object.assign({}, builtinCardProps));
3141
+ return createElement(WrappedEntryCard, _extends({}, builtinCardProps));
3152
3142
  }
3153
3143
 
3154
3144
  if (props.renderCustomCard) {
@@ -3161,11 +3151,11 @@ function FetchingWrappedEntryCard(props) {
3161
3151
  }
3162
3152
 
3163
3153
  return renderDefaultCard(); // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: Evaluate the dependencies
3164
- }, [props, entityKey]);
3154
+ }, [props, status, entry]);
3165
3155
  }
3166
3156
 
3167
3157
  function SingleEntryReferenceEditor(props) {
3168
- return createElement(SingleReferenceEditor, Object.assign({}, props, {
3158
+ return createElement(SingleReferenceEditor, _extends({}, props, {
3169
3159
  entityType: "Entry"
3170
3160
  }), function (_ref) {
3171
3161
  var allContentTypes = _ref.allContentTypes,
@@ -3175,7 +3165,7 @@ function SingleEntryReferenceEditor(props) {
3175
3165
  renderCustomCard = _ref.renderCustomCard,
3176
3166
  hasCardRemoveActions = _ref.hasCardRemoveActions,
3177
3167
  hasCardEditActions = _ref.hasCardEditActions;
3178
- return createElement(FetchingWrappedEntryCard, Object.assign({}, props, {
3168
+ return createElement(FetchingWrappedEntryCard, _extends({}, props, {
3179
3169
  allContentTypes: allContentTypes,
3180
3170
  isDisabled: isDisabled,
3181
3171
  entryId: entityId,
@@ -3266,19 +3256,19 @@ function Editor$1(props) {
3266
3256
  onSortEnd: onSortEnd,
3267
3257
  onMove: onMove,
3268
3258
  renderCustomCard: props.renderCustomCard && customCardRenderer
3269
- })), createElement(LinkEntityActions, Object.assign({
3259
+ })), createElement(LinkEntityActions, _extends({
3270
3260
  renderCustomActions: props.renderCustomActions
3271
3261
  }, linkActionsProps)));
3272
3262
  }
3273
3263
 
3274
3264
  function MultipleReferenceEditor(props) {
3275
3265
  var allContentTypes = props.sdk.space.getCachedContentTypes();
3276
- return createElement(ReferenceEditor, Object.assign({}, props), function (_ref2) {
3266
+ return createElement(ReferenceEditor, _extends({}, props), function (_ref2) {
3277
3267
  var value = _ref2.value,
3278
3268
  disabled = _ref2.disabled,
3279
3269
  setValue = _ref2.setValue,
3280
3270
  externalReset = _ref2.externalReset;
3281
- return createElement(Editor$1, Object.assign({}, props, {
3271
+ return createElement(Editor$1, _extends({}, props, {
3282
3272
  items: value || emptyArray,
3283
3273
  isDisabled: disabled,
3284
3274
  setValue: setValue,
@@ -3335,16 +3325,16 @@ var SortableLinkListInternal = /*#__PURE__*/SortableContainer(function (props) {
3335
3325
  function SortableLinkList(props) {
3336
3326
  // with the default distance of 0 the drag start event is "confused" with the click event,
3337
3327
  // so the latter one isn't fired and click handlers on child elements don't work
3338
- return React__default.createElement(SortableLinkListInternal, Object.assign({
3328
+ return React__default.createElement(SortableLinkListInternal, _extends({
3339
3329
  distance: 1
3340
3330
  }, props), props.children);
3341
3331
  }
3342
3332
 
3343
3333
  function MultipleEntryReferenceEditor(props) {
3344
- return createElement(MultipleReferenceEditor, Object.assign({}, props, {
3334
+ return createElement(MultipleReferenceEditor, _extends({}, props, {
3345
3335
  entityType: "Entry"
3346
3336
  }), function (childrenProps) {
3347
- return createElement(SortableLinkList, Object.assign({}, childrenProps, {
3337
+ return createElement(SortableLinkList, _extends({}, childrenProps, {
3348
3338
  axis: "y",
3349
3339
  useDragHandle: true
3350
3340
  }), function (_ref) {
@@ -3354,7 +3344,7 @@ function MultipleEntryReferenceEditor(props) {
3354
3344
  isDisabled = _ref.isDisabled,
3355
3345
  DragHandle = _ref.DragHandle;
3356
3346
  var lastIndex = items.length - 1;
3357
- return createElement(FetchingWrappedEntryCard, Object.assign({}, childrenProps, {
3347
+ return createElement(FetchingWrappedEntryCard, _extends({}, childrenProps, {
3358
3348
  key: item.sys.id + "-" + index,
3359
3349
  index: index,
3360
3350
  allContentTypes: childrenProps.allContentTypes,
@@ -3609,20 +3599,16 @@ var WrappedAssetLink = function WrappedAssetLink(props) {
3609
3599
  };
3610
3600
 
3611
3601
  function FetchingWrappedAssetCard(props) {
3612
- var _useEntities = useEntities(),
3613
- getAsset = _useEntities.getAsset,
3614
- loadEntityScheduledActions = _useEntities.loadEntityScheduledActions,
3615
- assets = _useEntities.assets;
3602
+ var _useEntity = useEntity('Asset', props.assetId),
3603
+ asset = _useEntity.data,
3604
+ status = _useEntity.status;
3616
3605
 
3617
- useEffect(function () {
3618
- if (!props.assetId) {
3619
- return;
3620
- }
3606
+ var _useEntityLoader = useEntityLoader(),
3607
+ getEntityScheduledActions = _useEntityLoader.getEntityScheduledActions;
3621
3608
 
3622
- getAsset(props.assetId); // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: Evaluate the dependencies
3623
- }, [props.assetId]);
3624
- var asset = assets[props.assetId];
3625
- var entityKey = asset === 'failed' ? 'failed' : asset === undefined ? 'undefined' : ":" + asset.sys.id + ":" + asset.sys.version;
3609
+ var loadEntityScheduledActions = useCallback(function () {
3610
+ return getEntityScheduledActions('Asset', props.assetId);
3611
+ }, [getEntityScheduledActions, props.assetId]);
3626
3612
  useEffect(function () {
3627
3613
  if (asset) {
3628
3614
  props.onAction && props.onAction({
@@ -3681,7 +3667,7 @@ function FetchingWrappedAssetCard(props) {
3681
3667
  };
3682
3668
 
3683
3669
  return useMemo(function () {
3684
- if (asset === 'failed') {
3670
+ if (status === 'error') {
3685
3671
  var card = createElement(MissingEntityCard, {
3686
3672
  entityType: "Asset",
3687
3673
  asSquare: props.viewType !== 'link',
@@ -3702,8 +3688,7 @@ function FetchingWrappedAssetCard(props) {
3702
3688
  return card;
3703
3689
  }
3704
3690
 
3705
- var getEntityUrl = props.getEntityUrl,
3706
- sdk = props.sdk;
3691
+ var getEntityUrl = props.getEntityUrl;
3707
3692
  var size = props.viewType === 'big_card' ? 'default' : 'small';
3708
3693
  var commonProps = {
3709
3694
  asset: asset,
@@ -3718,20 +3703,20 @@ function FetchingWrappedAssetCard(props) {
3718
3703
  };
3719
3704
 
3720
3705
  if (props.viewType === 'link') {
3721
- if (asset === undefined) {
3706
+ if (status === 'loading') {
3722
3707
  return createElement(EntryCard, {
3723
3708
  size: "small",
3724
3709
  isLoading: true
3725
3710
  });
3726
3711
  }
3727
3712
 
3728
- return createElement(WrappedAssetLink, Object.assign({}, commonProps, {
3713
+ return createElement(WrappedAssetLink, _extends({}, commonProps, {
3729
3714
  href: commonProps.entityUrl,
3730
- getEntityScheduledActions: sdk.space.getEntityScheduledActions
3715
+ getEntityScheduledActions: loadEntityScheduledActions
3731
3716
  }));
3732
3717
  }
3733
3718
 
3734
- if (asset === undefined) {
3719
+ if (status === 'loading') {
3735
3720
  return createElement(AssetCard, {
3736
3721
  size: size,
3737
3722
  isLoading: true
@@ -3746,7 +3731,7 @@ function FetchingWrappedAssetCard(props) {
3746
3731
  getAssetUrl: getEntityUrl
3747
3732
  });
3748
3733
 
3749
- return createElement(WrappedAssetCard, Object.assign({}, builtinCardProps));
3734
+ return createElement(WrappedAssetCard, _extends({}, builtinCardProps));
3750
3735
  }
3751
3736
 
3752
3737
  if (props.renderCustomCard) {
@@ -3763,17 +3748,17 @@ function FetchingWrappedAssetCard(props) {
3763
3748
  }
3764
3749
 
3765
3750
  return renderDefaultCard(); // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: Evaluate the dependencies
3766
- }, [props, entityKey]);
3751
+ }, [props, status, asset]);
3767
3752
  }
3768
3753
 
3769
3754
  function SingleMediaEditor(props) {
3770
- return createElement(SingleReferenceEditor, Object.assign({}, props, {
3755
+ return createElement(SingleReferenceEditor, _extends({}, props, {
3771
3756
  entityType: "Asset"
3772
3757
  }), function (_ref) {
3773
3758
  var entityId = _ref.entityId,
3774
3759
  isDisabled = _ref.isDisabled,
3775
3760
  setValue = _ref.setValue;
3776
- return createElement(FetchingWrappedAssetCard, Object.assign({}, props, {
3761
+ return createElement(FetchingWrappedAssetCard, _extends({}, props, {
3777
3762
  viewType: "big_card",
3778
3763
  assetId: entityId,
3779
3764
  isDisabled: isDisabled,
@@ -3795,12 +3780,12 @@ var styles$6 = {
3795
3780
  })
3796
3781
  };
3797
3782
  function MultipleMediaEditor(props) {
3798
- return createElement(MultipleReferenceEditor, Object.assign({}, props, {
3783
+ return createElement(MultipleReferenceEditor, _extends({}, props, {
3799
3784
  entityType: "Asset"
3800
3785
  }), function (childrenProps) {
3801
3786
  var _cx;
3802
3787
 
3803
- return createElement(SortableLinkList, Object.assign({}, childrenProps, {
3788
+ return createElement(SortableLinkList, _extends({}, childrenProps, {
3804
3789
  className: cx((_cx = {}, _cx[styles$6.gridContainer] = childrenProps.viewType === 'card', _cx)),
3805
3790
  axis: childrenProps.viewType === 'card' ? 'xy' : 'y',
3806
3791
  useDragHandle: true
@@ -3810,7 +3795,7 @@ function MultipleMediaEditor(props) {
3810
3795
  index = _ref.index,
3811
3796
  isDisabled = _ref.isDisabled,
3812
3797
  DragHandle = _ref.DragHandle;
3813
- return createElement(FetchingWrappedAssetCard, Object.assign({}, childrenProps, {
3798
+ return createElement(FetchingWrappedAssetCard, _extends({}, childrenProps, {
3814
3799
  isDisabled: isDisabled,
3815
3800
  key: item.sys.id + "-" + index,
3816
3801
  assetId: item.sys.id,
@@ -3828,5 +3813,447 @@ MultipleMediaEditor.defaultProps = {
3828
3813
  isInitiallyDisabled: true
3829
3814
  };
3830
3815
 
3831
- export { AssetThumbnail, CombinedLinkActions, CreateEntryLinkButton, CreateEntryMenuTrigger, EntityProvider, MissingEntityCard, MultipleEntryReferenceEditor, MultipleMediaEditor, ScheduledIconWithTooltip, SingleEntryReferenceEditor, SingleMediaEditor, SortableLinkList, WrappedAssetCard, WrappedEntryCard, getScheduleTooltipContent, useEntities };
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 };
3832
4259
  //# sourceMappingURL=field-editor-reference.esm.js.map