@atlaskit/editor-synced-block-provider 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cjs/common/syncBlockProvider.js +24 -12
  3. package/dist/cjs/common/syncBlockStoreManager.js +8 -5
  4. package/dist/cjs/index.js +28 -2
  5. package/dist/cjs/providers/confluenceContentAPI.js +238 -0
  6. package/dist/cjs/utils/ari.js +42 -0
  7. package/dist/cjs/utils/contentProperty.js +192 -0
  8. package/dist/es2019/common/syncBlockProvider.js +25 -13
  9. package/dist/es2019/common/syncBlockStoreManager.js +8 -5
  10. package/dist/es2019/index.js +5 -3
  11. package/dist/es2019/providers/confluenceContentAPI.js +150 -0
  12. package/dist/es2019/utils/ari.js +32 -0
  13. package/dist/es2019/utils/contentProperty.js +160 -0
  14. package/dist/esm/common/syncBlockProvider.js +24 -12
  15. package/dist/esm/common/syncBlockStoreManager.js +8 -5
  16. package/dist/esm/index.js +5 -3
  17. package/dist/esm/providers/confluenceContentAPI.js +232 -0
  18. package/dist/esm/utils/ari.js +36 -0
  19. package/dist/esm/utils/contentProperty.js +185 -0
  20. package/dist/types/common/syncBlockProvider.d.ts +3 -1
  21. package/dist/types/common/syncBlockStoreManager.d.ts +2 -1
  22. package/dist/types/common/types.d.ts +1 -0
  23. package/dist/types/index.d.ts +4 -2
  24. package/dist/types/providers/confluenceContentAPI.d.ts +41 -0
  25. package/dist/types/utils/ari.d.ts +10 -0
  26. package/dist/types/utils/contentProperty.d.ts +61 -0
  27. package/dist/types-ts4.5/common/syncBlockProvider.d.ts +3 -1
  28. package/dist/types-ts4.5/common/syncBlockStoreManager.d.ts +2 -1
  29. package/dist/types-ts4.5/common/types.d.ts +1 -0
  30. package/dist/types-ts4.5/index.d.ts +4 -2
  31. package/dist/types-ts4.5/providers/confluenceContentAPI.d.ts +41 -0
  32. package/dist/types-ts4.5/utils/ari.d.ts +10 -0
  33. package/dist/types-ts4.5/utils/contentProperty.d.ts +61 -0
  34. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @atlaskit/editor-synced-block-provider
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`9e7a5a0bb1869`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/9e7a5a0bb1869) -
8
+ Add content api sync block provider
9
+
3
10
  ## 0.2.0
4
11
 
5
12
  ### Minor Changes
@@ -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.useFetchDocNode = exports.SyncBlockProvider = void 0;
7
+ exports.useMemoizedSyncedBlockProvider = exports.useFetchDocNode = exports.SyncBlockProvider = void 0;
8
8
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
9
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
10
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
@@ -55,6 +55,9 @@ var SyncBlockProvider = exports.SyncBlockProvider = /*#__PURE__*/function (_Sync
55
55
  });
56
56
  return Promise.all(resourceIds);
57
57
  });
58
+ (0, _defineProperty2.default)(_this, "getSourceId", function () {
59
+ return _this.sourceId;
60
+ });
58
61
  _this.fetchProvider = fetchProvider;
59
62
  _this.writeProvider = writeProvider;
60
63
  _this.sourceId = sourceId;
@@ -68,25 +71,34 @@ var useFetchDocNode = exports.useFetchDocNode = function useFetchDocNode(editorV
68
71
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
69
72
  docNode = _useState2[0],
70
73
  setDocNode = _useState2[1];
74
+ var fetchNode = function fetchNode(editorView, node, provider) {
75
+ var nodes = [(0, _utils.convertSyncBlockPMNodeToSyncBlockData)(node, false)];
76
+ provider === null || provider === void 0 || provider.fetchNodesData(nodes).then(function (data) {
77
+ var _data$;
78
+ if (data && (_data$ = data[0]) !== null && _data$ !== void 0 && _data$.content) {
79
+ var newNode = editorView.state.schema.nodeFromJSON(data[0].content);
80
+ setDocNode(_objectSpread(_objectSpread({}, newNode.toJSON()), {}, {
81
+ version: 1
82
+ }));
83
+ }
84
+ });
85
+ };
71
86
  (0, _react.useEffect)(function () {
72
87
  if (!provider) {
73
88
  return;
74
89
  }
90
+ fetchNode(editorView, node, provider);
75
91
  var interval = window.setInterval(function () {
76
- var nodes = [(0, _utils.convertSyncBlockPMNodeToSyncBlockData)(node, false)];
77
- provider === null || provider === void 0 || provider.fetchNodesData(nodes).then(function (data) {
78
- var _data$;
79
- if (data && (_data$ = data[0]) !== null && _data$ !== void 0 && _data$.content) {
80
- var newNode = editorView.state.schema.nodeFromJSON(data[0].content);
81
- setDocNode(_objectSpread(_objectSpread({}, newNode.toJSON()), {}, {
82
- version: 1
83
- }));
84
- }
85
- });
86
- }, 1000);
92
+ fetchNode(editorView, node, provider);
93
+ }, 3000);
87
94
  return function () {
88
95
  window.clearInterval(interval);
89
96
  };
90
97
  }, [editorView, node, provider]);
91
98
  return docNode;
99
+ };
100
+ var useMemoizedSyncedBlockProvider = exports.useMemoizedSyncedBlockProvider = function useMemoizedSyncedBlockProvider(fetchProvider, writeProvider, sourceId) {
101
+ return (0, _react.useMemo)(function () {
102
+ return new SyncBlockProvider(fetchProvider, writeProvider, sourceId);
103
+ }, [fetchProvider, writeProvider, sourceId]);
92
104
  };
@@ -18,9 +18,10 @@ var _uuid = _interopRequireDefault(require("uuid"));
18
18
  // Handles caching, debouncing updates, and publish/subscribe for local changes.
19
19
  // Ensures consistency between local and remote state, and can be used in both editor and renderer contexts.
20
20
  var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/function () {
21
- function SyncBlockStoreManager(_dataProvider) {
21
+ function SyncBlockStoreManager(dataProvider) {
22
22
  (0, _classCallCheck2.default)(this, SyncBlockStoreManager);
23
23
  this.syncBlocks = new Map();
24
+ this.dataProvider = dataProvider;
24
25
  }
25
26
  return (0, _createClass2.default)(SyncBlockStoreManager, [{
26
27
  key: "setEditorView",
@@ -30,14 +31,13 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
30
31
  }, {
31
32
  key: "isSourceBlock",
32
33
  value: function isSourceBlock(node) {
33
- var _this$syncBlocks$get;
34
34
  if (node.type.name !== 'syncBlock') {
35
35
  return false;
36
36
  }
37
37
  var _node$attrs = node.attrs,
38
38
  resourceId = _node$attrs.resourceId,
39
39
  localId = _node$attrs.localId;
40
- return this.syncBlocks.has(resourceId) && ((_this$syncBlocks$get = this.syncBlocks.get(resourceId)) === null || _this$syncBlocks$get === void 0 ? void 0 : _this$syncBlocks$get.sourceLocalId) === localId;
40
+ return resourceId.includes(localId);
41
41
  }
42
42
  }, {
43
43
  key: "registerConfirmationCallback",
@@ -56,15 +56,18 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
56
56
  }, {
57
57
  key: "createSyncBlockNode",
58
58
  value: function createSyncBlockNode() {
59
+ var _this$dataProvider;
59
60
  // TODO: EDITOR-1644 - properly implement creation of the synced block
60
61
  // below is a temporary implementation for the creation of the synced block
61
62
  // the resource id needs to have pageId and content property key in it
63
+ // Note: If the data provider is not set, the resource id will be the local id
62
64
 
63
- var blockInstanceId = (0, _uuid.default)();
64
65
  var localId = (0, _uuid.default)();
66
+ var sourceId = (_this$dataProvider = this.dataProvider) === null || _this$dataProvider === void 0 ? void 0 : _this$dataProvider.getSourceId();
67
+ var resourceId = sourceId ? "".concat(sourceId, "/").concat(localId) : localId;
65
68
  var syncBlockNode = {
66
69
  attrs: {
67
- resourceId: "ari:cloud:confluence:fake_cloud_id:page/fake_page_id/".concat(blockInstanceId),
70
+ resourceId: resourceId,
68
71
  localId: localId
69
72
  },
70
73
  type: 'syncBlock'
package/dist/cjs/index.js CHANGED
@@ -21,12 +21,24 @@ Object.defineProperty(exports, "convertSyncBlockPMNodeToSyncBlockData", {
21
21
  return _utils.convertSyncBlockPMNodeToSyncBlockData;
22
22
  }
23
23
  });
24
+ Object.defineProperty(exports, "createContentAPIProvidersWithDefaultKey", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _confluenceContentAPI.createContentAPIProvidersWithDefaultKey;
28
+ }
29
+ });
24
30
  Object.defineProperty(exports, "generateSyncBlockSourceUrl", {
25
31
  enumerable: true,
26
32
  get: function get() {
27
33
  return _utils.generateSyncBlockSourceUrl;
28
34
  }
29
35
  });
36
+ Object.defineProperty(exports, "getConfluencePageAri", {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _ari.getConfluencePageAri;
40
+ }
41
+ });
30
42
  Object.defineProperty(exports, "getDefaultSyncBlockSchema", {
31
43
  enumerable: true,
32
44
  get: function get() {
@@ -51,8 +63,22 @@ Object.defineProperty(exports, "useFetchDocNode", {
51
63
  return _syncBlockProvider.useFetchDocNode;
52
64
  }
53
65
  });
66
+ Object.defineProperty(exports, "useMemoizedContentAPIProviders", {
67
+ enumerable: true,
68
+ get: function get() {
69
+ return _confluenceContentAPI.useMemoizedContentAPIProviders;
70
+ }
71
+ });
72
+ Object.defineProperty(exports, "useMemoizedSyncedBlockProvider", {
73
+ enumerable: true,
74
+ get: function get() {
75
+ return _syncBlockProvider.useMemoizedSyncedBlockProvider;
76
+ }
77
+ });
54
78
  var _syncBlockProvider = require("./common/syncBlockProvider");
55
79
  var _syncBlockStoreManager = require("./common/syncBlockStoreManager");
56
80
  var _inMemory = require("./providers/inMemory");
57
- var _utils = require("./utils/utils");
58
- var _schema = require("./common/schema");
81
+ var _schema = require("./common/schema");
82
+ var _confluenceContentAPI = require("./providers/confluenceContentAPI");
83
+ var _ari = require("./utils/ari");
84
+ var _utils = require("./utils/utils");
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.useMemoizedContentAPIProviders = exports.createContentAPIProvidersWithDefaultKey = void 0;
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
12
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
+ var _react = require("react");
14
+ var _ari = require("../utils/ari");
15
+ var _contentProperty = require("../utils/contentProperty");
16
+ /**
17
+ * Configuration for Content API providers
18
+ */
19
+
20
+ var getContentPropertyKey = function getContentPropertyKey(contentPropertyKey, localId) {
21
+ return contentPropertyKey + '-' + localId;
22
+ };
23
+ var parseSyncedBlockContentPropertyValue = function parseSyncedBlockContentPropertyValue(value) {
24
+ try {
25
+ if (typeof value === 'string') {
26
+ return JSON.parse(value);
27
+ }
28
+ return value;
29
+ } catch (error) {
30
+ // eslint-disable-next-line no-console
31
+ console.error('Failed to parse synced block content:', error);
32
+ return {
33
+ content: undefined
34
+ };
35
+ }
36
+ };
37
+
38
+ /**
39
+ * ADFFetchProvider implementation that fetches synced block data from Confluence Content API
40
+ */
41
+ var ConfluenceADFFetchProvider = /*#__PURE__*/function () {
42
+ function ConfluenceADFFetchProvider(config) {
43
+ (0, _classCallCheck2.default)(this, ConfluenceADFFetchProvider);
44
+ this.config = config;
45
+ }
46
+ return (0, _createClass2.default)(ConfluenceADFFetchProvider, [{
47
+ key: "fetchData",
48
+ value: function () {
49
+ var _fetchData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(resourceId) {
50
+ var _contentProperty$data, pageId, localId, key, options, contentProperty, value, syncedBlockData;
51
+ return _regenerator.default.wrap(function _callee$(_context) {
52
+ while (1) switch (_context.prev = _context.next) {
53
+ case 0:
54
+ _context.prev = 0;
55
+ pageId = (0, _ari.getPageIdFromAri)(resourceId);
56
+ localId = (0, _ari.getLocalIdFromAri)(resourceId);
57
+ key = getContentPropertyKey(this.config.contentPropertyKey, localId);
58
+ options = {
59
+ pageId: pageId,
60
+ key: key,
61
+ cloudId: this.config.cloudId
62
+ };
63
+ _context.next = 7;
64
+ return (0, _contentProperty.getContentProperty)(options);
65
+ case 7:
66
+ contentProperty = _context.sent;
67
+ value = (_contentProperty$data = contentProperty.data.confluence.page.properties) === null || _contentProperty$data === void 0 || (_contentProperty$data = _contentProperty$data[0]) === null || _contentProperty$data === void 0 ? void 0 : _contentProperty$data.value;
68
+ if (value) {
69
+ _context.next = 11;
70
+ break;
71
+ }
72
+ throw new Error('Content property value does not exist');
73
+ case 11:
74
+ // Parse the synced block content from the property value
75
+ syncedBlockData = parseSyncedBlockContentPropertyValue(value);
76
+ return _context.abrupt("return", {
77
+ content: syncedBlockData.content
78
+ });
79
+ case 15:
80
+ _context.prev = 15;
81
+ _context.t0 = _context["catch"](0);
82
+ // eslint-disable-next-line no-console
83
+ console.error('Failed to fetch synced block data:', _context.t0);
84
+ return _context.abrupt("return", {
85
+ content: undefined
86
+ });
87
+ case 19:
88
+ case "end":
89
+ return _context.stop();
90
+ }
91
+ }, _callee, this, [[0, 15]]);
92
+ }));
93
+ function fetchData(_x) {
94
+ return _fetchData.apply(this, arguments);
95
+ }
96
+ return fetchData;
97
+ }()
98
+ }]);
99
+ }();
100
+ /**
101
+ * ADFWriteProvider implementation that writes synced block data to Confluence Content API
102
+ */
103
+ var ConfluenceADFWriteProvider = /*#__PURE__*/function () {
104
+ function ConfluenceADFWriteProvider(config) {
105
+ var _this = this;
106
+ (0, _classCallCheck2.default)(this, ConfluenceADFWriteProvider);
107
+ (0, _defineProperty2.default)(this, "createNewContentProperty", /*#__PURE__*/function () {
108
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(pageId, key, value) {
109
+ var _contentProperty$data2;
110
+ var contentProperty;
111
+ return _regenerator.default.wrap(function _callee2$(_context2) {
112
+ while (1) switch (_context2.prev = _context2.next) {
113
+ case 0:
114
+ _context2.next = 2;
115
+ return (0, _contentProperty.createContentProperty)({
116
+ pageId: pageId,
117
+ key: key,
118
+ value: value,
119
+ cloudId: _this.config.cloudId
120
+ });
121
+ case 2:
122
+ contentProperty = _context2.sent;
123
+ if (!(((_contentProperty$data2 = contentProperty.data.confluence.createPageProperty.pageProperty) === null || _contentProperty$data2 === void 0 ? void 0 : _contentProperty$data2.key) === key)) {
124
+ _context2.next = 7;
125
+ break;
126
+ }
127
+ return _context2.abrupt("return", key);
128
+ case 7:
129
+ throw new Error('Failed to create content property');
130
+ case 8:
131
+ case "end":
132
+ return _context2.stop();
133
+ }
134
+ }, _callee2);
135
+ }));
136
+ return function (_x2, _x3, _x4) {
137
+ return _ref.apply(this, arguments);
138
+ };
139
+ }());
140
+ this.config = config;
141
+ }
142
+ return (0, _createClass2.default)(ConfluenceADFWriteProvider, [{
143
+ key: "writeData",
144
+ value: function () {
145
+ var _writeData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(sourceId, localId, data, resourceId) {
146
+ var pageId, syncedBlockValue, _contentProperty$data3, _localId, key, contentProperty, _key;
147
+ return _regenerator.default.wrap(function _callee3$(_context3) {
148
+ while (1) switch (_context3.prev = _context3.next) {
149
+ case 0:
150
+ _context3.prev = 0;
151
+ pageId = (0, _ari.getPageIdFromAri)(sourceId);
152
+ syncedBlockValue = JSON.stringify({
153
+ content: data
154
+ });
155
+ if (!resourceId) {
156
+ _context3.next = 20;
157
+ break;
158
+ }
159
+ // Update existing content property
160
+ _localId = (0, _ari.getLocalIdFromAri)(resourceId);
161
+ key = getContentPropertyKey(this.config.contentPropertyKey, _localId);
162
+ _context3.next = 8;
163
+ return (0, _contentProperty.updateContentProperty)({
164
+ pageId: pageId,
165
+ key: key,
166
+ value: syncedBlockValue,
167
+ cloudId: this.config.cloudId
168
+ });
169
+ case 8:
170
+ contentProperty = _context3.sent;
171
+ if (!(((_contentProperty$data3 = contentProperty.data.confluence.updateValuePageProperty.pageProperty) === null || _contentProperty$data3 === void 0 ? void 0 : _contentProperty$data3.key) === key)) {
172
+ _context3.next = 13;
173
+ break;
174
+ }
175
+ return _context3.abrupt("return", key);
176
+ case 13:
177
+ if (!(contentProperty.data.confluence.updateValuePageProperty.pageProperty === null)) {
178
+ _context3.next = 17;
179
+ break;
180
+ }
181
+ return _context3.abrupt("return", this.createNewContentProperty(pageId, key, syncedBlockValue));
182
+ case 17:
183
+ throw new Error('Failed to update content property');
184
+ case 18:
185
+ _context3.next = 22;
186
+ break;
187
+ case 20:
188
+ // Create new content property
189
+ _key = getContentPropertyKey(this.config.contentPropertyKey, localId);
190
+ return _context3.abrupt("return", this.createNewContentProperty(pageId, _key, syncedBlockValue));
191
+ case 22:
192
+ _context3.next = 28;
193
+ break;
194
+ case 24:
195
+ _context3.prev = 24;
196
+ _context3.t0 = _context3["catch"](0);
197
+ // eslint-disable-next-line no-console
198
+ console.error('Failed to write synced block data:', _context3.t0);
199
+ return _context3.abrupt("return", Promise.reject(_context3.t0));
200
+ case 28:
201
+ case "end":
202
+ return _context3.stop();
203
+ }
204
+ }, _callee3, this, [[0, 24]]);
205
+ }));
206
+ function writeData(_x5, _x6, _x7, _x8) {
207
+ return _writeData.apply(this, arguments);
208
+ }
209
+ return writeData;
210
+ }()
211
+ }]);
212
+ }();
213
+ /**
214
+ * Factory function to create both providers with shared configuration
215
+ */
216
+ var createContentAPIProviders = function createContentAPIProviders(config) {
217
+ var fetchProvider = new ConfluenceADFFetchProvider(config);
218
+ var writeProvider = new ConfluenceADFWriteProvider(config);
219
+ return {
220
+ fetchProvider: fetchProvider,
221
+ writeProvider: writeProvider
222
+ };
223
+ };
224
+
225
+ /**
226
+ * Convenience function to create providers with default content property key
227
+ */
228
+ var createContentAPIProvidersWithDefaultKey = exports.createContentAPIProvidersWithDefaultKey = function createContentAPIProvidersWithDefaultKey(cloudId) {
229
+ return createContentAPIProviders({
230
+ cloudId: cloudId,
231
+ contentPropertyKey: 'editor-synced-block'
232
+ });
233
+ };
234
+ var useMemoizedContentAPIProviders = exports.useMemoizedContentAPIProviders = function useMemoizedContentAPIProviders(cloudId) {
235
+ return (0, _react.useMemo)(function () {
236
+ return createContentAPIProvidersWithDefaultKey(cloudId);
237
+ }, [cloudId]);
238
+ };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getPageIdFromAri = exports.getLocalIdFromAri = exports.getContentPropertyIdFromAri = exports.getContentPropertyAri = exports.getConfluencePageAri = void 0;
7
+ var getConfluencePageAri = exports.getConfluencePageAri = function getConfluencePageAri(pageId, cloudId) {
8
+ return "ari:cloud:confluence:".concat(cloudId, ":page/").concat(pageId);
9
+ };
10
+ var getPageIdFromAri = exports.getPageIdFromAri = function getPageIdFromAri(ari) {
11
+ // eslint-disable-next-line require-unicode-regexp
12
+ var match = ari.match(/ari:cloud:confluence:[^:]+:page\/(\d+)/);
13
+ if (match !== null && match !== void 0 && match[1]) {
14
+ return match[1];
15
+ }
16
+ throw new Error("Invalid page ARI: ".concat(ari));
17
+ };
18
+
19
+ /**
20
+ *
21
+ * @param ari ari:cloud:confluence:<cloudId>:page/<pageId>/<localId>
22
+ * @returns
23
+ */
24
+ var getLocalIdFromAri = exports.getLocalIdFromAri = function getLocalIdFromAri(ari) {
25
+ // eslint-disable-next-line require-unicode-regexp
26
+ var match = ari.match(/ari:cloud:confluence:[^:]+:page\/\d+\/([a-zA-Z0-9-]+)/);
27
+ if (match !== null && match !== void 0 && match[1]) {
28
+ return match[1];
29
+ }
30
+ throw new Error("Invalid page ARI: ".concat(ari));
31
+ };
32
+ var getContentPropertyAri = exports.getContentPropertyAri = function getContentPropertyAri(contentPropertyId, cloudId) {
33
+ return "ari:cloud:confluence:".concat(cloudId, ":content/").concat(contentPropertyId);
34
+ };
35
+ var getContentPropertyIdFromAri = exports.getContentPropertyIdFromAri = function getContentPropertyIdFromAri(ari) {
36
+ // eslint-disable-next-line require-unicode-regexp
37
+ var match = ari.match(/ari:cloud:confluence:[^:]+:content\/([^/]+)/);
38
+ if (match) {
39
+ return match[1];
40
+ }
41
+ throw new Error("Invalid content property ARI: ".concat(ari));
42
+ };
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.updateContentProperty = exports.getContentProperty = exports.createContentProperty = void 0;
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
+ var _ari = require("./ari");
12
+ 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; }
13
+ 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; }
14
+ var COMMON_HEADERS = {
15
+ 'Content-Type': 'application/json',
16
+ Accept: 'application/json'
17
+ };
18
+ var AGG_HEADERS = {
19
+ 'X-ExperimentalApi': 'confluence-agg-beta'
20
+ };
21
+ var GRAPHQL_ENDPOINT = '/gateway/api/graphql';
22
+ var GET_OPERATION_NAME = 'EDITOR_SYNCED_BLOCK_GET';
23
+ var CREATE_OPERATION_NAME = 'EDITOR_SYNCED_BLOCK_CREATE';
24
+ var UPDATE_OPERATION_NAME = 'EDITOR_SYNCED_BLOCK_UPDATE';
25
+ /**
26
+ * Query to get the page property by key
27
+ * @param documentARI
28
+ * @param key
29
+ * @returns
30
+ */
31
+ var GET_QUERY = "query ".concat(GET_OPERATION_NAME, " ($id: ID!, $keys: [String]!) {\n\t\t\t\t\tconfluence {\n\t\t\t\t\t\tpage (id: $id) {\n\t\t\t\t\t\t\tproperties(keys: $keys) {\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}");
32
+
33
+ /**
34
+ * Query to create a page property with key and value
35
+ * @param documentARI
36
+ * @param key
37
+ * @param value
38
+ * @returns
39
+ */
40
+ var CREATE_QUERY = "mutation ".concat(CREATE_OPERATION_NAME, " ($input: ConfluenceCreatePagePropertyInput!){\n\t\t\t\t\t\tconfluence {\n\t\t\t\t\t\t\tcreatePageProperty(input: $input) {\n\t\t\t\t\t\t\t\tpageProperty {\n\t\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\t\tvalue\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}");
41
+
42
+ /**
43
+ * Query to update a page property with key and value without bumping the version
44
+ * @param documentARI
45
+ * @param key
46
+ * @param value
47
+ * @returns
48
+ */
49
+ var UPDATE_QUERY = "mutation ".concat(UPDATE_OPERATION_NAME, " ($input: ConfluenceUpdateValuePagePropertyInput!) {\n\t\t\t\t\t\tconfluence {\n\t\t\t\t\t\t\tupdateValuePageProperty(input: $input) {\n\t\t\t\t\t\t\t\tpageProperty {\n\t\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\t\tvalue\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}");
50
+ var getContentProperty = exports.getContentProperty = /*#__PURE__*/function () {
51
+ var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref) {
52
+ var pageId, key, cloudId, documentARI, bodyData, response, contentProperty;
53
+ return _regenerator.default.wrap(function _callee$(_context) {
54
+ while (1) switch (_context.prev = _context.next) {
55
+ case 0:
56
+ pageId = _ref.pageId, key = _ref.key, cloudId = _ref.cloudId;
57
+ documentARI = (0, _ari.getConfluencePageAri)(pageId, cloudId);
58
+ bodyData = {
59
+ query: GET_QUERY,
60
+ operationName: GET_OPERATION_NAME,
61
+ variables: {
62
+ id: documentARI,
63
+ keys: [key]
64
+ }
65
+ };
66
+ _context.next = 5;
67
+ return fetch(GRAPHQL_ENDPOINT, {
68
+ method: 'POST',
69
+ headers: _objectSpread(_objectSpread({}, COMMON_HEADERS), AGG_HEADERS),
70
+ body: JSON.stringify(bodyData)
71
+ });
72
+ case 5:
73
+ response = _context.sent;
74
+ if (response.ok) {
75
+ _context.next = 8;
76
+ break;
77
+ }
78
+ throw new Error("Failed to get content property: ".concat(response.statusText));
79
+ case 8:
80
+ _context.next = 10;
81
+ return response.json();
82
+ case 10:
83
+ contentProperty = _context.sent;
84
+ return _context.abrupt("return", contentProperty);
85
+ case 12:
86
+ case "end":
87
+ return _context.stop();
88
+ }
89
+ }, _callee);
90
+ }));
91
+ return function getContentProperty(_x) {
92
+ return _ref2.apply(this, arguments);
93
+ };
94
+ }();
95
+ var updateContentProperty = exports.updateContentProperty = /*#__PURE__*/function () {
96
+ var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(_ref3) {
97
+ var pageId, key, value, cloudId, documentARI, bodyData, response, contentProperty;
98
+ return _regenerator.default.wrap(function _callee2$(_context2) {
99
+ while (1) switch (_context2.prev = _context2.next) {
100
+ case 0:
101
+ pageId = _ref3.pageId, key = _ref3.key, value = _ref3.value, cloudId = _ref3.cloudId;
102
+ documentARI = (0, _ari.getConfluencePageAri)(pageId, cloudId);
103
+ bodyData = {
104
+ query: UPDATE_QUERY,
105
+ operationName: UPDATE_OPERATION_NAME,
106
+ variables: {
107
+ input: {
108
+ pageId: documentARI,
109
+ key: key,
110
+ value: value,
111
+ useSameVersion: true
112
+ }
113
+ }
114
+ };
115
+ _context2.next = 5;
116
+ return fetch(GRAPHQL_ENDPOINT, {
117
+ method: 'POST',
118
+ headers: _objectSpread(_objectSpread({}, COMMON_HEADERS), AGG_HEADERS),
119
+ body: JSON.stringify(bodyData)
120
+ });
121
+ case 5:
122
+ response = _context2.sent;
123
+ if (response.ok) {
124
+ _context2.next = 8;
125
+ break;
126
+ }
127
+ throw new Error("Failed to update content property: ".concat(response.statusText));
128
+ case 8:
129
+ _context2.next = 10;
130
+ return response.json();
131
+ case 10:
132
+ contentProperty = _context2.sent;
133
+ return _context2.abrupt("return", contentProperty);
134
+ case 12:
135
+ case "end":
136
+ return _context2.stop();
137
+ }
138
+ }, _callee2);
139
+ }));
140
+ return function updateContentProperty(_x2) {
141
+ return _ref4.apply(this, arguments);
142
+ };
143
+ }();
144
+ var createContentProperty = exports.createContentProperty = /*#__PURE__*/function () {
145
+ var _ref6 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(_ref5) {
146
+ var pageId, key, value, cloudId, documentARI, escapedValue, bodyData, response, contentProperty;
147
+ return _regenerator.default.wrap(function _callee3$(_context3) {
148
+ while (1) switch (_context3.prev = _context3.next) {
149
+ case 0:
150
+ pageId = _ref5.pageId, key = _ref5.key, value = _ref5.value, cloudId = _ref5.cloudId;
151
+ documentARI = (0, _ari.getConfluencePageAri)(pageId, cloudId); // eslint-disable-next-line require-unicode-regexp
152
+ escapedValue = value.replace(/"/g, '\\"');
153
+ bodyData = {
154
+ query: CREATE_QUERY,
155
+ operationName: CREATE_OPERATION_NAME,
156
+ variables: {
157
+ input: {
158
+ pageId: documentARI,
159
+ key: key,
160
+ value: escapedValue
161
+ }
162
+ }
163
+ };
164
+ _context3.next = 6;
165
+ return fetch(GRAPHQL_ENDPOINT, {
166
+ method: 'POST',
167
+ headers: _objectSpread(_objectSpread({}, COMMON_HEADERS), AGG_HEADERS),
168
+ body: JSON.stringify(bodyData)
169
+ });
170
+ case 6:
171
+ response = _context3.sent;
172
+ if (response.ok) {
173
+ _context3.next = 9;
174
+ break;
175
+ }
176
+ throw new Error("Failed to create content property: ".concat(response.statusText));
177
+ case 9:
178
+ _context3.next = 11;
179
+ return response.json();
180
+ case 11:
181
+ contentProperty = _context3.sent;
182
+ return _context3.abrupt("return", contentProperty);
183
+ case 13:
184
+ case "end":
185
+ return _context3.stop();
186
+ }
187
+ }, _callee3);
188
+ }));
189
+ return function createContentProperty(_x3) {
190
+ return _ref6.apply(this, arguments);
191
+ };
192
+ }();