@atlaskit/editor-synced-block-provider 2.2.0 → 2.2.2

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 (67) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/cjs/common/types.js +7 -24
  3. package/dist/cjs/hooks/useFetchSyncBlockData.js +49 -0
  4. package/dist/cjs/hooks/useHandleContentChanges.js +13 -0
  5. package/dist/cjs/index.js +11 -9
  6. package/dist/cjs/providers/{confluenceContentAPI.js → confluence/confluenceContentAPI.js} +19 -16
  7. package/dist/cjs/providers/{inMemory.js → in-memory/inMemory.js} +4 -1
  8. package/dist/cjs/{common → providers}/syncBlockProvider.js +1 -1
  9. package/dist/cjs/providers/types.js +23 -0
  10. package/dist/cjs/store-manager/referenceSyncBlockStoreManager.js +123 -0
  11. package/dist/cjs/store-manager/sourceSyncBlockStoreManager.js +191 -0
  12. package/dist/cjs/store-manager/syncBlockStoreManager.js +111 -0
  13. package/dist/cjs/utils/utils.js +1 -17
  14. package/dist/es2019/common/types.js +6 -8
  15. package/dist/es2019/hooks/useFetchSyncBlockData.js +38 -0
  16. package/dist/es2019/hooks/useHandleContentChanges.js +7 -0
  17. package/dist/es2019/index.js +8 -6
  18. package/dist/es2019/providers/{confluenceContentAPI.js → confluence/confluenceContentAPI.js} +19 -16
  19. package/dist/es2019/providers/{inMemory.js → in-memory/inMemory.js} +4 -1
  20. package/dist/es2019/{common → providers}/syncBlockProvider.js +1 -1
  21. package/dist/es2019/providers/types.js +2 -0
  22. package/dist/es2019/store-manager/referenceSyncBlockStoreManager.js +83 -0
  23. package/dist/es2019/store-manager/sourceSyncBlockStoreManager.js +115 -0
  24. package/dist/es2019/store-manager/syncBlockStoreManager.js +79 -0
  25. package/dist/es2019/utils/utils.js +0 -13
  26. package/dist/esm/common/types.js +6 -22
  27. package/dist/esm/hooks/useFetchSyncBlockData.js +42 -0
  28. package/dist/esm/hooks/useHandleContentChanges.js +7 -0
  29. package/dist/esm/index.js +8 -6
  30. package/dist/esm/providers/{confluenceContentAPI.js → confluence/confluenceContentAPI.js} +19 -16
  31. package/dist/esm/providers/{inMemory.js → in-memory/inMemory.js} +4 -1
  32. package/dist/esm/{common → providers}/syncBlockProvider.js +1 -1
  33. package/dist/esm/providers/types.js +16 -0
  34. package/dist/esm/store-manager/referenceSyncBlockStoreManager.js +116 -0
  35. package/dist/esm/store-manager/sourceSyncBlockStoreManager.js +184 -0
  36. package/dist/esm/store-manager/syncBlockStoreManager.js +105 -0
  37. package/dist/esm/utils/utils.js +0 -16
  38. package/dist/types/common/schema.d.ts +1 -1
  39. package/dist/types/common/types.d.ts +13 -26
  40. package/dist/types/hooks/useFetchSyncBlockData.d.ts +4 -0
  41. package/dist/types/hooks/useHandleContentChanges.d.ts +3 -0
  42. package/dist/types/index.d.ts +10 -7
  43. package/dist/{types-ts4.5/providers → types/providers/confluence}/confluenceContentAPI.d.ts +2 -1
  44. package/dist/types/providers/{inMemory.d.ts → in-memory/inMemory.d.ts} +1 -1
  45. package/dist/types/{common → providers}/syncBlockProvider.d.ts +2 -1
  46. package/dist/types/providers/types.d.ts +18 -0
  47. package/dist/types/store-manager/referenceSyncBlockStoreManager.d.ts +23 -0
  48. package/dist/types/store-manager/sourceSyncBlockStoreManager.d.ts +31 -0
  49. package/dist/types/{common → store-manager}/syncBlockStoreManager.d.ts +7 -25
  50. package/dist/types/utils/utils.d.ts +1 -2
  51. package/dist/types-ts4.5/common/schema.d.ts +1 -1
  52. package/dist/types-ts4.5/common/types.d.ts +13 -26
  53. package/dist/types-ts4.5/hooks/useFetchSyncBlockData.d.ts +4 -0
  54. package/dist/types-ts4.5/hooks/useHandleContentChanges.d.ts +3 -0
  55. package/dist/types-ts4.5/index.d.ts +10 -7
  56. package/dist/{types/providers → types-ts4.5/providers/confluence}/confluenceContentAPI.d.ts +2 -1
  57. package/dist/types-ts4.5/providers/{inMemory.d.ts → in-memory/inMemory.d.ts} +1 -1
  58. package/dist/types-ts4.5/{common → providers}/syncBlockProvider.d.ts +2 -1
  59. package/dist/types-ts4.5/providers/types.d.ts +18 -0
  60. package/dist/types-ts4.5/store-manager/referenceSyncBlockStoreManager.d.ts +23 -0
  61. package/dist/types-ts4.5/store-manager/sourceSyncBlockStoreManager.d.ts +31 -0
  62. package/dist/types-ts4.5/{common → store-manager}/syncBlockStoreManager.d.ts +7 -25
  63. package/dist/types-ts4.5/utils/utils.d.ts +1 -2
  64. package/package.json +2 -2
  65. package/dist/cjs/common/syncBlockStoreManager.js +0 -377
  66. package/dist/es2019/common/syncBlockStoreManager.js +0 -265
  67. package/dist/esm/common/syncBlockStoreManager.js +0 -370
@@ -1,377 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.SyncBlockStoreManager = void 0;
8
- exports.useFetchSyncBlockData = useFetchSyncBlockData;
9
- exports.useHandleContentChanges = useHandleContentChanges;
10
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
12
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
16
- var _react = require("react");
17
- var _uuid = _interopRequireDefault(require("uuid"));
18
- var _ari = require("../utils/ari");
19
- var _utils = require("../utils/utils");
20
- var _rebaseTransaction2 = require("./rebase-transaction");
21
- var _types = require("./types");
22
- 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; }
23
- 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; }
24
- // Do this typedef to make it clear that
25
- // this is a local identifier for a resource for local use
26
- // A store manager responsible for the lifecycle and state management of sync blocks in an editor instance.
27
- // Supports create, read, update, and delete operations for sync blocks.
28
- // Designed to manage local in-memory state and synchronize with an external data provider.
29
- // Handles caching, debouncing updates, and publish/subscribe for local changes.
30
- // Ensures consistency between local and remote state, and can be used in both editor and renderer contexts.
31
- var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/function () {
32
- function SyncBlockStoreManager(dataProvider) {
33
- (0, _classCallCheck2.default)(this, SyncBlockStoreManager);
34
- this.syncBlocks = new Map();
35
- this.syncBlockURLRequests = new Map();
36
- this.dataProvider = dataProvider;
37
- }
38
-
39
- /**
40
- *
41
- * @param node - The sync block node to get the source URL for
42
- * @returns The source URL for the sync block node if it exists. Otherwise trigger fetch and return undefined, syncBlock will update with URL asynchronously.
43
- */
44
- return (0, _createClass2.default)(SyncBlockStoreManager, [{
45
- key: "getSyncBlockSourceURL",
46
- value: function getSyncBlockSourceURL(node) {
47
- var _this = this;
48
- var _node$attrs = node.attrs,
49
- localId = _node$attrs.localId,
50
- resourceId = _node$attrs.resourceId;
51
- if (!localId || !resourceId || !this.dataProvider) {
52
- return undefined;
53
- }
54
- var existingSyncBlock = this.syncBlocks.get(localId);
55
- if (!existingSyncBlock) {
56
- return undefined;
57
- }
58
- var sourceURL = existingSyncBlock.sourceURL;
59
- if (sourceURL) {
60
- return sourceURL;
61
- }
62
-
63
- // if the sync block is a reference block, we need to fetch the URL to the source
64
- // we could optimise this further by checking if the sync block is on the same page as the source
65
- if (!this.isSourceBlock(node) && !this.syncBlockURLRequests.get(localId)) {
66
- var syncBlockNode = (0, _utils.convertSyncBlockPMNodeToSyncBlockNode)(node, false);
67
- this.syncBlockURLRequests.set(localId, true);
68
- this.dataProvider.retrieveSyncBlockSourceUrl(syncBlockNode).then(function (sourceURL) {
69
- var existingSyncBlock = _this.syncBlocks.get(localId);
70
- if (existingSyncBlock) {
71
- var syncBlock = _objectSpread(_objectSpread({}, existingSyncBlock), {}, {
72
- sourceURL: sourceURL
73
- });
74
- _this.syncBlocks.set(localId, syncBlock);
75
- }
76
- }).finally(function () {
77
- _this.syncBlockURLRequests.set(localId, false);
78
- });
79
- }
80
- return undefined;
81
- }
82
- }, {
83
- key: "fetchSyncBlockData",
84
- value: function () {
85
- var _fetchSyncBlockData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(syncBlockNode) {
86
- var syncNode, data, sourceURL, fetchSyncBlockDataResult;
87
- return _regenerator.default.wrap(function _callee$(_context) {
88
- while (1) switch (_context.prev = _context.next) {
89
- case 0:
90
- if (['bodiedSyncBlock', 'syncBlock'].includes(syncBlockNode.type.name)) {
91
- _context.next = 2;
92
- break;
93
- }
94
- throw new Error('Node is not a sync block');
95
- case 2:
96
- syncNode = (0, _utils.convertSyncBlockPMNodeToSyncBlockNode)(syncBlockNode, false);
97
- if (this.dataProvider) {
98
- _context.next = 5;
99
- break;
100
- }
101
- throw new Error('Data provider not set');
102
- case 5:
103
- _context.next = 7;
104
- return this.dataProvider.fetchNodesData([syncNode]);
105
- case 7:
106
- data = _context.sent;
107
- if (data) {
108
- _context.next = 10;
109
- break;
110
- }
111
- throw new Error('Failed to fetch sync block node data');
112
- case 10:
113
- sourceURL = this.getSyncBlockSourceURL(syncBlockNode);
114
- fetchSyncBlockDataResult = data[0];
115
- if (!('status' in fetchSyncBlockDataResult)) {
116
- // only adds it to the map if it did not error out
117
- this.syncBlocks.set(syncBlockNode.attrs.localId, {
118
- syncNode: syncNode,
119
- sourceURL: sourceURL,
120
- syncBlockData: fetchSyncBlockDataResult
121
- });
122
- }
123
- return _context.abrupt("return", fetchSyncBlockDataResult);
124
- case 14:
125
- case "end":
126
- return _context.stop();
127
- }
128
- }, _callee, this);
129
- }));
130
- function fetchSyncBlockData(_x) {
131
- return _fetchSyncBlockData.apply(this, arguments);
132
- }
133
- return fetchSyncBlockData;
134
- }()
135
- /**
136
- * Add/update a sync block node to/from the local cache
137
- * @param syncBlockNode - The sync block node to update
138
- */
139
- }, {
140
- key: "updateSyncBlockData",
141
- value: function updateSyncBlockData(syncBlockNode) {
142
- try {
143
- if (!this.isSourceBlock(syncBlockNode)) {
144
- throw new Error('Node is not a source sync block');
145
- }
146
- var _syncBlockNode$attrs = syncBlockNode.attrs,
147
- localId = _syncBlockNode$attrs.localId,
148
- resourceId = _syncBlockNode$attrs.resourceId;
149
- if (!localId || !resourceId) {
150
- throw new Error('Local ID or resource ID is not set');
151
- }
152
- var existingSyncBlock = this.syncBlocks.get(localId);
153
- var sourceURL = existingSyncBlock === null || existingSyncBlock === void 0 ? void 0 : existingSyncBlock.sourceURL;
154
- var syncBlock = {
155
- syncNode: (0, _utils.convertSyncBlockPMNodeToSyncBlockNode)(syncBlockNode),
156
- sourceURL: sourceURL,
157
- syncBlockData: _objectSpread(_objectSpread({}, (0, _utils.convertSyncBlockPMNodeToSyncBlockData)(syncBlockNode)), {}, {
158
- sourceDocumentAri: resourceId // same as resourceId ARI when content property API
159
- })
160
- };
161
- this.syncBlocks.set(localId, syncBlock);
162
- } catch (_unused) {
163
- //TODO: EDITOR-1921 - add error analytics
164
- }
165
- }
166
-
167
- /**
168
- * Save content of bodiedSyncBlock nodes in local cache to backend
169
- *
170
- * @returns true if saving all nodes successfully, false if fail to save some/all nodes
171
- */
172
- }, {
173
- key: "flushBodiedSyncBlocks",
174
- value: (function () {
175
- var _flushBodiedSyncBlocks = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
176
- var bodiedSyncBlockNodes, bodiedSyncBlockData, resourceIds;
177
- return _regenerator.default.wrap(function _callee2$(_context2) {
178
- while (1) switch (_context2.prev = _context2.next) {
179
- case 0:
180
- _context2.prev = 0;
181
- if (this.dataProvider) {
182
- _context2.next = 3;
183
- break;
184
- }
185
- throw new Error('Data provider not set');
186
- case 3:
187
- bodiedSyncBlockNodes = [];
188
- bodiedSyncBlockData = [];
189
- Array.from(this.syncBlocks.values()).forEach(function (syncBlock) {
190
- if (syncBlock.syncNode.type === 'bodiedSyncBlock') {
191
- bodiedSyncBlockNodes.push(syncBlock.syncNode);
192
- bodiedSyncBlockData.push(syncBlock.syncBlockData);
193
- }
194
- });
195
- if (!(bodiedSyncBlockNodes.length === 0)) {
196
- _context2.next = 8;
197
- break;
198
- }
199
- return _context2.abrupt("return", Promise.resolve(true));
200
- case 8:
201
- _context2.next = 10;
202
- return this.dataProvider.writeNodesData(bodiedSyncBlockNodes, bodiedSyncBlockData);
203
- case 10:
204
- resourceIds = _context2.sent;
205
- return _context2.abrupt("return", resourceIds.every(function (resourceId) {
206
- return resourceId !== undefined;
207
- }));
208
- case 14:
209
- _context2.prev = 14;
210
- _context2.t0 = _context2["catch"](0);
211
- return _context2.abrupt("return", false);
212
- case 17:
213
- case "end":
214
- return _context2.stop();
215
- }
216
- }, _callee2, this, [[0, 14]]);
217
- }));
218
- function flushBodiedSyncBlocks() {
219
- return _flushBodiedSyncBlocks.apply(this, arguments);
220
- }
221
- return flushBodiedSyncBlocks;
222
- }()
223
- /**
224
- * Get the URL for a sync block.
225
- * @param localId - The local ID of the sync block to get the URL for
226
- * @returns
227
- */
228
- )
229
- }, {
230
- key: "getSyncBlockURL",
231
- value: function getSyncBlockURL(localId) {
232
- var syncBlock = this.syncBlocks.get(localId);
233
- return syncBlock === null || syncBlock === void 0 ? void 0 : syncBlock.sourceURL;
234
- }
235
- }, {
236
- key: "setEditorView",
237
- value: function setEditorView(editorView) {
238
- this.editorView = editorView;
239
- }
240
- }, {
241
- key: "isSourceBlock",
242
- value: function isSourceBlock(node) {
243
- var _this$dataProvider;
244
- if (node.type.name !== 'bodiedSyncBlock') {
245
- return false;
246
- }
247
- var _node$attrs2 = node.attrs,
248
- resourceId = _node$attrs2.resourceId,
249
- localId = _node$attrs2.localId;
250
- var sourceId = (_this$dataProvider = this.dataProvider) === null || _this$dataProvider === void 0 ? void 0 : _this$dataProvider.getSourceId();
251
- if (!sourceId) {
252
- return false;
253
- }
254
- return typeof resourceId === 'string' && typeof sourceId === 'string' && typeof localId === 'string' && resourceId === (0, _ari.resourceIdFromSourceAndLocalId)(sourceId, localId);
255
- }
256
- }, {
257
- key: "registerConfirmationCallback",
258
- value: function registerConfirmationCallback(callback) {
259
- var _this2 = this;
260
- this.confirmationCallback = callback;
261
- return function () {
262
- _this2.confirmationCallback = undefined;
263
- };
264
- }
265
- }, {
266
- key: "requireConfirmationBeforeDelete",
267
- value: function requireConfirmationBeforeDelete() {
268
- return !!this.confirmationCallback;
269
- }
270
- }, {
271
- key: "createSyncBlockNode",
272
- value: function createSyncBlockNode() {
273
- var _this$dataProvider2;
274
- var blockInstanceId = (0, _uuid.default)();
275
- var sourceId = (_this$dataProvider2 = this.dataProvider) === null || _this$dataProvider2 === void 0 ? void 0 : _this$dataProvider2.getSourceId();
276
- if (!sourceId) {
277
- throw new Error('Provider of sync block plugin is not set');
278
- }
279
-
280
- // This should be generated by the data provider implementation as it differs between data providers
281
- var resourceId = (0, _ari.resourceIdFromSourceAndLocalId)(sourceId, blockInstanceId);
282
- var syncBlockNode = {
283
- attrs: {
284
- resourceId: resourceId,
285
- localId: blockInstanceId
286
- },
287
- type: 'bodiedSyncBlock'
288
- };
289
- return syncBlockNode;
290
- }
291
- }, {
292
- key: "deleteSyncBlocksWithConfirmation",
293
- value: function () {
294
- var _deleteSyncBlocksWithConfirmation = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(tr, syncBlockIds) {
295
- var _this3 = this;
296
- var confirmed, _this$editorView;
297
- return _regenerator.default.wrap(function _callee3$(_context3) {
298
- while (1) switch (_context3.prev = _context3.next) {
299
- case 0:
300
- if (!this.confirmationCallback) {
301
- _context3.next = 7;
302
- break;
303
- }
304
- this.confirmationTransaction = tr;
305
- _context3.next = 4;
306
- return this.confirmationCallback();
307
- case 4:
308
- confirmed = _context3.sent;
309
- if (confirmed) {
310
- (_this$editorView = this.editorView) === null || _this$editorView === void 0 || _this$editorView.dispatch(this.confirmationTransaction.setMeta('isConfirmedSyncBlockDeletion', true));
311
- // Need to update the BE on deletion
312
- syncBlockIds.forEach(function (_ref) {
313
- var localId = _ref.localId;
314
- return _this3.syncBlocks.delete(localId);
315
- });
316
- }
317
- this.confirmationTransaction = undefined;
318
- case 7:
319
- case "end":
320
- return _context3.stop();
321
- }
322
- }, _callee3, this);
323
- }));
324
- function deleteSyncBlocksWithConfirmation(_x2, _x3) {
325
- return _deleteSyncBlocksWithConfirmation.apply(this, arguments);
326
- }
327
- return deleteSyncBlocksWithConfirmation;
328
- }()
329
- }, {
330
- key: "rebaseTransaction",
331
- value: function rebaseTransaction(incomingTr, state) {
332
- if (!this.confirmationTransaction) {
333
- return;
334
- }
335
- this.confirmationTransaction = (0, _rebaseTransaction2.rebaseTransaction)(this.confirmationTransaction, incomingTr, state);
336
- }
337
- }]);
338
- }();
339
- function useFetchSyncBlockData(manager, syncBlockNode) {
340
- var _useState = (0, _react.useState)(null),
341
- _useState2 = (0, _slicedToArray2.default)(_useState, 2),
342
- fetchSyncBlockDataResult = _useState2[0],
343
- setFetchSyncBlockDataResult = _useState2[1];
344
- var fetchSyncBlockNode = (0, _react.useCallback)(function () {
345
- manager.fetchSyncBlockData(syncBlockNode).then(function (data) {
346
- if ('status' in data) {
347
- // if there is an error, we don't want to replace real existing data with the error data
348
- if (!fetchSyncBlockDataResult || 'status' in fetchSyncBlockDataResult) {
349
- setFetchSyncBlockDataResult(data);
350
- }
351
- } else {
352
- setFetchSyncBlockDataResult(data);
353
- }
354
- }).catch(function () {
355
- //TODO: EDITOR-1921 - add error analytics
356
- if (!fetchSyncBlockDataResult || 'status' in fetchSyncBlockDataResult) {
357
- setFetchSyncBlockDataResult({
358
- status: _types.SyncBlockStatus.Errored
359
- });
360
- }
361
- });
362
- }, [manager, syncBlockNode, fetchSyncBlockDataResult]);
363
- (0, _react.useEffect)(function () {
364
- fetchSyncBlockNode();
365
- var interval = window.setInterval(fetchSyncBlockNode, 3000);
366
- return function () {
367
- window.clearInterval(interval);
368
- };
369
- }, [fetchSyncBlockNode]);
370
- return fetchSyncBlockDataResult;
371
- }
372
- function useHandleContentChanges(manager, syncBlockNode) {
373
- (0, _react.useEffect)(function () {
374
- //TODO: EDITOR-1921 - add error analytics
375
- manager.updateSyncBlockData(syncBlockNode);
376
- }, [manager, syncBlockNode]);
377
- }
@@ -1,265 +0,0 @@
1
- import { useCallback, useEffect, useState } from 'react';
2
- import uuid from 'uuid';
3
- import { resourceIdFromSourceAndLocalId } from '../utils/ari';
4
- import { convertSyncBlockPMNodeToSyncBlockData, convertSyncBlockPMNodeToSyncBlockNode } from '../utils/utils';
5
- import { rebaseTransaction } from './rebase-transaction';
6
- import { SyncBlockStatus } from './types';
7
-
8
- // Do this typedef to make it clear that
9
- // this is a local identifier for a resource for local use
10
-
11
- // A store manager responsible for the lifecycle and state management of sync blocks in an editor instance.
12
- // Supports create, read, update, and delete operations for sync blocks.
13
- // Designed to manage local in-memory state and synchronize with an external data provider.
14
- // Handles caching, debouncing updates, and publish/subscribe for local changes.
15
- // Ensures consistency between local and remote state, and can be used in both editor and renderer contexts.
16
- export class SyncBlockStoreManager {
17
- constructor(dataProvider) {
18
- this.syncBlocks = new Map();
19
- this.syncBlockURLRequests = new Map();
20
- this.dataProvider = dataProvider;
21
- }
22
-
23
- /**
24
- *
25
- * @param node - The sync block node to get the source URL for
26
- * @returns The source URL for the sync block node if it exists. Otherwise trigger fetch and return undefined, syncBlock will update with URL asynchronously.
27
- */
28
- getSyncBlockSourceURL(node) {
29
- const {
30
- localId,
31
- resourceId
32
- } = node.attrs;
33
- if (!localId || !resourceId || !this.dataProvider) {
34
- return undefined;
35
- }
36
- const existingSyncBlock = this.syncBlocks.get(localId);
37
- if (!existingSyncBlock) {
38
- return undefined;
39
- }
40
- const {
41
- sourceURL
42
- } = existingSyncBlock;
43
- if (sourceURL) {
44
- return sourceURL;
45
- }
46
-
47
- // if the sync block is a reference block, we need to fetch the URL to the source
48
- // we could optimise this further by checking if the sync block is on the same page as the source
49
- if (!this.isSourceBlock(node) && !this.syncBlockURLRequests.get(localId)) {
50
- const syncBlockNode = convertSyncBlockPMNodeToSyncBlockNode(node, false);
51
- this.syncBlockURLRequests.set(localId, true);
52
- this.dataProvider.retrieveSyncBlockSourceUrl(syncBlockNode).then(sourceURL => {
53
- const existingSyncBlock = this.syncBlocks.get(localId);
54
- if (existingSyncBlock) {
55
- const syncBlock = {
56
- ...existingSyncBlock,
57
- sourceURL
58
- };
59
- this.syncBlocks.set(localId, syncBlock);
60
- }
61
- }).finally(() => {
62
- this.syncBlockURLRequests.set(localId, false);
63
- });
64
- }
65
- return undefined;
66
- }
67
- async fetchSyncBlockData(syncBlockNode) {
68
- if (!['bodiedSyncBlock', 'syncBlock'].includes(syncBlockNode.type.name)) {
69
- throw new Error('Node is not a sync block');
70
- }
71
- const syncNode = convertSyncBlockPMNodeToSyncBlockNode(syncBlockNode, false);
72
- if (!this.dataProvider) {
73
- throw new Error('Data provider not set');
74
- }
75
- const data = await this.dataProvider.fetchNodesData([syncNode]);
76
- if (!data) {
77
- throw new Error('Failed to fetch sync block node data');
78
- }
79
- const sourceURL = this.getSyncBlockSourceURL(syncBlockNode);
80
- const fetchSyncBlockDataResult = data[0];
81
- if (!('status' in fetchSyncBlockDataResult)) {
82
- // only adds it to the map if it did not error out
83
- this.syncBlocks.set(syncBlockNode.attrs.localId, {
84
- syncNode,
85
- sourceURL,
86
- syncBlockData: fetchSyncBlockDataResult
87
- });
88
- }
89
- return fetchSyncBlockDataResult;
90
- }
91
-
92
- /**
93
- * Add/update a sync block node to/from the local cache
94
- * @param syncBlockNode - The sync block node to update
95
- */
96
- updateSyncBlockData(syncBlockNode) {
97
- try {
98
- if (!this.isSourceBlock(syncBlockNode)) {
99
- throw new Error('Node is not a source sync block');
100
- }
101
- const {
102
- localId,
103
- resourceId
104
- } = syncBlockNode.attrs;
105
- if (!localId || !resourceId) {
106
- throw new Error('Local ID or resource ID is not set');
107
- }
108
- const existingSyncBlock = this.syncBlocks.get(localId);
109
- const sourceURL = existingSyncBlock === null || existingSyncBlock === void 0 ? void 0 : existingSyncBlock.sourceURL;
110
- const syncBlock = {
111
- syncNode: convertSyncBlockPMNodeToSyncBlockNode(syncBlockNode),
112
- sourceURL,
113
- syncBlockData: {
114
- ...convertSyncBlockPMNodeToSyncBlockData(syncBlockNode),
115
- sourceDocumentAri: resourceId // same as resourceId ARI when content property API
116
- }
117
- };
118
- this.syncBlocks.set(localId, syncBlock);
119
- } catch {
120
- //TODO: EDITOR-1921 - add error analytics
121
- }
122
- }
123
-
124
- /**
125
- * Save content of bodiedSyncBlock nodes in local cache to backend
126
- *
127
- * @returns true if saving all nodes successfully, false if fail to save some/all nodes
128
- */
129
- async flushBodiedSyncBlocks() {
130
- try {
131
- if (!this.dataProvider) {
132
- throw new Error('Data provider not set');
133
- }
134
- const bodiedSyncBlockNodes = [];
135
- const bodiedSyncBlockData = [];
136
- Array.from(this.syncBlocks.values()).forEach(syncBlock => {
137
- if (syncBlock.syncNode.type === 'bodiedSyncBlock') {
138
- bodiedSyncBlockNodes.push(syncBlock.syncNode);
139
- bodiedSyncBlockData.push(syncBlock.syncBlockData);
140
- }
141
- });
142
- if (bodiedSyncBlockNodes.length === 0) {
143
- return Promise.resolve(true);
144
- }
145
- const resourceIds = await this.dataProvider.writeNodesData(bodiedSyncBlockNodes, bodiedSyncBlockData);
146
- return resourceIds.every(resourceId => resourceId !== undefined);
147
- } catch {
148
- //TODO: EDITOR-1921 - add error analytics
149
- return false;
150
- }
151
- }
152
-
153
- /**
154
- * Get the URL for a sync block.
155
- * @param localId - The local ID of the sync block to get the URL for
156
- * @returns
157
- */
158
- getSyncBlockURL(localId) {
159
- const syncBlock = this.syncBlocks.get(localId);
160
- return syncBlock === null || syncBlock === void 0 ? void 0 : syncBlock.sourceURL;
161
- }
162
- setEditorView(editorView) {
163
- this.editorView = editorView;
164
- }
165
- isSourceBlock(node) {
166
- var _this$dataProvider;
167
- if (node.type.name !== 'bodiedSyncBlock') {
168
- return false;
169
- }
170
- const {
171
- resourceId,
172
- localId
173
- } = node.attrs;
174
- const sourceId = (_this$dataProvider = this.dataProvider) === null || _this$dataProvider === void 0 ? void 0 : _this$dataProvider.getSourceId();
175
- if (!sourceId) {
176
- return false;
177
- }
178
- return typeof resourceId === 'string' && typeof sourceId === 'string' && typeof localId === 'string' && resourceId === resourceIdFromSourceAndLocalId(sourceId, localId);
179
- }
180
- registerConfirmationCallback(callback) {
181
- this.confirmationCallback = callback;
182
- return () => {
183
- this.confirmationCallback = undefined;
184
- };
185
- }
186
- requireConfirmationBeforeDelete() {
187
- return !!this.confirmationCallback;
188
- }
189
- createSyncBlockNode() {
190
- var _this$dataProvider2;
191
- const blockInstanceId = uuid();
192
- const sourceId = (_this$dataProvider2 = this.dataProvider) === null || _this$dataProvider2 === void 0 ? void 0 : _this$dataProvider2.getSourceId();
193
- if (!sourceId) {
194
- throw new Error('Provider of sync block plugin is not set');
195
- }
196
-
197
- // This should be generated by the data provider implementation as it differs between data providers
198
- const resourceId = resourceIdFromSourceAndLocalId(sourceId, blockInstanceId);
199
- const syncBlockNode = {
200
- attrs: {
201
- resourceId,
202
- localId: blockInstanceId
203
- },
204
- type: 'bodiedSyncBlock'
205
- };
206
- return syncBlockNode;
207
- }
208
- async deleteSyncBlocksWithConfirmation(tr, syncBlockIds) {
209
- if (this.confirmationCallback) {
210
- this.confirmationTransaction = tr;
211
- const confirmed = await this.confirmationCallback();
212
- if (confirmed) {
213
- var _this$editorView;
214
- (_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.dispatch(this.confirmationTransaction.setMeta('isConfirmedSyncBlockDeletion', true));
215
- // Need to update the BE on deletion
216
- syncBlockIds.forEach(({
217
- localId
218
- }) => this.syncBlocks.delete(localId));
219
- }
220
- this.confirmationTransaction = undefined;
221
- }
222
- }
223
- rebaseTransaction(incomingTr, state) {
224
- if (!this.confirmationTransaction) {
225
- return;
226
- }
227
- this.confirmationTransaction = rebaseTransaction(this.confirmationTransaction, incomingTr, state);
228
- }
229
- }
230
- export function useFetchSyncBlockData(manager, syncBlockNode) {
231
- const [fetchSyncBlockDataResult, setFetchSyncBlockDataResult] = useState(null);
232
- const fetchSyncBlockNode = useCallback(() => {
233
- manager.fetchSyncBlockData(syncBlockNode).then(data => {
234
- if ('status' in data) {
235
- // if there is an error, we don't want to replace real existing data with the error data
236
- if (!fetchSyncBlockDataResult || 'status' in fetchSyncBlockDataResult) {
237
- setFetchSyncBlockDataResult(data);
238
- }
239
- } else {
240
- setFetchSyncBlockDataResult(data);
241
- }
242
- }).catch(() => {
243
- //TODO: EDITOR-1921 - add error analytics
244
- if (!fetchSyncBlockDataResult || 'status' in fetchSyncBlockDataResult) {
245
- setFetchSyncBlockDataResult({
246
- status: SyncBlockStatus.Errored
247
- });
248
- }
249
- });
250
- }, [manager, syncBlockNode, fetchSyncBlockDataResult]);
251
- useEffect(() => {
252
- fetchSyncBlockNode();
253
- const interval = window.setInterval(fetchSyncBlockNode, 3000);
254
- return () => {
255
- window.clearInterval(interval);
256
- };
257
- }, [fetchSyncBlockNode]);
258
- return fetchSyncBlockDataResult;
259
- }
260
- export function useHandleContentChanges(manager, syncBlockNode) {
261
- useEffect(() => {
262
- //TODO: EDITOR-1921 - add error analytics
263
- manager.updateSyncBlockData(syncBlockNode);
264
- }, [manager, syncBlockNode]);
265
- }