@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.
- package/CHANGELOG.md +17 -0
- package/dist/cjs/editor-commands/transform-node-utils/TRANSFORMATION_MATRIX.js +5 -5
- package/dist/cjs/editor-commands/transform-node-utils/{unwrapExpandStep.js → steps/unwrapExpandStep.js} +1 -1
- package/dist/cjs/editor-commands/transform-node-utils/{wrapIntoListStep.js → steps/wrapIntoListStep.js} +2 -2
- package/dist/cjs/editor-commands/transform-node-utils/{wrapStep.js → steps/wrapStep.js} +2 -2
- package/dist/cjs/editor-commands/transform-node-utils/utils.js +4 -1
- package/dist/cjs/editor-commands/transformNode.js +1 -1
- package/dist/es2019/editor-commands/transform-node-utils/TRANSFORMATION_MATRIX.js +5 -6
- package/dist/es2019/editor-commands/transform-node-utils/{unwrapExpandStep.js → steps/unwrapExpandStep.js} +1 -1
- package/dist/es2019/editor-commands/transform-node-utils/{wrapIntoListStep.js → steps/wrapIntoListStep.js} +2 -2
- package/dist/es2019/editor-commands/transform-node-utils/{wrapStep.js → steps/wrapStep.js} +2 -2
- package/dist/es2019/editor-commands/transform-node-utils/utils.js +3 -0
- package/dist/es2019/editor-commands/transformNode.js +1 -1
- package/dist/esm/editor-commands/transform-node-utils/TRANSFORMATION_MATRIX.js +5 -6
- package/dist/esm/editor-commands/transform-node-utils/{unwrapExpandStep.js → steps/unwrapExpandStep.js} +1 -1
- package/dist/esm/editor-commands/transform-node-utils/{wrapIntoListStep.js → steps/wrapIntoListStep.js} +2 -2
- package/dist/esm/editor-commands/transform-node-utils/{wrapStep.js → steps/wrapStep.js} +2 -2
- package/dist/esm/editor-commands/transform-node-utils/utils.js +3 -0
- package/dist/esm/editor-commands/transformNode.js +1 -1
- package/dist/types/blockMenuPluginType.d.ts +1 -1
- package/dist/types/editor-commands/selection.d.ts +1 -1
- package/dist/types/editor-commands/transform-node-utils/{flattenStep.d.ts → steps/flattenStep.d.ts} +1 -1
- package/dist/types/editor-commands/transform-node-utils/{unwrapExpandStep.d.ts → steps/unwrapExpandStep.d.ts} +1 -1
- package/dist/types/editor-commands/transform-node-utils/{unwrapStep.d.ts → steps/unwrapStep.d.ts} +1 -1
- package/dist/{types-ts4.5/editor-commands/transform-node-utils → types/editor-commands/transform-node-utils/steps}/wrapIntoListStep.d.ts +1 -1
- package/dist/types/editor-commands/transform-node-utils/{wrapStep.d.ts → steps/wrapStep.d.ts} +1 -1
- package/dist/types/editor-commands/transform-node-utils/utils.d.ts +1 -0
- package/dist/types/editor-commands/transformNode.d.ts +1 -1
- package/dist/{types-ts4.5/editor-commands/transforms → types/editor-commands}/types.d.ts +0 -3
- package/dist/types/index.d.ts +1 -1
- package/dist/types-ts4.5/blockMenuPluginType.d.ts +1 -1
- package/dist/types-ts4.5/editor-commands/selection.d.ts +1 -1
- package/dist/types-ts4.5/editor-commands/transform-node-utils/{flattenStep.d.ts → steps/flattenStep.d.ts} +1 -1
- package/dist/types-ts4.5/editor-commands/transform-node-utils/{unwrapExpandStep.d.ts → steps/unwrapExpandStep.d.ts} +1 -1
- package/dist/types-ts4.5/editor-commands/transform-node-utils/{unwrapStep.d.ts → steps/unwrapStep.d.ts} +1 -1
- package/dist/{types/editor-commands/transform-node-utils → types-ts4.5/editor-commands/transform-node-utils/steps}/wrapIntoListStep.d.ts +1 -1
- package/dist/types-ts4.5/editor-commands/transform-node-utils/{wrapStep.d.ts → steps/wrapStep.d.ts} +1 -1
- package/dist/types-ts4.5/editor-commands/transform-node-utils/utils.d.ts +1 -0
- package/dist/types-ts4.5/editor-commands/transformNode.d.ts +1 -1
- package/dist/{types/editor-commands/transforms → types-ts4.5/editor-commands}/types.d.ts +0 -3
- package/dist/types-ts4.5/index.d.ts +1 -1
- package/package.json +4 -4
- package/dist/cjs/editor-commands/transforms/block-transforms.js +0 -55
- package/dist/cjs/editor-commands/transforms/container-transforms.js +0 -448
- package/dist/cjs/editor-commands/transforms/inline-node-transforms.js +0 -39
- package/dist/cjs/editor-commands/transforms/layout/utils.js +0 -355
- package/dist/cjs/editor-commands/transforms/layout-transforms.js +0 -79
- package/dist/cjs/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.js +0 -97
- package/dist/cjs/editor-commands/transforms/list/transformTaskListToBlockNodes.js +0 -59
- package/dist/cjs/editor-commands/transforms/list/transformToTaskList.js +0 -44
- package/dist/cjs/editor-commands/transforms/list-transforms.js +0 -161
- package/dist/cjs/editor-commands/transforms/transformNodeToTargetType.js +0 -77
- package/dist/cjs/editor-commands/transforms/utils.js +0 -260
- package/dist/es2019/editor-commands/transforms/block-transforms.js +0 -58
- package/dist/es2019/editor-commands/transforms/container-transforms.js +0 -449
- package/dist/es2019/editor-commands/transforms/inline-node-transforms.js +0 -33
- package/dist/es2019/editor-commands/transforms/layout/utils.js +0 -334
- package/dist/es2019/editor-commands/transforms/layout-transforms.js +0 -75
- package/dist/es2019/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.js +0 -93
- package/dist/es2019/editor-commands/transforms/list/transformTaskListToBlockNodes.js +0 -47
- package/dist/es2019/editor-commands/transforms/list/transformToTaskList.js +0 -38
- package/dist/es2019/editor-commands/transforms/list-transforms.js +0 -168
- package/dist/es2019/editor-commands/transforms/transformNodeToTargetType.js +0 -76
- package/dist/es2019/editor-commands/transforms/utils.js +0 -248
- package/dist/esm/editor-commands/transforms/block-transforms.js +0 -50
- package/dist/esm/editor-commands/transforms/container-transforms.js +0 -441
- package/dist/esm/editor-commands/transforms/inline-node-transforms.js +0 -33
- package/dist/esm/editor-commands/transforms/layout/utils.js +0 -348
- package/dist/esm/editor-commands/transforms/layout-transforms.js +0 -72
- package/dist/esm/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.js +0 -90
- package/dist/esm/editor-commands/transforms/list/transformTaskListToBlockNodes.js +0 -53
- package/dist/esm/editor-commands/transforms/list/transformToTaskList.js +0 -37
- package/dist/esm/editor-commands/transforms/list-transforms.js +0 -155
- package/dist/esm/editor-commands/transforms/transformNodeToTargetType.js +0 -72
- package/dist/esm/editor-commands/transforms/utils.js +0 -253
- package/dist/types/editor-commands/transforms/block-transforms.d.ts +0 -5
- package/dist/types/editor-commands/transforms/container-transforms.d.ts +0 -20
- package/dist/types/editor-commands/transforms/inline-node-transforms.d.ts +0 -6
- package/dist/types/editor-commands/transforms/layout/utils.d.ts +0 -5
- package/dist/types/editor-commands/transforms/layout-transforms.d.ts +0 -6
- package/dist/types/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.d.ts +0 -3
- package/dist/types/editor-commands/transforms/list/transformTaskListToBlockNodes.d.ts +0 -3
- package/dist/types/editor-commands/transforms/list/transformToTaskList.d.ts +0 -10
- package/dist/types/editor-commands/transforms/list-transforms.d.ts +0 -23
- package/dist/types/editor-commands/transforms/transformNodeToTargetType.d.ts +0 -14
- package/dist/types/editor-commands/transforms/utils.d.ts +0 -45
- package/dist/types-ts4.5/editor-commands/transforms/block-transforms.d.ts +0 -5
- package/dist/types-ts4.5/editor-commands/transforms/container-transforms.d.ts +0 -20
- package/dist/types-ts4.5/editor-commands/transforms/inline-node-transforms.d.ts +0 -6
- package/dist/types-ts4.5/editor-commands/transforms/layout/utils.d.ts +0 -5
- package/dist/types-ts4.5/editor-commands/transforms/layout-transforms.d.ts +0 -6
- package/dist/types-ts4.5/editor-commands/transforms/list/transformOrderedUnorderedListToBlockNodes.d.ts +0 -3
- package/dist/types-ts4.5/editor-commands/transforms/list/transformTaskListToBlockNodes.d.ts +0 -3
- package/dist/types-ts4.5/editor-commands/transforms/list/transformToTaskList.d.ts +0 -10
- package/dist/types-ts4.5/editor-commands/transforms/list-transforms.d.ts +0 -23
- package/dist/types-ts4.5/editor-commands/transforms/transformNodeToTargetType.d.ts +0 -14
- package/dist/types-ts4.5/editor-commands/transforms/utils.d.ts +0 -45
- /package/dist/cjs/editor-commands/transform-node-utils/{flattenStep.js → steps/flattenStep.js} +0 -0
- /package/dist/cjs/editor-commands/transform-node-utils/{unwrapStep.js → steps/unwrapStep.js} +0 -0
- /package/dist/cjs/editor-commands/{transforms/types.js → types.js} +0 -0
- /package/dist/es2019/editor-commands/transform-node-utils/{flattenStep.js → steps/flattenStep.js} +0 -0
- /package/dist/es2019/editor-commands/transform-node-utils/{unwrapStep.js → steps/unwrapStep.js} +0 -0
- /package/dist/es2019/editor-commands/{transforms/types.js → types.js} +0 -0
- /package/dist/esm/editor-commands/transform-node-utils/{flattenStep.js → steps/flattenStep.js} +0 -0
- /package/dist/esm/editor-commands/transform-node-utils/{unwrapStep.js → steps/unwrapStep.js} +0 -0
- /package/dist/esm/editor-commands/{transforms/types.js → types.js} +0 -0
|
@@ -1,449 +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 { isBlockNodeType, isListNodeType, isContainerNodeType, isBlockNodeForExtraction, convertNodeToInlineContent, getContentSupportChecker, convertCodeBlockContentToParagraphs, filterMarksForTargetNodeType, getMarksWithBreakout } from './utils';
|
|
5
|
-
const convertInvalidNodeToValidNodeType = (sourceContent, sourceNodeType, validNodeType, withMarks) => {
|
|
6
|
-
const validTransformedContent = [];
|
|
7
|
-
// Headings are not valid inside headings so convert heading nodes to paragraphs
|
|
8
|
-
sourceContent.forEach(node => {
|
|
9
|
-
if (sourceNodeType === node.type) {
|
|
10
|
-
validTransformedContent.push(validNodeType.createChecked(node.attrs, node.content, withMarks ? node.marks : undefined));
|
|
11
|
-
} else {
|
|
12
|
-
validTransformedContent.push(node);
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
return Fragment.from(validTransformedContent);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Transform selection to container type
|
|
20
|
-
*/
|
|
21
|
-
export const transformToContainer = ({
|
|
22
|
-
tr,
|
|
23
|
-
sourceNode,
|
|
24
|
-
targetNodeType,
|
|
25
|
-
targetAttrs
|
|
26
|
-
}) => {
|
|
27
|
-
const selection = tr.selection;
|
|
28
|
-
const schema = tr.doc.type.schema;
|
|
29
|
-
const content = selection.content().content;
|
|
30
|
-
let transformedContent = content;
|
|
31
|
-
let marks = [];
|
|
32
|
-
if (sourceNode.type === schema.nodes.codeBlock) {
|
|
33
|
-
const paragraphNodes = convertCodeBlockContentToParagraphs(sourceNode, schema);
|
|
34
|
-
transformedContent = Fragment.fromArray(paragraphNodes);
|
|
35
|
-
marks = getMarksWithBreakout(sourceNode, targetNodeType);
|
|
36
|
-
}
|
|
37
|
-
if (targetNodeType === schema.nodes.blockquote) {
|
|
38
|
-
transformedContent = convertInvalidNodeToValidNodeType(transformedContent, schema.nodes.heading, schema.nodes.paragraph, true);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Preserve marks that are allowed in the target node type
|
|
42
|
-
// e.g. blocks (heading/ paragraph) with alignment need to remove alignment
|
|
43
|
-
// as panel/ blockQuote/ expands does not support alignment
|
|
44
|
-
if (sourceNode.type === schema.nodes.paragraph || sourceNode.type === schema.nodes.heading) {
|
|
45
|
-
transformedContent = filterMarksForTargetNodeType(transformedContent, targetNodeType);
|
|
46
|
-
}
|
|
47
|
-
const newNode = targetNodeType.createAndFill(targetAttrs, transformedContent, marks);
|
|
48
|
-
if (!newNode) {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
tr.replaceRangeWith(selection.from, selection.to, newNode);
|
|
52
|
-
return tr;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Transform container nodes (panel, expand, blockquote)
|
|
57
|
-
*/
|
|
58
|
-
export const transformContainerNode = ({
|
|
59
|
-
tr,
|
|
60
|
-
sourceNode,
|
|
61
|
-
sourcePos,
|
|
62
|
-
targetNodeType,
|
|
63
|
-
targetAttrs
|
|
64
|
-
}) => {
|
|
65
|
-
if (sourcePos === null) {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Transform container to block type - unwrap and convert content
|
|
70
|
-
if (isBlockNodeType(targetNodeType)) {
|
|
71
|
-
// special case container to codeblock
|
|
72
|
-
if (targetNodeType.name === 'codeBlock') {
|
|
73
|
-
return transformBetweenContainerTypes({
|
|
74
|
-
tr,
|
|
75
|
-
sourceNode,
|
|
76
|
-
sourcePos,
|
|
77
|
-
targetNodeType,
|
|
78
|
-
targetAttrs
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
return unwrapAndConvertToBlockType({
|
|
82
|
-
tr,
|
|
83
|
-
sourceNode,
|
|
84
|
-
sourcePos,
|
|
85
|
-
targetNodeType,
|
|
86
|
-
targetAttrs
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Transform container to list type
|
|
91
|
-
if (isListNodeType(targetNodeType)) {
|
|
92
|
-
return unwrapAndConvertToList({
|
|
93
|
-
tr,
|
|
94
|
-
sourceNode,
|
|
95
|
-
sourcePos,
|
|
96
|
-
targetNodeType,
|
|
97
|
-
targetAttrs
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Transform between container types
|
|
102
|
-
if (isContainerNodeType(targetNodeType)) {
|
|
103
|
-
return transformBetweenContainerTypes({
|
|
104
|
-
tr,
|
|
105
|
-
sourceNode,
|
|
106
|
-
sourcePos,
|
|
107
|
-
targetNodeType,
|
|
108
|
-
targetAttrs
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
return null;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Unwrap container node and convert content to block type
|
|
116
|
-
*/
|
|
117
|
-
export const unwrapAndConvertToBlockType = context => {
|
|
118
|
-
const {
|
|
119
|
-
tr,
|
|
120
|
-
targetNodeType,
|
|
121
|
-
targetAttrs,
|
|
122
|
-
sourceNode,
|
|
123
|
-
sourcePos
|
|
124
|
-
} = context;
|
|
125
|
-
const {
|
|
126
|
-
selection
|
|
127
|
-
} = tr;
|
|
128
|
-
const schema = selection.$from.doc.type.schema;
|
|
129
|
-
const {
|
|
130
|
-
paragraph,
|
|
131
|
-
heading,
|
|
132
|
-
codeBlock,
|
|
133
|
-
expand
|
|
134
|
-
} = schema.nodes;
|
|
135
|
-
const rangeStart = sourcePos !== null ? sourcePos : selection.from;
|
|
136
|
-
let sourceChildren = [...sourceNode.children];
|
|
137
|
-
let transformedContent = [];
|
|
138
|
-
|
|
139
|
-
// If the container is expand, we need to extract the title and convert it to a paragraph
|
|
140
|
-
// and add it to the beginning of the content
|
|
141
|
-
if (sourceNode.type === expand) {
|
|
142
|
-
var _sourceNode$attrs;
|
|
143
|
-
const title = (_sourceNode$attrs = sourceNode.attrs) === null || _sourceNode$attrs === void 0 ? void 0 : _sourceNode$attrs.title;
|
|
144
|
-
if (title) {
|
|
145
|
-
const titleContent = schema.text(title);
|
|
146
|
-
sourceChildren.unshift(paragraph.createChecked({}, titleContent));
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// if the container is a code block, convert text content to multiple paragraphs
|
|
151
|
-
if (sourceNode.type === codeBlock) {
|
|
152
|
-
sourceChildren = convertCodeBlockContentToParagraphs(sourceNode, schema);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// if target node is a paragraph, just do unwrap
|
|
156
|
-
if (targetNodeType === paragraph) {
|
|
157
|
-
transformedContent = sourceChildren;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// if target node is a headings, do unwrap and convert to heading
|
|
161
|
-
if (targetNodeType === heading && targetAttrs) {
|
|
162
|
-
const targetHeadingLevel = targetAttrs.level;
|
|
163
|
-
sourceChildren.forEach((node, index) => {
|
|
164
|
-
if (node.isTextblock) {
|
|
165
|
-
const headingNode = heading.create({
|
|
166
|
-
level: targetHeadingLevel
|
|
167
|
-
}, node.content);
|
|
168
|
-
sourceChildren[index] = headingNode;
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
transformedContent = sourceChildren;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// if target node is code block, do unwrap and convert to code block
|
|
175
|
-
if (targetNodeType === codeBlock) {
|
|
176
|
-
const codeBlockContent = sourceChildren.map(node => node.content.textBetween(0, node.content.size, '\n')).join('\n');
|
|
177
|
-
const marks = getMarksWithBreakout(sourceNode, targetNodeType);
|
|
178
|
-
transformedContent = [codeBlock.createChecked({}, schema.text(codeBlockContent), marks)];
|
|
179
|
-
}
|
|
180
|
-
const slice = new Slice(Fragment.fromArray(transformedContent), 0, 0);
|
|
181
|
-
tr.replaceRange(rangeStart, rangeStart + sourceNode.nodeSize, slice);
|
|
182
|
-
return tr;
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Unwrap container node and convert content to list
|
|
187
|
-
*/
|
|
188
|
-
export const unwrapAndConvertToList = ({
|
|
189
|
-
tr,
|
|
190
|
-
sourceNode,
|
|
191
|
-
sourcePos,
|
|
192
|
-
targetNodeType,
|
|
193
|
-
targetAttrs
|
|
194
|
-
}) => {
|
|
195
|
-
const {
|
|
196
|
-
schema
|
|
197
|
-
} = tr.doc.type;
|
|
198
|
-
const {
|
|
199
|
-
listItem,
|
|
200
|
-
paragraph,
|
|
201
|
-
taskList,
|
|
202
|
-
taskItem,
|
|
203
|
-
heading
|
|
204
|
-
} = schema.nodes;
|
|
205
|
-
const isTargetTaskList = targetNodeType === taskList;
|
|
206
|
-
const createListItemFromInline = content => {
|
|
207
|
-
if (!content) {
|
|
208
|
-
return isTargetTaskList ? taskItem.create() : listItem.create(null, paragraph.create());
|
|
209
|
-
} else {
|
|
210
|
-
return isTargetTaskList ? taskItem.create(null, content) : listItem.create(null, paragraph.create(null, content));
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
const getInlineContent = textblock => {
|
|
214
|
-
const inlineContent = [];
|
|
215
|
-
textblock.forEach(inline => {
|
|
216
|
-
inlineContent.push(inline);
|
|
217
|
-
});
|
|
218
|
-
return inlineContent;
|
|
219
|
-
};
|
|
220
|
-
const resultContent = [];
|
|
221
|
-
let currentListItems = [];
|
|
222
|
-
const targetListItemType = isTargetTaskList ? taskItem : listItem;
|
|
223
|
-
// Expand's title should become the first item of the list
|
|
224
|
-
if (sourceNode.type.name === 'expand') {
|
|
225
|
-
var _sourceNode$attrs2;
|
|
226
|
-
const title = (_sourceNode$attrs2 = sourceNode.attrs) === null || _sourceNode$attrs2 === void 0 ? void 0 : _sourceNode$attrs2.title;
|
|
227
|
-
if (title) {
|
|
228
|
-
const titleContent = schema.text(title);
|
|
229
|
-
currentListItems.push(createListItemFromInline(titleContent));
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
const createListAndAddToContent = () => {
|
|
233
|
-
if (currentListItems.length) {
|
|
234
|
-
const currentList = targetNodeType.create(targetAttrs || null, Fragment.from(currentListItems));
|
|
235
|
-
currentListItems = [];
|
|
236
|
-
resultContent.push(currentList);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
if (sourceNode.type.name === 'codeBlock') {
|
|
240
|
-
const codeText = sourceNode.textContent;
|
|
241
|
-
// check if code block only contains newline characters
|
|
242
|
-
// eslint-disable-next-line require-unicode-regexp
|
|
243
|
-
const isOnlyNewLines = codeText => codeText.replace(/\n/g, '').trim() === '';
|
|
244
|
-
if (!codeText || isOnlyNewLines(codeText)) {
|
|
245
|
-
// Empty code block - create an empty list item
|
|
246
|
-
currentListItems.push(createListItemFromInline());
|
|
247
|
-
}
|
|
248
|
-
if (codeText) {
|
|
249
|
-
const lines = codeText.split('\n');
|
|
250
|
-
// Remove empty lines
|
|
251
|
-
const nonEmptyLines = lines.filter(line => line.trim().length > 0);
|
|
252
|
-
nonEmptyLines.forEach(line => {
|
|
253
|
-
const lineText = schema.text(line);
|
|
254
|
-
currentListItems.push(createListItemFromInline(lineText));
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
} else {
|
|
258
|
-
sourceNode.forEach(child => {
|
|
259
|
-
if (targetListItemType.validContent(Fragment.from(child))) {
|
|
260
|
-
currentListItems.push(targetListItemType.create(null, child));
|
|
261
|
-
} else if (heading === child.type || isTargetTaskList && paragraph === child.type) {
|
|
262
|
-
const inline = Fragment.from(getInlineContent(child));
|
|
263
|
-
currentListItems.push(createListItemFromInline(inline));
|
|
264
|
-
} else {
|
|
265
|
-
// Create list and add list first
|
|
266
|
-
createListAndAddToContent();
|
|
267
|
-
// Then add content that can't be converted into listItem
|
|
268
|
-
resultContent.push(child);
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
if (!resultContent.length && !currentListItems.length) {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
createListAndAddToContent();
|
|
276
|
-
return tr.replaceWith(sourcePos, sourcePos + sourceNode.nodeSize, Fragment.from(resultContent));
|
|
277
|
-
};
|
|
278
|
-
export const transformBetweenContainerTypes = context => {
|
|
279
|
-
const {
|
|
280
|
-
tr,
|
|
281
|
-
sourceNode,
|
|
282
|
-
sourcePos,
|
|
283
|
-
targetNodeType,
|
|
284
|
-
targetAttrs
|
|
285
|
-
} = context;
|
|
286
|
-
|
|
287
|
-
// Special handling for codeBlock target
|
|
288
|
-
if (targetNodeType.name === 'codeBlock') {
|
|
289
|
-
const contentSplits = splitContentForCodeBlock(sourceNode, targetNodeType, targetAttrs, tr.doc.type.schema);
|
|
290
|
-
if (contentSplits.length === 0) {
|
|
291
|
-
const {
|
|
292
|
-
schema
|
|
293
|
-
} = tr.doc.type;
|
|
294
|
-
const marks = getMarksWithBreakout(sourceNode, targetNodeType);
|
|
295
|
-
const codeBlock = schema.nodes.codeBlock.create(targetAttrs, null, marks);
|
|
296
|
-
return tr.replaceWith(sourcePos, sourcePos + sourceNode.nodeSize, codeBlock);
|
|
297
|
-
}
|
|
298
|
-
return applySplitsToTransaction(tr, sourcePos, sourceNode.nodeSize, contentSplits);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// Get content validation for target container type
|
|
302
|
-
const isContentSupported = getContentSupportChecker(targetNodeType);
|
|
303
|
-
|
|
304
|
-
// Process content and collect splits
|
|
305
|
-
const contentSplits = splitContentAroundUnsupportedBlocks(sourceNode, isContentSupported, targetNodeType, targetAttrs, tr.doc.type.schema);
|
|
306
|
-
return applySplitsToTransaction(tr, sourcePos, sourceNode.nodeSize, contentSplits);
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Apply content splits to transaction - shared utility for replacing and inserting splits
|
|
311
|
-
*/
|
|
312
|
-
const applySplitsToTransaction = (tr, sourcePos, sourceNodeSize, contentSplits) => {
|
|
313
|
-
let insertPos = sourcePos;
|
|
314
|
-
contentSplits.forEach((splitNode, index) => {
|
|
315
|
-
if (index === 0) {
|
|
316
|
-
// Replace the original node with the first split
|
|
317
|
-
tr.replaceWith(sourcePos, sourcePos + sourceNodeSize, splitNode);
|
|
318
|
-
insertPos = sourcePos + splitNode.nodeSize;
|
|
319
|
-
} else {
|
|
320
|
-
// Insert additional splits after
|
|
321
|
-
tr.insert(insertPos, splitNode);
|
|
322
|
-
insertPos += splitNode.nodeSize;
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
return tr;
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Split content for codeBlock transformation, creating codeBlocks for text content
|
|
330
|
-
* and preserving unsupported blocks (like tables) separately
|
|
331
|
-
*/
|
|
332
|
-
const splitContentForCodeBlock = (sourceNode, targetNodeType, targetAttrs, schema) => {
|
|
333
|
-
var _sourceNode$attrs3;
|
|
334
|
-
const splits = [];
|
|
335
|
-
const children = sourceNode.content.content;
|
|
336
|
-
let currentTextContent = [];
|
|
337
|
-
const invalidContent = [];
|
|
338
|
-
// Handle expand title - add as first text if source is expand with title
|
|
339
|
-
if (sourceNode.type.name === 'expand' && (_sourceNode$attrs3 = sourceNode.attrs) !== null && _sourceNode$attrs3 !== void 0 && _sourceNode$attrs3.title) {
|
|
340
|
-
currentTextContent.push(sourceNode.attrs.title);
|
|
341
|
-
}
|
|
342
|
-
const flushCurrentCodeBlock = () => {
|
|
343
|
-
if (currentTextContent.length > 0) {
|
|
344
|
-
const codeText = currentTextContent.join('\n');
|
|
345
|
-
const marks = getMarksWithBreakout(sourceNode, targetNodeType);
|
|
346
|
-
const codeBlockNode = targetNodeType.create(targetAttrs, schema.text(codeText), marks);
|
|
347
|
-
splits.push(codeBlockNode);
|
|
348
|
-
currentTextContent = [];
|
|
349
|
-
}
|
|
350
|
-
};
|
|
351
|
-
const isCodeBlockCompatible = node => {
|
|
352
|
-
// Only text blocks (paragraph, heading) can be converted to codeBlock text
|
|
353
|
-
return node.isTextblock || node.type.name === 'codeBlock';
|
|
354
|
-
};
|
|
355
|
-
children.forEach(childNode => {
|
|
356
|
-
if (isCodeBlockCompatible(childNode)) {
|
|
357
|
-
// Extract text content from compatible nodes
|
|
358
|
-
if (childNode.type.name === 'codeBlock') {
|
|
359
|
-
// If it's already a codeBlock, extract its text
|
|
360
|
-
currentTextContent.push(childNode.textContent);
|
|
361
|
-
} else if (childNode.isTextblock) {
|
|
362
|
-
// Extract text from text blocks (paragraphs, headings, etc.)
|
|
363
|
-
const text = getInlineNodeTextContent(Fragment.from(childNode)).inlineTextContent;
|
|
364
|
-
if (text.trim()) {
|
|
365
|
-
currentTextContent.push(text);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
} else if (isBlockNodeForExtraction(childNode)) {
|
|
369
|
-
// Unsupported block node (table, etc.) - flush current codeBlock, add block, continue
|
|
370
|
-
flushCurrentCodeBlock();
|
|
371
|
-
splits.push(childNode);
|
|
372
|
-
} else if (isListNodeType(childNode.type)) {
|
|
373
|
-
const isTaskList = childNode.type.name === 'taskList';
|
|
374
|
-
const listItemType = isTaskList ? childNode.type.schema.nodes.taskItem : childNode.type.schema.nodes.listItem;
|
|
375
|
-
const listItems = findChildrenByType(childNode, listItemType);
|
|
376
|
-
listItems.forEach(listItem => {
|
|
377
|
-
const content = isTaskList ? Fragment.from(listItem.node) : listItem.node.content;
|
|
378
|
-
const inlineContent = getInlineNodeTextContent(content);
|
|
379
|
-
if (inlineContent.inlineTextContent.trim()) {
|
|
380
|
-
currentTextContent.push(inlineContent.inlineTextContent);
|
|
381
|
-
}
|
|
382
|
-
if (inlineContent.invalidContent.length > 0) {
|
|
383
|
-
invalidContent.push(...inlineContent.invalidContent);
|
|
384
|
-
}
|
|
385
|
-
});
|
|
386
|
-
} else {
|
|
387
|
-
// Other unsupported content - try to extract text if possible
|
|
388
|
-
const text = childNode.textContent;
|
|
389
|
-
if (text && text.trim()) {
|
|
390
|
-
currentTextContent.push(text);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
// Flush any remaining text content as a codeBlock
|
|
396
|
-
flushCurrentCodeBlock();
|
|
397
|
-
return [...splits, ...invalidContent];
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* Split content around unsupported block nodes, creating separate containers
|
|
402
|
-
* for content before and after each unsupported block
|
|
403
|
-
*/
|
|
404
|
-
const splitContentAroundUnsupportedBlocks = (sourceNode, isContentSupported, targetNodeType, targetAttrs, schema) => {
|
|
405
|
-
var _sourceNode$attrs4;
|
|
406
|
-
const splits = [];
|
|
407
|
-
const children = sourceNode.content.content;
|
|
408
|
-
let currentContainerContent = [];
|
|
409
|
-
|
|
410
|
-
// Handle expand title - add as first paragraph if source is expand with title
|
|
411
|
-
if (sourceNode.type.name === 'expand' && (_sourceNode$attrs4 = sourceNode.attrs) !== null && _sourceNode$attrs4 !== void 0 && _sourceNode$attrs4.title) {
|
|
412
|
-
const titleParagraph = schema.nodes.paragraph.create({}, schema.text(sourceNode.attrs.title));
|
|
413
|
-
currentContainerContent.push(titleParagraph);
|
|
414
|
-
}
|
|
415
|
-
const flushCurrentContainer = () => {
|
|
416
|
-
if (currentContainerContent.length > 0) {
|
|
417
|
-
const marks = getMarksWithBreakout(sourceNode, targetNodeType);
|
|
418
|
-
const containerNode = targetNodeType.create(targetAttrs, Fragment.fromArray(currentContainerContent), marks);
|
|
419
|
-
splits.push(containerNode);
|
|
420
|
-
currentContainerContent = [];
|
|
421
|
-
}
|
|
422
|
-
};
|
|
423
|
-
children.forEach(childNode => {
|
|
424
|
-
if (isContentSupported(childNode)) {
|
|
425
|
-
// Supported content - add to current container
|
|
426
|
-
currentContainerContent.push(childNode);
|
|
427
|
-
} else if (childNode.type.name === targetNodeType.name) {
|
|
428
|
-
// Same type of container merge contents
|
|
429
|
-
childNode.content.forEach(child => {
|
|
430
|
-
currentContainerContent.push(child);
|
|
431
|
-
});
|
|
432
|
-
} else if (childNode.isBlock) {
|
|
433
|
-
// Unsupported block node - flush current container, add block, continue
|
|
434
|
-
flushCurrentContainer();
|
|
435
|
-
splits.push(childNode);
|
|
436
|
-
} else {
|
|
437
|
-
// Unsupported inline content - convert to paragraph and add to container
|
|
438
|
-
const inlineContent = convertNodeToInlineContent(childNode, schema);
|
|
439
|
-
if (inlineContent.length > 0) {
|
|
440
|
-
const paragraph = schema.nodes.paragraph.create({}, Fragment.fromArray(inlineContent));
|
|
441
|
-
currentContainerContent.push(paragraph);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
// Flush any remaining container content
|
|
447
|
-
flushCurrentContainer();
|
|
448
|
-
return splits;
|
|
449
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { isListNodeType } from './utils';
|
|
2
|
-
export const getInlineNodeTextContent = sourceContent => {
|
|
3
|
-
let validTransformedContent = '';
|
|
4
|
-
const invalidContent = [];
|
|
5
|
-
if (sourceContent.content.length < 1) {
|
|
6
|
-
return {
|
|
7
|
-
inlineTextContent: '',
|
|
8
|
-
invalidContent
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
// Headings are not valid inside headings so convert heading nodes to paragraphs
|
|
12
|
-
sourceContent.forEach(node => {
|
|
13
|
-
if (['paragraph', 'heading', 'taskItem', 'codeBlock'].includes(node.type.name)) {
|
|
14
|
-
node.content.forEach(inlineNode => {
|
|
15
|
-
if (inlineNode.type.name === 'status') {
|
|
16
|
-
validTransformedContent += inlineNode.attrs.text;
|
|
17
|
-
} else {
|
|
18
|
-
validTransformedContent += `${inlineNode.textContent}`;
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
} else if (!isListNodeType(node.type)) {
|
|
22
|
-
invalidContent.push(node);
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
return {
|
|
26
|
-
inlineTextContent: validTransformedContent,
|
|
27
|
-
invalidContent
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
export const getInlineNodeTextNode = (sourceContent, schema) => {
|
|
31
|
-
const text = getInlineNodeTextContent(sourceContent).inlineTextContent;
|
|
32
|
-
return schema.text(text);
|
|
33
|
-
};
|