@atlaskit/editor-plugin-block-menu 9.0.18 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/cjs/editor-commands/transform-node-utils/TRANSFORMATION_MATRIX.js +5 -5
  3. package/dist/cjs/editor-commands/transform-node-utils/{unwrapExpandStep.js → steps/unwrapExpandStep.js} +1 -1
  4. package/dist/cjs/editor-commands/transform-node-utils/{wrapIntoListStep.js → steps/wrapIntoListStep.js} +2 -2
  5. package/dist/cjs/editor-commands/transform-node-utils/{wrapStep.js → steps/wrapStep.js} +2 -2
  6. package/dist/cjs/editor-commands/transform-node-utils/utils.js +4 -1
  7. package/dist/cjs/editor-commands/transformNode.js +1 -1
  8. package/dist/es2019/editor-commands/transform-node-utils/TRANSFORMATION_MATRIX.js +5 -6
  9. package/dist/es2019/editor-commands/transform-node-utils/{unwrapExpandStep.js → steps/unwrapExpandStep.js} +1 -1
  10. package/dist/es2019/editor-commands/transform-node-utils/{wrapIntoListStep.js → steps/wrapIntoListStep.js} +2 -2
  11. package/dist/es2019/editor-commands/transform-node-utils/{wrapStep.js → steps/wrapStep.js} +2 -2
  12. package/dist/es2019/editor-commands/transform-node-utils/utils.js +3 -0
  13. package/dist/es2019/editor-commands/transformNode.js +1 -1
  14. package/dist/esm/editor-commands/transform-node-utils/TRANSFORMATION_MATRIX.js +5 -6
  15. package/dist/esm/editor-commands/transform-node-utils/{unwrapExpandStep.js → steps/unwrapExpandStep.js} +1 -1
  16. package/dist/esm/editor-commands/transform-node-utils/{wrapIntoListStep.js → steps/wrapIntoListStep.js} +2 -2
  17. package/dist/esm/editor-commands/transform-node-utils/{wrapStep.js → steps/wrapStep.js} +2 -2
  18. package/dist/esm/editor-commands/transform-node-utils/utils.js +3 -0
  19. package/dist/esm/editor-commands/transformNode.js +1 -1
  20. package/dist/types/blockMenuPluginType.d.ts +1 -1
  21. package/dist/types/editor-commands/selection.d.ts +1 -1
  22. package/dist/types/editor-commands/transform-node-utils/{flattenStep.d.ts → steps/flattenStep.d.ts} +1 -1
  23. package/dist/types/editor-commands/transform-node-utils/{unwrapExpandStep.d.ts → steps/unwrapExpandStep.d.ts} +1 -1
  24. package/dist/types/editor-commands/transform-node-utils/{unwrapStep.d.ts → steps/unwrapStep.d.ts} +1 -1
  25. package/dist/{types-ts4.5/editor-commands/transform-node-utils → types/editor-commands/transform-node-utils/steps}/wrapIntoListStep.d.ts +1 -1
  26. package/dist/types/editor-commands/transform-node-utils/{wrapStep.d.ts → steps/wrapStep.d.ts} +1 -1
  27. package/dist/types/editor-commands/transform-node-utils/utils.d.ts +1 -0
  28. package/dist/types/editor-commands/transformNode.d.ts +1 -1
  29. package/dist/{types-ts4.5/editor-commands/transforms → types/editor-commands}/types.d.ts +0 -3
  30. package/dist/types/index.d.ts +1 -1
  31. package/dist/types-ts4.5/blockMenuPluginType.d.ts +1 -1
  32. package/dist/types-ts4.5/editor-commands/selection.d.ts +1 -1
  33. package/dist/types-ts4.5/editor-commands/transform-node-utils/{flattenStep.d.ts → steps/flattenStep.d.ts} +1 -1
  34. package/dist/types-ts4.5/editor-commands/transform-node-utils/{unwrapExpandStep.d.ts → steps/unwrapExpandStep.d.ts} +1 -1
  35. package/dist/types-ts4.5/editor-commands/transform-node-utils/{unwrapStep.d.ts → steps/unwrapStep.d.ts} +1 -1
  36. package/dist/{types/editor-commands/transform-node-utils → types-ts4.5/editor-commands/transform-node-utils/steps}/wrapIntoListStep.d.ts +1 -1
  37. package/dist/types-ts4.5/editor-commands/transform-node-utils/{wrapStep.d.ts → steps/wrapStep.d.ts} +1 -1
  38. package/dist/types-ts4.5/editor-commands/transform-node-utils/utils.d.ts +1 -0
  39. package/dist/types-ts4.5/editor-commands/transformNode.d.ts +1 -1
  40. package/dist/{types/editor-commands/transforms → types-ts4.5/editor-commands}/types.d.ts +0 -3
  41. package/dist/types-ts4.5/index.d.ts +1 -1
  42. package/package.json +4 -4
  43. package/dist/cjs/editor-commands/transforms/block-transforms.js +0 -55
  44. package/dist/cjs/editor-commands/transforms/container-transforms.js +0 -448
  45. package/dist/cjs/editor-commands/transforms/inline-node-transforms.js +0 -39
  46. package/dist/cjs/editor-commands/transforms/layout/utils.js +0 -355
  47. package/dist/cjs/editor-commands/transforms/layout-transforms.js +0 -79
  48. package/dist/cjs/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.js +0 -97
  49. package/dist/cjs/editor-commands/transforms/list/transformTaskListToBlockNodes.js +0 -59
  50. package/dist/cjs/editor-commands/transforms/list/transformToTaskList.js +0 -44
  51. package/dist/cjs/editor-commands/transforms/list-transforms.js +0 -161
  52. package/dist/cjs/editor-commands/transforms/transformNodeToTargetType.js +0 -77
  53. package/dist/cjs/editor-commands/transforms/utils.js +0 -260
  54. package/dist/es2019/editor-commands/transforms/block-transforms.js +0 -58
  55. package/dist/es2019/editor-commands/transforms/container-transforms.js +0 -449
  56. package/dist/es2019/editor-commands/transforms/inline-node-transforms.js +0 -33
  57. package/dist/es2019/editor-commands/transforms/layout/utils.js +0 -334
  58. package/dist/es2019/editor-commands/transforms/layout-transforms.js +0 -75
  59. package/dist/es2019/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.js +0 -93
  60. package/dist/es2019/editor-commands/transforms/list/transformTaskListToBlockNodes.js +0 -47
  61. package/dist/es2019/editor-commands/transforms/list/transformToTaskList.js +0 -38
  62. package/dist/es2019/editor-commands/transforms/list-transforms.js +0 -168
  63. package/dist/es2019/editor-commands/transforms/transformNodeToTargetType.js +0 -76
  64. package/dist/es2019/editor-commands/transforms/utils.js +0 -248
  65. package/dist/esm/editor-commands/transforms/block-transforms.js +0 -50
  66. package/dist/esm/editor-commands/transforms/container-transforms.js +0 -441
  67. package/dist/esm/editor-commands/transforms/inline-node-transforms.js +0 -33
  68. package/dist/esm/editor-commands/transforms/layout/utils.js +0 -348
  69. package/dist/esm/editor-commands/transforms/layout-transforms.js +0 -72
  70. package/dist/esm/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.js +0 -90
  71. package/dist/esm/editor-commands/transforms/list/transformTaskListToBlockNodes.js +0 -53
  72. package/dist/esm/editor-commands/transforms/list/transformToTaskList.js +0 -37
  73. package/dist/esm/editor-commands/transforms/list-transforms.js +0 -155
  74. package/dist/esm/editor-commands/transforms/transformNodeToTargetType.js +0 -72
  75. package/dist/esm/editor-commands/transforms/utils.js +0 -253
  76. package/dist/types/editor-commands/transforms/block-transforms.d.ts +0 -5
  77. package/dist/types/editor-commands/transforms/container-transforms.d.ts +0 -20
  78. package/dist/types/editor-commands/transforms/inline-node-transforms.d.ts +0 -6
  79. package/dist/types/editor-commands/transforms/layout/utils.d.ts +0 -5
  80. package/dist/types/editor-commands/transforms/layout-transforms.d.ts +0 -6
  81. package/dist/types/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.d.ts +0 -3
  82. package/dist/types/editor-commands/transforms/list/transformTaskListToBlockNodes.d.ts +0 -3
  83. package/dist/types/editor-commands/transforms/list/transformToTaskList.d.ts +0 -10
  84. package/dist/types/editor-commands/transforms/list-transforms.d.ts +0 -23
  85. package/dist/types/editor-commands/transforms/transformNodeToTargetType.d.ts +0 -14
  86. package/dist/types/editor-commands/transforms/utils.d.ts +0 -45
  87. package/dist/types-ts4.5/editor-commands/transforms/block-transforms.d.ts +0 -5
  88. package/dist/types-ts4.5/editor-commands/transforms/container-transforms.d.ts +0 -20
  89. package/dist/types-ts4.5/editor-commands/transforms/inline-node-transforms.d.ts +0 -6
  90. package/dist/types-ts4.5/editor-commands/transforms/layout/utils.d.ts +0 -5
  91. package/dist/types-ts4.5/editor-commands/transforms/layout-transforms.d.ts +0 -6
  92. package/dist/types-ts4.5/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.d.ts +0 -3
  93. package/dist/types-ts4.5/editor-commands/transforms/list/transformTaskListToBlockNodes.d.ts +0 -3
  94. package/dist/types-ts4.5/editor-commands/transforms/list/transformToTaskList.d.ts +0 -10
  95. package/dist/types-ts4.5/editor-commands/transforms/list-transforms.d.ts +0 -23
  96. package/dist/types-ts4.5/editor-commands/transforms/transformNodeToTargetType.d.ts +0 -14
  97. package/dist/types-ts4.5/editor-commands/transforms/utils.d.ts +0 -45
  98. /package/dist/cjs/editor-commands/transform-node-utils/{flattenStep.js → steps/flattenStep.js} +0 -0
  99. /package/dist/cjs/editor-commands/transform-node-utils/{unwrapStep.js → steps/unwrapStep.js} +0 -0
  100. /package/dist/cjs/editor-commands/{transforms/types.js → types.js} +0 -0
  101. /package/dist/es2019/editor-commands/transform-node-utils/{flattenStep.js → steps/flattenStep.js} +0 -0
  102. /package/dist/es2019/editor-commands/transform-node-utils/{unwrapStep.js → steps/unwrapStep.js} +0 -0
  103. /package/dist/es2019/editor-commands/{transforms/types.js → types.js} +0 -0
  104. /package/dist/esm/editor-commands/transform-node-utils/{flattenStep.js → steps/flattenStep.js} +0 -0
  105. /package/dist/esm/editor-commands/transform-node-utils/{unwrapStep.js → steps/unwrapStep.js} +0 -0
  106. /package/dist/esm/editor-commands/{transforms/types.js → types.js} +0 -0
@@ -1,334 +0,0 @@
1
- import { transformListRecursively, isBulletOrOrderedList, isTaskList, getSupportedListTypesSet } from '@atlaskit/editor-common/transforms';
2
- import { Fragment } from '@atlaskit/editor-prosemirror/model';
3
- import { findChildrenByType } from '@atlaskit/editor-prosemirror/utils';
4
- import { getInlineNodeTextContent } from '../inline-node-transforms';
5
- import { getContentSupportChecker, isBlockNode, isBlockNodeForExtraction, isBlockNodeType, isContainerNode, isContainerNodeType, isHeadingOrParagraphNode, isListNode, isListNodeType } from '../utils';
6
- export const unwrapLayoutNodesToTextNodes = (context, finalTargetNodeType) => {
7
- const {
8
- tr,
9
- sourceNode,
10
- targetNodeType,
11
- targetAttrs
12
- } = context;
13
- const schema = tr.doc.type.schema || {};
14
- const isValid = getContentSupportChecker(finalTargetNodeType);
15
- const createTextNode = node => {
16
- const isTextNode = node.type.name === 'text';
17
- let nodeToTransform = node;
18
- if (isContainerNodeType(finalTargetNodeType) || isListNodeType(finalTargetNodeType)) {
19
- nodeToTransform = node.mark(node.marks.filter(mark => !['alignment', 'indentation'].includes(mark.type.name)));
20
- }
21
- if (isValid(nodeToTransform) && !isTextNode) {
22
- return nodeToTransform;
23
- }
24
- return targetNodeType.createChecked(targetAttrs, isTextNode ? nodeToTransform : nodeToTransform.content, nodeToTransform.marks);
25
- };
26
- if (isBlockNode(sourceNode)) {
27
- // code block acts like a container, we need to unwrap it
28
- if (sourceNode.type === schema.nodes.codeBlock) {
29
- const textNodes = sourceNode.textContent.split('\n').map(line => targetNodeType.createChecked(undefined, line ? schema.text(line) : null));
30
- return textNodes;
31
- }
32
- return [createTextNode(sourceNode)];
33
- }
34
- if (isContainerNode(sourceNode)) {
35
- var _sourceNode$attrs;
36
- const containerNodes = [];
37
- if (sourceNode.type.name === 'expand' && (_sourceNode$attrs = sourceNode.attrs) !== null && _sourceNode$attrs !== void 0 && _sourceNode$attrs.title) {
38
- containerNodes.push(createTextNode(schema.text(sourceNode.attrs.title)));
39
- }
40
- sourceNode.content.forEach(childNode => {
41
- if (isHeadingOrParagraphNode(childNode)) {
42
- containerNodes.push(createTextNode(childNode));
43
- return;
44
- }
45
- containerNodes.push(childNode);
46
- });
47
- return containerNodes;
48
- }
49
- if (isListNode(sourceNode)) {
50
- if (isBlockNodeType(finalTargetNodeType)) {
51
- if (sourceNode.type.name === 'taskList') {
52
- const taskItemsResult = findChildrenByType(sourceNode, schema.nodes.taskItem);
53
- const taskItems = taskItemsResult.map(item => item.node);
54
- const taskItemFragments = taskItems.map(taskItem => taskItem.content);
55
- return taskItemFragments.map(fragment => targetNodeType.createChecked(targetAttrs, fragment.content));
56
- } else {
57
- const paragraphs = findChildrenByType(sourceNode, schema.nodes.paragraph);
58
- const paragraphNodes = paragraphs.map(paragraph => paragraph.node);
59
- if (targetNodeType === schema.nodes.heading) {
60
- return paragraphNodes.map(paragraphNode => targetNodeType.createChecked(targetAttrs, paragraphNode.content));
61
- }
62
- return paragraphNodes;
63
- }
64
- }
65
- return [sourceNode];
66
- }
67
- return [sourceNode];
68
- };
69
- const transformToCodeBlock = (nodes, schema) => {
70
- const newNodes = [];
71
- const addToNewNodes = content => {
72
- if (newNodes.length === 0) {
73
- newNodes.push({
74
- canBeTransformed: true,
75
- content
76
- });
77
- } else {
78
- // Check if last node can also be transformed, if yes then append content
79
- const lastItem = newNodes[newNodes.length - 1];
80
- if (lastItem.canBeTransformed) {
81
- newNodes[newNodes.length - 1] = {
82
- content: [...lastItem.content, ...content],
83
- canBeTransformed: true
84
- };
85
- } else {
86
- newNodes.push({
87
- content,
88
- canBeTransformed: true
89
- });
90
- }
91
- }
92
- };
93
- nodes.forEach(node => {
94
- if (node.isTextblock) {
95
- const inlineTextContent = node.type === schema.nodes.codeBlock ? node.textContent : getInlineNodeTextContent(Fragment.from(node)).inlineTextContent;
96
- addToNewNodes([inlineTextContent]);
97
- } else if (isListNode(node)) {
98
- const textContent = [];
99
- const listItemType = node.type === schema.nodes.taskList ? schema.nodes.taskItem : schema.nodes.listItem;
100
- const listItems = findChildrenByType(node, listItemType).map(item => item.node);
101
- listItems.forEach(listItem => {
102
- if (listItem.type === schema.nodes.taskItem) {
103
- const inlineTextContent = getInlineNodeTextContent(Fragment.from(listItem)).inlineTextContent;
104
- textContent.push(inlineTextContent);
105
- } else {
106
- const inlineTextContent = getInlineNodeTextContent(listItem.content).inlineTextContent;
107
- textContent.push(inlineTextContent);
108
- }
109
- });
110
- addToNewNodes(textContent);
111
- } else {
112
- // If not text block or list node, then cannot be transformed
113
- newNodes.push({
114
- canBeTransformed: false,
115
- content: node
116
- });
117
- }
118
- });
119
- return newNodes.map(({
120
- canBeTransformed,
121
- content
122
- }) => {
123
- if (canBeTransformed) {
124
- const text = content.join('\n');
125
- if (text === '') {
126
- return undefined;
127
- }
128
- return schema.nodes.codeBlock.createChecked(null, schema.text(text));
129
- } else {
130
- return content;
131
- }
132
- }).filter(Boolean);
133
- };
134
- const transformToBlockNode = (nodes, targetNodeType, schema) => {
135
- if (targetNodeType === schema.nodes.codeBlock) {
136
- return transformToCodeBlock(nodes, schema);
137
- }
138
- const newNodes = [];
139
- nodes.forEach(node => {
140
- if (isListNode(node)) {
141
- const listItemType = node.type === schema.nodes.taskList ? schema.nodes.taskItem : schema.nodes.listItem;
142
- const listItems = findChildrenByType(node, listItemType).map(item => item.node);
143
- listItems.forEach(listItem => {
144
- if (listItem.type === schema.nodes.taskItem) {
145
- const inlineContent = [...listItem.content.content];
146
- if (inlineContent.length > 0) {
147
- newNodes.push(targetNodeType.createChecked(null, inlineContent));
148
- }
149
- } else {
150
- listItem.forEach(child => {
151
- if (isHeadingOrParagraphNode(child)) {
152
- newNodes.push(targetNodeType.createChecked(null, [...child.content.content]));
153
- }
154
- });
155
- }
156
- });
157
- } else {
158
- newNodes.push(node);
159
- }
160
- });
161
- return newNodes;
162
- };
163
- const transformToContainerNode = (nodes, targetNodeType) => {
164
- const newNodes = [];
165
- const isNodeValid = getContentSupportChecker(targetNodeType);
166
- nodes.forEach(node => {
167
- // If the node is not supported then we append as is
168
- if (isBlockNodeForExtraction(node)) {
169
- newNodes.push({
170
- node: node,
171
- canBeTransformed: false
172
- });
173
- } else {
174
- const isSameNodeType = node.type === targetNodeType;
175
-
176
- // If the node is not valid and not the same type, we cannot transform it
177
- if (!isNodeValid(node) && !isSameNodeType) {
178
- newNodes.push({
179
- node: node,
180
- canBeTransformed: false
181
- });
182
- return;
183
- }
184
- const nodes = isSameNodeType ? node.content.content : [node];
185
- if (newNodes.length === 0) {
186
- newNodes.push({
187
- node: nodes,
188
- canBeTransformed: true
189
- });
190
- } else {
191
- const lastItem = newNodes[newNodes.length - 1];
192
- if (lastItem.canBeTransformed) {
193
- newNodes[newNodes.length - 1] = {
194
- node: [...lastItem.node, ...nodes],
195
- canBeTransformed: true
196
- };
197
- } else {
198
- newNodes.push({
199
- node: nodes,
200
- canBeTransformed: true
201
- });
202
- }
203
- }
204
- }
205
- });
206
- return newNodes.map(({
207
- node,
208
- canBeTransformed
209
- }) => {
210
- if (canBeTransformed) {
211
- return targetNodeType.createChecked(null, Fragment.fromArray(node));
212
- } else {
213
- return node;
214
- }
215
- });
216
- };
217
- export const transformToListNode = (nodes, targetNodeType, schema) => {
218
- const isTargetTask = isTaskList(targetNodeType);
219
- const listItems = [];
220
- const listItemType = isTargetTask ? schema.nodes.taskItem : schema.nodes.listItem;
221
- const unsupportedContent = [];
222
- const onhandleUnsupportedContent = content => {
223
- unsupportedContent.push(content);
224
- };
225
- const isValid = getContentSupportChecker(listItemType);
226
- nodes.forEach(node => {
227
- // Append unsupported nodes as is
228
- if (isBlockNodeForExtraction(node)) {
229
- listItems.push({
230
- canBeTransformed: false,
231
- node
232
- });
233
- } else {
234
- let newListItems;
235
-
236
- // If the node is a list, we may need to transform it
237
- if (isListNode(node)) {
238
- const isSourceBulletOrOrdered = isBulletOrOrderedList(node.type);
239
- const isTargetTask = isTaskList(targetNodeType);
240
- const isSourceTask = isTaskList(node.type);
241
- const isTargetBulletOrOrdered = isBulletOrOrderedList(targetNodeType);
242
- const supportedListTypes = getSupportedListTypesSet(schema.nodes);
243
- if (node.type === targetNodeType) {
244
- // For the same list type, we can keep the structure
245
- newListItems = node.content.content;
246
- } else {
247
- const newList = transformListRecursively({
248
- isSourceBulletOrOrdered,
249
- isSourceTask,
250
- isTargetBulletOrOrdered,
251
- isTargetTask,
252
- listNode: node,
253
- schema,
254
- supportedListTypes,
255
- targetNodeType
256
- }, onhandleUnsupportedContent);
257
- newListItems = [...newList.content.content];
258
- }
259
- } else if (isHeadingOrParagraphNode(node) || isValid(node)) {
260
- if (isTargetTask) {
261
- const inlineContent = [...node.content.content];
262
- if (inlineContent.length > 0) {
263
- newListItems = [listItemType.create(null, inlineContent)];
264
- }
265
- } else {
266
- newListItems = [listItemType.create(null, node)];
267
- }
268
- } else if (!isValid(node)) {
269
- listItems.push({
270
- canBeTransformed: false,
271
- node
272
- });
273
- }
274
- if (newListItems) {
275
- if (listItems.length === 0) {
276
- listItems.push({
277
- canBeTransformed: true,
278
- node: newListItems
279
- });
280
- } else {
281
- const lastItem = listItems[listItems.length - 1];
282
- if (lastItem.canBeTransformed) {
283
- listItems[listItems.length - 1] = {
284
- node: [...lastItem.node, ...newListItems],
285
- canBeTransformed: true
286
- };
287
- } else {
288
- listItems.push({
289
- node: newListItems,
290
- canBeTransformed: true
291
- });
292
- }
293
- }
294
- }
295
- }
296
- });
297
- return [...listItems.map(({
298
- node,
299
- canBeTransformed
300
- }) => {
301
- if (canBeTransformed) {
302
- return targetNodeType.createChecked(null, Fragment.fromArray(node));
303
- } else {
304
- return node;
305
- }
306
- }), ...unsupportedContent];
307
- };
308
- export const convertUnwrappedLayoutContent = (nodes, targetNodeType, schema, targetAttrs) => {
309
- if (nodes.length === 1 && nodes[0].content.size === 0 && !isContainerNodeType(targetNodeType)) {
310
- if (isBlockNodeType(targetNodeType)) {
311
- if (['heading', 'paragraph'].includes(targetNodeType.name)) {
312
- return [targetNodeType.createChecked(targetAttrs)];
313
- }
314
- if (targetNodeType.name === 'codeBlock') {
315
- return [targetNodeType.createChecked()];
316
- }
317
- }
318
- if (isListNodeType(targetNodeType)) {
319
- const listItem = isTaskList(targetNodeType) ? schema.nodes.taskItem.createChecked() : schema.nodes.listItem.createChecked(null, schema.nodes.paragraph.createChecked());
320
- return [targetNodeType.createChecked(null, listItem)];
321
- }
322
- return nodes;
323
- }
324
- if (isBlockNodeType(targetNodeType)) {
325
- return transformToBlockNode(nodes, targetNodeType, schema);
326
- }
327
- if (isContainerNodeType(targetNodeType)) {
328
- return transformToContainerNode(nodes, targetNodeType);
329
- }
330
- if (isListNodeType(targetNodeType)) {
331
- return transformToListNode(nodes, targetNodeType, schema);
332
- }
333
- return [];
334
- };
@@ -1,75 +0,0 @@
1
- import { DEFAULT_TWO_COLUMN_LAYOUT_COLUMN_WIDTH } from '@atlaskit/editor-common/styles';
2
- import { Fragment } from '@atlaskit/editor-prosemirror/model';
3
- import { convertUnwrappedLayoutContent, unwrapLayoutNodesToTextNodes } from './layout/utils';
4
- import { getMarksWithBreakout, isHeadingOrParagraphNode } from './utils';
5
- export const createDefaultLayoutSection = (schema, content, marks) => {
6
- const {
7
- layoutSection,
8
- layoutColumn,
9
- paragraph
10
- } = schema.nodes;
11
- const layoutContent = Fragment.fromArray([layoutColumn.createChecked({
12
- width: DEFAULT_TWO_COLUMN_LAYOUT_COLUMN_WIDTH
13
- }, content), layoutColumn.create({
14
- width: DEFAULT_TWO_COLUMN_LAYOUT_COLUMN_WIDTH
15
- }, paragraph.createAndFill())]);
16
- return layoutSection.createChecked(undefined, layoutContent, marks);
17
- };
18
- export const convertToLayout = context => {
19
- const {
20
- tr,
21
- sourceNode,
22
- sourcePos
23
- } = context;
24
- const content = sourceNode.mark(sourceNode.marks.filter(mark => mark.type.name !== 'breakout'));
25
-
26
- // Layout supports breakout mark that can have width attribute
27
- // When other nodes with breakout (codeBlock and expand) are converted to a layout, the layout should get width of original node
28
- const marks = getMarksWithBreakout(sourceNode, tr.doc.type.schema.nodes.layoutSection);
29
- const layoutSectionNode = createDefaultLayoutSection(tr.doc.type.schema, content, marks);
30
- if (isHeadingOrParagraphNode(sourceNode)) {
31
- // -1 to fix when sourceNode is the last node in the document, unable to convert to layout
32
- tr.replaceRangeWith(sourcePos > 0 ? sourcePos - 1 : sourcePos, sourcePos + sourceNode.nodeSize - 1, layoutSectionNode);
33
- } else {
34
- tr.replaceRangeWith(sourcePos, sourcePos + sourceNode.nodeSize, layoutSectionNode);
35
- }
36
- return tr;
37
- };
38
- export const transformLayoutNode = context => {
39
- const {
40
- tr,
41
- sourceNode,
42
- targetNodeType,
43
- sourcePos,
44
- targetAttrs
45
- } = context;
46
- const schema = tr.doc.type.schema || {};
47
- const {
48
- layoutSection,
49
- layoutColumn,
50
- paragraph,
51
- heading
52
- } = schema.nodes || {};
53
- const layoutColumnNodes = [];
54
- const targetTextNodeType = targetNodeType === heading ? heading : paragraph;
55
- sourceNode.children.forEach(child => {
56
- if (child.type === layoutColumn) {
57
- const unwrappedContent = [];
58
- child.content.forEach(node => {
59
- // Unwrap all nodes and convert to text nodes
60
- const context = {
61
- tr,
62
- sourceNode: node,
63
- targetNodeType: targetTextNodeType,
64
- sourcePos: 0,
65
- targetAttrs
66
- };
67
- const newContent = unwrapLayoutNodesToTextNodes(context, targetNodeType);
68
- unwrappedContent.push(...newContent);
69
- });
70
- const newColumnContent = convertUnwrappedLayoutContent(unwrappedContent, targetNodeType, schema, targetAttrs);
71
- layoutColumnNodes.push(layoutColumn.createChecked(child.attrs, Fragment.fromArray(newColumnContent), child.marks));
72
- }
73
- });
74
- return tr.replaceRangeWith(sourcePos, sourcePos + sourceNode.nodeSize, layoutSection.createChecked(sourceNode.attrs, Fragment.fromArray(layoutColumnNodes), sourceNode.marks));
75
- };
@@ -1,93 +0,0 @@
1
- import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
2
- import { findChildrenByType } from '@atlaskit/editor-prosemirror/utils';
3
- import { getInlineNodeTextContent } from '../inline-node-transforms';
4
- import { getContentSupportChecker, isListNode } from '../utils';
5
- export const transformOrderedUnorderedListToBlockNodes = context => {
6
- const {
7
- tr,
8
- targetNodeType,
9
- targetAttrs,
10
- sourceNode,
11
- sourcePos
12
- } = context;
13
- const {
14
- selection
15
- } = tr;
16
- const schema = selection.$from.doc.type.schema;
17
- const targetNodes = [];
18
- // find all list items inside the list node
19
- const listItems = findChildrenByType(sourceNode, schema.nodes.listItem);
20
- let textContent = [];
21
- const flushTextContent = () => {
22
- if (targetNodeType !== schema.nodes.codeBlock) {
23
- return;
24
- }
25
- if (textContent.length > 0) {
26
- const inlineText = textContent.join('\n');
27
- targetNodes.push(schema.nodes.codeBlock.createChecked({}, inlineText !== '' ? schema.text(inlineText) : null));
28
- textContent = [];
29
- }
30
- };
31
- listItems.forEach(listItem => {
32
- // Convert paragraphs to headings if target is heading
33
- const content = listItem.node.content.content;
34
- content.forEach(node => {
35
- const isValid = getContentSupportChecker(targetNodeType)(node);
36
- if (isListNode(node)) {
37
- // Skip nested lists as it will return listItems that we will deal with separately
38
- return;
39
- }
40
-
41
- // Deal with the case where targetNode and node are both codeBlocks, then append text content
42
- if (targetNodeType === schema.nodes.codeBlock && node.type === schema.nodes.codeBlock) {
43
- const inlineContent = node.textContent;
44
- textContent = [...textContent, inlineContent];
45
- return;
46
- }
47
-
48
- // If the node is not valid for the target container,
49
- // flush any existing text content if target is codeBlock
50
- // and extract the node without any conversion
51
- if (!isValid && !node.isTextblock) {
52
- flushTextContent();
53
- targetNodes.push(node);
54
- return;
55
- }
56
-
57
- // If the target is codeBlock, accumulate text content
58
- if (targetNodeType === schema.nodes.codeBlock) {
59
- const inlineContent = getInlineNodeTextContent(Fragment.from(node)).inlineTextContent;
60
- textContent = [...textContent, inlineContent];
61
- return;
62
- }
63
-
64
- // Convert codeblocks to block nodes
65
- if (node.type === schema.nodes.codeBlock) {
66
- var _targetAttrs$level;
67
- // eslint-disable-next-line @atlassian/perf-linting/no-expensive-split-replace -- Ignored via go/ees017 (to be fixed)
68
- const textContent = node.textContent.split('\n');
69
- const attributes = targetNodeType === schema.nodes.heading ? {
70
- level: (_targetAttrs$level = targetAttrs === null || targetAttrs === void 0 ? void 0 : targetAttrs.level) !== null && _targetAttrs$level !== void 0 ? _targetAttrs$level : 1
71
- } : null;
72
- textContent.forEach(textLine => {
73
- targetNodes.push(targetNodeType.createChecked(attributes, textLine ? schema.text(textLine) : null));
74
- });
75
- return;
76
- }
77
- if (targetNodeType === schema.nodes.heading && targetAttrs) {
78
- const targetHeadingLevel = targetAttrs.level;
79
- targetNodes.push(schema.nodes.heading.createChecked({
80
- level: targetHeadingLevel
81
- }, node.content));
82
- return;
83
- }
84
- targetNodes.push(node);
85
- });
86
- });
87
- flushTextContent();
88
- const fragment = Fragment.fromArray(targetNodes);
89
- const slice = new Slice(fragment, 0, 0);
90
- const rangeStart = sourcePos !== null ? sourcePos : selection.from;
91
- tr.replaceRange(rangeStart, rangeStart + sourceNode.nodeSize, slice);
92
- return tr;
93
- };
@@ -1,47 +0,0 @@
1
- import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
2
- import { findChildrenByType } from '@atlaskit/editor-prosemirror/utils';
3
- export const transformTaskListToBlockNodes = context => {
4
- const {
5
- tr,
6
- targetNodeType,
7
- targetAttrs,
8
- sourceNode,
9
- sourcePos
10
- } = context;
11
- const {
12
- selection
13
- } = tr;
14
- const schema = selection.$from.doc.type.schema;
15
- const taskItemsResult = findChildrenByType(sourceNode, schema.nodes.taskItem);
16
- const taskItems = taskItemsResult.map(item => item.node);
17
- const taskItemFragments = taskItems.map(taskItem => taskItem.content);
18
- let targetNodes = [];
19
-
20
- // Convert fragments to headings if target is heading
21
- if (targetNodeType === schema.nodes.heading && targetAttrs) {
22
- // convert the fragments to headings
23
- const targetHeadingLevel = targetAttrs.level;
24
- targetNodes = taskItemFragments.map(fragment => schema.nodes.heading.createChecked({
25
- level: targetHeadingLevel
26
- }, fragment.content));
27
- }
28
-
29
- // Convert fragments to paragraphs if target is paragraphs
30
- if (targetNodeType === schema.nodes.paragraph) {
31
- // convert the fragments to paragraphs
32
- targetNodes = taskItemFragments.map(fragment => schema.nodes.paragraph.createChecked({}, fragment.content));
33
- }
34
-
35
- // Convert fragments to code block if target is code block
36
- if (targetNodeType === schema.nodes.codeBlock) {
37
- // convert the fragments to one code block
38
- const codeBlockContent = taskItemFragments.map(fragment => fragment.textBetween(0, fragment.size, '\n')).join('\n');
39
- targetNodes = [schema.nodes.codeBlock.createChecked({}, schema.text(codeBlockContent))];
40
- }
41
-
42
- // Replace the task list node with the new content in the transaction
43
- const slice = new Slice(Fragment.fromArray(targetNodes), 0, 0);
44
- const rangeStart = sourcePos !== null ? sourcePos : selection.from;
45
- tr.replaceRange(rangeStart, rangeStart + sourceNode.nodeSize, slice);
46
- return tr;
47
- };
@@ -1,38 +0,0 @@
1
- /**
2
- * Transform selection to task list
3
- * Handles the special structure where taskItem contains text directly (no paragraph wrapper)
4
- */
5
- export const transformToTaskList = (tr, range, targetNodeType, targetAttrs, nodes) => {
6
- try {
7
- const {
8
- taskItem
9
- } = nodes;
10
- const listItems = [];
11
-
12
- // Process each block in the range
13
- tr.doc.nodesBetween(range.start, range.end, node => {
14
- if (node.isBlock) {
15
- // For block nodes like paragraphs, directly use their inline content
16
- const inlineContent = [...node.content.content];
17
- if (inlineContent.length > 0) {
18
- // Create task item with inline content directly
19
- const listItem = taskItem.create(targetAttrs, inlineContent);
20
- listItems.push(listItem);
21
- }
22
- }
23
- return false; // Don't traverse into children
24
- });
25
- if (listItems.length === 0) {
26
- return null;
27
- }
28
-
29
- // Create the new task list
30
- const newList = targetNodeType.create(targetAttrs, listItems);
31
-
32
- // Replace the range with the new list
33
- tr.replaceWith(range.start, range.end, newList);
34
- return tr;
35
- } catch {
36
- return null;
37
- }
38
- };