@seafile/sea-email-editor 0.0.10 → 0.0.11

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.
@@ -12,6 +12,7 @@
12
12
  flex: 1;
13
13
  width: 100%;
14
14
  padding: 10px;
15
+ overflow-y: scroll;
15
16
  }
16
17
 
17
18
  .sea-email-editor .sea-email-editor-content *:first-child {
@@ -75,13 +75,19 @@ const Main = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
75
75
  const [firstNode] = editor.children;
76
76
  if (!firstNode) return;
77
77
  if (focusRange && focusRange !== null && focusRange !== void 0 && focusRange.anchor) {
78
- const startOfFirstNode = _slate.Editor.start(editor, focusRange.anchor.path);
79
- const range = {
80
- anchor: startOfFirstNode,
81
- focus: startOfFirstNode
82
- };
83
- (0, _core.focusEditor)(editor, range);
84
- setTimeout(() => (0, _core.focusEditor)(editor, focusRange), 0);
78
+ try {
79
+ const startOfFirstNode = _slate.Editor.start(editor, focusRange.anchor.path);
80
+ const range = {
81
+ anchor: startOfFirstNode,
82
+ focus: startOfFirstNode
83
+ };
84
+ (0, _core.focusEditor)(editor, range);
85
+ setTimeout(() => (0, _core.focusEditor)(editor, focusRange), 0);
86
+ } catch {
87
+ focusRangeRef.current = null;
88
+ focusNode(editor);
89
+ return;
90
+ }
85
91
  focusRangeRef.current = null;
86
92
  return;
87
93
  }
@@ -10,6 +10,17 @@ var _typeOf = _interopRequireDefault(require("type-of"));
10
10
  var _constants = require("./constants");
11
11
  var _rules = _interopRequireDefault(require("./rules"));
12
12
  var _helper = require("./helper");
13
+ var _dom = require("../../utils/dom");
14
+ const generateDefaultValue = () => {
15
+ return [{
16
+ id: _slugid.default.nice(),
17
+ type: _constants.PARAGRAPH,
18
+ children: [{
19
+ text: '',
20
+ id: _slugid.default.nice()
21
+ }]
22
+ }];
23
+ };
13
24
  const cruftNewline = element => {
14
25
  return !(element.nodeName === '#text' && element.nodeValue === '\n');
15
26
  };
@@ -98,14 +109,7 @@ const deserializeElements = function () {
98
109
 
99
110
  const formatElementNodes = nodes => {
100
111
  if (nodes.length === 0) {
101
- return [{
102
- id: _slugid.default.nice(),
103
- type: _constants.PARAGRAPH,
104
- children: [{
105
- text: '',
106
- id: _slugid.default.nice()
107
- }]
108
- }];
112
+ return generateDefaultValue();
109
113
  }
110
114
  nodes = nodes.reduce((memo, node) => {
111
115
  if (_constants.TOP_LEVEL_TYPES.includes(node.type)) {
@@ -145,10 +149,9 @@ const formatElementNodes = nodes => {
145
149
  return nodes;
146
150
  };
147
151
  const deserializeHtml = html => {
148
- const parsed = new DOMParser().parseFromString(html.replace('\n', '').replace(' ', ''), 'text/html');
149
- const fragment = parsed.body;
150
- fragment.querySelectorAll('style, title').forEach(el => el.remove());
151
- const children = Array.from(fragment.childNodes);
152
+ const HTMLBody = (0, _dom.parseAndCleanHTML)(html);
153
+ if (!HTMLBody) return generateDefaultValue();
154
+ const children = Array.from(HTMLBody.childNodes);
152
155
  let nodes = [];
153
156
  nodes = deserializeElements(children, true);
154
157
  nodes = formatElementNodes(nodes);
@@ -13,7 +13,7 @@ const paragraphRule = (element, parseChild) => {
13
13
  nodeName,
14
14
  childNodes
15
15
  } = element;
16
- if (nodeName === 'DIV' && element.parentElement.nodeName !== 'LI') {
16
+ if ((nodeName === 'DIV' || nodeName === 'ARTICLE') && element.parentElement.nodeName !== 'LI') {
17
17
  if (childNodes.length === 0) {
18
18
  const node = {
19
19
  id: _slugid.default.nice(),
@@ -25,10 +25,14 @@ const paragraphRule = (element, parseChild) => {
25
25
  };
26
26
  return (0, _helper.mergeElementOther2SlateNode)(element, node);
27
27
  }
28
+ const children = parseChild(childNodes);
28
29
  const node = {
29
30
  id: _slugid.default.nice(),
30
31
  type: _constants.PARAGRAPH,
31
- children: parseChild(childNodes)
32
+ children: children.length === 0 ? [{
33
+ id: _slugid.default.nice(),
34
+ text: ''
35
+ }] : children
32
36
  };
33
37
  return (0, _helper.mergeElementOther2SlateNode)(element, node);
34
38
  }
@@ -33,8 +33,10 @@ const isUrl = url => {
33
33
  // Check document is empty or only contains void nodes
34
34
  exports.isUrl = isUrl;
35
35
  const isDocumentEmpty = editor => {
36
- const document = editor.children;
36
+ const document = editor === null || editor === void 0 ? void 0 : editor.children;
37
+ if (!Array.isArray(document) || document.length === 0) return true;
37
38
  const [firstChildNode] = document;
39
+ if (!firstChildNode || !Array.isArray(firstChildNode.children)) return true;
38
40
  // Check if document has only one block node
39
41
  const isWrapperEmpty = document.length === 1 && _slate.Node.string(firstChildNode).length === 0;
40
42
  if (!isWrapperEmpty) return false;
package/dist/utils/dom.js CHANGED
@@ -6,9 +6,10 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.addClass = addClass;
7
7
  exports.getTarget = exports.getEventClassName = exports.getDataAttr = exports.canUseDOM = exports.addClassName = void 0;
8
8
  exports.hasClass = hasClass;
9
- exports.isNearBottom = exports.isInputOrEditorActive = void 0;
9
+ exports.parseAndCleanHTML = exports.isNearBottom = exports.isInputOrEditorActive = void 0;
10
10
  exports.removeClass = removeClass;
11
11
  exports.removeClassName = void 0;
12
+ var _typeDetection = require("./type-detection");
12
13
  const canUseDOM = exports.canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
13
14
  const getEventClassName = e => {
14
15
  // svg mouseEvent event.target.className is an object
@@ -130,4 +131,318 @@ const isNearBottom = function (element) {
130
131
  const distanceToBottom = scrollHeight - (scrollTop + clientHeight);
131
132
  return distanceToBottom <= threshold;
132
133
  };
133
- exports.isNearBottom = isNearBottom;
134
+ exports.isNearBottom = isNearBottom;
135
+ const cleanWhiteSpace = function () {
136
+ let html = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
137
+ return html.replace(/\s*[\n\t\r]+\s*/g, '');
138
+ };
139
+ const removeInvalidNodes = root => {
140
+ if (!root || !root.querySelectorAll) return;
141
+ const elementsToRemove = root.querySelectorAll('style, title, script, link[rel="stylesheet"]');
142
+ elementsToRemove.forEach(element => {
143
+ element.remove();
144
+ });
145
+ for (let i = root.childNodes.length - 1; i >= 0; i--) {
146
+ const child = root.childNodes[i];
147
+ if (child.nodeType === Node.COMMENT_NODE) {
148
+ root.removeChild(child);
149
+ } else if (child.nodeType === Node.ELEMENT_NODE) {
150
+ removeInvalidNodes(child);
151
+ }
152
+ }
153
+ };
154
+ const stripCSSComments = function () {
155
+ let cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
156
+ return cssText.replace(/\/\*[\s\S]*?\*\//g, '');
157
+ };
158
+ const splitByDelimiter = function () {
159
+ let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
160
+ let delimiter = arguments.length > 1 ? arguments[1] : undefined;
161
+ const parts = [];
162
+ let buffer = '';
163
+ let quote = '';
164
+ let roundDepth = 0;
165
+ let squareDepth = 0;
166
+ for (let i = 0; i < value.length; i++) {
167
+ const char = value[i];
168
+ const prevChar = value[i - 1];
169
+ if (quote) {
170
+ buffer += char;
171
+ if (char === quote && prevChar !== '\\') {
172
+ quote = '';
173
+ }
174
+ continue;
175
+ }
176
+ if (char === '"' || char === '\'') {
177
+ quote = char;
178
+ buffer += char;
179
+ continue;
180
+ }
181
+ if (char === '(') {
182
+ roundDepth += 1;
183
+ buffer += char;
184
+ continue;
185
+ }
186
+ if (char === ')') {
187
+ roundDepth = Math.max(0, roundDepth - 1);
188
+ buffer += char;
189
+ continue;
190
+ }
191
+ if (char === '[') {
192
+ squareDepth += 1;
193
+ buffer += char;
194
+ continue;
195
+ }
196
+ if (char === ']') {
197
+ squareDepth = Math.max(0, squareDepth - 1);
198
+ buffer += char;
199
+ continue;
200
+ }
201
+ if (char === delimiter && roundDepth === 0 && squareDepth === 0) {
202
+ parts.push(buffer);
203
+ buffer = '';
204
+ continue;
205
+ }
206
+ buffer += char;
207
+ }
208
+ if (buffer) {
209
+ parts.push(buffer);
210
+ }
211
+ return parts;
212
+ };
213
+ const findMatchingParenIndex = function () {
214
+ let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
215
+ let startIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
216
+ let quote = '';
217
+ let depth = 0;
218
+ for (let i = startIndex; i < value.length; i++) {
219
+ const char = value[i];
220
+ const prevChar = value[i - 1];
221
+ if (quote) {
222
+ if (char === quote && prevChar !== '\\') {
223
+ quote = '';
224
+ }
225
+ continue;
226
+ }
227
+ if (char === '"' || char === '\'') {
228
+ quote = char;
229
+ continue;
230
+ }
231
+ if (char === '(') {
232
+ depth += 1;
233
+ continue;
234
+ }
235
+ if (char === ')') {
236
+ depth -= 1;
237
+ if (depth === 0) {
238
+ return i;
239
+ }
240
+ }
241
+ }
242
+ return -1;
243
+ };
244
+ const getInlineStyleState = element => {
245
+ const properties = [];
246
+ for (let i = 0; i < element.style.length; i++) {
247
+ properties.push(element.style[i]);
248
+ }
249
+ return properties.reduce((state, property) => {
250
+ state[property] = {
251
+ value: element.style.getPropertyValue(property),
252
+ priority: element.style.getPropertyPriority(property),
253
+ specificity: 1000,
254
+ order: Number.MAX_SAFE_INTEGER
255
+ };
256
+ return state;
257
+ }, {});
258
+ };
259
+ const compareStylePriority = (currentStyle, nextStyle) => {
260
+ if (!currentStyle) return true;
261
+ const currentImportant = currentStyle.priority === 'important';
262
+ const nextImportant = nextStyle.priority === 'important';
263
+ if (currentImportant !== nextImportant) {
264
+ return nextImportant;
265
+ }
266
+ if (currentStyle.specificity !== nextStyle.specificity) {
267
+ return nextStyle.specificity > currentStyle.specificity;
268
+ }
269
+ return nextStyle.order >= currentStyle.order;
270
+ };
271
+ const applyStyleDeclarations = function (element) {
272
+ let declarationText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
273
+ let metadata = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
274
+ let styleStateMap = arguments.length > 3 ? arguments[3] : undefined;
275
+ if (!element || !declarationText) return;
276
+ const styleState = styleStateMap.get(element) || getInlineStyleState(element);
277
+ splitByDelimiter(declarationText, ';').forEach(declaration => {
278
+ const separatorIndex = declaration.indexOf(':');
279
+ if (separatorIndex < 0) return;
280
+ const property = declaration.slice(0, separatorIndex).trim();
281
+ let value = declaration.slice(separatorIndex + 1).trim();
282
+ if (!property || !value) return;
283
+ let priority = '';
284
+ if (/!important\s*$/i.test(value)) {
285
+ priority = 'important';
286
+ value = value.replace(/\s*!important\s*$/i, '').trim();
287
+ }
288
+ const nextStyle = {
289
+ value,
290
+ priority,
291
+ specificity: metadata.specificity || 0,
292
+ order: metadata.order || 0
293
+ };
294
+ if (compareStylePriority(styleState[property], nextStyle)) {
295
+ styleState[property] = nextStyle;
296
+ }
297
+ });
298
+ styleStateMap.set(element, styleState);
299
+ };
300
+ const calculateSelectorSpecificity = function () {
301
+ let selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
302
+ const pseudoFunctionNames = ['is', 'not', 'has', 'where'];
303
+ let normalizedSelector = selector.replace(/::[\w-]+/g, ' ');
304
+ let pseudoSpecificity = 0;
305
+ pseudoFunctionNames.forEach(pseudoName => {
306
+ const token = `:${pseudoName}(`;
307
+ let tokenIndex = normalizedSelector.indexOf(token);
308
+ while (tokenIndex > -1) {
309
+ const contentStartIndex = tokenIndex + token.length - 1;
310
+ const contentEndIndex = findMatchingParenIndex(normalizedSelector, contentStartIndex);
311
+ if (contentEndIndex < 0) break;
312
+ const content = normalizedSelector.slice(contentStartIndex + 1, contentEndIndex);
313
+ if (pseudoName !== 'where') {
314
+ const specificityList = splitByDelimiter(content, ',').map(item => calculateSelectorSpecificity(item.trim())).filter(Boolean);
315
+ pseudoSpecificity += specificityList.length > 0 ? Math.max(...specificityList) : 0;
316
+ }
317
+ normalizedSelector = normalizedSelector.slice(0, tokenIndex) + ' ' + normalizedSelector.slice(contentEndIndex + 1);
318
+ tokenIndex = normalizedSelector.indexOf(token);
319
+ }
320
+ });
321
+ const idCount = (normalizedSelector.match(/#[\w-]+/g) || []).length;
322
+ const classCount = (normalizedSelector.match(/\.[\w-]+/g) || []).length;
323
+ const attributeCount = (normalizedSelector.match(/\[[^\]]+\]/g) || []).length;
324
+ const pseudoClassCount = (normalizedSelector.match(/:(?!:)[\w-]+(?:\([^)]*\))?/g) || []).length;
325
+ const elementSelector = normalizedSelector.replace(/#[\w-]+/g, ' ').replace(/\.[\w-]+/g, ' ').replace(/\[[^\]]+\]/g, ' ').replace(/:(?!:)[\w-]+(?:\([^)]*\))?/g, ' ').replace(/[>+~*]/g, ' ');
326
+ const typeCount = (elementSelector.match(/(^|\s)[a-zA-Z][\w-]*/g) || []).length;
327
+ return pseudoSpecificity + idCount * 100 + (classCount + attributeCount + pseudoClassCount) * 10 + typeCount;
328
+ };
329
+ const applyCollectedStyles = styleStateMap => {
330
+ styleStateMap.forEach((styleState, element) => {
331
+ const properties = Object.keys(styleState);
332
+ element.removeAttribute('style');
333
+ properties.forEach(property => {
334
+ const item = styleState[property];
335
+ if (!(item !== null && item !== void 0 && item.value)) return;
336
+ element.style.setProperty(property, item.value, item.priority);
337
+ });
338
+ });
339
+ };
340
+ const splitCSSBlocks = function () {
341
+ let cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
342
+ const blocks = [];
343
+ let buffer = '';
344
+ let depth = 0;
345
+ let quote = '';
346
+ let roundDepth = 0;
347
+ for (let i = 0; i < cssText.length; i++) {
348
+ const char = cssText[i];
349
+ const prevChar = cssText[i - 1];
350
+ buffer += char;
351
+ if (quote) {
352
+ if (char === quote && prevChar !== '\\') {
353
+ quote = '';
354
+ }
355
+ continue;
356
+ }
357
+ if (char === '"' || char === '\'') {
358
+ quote = char;
359
+ continue;
360
+ }
361
+ if (char === '(') {
362
+ roundDepth += 1;
363
+ continue;
364
+ }
365
+ if (char === ')') {
366
+ roundDepth = Math.max(0, roundDepth - 1);
367
+ continue;
368
+ }
369
+ if (roundDepth > 0) {
370
+ continue;
371
+ }
372
+ if (char === '{') {
373
+ depth += 1;
374
+ continue;
375
+ }
376
+ if (char === '}') {
377
+ depth -= 1;
378
+ if (depth === 0) {
379
+ blocks.push(buffer.trim());
380
+ buffer = '';
381
+ }
382
+ }
383
+ }
384
+ return blocks;
385
+ };
386
+ const inlineCSSRules = function (root) {
387
+ let cssText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
388
+ let styleStateMap = arguments.length > 2 ? arguments[2] : undefined;
389
+ let context = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
390
+ order: 0
391
+ };
392
+ const normalizedCSS = stripCSSComments(cssText).trim();
393
+ if (!normalizedCSS) return;
394
+ splitCSSBlocks(normalizedCSS).forEach(block => {
395
+ const startIndex = block.indexOf('{');
396
+ const endIndex = block.lastIndexOf('}');
397
+ if (startIndex < 0 || endIndex < 0 || endIndex <= startIndex) return;
398
+ const selectorText = block.slice(0, startIndex).trim();
399
+ const declarationText = block.slice(startIndex + 1, endIndex).trim();
400
+ if (!selectorText || !declarationText) return;
401
+ if (selectorText.startsWith('@')) {
402
+ if (declarationText.includes('{')) {
403
+ inlineCSSRules(root, declarationText, styleStateMap, context);
404
+ }
405
+ return;
406
+ }
407
+ splitByDelimiter(selectorText, ',').forEach(selector => {
408
+ const normalizedSelector = selector.trim();
409
+ if (!normalizedSelector) return;
410
+ try {
411
+ const metadata = {
412
+ specificity: calculateSelectorSpecificity(normalizedSelector),
413
+ order: context.order++
414
+ };
415
+ root.querySelectorAll(normalizedSelector).forEach(element => {
416
+ applyStyleDeclarations(element, declarationText, metadata, styleStateMap);
417
+ });
418
+ } catch {
419
+ // Ignore selectors that cannot be queried, such as pseudo-elements.
420
+ }
421
+ });
422
+ });
423
+ };
424
+ const convertStyleToInlineStyle = parsed => {
425
+ const styleStateMap = new Map();
426
+ const context = {
427
+ order: 0
428
+ };
429
+ parsed.querySelectorAll('style').forEach(styleElement => {
430
+ inlineCSSRules(parsed, styleElement.textContent || '', styleStateMap, context);
431
+ });
432
+ applyCollectedStyles(styleStateMap);
433
+ };
434
+ const parseAndCleanHTML = function () {
435
+ let html = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
436
+ if (!html) return null;
437
+ if ((0, _typeDetection.isNumber)(html)) {
438
+ html = String(html);
439
+ }
440
+ if (!(0, _typeDetection.isString)(html)) return null;
441
+ const sanitizedHTML = cleanWhiteSpace(html);
442
+ const parsed = new DOMParser().parseFromString(sanitizedHTML, 'text/html');
443
+ convertStyleToInlineStyle(parsed);
444
+ const body = parsed.body;
445
+ removeInvalidNodes(body);
446
+ return body;
447
+ };
448
+ exports.parseAndCleanHTML = parseAndCleanHTML;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sea-email-editor",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "A Slate-based rich email editor with HTML, markdown, tables, images, links, and block plugins.",
5
5
  "main": "dist/index.js",
6
6
  "dependencies": {