@atlaskit/adf-schema 40.8.1 → 40.9.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,17 @@
1
1
  # @atlaskit/adf-schema
2
2
 
3
+ ## 40.9.0
4
+
5
+ ### Minor Changes
6
+
7
+ - dcc54e6: [ED-24636] Implements BatchAttrsStep
8
+
9
+ ## 40.8.2
10
+
11
+ ### Patch Changes
12
+
13
+ - fde250b: Unskip parsing rules to parse content of expand and nestedExpand.
14
+
3
15
  ## 40.8.1
4
16
 
5
17
  ### Patch Changes
@@ -36,11 +36,11 @@ var nodeSpecOptions = {
36
36
  }, {
37
37
  context: 'expand//',
38
38
  tag: '[data-node-type="expand"]',
39
- skip: true
39
+ getAttrs: getExpandAttrs
40
40
  }, {
41
41
  context: 'nestedExpand//',
42
42
  tag: '[data-node-type="expand"]',
43
- skip: true
43
+ getAttrs: getExpandAttrs
44
44
  }, {
45
45
  tag: '[data-node-type="nestedExpand"] button',
46
46
  ignore: true
@@ -19,6 +19,14 @@ var _nodeTypes = require("../../next-schema/generated/nodeTypes");
19
19
  * @name nestedExpand_with_no_marks_node
20
20
  */
21
21
 
22
+ function getExpandAttrs(domNode) {
23
+ var dom = domNode;
24
+ return {
25
+ title: dom.getAttribute('data-title'),
26
+ __expanded: true
27
+ };
28
+ }
29
+
22
30
  /**
23
31
  * @name nestedExpand
24
32
  * @description an expand that can be nested (eg. inside table, layout).
@@ -27,7 +35,7 @@ var nestedExpand = exports.nestedExpand = (0, _nodeTypes.nestedExpand)({
27
35
  parseDOM: [{
28
36
  context: 'nestedExpand//',
29
37
  tag: '[data-node-type="nestedExpand"]',
30
- skip: true
38
+ getAttrs: getExpandAttrs
31
39
  }, {
32
40
  tag: '[data-node-type="nestedExpand"] button',
33
41
  ignore: true
@@ -36,13 +44,7 @@ var nestedExpand = exports.nestedExpand = (0, _nodeTypes.nestedExpand)({
36
44
  ignore: true
37
45
  }, {
38
46
  tag: 'div[data-node-type="nestedExpand"]',
39
- getAttrs: function getAttrs(domNode) {
40
- var dom = domNode;
41
- return {
42
- title: dom.getAttribute('data-title'),
43
- __expanded: true
44
- };
45
- }
47
+ getAttrs: getExpandAttrs
46
48
  }],
47
49
  toDOM: function toDOM(node) {
48
50
  var attrs = {
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.BatchAttrsStep = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
12
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
13
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
14
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
15
+ var _model = require("@atlaskit/editor-prosemirror/model");
16
+ var _transform = require("@atlaskit/editor-prosemirror/transform");
17
+ 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; }
18
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
+ function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
20
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
21
+ var stepType = 'batchAttrs';
22
+ var isValidData = function isValidData(data) {
23
+ if (data !== null && !Array.isArray(data)) {
24
+ return false;
25
+ }
26
+ return !data.some(function (d) {
27
+ var invalidPosition = typeof d.position !== 'number';
28
+ var invalidNodeType = typeof d.nodeType !== 'string';
29
+ var invalidAttrs = (0, _typeof2.default)(d.attrs) !== 'object';
30
+ return invalidPosition || invalidNodeType || invalidAttrs;
31
+ });
32
+ };
33
+
34
+ /**
35
+ * 📢 Public API: Editor FE Platform
36
+ *
37
+ * Represents a step that applies a batch of attribute changes to nodes in a ProseMirror document.
38
+ *
39
+ * This step is particularly useful when you need to update the attributes of multiple nodes in a document
40
+ * in a single operation. For example, you might want to change the color of several panels or update metadata
41
+ * for various sections without needing to perform multiple separate operations.
42
+ *
43
+ * **Use Cases:**
44
+ * - **Efficiency**: Apply multiple attribute changes in a single step to reduce the number of operations.
45
+ * - **Atomicity**: Ensure that a group of attribute changes are applied together, maintaining document consistency.
46
+ * - **Consistency**: Use when changes are logically related, such as updating theme attributes for a document section.
47
+ *
48
+ * **When Not to Use:**
49
+ * - **Single Changes**: If you only need to change attributes on a single node, a more straightforward step might be suitable like `AttrsStep` from prosemirror.
50
+ * - **Complex Node Transformations**: This step is designed for attribute changes rather than structural changes.
51
+ * - **Performance Concerns**: While efficient for batch updates, unnecessary use for single updates may add overhead.
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * import { BatchAttrsStep } from '@atlaskit/adf-schema/steps';
56
+ *
57
+ * // Define the attribute changes
58
+ * const changes = [
59
+ * {
60
+ * position: 0, // Position of the first panel
61
+ * nodeType: 'panel',
62
+ * attrs: { panelType: 'error' }
63
+ * },
64
+ * {
65
+ * position: 7, // Position of the second panel
66
+ * nodeType: 'panel',
67
+ * attrs: { panelType: 'success' }
68
+ * }
69
+ * ];
70
+ *
71
+ * // Create the step and apply it to the document
72
+ * const step = new BatchAttrsStep(changes);
73
+ *
74
+ * const tr = editorState.tr;
75
+ *
76
+ * tr.step(step);
77
+ * ```
78
+ *
79
+ * @class
80
+ * @extends {Step}
81
+ */
82
+ var BatchAttrsStep = exports.BatchAttrsStep = /*#__PURE__*/function (_Step) {
83
+ function BatchAttrsStep(data) {
84
+ var _this;
85
+ var inverted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
86
+ (0, _classCallCheck2.default)(this, BatchAttrsStep);
87
+ _this = _callSuper(this, BatchAttrsStep);
88
+ _this.data = data;
89
+ _this.inverted = inverted;
90
+ return _this;
91
+ }
92
+ (0, _inherits2.default)(BatchAttrsStep, _Step);
93
+ return (0, _createClass2.default)(BatchAttrsStep, [{
94
+ key: "apply",
95
+ value: function apply(doc) {
96
+ var resultDoc = this.data.reduce(function (acc, value) {
97
+ var _acc$doc;
98
+ if (!acc.doc || acc.failed) {
99
+ return acc;
100
+ }
101
+ var position = value.position,
102
+ attrs = value.attrs;
103
+ if (acc.doc && position > acc.doc.nodeSize || position < 0) {
104
+ return _transform.StepResult.fail("Position ".concat(position, " out of document range."));
105
+ }
106
+ var target = (_acc$doc = acc.doc) === null || _acc$doc === void 0 ? void 0 : _acc$doc.nodeAt(position);
107
+ if (!target) {
108
+ return _transform.StepResult.fail("No node at given position: ".concat(position));
109
+ }
110
+ if (target.isText) {
111
+ return _transform.StepResult.fail('Target is a text node. Attributes are not allowed.');
112
+ }
113
+ var mergedAttrs = _objectSpread(_objectSpread({}, target.attrs || {}), attrs || {});
114
+ var updatedNode = target.type.create(mergedAttrs, null, target.marks);
115
+ var slice = new _model.Slice(_model.Fragment.from(updatedNode), 0, target.isLeaf ? 0 : 1);
116
+ return _transform.StepResult.fromReplace(acc.doc, position, position + 1, slice);
117
+ }, _transform.StepResult.ok(doc));
118
+ return resultDoc;
119
+ }
120
+ }, {
121
+ key: "invert",
122
+ value: function invert(doc) {
123
+ var previousData = this.data.reduce(function (acc, value) {
124
+ var position = value.position,
125
+ nodeType = value.nodeType,
126
+ nextAttrs = value.attrs;
127
+ if (position > doc.nodeSize) {
128
+ return acc;
129
+ }
130
+ var target = doc.nodeAt(position);
131
+ if (!target) {
132
+ return acc;
133
+ }
134
+ if (target.isText) {
135
+ return acc;
136
+ }
137
+ var previousAttrs = Object.keys(nextAttrs).reduce(function (attributesIterator, key) {
138
+ var oldValue = target.attrs[key];
139
+ attributesIterator[key] = oldValue;
140
+ return attributesIterator;
141
+ }, {});
142
+ var prev = {
143
+ position: position,
144
+ nodeType: nodeType,
145
+ attrs: previousAttrs
146
+ };
147
+ acc.push(prev);
148
+ return acc;
149
+ }, []);
150
+ return new BatchAttrsStep(previousData, true);
151
+ }
152
+ }, {
153
+ key: "map",
154
+ value: function map(mapping) {
155
+ var mappedData = this.data.reduce(function (acc, value) {
156
+ var position = value.position;
157
+ var mappedPosition = mapping.mapResult(position);
158
+ if (mappedPosition.deleted) {
159
+ return acc;
160
+ }
161
+ acc.push(_objectSpread(_objectSpread({}, value), {}, {
162
+ position: mappedPosition.pos
163
+ }));
164
+ return acc;
165
+ }, []);
166
+ if (mappedData.length === 0) {
167
+ return null;
168
+ }
169
+ return new BatchAttrsStep(mappedData, this.inverted);
170
+ }
171
+ }, {
172
+ key: "toJSON",
173
+ value: function toJSON() {
174
+ return {
175
+ stepType: stepType,
176
+ data: this.data,
177
+ inverted: this.inverted
178
+ };
179
+ }
180
+ }], [{
181
+ key: "fromJSON",
182
+ value: function fromJSON(_schema, json) {
183
+ var data = json === null || json === void 0 ? void 0 : json.data;
184
+ if (!isValidData(data)) {
185
+ throw new Error('Invalid input for BatchAttrsStep.fromJSON');
186
+ }
187
+ return new BatchAttrsStep(data, Boolean(json.inverted));
188
+ }
189
+ }]);
190
+ }(_transform.Step);
191
+ _transform.Step.jsonID(stepType, BatchAttrsStep);
package/dist/cjs/steps.js CHANGED
@@ -9,6 +9,12 @@ Object.defineProperty(exports, "AnalyticsStep", {
9
9
  return _analytics.AnalyticsStep;
10
10
  }
11
11
  });
12
+ Object.defineProperty(exports, "BatchAttrsStep", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _batchAttrsStep.BatchAttrsStep;
16
+ }
17
+ });
12
18
  Object.defineProperty(exports, "InsertTypeAheadStages", {
13
19
  enumerable: true,
14
20
  get: function get() {
@@ -43,4 +49,5 @@ var _typeAhead = require("./steps/type-ahead");
43
49
  var _setAttrs = require("./steps/set-attrs");
44
50
  var _analytics = require("./steps/analytics");
45
51
  var _linkMetaStep = require("./steps/link-meta-step");
46
- var _overrideDocumentStep = require("./steps/override-document-step");
52
+ var _overrideDocumentStep = require("./steps/override-document-step");
53
+ var _batchAttrsStep = require("./steps/batch-attrs-step");
@@ -27,11 +27,11 @@ const nodeSpecOptions = {
27
27
  }, {
28
28
  context: 'expand//',
29
29
  tag: '[data-node-type="expand"]',
30
- skip: true
30
+ getAttrs: getExpandAttrs
31
31
  }, {
32
32
  context: 'nestedExpand//',
33
33
  tag: '[data-node-type="expand"]',
34
- skip: true
34
+ getAttrs: getExpandAttrs
35
35
  }, {
36
36
  tag: '[data-node-type="nestedExpand"] button',
37
37
  ignore: true
@@ -14,6 +14,14 @@ import { nestedExpand as nestedExpandFactory } from '../../next-schema/generated
14
14
  * @name nestedExpand_with_no_marks_node
15
15
  */
16
16
 
17
+ function getExpandAttrs(domNode) {
18
+ const dom = domNode;
19
+ return {
20
+ title: dom.getAttribute('data-title'),
21
+ __expanded: true
22
+ };
23
+ }
24
+
17
25
  /**
18
26
  * @name nestedExpand
19
27
  * @description an expand that can be nested (eg. inside table, layout).
@@ -22,7 +30,7 @@ export const nestedExpand = nestedExpandFactory({
22
30
  parseDOM: [{
23
31
  context: 'nestedExpand//',
24
32
  tag: '[data-node-type="nestedExpand"]',
25
- skip: true
33
+ getAttrs: getExpandAttrs
26
34
  }, {
27
35
  tag: '[data-node-type="nestedExpand"] button',
28
36
  ignore: true
@@ -31,13 +39,7 @@ export const nestedExpand = nestedExpandFactory({
31
39
  ignore: true
32
40
  }, {
33
41
  tag: 'div[data-node-type="nestedExpand"]',
34
- getAttrs: domNode => {
35
- const dom = domNode;
36
- return {
37
- title: dom.getAttribute('data-title'),
38
- __expanded: true
39
- };
40
- }
42
+ getAttrs: getExpandAttrs
41
43
  }],
42
44
  toDOM(node) {
43
45
  const attrs = {
@@ -0,0 +1,167 @@
1
+ import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
2
+ import { Step, StepResult } from '@atlaskit/editor-prosemirror/transform';
3
+ const stepType = 'batchAttrs';
4
+ const isValidData = data => {
5
+ if (data !== null && !Array.isArray(data)) {
6
+ return false;
7
+ }
8
+ return !data.some(d => {
9
+ const invalidPosition = typeof d.position !== 'number';
10
+ const invalidNodeType = typeof d.nodeType !== 'string';
11
+ const invalidAttrs = typeof d.attrs !== 'object';
12
+ return invalidPosition || invalidNodeType || invalidAttrs;
13
+ });
14
+ };
15
+
16
+ /**
17
+ * 📢 Public API: Editor FE Platform
18
+ *
19
+ * Represents a step that applies a batch of attribute changes to nodes in a ProseMirror document.
20
+ *
21
+ * This step is particularly useful when you need to update the attributes of multiple nodes in a document
22
+ * in a single operation. For example, you might want to change the color of several panels or update metadata
23
+ * for various sections without needing to perform multiple separate operations.
24
+ *
25
+ * **Use Cases:**
26
+ * - **Efficiency**: Apply multiple attribute changes in a single step to reduce the number of operations.
27
+ * - **Atomicity**: Ensure that a group of attribute changes are applied together, maintaining document consistency.
28
+ * - **Consistency**: Use when changes are logically related, such as updating theme attributes for a document section.
29
+ *
30
+ * **When Not to Use:**
31
+ * - **Single Changes**: If you only need to change attributes on a single node, a more straightforward step might be suitable like `AttrsStep` from prosemirror.
32
+ * - **Complex Node Transformations**: This step is designed for attribute changes rather than structural changes.
33
+ * - **Performance Concerns**: While efficient for batch updates, unnecessary use for single updates may add overhead.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * import { BatchAttrsStep } from '@atlaskit/adf-schema/steps';
38
+ *
39
+ * // Define the attribute changes
40
+ * const changes = [
41
+ * {
42
+ * position: 0, // Position of the first panel
43
+ * nodeType: 'panel',
44
+ * attrs: { panelType: 'error' }
45
+ * },
46
+ * {
47
+ * position: 7, // Position of the second panel
48
+ * nodeType: 'panel',
49
+ * attrs: { panelType: 'success' }
50
+ * }
51
+ * ];
52
+ *
53
+ * // Create the step and apply it to the document
54
+ * const step = new BatchAttrsStep(changes);
55
+ *
56
+ * const tr = editorState.tr;
57
+ *
58
+ * tr.step(step);
59
+ * ```
60
+ *
61
+ * @class
62
+ * @extends {Step}
63
+ */
64
+ export class BatchAttrsStep extends Step {
65
+ constructor(data, inverted = false) {
66
+ super();
67
+ this.data = data;
68
+ this.inverted = inverted;
69
+ }
70
+ apply(doc) {
71
+ const resultDoc = this.data.reduce((acc, value) => {
72
+ var _acc$doc;
73
+ if (!acc.doc || acc.failed) {
74
+ return acc;
75
+ }
76
+ const {
77
+ position,
78
+ attrs
79
+ } = value;
80
+ if (acc.doc && position > acc.doc.nodeSize || position < 0) {
81
+ return StepResult.fail(`Position ${position} out of document range.`);
82
+ }
83
+ const target = (_acc$doc = acc.doc) === null || _acc$doc === void 0 ? void 0 : _acc$doc.nodeAt(position);
84
+ if (!target) {
85
+ return StepResult.fail(`No node at given position: ${position}`);
86
+ }
87
+ if (target.isText) {
88
+ return StepResult.fail('Target is a text node. Attributes are not allowed.');
89
+ }
90
+ const mergedAttrs = {
91
+ ...(target.attrs || {}),
92
+ ...(attrs || {})
93
+ };
94
+ const updatedNode = target.type.create(mergedAttrs, null, target.marks);
95
+ const slice = new Slice(Fragment.from(updatedNode), 0, target.isLeaf ? 0 : 1);
96
+ return StepResult.fromReplace(acc.doc, position, position + 1, slice);
97
+ }, StepResult.ok(doc));
98
+ return resultDoc;
99
+ }
100
+ invert(doc) {
101
+ const previousData = this.data.reduce((acc, value) => {
102
+ const {
103
+ position,
104
+ nodeType,
105
+ attrs: nextAttrs
106
+ } = value;
107
+ if (position > doc.nodeSize) {
108
+ return acc;
109
+ }
110
+ const target = doc.nodeAt(position);
111
+ if (!target) {
112
+ return acc;
113
+ }
114
+ if (target.isText) {
115
+ return acc;
116
+ }
117
+ const previousAttrs = Object.keys(nextAttrs).reduce((attributesIterator, key) => {
118
+ const oldValue = target.attrs[key];
119
+ attributesIterator[key] = oldValue;
120
+ return attributesIterator;
121
+ }, {});
122
+ const prev = {
123
+ position,
124
+ nodeType,
125
+ attrs: previousAttrs
126
+ };
127
+ acc.push(prev);
128
+ return acc;
129
+ }, []);
130
+ return new BatchAttrsStep(previousData, true);
131
+ }
132
+ map(mapping) {
133
+ const mappedData = this.data.reduce((acc, value) => {
134
+ const {
135
+ position
136
+ } = value;
137
+ const mappedPosition = mapping.mapResult(position);
138
+ if (mappedPosition.deleted) {
139
+ return acc;
140
+ }
141
+ acc.push({
142
+ ...value,
143
+ position: mappedPosition.pos
144
+ });
145
+ return acc;
146
+ }, []);
147
+ if (mappedData.length === 0) {
148
+ return null;
149
+ }
150
+ return new BatchAttrsStep(mappedData, this.inverted);
151
+ }
152
+ toJSON() {
153
+ return {
154
+ stepType,
155
+ data: this.data,
156
+ inverted: this.inverted
157
+ };
158
+ }
159
+ static fromJSON(_schema, json) {
160
+ const data = json === null || json === void 0 ? void 0 : json.data;
161
+ if (!isValidData(data)) {
162
+ throw new Error('Invalid input for BatchAttrsStep.fromJSON');
163
+ }
164
+ return new BatchAttrsStep(data, Boolean(json.inverted));
165
+ }
166
+ }
167
+ Step.jsonID(stepType, BatchAttrsStep);
@@ -2,4 +2,5 @@ export { InsertTypeAheadStages, InsertTypeAheadStep } from './steps/type-ahead';
2
2
  export { SetAttrsStep } from './steps/set-attrs';
3
3
  export { AnalyticsStep } from './steps/analytics';
4
4
  export { LinkMetaStep } from './steps/link-meta-step';
5
- export { OverrideDocumentStep } from './steps/override-document-step';
5
+ export { OverrideDocumentStep } from './steps/override-document-step';
6
+ export { BatchAttrsStep } from './steps/batch-attrs-step';
@@ -30,11 +30,11 @@ var nodeSpecOptions = {
30
30
  }, {
31
31
  context: 'expand//',
32
32
  tag: '[data-node-type="expand"]',
33
- skip: true
33
+ getAttrs: getExpandAttrs
34
34
  }, {
35
35
  context: 'nestedExpand//',
36
36
  tag: '[data-node-type="expand"]',
37
- skip: true
37
+ getAttrs: getExpandAttrs
38
38
  }, {
39
39
  tag: '[data-node-type="nestedExpand"] button',
40
40
  ignore: true
@@ -14,6 +14,14 @@ import { nestedExpand as nestedExpandFactory } from '../../next-schema/generated
14
14
  * @name nestedExpand_with_no_marks_node
15
15
  */
16
16
 
17
+ function getExpandAttrs(domNode) {
18
+ var dom = domNode;
19
+ return {
20
+ title: dom.getAttribute('data-title'),
21
+ __expanded: true
22
+ };
23
+ }
24
+
17
25
  /**
18
26
  * @name nestedExpand
19
27
  * @description an expand that can be nested (eg. inside table, layout).
@@ -22,7 +30,7 @@ export var nestedExpand = nestedExpandFactory({
22
30
  parseDOM: [{
23
31
  context: 'nestedExpand//',
24
32
  tag: '[data-node-type="nestedExpand"]',
25
- skip: true
33
+ getAttrs: getExpandAttrs
26
34
  }, {
27
35
  tag: '[data-node-type="nestedExpand"] button',
28
36
  ignore: true
@@ -31,13 +39,7 @@ export var nestedExpand = nestedExpandFactory({
31
39
  ignore: true
32
40
  }, {
33
41
  tag: 'div[data-node-type="nestedExpand"]',
34
- getAttrs: function getAttrs(domNode) {
35
- var dom = domNode;
36
- return {
37
- title: dom.getAttribute('data-title'),
38
- __expanded: true
39
- };
40
- }
42
+ getAttrs: getExpandAttrs
41
43
  }],
42
44
  toDOM: function toDOM(node) {
43
45
  var attrs = {
@@ -0,0 +1,184 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
5
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
6
+ import _inherits from "@babel/runtime/helpers/inherits";
7
+ import _typeof from "@babel/runtime/helpers/typeof";
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
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
11
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
12
+ import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
13
+ import { Step, StepResult } from '@atlaskit/editor-prosemirror/transform';
14
+ var stepType = 'batchAttrs';
15
+ var isValidData = function isValidData(data) {
16
+ if (data !== null && !Array.isArray(data)) {
17
+ return false;
18
+ }
19
+ return !data.some(function (d) {
20
+ var invalidPosition = typeof d.position !== 'number';
21
+ var invalidNodeType = typeof d.nodeType !== 'string';
22
+ var invalidAttrs = _typeof(d.attrs) !== 'object';
23
+ return invalidPosition || invalidNodeType || invalidAttrs;
24
+ });
25
+ };
26
+
27
+ /**
28
+ * 📢 Public API: Editor FE Platform
29
+ *
30
+ * Represents a step that applies a batch of attribute changes to nodes in a ProseMirror document.
31
+ *
32
+ * This step is particularly useful when you need to update the attributes of multiple nodes in a document
33
+ * in a single operation. For example, you might want to change the color of several panels or update metadata
34
+ * for various sections without needing to perform multiple separate operations.
35
+ *
36
+ * **Use Cases:**
37
+ * - **Efficiency**: Apply multiple attribute changes in a single step to reduce the number of operations.
38
+ * - **Atomicity**: Ensure that a group of attribute changes are applied together, maintaining document consistency.
39
+ * - **Consistency**: Use when changes are logically related, such as updating theme attributes for a document section.
40
+ *
41
+ * **When Not to Use:**
42
+ * - **Single Changes**: If you only need to change attributes on a single node, a more straightforward step might be suitable like `AttrsStep` from prosemirror.
43
+ * - **Complex Node Transformations**: This step is designed for attribute changes rather than structural changes.
44
+ * - **Performance Concerns**: While efficient for batch updates, unnecessary use for single updates may add overhead.
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * import { BatchAttrsStep } from '@atlaskit/adf-schema/steps';
49
+ *
50
+ * // Define the attribute changes
51
+ * const changes = [
52
+ * {
53
+ * position: 0, // Position of the first panel
54
+ * nodeType: 'panel',
55
+ * attrs: { panelType: 'error' }
56
+ * },
57
+ * {
58
+ * position: 7, // Position of the second panel
59
+ * nodeType: 'panel',
60
+ * attrs: { panelType: 'success' }
61
+ * }
62
+ * ];
63
+ *
64
+ * // Create the step and apply it to the document
65
+ * const step = new BatchAttrsStep(changes);
66
+ *
67
+ * const tr = editorState.tr;
68
+ *
69
+ * tr.step(step);
70
+ * ```
71
+ *
72
+ * @class
73
+ * @extends {Step}
74
+ */
75
+ export var BatchAttrsStep = /*#__PURE__*/function (_Step) {
76
+ function BatchAttrsStep(data) {
77
+ var _this;
78
+ var inverted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
79
+ _classCallCheck(this, BatchAttrsStep);
80
+ _this = _callSuper(this, BatchAttrsStep);
81
+ _this.data = data;
82
+ _this.inverted = inverted;
83
+ return _this;
84
+ }
85
+ _inherits(BatchAttrsStep, _Step);
86
+ return _createClass(BatchAttrsStep, [{
87
+ key: "apply",
88
+ value: function apply(doc) {
89
+ var resultDoc = this.data.reduce(function (acc, value) {
90
+ var _acc$doc;
91
+ if (!acc.doc || acc.failed) {
92
+ return acc;
93
+ }
94
+ var position = value.position,
95
+ attrs = value.attrs;
96
+ if (acc.doc && position > acc.doc.nodeSize || position < 0) {
97
+ return StepResult.fail("Position ".concat(position, " out of document range."));
98
+ }
99
+ var target = (_acc$doc = acc.doc) === null || _acc$doc === void 0 ? void 0 : _acc$doc.nodeAt(position);
100
+ if (!target) {
101
+ return StepResult.fail("No node at given position: ".concat(position));
102
+ }
103
+ if (target.isText) {
104
+ return StepResult.fail('Target is a text node. Attributes are not allowed.');
105
+ }
106
+ var mergedAttrs = _objectSpread(_objectSpread({}, target.attrs || {}), attrs || {});
107
+ var updatedNode = target.type.create(mergedAttrs, null, target.marks);
108
+ var slice = new Slice(Fragment.from(updatedNode), 0, target.isLeaf ? 0 : 1);
109
+ return StepResult.fromReplace(acc.doc, position, position + 1, slice);
110
+ }, StepResult.ok(doc));
111
+ return resultDoc;
112
+ }
113
+ }, {
114
+ key: "invert",
115
+ value: function invert(doc) {
116
+ var previousData = this.data.reduce(function (acc, value) {
117
+ var position = value.position,
118
+ nodeType = value.nodeType,
119
+ nextAttrs = value.attrs;
120
+ if (position > doc.nodeSize) {
121
+ return acc;
122
+ }
123
+ var target = doc.nodeAt(position);
124
+ if (!target) {
125
+ return acc;
126
+ }
127
+ if (target.isText) {
128
+ return acc;
129
+ }
130
+ var previousAttrs = Object.keys(nextAttrs).reduce(function (attributesIterator, key) {
131
+ var oldValue = target.attrs[key];
132
+ attributesIterator[key] = oldValue;
133
+ return attributesIterator;
134
+ }, {});
135
+ var prev = {
136
+ position: position,
137
+ nodeType: nodeType,
138
+ attrs: previousAttrs
139
+ };
140
+ acc.push(prev);
141
+ return acc;
142
+ }, []);
143
+ return new BatchAttrsStep(previousData, true);
144
+ }
145
+ }, {
146
+ key: "map",
147
+ value: function map(mapping) {
148
+ var mappedData = this.data.reduce(function (acc, value) {
149
+ var position = value.position;
150
+ var mappedPosition = mapping.mapResult(position);
151
+ if (mappedPosition.deleted) {
152
+ return acc;
153
+ }
154
+ acc.push(_objectSpread(_objectSpread({}, value), {}, {
155
+ position: mappedPosition.pos
156
+ }));
157
+ return acc;
158
+ }, []);
159
+ if (mappedData.length === 0) {
160
+ return null;
161
+ }
162
+ return new BatchAttrsStep(mappedData, this.inverted);
163
+ }
164
+ }, {
165
+ key: "toJSON",
166
+ value: function toJSON() {
167
+ return {
168
+ stepType: stepType,
169
+ data: this.data,
170
+ inverted: this.inverted
171
+ };
172
+ }
173
+ }], [{
174
+ key: "fromJSON",
175
+ value: function fromJSON(_schema, json) {
176
+ var data = json === null || json === void 0 ? void 0 : json.data;
177
+ if (!isValidData(data)) {
178
+ throw new Error('Invalid input for BatchAttrsStep.fromJSON');
179
+ }
180
+ return new BatchAttrsStep(data, Boolean(json.inverted));
181
+ }
182
+ }]);
183
+ }(Step);
184
+ Step.jsonID(stepType, BatchAttrsStep);
package/dist/esm/steps.js CHANGED
@@ -2,4 +2,5 @@ export { InsertTypeAheadStages, InsertTypeAheadStep } from './steps/type-ahead';
2
2
  export { SetAttrsStep } from './steps/set-attrs';
3
3
  export { AnalyticsStep } from './steps/analytics';
4
4
  export { LinkMetaStep } from './steps/link-meta-step';
5
- export { OverrideDocumentStep } from './steps/override-document-step';
5
+ export { OverrideDocumentStep } from './steps/override-document-step';
6
+ export { BatchAttrsStep } from './steps/batch-attrs-step';
@@ -0,0 +1,74 @@
1
+ import { type Schema } from '@atlaskit/editor-prosemirror/model';
2
+ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
+ import { Step, StepResult } from '@atlaskit/editor-prosemirror/transform';
4
+ import type { Mappable } from '@atlaskit/editor-prosemirror/transform';
5
+ export type BatchAttrsStepData = {
6
+ position: number;
7
+ nodeType: string;
8
+ attrs: Record<string, unknown>;
9
+ };
10
+ /**
11
+ * 📢 Public API: Editor FE Platform
12
+ *
13
+ * Represents a step that applies a batch of attribute changes to nodes in a ProseMirror document.
14
+ *
15
+ * This step is particularly useful when you need to update the attributes of multiple nodes in a document
16
+ * in a single operation. For example, you might want to change the color of several panels or update metadata
17
+ * for various sections without needing to perform multiple separate operations.
18
+ *
19
+ * **Use Cases:**
20
+ * - **Efficiency**: Apply multiple attribute changes in a single step to reduce the number of operations.
21
+ * - **Atomicity**: Ensure that a group of attribute changes are applied together, maintaining document consistency.
22
+ * - **Consistency**: Use when changes are logically related, such as updating theme attributes for a document section.
23
+ *
24
+ * **When Not to Use:**
25
+ * - **Single Changes**: If you only need to change attributes on a single node, a more straightforward step might be suitable like `AttrsStep` from prosemirror.
26
+ * - **Complex Node Transformations**: This step is designed for attribute changes rather than structural changes.
27
+ * - **Performance Concerns**: While efficient for batch updates, unnecessary use for single updates may add overhead.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * import { BatchAttrsStep } from '@atlaskit/adf-schema/steps';
32
+ *
33
+ * // Define the attribute changes
34
+ * const changes = [
35
+ * {
36
+ * position: 0, // Position of the first panel
37
+ * nodeType: 'panel',
38
+ * attrs: { panelType: 'error' }
39
+ * },
40
+ * {
41
+ * position: 7, // Position of the second panel
42
+ * nodeType: 'panel',
43
+ * attrs: { panelType: 'success' }
44
+ * }
45
+ * ];
46
+ *
47
+ * // Create the step and apply it to the document
48
+ * const step = new BatchAttrsStep(changes);
49
+ *
50
+ * const tr = editorState.tr;
51
+ *
52
+ * tr.step(step);
53
+ * ```
54
+ *
55
+ * @class
56
+ * @extends {Step}
57
+ */
58
+ export declare class BatchAttrsStep extends Step {
59
+ data: Array<BatchAttrsStepData>;
60
+ private inverted;
61
+ constructor(data: Array<BatchAttrsStepData>, inverted?: boolean);
62
+ apply(doc: PMNode): StepResult;
63
+ invert(doc: PMNode): BatchAttrsStep;
64
+ map(mapping: Mappable): BatchAttrsStep;
65
+ toJSON(): {
66
+ stepType: string;
67
+ data: BatchAttrsStepData[];
68
+ inverted: boolean;
69
+ };
70
+ static fromJSON(_schema: Schema, json: {
71
+ data: Array<Record<string, unknown>>;
72
+ inverted?: boolean;
73
+ }): BatchAttrsStep;
74
+ }
@@ -6,3 +6,5 @@ export { LinkMetaStep } from './steps/link-meta-step';
6
6
  export type { LinkStepMetadata } from './steps/link-meta-step';
7
7
  export { OverrideDocumentStep } from './steps/override-document-step';
8
8
  export type { OverrideDocumentStepJSON } from './steps/override-document-step';
9
+ export { BatchAttrsStep } from './steps/batch-attrs-step';
10
+ export type { BatchAttrsStepData } from './steps/batch-attrs-step';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/adf-schema",
3
- "version": "40.8.1",
3
+ "version": "40.9.0",
4
4
  "description": "Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"