@atlaskit/editor-synced-block-provider 2.2.3 → 2.3.1

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 (47) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/hooks/useFetchSyncBlockData.js +5 -30
  3. package/dist/cjs/providers/confluence/confluenceContentAPI.js +50 -0
  4. package/dist/cjs/providers/in-memory/inMemory.js +7 -0
  5. package/dist/cjs/providers/syncBlockProvider.js +75 -19
  6. package/dist/cjs/store-manager/referenceSyncBlockStoreManager.js +319 -63
  7. package/dist/cjs/store-manager/sourceSyncBlockStoreManager.js +35 -11
  8. package/dist/cjs/store-manager/syncBlockStoreManager.js +27 -4
  9. package/dist/cjs/utils/contentProperty.js +54 -1
  10. package/dist/cjs/utils/mergeFetchSyncBlockDataResult.js +38 -0
  11. package/dist/es2019/hooks/useFetchSyncBlockData.js +6 -31
  12. package/dist/es2019/providers/confluence/confluenceContentAPI.js +31 -1
  13. package/dist/es2019/providers/in-memory/inMemory.js +7 -0
  14. package/dist/es2019/providers/syncBlockProvider.js +31 -3
  15. package/dist/es2019/store-manager/referenceSyncBlockStoreManager.js +172 -44
  16. package/dist/es2019/store-manager/sourceSyncBlockStoreManager.js +16 -4
  17. package/dist/es2019/store-manager/syncBlockStoreManager.js +21 -4
  18. package/dist/es2019/utils/contentProperty.js +54 -0
  19. package/dist/es2019/utils/mergeFetchSyncBlockDataResult.js +30 -0
  20. package/dist/esm/hooks/useFetchSyncBlockData.js +6 -31
  21. package/dist/esm/providers/confluence/confluenceContentAPI.js +51 -1
  22. package/dist/esm/providers/in-memory/inMemory.js +7 -0
  23. package/dist/esm/providers/syncBlockProvider.js +73 -17
  24. package/dist/esm/store-manager/referenceSyncBlockStoreManager.js +320 -64
  25. package/dist/esm/store-manager/sourceSyncBlockStoreManager.js +35 -11
  26. package/dist/esm/store-manager/syncBlockStoreManager.js +27 -4
  27. package/dist/esm/utils/contentProperty.js +53 -0
  28. package/dist/esm/utils/mergeFetchSyncBlockDataResult.js +31 -0
  29. package/dist/types/common/schema.d.ts +1 -1
  30. package/dist/types/providers/confluence/confluenceContentAPI.d.ts +2 -1
  31. package/dist/types/providers/syncBlockProvider.d.ts +3 -2
  32. package/dist/types/providers/types.d.ts +8 -0
  33. package/dist/types/store-manager/referenceSyncBlockStoreManager.d.ts +24 -8
  34. package/dist/types/store-manager/sourceSyncBlockStoreManager.d.ts +1 -1
  35. package/dist/types/store-manager/syncBlockStoreManager.d.ts +12 -4
  36. package/dist/types/utils/contentProperty.d.ts +35 -0
  37. package/dist/types/utils/mergeFetchSyncBlockDataResult.d.ts +12 -0
  38. package/dist/types-ts4.5/common/schema.d.ts +1 -1
  39. package/dist/types-ts4.5/providers/confluence/confluenceContentAPI.d.ts +2 -1
  40. package/dist/types-ts4.5/providers/syncBlockProvider.d.ts +3 -2
  41. package/dist/types-ts4.5/providers/types.d.ts +8 -0
  42. package/dist/types-ts4.5/store-manager/referenceSyncBlockStoreManager.d.ts +24 -8
  43. package/dist/types-ts4.5/store-manager/sourceSyncBlockStoreManager.d.ts +1 -1
  44. package/dist/types-ts4.5/store-manager/syncBlockStoreManager.d.ts +12 -4
  45. package/dist/types-ts4.5/utils/contentProperty.d.ts +35 -0
  46. package/dist/types-ts4.5/utils/mergeFetchSyncBlockDataResult.d.ts +12 -0
  47. package/package.json +1 -1
@@ -6,12 +6,17 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.ReferenceSyncBlockStoreManager = void 0;
8
8
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
11
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
12
12
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
+ var _mergeFetchSyncBlockDataResult = require("../utils/mergeFetchSyncBlockDataResult");
13
15
  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; }
14
16
  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) { (0, _defineProperty2.default)(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; }
17
+ 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; } } }; }
18
+ 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; } }
19
+ 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; }
15
20
  var createSyncBlockNode = function createSyncBlockNode(localId, resourceId) {
16
21
  return {
17
22
  type: 'syncBlock',
@@ -24,108 +29,359 @@ var createSyncBlockNode = function createSyncBlockNode(localId, resourceId) {
24
29
  var ReferenceSyncBlockStoreManager = exports.ReferenceSyncBlockStoreManager = /*#__PURE__*/function () {
25
30
  function ReferenceSyncBlockStoreManager(dataProvider) {
26
31
  (0, _classCallCheck2.default)(this, ReferenceSyncBlockStoreManager);
27
- this.dataProvider = dataProvider;
32
+ (0, _defineProperty2.default)(this, "isInitialized", false);
33
+ (0, _defineProperty2.default)(this, "isRefreshingSubscriptions", false);
28
34
  this.syncBlockCache = new Map();
35
+ this.subscriptions = new Map();
36
+ this.dataProvider = dataProvider;
29
37
  this.syncBlockURLRequests = new Map();
30
38
  }
31
-
32
- /**
33
- *
34
- * @param localId - The local ID of the sync block to get the source URL for
35
- * @param resourceId - The resource ID of the sync block to get the source URL for
36
- * Fetches source URl for a sync block and updates sync block data with the source URL asynchronously.
37
- */
38
39
  return (0, _createClass2.default)(ReferenceSyncBlockStoreManager, [{
40
+ key: "init",
41
+ value: function () {
42
+ var _init = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(editorView) {
43
+ var _this = this;
44
+ var syncBlockNodes, dataResults;
45
+ return _regenerator.default.wrap(function _callee$(_context) {
46
+ while (1) switch (_context.prev = _context.next) {
47
+ case 0:
48
+ if (!(!this.editorView && !this.isInitialized)) {
49
+ _context.next = 15;
50
+ break;
51
+ }
52
+ this.editorView = editorView;
53
+ syncBlockNodes = editorView.state.doc.children.filter(function (node) {
54
+ return node.type.name === 'syncBlock';
55
+ }).map(function (node) {
56
+ return node.toJSON();
57
+ }) || [];
58
+ if (!(syncBlockNodes.length > 0)) {
59
+ _context.next = 15;
60
+ break;
61
+ }
62
+ _context.prev = 4;
63
+ _context.next = 7;
64
+ return this.fetchSyncBlocksData(syncBlockNodes);
65
+ case 7:
66
+ dataResults = _context.sent;
67
+ if (dataResults) {
68
+ _context.next = 10;
69
+ break;
70
+ }
71
+ throw new Error('No data results returned when initializing sync block store manager');
72
+ case 10:
73
+ dataResults.forEach(function (dataResult) {
74
+ _this.updateCache(dataResult);
75
+ });
76
+ _context.next = 15;
77
+ break;
78
+ case 13:
79
+ _context.prev = 13;
80
+ _context.t0 = _context["catch"](4);
81
+ case 15:
82
+ this.isInitialized = true;
83
+ case 16:
84
+ case "end":
85
+ return _context.stop();
86
+ }
87
+ }, _callee, this, [[4, 13]]);
88
+ }));
89
+ function init(_x) {
90
+ return _init.apply(this, arguments);
91
+ }
92
+ return init;
93
+ }()
94
+ /**
95
+ * Refreshes the subscriptions for all sync blocks.
96
+ * @returns {Promise<void>}
97
+ */
98
+ }, {
99
+ key: "refreshSubscriptions",
100
+ value: (function () {
101
+ var _refreshSubscriptions = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
102
+ var syncBlocks, _iterator, _step, _loop;
103
+ return _regenerator.default.wrap(function _callee2$(_context3) {
104
+ while (1) switch (_context3.prev = _context3.next) {
105
+ case 0:
106
+ if (!(this.isRefreshingSubscriptions || !this.isInitialized)) {
107
+ _context3.next = 2;
108
+ break;
109
+ }
110
+ return _context3.abrupt("return");
111
+ case 2:
112
+ this.isRefreshingSubscriptions = true;
113
+ syncBlocks = [];
114
+ _iterator = _createForOfIteratorHelper(this.subscriptions.entries());
115
+ _context3.prev = 5;
116
+ _loop = /*#__PURE__*/_regenerator.default.mark(function _loop() {
117
+ var _step$value, resourceId, callbacks;
118
+ return _regenerator.default.wrap(function _loop$(_context2) {
119
+ while (1) switch (_context2.prev = _context2.next) {
120
+ case 0:
121
+ _step$value = (0, _slicedToArray2.default)(_step.value, 2), resourceId = _step$value[0], callbacks = _step$value[1];
122
+ Object.keys(callbacks).forEach(function (localId) {
123
+ syncBlocks.push(createSyncBlockNode(localId, resourceId));
124
+ });
125
+ case 2:
126
+ case "end":
127
+ return _context2.stop();
128
+ }
129
+ }, _loop);
130
+ });
131
+ _iterator.s();
132
+ case 8:
133
+ if ((_step = _iterator.n()).done) {
134
+ _context3.next = 12;
135
+ break;
136
+ }
137
+ return _context3.delegateYield(_loop(), "t0", 10);
138
+ case 10:
139
+ _context3.next = 8;
140
+ break;
141
+ case 12:
142
+ _context3.next = 17;
143
+ break;
144
+ case 14:
145
+ _context3.prev = 14;
146
+ _context3.t1 = _context3["catch"](5);
147
+ _iterator.e(_context3.t1);
148
+ case 17:
149
+ _context3.prev = 17;
150
+ _iterator.f();
151
+ return _context3.finish(17);
152
+ case 20:
153
+ _context3.prev = 20;
154
+ _context3.next = 23;
155
+ return this.fetchSyncBlocksData(syncBlocks);
156
+ case 23:
157
+ _context3.next = 27;
158
+ break;
159
+ case 25:
160
+ _context3.prev = 25;
161
+ _context3.t2 = _context3["catch"](20);
162
+ case 27:
163
+ _context3.prev = 27;
164
+ this.isRefreshingSubscriptions = false;
165
+ return _context3.finish(27);
166
+ case 30:
167
+ case "end":
168
+ return _context3.stop();
169
+ }
170
+ }, _callee2, this, [[5, 14, 17, 20], [20, 25, 27, 30]]);
171
+ }));
172
+ function refreshSubscriptions() {
173
+ return _refreshSubscriptions.apply(this, arguments);
174
+ }
175
+ return refreshSubscriptions;
176
+ }())
177
+ }, {
39
178
  key: "fetchSyncBlockSourceURL",
40
- value: function fetchSyncBlockSourceURL(_ref) {
41
- var _this = this;
42
- var localId = _ref.localId,
43
- resourceId = _ref.resourceId;
44
- if (!localId || !resourceId || !this.dataProvider) {
179
+ value: function fetchSyncBlockSourceURL(resourceId) {
180
+ var _this2 = this;
181
+ if (!resourceId || !this.dataProvider) {
45
182
  return;
46
183
  }
47
184
 
48
185
  // if the sync block is a reference block, we need to fetch the URL to the source
49
186
  // we could optimise this further by checking if the sync block is on the same page as the source
50
- if (!this.syncBlockURLRequests.get(localId)) {
51
- this.syncBlockURLRequests.set(localId, true);
52
- this.dataProvider.retrieveSyncBlockSourceUrl(createSyncBlockNode(localId, resourceId)).then(function (sourceURL) {
53
- var existingSyncBlock = _this.syncBlockCache.get(localId);
54
- if (existingSyncBlock) {
55
- existingSyncBlock.sourceURL = sourceURL;
187
+ if (!this.syncBlockURLRequests.get(resourceId)) {
188
+ this.syncBlockURLRequests.set(resourceId, true);
189
+ this.dataProvider.retrieveSyncBlockSourceUrl(createSyncBlockNode('', resourceId)).then(function (sourceURL) {
190
+ var existingSyncBlock = _this2.getFromCache(resourceId);
191
+ if (existingSyncBlock && existingSyncBlock.data) {
192
+ existingSyncBlock.data = _objectSpread(_objectSpread({}, existingSyncBlock.data), {}, {
193
+ sourceURL: sourceURL
194
+ });
195
+ _this2.updateCache(existingSyncBlock);
56
196
  }
57
197
  }).finally(function () {
58
- _this.syncBlockURLRequests.set(localId, false);
198
+ _this2.syncBlockURLRequests.set(resourceId, false);
59
199
  });
60
200
  }
61
201
  }
202
+
203
+ /**
204
+ * Fetch sync block data for a given sync block node.
205
+ * @param syncBlockNode - The sync block node to fetch data for
206
+ * @returns The fetched sync block data result
207
+ */
62
208
  }, {
63
209
  key: "fetchSyncBlockData",
64
- value: function () {
65
- var _fetchSyncBlockData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(syncBlockNode) {
66
- var syncNode, existingSyncBlock, data, fetchSyncBlockDataResult;
67
- return _regenerator.default.wrap(function _callee$(_context) {
68
- while (1) switch (_context.prev = _context.next) {
210
+ value: (function () {
211
+ var _fetchSyncBlockData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(syncBlockNode) {
212
+ var syncNode, data;
213
+ return _regenerator.default.wrap(function _callee3$(_context4) {
214
+ while (1) switch (_context4.prev = _context4.next) {
69
215
  case 0:
70
216
  if (this.dataProvider) {
71
- _context.next = 2;
217
+ _context4.next = 2;
72
218
  break;
73
219
  }
74
220
  throw new Error('Data provider not set');
75
221
  case 2:
76
- syncNode = createSyncBlockNode(syncBlockNode.attrs.localId, syncBlockNode.attrs.resourceId); // async fetch source URL if it is not already fetched
77
- existingSyncBlock = this.syncBlockCache.get(syncBlockNode.attrs.localId);
78
- if (!(existingSyncBlock !== null && existingSyncBlock !== void 0 && existingSyncBlock.sourceURL)) {
79
- this.fetchSyncBlockSourceURL({
80
- localId: syncBlockNode.attrs.localId,
81
- resourceId: syncBlockNode.attrs.resourceId
82
- });
222
+ syncNode = createSyncBlockNode(syncBlockNode.attrs.localId, syncBlockNode.attrs.resourceId);
223
+ _context4.next = 5;
224
+ return this.fetchSyncBlocksData([syncNode]);
225
+ case 5:
226
+ data = _context4.sent;
227
+ if (!(!data || data.length === 0)) {
228
+ _context4.next = 8;
229
+ break;
83
230
  }
84
- _context.prev = 5;
85
- _context.next = 8;
86
- return this.dataProvider.fetchNodesData([syncNode]);
231
+ throw new Error('Failed to fetch sync block data');
87
232
  case 8:
88
- data = _context.sent;
233
+ return _context4.abrupt("return", data[0]);
234
+ case 9:
235
+ case "end":
236
+ return _context4.stop();
237
+ }
238
+ }, _callee3, this);
239
+ }));
240
+ function fetchSyncBlockData(_x2) {
241
+ return _fetchSyncBlockData.apply(this, arguments);
242
+ }
243
+ return fetchSyncBlockData;
244
+ }())
245
+ }, {
246
+ key: "fetchSyncBlocksData",
247
+ value: function () {
248
+ var _fetchSyncBlocksData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(syncBlockNodes) {
249
+ var _this3 = this;
250
+ var data, resolvedData;
251
+ return _regenerator.default.wrap(function _callee4$(_context5) {
252
+ while (1) switch (_context5.prev = _context5.next) {
253
+ case 0:
254
+ if (this.dataProvider) {
255
+ _context5.next = 2;
256
+ break;
257
+ }
258
+ throw new Error('Data provider not set');
259
+ case 2:
260
+ _context5.next = 4;
261
+ return this.dataProvider.fetchNodesData(syncBlockNodes);
262
+ case 4:
263
+ data = _context5.sent;
89
264
  if (data) {
90
- _context.next = 11;
265
+ _context5.next = 7;
91
266
  break;
92
267
  }
93
268
  throw new Error('Failed to fetch sync block node data');
94
- case 11:
95
- _context.next = 16;
96
- break;
97
- case 13:
98
- _context.prev = 13;
99
- _context.t0 = _context["catch"](5);
100
- throw new Error('Failed to fetch sync block node data');
101
- case 16:
102
- fetchSyncBlockDataResult = data[0];
103
- if (!fetchSyncBlockDataResult.error && fetchSyncBlockDataResult.data) {
104
- // only adds it to the map if it did not error out
105
- this.syncBlockCache.set(syncBlockNode.attrs.localId, _objectSpread(_objectSpread({}, existingSyncBlock), fetchSyncBlockDataResult.data));
106
- }
107
- return _context.abrupt("return", fetchSyncBlockDataResult);
108
- case 19:
269
+ case 7:
270
+ resolvedData = [];
271
+ data.forEach(function (fetchSyncBlockDataResult) {
272
+ var _resolvedFetchSyncBlo;
273
+ if (!fetchSyncBlockDataResult.resourceId) {
274
+ return;
275
+ }
276
+ var existingSyncBlock = _this3.getFromCache(fetchSyncBlockDataResult.resourceId);
277
+ var resolvedFetchSyncBlockDataResult = existingSyncBlock ? (0, _mergeFetchSyncBlockDataResult.resolveFetchSyncBlockDataResult)(existingSyncBlock, fetchSyncBlockDataResult) : fetchSyncBlockDataResult;
278
+ _this3.updateCache(resolvedFetchSyncBlockDataResult);
279
+ resolvedData.push(resolvedFetchSyncBlockDataResult);
280
+
281
+ // fetch source URL if not already present
282
+ if (!((_resolvedFetchSyncBlo = resolvedFetchSyncBlockDataResult.data) !== null && _resolvedFetchSyncBlo !== void 0 && _resolvedFetchSyncBlo.sourceURL) && resolvedFetchSyncBlockDataResult.resourceId) {
283
+ _this3.fetchSyncBlockSourceURL(resolvedFetchSyncBlockDataResult.resourceId);
284
+ }
285
+ });
286
+ return _context5.abrupt("return", resolvedData);
287
+ case 10:
109
288
  case "end":
110
- return _context.stop();
289
+ return _context5.stop();
111
290
  }
112
- }, _callee, this, [[5, 13]]);
291
+ }, _callee4, this);
113
292
  }));
114
- function fetchSyncBlockData(_x) {
115
- return _fetchSyncBlockData.apply(this, arguments);
293
+ function fetchSyncBlocksData(_x3) {
294
+ return _fetchSyncBlocksData.apply(this, arguments);
116
295
  }
117
- return fetchSyncBlockData;
296
+ return fetchSyncBlocksData;
118
297
  }()
298
+ }, {
299
+ key: "updateCache",
300
+ value: function updateCache(syncBlock) {
301
+ var resourceId = syncBlock.resourceId;
302
+ if (resourceId) {
303
+ this.syncBlockCache.set(resourceId, syncBlock);
304
+ var callbacks = this.subscriptions.get(resourceId);
305
+ if (callbacks) {
306
+ Object.values(callbacks).forEach(function (callback) {
307
+ callback(syncBlock);
308
+ });
309
+ }
310
+ }
311
+ }
312
+ }, {
313
+ key: "getFromCache",
314
+ value: function getFromCache(resourceId) {
315
+ return this.syncBlockCache.get(resourceId);
316
+ }
317
+ }, {
318
+ key: "deleteFromCache",
319
+ value: function deleteFromCache(resourceId) {
320
+ this.syncBlockCache.delete(resourceId);
321
+ }
322
+ }, {
323
+ key: "subscribe",
324
+ value: function subscribe(node, callback) {
325
+ var _this4 = this;
326
+ // check node is a sync block, as we only support sync block subscriptions
327
+ if (node.type.name !== 'syncBlock') {
328
+ return function () {};
329
+ }
330
+ var _node$attrs = node.attrs,
331
+ resourceId = _node$attrs.resourceId,
332
+ localId = _node$attrs.localId;
333
+ if (!localId || !resourceId) {
334
+ return function () {};
335
+ }
336
+
337
+ // add to subscriptions map
338
+ var resourceSubscriptions = this.subscriptions.get(resourceId) || {};
339
+ this.subscriptions.set(resourceId, _objectSpread(_objectSpread({}, resourceSubscriptions), {}, (0, _defineProperty2.default)({}, localId, callback)));
340
+
341
+ // call the callback immediately if we have cached data
342
+ var cachedData = this.getFromCache(resourceId);
343
+ if (cachedData) {
344
+ callback(cachedData);
345
+ } else {
346
+ this.fetchSyncBlockData(node).catch(function () {});
347
+ }
348
+ return function () {
349
+ var resourceSubscriptions = _this4.subscriptions.get(resourceId);
350
+ if (resourceSubscriptions) {
351
+ delete resourceSubscriptions[localId];
352
+ if (Object.keys(resourceSubscriptions).length === 0) {
353
+ _this4.subscriptions.delete(resourceId);
354
+ _this4.deleteFromCache(resourceId);
355
+ } else {
356
+ _this4.subscriptions.set(resourceId, resourceSubscriptions);
357
+ }
358
+ }
359
+ };
360
+ }
361
+
119
362
  /**
120
363
  * Get the URL for a sync block.
121
- * @param localId - The local ID of the sync block to get the URL for
364
+ * @param resourceId - The resource ID of the sync block
122
365
  * @returns
123
366
  */
124
367
  }, {
125
368
  key: "getSyncBlockURL",
126
- value: function getSyncBlockURL(localId) {
127
- var syncBlock = this.syncBlockCache.get(localId);
128
- return syncBlock === null || syncBlock === void 0 ? void 0 : syncBlock.sourceURL;
369
+ value: function getSyncBlockURL(resourceId) {
370
+ var _syncBlock$data;
371
+ var syncBlock = this.getFromCache(resourceId);
372
+ if (!syncBlock) {
373
+ return undefined;
374
+ }
375
+ return (_syncBlock$data = syncBlock.data) === null || _syncBlock$data === void 0 ? void 0 : _syncBlock$data.sourceURL;
376
+ }
377
+ }, {
378
+ key: "destroy",
379
+ value: function destroy() {
380
+ this.syncBlockCache.clear();
381
+ this.subscriptions.clear();
382
+ this.syncBlockURLRequests.clear();
383
+ this.editorView = undefined;
384
+ this.isInitialized = false;
129
385
  }
130
386
  }]);
131
387
  }();
@@ -143,12 +143,12 @@ var SourceSyncBlockStoreManager = exports.SourceSyncBlockStoreManager = /*#__PUR
143
143
  value: function () {
144
144
  var _deleteSyncBlocksWithConfirmation = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(tr, syncBlockIds) {
145
145
  var _this2 = this;
146
- var confirmed, _this$editorView;
146
+ var confirmed, _this$editorView, results;
147
147
  return _regenerator.default.wrap(function _callee2$(_context2) {
148
148
  while (1) switch (_context2.prev = _context2.next) {
149
149
  case 0:
150
150
  if (!this.confirmationCallback) {
151
- _context2.next = 7;
151
+ _context2.next = 19;
152
152
  break;
153
153
  }
154
154
  this.confirmationTransaction = tr;
@@ -156,20 +156,44 @@ var SourceSyncBlockStoreManager = exports.SourceSyncBlockStoreManager = /*#__PUR
156
156
  return this.confirmationCallback();
157
157
  case 4:
158
158
  confirmed = _context2.sent;
159
- if (confirmed) {
160
- (_this$editorView = this.editorView) === null || _this$editorView === void 0 || _this$editorView.dispatch(this.confirmationTransaction.setMeta('isConfirmedSyncBlockDeletion', true));
161
- // Need to update the BE on deletion
162
- syncBlockIds.forEach(function (_ref) {
163
- var localId = _ref.localId;
164
- return _this2.syncBlockCache.delete(localId);
165
- });
159
+ if (!confirmed) {
160
+ _context2.next = 18;
161
+ break;
162
+ }
163
+ (_this$editorView = this.editorView) === null || _this$editorView === void 0 || _this$editorView.dispatch(this.confirmationTransaction.setMeta('isConfirmedSyncBlockDeletion', true));
164
+ _context2.prev = 7;
165
+ if (this.dataProvider) {
166
+ _context2.next = 10;
167
+ break;
166
168
  }
169
+ throw new Error('Data provider not set');
170
+ case 10:
171
+ _context2.next = 12;
172
+ return this.dataProvider.deleteNodesData(syncBlockIds.map(function (attrs) {
173
+ return attrs.resourceId;
174
+ }));
175
+ case 12:
176
+ results = _context2.sent;
177
+ results.forEach(function (result) {
178
+ if (result.success) {
179
+ // Only delete when it's deleted successfully in backend
180
+ _this2.syncBlockCache.delete(result.resourceId);
181
+ } else {
182
+ // TODO: EDITOR-1921 - add error analytics with result.error
183
+ }
184
+ });
185
+ _context2.next = 18;
186
+ break;
187
+ case 16:
188
+ _context2.prev = 16;
189
+ _context2.t0 = _context2["catch"](7);
190
+ case 18:
167
191
  this.confirmationTransaction = undefined;
168
- case 7:
192
+ case 19:
169
193
  case "end":
170
194
  return _context2.stop();
171
195
  }
172
- }, _callee2, this);
196
+ }, _callee2, this, [[7, 16]]);
173
197
  }));
174
198
  function deleteSyncBlocksWithConfirmation(_x, _x2) {
175
199
  return _deleteSyncBlocksWithConfirmation.apply(this, arguments);
@@ -20,6 +20,12 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
20
20
  this.referenceSyncBlockStoreManager = new _referenceSyncBlockStoreManager.ReferenceSyncBlockStoreManager(dataProvider);
21
21
  this.sourceSyncBlockStoreManager = new _sourceSyncBlockStoreManager.SourceSyncBlockStoreManager(dataProvider);
22
22
  }
23
+
24
+ /**
25
+ * Fetch sync block data for a given sync block node.
26
+ * @param syncBlockNode - The sync block node to fetch data for
27
+ * @returns The fetched sync block data result
28
+ */
23
29
  return (0, _createClass2.default)(SyncBlockStoreManager, [{
24
30
  key: "fetchSyncBlockData",
25
31
  value: function fetchSyncBlockData(syncBlockNode) {
@@ -60,20 +66,22 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
60
66
 
61
67
  /**
62
68
  * Get the URL for a sync block.
63
- * @param localId - The local ID of the sync block to get the URL for
69
+ * @param resourceId - The resource ID of the sync block to get the URL for
64
70
  * @returns
65
71
  */
66
72
  }, {
67
73
  key: "getSyncBlockURL",
68
- value: function getSyncBlockURL(localId) {
74
+ value: function getSyncBlockURL(resourceId) {
69
75
  // only applicable to reference sync block, for now (will be refactored further)
70
- return this.referenceSyncBlockStoreManager.getSyncBlockURL(localId);
76
+ return this.referenceSyncBlockStoreManager.getSyncBlockURL(resourceId);
71
77
  }
72
78
  }, {
73
79
  key: "setEditorView",
74
80
  value: function setEditorView(editorView) {
75
- // only applicable to source sync block, for now (will be refactored further)
76
81
  this.sourceSyncBlockStoreManager.setEditorView(editorView);
82
+ if (editorView) {
83
+ this.referenceSyncBlockStoreManager.init(editorView);
84
+ }
77
85
  }
78
86
  }, {
79
87
  key: "isSourceBlock",
@@ -98,6 +106,16 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
98
106
  // only applicable to source sync block, for now (will be refactored further)
99
107
  return this.sourceSyncBlockStoreManager.createSyncBlockNode();
100
108
  }
109
+ }, {
110
+ key: "subscribeToSyncBlockData",
111
+ value: function subscribeToSyncBlockData(node, callback) {
112
+ return this.referenceSyncBlockStoreManager.subscribe(node, callback);
113
+ }
114
+ }, {
115
+ key: "refreshSubscriptions",
116
+ value: function refreshSubscriptions() {
117
+ this.referenceSyncBlockStoreManager.refreshSubscriptions();
118
+ }
101
119
  }, {
102
120
  key: "deleteSyncBlocksWithConfirmation",
103
121
  value: function deleteSyncBlocksWithConfirmation(tr, syncBlockIds) {
@@ -110,5 +128,10 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
110
128
  // only applicable to source sync block, for now (will be refactored further)
111
129
  this.sourceSyncBlockStoreManager.rebaseTransaction(incomingTr, state);
112
130
  }
131
+ }, {
132
+ key: "destroy",
133
+ value: function destroy() {
134
+ this.referenceSyncBlockStoreManager.destroy();
135
+ }
113
136
  }]);
114
137
  }();
@@ -4,7 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.updateContentProperty = exports.getContentProperty = exports.createContentProperty = void 0;
7
+ exports.updateContentProperty = exports.getContentProperty = exports.deleteContentProperty = exports.createContentProperty = void 0;
8
8
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
@@ -23,6 +23,7 @@ var GRAPHQL_ENDPOINT = '/gateway/api/graphql';
23
23
  var GET_OPERATION_NAME = 'EDITOR_SYNCED_BLOCK_GET';
24
24
  var CREATE_OPERATION_NAME = 'EDITOR_SYNCED_BLOCK_CREATE';
25
25
  var UPDATE_OPERATION_NAME = 'EDITOR_SYNCED_BLOCK_UPDATE';
26
+ var DELETE_OPERATION_NAME = 'EDITOR_SYNCED_BLOCK_DELETE';
26
27
  /**
27
28
  * Query to get the page property by key
28
29
  * @param documentARI
@@ -74,6 +75,8 @@ var UPDATE_PAGE_QUERY = "mutation ".concat(UPDATE_OPERATION_NAME, " ($input: Con
74
75
  * @returns
75
76
  */
76
77
  var UPDATE_BLOG_QUERY = "mutation ".concat(UPDATE_OPERATION_NAME, " ($input: ConfluenceUpdateValueBlogPostPropertyInput!) {\n\tconfluence {\n\t\tupdateValueBlogPostProperty(input: $input) {\n\t\t\tblogPostProperty {\n\t\t\t\tkey,\n\t\t\t\tvalue\n\t\t\t}\n\t\t}\n\t}\n}");
78
+ var DELETE_PAGE_QUERY = "mutation ".concat(DELETE_OPERATION_NAME, " ($input: ConfluenceDeletePagePropertyInput!) {\n\t\t\t\t\t\tconfluence {\n\t\t\t\t\t\t\tdeletePageProperty(input: $input) {\n\t\t\t\t\t\t\t\tsuccess, errors {\n\t\t\t\t\t\t\t\t message\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}");
79
+ var DELETE_BLOG_QUERY = "mutation ".concat(DELETE_OPERATION_NAME, " ($input: ConfluenceDeleteBlogPostPropertyInput!) {\n\t\t\t\t\t\tconfluence {\n\t\t\t\t\t\t\tdeleteBlogPostProperty(input: $input) {\n\t\t\t\t\t\t\t\tsuccess, errors {\n\t\t\t\t\t\t\t\t message\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}");
77
80
  var getContentProperty = exports.getContentProperty = /*#__PURE__*/function () {
78
81
  var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref) {
79
82
  var pageId, key, cloudId, pageType, documentARI, isBlog, bodyData, response;
@@ -231,4 +234,54 @@ var createContentProperty = exports.createContentProperty = /*#__PURE__*/functio
231
234
  return function createContentProperty(_x3) {
232
235
  return _ref6.apply(this, arguments);
233
236
  };
237
+ }();
238
+ var deleteContentProperty = exports.deleteContentProperty = /*#__PURE__*/function () {
239
+ var _ref8 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(_ref7) {
240
+ var pageId, cloudId, pageType, key, documentARI, isBlog, bodyData, response;
241
+ return _regenerator.default.wrap(function _callee4$(_context4) {
242
+ while (1) switch (_context4.prev = _context4.next) {
243
+ case 0:
244
+ pageId = _ref7.pageId, cloudId = _ref7.cloudId, pageType = _ref7.pageType, key = _ref7.key;
245
+ documentARI = (0, _ari.getConfluencePageAri)(pageId, cloudId, pageType);
246
+ isBlog = (0, _utils.isBlogPageType)(pageType);
247
+ bodyData = {
248
+ query: isBlog ? DELETE_BLOG_QUERY : DELETE_PAGE_QUERY,
249
+ operationName: DELETE_OPERATION_NAME,
250
+ variables: {
251
+ input: _objectSpread(_objectSpread({}, isBlog ? {
252
+ blogPostId: documentARI
253
+ } : {
254
+ pageId: documentARI
255
+ }), {}, {
256
+ key: key
257
+ })
258
+ }
259
+ };
260
+ _context4.next = 6;
261
+ return fetch(GRAPHQL_ENDPOINT, {
262
+ method: 'POST',
263
+ headers: _objectSpread(_objectSpread({}, COMMON_HEADERS), AGG_HEADERS),
264
+ body: JSON.stringify(bodyData)
265
+ });
266
+ case 6:
267
+ response = _context4.sent;
268
+ if (response.ok) {
269
+ _context4.next = 9;
270
+ break;
271
+ }
272
+ throw new Error("Failed to delete content property: ".concat(response.statusText));
273
+ case 9:
274
+ _context4.next = 11;
275
+ return response.json();
276
+ case 11:
277
+ return _context4.abrupt("return", _context4.sent);
278
+ case 12:
279
+ case "end":
280
+ return _context4.stop();
281
+ }
282
+ }, _callee4);
283
+ }));
284
+ return function deleteContentProperty(_x4) {
285
+ return _ref8.apply(this, arguments);
286
+ };
234
287
  }();