@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
@@ -0,0 +1,42 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { useCallback, useEffect, useState } from 'react';
3
+ import { SyncBlockError } from '../common/types';
4
+ export var useFetchSyncBlockData = function useFetchSyncBlockData(manager, syncBlockNode) {
5
+ var _useState = useState(null),
6
+ _useState2 = _slicedToArray(_useState, 2),
7
+ fetchSyncBlockDataResult = _useState2[0],
8
+ setFetchSyncBlockDataResult = _useState2[1];
9
+ var fetchSyncBlockNode = useCallback(function () {
10
+ manager.fetchSyncBlockData(syncBlockNode).then(function (data) {
11
+ if (data !== null && data !== void 0 && data.error) {
12
+ // if there is an error, we don't want to replace real existing data with the error data
13
+ setFetchSyncBlockDataResult(function (prev) {
14
+ if (!prev || prev.error) {
15
+ return data;
16
+ }
17
+ return prev;
18
+ });
19
+ } else {
20
+ setFetchSyncBlockDataResult(data !== null && data !== void 0 ? data : null);
21
+ }
22
+ }).catch(function () {
23
+ //TODO: EDITOR-1921 - add error analytics
24
+ setFetchSyncBlockDataResult(function (prev) {
25
+ if (!prev || prev.error) {
26
+ return {
27
+ error: SyncBlockError.Errored
28
+ };
29
+ }
30
+ return prev;
31
+ });
32
+ });
33
+ }, [manager, syncBlockNode]);
34
+ useEffect(function () {
35
+ fetchSyncBlockNode();
36
+ var interval = window.setInterval(fetchSyncBlockNode, 3000);
37
+ return function () {
38
+ window.clearInterval(interval);
39
+ };
40
+ }, [fetchSyncBlockNode]);
41
+ return fetchSyncBlockDataResult;
42
+ };
@@ -0,0 +1,7 @@
1
+ import { useEffect } from 'react';
2
+ export var useHandleContentChanges = function useHandleContentChanges(manager, syncBlockNode) {
3
+ useEffect(function () {
4
+ //TODO: EDITOR-1921 - add error analytics
5
+ manager.updateSyncBlockData(syncBlockNode);
6
+ }, [manager, syncBlockNode]);
7
+ };
package/dist/esm/index.js CHANGED
@@ -1,11 +1,13 @@
1
1
  /* eslint-disable @atlaskit/editor/no-re-export */
2
2
 
3
- export { SyncBlockProvider as SyncedBlockProvider, useMemoizedSyncedBlockProvider } from './common/syncBlockProvider';
4
- export { SyncBlockStoreManager, useFetchSyncBlockData, useHandleContentChanges } from './common/syncBlockStoreManager';
5
- export { SyncBlockStatus } from './common/types';
6
- export { inMemoryFetchProvider, inMemoryWriteProvider } from './providers/inMemory';
3
+ export { SyncBlockProvider as SyncedBlockProvider, useMemoizedSyncedBlockProvider } from './providers/syncBlockProvider';
4
+ export { SyncBlockStoreManager } from './store-manager/syncBlockStoreManager';
5
+ export { useFetchSyncBlockData } from './hooks/useFetchSyncBlockData';
6
+ export { useHandleContentChanges } from './hooks/useHandleContentChanges';
7
+ export { SyncBlockError } from './common/types';
8
+ export { inMemoryFetchProvider, inMemoryWriteProvider } from './providers/in-memory/inMemory';
7
9
  export { getDefaultSyncBlockSchema } from './common/schema';
8
- export { createContentAPIProvidersWithDefaultKey, useMemoizedContentAPIProviders } from './providers/confluenceContentAPI';
10
+ export { createContentAPIProvidersWithDefaultKey, useMemoizedContentAPIProviders } from './providers/confluence/confluenceContentAPI';
9
11
  export { getConfluencePageAri } from './utils/ari';
10
- export { convertSyncBlockPMNodeToSyncBlockNode as convertSyncBlockPMNodeToSyncBlockData } from './utils/utils';
12
+ export { convertSyncBlockPMNodeToSyncBlockData } from './utils/utils';
11
13
  export { rebaseTransaction } from './common/rebase-transaction';
@@ -5,10 +5,10 @@ import _createClass from "@babel/runtime/helpers/createClass";
5
5
  import _typeof from "@babel/runtime/helpers/typeof";
6
6
  import _regeneratorRuntime from "@babel/runtime/regenerator";
7
7
  import { useMemo } from 'react';
8
- import { SyncBlockStatus } from '../common/types';
9
- import { getLocalIdFromAri, getPageIdAndTypeFromAri } from '../utils/ari';
10
- import { getContentProperty, createContentProperty, updateContentProperty } from '../utils/contentProperty';
11
- import { isBlogPageType } from '../utils/utils';
8
+ import { SyncBlockError } from '../../common/types';
9
+ import { getLocalIdFromAri, getPageIdAndTypeFromAri } from '../../utils/ari';
10
+ import { getContentProperty, createContentProperty, updateContentProperty } from '../../utils/contentProperty';
11
+ import { isBlogPageType } from '../../utils/utils';
12
12
 
13
13
  /**
14
14
  * Configuration for Content API providers
@@ -32,16 +32,16 @@ var parseSyncedBlockContentPropertyValue = function parseSyncedBlockContentPrope
32
32
  throw new Error("Failed to parse synced block data: ".concat(error));
33
33
  }
34
34
  };
35
- var getResponseStatus = function getResponseStatus(contentProperty) {
35
+ var getResponseError = function getResponseError(contentProperty) {
36
36
  var _content$properties;
37
37
  var content = 'blogPost' in contentProperty.data.confluence ? contentProperty.data.confluence.blogPost : contentProperty.data.confluence.page;
38
38
  if (!content) {
39
- return SyncBlockStatus.Unauthorized;
39
+ return SyncBlockError.Forbidden;
40
40
  }
41
41
  if (!((_content$properties = content.properties) !== null && _content$properties !== void 0 && _content$properties[0])) {
42
- return SyncBlockStatus.NotFound;
42
+ return SyncBlockError.NotFound;
43
43
  }
44
- return SyncBlockStatus.Errored;
44
+ return SyncBlockError.Errored;
45
45
  };
46
46
 
47
47
  /**
@@ -56,7 +56,7 @@ var ConfluenceADFFetchProvider = /*#__PURE__*/function () {
56
56
  key: "fetchData",
57
57
  value: function () {
58
58
  var _fetchData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(resourceId) {
59
- var _getPageIdAndTypeFrom, pageId, pageType, localId, key, options, status, value, _contentProperty$data, contentProperty, _contentProperty$data2, _contentProperty, syncedBlockData;
59
+ var _getPageIdAndTypeFrom, pageId, pageType, localId, key, options, error, value, _contentProperty$data, contentProperty, _contentProperty$data2, _contentProperty, syncedBlockData;
60
60
  return _regeneratorRuntime.wrap(function _callee$(_context) {
61
61
  while (1) switch (_context.prev = _context.next) {
62
62
  case 0:
@@ -79,7 +79,7 @@ var ConfluenceADFFetchProvider = /*#__PURE__*/function () {
79
79
  case 8:
80
80
  contentProperty = _context.sent;
81
81
  value = (_contentProperty$data = contentProperty.data.confluence.blogPost) === null || _contentProperty$data === void 0 || (_contentProperty$data = _contentProperty$data.properties) === null || _contentProperty$data === void 0 || (_contentProperty$data = _contentProperty$data[0]) === null || _contentProperty$data === void 0 ? void 0 : _contentProperty$data.value;
82
- status = getResponseStatus(contentProperty);
82
+ error = getResponseError(contentProperty);
83
83
  _context.next = 18;
84
84
  break;
85
85
  case 13:
@@ -88,29 +88,32 @@ var ConfluenceADFFetchProvider = /*#__PURE__*/function () {
88
88
  case 15:
89
89
  _contentProperty = _context.sent;
90
90
  value = (_contentProperty$data2 = _contentProperty.data.confluence.page) === null || _contentProperty$data2 === void 0 || (_contentProperty$data2 = _contentProperty$data2.properties) === null || _contentProperty$data2 === void 0 || (_contentProperty$data2 = _contentProperty$data2[0]) === null || _contentProperty$data2 === void 0 ? void 0 : _contentProperty$data2.value;
91
- status = getResponseStatus(_contentProperty);
91
+ error = getResponseError(_contentProperty);
92
92
  case 18:
93
93
  if (value) {
94
94
  _context.next = 20;
95
95
  break;
96
96
  }
97
97
  return _context.abrupt("return", {
98
- status: status,
98
+ error: error,
99
99
  resourceId: resourceId
100
100
  });
101
101
  case 20:
102
102
  // Parse the synced block content from the property value
103
103
  syncedBlockData = parseSyncedBlockContentPropertyValue(value);
104
104
  return _context.abrupt("return", {
105
- content: syncedBlockData.content,
106
- resourceId: resourceId,
107
- blockInstanceId: localId
105
+ data: {
106
+ content: syncedBlockData.content,
107
+ resourceId: resourceId,
108
+ blockInstanceId: localId
109
+ },
110
+ resourceId: resourceId
108
111
  });
109
112
  case 24:
110
113
  _context.prev = 24;
111
114
  _context.t0 = _context["catch"](2);
112
115
  return _context.abrupt("return", {
113
- status: SyncBlockStatus.Errored,
116
+ error: SyncBlockError.Errored,
114
117
  resourceId: resourceId
115
118
  });
116
119
  case 27:
@@ -5,7 +5,10 @@ export var inMemoryFetchProvider = {
5
5
  if (!data) {
6
6
  throw new Error('Sync Block Provider (in-mem): Data not found');
7
7
  }
8
- return Promise.resolve(data);
8
+ return Promise.resolve({
9
+ data: data,
10
+ resourceId: resourceId
11
+ });
9
12
  }
10
13
  };
11
14
  export var inMemoryWriteProvider = {
@@ -9,8 +9,8 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
9
9
  function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
10
10
  function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
11
11
  import { useMemo } from 'react';
12
+ import { SyncBlockDataProvider } from '../providers/types';
12
13
  import { getLocalIdFromAri, getPageARIFromResourceId } from '../utils/ari';
13
- import { SyncBlockDataProvider } from './types';
14
14
  export var SyncBlockProvider = /*#__PURE__*/function (_SyncBlockDataProvide) {
15
15
  function SyncBlockProvider(fetchProvider, writeProvider, sourceId) {
16
16
  var _this;
@@ -0,0 +1,16 @@
1
+ import _createClass from "@babel/runtime/helpers/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
4
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
5
+ import _inherits from "@babel/runtime/helpers/inherits";
6
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
7
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
8
+ import { NodeDataProvider } from '@atlaskit/node-data-provider';
9
+ export var SyncBlockDataProvider = /*#__PURE__*/function (_NodeDataProvider) {
10
+ function SyncBlockDataProvider() {
11
+ _classCallCheck(this, SyncBlockDataProvider);
12
+ return _callSuper(this, SyncBlockDataProvider, arguments);
13
+ }
14
+ _inherits(SyncBlockDataProvider, _NodeDataProvider);
15
+ return _createClass(SyncBlockDataProvider);
16
+ }(NodeDataProvider);
@@ -0,0 +1,116 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
+ import _createClass from "@babel/runtime/helpers/createClass";
5
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
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
+ 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
+ var createSyncBlockNode = function createSyncBlockNode(localId, resourceId) {
9
+ return {
10
+ type: 'syncBlock',
11
+ attrs: {
12
+ localId: localId,
13
+ resourceId: resourceId
14
+ }
15
+ };
16
+ };
17
+ export var ReferenceSyncBlockStoreManager = /*#__PURE__*/function () {
18
+ function ReferenceSyncBlockStoreManager(dataProvider) {
19
+ _classCallCheck(this, ReferenceSyncBlockStoreManager);
20
+ this.dataProvider = dataProvider;
21
+ this.syncBlockCache = new Map();
22
+ this.syncBlockURLRequests = new Map();
23
+ }
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
+ return _createClass(ReferenceSyncBlockStoreManager, [{
32
+ 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) {
38
+ return;
39
+ }
40
+
41
+ // if the sync block is a reference block, we need to fetch the URL to the source
42
+ // 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;
49
+ }
50
+ }).finally(function () {
51
+ _this.syncBlockURLRequests.set(localId, false);
52
+ });
53
+ }
54
+ }
55
+ }, {
56
+ 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) {
62
+ case 0:
63
+ if (this.dataProvider) {
64
+ _context.next = 2;
65
+ break;
66
+ }
67
+ throw new Error('Data provider not set');
68
+ 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
+ });
76
+ }
77
+ _context.next = 7;
78
+ return this.dataProvider.fetchNodesData([syncNode]);
79
+ case 7:
80
+ data = _context.sent;
81
+ if (data) {
82
+ _context.next = 10;
83
+ break;
84
+ }
85
+ throw new Error('Failed to fetch sync block node data');
86
+ case 10:
87
+ fetchSyncBlockDataResult = data[0];
88
+ if (!fetchSyncBlockDataResult.error && fetchSyncBlockDataResult.data) {
89
+ // only adds it to the map if it did not error out
90
+ this.syncBlockCache.set(syncBlockNode.attrs.localId, _objectSpread(_objectSpread({}, existingSyncBlock), fetchSyncBlockDataResult.data));
91
+ }
92
+ return _context.abrupt("return", fetchSyncBlockDataResult);
93
+ case 13:
94
+ case "end":
95
+ return _context.stop();
96
+ }
97
+ }, _callee, this);
98
+ }));
99
+ function fetchSyncBlockData(_x) {
100
+ return _fetchSyncBlockData.apply(this, arguments);
101
+ }
102
+ return fetchSyncBlockData;
103
+ }()
104
+ /**
105
+ * Get the URL for a sync block.
106
+ * @param localId - The local ID of the sync block to get the URL for
107
+ * @returns
108
+ */
109
+ }, {
110
+ key: "getSyncBlockURL",
111
+ value: function getSyncBlockURL(localId) {
112
+ var syncBlock = this.syncBlockCache.get(localId);
113
+ return syncBlock === null || syncBlock === void 0 ? void 0 : syncBlock.sourceURL;
114
+ }
115
+ }]);
116
+ }();
@@ -0,0 +1,184 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
5
+ import uuid from 'uuid';
6
+ import { rebaseTransaction as _rebaseTransaction } from '../common/rebase-transaction';
7
+ import { resourceIdFromSourceAndLocalId } from '../utils/ari';
8
+ import { convertSyncBlockPMNodeToSyncBlockData } from '../utils/utils';
9
+ export var SourceSyncBlockStoreManager = /*#__PURE__*/function () {
10
+ function SourceSyncBlockStoreManager(dataProvider) {
11
+ _classCallCheck(this, SourceSyncBlockStoreManager);
12
+ this.dataProvider = dataProvider;
13
+ this.syncBlockCache = new Map();
14
+ }
15
+
16
+ /**
17
+ * Add/update a sync block node to/from the local cache
18
+ * @param syncBlockNode - The sync block node to update
19
+ */
20
+ return _createClass(SourceSyncBlockStoreManager, [{
21
+ key: "updateSyncBlockData",
22
+ value: function updateSyncBlockData(syncBlockNode) {
23
+ try {
24
+ var _syncBlockNode$attrs = syncBlockNode.attrs,
25
+ localId = _syncBlockNode$attrs.localId,
26
+ resourceId = _syncBlockNode$attrs.resourceId;
27
+ if (!localId || !resourceId) {
28
+ throw new Error('Local ID or resource ID is not set');
29
+ }
30
+ var syncBlockData = convertSyncBlockPMNodeToSyncBlockData(syncBlockNode);
31
+ this.syncBlockCache.set(localId, syncBlockData);
32
+ } catch (_unused) {
33
+ //TODO: EDITOR-1921 - add error analytics
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Save content of bodiedSyncBlock nodes in local cache to backend
39
+ *
40
+ * @returns true if saving all nodes successfully, false if fail to save some/all nodes
41
+ */
42
+ }, {
43
+ key: "flushBodiedSyncBlocks",
44
+ value: (function () {
45
+ var _flushBodiedSyncBlocks = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
46
+ var bodiedSyncBlockNodes, bodiedSyncBlockData, resourceIds;
47
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
48
+ while (1) switch (_context.prev = _context.next) {
49
+ case 0:
50
+ _context.prev = 0;
51
+ if (this.dataProvider) {
52
+ _context.next = 3;
53
+ break;
54
+ }
55
+ throw new Error('Data provider not set');
56
+ case 3:
57
+ bodiedSyncBlockNodes = [];
58
+ bodiedSyncBlockData = [];
59
+ Array.from(this.syncBlockCache.values()).forEach(function (syncBlockData) {
60
+ bodiedSyncBlockNodes.push({
61
+ type: 'bodiedSyncBlock',
62
+ attrs: {
63
+ localId: syncBlockData.blockInstanceId,
64
+ resourceId: syncBlockData.resourceId
65
+ }
66
+ });
67
+ bodiedSyncBlockData.push(syncBlockData);
68
+ });
69
+ if (!(bodiedSyncBlockNodes.length === 0)) {
70
+ _context.next = 8;
71
+ break;
72
+ }
73
+ return _context.abrupt("return", Promise.resolve(true));
74
+ case 8:
75
+ _context.next = 10;
76
+ return this.dataProvider.writeNodesData(bodiedSyncBlockNodes, bodiedSyncBlockData);
77
+ case 10:
78
+ resourceIds = _context.sent;
79
+ return _context.abrupt("return", resourceIds.every(function (resourceId) {
80
+ return resourceId !== undefined;
81
+ }));
82
+ case 14:
83
+ _context.prev = 14;
84
+ _context.t0 = _context["catch"](0);
85
+ return _context.abrupt("return", false);
86
+ case 17:
87
+ case "end":
88
+ return _context.stop();
89
+ }
90
+ }, _callee, this, [[0, 14]]);
91
+ }));
92
+ function flushBodiedSyncBlocks() {
93
+ return _flushBodiedSyncBlocks.apply(this, arguments);
94
+ }
95
+ return flushBodiedSyncBlocks;
96
+ }())
97
+ }, {
98
+ key: "setEditorView",
99
+ value: function setEditorView(editorView) {
100
+ this.editorView = editorView;
101
+ }
102
+ }, {
103
+ key: "registerConfirmationCallback",
104
+ value: function registerConfirmationCallback(callback) {
105
+ var _this = this;
106
+ this.confirmationCallback = callback;
107
+ return function () {
108
+ _this.confirmationCallback = undefined;
109
+ };
110
+ }
111
+ }, {
112
+ key: "requireConfirmationBeforeDelete",
113
+ value: function requireConfirmationBeforeDelete() {
114
+ return !!this.confirmationCallback;
115
+ }
116
+ }, {
117
+ key: "createSyncBlockNode",
118
+ value: function createSyncBlockNode() {
119
+ var _this$dataProvider;
120
+ var blockInstanceId = uuid();
121
+ var sourceId = (_this$dataProvider = this.dataProvider) === null || _this$dataProvider === void 0 ? void 0 : _this$dataProvider.getSourceId();
122
+ if (!sourceId) {
123
+ throw new Error('Provider of sync block plugin is not set');
124
+ }
125
+
126
+ // This should be generated by the data provider implementation as it differs between data providers
127
+ var resourceId = resourceIdFromSourceAndLocalId(sourceId, blockInstanceId);
128
+ var syncBlockNode = {
129
+ attrs: {
130
+ resourceId: resourceId,
131
+ localId: blockInstanceId
132
+ },
133
+ type: 'bodiedSyncBlock'
134
+ };
135
+ return syncBlockNode;
136
+ }
137
+ }, {
138
+ key: "deleteSyncBlocksWithConfirmation",
139
+ value: function () {
140
+ var _deleteSyncBlocksWithConfirmation = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(tr, syncBlockIds) {
141
+ var _this2 = this;
142
+ var confirmed, _this$editorView;
143
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
144
+ while (1) switch (_context2.prev = _context2.next) {
145
+ case 0:
146
+ if (!this.confirmationCallback) {
147
+ _context2.next = 7;
148
+ break;
149
+ }
150
+ this.confirmationTransaction = tr;
151
+ _context2.next = 4;
152
+ return this.confirmationCallback();
153
+ case 4:
154
+ confirmed = _context2.sent;
155
+ if (confirmed) {
156
+ (_this$editorView = this.editorView) === null || _this$editorView === void 0 || _this$editorView.dispatch(this.confirmationTransaction.setMeta('isConfirmedSyncBlockDeletion', true));
157
+ // Need to update the BE on deletion
158
+ syncBlockIds.forEach(function (_ref) {
159
+ var localId = _ref.localId;
160
+ return _this2.syncBlockCache.delete(localId);
161
+ });
162
+ }
163
+ this.confirmationTransaction = undefined;
164
+ case 7:
165
+ case "end":
166
+ return _context2.stop();
167
+ }
168
+ }, _callee2, this);
169
+ }));
170
+ function deleteSyncBlocksWithConfirmation(_x, _x2) {
171
+ return _deleteSyncBlocksWithConfirmation.apply(this, arguments);
172
+ }
173
+ return deleteSyncBlocksWithConfirmation;
174
+ }()
175
+ }, {
176
+ key: "rebaseTransaction",
177
+ value: function rebaseTransaction(incomingTr, state) {
178
+ if (!this.confirmationTransaction) {
179
+ return;
180
+ }
181
+ this.confirmationTransaction = _rebaseTransaction(this.confirmationTransaction, incomingTr, state);
182
+ }
183
+ }]);
184
+ }();
@@ -0,0 +1,105 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import { ReferenceSyncBlockStoreManager } from './referenceSyncBlockStoreManager';
4
+ import { SourceSyncBlockStoreManager } from './sourceSyncBlockStoreManager';
5
+
6
+ // A store manager responsible for the lifecycle and state management of sync blocks in an editor instance.
7
+ // Supports create, read, update, and delete operations for sync blocks.
8
+ // Designed to manage local in-memory state and synchronize with an external data provider.
9
+ // Handles caching, debouncing updates, and publish/subscribe for local changes.
10
+ // Ensures consistency between local and remote state, and can be used in both editor and renderer contexts.
11
+ export var SyncBlockStoreManager = /*#__PURE__*/function () {
12
+ function SyncBlockStoreManager(dataProvider) {
13
+ _classCallCheck(this, SyncBlockStoreManager);
14
+ this.referenceSyncBlockStoreManager = new ReferenceSyncBlockStoreManager(dataProvider);
15
+ this.sourceSyncBlockStoreManager = new SourceSyncBlockStoreManager(dataProvider);
16
+ }
17
+ return _createClass(SyncBlockStoreManager, [{
18
+ key: "fetchSyncBlockData",
19
+ value: function fetchSyncBlockData(syncBlockNode) {
20
+ if (this.isSourceBlock(syncBlockNode)) {
21
+ return Promise.reject(new Error('Invalid sync block node type provided for fetchSyncBlockData'));
22
+ }
23
+ return this.referenceSyncBlockStoreManager.fetchSyncBlockData(syncBlockNode);
24
+ }
25
+
26
+ /**
27
+ * Add/update a sync block node to/from the local cache
28
+ * @param syncBlockNode - The sync block node to update
29
+ */
30
+ }, {
31
+ key: "updateSyncBlockData",
32
+ value: function updateSyncBlockData(syncBlockNode) {
33
+ if (this.isSourceBlock(syncBlockNode)) {
34
+ this.sourceSyncBlockStoreManager.updateSyncBlockData(syncBlockNode);
35
+ } else {
36
+ throw new Error('Invalid sync block node type provided for updateSyncBlockData');
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Save content of bodiedSyncBlock nodes in local cache to backend
42
+ *
43
+ * @returns true if saving all nodes successfully, false if fail to save some/all nodes
44
+ */
45
+ }, {
46
+ key: "flushBodiedSyncBlocks",
47
+ value: function flushBodiedSyncBlocks() {
48
+ // only applicable to source sync block, for now (will be refactored further)
49
+ return this.sourceSyncBlockStoreManager.flushBodiedSyncBlocks();
50
+ }
51
+
52
+ /**
53
+ * Get the URL for a sync block.
54
+ * @param localId - The local ID of the sync block to get the URL for
55
+ * @returns
56
+ */
57
+ }, {
58
+ key: "getSyncBlockURL",
59
+ value: function getSyncBlockURL(localId) {
60
+ // only applicable to reference sync block, for now (will be refactored further)
61
+ return this.referenceSyncBlockStoreManager.getSyncBlockURL(localId);
62
+ }
63
+ }, {
64
+ key: "setEditorView",
65
+ value: function setEditorView(editorView) {
66
+ // only applicable to source sync block, for now (will be refactored further)
67
+ this.sourceSyncBlockStoreManager.setEditorView(editorView);
68
+ }
69
+ }, {
70
+ key: "isSourceBlock",
71
+ value: function isSourceBlock(node) {
72
+ return node.type.name === 'bodiedSyncBlock';
73
+ }
74
+ }, {
75
+ key: "registerConfirmationCallback",
76
+ value: function registerConfirmationCallback(callback) {
77
+ // only applicable to source sync block, for now (will be refactored further)
78
+ return this.sourceSyncBlockStoreManager.registerConfirmationCallback(callback);
79
+ }
80
+ }, {
81
+ key: "requireConfirmationBeforeDelete",
82
+ value: function requireConfirmationBeforeDelete() {
83
+ // only applicable to source sync block, for now (will be refactored further)
84
+ return this.sourceSyncBlockStoreManager.requireConfirmationBeforeDelete();
85
+ }
86
+ }, {
87
+ key: "createSyncBlockNode",
88
+ value: function createSyncBlockNode() {
89
+ // only applicable to source sync block, for now (will be refactored further)
90
+ return this.sourceSyncBlockStoreManager.createSyncBlockNode();
91
+ }
92
+ }, {
93
+ key: "deleteSyncBlocksWithConfirmation",
94
+ value: function deleteSyncBlocksWithConfirmation(tr, syncBlockIds) {
95
+ // only applicable to source sync block, for now (will be refactored further)
96
+ return this.sourceSyncBlockStoreManager.deleteSyncBlocksWithConfirmation(tr, syncBlockIds);
97
+ }
98
+ }, {
99
+ key: "rebaseTransaction",
100
+ value: function rebaseTransaction(incomingTr, state) {
101
+ // only applicable to source sync block, for now (will be refactored further)
102
+ this.sourceSyncBlockStoreManager.rebaseTransaction(incomingTr, state);
103
+ }
104
+ }]);
105
+ }();
@@ -1,19 +1,3 @@
1
- import { JSONTransformer } from '@atlaskit/editor-json-transformer';
2
- export var convertSyncBlockPMNodeToSyncBlockNode = function convertSyncBlockPMNodeToSyncBlockNode(node) {
3
- var includeContent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
4
- var transformer = new JSONTransformer();
5
- var toJSON = function toJSON(node) {
6
- return transformer.encodeNode(node);
7
- };
8
- return {
9
- type: node.type.name === 'bodiedSyncBlock' ? 'bodiedSyncBlock' : 'syncBlock',
10
- attrs: {
11
- localId: node.attrs.localId,
12
- resourceId: node.attrs.resourceId
13
- },
14
- content: includeContent ? node.content.content.map(toJSON) : undefined
15
- };
16
- };
17
1
  export var convertSyncBlockPMNodeToSyncBlockData = function convertSyncBlockPMNodeToSyncBlockData(node) {
18
2
  return {
19
3
  blockInstanceId: node.attrs.localId,
@@ -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<"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">;
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">;