@atlaskit/editor-synced-block-provider 2.2.3 → 2.3.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.
Files changed (29) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cjs/hooks/useFetchSyncBlockData.js +5 -30
  3. package/dist/cjs/providers/syncBlockProvider.js +23 -4
  4. package/dist/cjs/store-manager/referenceSyncBlockStoreManager.js +319 -63
  5. package/dist/cjs/store-manager/syncBlockStoreManager.js +27 -4
  6. package/dist/cjs/utils/mergeFetchSyncBlockDataResult.js +38 -0
  7. package/dist/es2019/hooks/useFetchSyncBlockData.js +6 -31
  8. package/dist/es2019/providers/syncBlockProvider.js +17 -3
  9. package/dist/es2019/store-manager/referenceSyncBlockStoreManager.js +172 -44
  10. package/dist/es2019/store-manager/syncBlockStoreManager.js +21 -4
  11. package/dist/es2019/utils/mergeFetchSyncBlockDataResult.js +30 -0
  12. package/dist/esm/hooks/useFetchSyncBlockData.js +6 -31
  13. package/dist/esm/providers/syncBlockProvider.js +21 -2
  14. package/dist/esm/store-manager/referenceSyncBlockStoreManager.js +320 -64
  15. package/dist/esm/store-manager/syncBlockStoreManager.js +27 -4
  16. package/dist/esm/utils/mergeFetchSyncBlockDataResult.js +31 -0
  17. package/dist/types/common/schema.d.ts +1 -1
  18. package/dist/types/providers/syncBlockProvider.d.ts +1 -1
  19. package/dist/types/providers/types.d.ts +1 -0
  20. package/dist/types/store-manager/referenceSyncBlockStoreManager.d.ts +24 -8
  21. package/dist/types/store-manager/syncBlockStoreManager.d.ts +12 -4
  22. package/dist/types/utils/mergeFetchSyncBlockDataResult.d.ts +12 -0
  23. package/dist/types-ts4.5/common/schema.d.ts +1 -1
  24. package/dist/types-ts4.5/providers/syncBlockProvider.d.ts +1 -1
  25. package/dist/types-ts4.5/providers/types.d.ts +1 -0
  26. package/dist/types-ts4.5/store-manager/referenceSyncBlockStoreManager.d.ts +24 -8
  27. package/dist/types-ts4.5/store-manager/syncBlockStoreManager.d.ts +12 -4
  28. package/dist/types-ts4.5/utils/mergeFetchSyncBlockDataResult.d.ts +12 -0
  29. package/package.json +1 -1
@@ -1,10 +1,15 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
3
  import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
4
  import _createClass from "@babel/runtime/helpers/createClass";
5
- import _regeneratorRuntime from "@babel/runtime/regenerator";
5
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
6
6
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
7
7
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
8
+ 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; } } }; }
9
+ 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; } }
10
+ 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; }
11
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
12
+ import { resolveFetchSyncBlockDataResult } from '../utils/mergeFetchSyncBlockDataResult';
8
13
  var createSyncBlockNode = function createSyncBlockNode(localId, resourceId) {
9
14
  return {
10
15
  type: 'syncBlock',
@@ -17,108 +22,359 @@ var createSyncBlockNode = function createSyncBlockNode(localId, resourceId) {
17
22
  export var ReferenceSyncBlockStoreManager = /*#__PURE__*/function () {
18
23
  function ReferenceSyncBlockStoreManager(dataProvider) {
19
24
  _classCallCheck(this, ReferenceSyncBlockStoreManager);
20
- this.dataProvider = dataProvider;
25
+ _defineProperty(this, "isInitialized", false);
26
+ _defineProperty(this, "isRefreshingSubscriptions", false);
21
27
  this.syncBlockCache = new Map();
28
+ this.subscriptions = new Map();
29
+ this.dataProvider = dataProvider;
22
30
  this.syncBlockURLRequests = new Map();
23
31
  }
24
-
25
- /**
26
- *
27
- * @param localId - The local ID of the sync block to get the source URL for
28
- * @param resourceId - The resource ID of the sync block to get the source URL for
29
- * Fetches source URl for a sync block and updates sync block data with the source URL asynchronously.
30
- */
31
32
  return _createClass(ReferenceSyncBlockStoreManager, [{
33
+ key: "init",
34
+ value: function () {
35
+ var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(editorView) {
36
+ var _this = this;
37
+ var syncBlockNodes, dataResults;
38
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
39
+ while (1) switch (_context.prev = _context.next) {
40
+ case 0:
41
+ if (!(!this.editorView && !this.isInitialized)) {
42
+ _context.next = 15;
43
+ break;
44
+ }
45
+ this.editorView = editorView;
46
+ syncBlockNodes = editorView.state.doc.children.filter(function (node) {
47
+ return node.type.name === 'syncBlock';
48
+ }).map(function (node) {
49
+ return node.toJSON();
50
+ }) || [];
51
+ if (!(syncBlockNodes.length > 0)) {
52
+ _context.next = 15;
53
+ break;
54
+ }
55
+ _context.prev = 4;
56
+ _context.next = 7;
57
+ return this.fetchSyncBlocksData(syncBlockNodes);
58
+ case 7:
59
+ dataResults = _context.sent;
60
+ if (dataResults) {
61
+ _context.next = 10;
62
+ break;
63
+ }
64
+ throw new Error('No data results returned when initializing sync block store manager');
65
+ case 10:
66
+ dataResults.forEach(function (dataResult) {
67
+ _this.updateCache(dataResult);
68
+ });
69
+ _context.next = 15;
70
+ break;
71
+ case 13:
72
+ _context.prev = 13;
73
+ _context.t0 = _context["catch"](4);
74
+ case 15:
75
+ this.isInitialized = true;
76
+ case 16:
77
+ case "end":
78
+ return _context.stop();
79
+ }
80
+ }, _callee, this, [[4, 13]]);
81
+ }));
82
+ function init(_x) {
83
+ return _init.apply(this, arguments);
84
+ }
85
+ return init;
86
+ }()
87
+ /**
88
+ * Refreshes the subscriptions for all sync blocks.
89
+ * @returns {Promise<void>}
90
+ */
91
+ }, {
92
+ key: "refreshSubscriptions",
93
+ value: (function () {
94
+ var _refreshSubscriptions = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
95
+ var syncBlocks, _iterator, _step, _loop;
96
+ return _regeneratorRuntime.wrap(function _callee2$(_context3) {
97
+ while (1) switch (_context3.prev = _context3.next) {
98
+ case 0:
99
+ if (!(this.isRefreshingSubscriptions || !this.isInitialized)) {
100
+ _context3.next = 2;
101
+ break;
102
+ }
103
+ return _context3.abrupt("return");
104
+ case 2:
105
+ this.isRefreshingSubscriptions = true;
106
+ syncBlocks = [];
107
+ _iterator = _createForOfIteratorHelper(this.subscriptions.entries());
108
+ _context3.prev = 5;
109
+ _loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop() {
110
+ var _step$value, resourceId, callbacks;
111
+ return _regeneratorRuntime.wrap(function _loop$(_context2) {
112
+ while (1) switch (_context2.prev = _context2.next) {
113
+ case 0:
114
+ _step$value = _slicedToArray(_step.value, 2), resourceId = _step$value[0], callbacks = _step$value[1];
115
+ Object.keys(callbacks).forEach(function (localId) {
116
+ syncBlocks.push(createSyncBlockNode(localId, resourceId));
117
+ });
118
+ case 2:
119
+ case "end":
120
+ return _context2.stop();
121
+ }
122
+ }, _loop);
123
+ });
124
+ _iterator.s();
125
+ case 8:
126
+ if ((_step = _iterator.n()).done) {
127
+ _context3.next = 12;
128
+ break;
129
+ }
130
+ return _context3.delegateYield(_loop(), "t0", 10);
131
+ case 10:
132
+ _context3.next = 8;
133
+ break;
134
+ case 12:
135
+ _context3.next = 17;
136
+ break;
137
+ case 14:
138
+ _context3.prev = 14;
139
+ _context3.t1 = _context3["catch"](5);
140
+ _iterator.e(_context3.t1);
141
+ case 17:
142
+ _context3.prev = 17;
143
+ _iterator.f();
144
+ return _context3.finish(17);
145
+ case 20:
146
+ _context3.prev = 20;
147
+ _context3.next = 23;
148
+ return this.fetchSyncBlocksData(syncBlocks);
149
+ case 23:
150
+ _context3.next = 27;
151
+ break;
152
+ case 25:
153
+ _context3.prev = 25;
154
+ _context3.t2 = _context3["catch"](20);
155
+ case 27:
156
+ _context3.prev = 27;
157
+ this.isRefreshingSubscriptions = false;
158
+ return _context3.finish(27);
159
+ case 30:
160
+ case "end":
161
+ return _context3.stop();
162
+ }
163
+ }, _callee2, this, [[5, 14, 17, 20], [20, 25, 27, 30]]);
164
+ }));
165
+ function refreshSubscriptions() {
166
+ return _refreshSubscriptions.apply(this, arguments);
167
+ }
168
+ return refreshSubscriptions;
169
+ }())
170
+ }, {
32
171
  key: "fetchSyncBlockSourceURL",
33
- value: function fetchSyncBlockSourceURL(_ref) {
34
- var _this = this;
35
- var localId = _ref.localId,
36
- resourceId = _ref.resourceId;
37
- if (!localId || !resourceId || !this.dataProvider) {
172
+ value: function fetchSyncBlockSourceURL(resourceId) {
173
+ var _this2 = this;
174
+ if (!resourceId || !this.dataProvider) {
38
175
  return;
39
176
  }
40
177
 
41
178
  // if the sync block is a reference block, we need to fetch the URL to the source
42
179
  // we could optimise this further by checking if the sync block is on the same page as the source
43
- if (!this.syncBlockURLRequests.get(localId)) {
44
- this.syncBlockURLRequests.set(localId, true);
45
- this.dataProvider.retrieveSyncBlockSourceUrl(createSyncBlockNode(localId, resourceId)).then(function (sourceURL) {
46
- var existingSyncBlock = _this.syncBlockCache.get(localId);
47
- if (existingSyncBlock) {
48
- existingSyncBlock.sourceURL = sourceURL;
180
+ if (!this.syncBlockURLRequests.get(resourceId)) {
181
+ this.syncBlockURLRequests.set(resourceId, true);
182
+ this.dataProvider.retrieveSyncBlockSourceUrl(createSyncBlockNode('', resourceId)).then(function (sourceURL) {
183
+ var existingSyncBlock = _this2.getFromCache(resourceId);
184
+ if (existingSyncBlock && existingSyncBlock.data) {
185
+ existingSyncBlock.data = _objectSpread(_objectSpread({}, existingSyncBlock.data), {}, {
186
+ sourceURL: sourceURL
187
+ });
188
+ _this2.updateCache(existingSyncBlock);
49
189
  }
50
190
  }).finally(function () {
51
- _this.syncBlockURLRequests.set(localId, false);
191
+ _this2.syncBlockURLRequests.set(resourceId, false);
52
192
  });
53
193
  }
54
194
  }
195
+
196
+ /**
197
+ * Fetch sync block data for a given sync block node.
198
+ * @param syncBlockNode - The sync block node to fetch data for
199
+ * @returns The fetched sync block data result
200
+ */
55
201
  }, {
56
202
  key: "fetchSyncBlockData",
57
- value: function () {
58
- var _fetchSyncBlockData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(syncBlockNode) {
59
- var syncNode, existingSyncBlock, data, fetchSyncBlockDataResult;
60
- return _regeneratorRuntime.wrap(function _callee$(_context) {
61
- while (1) switch (_context.prev = _context.next) {
203
+ value: (function () {
204
+ var _fetchSyncBlockData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(syncBlockNode) {
205
+ var syncNode, data;
206
+ return _regeneratorRuntime.wrap(function _callee3$(_context4) {
207
+ while (1) switch (_context4.prev = _context4.next) {
62
208
  case 0:
63
209
  if (this.dataProvider) {
64
- _context.next = 2;
210
+ _context4.next = 2;
65
211
  break;
66
212
  }
67
213
  throw new Error('Data provider not set');
68
214
  case 2:
69
- syncNode = createSyncBlockNode(syncBlockNode.attrs.localId, syncBlockNode.attrs.resourceId); // async fetch source URL if it is not already fetched
70
- existingSyncBlock = this.syncBlockCache.get(syncBlockNode.attrs.localId);
71
- if (!(existingSyncBlock !== null && existingSyncBlock !== void 0 && existingSyncBlock.sourceURL)) {
72
- this.fetchSyncBlockSourceURL({
73
- localId: syncBlockNode.attrs.localId,
74
- resourceId: syncBlockNode.attrs.resourceId
75
- });
215
+ syncNode = createSyncBlockNode(syncBlockNode.attrs.localId, syncBlockNode.attrs.resourceId);
216
+ _context4.next = 5;
217
+ return this.fetchSyncBlocksData([syncNode]);
218
+ case 5:
219
+ data = _context4.sent;
220
+ if (!(!data || data.length === 0)) {
221
+ _context4.next = 8;
222
+ break;
76
223
  }
77
- _context.prev = 5;
78
- _context.next = 8;
79
- return this.dataProvider.fetchNodesData([syncNode]);
224
+ throw new Error('Failed to fetch sync block data');
80
225
  case 8:
81
- data = _context.sent;
226
+ return _context4.abrupt("return", data[0]);
227
+ case 9:
228
+ case "end":
229
+ return _context4.stop();
230
+ }
231
+ }, _callee3, this);
232
+ }));
233
+ function fetchSyncBlockData(_x2) {
234
+ return _fetchSyncBlockData.apply(this, arguments);
235
+ }
236
+ return fetchSyncBlockData;
237
+ }())
238
+ }, {
239
+ key: "fetchSyncBlocksData",
240
+ value: function () {
241
+ var _fetchSyncBlocksData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(syncBlockNodes) {
242
+ var _this3 = this;
243
+ var data, resolvedData;
244
+ return _regeneratorRuntime.wrap(function _callee4$(_context5) {
245
+ while (1) switch (_context5.prev = _context5.next) {
246
+ case 0:
247
+ if (this.dataProvider) {
248
+ _context5.next = 2;
249
+ break;
250
+ }
251
+ throw new Error('Data provider not set');
252
+ case 2:
253
+ _context5.next = 4;
254
+ return this.dataProvider.fetchNodesData(syncBlockNodes);
255
+ case 4:
256
+ data = _context5.sent;
82
257
  if (data) {
83
- _context.next = 11;
258
+ _context5.next = 7;
84
259
  break;
85
260
  }
86
261
  throw new Error('Failed to fetch sync block node data');
87
- case 11:
88
- _context.next = 16;
89
- break;
90
- case 13:
91
- _context.prev = 13;
92
- _context.t0 = _context["catch"](5);
93
- throw new Error('Failed to fetch sync block node data');
94
- case 16:
95
- fetchSyncBlockDataResult = data[0];
96
- if (!fetchSyncBlockDataResult.error && fetchSyncBlockDataResult.data) {
97
- // only adds it to the map if it did not error out
98
- this.syncBlockCache.set(syncBlockNode.attrs.localId, _objectSpread(_objectSpread({}, existingSyncBlock), fetchSyncBlockDataResult.data));
99
- }
100
- return _context.abrupt("return", fetchSyncBlockDataResult);
101
- case 19:
262
+ case 7:
263
+ resolvedData = [];
264
+ data.forEach(function (fetchSyncBlockDataResult) {
265
+ var _resolvedFetchSyncBlo;
266
+ if (!fetchSyncBlockDataResult.resourceId) {
267
+ return;
268
+ }
269
+ var existingSyncBlock = _this3.getFromCache(fetchSyncBlockDataResult.resourceId);
270
+ var resolvedFetchSyncBlockDataResult = existingSyncBlock ? resolveFetchSyncBlockDataResult(existingSyncBlock, fetchSyncBlockDataResult) : fetchSyncBlockDataResult;
271
+ _this3.updateCache(resolvedFetchSyncBlockDataResult);
272
+ resolvedData.push(resolvedFetchSyncBlockDataResult);
273
+
274
+ // fetch source URL if not already present
275
+ if (!((_resolvedFetchSyncBlo = resolvedFetchSyncBlockDataResult.data) !== null && _resolvedFetchSyncBlo !== void 0 && _resolvedFetchSyncBlo.sourceURL) && resolvedFetchSyncBlockDataResult.resourceId) {
276
+ _this3.fetchSyncBlockSourceURL(resolvedFetchSyncBlockDataResult.resourceId);
277
+ }
278
+ });
279
+ return _context5.abrupt("return", resolvedData);
280
+ case 10:
102
281
  case "end":
103
- return _context.stop();
282
+ return _context5.stop();
104
283
  }
105
- }, _callee, this, [[5, 13]]);
284
+ }, _callee4, this);
106
285
  }));
107
- function fetchSyncBlockData(_x) {
108
- return _fetchSyncBlockData.apply(this, arguments);
286
+ function fetchSyncBlocksData(_x3) {
287
+ return _fetchSyncBlocksData.apply(this, arguments);
109
288
  }
110
- return fetchSyncBlockData;
289
+ return fetchSyncBlocksData;
111
290
  }()
291
+ }, {
292
+ key: "updateCache",
293
+ value: function updateCache(syncBlock) {
294
+ var resourceId = syncBlock.resourceId;
295
+ if (resourceId) {
296
+ this.syncBlockCache.set(resourceId, syncBlock);
297
+ var callbacks = this.subscriptions.get(resourceId);
298
+ if (callbacks) {
299
+ Object.values(callbacks).forEach(function (callback) {
300
+ callback(syncBlock);
301
+ });
302
+ }
303
+ }
304
+ }
305
+ }, {
306
+ key: "getFromCache",
307
+ value: function getFromCache(resourceId) {
308
+ return this.syncBlockCache.get(resourceId);
309
+ }
310
+ }, {
311
+ key: "deleteFromCache",
312
+ value: function deleteFromCache(resourceId) {
313
+ this.syncBlockCache.delete(resourceId);
314
+ }
315
+ }, {
316
+ key: "subscribe",
317
+ value: function subscribe(node, callback) {
318
+ var _this4 = this;
319
+ // check node is a sync block, as we only support sync block subscriptions
320
+ if (node.type.name !== 'syncBlock') {
321
+ return function () {};
322
+ }
323
+ var _node$attrs = node.attrs,
324
+ resourceId = _node$attrs.resourceId,
325
+ localId = _node$attrs.localId;
326
+ if (!localId || !resourceId) {
327
+ return function () {};
328
+ }
329
+
330
+ // add to subscriptions map
331
+ var resourceSubscriptions = this.subscriptions.get(resourceId) || {};
332
+ this.subscriptions.set(resourceId, _objectSpread(_objectSpread({}, resourceSubscriptions), {}, _defineProperty({}, localId, callback)));
333
+
334
+ // call the callback immediately if we have cached data
335
+ var cachedData = this.getFromCache(resourceId);
336
+ if (cachedData) {
337
+ callback(cachedData);
338
+ } else {
339
+ this.fetchSyncBlockData(node).catch(function () {});
340
+ }
341
+ return function () {
342
+ var resourceSubscriptions = _this4.subscriptions.get(resourceId);
343
+ if (resourceSubscriptions) {
344
+ delete resourceSubscriptions[localId];
345
+ if (Object.keys(resourceSubscriptions).length === 0) {
346
+ _this4.subscriptions.delete(resourceId);
347
+ _this4.deleteFromCache(resourceId);
348
+ } else {
349
+ _this4.subscriptions.set(resourceId, resourceSubscriptions);
350
+ }
351
+ }
352
+ };
353
+ }
354
+
112
355
  /**
113
356
  * Get the URL for a sync block.
114
- * @param localId - The local ID of the sync block to get the URL for
357
+ * @param resourceId - The resource ID of the sync block
115
358
  * @returns
116
359
  */
117
360
  }, {
118
361
  key: "getSyncBlockURL",
119
- value: function getSyncBlockURL(localId) {
120
- var syncBlock = this.syncBlockCache.get(localId);
121
- return syncBlock === null || syncBlock === void 0 ? void 0 : syncBlock.sourceURL;
362
+ value: function getSyncBlockURL(resourceId) {
363
+ var _syncBlock$data;
364
+ var syncBlock = this.getFromCache(resourceId);
365
+ if (!syncBlock) {
366
+ return undefined;
367
+ }
368
+ return (_syncBlock$data = syncBlock.data) === null || _syncBlock$data === void 0 ? void 0 : _syncBlock$data.sourceURL;
369
+ }
370
+ }, {
371
+ key: "destroy",
372
+ value: function destroy() {
373
+ this.syncBlockCache.clear();
374
+ this.subscriptions.clear();
375
+ this.syncBlockURLRequests.clear();
376
+ this.editorView = undefined;
377
+ this.isInitialized = false;
122
378
  }
123
379
  }]);
124
380
  }();
@@ -14,6 +14,12 @@ export var SyncBlockStoreManager = /*#__PURE__*/function () {
14
14
  this.referenceSyncBlockStoreManager = new ReferenceSyncBlockStoreManager(dataProvider);
15
15
  this.sourceSyncBlockStoreManager = new SourceSyncBlockStoreManager(dataProvider);
16
16
  }
17
+
18
+ /**
19
+ * Fetch sync block data for a given sync block node.
20
+ * @param syncBlockNode - The sync block node to fetch data for
21
+ * @returns The fetched sync block data result
22
+ */
17
23
  return _createClass(SyncBlockStoreManager, [{
18
24
  key: "fetchSyncBlockData",
19
25
  value: function fetchSyncBlockData(syncBlockNode) {
@@ -54,20 +60,22 @@ export var SyncBlockStoreManager = /*#__PURE__*/function () {
54
60
 
55
61
  /**
56
62
  * Get the URL for a sync block.
57
- * @param localId - The local ID of the sync block to get the URL for
63
+ * @param resourceId - The resource ID of the sync block to get the URL for
58
64
  * @returns
59
65
  */
60
66
  }, {
61
67
  key: "getSyncBlockURL",
62
- value: function getSyncBlockURL(localId) {
68
+ value: function getSyncBlockURL(resourceId) {
63
69
  // only applicable to reference sync block, for now (will be refactored further)
64
- return this.referenceSyncBlockStoreManager.getSyncBlockURL(localId);
70
+ return this.referenceSyncBlockStoreManager.getSyncBlockURL(resourceId);
65
71
  }
66
72
  }, {
67
73
  key: "setEditorView",
68
74
  value: function setEditorView(editorView) {
69
- // only applicable to source sync block, for now (will be refactored further)
70
75
  this.sourceSyncBlockStoreManager.setEditorView(editorView);
76
+ if (editorView) {
77
+ this.referenceSyncBlockStoreManager.init(editorView);
78
+ }
71
79
  }
72
80
  }, {
73
81
  key: "isSourceBlock",
@@ -92,6 +100,16 @@ export var SyncBlockStoreManager = /*#__PURE__*/function () {
92
100
  // only applicable to source sync block, for now (will be refactored further)
93
101
  return this.sourceSyncBlockStoreManager.createSyncBlockNode();
94
102
  }
103
+ }, {
104
+ key: "subscribeToSyncBlockData",
105
+ value: function subscribeToSyncBlockData(node, callback) {
106
+ return this.referenceSyncBlockStoreManager.subscribe(node, callback);
107
+ }
108
+ }, {
109
+ key: "refreshSubscriptions",
110
+ value: function refreshSubscriptions() {
111
+ this.referenceSyncBlockStoreManager.refreshSubscriptions();
112
+ }
95
113
  }, {
96
114
  key: "deleteSyncBlocksWithConfirmation",
97
115
  value: function deleteSyncBlocksWithConfirmation(tr, syncBlockIds) {
@@ -104,5 +122,10 @@ export var SyncBlockStoreManager = /*#__PURE__*/function () {
104
122
  // only applicable to source sync block, for now (will be refactored further)
105
123
  this.sourceSyncBlockStoreManager.rebaseTransaction(incomingTr, state);
106
124
  }
125
+ }, {
126
+ key: "destroy",
127
+ value: function destroy() {
128
+ this.referenceSyncBlockStoreManager.destroy();
129
+ }
107
130
  }]);
108
131
  }();
@@ -0,0 +1,31 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ /**
5
+ * Merges two FetchSyncBlockDataResult objects,
6
+ * currently it only preserves the sourceURL from the old result,
7
+ * but this can be extended in the future to preserve other fields and resolve conflicts as needed.
8
+ * e.g. compare timestamps or version numbers to determine which data is more recent.
9
+ *
10
+ * @param oldResult - The existing FetchSyncBlockDataResult object.
11
+ * @param newResult - The new FetchSyncBlockDataResult object to merge.
12
+ * @returns A merged FetchSyncBlockDataResult object.
13
+ */
14
+ export var resolveFetchSyncBlockDataResult = function resolveFetchSyncBlockDataResult(oldResult, newResult) {
15
+ var _newResult$data, _oldResult$data;
16
+ // if the old result has no data, we simple return the new result
17
+ if (!oldResult.data) {
18
+ return newResult;
19
+ } else if (!newResult.data) {
20
+ // if the new result has no data, we simply return the old result
21
+ // TODO: EDITOR-2533 - handle this case based on the error type and whether we should keep old data or not
22
+ return oldResult;
23
+ }
24
+
25
+ // otherwise, we merge the two results, preserving the sourceURL from the old result if it exists
26
+ return _objectSpread(_objectSpread({}, newResult), {}, {
27
+ data: _objectSpread(_objectSpread({}, newResult.data), {}, {
28
+ sourceURL: ((_newResult$data = newResult.data) === null || _newResult$data === void 0 ? void 0 : _newResult$data.sourceURL) || ((_oldResult$data = oldResult.data) === null || _oldResult$data === void 0 ? void 0 : _oldResult$data.sourceURL) || undefined
29
+ })
30
+ });
31
+ };
@@ -3,4 +3,4 @@
3
3
  * Otherwise we could import defaultSchemaConfig from '@atlaskit/adf-schema/schema-default';
4
4
  * @returns
5
5
  */
6
- export declare const getDefaultSyncBlockSchema: () => import("prosemirror-model").Schema<"doc" | "paragraph" | "text" | "bulletList" | "orderedList" | "listItem" | "heading" | "blockquote" | "codeBlock" | "panel" | "rule" | "expand" | "nestedExpand" | "table" | "tableCell" | "tableHeader" | "tableRow" | "date" | "status" | "layoutSection" | "layoutColumn" | "unsupportedBlock" | "unsupportedInline", "link" | "em" | "strong" | "strike" | "subsup" | "underline" | "code" | "textColor" | "backgroundColor" | "alignment" | "indentation" | "border" | "unsupportedMark" | "unsupportedNodeAttribute" | "typeAheadQuery">;
6
+ export declare const getDefaultSyncBlockSchema: () => import("prosemirror-model").Schema<"status" | "doc" | "paragraph" | "text" | "bulletList" | "orderedList" | "listItem" | "heading" | "blockquote" | "codeBlock" | "panel" | "rule" | "expand" | "nestedExpand" | "table" | "tableCell" | "tableHeader" | "tableRow" | "date" | "layoutSection" | "layoutColumn" | "unsupportedBlock" | "unsupportedInline", "link" | "em" | "strong" | "strike" | "subsup" | "underline" | "code" | "textColor" | "backgroundColor" | "alignment" | "indentation" | "border" | "unsupportedMark" | "unsupportedNodeAttribute" | "typeAheadQuery">;
@@ -1,5 +1,5 @@
1
1
  import type { JSONNode } from '@atlaskit/editor-json-transformer/types';
2
- import type { SyncBlockData, SyncBlockNode } from '../common/types';
2
+ import { type SyncBlockData, type SyncBlockNode } from '../common/types';
3
3
  import { SyncBlockDataProvider, type ADFFetchProvider, type ADFWriteProvider, type FetchSyncBlockDataResult } from '../providers/types';
4
4
  export declare class SyncBlockProvider extends SyncBlockDataProvider {
5
5
  name: string;
@@ -16,3 +16,4 @@ export declare abstract class SyncBlockDataProvider extends NodeDataProvider<Syn
16
16
  abstract getSourceId(): ResourceId;
17
17
  abstract retrieveSyncBlockSourceUrl(node: SyncBlockNode): Promise<string | undefined>;
18
18
  }
19
+ export type SubscriptionCallback = (data: FetchSyncBlockDataResult) => void;
@@ -1,23 +1,39 @@
1
1
  import { type Node as PMNode } from '@atlaskit/editor-prosemirror/model';
2
- import type { BlockInstanceId } from '../common/types';
3
- import type { FetchSyncBlockDataResult, SyncBlockDataProvider } from '../providers/types';
2
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
3
+ import type { ResourceId, SyncBlockNode } from '../common/types';
4
+ import type { FetchSyncBlockDataResult, SubscriptionCallback, SyncBlockDataProvider } from '../providers/types';
4
5
  export declare class ReferenceSyncBlockStoreManager {
5
6
  private dataProvider?;
6
7
  private syncBlockCache;
8
+ private subscriptions;
7
9
  private syncBlockURLRequests;
10
+ private editorView?;
11
+ private isInitialized;
12
+ private isRefreshingSubscriptions;
8
13
  constructor(dataProvider?: SyncBlockDataProvider);
14
+ init(editorView: EditorView): Promise<void>;
9
15
  /**
10
- *
11
- * @param localId - The local ID of the sync block to get the source URL for
12
- * @param resourceId - The resource ID of the sync block to get the source URL for
13
- * Fetches source URl for a sync block and updates sync block data with the source URL asynchronously.
16
+ * Refreshes the subscriptions for all sync blocks.
17
+ * @returns {Promise<void>}
14
18
  */
19
+ refreshSubscriptions(): Promise<void>;
15
20
  private fetchSyncBlockSourceURL;
21
+ /**
22
+ * Fetch sync block data for a given sync block node.
23
+ * @param syncBlockNode - The sync block node to fetch data for
24
+ * @returns The fetched sync block data result
25
+ */
16
26
  fetchSyncBlockData(syncBlockNode: PMNode): Promise<FetchSyncBlockDataResult>;
27
+ fetchSyncBlocksData(syncBlockNodes: SyncBlockNode[]): Promise<FetchSyncBlockDataResult[]>;
28
+ private updateCache;
29
+ private getFromCache;
30
+ private deleteFromCache;
31
+ subscribe(node: PMNode, callback: SubscriptionCallback): () => void;
17
32
  /**
18
33
  * Get the URL for a sync block.
19
- * @param localId - The local ID of the sync block to get the URL for
34
+ * @param resourceId - The resource ID of the sync block
20
35
  * @returns
21
36
  */
22
- getSyncBlockURL(localId: BlockInstanceId): string | undefined;
37
+ getSyncBlockURL(resourceId: ResourceId): string | undefined;
38
+ destroy(): void;
23
39
  }
@@ -1,13 +1,18 @@
1
1
  import type { EditorView } from '@atlaskit/editor-prosemirror/dist/types/view';
2
2
  import { type Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
3
  import type { EditorState, Transaction } from '@atlaskit/editor-prosemirror/state';
4
- import type { BlockInstanceId, SyncBlockAttrs, SyncBlockNode } from '../common/types';
5
- import type { FetchSyncBlockDataResult, SyncBlockDataProvider } from '../providers/types';
4
+ import type { ResourceId, SyncBlockAttrs, SyncBlockNode } from '../common/types';
5
+ import type { FetchSyncBlockDataResult, SubscriptionCallback, SyncBlockDataProvider } from '../providers/types';
6
6
  import { type ConfirmationCallback } from './sourceSyncBlockStoreManager';
7
7
  export declare class SyncBlockStoreManager {
8
8
  private referenceSyncBlockStoreManager;
9
9
  private sourceSyncBlockStoreManager;
10
10
  constructor(dataProvider?: SyncBlockDataProvider);
11
+ /**
12
+ * Fetch sync block data for a given sync block node.
13
+ * @param syncBlockNode - The sync block node to fetch data for
14
+ * @returns The fetched sync block data result
15
+ */
11
16
  fetchSyncBlockData(syncBlockNode: PMNode): Promise<FetchSyncBlockDataResult | undefined>;
12
17
  /**
13
18
  * Add/update a sync block node to/from the local cache
@@ -22,15 +27,18 @@ export declare class SyncBlockStoreManager {
22
27
  flushBodiedSyncBlocks(): Promise<boolean>;
23
28
  /**
24
29
  * Get the URL for a sync block.
25
- * @param localId - The local ID of the sync block to get the URL for
30
+ * @param resourceId - The resource ID of the sync block to get the URL for
26
31
  * @returns
27
32
  */
28
- getSyncBlockURL(localId: BlockInstanceId): string | undefined;
33
+ getSyncBlockURL(resourceId: ResourceId): string | undefined;
29
34
  setEditorView(editorView: EditorView | undefined): void;
30
35
  isSourceBlock(node: PMNode): boolean;
31
36
  registerConfirmationCallback(callback: ConfirmationCallback): () => void;
32
37
  requireConfirmationBeforeDelete(): boolean;
33
38
  createSyncBlockNode(): SyncBlockNode;
39
+ subscribeToSyncBlockData(node: PMNode, callback: SubscriptionCallback): () => void;
40
+ refreshSubscriptions(): void;
34
41
  deleteSyncBlocksWithConfirmation(tr: Transaction, syncBlockIds: SyncBlockAttrs[]): Promise<void>;
35
42
  rebaseTransaction(incomingTr: Transaction, state: EditorState): void;
43
+ destroy(): void;
36
44
  }