@atlaskit/editor-json-transformer 9.0.1 → 9.1.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @atlaskit/editor-json-transformer
2
2
 
3
+ ## 9.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`05b85944df003`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/05b85944df003) -
8
+ Autofix: add explicit package exports (barrel removal)
9
+
10
+ ## 9.0.2
11
+
12
+ ### Patch Changes
13
+
14
+ - [`9bf0cd071b1fd`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/9bf0cd071b1fd) -
15
+ [EDITOR-5943] Legacised old vr test and duplicate test with platform_editor_diff_plugin_extended
16
+ true. Also extracted JSONtransformer out to its own file to debarrel the file.
17
+ - Updated dependencies
18
+
3
19
  ## 9.0.1
4
20
 
5
21
  ### Patch Changes
package/compass.yml ADDED
@@ -0,0 +1,41 @@
1
+ configVersion: 1
2
+ id: ari:cloud:compass:a436116f-02ce-4520-8fbb-7301462a1674:component/c5751cc6-3513-4070-9deb-af31e86aed34/29eed8fc-9eae-4456-a0f7-fd6dd012034a
3
+ name: '@atlaskit/editor-json-transformer'
4
+ ownerId: ari:cloud:identity::team/f3f77d87-a3bb-439d-a3a5-c7d481ec4e3b # Eng - Create - Native Content Types - Collab (Michael Kimpton)
5
+ labels:
6
+ - platform-code
7
+ - platform-afm
8
+ typeId: OTHER
9
+ fields:
10
+ tier: 4
11
+ lifecycle: Active
12
+ isMonorepoProject: true
13
+ links:
14
+ - name: Root Repository
15
+ type: REPOSITORY
16
+ url: https://bitbucket.org/atlassian/atlassian-frontend-monorepo/src/master
17
+ - name: Slack Channel
18
+ type: CHAT_CHANNEL
19
+ url: https://atlassian.enterprise.slack.com/archives/C02GEULKMLN # #team-cc-editor-services
20
+ - name: Editor Json Transformer
21
+ type: REPOSITORY
22
+ url: https://bitbucket.org/atlassian/atlassian-frontend-monorepo/src/master/platform/packages/editor/editor-json-transformer
23
+ customFields:
24
+ - name: Department
25
+ type: text
26
+ value: Eng - Editor Collaboration Platform
27
+ - name: Trusted Reviewer Teams
28
+ type: text
29
+ value: ari:cloud:identity::team/cf6a670b-5252-4c68-a769-9207de366beb
30
+ - name: Technical Owner
31
+ type: user
32
+ value: ari:cloud:identity::user/712020:d2fbbbd7-fd7c-4d3a-9f0a-7a97b6b26e40 # Michael Kimpton
33
+ - name: Required Reviewers Opt In
34
+ type: boolean
35
+ value: true
36
+ - name: Reviewer Selection Mechanism
37
+ type: text
38
+ value: random(2)
39
+ - name: Required Reviewer Approvals
40
+ type: number
41
+ value: 1
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "isJSONDocNode", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _main.isJSONDocNode;
10
+ }
11
+ });
12
+ var _main = require("../main");
package/dist/cjs/index.js CHANGED
@@ -1,296 +1,31 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- var _typeof3 = require("@babel/runtime/helpers/typeof");
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
- exports.SchemaStage = exports.JSONTransformer = void 0;
9
- exports.isJSONDocNode = isJSONDocNode;
10
- exports.nodeToJSON = void 0;
11
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
12
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
- var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
14
- var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
16
- var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
17
- var _adfSchema = require("@atlaskit/adf-schema");
18
- var _schemaDefault = require("@atlaskit/adf-schema/schema-default");
19
- var _markOverrideRules = require("./markOverrideRules");
20
- var _sanitizeNode = require("./sanitize/sanitize-node");
21
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof3(i) ? i : i + ""; }
22
- function _toPrimitive(t, r) { if ("object" != _typeof3(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof3(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
23
- 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; }
24
- 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; } // Disable no-re-export rule for entry point files
25
- /* eslint-disable @atlaskit/editor/no-re-export */
26
- var SchemaStage = exports.SchemaStage = /*#__PURE__*/function (SchemaStage) {
27
- SchemaStage["FINAL"] = "final";
28
- SchemaStage["STAGE_0"] = "stage0";
29
- return SchemaStage;
30
- }({});
31
- var isType = function isType(type) {
32
- return function (node) {
33
- return node.type.name === type;
34
- };
35
- };
36
-
37
- // Create a Set of node types that should have empty attrs removed
38
- var NODES_WITH_EMPTY_ATTRS_REMOVAL = new Set(['paragraph', 'layoutSection', 'blockquote', 'nestedExpand', 'bulletList', 'listItem', 'orderedList', 'rule', 'caption', 'tableRow', 'media', 'mediaSingle', 'mediaInline']);
39
- var isCodeBlock = isType('codeBlock');
40
- var isMediaNode = isType('media');
41
- var isMediaInline = isType('mediaInline');
42
- var isMediaSingleNode = isType('mediaSingle');
43
- var isMentionNode = isType('mention');
44
- var isParagraph = isType('paragraph');
45
- var isHeading = isType('heading');
46
- var isTable = isType('table');
47
- var isTableCell = isType('tableCell');
48
- var isTableHeader = isType('tableHeader');
49
- var isLinkMark = isType('link');
50
- var isUnsupportedMark = isType('unsupportedMark');
51
- var isUnsupportedNodeAttributeMark = isType('unsupportedNodeAttribute');
52
- var isExpand = isType('expand');
53
- var isNestedExpand = isType('nestedExpand');
54
- var isUnsupportedNode = function isUnsupportedNode(node) {
55
- return isType('unsupportedBlock')(node) || isType('unsupportedInline')(node);
56
- };
57
- var isDataConsumer = isType('dataConsumer');
58
- var isFragmentMark = isType('fragment');
59
- var isHardBreak = isType('hardBreak');
60
-
61
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
- var _filterNull = function filterNull(subject) {
63
- var output = _objectSpread({}, subject);
64
- // eslint-disable-next-line guard-for-in
65
- for (var key in output) {
66
- var current = output[key];
67
- if (current === null) {
68
- var _output = output,
69
- unusedKey = _output[key],
70
- filteredObj = (0, _objectWithoutProperties2.default)(_output, [key].map(_toPropertyKey));
71
- output = filteredObj;
72
- } else if ((0, _typeof2.default)(current) === 'object' && !Array.isArray(current)) {
73
- output[key] = _filterNull(current);
74
- }
75
- }
76
- return output;
77
- };
78
- var createDocFromContent = function createDocFromContent(content) {
79
- return {
80
- version: 1,
81
- type: 'doc',
82
- content: content || []
83
- };
84
- };
85
- var emptyDoc = createDocFromContent([{
86
- type: 'paragraph',
87
- content: []
88
- }]);
89
-
90
- /**
91
- * Transforms a Prosemirror node into a JSON representation. Compared with
92
- * native Prosemirror JSON, this representation ensures we have valid ADF attrs.
93
- *
94
- * @param node The Prosemirror node to transform.
95
- * @param mentionMap A map of mention IDs to their display names.
96
- * @returns The JSON representation of the node.
97
- */
98
- var _toJSON = exports.nodeToJSON = function toJSON(node, mentionMap) {
99
- var obj = {
100
- type: node.type.name
101
- };
102
- if (isUnsupportedNode(node)) {
103
- return node.attrs.originalValue;
104
- } else if (isMediaNode(node)) {
105
- obj.attrs = (0, _adfSchema.mediaToJSON)(node).attrs;
106
- } else if (isMediaSingleNode(node)) {
107
- obj.attrs = (0, _adfSchema.mediaSingleToJSON)(node).attrs;
108
- } else if (isMediaInline(node)) {
109
- obj.attrs = (0, _adfSchema.mediaToJSON)(node).attrs;
110
- } else if (isMentionNode(node)) {
111
- obj.attrs = (0, _adfSchema.mentionToJSON)(node).attrs;
112
- } else if (isCodeBlock(node)) {
113
- obj.attrs = (0, _adfSchema.codeBlockToJSON)(node).attrs;
114
- } else if (isTable(node)) {
115
- obj.attrs = (0, _adfSchema.tableToJSON)(node).attrs;
116
- } else if (isTableCell(node)) {
117
- obj.attrs = (0, _adfSchema.toJSONTableCell)(node).attrs;
118
- } else if (isTableHeader(node)) {
119
- obj.attrs = (0, _adfSchema.toJSONTableHeader)(node).attrs;
120
- } else if (isExpand(node) || isNestedExpand(node)) {
121
- obj.attrs = (0, _adfSchema.expandToJSON)(node).attrs;
122
- } else if (node.attrs && Object.keys(node.attrs).length) {
123
- obj.attrs = node.attrs;
124
- }
125
- if (obj.attrs) {
126
- obj.attrs = _filterNull(obj.attrs);
127
- }
128
- if (isMentionNode(node) && mentionMap) {
129
- var _text, _obj$attrs;
130
- // If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
131
- var name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? "@".concat(mentionMap[node.attrs.id]) : '';
132
- // Ignored via go/ees005
133
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
134
- var text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
135
- obj.attrs = _objectSpread(_objectSpread({}, obj.attrs), {}, {
136
- // If an explicit text value is set on the mention then that should take priority over the mentionMap value.
137
- text: !!text ? text : name
138
- });
139
- }
140
- if (NODES_WITH_EMPTY_ATTRS_REMOVAL.has(node.type.name) && obj.attrs && !Object.keys(obj.attrs).length || isHardBreak(node)) {
141
- delete obj.attrs;
142
- }
143
- if (node.isText) {
144
- obj.text = node.textContent;
145
- } else {
146
- node.content.forEach(function (child) {
147
- obj.content = obj.content || [];
148
- obj.content.push(_toJSON(child, mentionMap));
149
- });
150
- }
151
- if (isParagraph(node) || isHeading(node)) {
152
- obj.content = obj.content || [];
153
- }
154
- if (node.marks.length) {
155
- // Run any custom mark serialisers
156
- var parsedMarks = node.marks.map(function (mark) {
157
- if (isUnsupportedMark(mark)) {
158
- return canOverrideMark(mark, node.marks) ? null : mark.attrs.originalValue;
159
- } else if (isUnsupportedNodeAttributeMark(mark)) {
160
- return null;
161
- } else if (isLinkMark(mark)) {
162
- return (0, _adfSchema.linkToJSON)(mark);
163
- } else if (isDataConsumer(mark)) {
164
- var _serialised$attrs$sou;
165
- var serialised = (0, _adfSchema.dataConsumerToJSON)(mark);
166
- return !serialised.attrs.sources || ((_serialised$attrs$sou = serialised.attrs.sources) === null || _serialised$attrs$sou === void 0 ? void 0 : _serialised$attrs$sou.length) === 0 ? null : serialised;
167
- } else if (isFragmentMark(mark)) {
168
- var fragmentMark = (0, _adfSchema.fragmentToJSON)(mark);
169
- if (!fragmentMark.attrs.localId) {
170
- return null;
171
- }
172
- return fragmentMark;
173
- } else {
174
- return mark.toJSON();
175
- }
176
- }).filter(function (maybeMark) {
177
- return maybeMark !== null;
178
- });
179
-
180
- // Only set if we have a non-empty array, otherwise explicitly undefine it (as we only run this path if `node.marks.length`)
181
- obj.marks = (parsedMarks === null || parsedMarks === void 0 ? void 0 : parsedMarks.length) > 0 ? parsedMarks : undefined;
182
- var nodeAttributeMark = node.marks.find(isUnsupportedNodeAttributeMark);
183
- if (nodeAttributeMark && nodeAttributeMark.attrs.type.nodeType === obj.type) {
184
- obj.attrs = _objectSpread({}, getUnwrappedNodeAttributes(node, nodeAttributeMark, obj));
185
- }
6
+ Object.defineProperty(exports, "JSONTransformer", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _jsonTransformer.JSONTransformer;
186
10
  }
187
- return obj;
188
- };
189
- var canOverrideMark = function canOverrideMark(mark, existingMarks) {
190
- if (existingMarks.some(function (e) {
191
- return mark.attrs.originalValue.type === e.type.name;
192
- })) {
193
- return (0, _markOverrideRules.markOverrideRuleFor)(mark.attrs.originalValue.type).canOverrideUnsupportedMark();
194
- }
195
- return false;
196
- };
197
- var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark, obj) {
198
- var nodeAttributes = node.type.spec.attrs;
199
- var attributes = _objectSpread(_objectSpread({}, mark.attrs.unsupported), obj.attrs);
200
- // Ignored via go/ees005
201
- // eslint-disable-next-line no-var
202
- for (var key in obj.attrs) {
203
- if (obj.attrs.hasOwnProperty(key)) {
204
- var attribute = nodeAttributes ? nodeAttributes[key] : null;
205
- if (attribute) {
206
- if (attribute.default === node.attrs[key] && mark.attrs.unsupported[key]) {
207
- return _objectSpread(_objectSpread({}, attributes), {}, (0, _defineProperty2.default)({}, key, mark.attrs.unsupported[key]));
208
- }
209
- }
210
- }
211
- }
212
- return attributes;
213
- };
214
- var JSONTransformer = exports.JSONTransformer = /*#__PURE__*/function () {
215
- /**
216
- * @param schema The current editor schema
217
- * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
218
- * mention node.attr.text values with mapped names.
219
- */
220
- function JSONTransformer() {
221
- var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _schemaDefault.defaultSchema;
222
- var mentionMap = arguments.length > 1 ? arguments[1] : undefined;
223
- (0, _classCallCheck2.default)(this, JSONTransformer);
224
- this.schema = schema;
225
- this.mentionMap = mentionMap;
226
- }
227
- return (0, _createClass2.default)(JSONTransformer, [{
228
- key: "encode",
229
- value: function encode(node) {
230
- var _this = this;
231
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
232
- var content = [];
233
- node.content.forEach(function (child) {
234
- content.push((0, _sanitizeNode.sanitizeNode)(_toJSON(child, _this.mentionMap), options));
235
- });
236
- if (!content || (0, _isEqual.default)(content, emptyDoc.content)) {
237
- return createDocFromContent([]);
238
- }
239
- return createDocFromContent(content);
240
- }
241
- }, {
242
- key: "internalParse",
243
- value: function internalParse(content, schema) {
244
- var doc = schema.nodeFromJSON(content);
245
- doc.check();
246
- return doc;
247
- }
248
- }, {
249
- key: "parse",
250
- value: function parse(content, stage) {
251
- if (content.type !== 'doc') {
252
- throw new Error('Expected content format to be ADF');
253
- }
254
- var schema = !!stage ? (0, _schemaDefault.getSchemaBasedOnStage)(stage) : this.schema;
255
- if (!content.content || content.content.length === 0) {
256
- return this.internalParse(emptyDoc, schema);
257
- }
258
- return this.internalParse(content, schema);
259
- }
260
-
261
- /**
262
- * This method is used to encode a single node
263
- */
264
- }, {
265
- key: "encodeNode",
266
- value: function encodeNode(node) {
267
- return (0, _sanitizeNode.sanitizeNode)(_toJSON(node, this.mentionMap));
268
- }
269
- }]);
270
- }();
271
- /**
272
- * Quick type guard to check if a value is a valid JSONDocNode
273
- * This is only a basic structural check, does not validate the entire object nor the schema.
274
- * @param value - The value to check
275
- * @returns true if the value is a valid JSONDocNode structure
276
- */
277
- function isJSONDocNode(value) {
278
- if (!value || (0, _typeof2.default)(value) !== 'object') {
279
- return false;
280
- }
281
- var doc = value;
282
-
283
- // Check required properties
284
- if (doc.type !== 'doc') {
285
- return false;
11
+ });
12
+ Object.defineProperty(exports, "SchemaStage", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _jsonTransformer.SchemaStage;
286
16
  }
287
- if (!Array.isArray(doc.content)) {
288
- return false;
17
+ });
18
+ Object.defineProperty(exports, "isJSONDocNode", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _main.isJSONDocNode;
289
22
  }
290
-
291
- // Version is optional but if present should be a number
292
- if (doc.version !== undefined && typeof doc.version !== 'number') {
293
- return false;
23
+ });
24
+ Object.defineProperty(exports, "nodeToJSON", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _jsonTransformer.toJSON;
294
28
  }
295
- return true;
296
- }
29
+ });
30
+ var _jsonTransformer = require("./jsonTransformer");
31
+ var _main = require("./entry-points/main");
@@ -0,0 +1,268 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof3 = require("@babel/runtime/helpers/typeof");
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.toJSON = exports.SchemaStage = exports.JSONTransformer = void 0;
9
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
12
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
+ var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
15
+ var _adfSchema = require("@atlaskit/adf-schema");
16
+ var _schemaDefault = require("@atlaskit/adf-schema/schema-default");
17
+ var _markOverrideRules = require("./markOverrideRules");
18
+ var _sanitizeNode = require("./sanitize/sanitize-node");
19
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof3(i) ? i : i + ""; }
20
+ function _toPrimitive(t, r) { if ("object" != _typeof3(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof3(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
21
+ 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; }
22
+ 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; }
23
+ var SchemaStage = exports.SchemaStage = /*#__PURE__*/function (SchemaStage) {
24
+ SchemaStage["FINAL"] = "final";
25
+ SchemaStage["STAGE_0"] = "stage0";
26
+ return SchemaStage;
27
+ }({});
28
+ var isType = function isType(type) {
29
+ return function (node) {
30
+ return node.type.name === type;
31
+ };
32
+ };
33
+
34
+ // Create a Set of node types that should have empty attrs removed
35
+ var NODES_WITH_EMPTY_ATTRS_REMOVAL = new Set(['paragraph', 'layoutSection', 'blockquote', 'nestedExpand', 'bulletList', 'listItem', 'orderedList', 'rule', 'caption', 'tableRow', 'media', 'mediaSingle', 'mediaInline']);
36
+ var isCodeBlock = isType('codeBlock');
37
+ var isMediaNode = isType('media');
38
+ var isMediaInline = isType('mediaInline');
39
+ var isMediaSingleNode = isType('mediaSingle');
40
+ var isMentionNode = isType('mention');
41
+ var isParagraph = isType('paragraph');
42
+ var isHeading = isType('heading');
43
+ var isTable = isType('table');
44
+ var isTableCell = isType('tableCell');
45
+ var isTableHeader = isType('tableHeader');
46
+ var isLinkMark = isType('link');
47
+ var isUnsupportedMark = isType('unsupportedMark');
48
+ var isUnsupportedNodeAttributeMark = isType('unsupportedNodeAttribute');
49
+ var isExpand = isType('expand');
50
+ var isNestedExpand = isType('nestedExpand');
51
+ var isUnsupportedNode = function isUnsupportedNode(node) {
52
+ return isType('unsupportedBlock')(node) || isType('unsupportedInline')(node);
53
+ };
54
+ var isDataConsumer = isType('dataConsumer');
55
+ var isFragmentMark = isType('fragment');
56
+ var isHardBreak = isType('hardBreak');
57
+ var createDocFromContent = function createDocFromContent(content) {
58
+ return {
59
+ version: 1,
60
+ type: 'doc',
61
+ content: content || []
62
+ };
63
+ };
64
+ var emptyDoc = createDocFromContent([{
65
+ type: 'paragraph',
66
+ content: []
67
+ }]);
68
+
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ var _filterNull = function filterNull(subject) {
71
+ var output = _objectSpread({}, subject);
72
+ // eslint-disable-next-line guard-for-in
73
+ for (var key in output) {
74
+ var current = output[key];
75
+ if (current === null) {
76
+ var _output = output,
77
+ unusedKey = _output[key],
78
+ filteredObj = (0, _objectWithoutProperties2.default)(_output, [key].map(_toPropertyKey));
79
+ output = filteredObj;
80
+ } else if ((0, _typeof2.default)(current) === 'object' && !Array.isArray(current)) {
81
+ output[key] = _filterNull(current);
82
+ }
83
+ }
84
+ return output;
85
+ };
86
+ var canOverrideMark = function canOverrideMark(mark, existingMarks) {
87
+ if (existingMarks.some(function (e) {
88
+ return mark.attrs.originalValue.type === e.type.name;
89
+ })) {
90
+ return (0, _markOverrideRules.markOverrideRuleFor)(mark.attrs.originalValue.type).canOverrideUnsupportedMark();
91
+ }
92
+ return false;
93
+ };
94
+ var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark, obj) {
95
+ var nodeAttributes = node.type.spec.attrs;
96
+ var attributes = _objectSpread(_objectSpread({}, mark.attrs.unsupported), obj.attrs);
97
+ // Ignored via go/ees005
98
+ // eslint-disable-next-line no-var
99
+ for (var key in obj.attrs) {
100
+ if (obj.attrs.hasOwnProperty(key)) {
101
+ var attribute = nodeAttributes ? nodeAttributes[key] : null;
102
+ if (attribute) {
103
+ if (attribute.default === node.attrs[key] && mark.attrs.unsupported[key]) {
104
+ return _objectSpread(_objectSpread({}, attributes), {}, (0, _defineProperty2.default)({}, key, mark.attrs.unsupported[key]));
105
+ }
106
+ }
107
+ }
108
+ }
109
+ return attributes;
110
+ };
111
+
112
+ /**
113
+ * Transforms a Prosemirror node into a JSON representation. Compared with
114
+ * native Prosemirror JSON, this representation ensures we have valid ADF attrs.
115
+ *
116
+ * @param node The Prosemirror node to transform.
117
+ * @param mentionMap A map of mention IDs to their display names.
118
+ * @returns The JSON representation of the node.
119
+ */
120
+ var _toJSON = exports.toJSON = function toJSON(node, mentionMap) {
121
+ var obj = {
122
+ type: node.type.name
123
+ };
124
+ if (isUnsupportedNode(node)) {
125
+ return node.attrs.originalValue;
126
+ } else if (isMediaNode(node)) {
127
+ obj.attrs = (0, _adfSchema.mediaToJSON)(node).attrs;
128
+ } else if (isMediaSingleNode(node)) {
129
+ obj.attrs = (0, _adfSchema.mediaSingleToJSON)(node).attrs;
130
+ } else if (isMediaInline(node)) {
131
+ obj.attrs = (0, _adfSchema.mediaToJSON)(node).attrs;
132
+ } else if (isMentionNode(node)) {
133
+ obj.attrs = (0, _adfSchema.mentionToJSON)(node).attrs;
134
+ } else if (isCodeBlock(node)) {
135
+ obj.attrs = (0, _adfSchema.codeBlockToJSON)(node).attrs;
136
+ } else if (isTable(node)) {
137
+ obj.attrs = (0, _adfSchema.tableToJSON)(node).attrs;
138
+ } else if (isTableCell(node)) {
139
+ obj.attrs = (0, _adfSchema.toJSONTableCell)(node).attrs;
140
+ } else if (isTableHeader(node)) {
141
+ obj.attrs = (0, _adfSchema.toJSONTableHeader)(node).attrs;
142
+ } else if (isExpand(node) || isNestedExpand(node)) {
143
+ obj.attrs = (0, _adfSchema.expandToJSON)(node).attrs;
144
+ } else if (node.attrs && Object.keys(node.attrs).length) {
145
+ obj.attrs = node.attrs;
146
+ }
147
+ if (obj.attrs) {
148
+ obj.attrs = _filterNull(obj.attrs);
149
+ }
150
+ if (isMentionNode(node) && mentionMap) {
151
+ var _text, _obj$attrs;
152
+ // If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
153
+ var name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? "@".concat(mentionMap[node.attrs.id]) : '';
154
+ // Ignored via go/ees005
155
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
156
+ var text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
157
+ obj.attrs = _objectSpread(_objectSpread({}, obj.attrs), {}, {
158
+ // If an explicit text value is set on the mention then that should take priority over the mentionMap value.
159
+ text: !!text ? text : name
160
+ });
161
+ }
162
+ if (NODES_WITH_EMPTY_ATTRS_REMOVAL.has(node.type.name) && obj.attrs && !Object.keys(obj.attrs).length || isHardBreak(node)) {
163
+ delete obj.attrs;
164
+ }
165
+ if (node.isText) {
166
+ obj.text = node.textContent;
167
+ } else {
168
+ node.content.forEach(function (child) {
169
+ obj.content = obj.content || [];
170
+ obj.content.push(_toJSON(child, mentionMap));
171
+ });
172
+ }
173
+ if (isParagraph(node) || isHeading(node)) {
174
+ obj.content = obj.content || [];
175
+ }
176
+ if (node.marks.length) {
177
+ // Run any custom mark serialisers
178
+ var parsedMarks = node.marks.map(function (mark) {
179
+ if (isUnsupportedMark(mark)) {
180
+ return canOverrideMark(mark, node.marks) ? null : mark.attrs.originalValue;
181
+ } else if (isUnsupportedNodeAttributeMark(mark)) {
182
+ return null;
183
+ } else if (isLinkMark(mark)) {
184
+ return (0, _adfSchema.linkToJSON)(mark);
185
+ } else if (isDataConsumer(mark)) {
186
+ var _serialised$attrs$sou;
187
+ var serialised = (0, _adfSchema.dataConsumerToJSON)(mark);
188
+ return !serialised.attrs.sources || ((_serialised$attrs$sou = serialised.attrs.sources) === null || _serialised$attrs$sou === void 0 ? void 0 : _serialised$attrs$sou.length) === 0 ? null : serialised;
189
+ } else if (isFragmentMark(mark)) {
190
+ var fragmentMark = (0, _adfSchema.fragmentToJSON)(mark);
191
+ if (!fragmentMark.attrs.localId) {
192
+ return null;
193
+ }
194
+ return fragmentMark;
195
+ } else {
196
+ return mark.toJSON();
197
+ }
198
+ }).filter(function (maybeMark) {
199
+ return maybeMark !== null;
200
+ });
201
+
202
+ // Only set if we have a non-empty array, otherwise explicitly undefine it (as we only run this path if `node.marks.length`)
203
+ obj.marks = (parsedMarks === null || parsedMarks === void 0 ? void 0 : parsedMarks.length) > 0 ? parsedMarks : undefined;
204
+ var nodeAttributeMark = node.marks.find(isUnsupportedNodeAttributeMark);
205
+ if (nodeAttributeMark && nodeAttributeMark.attrs.type.nodeType === obj.type) {
206
+ obj.attrs = _objectSpread({}, getUnwrappedNodeAttributes(node, nodeAttributeMark, obj));
207
+ }
208
+ }
209
+ return obj;
210
+ };
211
+ var JSONTransformer = exports.JSONTransformer = /*#__PURE__*/function () {
212
+ /**
213
+ * Creates a new JSONTransformer instance.
214
+ * @param schema The current editor schema
215
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
216
+ * mention node.attr.text values with mapped names.
217
+ */
218
+ function JSONTransformer() {
219
+ var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _schemaDefault.defaultSchema;
220
+ var mentionMap = arguments.length > 1 ? arguments[1] : undefined;
221
+ (0, _classCallCheck2.default)(this, JSONTransformer);
222
+ this.schema = schema;
223
+ this.mentionMap = mentionMap;
224
+ }
225
+ return (0, _createClass2.default)(JSONTransformer, [{
226
+ key: "encode",
227
+ value: function encode(node) {
228
+ var _this = this;
229
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
230
+ var content = [];
231
+ node.content.forEach(function (child) {
232
+ content.push((0, _sanitizeNode.sanitizeNode)(_toJSON(child, _this.mentionMap), options));
233
+ });
234
+ if (!content || (0, _isEqual.default)(content, emptyDoc.content)) {
235
+ return createDocFromContent([]);
236
+ }
237
+ return createDocFromContent(content);
238
+ }
239
+ }, {
240
+ key: "internalParse",
241
+ value: function internalParse(content, schema) {
242
+ var doc = schema.nodeFromJSON(content);
243
+ doc.check();
244
+ return doc;
245
+ }
246
+ }, {
247
+ key: "parse",
248
+ value: function parse(content, stage) {
249
+ if (content.type !== 'doc') {
250
+ throw new Error('Expected content format to be ADF');
251
+ }
252
+ var schema = !!stage ? (0, _schemaDefault.getSchemaBasedOnStage)(stage) : this.schema;
253
+ if (!content.content || content.content.length === 0) {
254
+ return this.internalParse(emptyDoc, schema);
255
+ }
256
+ return this.internalParse(content, schema);
257
+ }
258
+
259
+ /**
260
+ * This method is used to encode a single node
261
+ */
262
+ }, {
263
+ key: "encodeNode",
264
+ value: function encodeNode(node) {
265
+ return (0, _sanitizeNode.sanitizeNode)(_toJSON(node, this.mentionMap));
266
+ }
267
+ }]);
268
+ }();