@atlaskit/editor-json-transformer 8.19.1 → 8.20.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,14 @@
1
1
  # @atlaskit/editor-json-transformer
2
2
 
3
+ ## 8.20.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#152594](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/152594)
8
+ [`91885511b74c6`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/91885511b74c6) -
9
+ The JSON Transformer now supports being initialised with a schema rather. This is backwards
10
+ compatible and the parse method still supports overriding this based on the set stage
11
+
3
12
  ## 8.19.1
4
13
 
5
14
  ### Patch Changes
package/dist/cjs/index.js CHANGED
@@ -50,6 +50,8 @@ var isUnsupportedNode = function isUnsupportedNode(node) {
50
50
  };
51
51
  var isDataConsumer = isType('dataConsumer');
52
52
  var isFragmentMark = isType('fragment');
53
+
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
55
  var filterNull = function filterNull(subject) {
54
56
  var output = _objectSpread({}, subject);
55
57
  for (var key in output) {
@@ -182,7 +184,9 @@ var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark,
182
184
  };
183
185
  var JSONTransformer = exports.JSONTransformer = /*#__PURE__*/function () {
184
186
  function JSONTransformer() {
187
+ var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _schemaDefault.defaultSchema;
185
188
  (0, _classCallCheck2.default)(this, JSONTransformer);
189
+ this.schema = schema;
186
190
  }
187
191
  (0, _createClass2.default)(JSONTransformer, [{
188
192
  key: "encode",
@@ -198,9 +202,7 @@ var JSONTransformer = exports.JSONTransformer = /*#__PURE__*/function () {
198
202
  }
199
203
  }, {
200
204
  key: "internalParse",
201
- value: function internalParse(content) {
202
- var stage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : SchemaStage.FINAL;
203
- var schema = (0, _schemaDefault.getSchemaBasedOnStage)(stage);
205
+ value: function internalParse(content, schema) {
204
206
  var doc = schema.nodeFromJSON(content);
205
207
  doc.check();
206
208
  return doc;
@@ -211,10 +213,11 @@ var JSONTransformer = exports.JSONTransformer = /*#__PURE__*/function () {
211
213
  if (content.type !== 'doc') {
212
214
  throw new Error('Expected content format to be ADF');
213
215
  }
216
+ var schema = !!stage ? (0, _schemaDefault.getSchemaBasedOnStage)(stage) : this.schema;
214
217
  if (!content.content || content.content.length === 0) {
215
- return this.internalParse(emptyDoc, stage);
218
+ return this.internalParse(emptyDoc, schema);
216
219
  }
217
- return this.internalParse(content, stage);
220
+ return this.internalParse(content, schema);
218
221
  }
219
222
 
220
223
  /**
@@ -1,6 +1,6 @@
1
1
  import isEqual from 'lodash/isEqual';
2
2
  import { codeBlockToJSON, dataConsumerToJSON, expandToJSON, fragmentToJSON, linkToJSON, mediaSingleToJSON, mediaToJSON, mentionToJSON, tableToJSON, toJSONTableCell, toJSONTableHeader } from '@atlaskit/adf-schema';
3
- import { getSchemaBasedOnStage } from '@atlaskit/adf-schema/schema-default';
3
+ import { defaultSchema, getSchemaBasedOnStage } from '@atlaskit/adf-schema/schema-default';
4
4
  import { markOverrideRuleFor } from './markOverrideRules';
5
5
  import { sanitizeNode } from './sanitize/sanitize-node';
6
6
  export let SchemaStage = /*#__PURE__*/function (SchemaStage) {
@@ -27,6 +27,8 @@ 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
+
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
32
  const filterNull = subject => {
31
33
  let output = {
32
34
  ...subject
@@ -165,6 +167,9 @@ const getUnwrappedNodeAttributes = (node, mark, obj) => {
165
167
  return attributes;
166
168
  };
167
169
  export class JSONTransformer {
170
+ constructor(schema = defaultSchema) {
171
+ this.schema = schema;
172
+ }
168
173
  encode(node) {
169
174
  const content = [];
170
175
  node.content.forEach(child => {
@@ -175,8 +180,7 @@ export class JSONTransformer {
175
180
  }
176
181
  return createDocFromContent(content);
177
182
  }
178
- internalParse(content, stage = SchemaStage.FINAL) {
179
- const schema = getSchemaBasedOnStage(stage);
183
+ internalParse(content, schema) {
180
184
  const doc = schema.nodeFromJSON(content);
181
185
  doc.check();
182
186
  return doc;
@@ -185,10 +189,11 @@ export class JSONTransformer {
185
189
  if (content.type !== 'doc') {
186
190
  throw new Error('Expected content format to be ADF');
187
191
  }
192
+ const schema = !!stage ? getSchemaBasedOnStage(stage) : this.schema;
188
193
  if (!content.content || content.content.length === 0) {
189
- return this.internalParse(emptyDoc, stage);
194
+ return this.internalParse(emptyDoc, schema);
190
195
  }
191
- return this.internalParse(content, stage);
196
+ return this.internalParse(content, schema);
192
197
  }
193
198
 
194
199
  /**
package/dist/esm/index.js CHANGED
@@ -9,7 +9,7 @@ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbol
9
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
10
  import isEqual from 'lodash/isEqual';
11
11
  import { codeBlockToJSON, dataConsumerToJSON, expandToJSON, fragmentToJSON, linkToJSON, mediaSingleToJSON, mediaToJSON, mentionToJSON, tableToJSON, toJSONTableCell, toJSONTableHeader } from '@atlaskit/adf-schema';
12
- import { getSchemaBasedOnStage } from '@atlaskit/adf-schema/schema-default';
12
+ import { defaultSchema, getSchemaBasedOnStage } from '@atlaskit/adf-schema/schema-default';
13
13
  import { markOverrideRuleFor } from './markOverrideRules';
14
14
  import { sanitizeNode } from './sanitize/sanitize-node';
15
15
  export var SchemaStage = /*#__PURE__*/function (SchemaStage) {
@@ -42,6 +42,8 @@ var isUnsupportedNode = function isUnsupportedNode(node) {
42
42
  };
43
43
  var isDataConsumer = isType('dataConsumer');
44
44
  var isFragmentMark = isType('fragment');
45
+
46
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
47
  var filterNull = function filterNull(subject) {
46
48
  var output = _objectSpread({}, subject);
47
49
  for (var key in output) {
@@ -174,7 +176,9 @@ var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark,
174
176
  };
175
177
  export var JSONTransformer = /*#__PURE__*/function () {
176
178
  function JSONTransformer() {
179
+ var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultSchema;
177
180
  _classCallCheck(this, JSONTransformer);
181
+ this.schema = schema;
178
182
  }
179
183
  _createClass(JSONTransformer, [{
180
184
  key: "encode",
@@ -190,9 +194,7 @@ export var JSONTransformer = /*#__PURE__*/function () {
190
194
  }
191
195
  }, {
192
196
  key: "internalParse",
193
- value: function internalParse(content) {
194
- var stage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : SchemaStage.FINAL;
195
- var schema = getSchemaBasedOnStage(stage);
197
+ value: function internalParse(content, schema) {
196
198
  var doc = schema.nodeFromJSON(content);
197
199
  doc.check();
198
200
  return doc;
@@ -203,10 +205,11 @@ export var JSONTransformer = /*#__PURE__*/function () {
203
205
  if (content.type !== 'doc') {
204
206
  throw new Error('Expected content format to be ADF');
205
207
  }
208
+ var schema = !!stage ? getSchemaBasedOnStage(stage) : this.schema;
206
209
  if (!content.content || content.content.length === 0) {
207
- return this.internalParse(emptyDoc, stage);
210
+ return this.internalParse(emptyDoc, schema);
208
211
  }
209
- return this.internalParse(content, stage);
212
+ return this.internalParse(content, schema);
210
213
  }
211
214
 
212
215
  /**
@@ -1,4 +1,4 @@
1
- import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
1
+ import type { Node as PMNode, Schema } from '@atlaskit/editor-prosemirror/model';
2
2
  import type { JSONDocNode, JSONNode } from './types';
3
3
  export type { JSONDocNode, JSONNode } from './types';
4
4
  interface Transformer<T> {
@@ -10,6 +10,8 @@ export declare enum SchemaStage {
10
10
  STAGE_0 = "stage0"
11
11
  }
12
12
  export declare class JSONTransformer implements Transformer<JSONDocNode> {
13
+ private schema;
14
+ constructor(schema?: Schema);
13
15
  encode(node: PMNode): JSONDocNode;
14
16
  private internalParse;
15
17
  parse(content: JSONDocNode, stage?: SchemaStage): PMNode;
@@ -1,4 +1,4 @@
1
- import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
1
+ import type { Node as PMNode, Schema } from '@atlaskit/editor-prosemirror/model';
2
2
  import type { JSONDocNode, JSONNode } from './types';
3
3
  export type { JSONDocNode, JSONNode } from './types';
4
4
  interface Transformer<T> {
@@ -10,6 +10,8 @@ export declare enum SchemaStage {
10
10
  STAGE_0 = "stage0"
11
11
  }
12
12
  export declare class JSONTransformer implements Transformer<JSONDocNode> {
13
+ private schema;
14
+ constructor(schema?: Schema);
13
15
  encode(node: PMNode): JSONDocNode;
14
16
  private internalParse;
15
17
  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.19.1",
3
+ "version": "8.20.0",
4
4
  "description": "JSON transformer",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"