@seafile/sea-email-editor 0.0.12 → 0.0.14
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.
|
@@ -11,6 +11,7 @@ var _constants = require("./constants");
|
|
|
11
11
|
var _rules = _interopRequireDefault(require("./rules"));
|
|
12
12
|
var _helper = require("./helper");
|
|
13
13
|
var _dom = require("../../utils/dom");
|
|
14
|
+
var _constants2 = require("../../extension/constants");
|
|
14
15
|
const generateDefaultValue = () => {
|
|
15
16
|
return [{
|
|
16
17
|
id: _slugid.default.nice(),
|
|
@@ -107,12 +108,75 @@ const deserializeElements = function () {
|
|
|
107
108
|
// return node;
|
|
108
109
|
// };
|
|
109
110
|
|
|
111
|
+
// Split block-level children out of a paragraph-like node into siblings.
|
|
112
|
+
// Recursively processes children of any block node to flatten nested structures.
|
|
113
|
+
const flattenNestedBlockChildren = (children, parentNode) => {
|
|
114
|
+
const result = [];
|
|
115
|
+
let inlineChildren = [];
|
|
116
|
+
for (const child of children) {
|
|
117
|
+
if (child.type && _constants.TOP_LEVEL_TYPES.includes(child.type)) {
|
|
118
|
+
if (inlineChildren.length > 0) {
|
|
119
|
+
result.push({
|
|
120
|
+
id: _slugid.default.nice(),
|
|
121
|
+
type: parentNode.type,
|
|
122
|
+
children: inlineChildren
|
|
123
|
+
});
|
|
124
|
+
inlineChildren = [];
|
|
125
|
+
}
|
|
126
|
+
// If this block child itself has children with nested blocks, recurse
|
|
127
|
+
if (Array.isArray(child.children) && child.children.some(c => c.type && _constants.TOP_LEVEL_TYPES.includes(c.type))) {
|
|
128
|
+
const flattened = flattenNestedBlockChildren(child.children, {
|
|
129
|
+
type: _constants2.ElementTypes.PARAGRAPH
|
|
130
|
+
});
|
|
131
|
+
// Paragraph types cannot contain block children — spread as siblings
|
|
132
|
+
if (child.type === _constants2.ElementTypes.PARAGRAPH || child.type === _constants2.ElementTypes.P) {
|
|
133
|
+
result.push(...flattened);
|
|
134
|
+
} else {
|
|
135
|
+
result.push({
|
|
136
|
+
...child,
|
|
137
|
+
children: flattened
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
result.push(child);
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
inlineChildren.push(child);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (inlineChildren.length > 0) {
|
|
148
|
+
result.push({
|
|
149
|
+
id: _slugid.default.nice(),
|
|
150
|
+
type: parentNode.type,
|
|
151
|
+
children: inlineChildren
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
};
|
|
110
156
|
const formatElementNodes = nodes => {
|
|
111
157
|
if (nodes.length === 0) {
|
|
112
158
|
return generateDefaultValue();
|
|
113
159
|
}
|
|
114
160
|
nodes = nodes.reduce((memo, node) => {
|
|
115
161
|
if (_constants.TOP_LEVEL_TYPES.includes(node.type)) {
|
|
162
|
+
// If a paragraph-like node contains nested block-level children,
|
|
163
|
+
// split them into sibling relationships
|
|
164
|
+
if ((node.type === _constants2.ElementTypes.PARAGRAPH || node.type === _constants2.ElementTypes.P) && Array.isArray(node.children) && node.children.some(child => child.type && _constants.TOP_LEVEL_TYPES.includes(child.type))) {
|
|
165
|
+
const flattened = flattenNestedBlockChildren(node.children, node);
|
|
166
|
+
memo.push(...flattened);
|
|
167
|
+
return memo;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Blockquote may also contain nested paragraphs that need flattening
|
|
171
|
+
if (node.type === _constants2.ElementTypes.BLOCKQUOTE && Array.isArray(node.children) && node.children.some(child => child.type && _constants.TOP_LEVEL_TYPES.includes(child.type))) {
|
|
172
|
+
memo.push({
|
|
173
|
+
...node,
|
|
174
|
+
children: flattenNestedBlockChildren(node.children, {
|
|
175
|
+
type: _constants2.ElementTypes.PARAGRAPH
|
|
176
|
+
})
|
|
177
|
+
});
|
|
178
|
+
return memo;
|
|
179
|
+
}
|
|
116
180
|
memo.push(node);
|
|
117
181
|
}
|
|
118
182
|
if (node.type === _constants.LIST_ITEM) {
|
|
@@ -22,22 +22,34 @@ const getProcessor = () => {
|
|
|
22
22
|
const reconciledSlateNodes = nodes => {
|
|
23
23
|
return nodes;
|
|
24
24
|
};
|
|
25
|
+
const getLeadingLineBreakCount = content => {
|
|
26
|
+
if (typeof content !== 'string') return 0;
|
|
27
|
+
const matched = content.match(/^(?:\r\n|\r|\n)+/);
|
|
28
|
+
if (!matched) return 0;
|
|
29
|
+
return matched[0].match(/\r\n|\r|\n/g).length;
|
|
30
|
+
};
|
|
31
|
+
const generateLeadingParagraphs = count => {
|
|
32
|
+
return Array.from({
|
|
33
|
+
length: count
|
|
34
|
+
}, () => (0, _core.generateDefaultParagraph)());
|
|
35
|
+
};
|
|
25
36
|
|
|
26
37
|
// md string --> md ast --> slate ast
|
|
27
38
|
// https://github.com/syntax-tree/mdast
|
|
28
39
|
const mdStringToSlate = mdString => {
|
|
29
|
-
if (!mdString) return [(0, _core.generateDefaultParagraph)()];
|
|
30
40
|
let content = mdString;
|
|
31
41
|
if (typeof mdString === 'number') {
|
|
32
42
|
content = mdString + '';
|
|
33
43
|
}
|
|
44
|
+
if (!content) return [(0, _core.generateDefaultParagraph)()];
|
|
45
|
+
const leadingParagraphs = generateLeadingParagraphs(getLeadingLineBreakCount(content));
|
|
34
46
|
|
|
35
47
|
// md string --> md ast
|
|
36
48
|
const root = getProcessor().parse(content);
|
|
37
49
|
|
|
38
50
|
// the mdString is '\r\n'
|
|
39
51
|
if (root.children.length === 0) {
|
|
40
|
-
return [(0, _core.generateDefaultParagraph)()];
|
|
52
|
+
return leadingParagraphs.length > 0 ? leadingParagraphs : [(0, _core.generateDefaultParagraph)()];
|
|
41
53
|
}
|
|
42
54
|
|
|
43
55
|
// md ast --> slate ast
|
|
@@ -45,6 +57,6 @@ const mdStringToSlate = mdString => {
|
|
|
45
57
|
|
|
46
58
|
// Format the document
|
|
47
59
|
const nodes = reconciledSlateNodes(slateNodes);
|
|
48
|
-
return [
|
|
60
|
+
return [...leadingParagraphs, ...nodes];
|
|
49
61
|
};
|
|
50
62
|
var _default = exports.default = mdStringToSlate;
|
package/dist/utils/dom.js
CHANGED
|
@@ -142,14 +142,26 @@ const removeInvalidNodes = root => {
|
|
|
142
142
|
elementsToRemove.forEach(element => {
|
|
143
143
|
element.remove();
|
|
144
144
|
});
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
const children = Array.from(root.childNodes);
|
|
146
|
+
let prevWasBr = false;
|
|
147
|
+
children.forEach(child => {
|
|
147
148
|
if (child.nodeType === Node.COMMENT_NODE) {
|
|
148
149
|
root.removeChild(child);
|
|
149
150
|
} else if (child.nodeType === Node.ELEMENT_NODE) {
|
|
150
|
-
|
|
151
|
+
if (child.nodeName === 'BR') {
|
|
152
|
+
if (prevWasBr) {
|
|
153
|
+
root.removeChild(child);
|
|
154
|
+
} else {
|
|
155
|
+
prevWasBr = true;
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
prevWasBr = false;
|
|
159
|
+
removeInvalidNodes(child);
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
prevWasBr = false;
|
|
151
163
|
}
|
|
152
|
-
}
|
|
164
|
+
});
|
|
153
165
|
};
|
|
154
166
|
const stripCSSComments = function () {
|
|
155
167
|
let cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
package/package.json
CHANGED