@atlaskit/editor-json-transformer 9.0.0 → 9.0.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.
- package/CHANGELOG.md +15 -0
- package/compass.yml +41 -0
- package/dist/cjs/index.js +20 -260
- package/dist/cjs/jsonTransformer.js +268 -0
- package/dist/es2019/index.js +1 -239
- package/dist/es2019/jsonTransformer.js +239 -0
- package/dist/esm/index.js +1 -259
- package/dist/esm/jsonTransformer.js +261 -0
- package/dist/types/index.d.ts +2 -38
- package/dist/types/jsonTransformer.d.ts +39 -0
- package/editor-json-transformer.docs.tsx +3 -8
- package/jsonTransformer/package.json +8 -0
- package/package.json +4 -4
package/dist/esm/index.js
CHANGED
|
@@ -1,266 +1,8 @@
|
|
|
1
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
1
|
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
2
|
// Disable no-re-export rule for entry point files
|
|
11
3
|
/* eslint-disable @atlaskit/editor/no-re-export */
|
|
12
4
|
|
|
13
|
-
|
|
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
|
-
}();
|
|
5
|
+
export { JSONTransformer, SchemaStage, toJSON as nodeToJSON } from './jsonTransformer';
|
|
264
6
|
|
|
265
7
|
/**
|
|
266
8
|
* Quick type guard to check if a value is a valid JSONDocNode
|
|
@@ -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
|
+
}();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,42 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { SanitizeNodeOptions } from './sanitize/sanitize-node';
|
|
3
|
-
import type { JSONDocNode, JSONNode } from './types';
|
|
1
|
+
import type { JSONDocNode } from './types';
|
|
4
2
|
export type { JSONDocNode, JSONNode } from './types';
|
|
5
|
-
|
|
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
|
-
}
|
|
3
|
+
export { JSONTransformer, SchemaStage, toJSON as nodeToJSON } from './jsonTransformer';
|
|
40
4
|
/**
|
|
41
5
|
* Quick type guard to check if a value is a valid JSONDocNode
|
|
42
6
|
* This is only a basic structural check, does not validate the entire object nor the schema.
|
|
@@ -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 {};
|
|
@@ -14,13 +14,13 @@ const packagePath = path.resolve(__dirname);
|
|
|
14
14
|
const documentation: StructuredContentSource = {
|
|
15
15
|
components: [
|
|
16
16
|
{
|
|
17
|
-
name: '
|
|
17
|
+
name: 'JSONTransformer',
|
|
18
18
|
description: 'JSON transformer',
|
|
19
19
|
status: 'general-availability',
|
|
20
20
|
import: {
|
|
21
|
-
name: '
|
|
21
|
+
name: 'JSONTransformer',
|
|
22
22
|
package: '@atlaskit/editor-json-transformer',
|
|
23
|
-
type: '
|
|
23
|
+
type: 'named',
|
|
24
24
|
packagePath,
|
|
25
25
|
packageJson,
|
|
26
26
|
},
|
|
@@ -30,11 +30,6 @@ const documentation: StructuredContentSource = {
|
|
|
30
30
|
keywords: ['editor', 'editor-json-transformer', 'atlaskit'],
|
|
31
31
|
categories: ['editor'],
|
|
32
32
|
examples: [
|
|
33
|
-
{
|
|
34
|
-
name: 'JSON transformer',
|
|
35
|
-
description: 'Transform between JSON and ADF.',
|
|
36
|
-
source: path.resolve(packagePath, './examples/0-json-transformer.tsx'),
|
|
37
|
-
},
|
|
38
33
|
{
|
|
39
34
|
name: 'Layouts',
|
|
40
35
|
description: 'JSON transformer with layout nodes.',
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@atlaskit/editor-json-transformer/jsonTransformer",
|
|
3
|
+
"main": "../dist/cjs/jsonTransformer.js",
|
|
4
|
+
"module": "../dist/esm/jsonTransformer.js",
|
|
5
|
+
"module:es2019": "../dist/es2019/jsonTransformer.js",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"types": "../dist/types/jsonTransformer.d.ts"
|
|
8
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/editor-json-transformer",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.2",
|
|
4
4
|
"description": "JSON transformer",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -21,14 +21,14 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@atlaskit/adf-schema": "^
|
|
25
|
-
"@atlaskit/adf-utils": "^20.
|
|
24
|
+
"@atlaskit/adf-schema": "^56.1.0",
|
|
25
|
+
"@atlaskit/adf-utils": "^20.3.0",
|
|
26
26
|
"@atlaskit/editor-prosemirror": "^8.0.0",
|
|
27
27
|
"@babel/runtime": "^7.0.0",
|
|
28
28
|
"lodash": "^4.17.21"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@atlassian/a11y-jest-testing": "^0.
|
|
31
|
+
"@atlassian/a11y-jest-testing": "^0.13.0",
|
|
32
32
|
"@atlassian/structured-docs-types": "workspace:^",
|
|
33
33
|
"react": "^18.2.0"
|
|
34
34
|
},
|