@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/dist/esm/index.js CHANGED
@@ -1,290 +1,5 @@
1
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/createClass";
3
- import _typeof from "@babel/runtime/helpers/typeof";
4
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
5
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
6
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
7
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
- 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; }
9
- 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; }
10
1
  // Disable no-re-export rule for entry point files
11
2
  /* eslint-disable @atlaskit/editor/no-re-export */
12
3
 
13
- import isEqual from 'lodash/isEqual';
14
- import { codeBlockToJSON, dataConsumerToJSON, expandToJSON, fragmentToJSON, linkToJSON, mediaSingleToJSON, mediaToJSON, mentionToJSON, tableToJSON, toJSONTableCell, toJSONTableHeader } from '@atlaskit/adf-schema';
15
- import { defaultSchema, getSchemaBasedOnStage } from '@atlaskit/adf-schema/schema-default';
16
- import { markOverrideRuleFor } from './markOverrideRules';
17
- import { sanitizeNode } from './sanitize/sanitize-node';
18
- export var SchemaStage = /*#__PURE__*/function (SchemaStage) {
19
- SchemaStage["FINAL"] = "final";
20
- SchemaStage["STAGE_0"] = "stage0";
21
- return SchemaStage;
22
- }({});
23
- var isType = function isType(type) {
24
- return function (node) {
25
- return node.type.name === type;
26
- };
27
- };
28
-
29
- // Create a Set of node types that should have empty attrs removed
30
- var NODES_WITH_EMPTY_ATTRS_REMOVAL = new Set(['paragraph', 'layoutSection', 'blockquote', 'nestedExpand', 'bulletList', 'listItem', 'orderedList', 'rule', 'caption', 'tableRow', 'media', 'mediaSingle', 'mediaInline']);
31
- var isCodeBlock = isType('codeBlock');
32
- var isMediaNode = isType('media');
33
- var isMediaInline = isType('mediaInline');
34
- var isMediaSingleNode = isType('mediaSingle');
35
- var isMentionNode = isType('mention');
36
- var isParagraph = isType('paragraph');
37
- var isHeading = isType('heading');
38
- var isTable = isType('table');
39
- var isTableCell = isType('tableCell');
40
- var isTableHeader = isType('tableHeader');
41
- var isLinkMark = isType('link');
42
- var isUnsupportedMark = isType('unsupportedMark');
43
- var isUnsupportedNodeAttributeMark = isType('unsupportedNodeAttribute');
44
- var isExpand = isType('expand');
45
- var isNestedExpand = isType('nestedExpand');
46
- var isUnsupportedNode = function isUnsupportedNode(node) {
47
- return isType('unsupportedBlock')(node) || isType('unsupportedInline')(node);
48
- };
49
- var isDataConsumer = isType('dataConsumer');
50
- var isFragmentMark = isType('fragment');
51
- var isHardBreak = isType('hardBreak');
52
-
53
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
- var _filterNull = function filterNull(subject) {
55
- var output = _objectSpread({}, subject);
56
- // eslint-disable-next-line guard-for-in
57
- for (var key in output) {
58
- var current = output[key];
59
- if (current === null) {
60
- var _output = output,
61
- unusedKey = _output[key],
62
- filteredObj = _objectWithoutProperties(_output, [key].map(_toPropertyKey));
63
- output = filteredObj;
64
- } else if (_typeof(current) === 'object' && !Array.isArray(current)) {
65
- output[key] = _filterNull(current);
66
- }
67
- }
68
- return output;
69
- };
70
- var createDocFromContent = function createDocFromContent(content) {
71
- return {
72
- version: 1,
73
- type: 'doc',
74
- content: content || []
75
- };
76
- };
77
- var emptyDoc = createDocFromContent([{
78
- type: 'paragraph',
79
- content: []
80
- }]);
81
-
82
- /**
83
- * Transforms a Prosemirror node into a JSON representation. Compared with
84
- * native Prosemirror JSON, this representation ensures we have valid ADF attrs.
85
- *
86
- * @param node The Prosemirror node to transform.
87
- * @param mentionMap A map of mention IDs to their display names.
88
- * @returns The JSON representation of the node.
89
- */
90
- var _toJSON = function toJSON(node, mentionMap) {
91
- var obj = {
92
- type: node.type.name
93
- };
94
- if (isUnsupportedNode(node)) {
95
- return node.attrs.originalValue;
96
- } else if (isMediaNode(node)) {
97
- obj.attrs = mediaToJSON(node).attrs;
98
- } else if (isMediaSingleNode(node)) {
99
- obj.attrs = mediaSingleToJSON(node).attrs;
100
- } else if (isMediaInline(node)) {
101
- obj.attrs = mediaToJSON(node).attrs;
102
- } else if (isMentionNode(node)) {
103
- obj.attrs = mentionToJSON(node).attrs;
104
- } else if (isCodeBlock(node)) {
105
- obj.attrs = codeBlockToJSON(node).attrs;
106
- } else if (isTable(node)) {
107
- obj.attrs = tableToJSON(node).attrs;
108
- } else if (isTableCell(node)) {
109
- obj.attrs = toJSONTableCell(node).attrs;
110
- } else if (isTableHeader(node)) {
111
- obj.attrs = toJSONTableHeader(node).attrs;
112
- } else if (isExpand(node) || isNestedExpand(node)) {
113
- obj.attrs = expandToJSON(node).attrs;
114
- } else if (node.attrs && Object.keys(node.attrs).length) {
115
- obj.attrs = node.attrs;
116
- }
117
- if (obj.attrs) {
118
- obj.attrs = _filterNull(obj.attrs);
119
- }
120
- if (isMentionNode(node) && mentionMap) {
121
- var _text, _obj$attrs;
122
- // If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
123
- var name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? "@".concat(mentionMap[node.attrs.id]) : '';
124
- // Ignored via go/ees005
125
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
- var text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
127
- obj.attrs = _objectSpread(_objectSpread({}, obj.attrs), {}, {
128
- // If an explicit text value is set on the mention then that should take priority over the mentionMap value.
129
- text: !!text ? text : name
130
- });
131
- }
132
- if (NODES_WITH_EMPTY_ATTRS_REMOVAL.has(node.type.name) && obj.attrs && !Object.keys(obj.attrs).length || isHardBreak(node)) {
133
- delete obj.attrs;
134
- }
135
- if (node.isText) {
136
- obj.text = node.textContent;
137
- } else {
138
- node.content.forEach(function (child) {
139
- obj.content = obj.content || [];
140
- obj.content.push(_toJSON(child, mentionMap));
141
- });
142
- }
143
- if (isParagraph(node) || isHeading(node)) {
144
- obj.content = obj.content || [];
145
- }
146
- if (node.marks.length) {
147
- // Run any custom mark serialisers
148
- var parsedMarks = node.marks.map(function (mark) {
149
- if (isUnsupportedMark(mark)) {
150
- return canOverrideMark(mark, node.marks) ? null : mark.attrs.originalValue;
151
- } else if (isUnsupportedNodeAttributeMark(mark)) {
152
- return null;
153
- } else if (isLinkMark(mark)) {
154
- return linkToJSON(mark);
155
- } else if (isDataConsumer(mark)) {
156
- var _serialised$attrs$sou;
157
- var serialised = dataConsumerToJSON(mark);
158
- 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;
159
- } else if (isFragmentMark(mark)) {
160
- var fragmentMark = fragmentToJSON(mark);
161
- if (!fragmentMark.attrs.localId) {
162
- return null;
163
- }
164
- return fragmentMark;
165
- } else {
166
- return mark.toJSON();
167
- }
168
- }).filter(function (maybeMark) {
169
- return maybeMark !== null;
170
- });
171
-
172
- // Only set if we have a non-empty array, otherwise explicitly undefine it (as we only run this path if `node.marks.length`)
173
- obj.marks = (parsedMarks === null || parsedMarks === void 0 ? void 0 : parsedMarks.length) > 0 ? parsedMarks : undefined;
174
- var nodeAttributeMark = node.marks.find(isUnsupportedNodeAttributeMark);
175
- if (nodeAttributeMark && nodeAttributeMark.attrs.type.nodeType === obj.type) {
176
- obj.attrs = _objectSpread({}, getUnwrappedNodeAttributes(node, nodeAttributeMark, obj));
177
- }
178
- }
179
- return obj;
180
- };
181
- export { _toJSON as nodeToJSON };
182
- var canOverrideMark = function canOverrideMark(mark, existingMarks) {
183
- if (existingMarks.some(function (e) {
184
- return mark.attrs.originalValue.type === e.type.name;
185
- })) {
186
- return markOverrideRuleFor(mark.attrs.originalValue.type).canOverrideUnsupportedMark();
187
- }
188
- return false;
189
- };
190
- var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark, obj) {
191
- var nodeAttributes = node.type.spec.attrs;
192
- var attributes = _objectSpread(_objectSpread({}, mark.attrs.unsupported), obj.attrs);
193
- // Ignored via go/ees005
194
- // eslint-disable-next-line no-var
195
- for (var key in obj.attrs) {
196
- if (obj.attrs.hasOwnProperty(key)) {
197
- var attribute = nodeAttributes ? nodeAttributes[key] : null;
198
- if (attribute) {
199
- if (attribute.default === node.attrs[key] && mark.attrs.unsupported[key]) {
200
- return _objectSpread(_objectSpread({}, attributes), {}, _defineProperty({}, key, mark.attrs.unsupported[key]));
201
- }
202
- }
203
- }
204
- }
205
- return attributes;
206
- };
207
- export var JSONTransformer = /*#__PURE__*/function () {
208
- /**
209
- * @param schema The current editor schema
210
- * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
211
- * mention node.attr.text values with mapped names.
212
- */
213
- function JSONTransformer() {
214
- var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultSchema;
215
- var mentionMap = arguments.length > 1 ? arguments[1] : undefined;
216
- _classCallCheck(this, JSONTransformer);
217
- this.schema = schema;
218
- this.mentionMap = mentionMap;
219
- }
220
- return _createClass(JSONTransformer, [{
221
- key: "encode",
222
- value: function encode(node) {
223
- var _this = this;
224
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
225
- var content = [];
226
- node.content.forEach(function (child) {
227
- content.push(sanitizeNode(_toJSON(child, _this.mentionMap), options));
228
- });
229
- if (!content || isEqual(content, emptyDoc.content)) {
230
- return createDocFromContent([]);
231
- }
232
- return createDocFromContent(content);
233
- }
234
- }, {
235
- key: "internalParse",
236
- value: function internalParse(content, schema) {
237
- var doc = schema.nodeFromJSON(content);
238
- doc.check();
239
- return doc;
240
- }
241
- }, {
242
- key: "parse",
243
- value: function parse(content, stage) {
244
- if (content.type !== 'doc') {
245
- throw new Error('Expected content format to be ADF');
246
- }
247
- var schema = !!stage ? getSchemaBasedOnStage(stage) : this.schema;
248
- if (!content.content || content.content.length === 0) {
249
- return this.internalParse(emptyDoc, schema);
250
- }
251
- return this.internalParse(content, schema);
252
- }
253
-
254
- /**
255
- * This method is used to encode a single node
256
- */
257
- }, {
258
- key: "encodeNode",
259
- value: function encodeNode(node) {
260
- return sanitizeNode(_toJSON(node, this.mentionMap));
261
- }
262
- }]);
263
- }();
264
-
265
- /**
266
- * Quick type guard to check if a value is a valid JSONDocNode
267
- * This is only a basic structural check, does not validate the entire object nor the schema.
268
- * @param value - The value to check
269
- * @returns true if the value is a valid JSONDocNode structure
270
- */
271
- export function isJSONDocNode(value) {
272
- if (!value || _typeof(value) !== 'object') {
273
- return false;
274
- }
275
- var doc = value;
276
-
277
- // Check required properties
278
- if (doc.type !== 'doc') {
279
- return false;
280
- }
281
- if (!Array.isArray(doc.content)) {
282
- return false;
283
- }
284
-
285
- // Version is optional but if present should be a number
286
- if (doc.version !== undefined && typeof doc.version !== 'number') {
287
- return false;
288
- }
289
- return true;
290
- }
4
+ export { JSONTransformer, SchemaStage, toJSON as nodeToJSON } from './jsonTransformer';
5
+ export { isJSONDocNode } from './entry-points/main';
@@ -0,0 +1,261 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _typeof from "@babel/runtime/helpers/typeof";
4
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
5
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
6
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
7
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
+ 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; }
9
+ 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; }
10
+ import isEqual from 'lodash/isEqual';
11
+ import { codeBlockToJSON, dataConsumerToJSON, expandToJSON, fragmentToJSON, linkToJSON, mediaSingleToJSON, mediaToJSON, mentionToJSON, tableToJSON, toJSONTableCell, toJSONTableHeader } from '@atlaskit/adf-schema';
12
+ import { defaultSchema, getSchemaBasedOnStage } from '@atlaskit/adf-schema/schema-default';
13
+ import { markOverrideRuleFor } from './markOverrideRules';
14
+ import { sanitizeNode } from './sanitize/sanitize-node';
15
+ export var SchemaStage = /*#__PURE__*/function (SchemaStage) {
16
+ SchemaStage["FINAL"] = "final";
17
+ SchemaStage["STAGE_0"] = "stage0";
18
+ return SchemaStage;
19
+ }({});
20
+ var isType = function isType(type) {
21
+ return function (node) {
22
+ return node.type.name === type;
23
+ };
24
+ };
25
+
26
+ // Create a Set of node types that should have empty attrs removed
27
+ var NODES_WITH_EMPTY_ATTRS_REMOVAL = new Set(['paragraph', 'layoutSection', 'blockquote', 'nestedExpand', 'bulletList', 'listItem', 'orderedList', 'rule', 'caption', 'tableRow', 'media', 'mediaSingle', 'mediaInline']);
28
+ var isCodeBlock = isType('codeBlock');
29
+ var isMediaNode = isType('media');
30
+ var isMediaInline = isType('mediaInline');
31
+ var isMediaSingleNode = isType('mediaSingle');
32
+ var isMentionNode = isType('mention');
33
+ var isParagraph = isType('paragraph');
34
+ var isHeading = isType('heading');
35
+ var isTable = isType('table');
36
+ var isTableCell = isType('tableCell');
37
+ var isTableHeader = isType('tableHeader');
38
+ var isLinkMark = isType('link');
39
+ var isUnsupportedMark = isType('unsupportedMark');
40
+ var isUnsupportedNodeAttributeMark = isType('unsupportedNodeAttribute');
41
+ var isExpand = isType('expand');
42
+ var isNestedExpand = isType('nestedExpand');
43
+ var isUnsupportedNode = function isUnsupportedNode(node) {
44
+ return isType('unsupportedBlock')(node) || isType('unsupportedInline')(node);
45
+ };
46
+ var isDataConsumer = isType('dataConsumer');
47
+ var isFragmentMark = isType('fragment');
48
+ var isHardBreak = isType('hardBreak');
49
+ var createDocFromContent = function createDocFromContent(content) {
50
+ return {
51
+ version: 1,
52
+ type: 'doc',
53
+ content: content || []
54
+ };
55
+ };
56
+ var emptyDoc = createDocFromContent([{
57
+ type: 'paragraph',
58
+ content: []
59
+ }]);
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 = _objectWithoutProperties(_output, [key].map(_toPropertyKey));
71
+ output = filteredObj;
72
+ } else if (_typeof(current) === 'object' && !Array.isArray(current)) {
73
+ output[key] = _filterNull(current);
74
+ }
75
+ }
76
+ return output;
77
+ };
78
+ var canOverrideMark = function canOverrideMark(mark, existingMarks) {
79
+ if (existingMarks.some(function (e) {
80
+ return mark.attrs.originalValue.type === e.type.name;
81
+ })) {
82
+ return markOverrideRuleFor(mark.attrs.originalValue.type).canOverrideUnsupportedMark();
83
+ }
84
+ return false;
85
+ };
86
+ var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark, obj) {
87
+ var nodeAttributes = node.type.spec.attrs;
88
+ var attributes = _objectSpread(_objectSpread({}, mark.attrs.unsupported), obj.attrs);
89
+ // Ignored via go/ees005
90
+ // eslint-disable-next-line no-var
91
+ for (var key in obj.attrs) {
92
+ if (obj.attrs.hasOwnProperty(key)) {
93
+ var attribute = nodeAttributes ? nodeAttributes[key] : null;
94
+ if (attribute) {
95
+ if (attribute.default === node.attrs[key] && mark.attrs.unsupported[key]) {
96
+ return _objectSpread(_objectSpread({}, attributes), {}, _defineProperty({}, key, mark.attrs.unsupported[key]));
97
+ }
98
+ }
99
+ }
100
+ }
101
+ return attributes;
102
+ };
103
+
104
+ /**
105
+ * Transforms a Prosemirror node into a JSON representation. Compared with
106
+ * native Prosemirror JSON, this representation ensures we have valid ADF attrs.
107
+ *
108
+ * @param node The Prosemirror node to transform.
109
+ * @param mentionMap A map of mention IDs to their display names.
110
+ * @returns The JSON representation of the node.
111
+ */
112
+ var _toJSON = function toJSON(node, mentionMap) {
113
+ var obj = {
114
+ type: node.type.name
115
+ };
116
+ if (isUnsupportedNode(node)) {
117
+ return node.attrs.originalValue;
118
+ } else if (isMediaNode(node)) {
119
+ obj.attrs = mediaToJSON(node).attrs;
120
+ } else if (isMediaSingleNode(node)) {
121
+ obj.attrs = mediaSingleToJSON(node).attrs;
122
+ } else if (isMediaInline(node)) {
123
+ obj.attrs = mediaToJSON(node).attrs;
124
+ } else if (isMentionNode(node)) {
125
+ obj.attrs = mentionToJSON(node).attrs;
126
+ } else if (isCodeBlock(node)) {
127
+ obj.attrs = codeBlockToJSON(node).attrs;
128
+ } else if (isTable(node)) {
129
+ obj.attrs = tableToJSON(node).attrs;
130
+ } else if (isTableCell(node)) {
131
+ obj.attrs = toJSONTableCell(node).attrs;
132
+ } else if (isTableHeader(node)) {
133
+ obj.attrs = toJSONTableHeader(node).attrs;
134
+ } else if (isExpand(node) || isNestedExpand(node)) {
135
+ obj.attrs = expandToJSON(node).attrs;
136
+ } else if (node.attrs && Object.keys(node.attrs).length) {
137
+ obj.attrs = node.attrs;
138
+ }
139
+ if (obj.attrs) {
140
+ obj.attrs = _filterNull(obj.attrs);
141
+ }
142
+ if (isMentionNode(node) && mentionMap) {
143
+ var _text, _obj$attrs;
144
+ // If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
145
+ var name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? "@".concat(mentionMap[node.attrs.id]) : '';
146
+ // Ignored via go/ees005
147
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
148
+ var text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
149
+ obj.attrs = _objectSpread(_objectSpread({}, obj.attrs), {}, {
150
+ // If an explicit text value is set on the mention then that should take priority over the mentionMap value.
151
+ text: !!text ? text : name
152
+ });
153
+ }
154
+ if (NODES_WITH_EMPTY_ATTRS_REMOVAL.has(node.type.name) && obj.attrs && !Object.keys(obj.attrs).length || isHardBreak(node)) {
155
+ delete obj.attrs;
156
+ }
157
+ if (node.isText) {
158
+ obj.text = node.textContent;
159
+ } else {
160
+ node.content.forEach(function (child) {
161
+ obj.content = obj.content || [];
162
+ obj.content.push(_toJSON(child, mentionMap));
163
+ });
164
+ }
165
+ if (isParagraph(node) || isHeading(node)) {
166
+ obj.content = obj.content || [];
167
+ }
168
+ if (node.marks.length) {
169
+ // Run any custom mark serialisers
170
+ var parsedMarks = node.marks.map(function (mark) {
171
+ if (isUnsupportedMark(mark)) {
172
+ return canOverrideMark(mark, node.marks) ? null : mark.attrs.originalValue;
173
+ } else if (isUnsupportedNodeAttributeMark(mark)) {
174
+ return null;
175
+ } else if (isLinkMark(mark)) {
176
+ return linkToJSON(mark);
177
+ } else if (isDataConsumer(mark)) {
178
+ var _serialised$attrs$sou;
179
+ var serialised = dataConsumerToJSON(mark);
180
+ 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;
181
+ } else if (isFragmentMark(mark)) {
182
+ var fragmentMark = fragmentToJSON(mark);
183
+ if (!fragmentMark.attrs.localId) {
184
+ return null;
185
+ }
186
+ return fragmentMark;
187
+ } else {
188
+ return mark.toJSON();
189
+ }
190
+ }).filter(function (maybeMark) {
191
+ return maybeMark !== null;
192
+ });
193
+
194
+ // Only set if we have a non-empty array, otherwise explicitly undefine it (as we only run this path if `node.marks.length`)
195
+ obj.marks = (parsedMarks === null || parsedMarks === void 0 ? void 0 : parsedMarks.length) > 0 ? parsedMarks : undefined;
196
+ var nodeAttributeMark = node.marks.find(isUnsupportedNodeAttributeMark);
197
+ if (nodeAttributeMark && nodeAttributeMark.attrs.type.nodeType === obj.type) {
198
+ obj.attrs = _objectSpread({}, getUnwrappedNodeAttributes(node, nodeAttributeMark, obj));
199
+ }
200
+ }
201
+ return obj;
202
+ };
203
+ export { _toJSON as toJSON };
204
+ export var JSONTransformer = /*#__PURE__*/function () {
205
+ /**
206
+ * Creates a new JSONTransformer instance.
207
+ * @param schema The current editor schema
208
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
209
+ * mention node.attr.text values with mapped names.
210
+ */
211
+ function JSONTransformer() {
212
+ var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultSchema;
213
+ var mentionMap = arguments.length > 1 ? arguments[1] : undefined;
214
+ _classCallCheck(this, JSONTransformer);
215
+ this.schema = schema;
216
+ this.mentionMap = mentionMap;
217
+ }
218
+ return _createClass(JSONTransformer, [{
219
+ key: "encode",
220
+ value: function encode(node) {
221
+ var _this = this;
222
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
223
+ var content = [];
224
+ node.content.forEach(function (child) {
225
+ content.push(sanitizeNode(_toJSON(child, _this.mentionMap), options));
226
+ });
227
+ if (!content || isEqual(content, emptyDoc.content)) {
228
+ return createDocFromContent([]);
229
+ }
230
+ return createDocFromContent(content);
231
+ }
232
+ }, {
233
+ key: "internalParse",
234
+ value: function internalParse(content, schema) {
235
+ var doc = schema.nodeFromJSON(content);
236
+ doc.check();
237
+ return doc;
238
+ }
239
+ }, {
240
+ key: "parse",
241
+ value: function parse(content, stage) {
242
+ if (content.type !== 'doc') {
243
+ throw new Error('Expected content format to be ADF');
244
+ }
245
+ var schema = !!stage ? getSchemaBasedOnStage(stage) : this.schema;
246
+ if (!content.content || content.content.length === 0) {
247
+ return this.internalParse(emptyDoc, schema);
248
+ }
249
+ return this.internalParse(content, schema);
250
+ }
251
+
252
+ /**
253
+ * This method is used to encode a single node
254
+ */
255
+ }, {
256
+ key: "encodeNode",
257
+ value: function encodeNode(node) {
258
+ return sanitizeNode(_toJSON(node, this.mentionMap));
259
+ }
260
+ }]);
261
+ }();
@@ -0,0 +1,27 @@
1
+ import _typeof from "@babel/runtime/helpers/typeof";
2
+ /**
3
+ * Quick type guard to check if a value is a valid JSONDocNode
4
+ * This is only a basic structural check, does not validate the entire object nor the schema.
5
+ * @param value - The value to check
6
+ * @returns true if the value is a valid JSONDocNode structure
7
+ */
8
+ export function isJSONDocNode(value) {
9
+ if (!value || _typeof(value) !== 'object') {
10
+ return false;
11
+ }
12
+ var doc = value;
13
+
14
+ // Check required properties
15
+ if (doc.type !== 'doc') {
16
+ return false;
17
+ }
18
+ if (!Array.isArray(doc.content)) {
19
+ return false;
20
+ }
21
+
22
+ // Version is optional but if present should be a number
23
+ if (doc.version !== undefined && typeof doc.version !== 'number') {
24
+ return false;
25
+ }
26
+ return true;
27
+ }
@@ -0,0 +1 @@
1
+ export { isJSONDocNode } from '../main';
@@ -1,46 +1,3 @@
1
- import type { Node as PMNode, Schema } from '@atlaskit/editor-prosemirror/model';
2
- import type { SanitizeNodeOptions } from './sanitize/sanitize-node';
3
- import type { JSONDocNode, JSONNode } from './types';
4
1
  export type { JSONDocNode, JSONNode } from './types';
5
- interface Transformer<T> {
6
- encode: (node: PMNode) => T;
7
- parse: (content: T) => PMNode;
8
- }
9
- export declare enum SchemaStage {
10
- FINAL = "final",
11
- STAGE_0 = "stage0"
12
- }
13
- /**
14
- * Transforms a Prosemirror node into a JSON representation. Compared with
15
- * native Prosemirror JSON, this representation ensures we have valid ADF attrs.
16
- *
17
- * @param node The Prosemirror node to transform.
18
- * @param mentionMap A map of mention IDs to their display names.
19
- * @returns The JSON representation of the node.
20
- */
21
- declare const toJSON: (node: PMNode, mentionMap?: Record<string, string | undefined>) => JSONNode;
22
- export { toJSON as nodeToJSON };
23
- export declare class JSONTransformer implements Transformer<JSONDocNode> {
24
- private schema;
25
- private mentionMap;
26
- /**
27
- * @param schema The current editor schema
28
- * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
29
- * mention node.attr.text values with mapped names.
30
- */
31
- constructor(schema?: Schema, mentionMap?: Record<string, string | undefined>);
32
- encode(node: PMNode, options?: SanitizeNodeOptions): JSONDocNode;
33
- private internalParse;
34
- parse(content: JSONDocNode, stage?: SchemaStage): PMNode;
35
- /**
36
- * This method is used to encode a single node
37
- */
38
- encodeNode(node: PMNode): JSONNode;
39
- }
40
- /**
41
- * Quick type guard to check if a value is a valid JSONDocNode
42
- * This is only a basic structural check, does not validate the entire object nor the schema.
43
- * @param value - The value to check
44
- * @returns true if the value is a valid JSONDocNode structure
45
- */
46
- export declare function isJSONDocNode(value: unknown): value is JSONDocNode;
2
+ export { JSONTransformer, SchemaStage, toJSON as nodeToJSON } from './jsonTransformer';
3
+ export { isJSONDocNode } from './entry-points/main';
@@ -0,0 +1,39 @@
1
+ import type { Node as PMNode, Schema } from '@atlaskit/editor-prosemirror/model';
2
+ import type { SanitizeNodeOptions } from './sanitize/sanitize-node';
3
+ import type { JSONDocNode, JSONNode } from './types';
4
+ export declare enum SchemaStage {
5
+ FINAL = "final",
6
+ STAGE_0 = "stage0"
7
+ }
8
+ interface Transformer<T> {
9
+ encode: (node: PMNode) => T;
10
+ parse: (content: T) => PMNode;
11
+ }
12
+ /**
13
+ * Transforms a Prosemirror node into a JSON representation. Compared with
14
+ * native Prosemirror JSON, this representation ensures we have valid ADF attrs.
15
+ *
16
+ * @param node The Prosemirror node to transform.
17
+ * @param mentionMap A map of mention IDs to their display names.
18
+ * @returns The JSON representation of the node.
19
+ */
20
+ export declare const toJSON: (node: PMNode, mentionMap?: Record<string, string | undefined>) => JSONNode;
21
+ export declare class JSONTransformer implements Transformer<JSONDocNode> {
22
+ private schema;
23
+ private mentionMap;
24
+ /**
25
+ * Creates a new JSONTransformer instance.
26
+ * @param schema The current editor schema
27
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
28
+ * mention node.attr.text values with mapped names.
29
+ */
30
+ constructor(schema?: Schema, mentionMap?: Record<string, string | undefined>);
31
+ encode(node: PMNode, options?: SanitizeNodeOptions): JSONDocNode;
32
+ private internalParse;
33
+ parse(content: JSONDocNode, stage?: SchemaStage): PMNode;
34
+ /**
35
+ * This method is used to encode a single node
36
+ */
37
+ encodeNode(node: PMNode): JSONNode;
38
+ }
39
+ export {};
@@ -0,0 +1,8 @@
1
+ import type { JSONDocNode } from './types';
2
+ /**
3
+ * Quick type guard to check if a value is a valid JSONDocNode
4
+ * This is only a basic structural check, does not validate the entire object nor the schema.
5
+ * @param value - The value to check
6
+ * @returns true if the value is a valid JSONDocNode structure
7
+ */
8
+ export declare function isJSONDocNode(value: unknown): value is JSONDocNode;