@atlaskit/editor-json-transformer 8.20.2 → 8.20.4

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,22 @@
1
1
  # @atlaskit/editor-json-transformer
2
2
 
3
+ ## 8.20.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [`9a4a3df453d84`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/9a4a3df453d84) -
8
+ Updated the JSONTransformer to support using an mention mapping. During the encoding of mentions
9
+ the text names can be pulled from the mention map and encoded.
10
+ - Updated dependencies
11
+
12
+ ## 8.20.3
13
+
14
+ ### Patch Changes
15
+
16
+ - [#159176](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/159176)
17
+ [`8f1d77592a9dc`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/8f1d77592a9dc) -
18
+ Bump adf-schema to 44.2.0
19
+
3
20
  ## 8.20.2
4
21
 
5
22
  ### Patch Changes
@@ -5,8 +5,7 @@
5
5
  "target": "es5",
6
6
  "composite": true,
7
7
  "outDir": "../dist",
8
- "rootDir": "../",
9
- "baseUrl": "../"
8
+ "rootDir": "../"
10
9
  },
11
10
  "include": [
12
11
  "../src/**/*.ts",
@@ -1,24 +1,24 @@
1
1
  {
2
- "extends": "../../../../tsconfig.entry-points.jira.json",
3
- "compilerOptions": {
4
- "declaration": true,
5
- "target": "es5",
6
- "outDir": "../../../../../tsDist/@atlaskit__editor-json-transformer/app",
7
- "composite": true,
8
- "rootDir": "../"
9
- },
10
- "include": [
11
- "../src/**/*.ts",
12
- "../src/**/*.tsx"
13
- ],
14
- "exclude": [
15
- "../src/**/__tests__/*",
16
- "../src/**/*.test.*",
17
- "../src/**/test.*"
18
- ],
19
- "references": [
20
- {
21
- "path": "../../adf-utils/afm-jira/tsconfig.json"
22
- }
23
- ]
24
- }
2
+ "extends": "../../../../tsconfig.entry-points.jira.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "target": "es5",
6
+ "outDir": "../../../../../tsDist/@atlaskit__editor-json-transformer/app",
7
+ "rootDir": "../",
8
+ "composite": true
9
+ },
10
+ "include": [
11
+ "../src/**/*.ts",
12
+ "../src/**/*.tsx"
13
+ ],
14
+ "exclude": [
15
+ "../src/**/__tests__/*",
16
+ "../src/**/*.test.*",
17
+ "../src/**/test.*"
18
+ ],
19
+ "references": [
20
+ {
21
+ "path": "../../adf-utils/afm-jira/tsconfig.json"
22
+ }
23
+ ]
24
+ }
@@ -1,24 +1,24 @@
1
1
  {
2
- "extends": "../../../../tsconfig.entry-points.post-office.json",
3
- "compilerOptions": {
4
- "declaration": true,
5
- "target": "es5",
6
- "outDir": "../../../../../tsDist/@atlaskit__editor-json-transformer/app",
7
- "composite": true,
8
- "rootDir": "../"
9
- },
10
- "include": [
11
- "../src/**/*.ts",
12
- "../src/**/*.tsx"
13
- ],
14
- "exclude": [
15
- "../src/**/__tests__/*",
16
- "../src/**/*.test.*",
17
- "../src/**/test.*"
18
- ],
19
- "references": [
20
- {
21
- "path": "../../adf-utils/afm-post-office/tsconfig.json"
22
- }
23
- ]
24
- }
2
+ "extends": "../../../../tsconfig.entry-points.post-office.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "target": "es5",
6
+ "outDir": "../../../../../tsDist/@atlaskit__editor-json-transformer/app",
7
+ "rootDir": "../",
8
+ "composite": true
9
+ },
10
+ "include": [
11
+ "../src/**/*.ts",
12
+ "../src/**/*.tsx"
13
+ ],
14
+ "exclude": [
15
+ "../src/**/__tests__/*",
16
+ "../src/**/*.test.*",
17
+ "../src/**/test.*"
18
+ ],
19
+ "references": [
20
+ {
21
+ "path": "../../adf-utils/afm-post-office/tsconfig.json"
22
+ }
23
+ ]
24
+ }
package/dist/cjs/index.js CHANGED
@@ -50,6 +50,7 @@ var isUnsupportedNode = function isUnsupportedNode(node) {
50
50
  };
51
51
  var isDataConsumer = isType('dataConsumer');
52
52
  var isFragmentMark = isType('fragment');
53
+ var isHardBreak = isType('hardBreak');
53
54
 
54
55
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
56
  var filterNull = function filterNull(subject) {
@@ -78,7 +79,7 @@ var emptyDoc = createDocFromContent([{
78
79
  type: 'paragraph',
79
80
  content: []
80
81
  }]);
81
- var toJSON = function toJSON(node) {
82
+ var toJSON = function toJSON(node, mentionMap) {
82
83
  var obj = {
83
84
  type: node.type.name
84
85
  };
@@ -108,17 +109,33 @@ var toJSON = function toJSON(node) {
108
109
  if (obj.attrs) {
109
110
  obj.attrs = filterNull(obj.attrs);
110
111
  }
112
+ if (isMentionNode(node) && mentionMap) {
113
+ var _text, _obj$attrs;
114
+ // If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
115
+ var name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? "@".concat(mentionMap[node.attrs.id]) : '';
116
+ var text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
117
+ obj.attrs = _objectSpread(_objectSpread({}, obj.attrs), {}, {
118
+ // If an explicit text value is set on the mention then that should take priority over the mentionMap value.
119
+ text: !!text ? text : name
120
+ });
121
+ }
111
122
 
112
123
  // Remove the attrs property if it's empty, this is currently limited to paragraph nodes.
113
124
  if (isParagraph(node) && obj.attrs && !Object.keys(obj.attrs).length) {
114
125
  delete obj.attrs;
115
126
  }
127
+
128
+ // Hard break has optional attr.text which was added for PM <-> ADF compatibility.
129
+ // No need to preserve it.
130
+ if (isHardBreak(node)) {
131
+ delete obj.attrs;
132
+ }
116
133
  if (node.isText) {
117
134
  obj.text = node.textContent;
118
135
  } else {
119
136
  node.content.forEach(function (child) {
120
137
  obj.content = obj.content || [];
121
- obj.content.push(toJSON(child));
138
+ obj.content.push(toJSON(child, mentionMap));
122
139
  });
123
140
  }
124
141
  if (isParagraph(node) || isHeading(node)) {
@@ -183,17 +200,25 @@ var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark,
183
200
  return attributes;
184
201
  };
185
202
  var JSONTransformer = exports.JSONTransformer = /*#__PURE__*/function () {
203
+ /**
204
+ * @param schema The current editor schema
205
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
206
+ * mention node.attr.text values with mapped names.
207
+ */
186
208
  function JSONTransformer() {
187
209
  var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _schemaDefault.defaultSchema;
210
+ var mentionMap = arguments.length > 1 ? arguments[1] : undefined;
188
211
  (0, _classCallCheck2.default)(this, JSONTransformer);
189
212
  this.schema = schema;
213
+ this.mentionMap = mentionMap;
190
214
  }
191
215
  (0, _createClass2.default)(JSONTransformer, [{
192
216
  key: "encode",
193
217
  value: function encode(node) {
218
+ var _this = this;
194
219
  var content = [];
195
220
  node.content.forEach(function (child) {
196
- content.push((0, _sanitizeNode.sanitizeNode)(toJSON(child)));
221
+ content.push((0, _sanitizeNode.sanitizeNode)(toJSON(child, _this.mentionMap)));
197
222
  });
198
223
  if (!content || (0, _isEqual.default)(content, emptyDoc.content)) {
199
224
  return createDocFromContent([]);
@@ -226,7 +251,7 @@ var JSONTransformer = exports.JSONTransformer = /*#__PURE__*/function () {
226
251
  }, {
227
252
  key: "encodeNode",
228
253
  value: function encodeNode(node) {
229
- return (0, _sanitizeNode.sanitizeNode)(toJSON(node));
254
+ return (0, _sanitizeNode.sanitizeNode)(toJSON(node, this.mentionMap));
230
255
  }
231
256
  }]);
232
257
  return JSONTransformer;
@@ -27,6 +27,7 @@ const isNestedExpand = isType('nestedExpand');
27
27
  const isUnsupportedNode = node => isType('unsupportedBlock')(node) || isType('unsupportedInline')(node);
28
28
  const isDataConsumer = isType('dataConsumer');
29
29
  const isFragmentMark = isType('fragment');
30
+ const isHardBreak = isType('hardBreak');
30
31
 
31
32
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
33
  const filterNull = subject => {
@@ -58,7 +59,7 @@ const emptyDoc = createDocFromContent([{
58
59
  type: 'paragraph',
59
60
  content: []
60
61
  }]);
61
- const toJSON = node => {
62
+ const toJSON = (node, mentionMap) => {
62
63
  const obj = {
63
64
  type: node.type.name
64
65
  };
@@ -88,17 +89,34 @@ const toJSON = node => {
88
89
  if (obj.attrs) {
89
90
  obj.attrs = filterNull(obj.attrs);
90
91
  }
92
+ if (isMentionNode(node) && mentionMap) {
93
+ var _text, _obj$attrs;
94
+ // If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
95
+ const name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? `@${mentionMap[node.attrs.id]}` : '';
96
+ const text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
97
+ obj.attrs = {
98
+ ...obj.attrs,
99
+ // If an explicit text value is set on the mention then that should take priority over the mentionMap value.
100
+ text: !!text ? text : name
101
+ };
102
+ }
91
103
 
92
104
  // Remove the attrs property if it's empty, this is currently limited to paragraph nodes.
93
105
  if (isParagraph(node) && obj.attrs && !Object.keys(obj.attrs).length) {
94
106
  delete obj.attrs;
95
107
  }
108
+
109
+ // Hard break has optional attr.text which was added for PM <-> ADF compatibility.
110
+ // No need to preserve it.
111
+ if (isHardBreak(node)) {
112
+ delete obj.attrs;
113
+ }
96
114
  if (node.isText) {
97
115
  obj.text = node.textContent;
98
116
  } else {
99
117
  node.content.forEach(child => {
100
118
  obj.content = obj.content || [];
101
- obj.content.push(toJSON(child));
119
+ obj.content.push(toJSON(child, mentionMap));
102
120
  });
103
121
  }
104
122
  if (isParagraph(node) || isHeading(node)) {
@@ -167,13 +185,19 @@ const getUnwrappedNodeAttributes = (node, mark, obj) => {
167
185
  return attributes;
168
186
  };
169
187
  export class JSONTransformer {
170
- constructor(schema = defaultSchema) {
188
+ /**
189
+ * @param schema The current editor schema
190
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
191
+ * mention node.attr.text values with mapped names.
192
+ */
193
+ constructor(schema = defaultSchema, mentionMap) {
171
194
  this.schema = schema;
195
+ this.mentionMap = mentionMap;
172
196
  }
173
197
  encode(node) {
174
198
  const content = [];
175
199
  node.content.forEach(child => {
176
- content.push(sanitizeNode(toJSON(child)));
200
+ content.push(sanitizeNode(toJSON(child, this.mentionMap)));
177
201
  });
178
202
  if (!content || isEqual(content, emptyDoc.content)) {
179
203
  return createDocFromContent([]);
@@ -200,6 +224,6 @@ export class JSONTransformer {
200
224
  * This method is used to encode a single node
201
225
  */
202
226
  encodeNode(node) {
203
- return sanitizeNode(toJSON(node));
227
+ return sanitizeNode(toJSON(node, this.mentionMap));
204
228
  }
205
229
  }
package/dist/esm/index.js CHANGED
@@ -42,6 +42,7 @@ var isUnsupportedNode = function isUnsupportedNode(node) {
42
42
  };
43
43
  var isDataConsumer = isType('dataConsumer');
44
44
  var isFragmentMark = isType('fragment');
45
+ var isHardBreak = isType('hardBreak');
45
46
 
46
47
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
48
  var filterNull = function filterNull(subject) {
@@ -70,7 +71,7 @@ var emptyDoc = createDocFromContent([{
70
71
  type: 'paragraph',
71
72
  content: []
72
73
  }]);
73
- var toJSON = function toJSON(node) {
74
+ var toJSON = function toJSON(node, mentionMap) {
74
75
  var obj = {
75
76
  type: node.type.name
76
77
  };
@@ -100,17 +101,33 @@ var toJSON = function toJSON(node) {
100
101
  if (obj.attrs) {
101
102
  obj.attrs = filterNull(obj.attrs);
102
103
  }
104
+ if (isMentionNode(node) && mentionMap) {
105
+ var _text, _obj$attrs;
106
+ // If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
107
+ var name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? "@".concat(mentionMap[node.attrs.id]) : '';
108
+ var text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
109
+ obj.attrs = _objectSpread(_objectSpread({}, obj.attrs), {}, {
110
+ // If an explicit text value is set on the mention then that should take priority over the mentionMap value.
111
+ text: !!text ? text : name
112
+ });
113
+ }
103
114
 
104
115
  // Remove the attrs property if it's empty, this is currently limited to paragraph nodes.
105
116
  if (isParagraph(node) && obj.attrs && !Object.keys(obj.attrs).length) {
106
117
  delete obj.attrs;
107
118
  }
119
+
120
+ // Hard break has optional attr.text which was added for PM <-> ADF compatibility.
121
+ // No need to preserve it.
122
+ if (isHardBreak(node)) {
123
+ delete obj.attrs;
124
+ }
108
125
  if (node.isText) {
109
126
  obj.text = node.textContent;
110
127
  } else {
111
128
  node.content.forEach(function (child) {
112
129
  obj.content = obj.content || [];
113
- obj.content.push(toJSON(child));
130
+ obj.content.push(toJSON(child, mentionMap));
114
131
  });
115
132
  }
116
133
  if (isParagraph(node) || isHeading(node)) {
@@ -175,17 +192,25 @@ var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark,
175
192
  return attributes;
176
193
  };
177
194
  export var JSONTransformer = /*#__PURE__*/function () {
195
+ /**
196
+ * @param schema The current editor schema
197
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
198
+ * mention node.attr.text values with mapped names.
199
+ */
178
200
  function JSONTransformer() {
179
201
  var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultSchema;
202
+ var mentionMap = arguments.length > 1 ? arguments[1] : undefined;
180
203
  _classCallCheck(this, JSONTransformer);
181
204
  this.schema = schema;
205
+ this.mentionMap = mentionMap;
182
206
  }
183
207
  _createClass(JSONTransformer, [{
184
208
  key: "encode",
185
209
  value: function encode(node) {
210
+ var _this = this;
186
211
  var content = [];
187
212
  node.content.forEach(function (child) {
188
- content.push(sanitizeNode(toJSON(child)));
213
+ content.push(sanitizeNode(toJSON(child, _this.mentionMap)));
189
214
  });
190
215
  if (!content || isEqual(content, emptyDoc.content)) {
191
216
  return createDocFromContent([]);
@@ -218,7 +243,7 @@ export var JSONTransformer = /*#__PURE__*/function () {
218
243
  }, {
219
244
  key: "encodeNode",
220
245
  value: function encodeNode(node) {
221
- return sanitizeNode(toJSON(node));
246
+ return sanitizeNode(toJSON(node, this.mentionMap));
222
247
  }
223
248
  }]);
224
249
  return JSONTransformer;
@@ -11,7 +11,13 @@ export declare enum SchemaStage {
11
11
  }
12
12
  export declare class JSONTransformer implements Transformer<JSONDocNode> {
13
13
  private schema;
14
- constructor(schema?: Schema);
14
+ private mentionMap;
15
+ /**
16
+ * @param schema The current editor schema
17
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
18
+ * mention node.attr.text values with mapped names.
19
+ */
20
+ constructor(schema?: Schema, mentionMap?: Record<string, string | undefined>);
15
21
  encode(node: PMNode): JSONDocNode;
16
22
  private internalParse;
17
23
  parse(content: JSONDocNode, stage?: SchemaStage): PMNode;
@@ -11,7 +11,13 @@ export declare enum SchemaStage {
11
11
  }
12
12
  export declare class JSONTransformer implements Transformer<JSONDocNode> {
13
13
  private schema;
14
- constructor(schema?: Schema);
14
+ private mentionMap;
15
+ /**
16
+ * @param schema The current editor schema
17
+ * @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
18
+ * mention node.attr.text values with mapped names.
19
+ */
20
+ constructor(schema?: Schema, mentionMap?: Record<string, string | undefined>);
15
21
  encode(node: PMNode): JSONDocNode;
16
22
  private internalParse;
17
23
  parse(content: JSONDocNode, stage?: SchemaStage): PMNode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-json-transformer",
3
- "version": "8.20.2",
3
+ "version": "8.20.4",
4
4
  "description": "JSON transformer",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -36,14 +36,14 @@
36
36
  "./types": "./src/types.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@atlaskit/adf-schema": "^42.3.1",
40
- "@atlaskit/adf-utils": "^19.10.0",
39
+ "@atlaskit/adf-schema": "^44.2.0",
40
+ "@atlaskit/adf-utils": "^19.11.0",
41
41
  "@atlaskit/editor-prosemirror": "6.0.0",
42
42
  "@babel/runtime": "^7.0.0",
43
43
  "lodash": "^4.17.21"
44
44
  },
45
45
  "devDependencies": {
46
- "@atlaskit/editor-plugin-highlight": "1.18.3",
46
+ "@atlaskit/editor-plugin-highlight": "1.18.6",
47
47
  "@atlaskit/platform-feature-flags": "^0.3.0",
48
48
  "react": "^16.8.0",
49
49
  "typescript": "~5.4.2"