@20minutes/draft-convert 3.0.3 → 3.1.1

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 (42) hide show
  1. package/README.md +2 -3
  2. package/esm/blockEntities.js +62 -65
  3. package/esm/blockInlineStyles.js +69 -92
  4. package/esm/convertFromHTML.js +447 -481
  5. package/esm/convertToHTML.js +81 -102
  6. package/esm/default/defaultBlockHTML.js +33 -30
  7. package/esm/default/defaultInlineHTML.js +16 -16
  8. package/esm/encodeBlock.js +38 -42
  9. package/esm/index.js +4 -4
  10. package/esm/util/accumulateFunction.js +7 -9
  11. package/esm/util/blockTypeObjectFunction.js +7 -9
  12. package/esm/util/getBlockTags.js +20 -20
  13. package/esm/util/getElementHTML.js +25 -29
  14. package/esm/util/getElementTagLength.js +15 -17
  15. package/esm/util/getNestedBlockTags.js +21 -26
  16. package/esm/util/parseHTML.js +15 -15
  17. package/esm/util/rangeSort.js +6 -6
  18. package/esm/util/splitReactElement.js +27 -13
  19. package/esm/util/styleObjectFunction.js +6 -8
  20. package/esm/util/updateMutation.js +57 -47
  21. package/lib/blockEntities.js +74 -69
  22. package/lib/blockInlineStyles.js +81 -96
  23. package/lib/convertFromHTML.js +502 -487
  24. package/lib/convertToHTML.js +134 -106
  25. package/lib/default/defaultBlockHTML.js +47 -35
  26. package/lib/default/defaultInlineHTML.js +29 -21
  27. package/lib/encodeBlock.js +50 -46
  28. package/lib/index.js +25 -23
  29. package/lib/util/accumulateFunction.js +13 -11
  30. package/lib/util/blockTypeObjectFunction.js +13 -11
  31. package/lib/util/getBlockTags.js +35 -27
  32. package/lib/util/getElementHTML.js +40 -36
  33. package/lib/util/getElementTagLength.js +28 -22
  34. package/lib/util/getNestedBlockTags.js +35 -32
  35. package/lib/util/parseHTML.js +22 -18
  36. package/lib/util/rangeSort.js +13 -9
  37. package/lib/util/splitReactElement.js +42 -19
  38. package/lib/util/styleObjectFunction.js +12 -10
  39. package/lib/util/updateMutation.js +64 -51
  40. package/package.json +25 -28
  41. package/dist/draft-convert.js +0 -440
  42. package/dist/draft-convert.min.js +0 -1
package/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  @20minutes/draft-convert
2
2
  ===============
3
3
 
4
- [![Node CI](https://github.com/20minutes/draft-convert/actions/workflows/ci.yml/badge.svg)](https://github.com/20minutes/draft-convert/actions/workflows/ci.yml)
5
- [![npm version](https://badge.fury.io/js/@20minutes%2Fdraft-convert.svg)](https://badge.fury.io/js/@20minutes%2Fdraft-convert)
6
- [![npm downloads](https://img.shields.io/npm/dt/@20minutes%2Fdraft-convert.svg?style=flat)](https://www.npmjs.com/package/@20minutes%2Fdraft-convert)
4
+ [![Build status](https://github.com/20minutes/draft-convert/actions/workflows/tests.yml/badge.svg)](https://github.com/20minutes/draft-convert/actions/workflows/tests.yml)
5
+ [![NPM](https://img.shields.io/npm/v/@20minutes/draft-convert)](https://www.npmjs.com/package/@20minutes/draft-convert)
7
6
 
8
7
  Forked version:
9
8
  - with deps up to date
@@ -1,67 +1,64 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
3
- 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; }
4
- 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; }
5
- import updateMutation from './util/updateMutation';
6
- import rangeSort from './util/rangeSort';
7
- import getElementHTML from './util/getElementHTML';
8
- import getElementTagLength from './util/getElementTagLength';
9
- var converter = function converter() {
10
- var entity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
11
- var originalText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
12
- return originalText;
13
- };
14
- export default (function (block, entityMap) {
15
- var entityConverter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : converter;
16
- var resultText = _toConsumableArray(block.text);
17
- var getEntityHTML = entityConverter;
18
- if (entityConverter.__isMiddleware) {
19
- getEntityHTML = entityConverter(converter);
20
- }
21
- if (Object.prototype.hasOwnProperty.call(block, 'entityRanges') && block.entityRanges.length > 0) {
22
- var entities = block.entityRanges.sort(rangeSort);
23
- var styles = block.inlineStyleRanges;
24
- var _loop = function _loop(index) {
25
- var entityRange = entities[index];
26
- var entity = entityMap[entityRange.key];
27
- var originalText = resultText.slice(entityRange.offset, entityRange.offset + entityRange.length).join('');
28
- var entityHTML = getEntityHTML(entity, originalText);
29
- var elementHTML = getElementHTML(entityHTML, originalText);
30
- var converted;
31
- if (!!elementHTML || elementHTML === '') {
32
- converted = _toConsumableArray(elementHTML);
33
- } else {
34
- converted = originalText;
35
- }
36
- var prefixLength = getElementTagLength(entityHTML, 'start');
37
- var suffixLength = getElementTagLength(entityHTML, 'end');
38
- var updateLaterMutation = function updateLaterMutation(mutation, mutationIndex) {
39
- if (mutationIndex > index || Object.prototype.hasOwnProperty.call(mutation, 'style')) {
40
- return updateMutation(mutation, entityRange.offset, entityRange.length, converted.length, prefixLength, suffixLength);
1
+ import updateMutation from './util/updateMutation.js';
2
+ import rangeSort from './util/rangeSort.js';
3
+ import getElementHTML from './util/getElementHTML.js';
4
+ import getElementTagLength from './util/getElementTagLength.js';
5
+ const converter = (entity = {}, originalText = '')=>originalText;
6
+ export default ((block, entityMap, entityConverter = converter)=>{
7
+ let resultText = [
8
+ ...block.text
9
+ ];
10
+ let getEntityHTML = entityConverter;
11
+ if (entityConverter.__isMiddleware) {
12
+ getEntityHTML = entityConverter(converter);
13
+ }
14
+ if (Object.prototype.hasOwnProperty.call(block, 'entityRanges') && block.entityRanges.length > 0) {
15
+ let entities = block.entityRanges.sort(rangeSort);
16
+ let styles = block.inlineStyleRanges;
17
+ for(let index = 0; index < entities.length; index += 1){
18
+ const entityRange = entities[index];
19
+ const entity = entityMap[entityRange.key];
20
+ const originalText = resultText.slice(entityRange.offset, entityRange.offset + entityRange.length).join('');
21
+ const entityHTML = getEntityHTML(entity, originalText);
22
+ const elementHTML = getElementHTML(entityHTML, originalText);
23
+ let converted;
24
+ if (!!elementHTML || elementHTML === '') {
25
+ converted = [
26
+ ...elementHTML
27
+ ];
28
+ } else {
29
+ converted = originalText;
30
+ }
31
+ const prefixLength = getElementTagLength(entityHTML, 'start');
32
+ const suffixLength = getElementTagLength(entityHTML, 'end');
33
+ const updateLaterMutation = (mutation, mutationIndex)=>{
34
+ if (mutationIndex > index || Object.prototype.hasOwnProperty.call(mutation, 'style')) {
35
+ return updateMutation(mutation, entityRange.offset, entityRange.length, converted.length, prefixLength, suffixLength);
36
+ }
37
+ return mutation;
38
+ };
39
+ const updateLaterMutations = (mutationList)=>mutationList.reduce((acc, mutation, mutationIndex)=>{
40
+ const updatedMutation = updateLaterMutation(mutation, mutationIndex);
41
+ if (Array.isArray(updatedMutation)) {
42
+ return acc.concat(updatedMutation);
43
+ }
44
+ return acc.concat([
45
+ updatedMutation
46
+ ]);
47
+ }, []);
48
+ entities = updateLaterMutations(entities);
49
+ styles = updateLaterMutations(styles);
50
+ resultText = [
51
+ ...resultText.slice(0, entityRange.offset),
52
+ ...converted,
53
+ ...resultText.slice(entityRange.offset + entityRange.length)
54
+ ];
41
55
  }
42
- return mutation;
43
- };
44
- var updateLaterMutations = function updateLaterMutations(mutationList) {
45
- return mutationList.reduce(function (acc, mutation, mutationIndex) {
46
- var updatedMutation = updateLaterMutation(mutation, mutationIndex);
47
- if (Array.isArray(updatedMutation)) {
48
- return acc.concat(updatedMutation);
49
- }
50
- return acc.concat([updatedMutation]);
51
- }, []);
52
- };
53
- entities = updateLaterMutations(entities);
54
- styles = updateLaterMutations(styles);
55
- resultText = [].concat(_toConsumableArray(resultText.slice(0, entityRange.offset)), _toConsumableArray(converted), _toConsumableArray(resultText.slice(entityRange.offset + entityRange.length)));
56
- };
57
- for (var index = 0; index < entities.length; index += 1) {
58
- _loop(index);
56
+ return {
57
+ ...block,
58
+ text: resultText.join(''),
59
+ inlineStyleRanges: styles,
60
+ entityRanges: entities
61
+ };
59
62
  }
60
- return _objectSpread(_objectSpread({}, block), {}, {
61
- text: resultText.join(''),
62
- inlineStyleRanges: styles,
63
- entityRanges: entities
64
- });
65
- }
66
- return block;
67
- });
63
+ return block;
64
+ });
@@ -1,98 +1,75 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
1
  import invariant from 'invariant';
3
- import styleObjectFunction from './util/styleObjectFunction';
4
- import accumulateFunction from './util/accumulateFunction';
5
- import getElementHTML from './util/getElementHTML';
6
- import rangeSort from './util/rangeSort';
7
- import defaultInlineHTML from './default/defaultInlineHTML';
8
- var subtractStyles = function subtractStyles(original, toRemove) {
9
- return original.filter(function (el) {
10
- return !toRemove.some(function (elToRemove) {
11
- return elToRemove.style === el.style;
12
- });
13
- });
2
+ import styleObjectFunction from './util/styleObjectFunction.js';
3
+ import accumulateFunction from './util/accumulateFunction.js';
4
+ import getElementHTML from './util/getElementHTML.js';
5
+ import rangeSort from './util/rangeSort.js';
6
+ import defaultInlineHTML from './default/defaultInlineHTML.js';
7
+ const subtractStyles = (original, toRemove)=>original.filter((el)=>!toRemove.some((elToRemove)=>elToRemove.style === el.style));
8
+ const popEndingStyles = (styleStack, endingStyles)=>endingStyles.reduceRight((stack, style)=>{
9
+ const styleToRemove = stack[stack.length - 1];
10
+ invariant(styleToRemove.style === style.style, `Style ${styleToRemove.style} to be removed doesn't match expected ${style.style}`);
11
+ return stack.slice(0, -1);
12
+ }, styleStack);
13
+ const characterStyles = (offset, ranges)=>ranges.filter((range)=>offset >= range.offset && offset < range.offset + range.length);
14
+ const rangeIsSubset = (firstRange, secondRange)=>{
15
+ // returns true if the second range is a subset of the first
16
+ const secondStartWithinFirst = firstRange.offset <= secondRange.offset;
17
+ const secondEndWithinFirst = firstRange.offset + firstRange.length >= secondRange.offset + secondRange.length;
18
+ return secondStartWithinFirst && secondEndWithinFirst;
14
19
  };
15
- var popEndingStyles = function popEndingStyles(styleStack, endingStyles) {
16
- return endingStyles.reduceRight(function (stack, style) {
17
- var styleToRemove = stack[stack.length - 1];
18
- invariant(styleToRemove.style === style.style, "Style ".concat(styleToRemove.style, " to be removed doesn't match expected ").concat(style.style));
19
- return stack.slice(0, -1);
20
- }, styleStack);
20
+ const latestStyleLast = (s1, s2)=>{
21
+ // make sure longer-lasting styles are added first
22
+ const s2endIndex = s2.offset + s2.length;
23
+ const s1endIndex = s1.offset + s1.length;
24
+ return s2endIndex - s1endIndex;
21
25
  };
22
- var characterStyles = function characterStyles(offset, ranges) {
23
- return ranges.filter(function (range) {
24
- return offset >= range.offset && offset < range.offset + range.length;
25
- });
26
- };
27
- var rangeIsSubset = function rangeIsSubset(firstRange, secondRange) {
28
- // returns true if the second range is a subset of the first
29
- var secondStartWithinFirst = firstRange.offset <= secondRange.offset;
30
- var secondEndWithinFirst = firstRange.offset + firstRange.length >= secondRange.offset + secondRange.length;
31
- return secondStartWithinFirst && secondEndWithinFirst;
32
- };
33
- var latestStyleLast = function latestStyleLast(s1, s2) {
34
- // make sure longer-lasting styles are added first
35
- var s2endIndex = s2.offset + s2.length;
36
- var s1endIndex = s1.offset + s1.length;
37
- return s2endIndex - s1endIndex;
38
- };
39
- var getStylesToReset = function getStylesToReset(remainingStyles, newStyles) {
40
- var i = 0;
41
- while (i < remainingStyles.length) {
42
- if (newStyles.every(rangeIsSubset.bind(null, remainingStyles[i]))) {
43
- i += 1;
44
- } else {
45
- return remainingStyles.slice(i);
26
+ const getStylesToReset = (remainingStyles, newStyles)=>{
27
+ let i = 0;
28
+ while(i < remainingStyles.length){
29
+ if (newStyles.every(rangeIsSubset.bind(null, remainingStyles[i]))) {
30
+ i += 1;
31
+ } else {
32
+ return remainingStyles.slice(i);
33
+ }
46
34
  }
47
- }
48
- return [];
49
- };
50
- var appendStartMarkup = function appendStartMarkup(inlineHTML, string, styleRange) {
51
- return string + getElementHTML(inlineHTML(styleRange.style)).start;
52
- };
53
- var prependEndMarkup = function prependEndMarkup(inlineHTML, string, styleRange) {
54
- return getElementHTML(inlineHTML(styleRange.style)).end + string;
55
- };
56
- var defaultCustomInlineHTML = function defaultCustomInlineHTML(next) {
57
- return function (style) {
58
- return next(style);
59
- };
35
+ return [];
60
36
  };
37
+ const appendStartMarkup = (inlineHTML, string, styleRange)=>string + getElementHTML(inlineHTML(styleRange.style)).start;
38
+ const prependEndMarkup = (inlineHTML, string, styleRange)=>getElementHTML(inlineHTML(styleRange.style)).end + string;
39
+ const defaultCustomInlineHTML = (next)=>(style)=>next(style);
61
40
  defaultCustomInlineHTML.__isMiddleware = true;
62
- export default (function (rawBlock) {
63
- var customInlineHTML = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultCustomInlineHTML;
64
- invariant(rawBlock !== null && rawBlock !== undefined, 'Expected raw block to be non-null');
65
- var inlineHTML;
66
- if (customInlineHTML.__isMiddleware === true) {
67
- inlineHTML = customInlineHTML(defaultInlineHTML);
68
- } else {
69
- inlineHTML = accumulateFunction(styleObjectFunction(customInlineHTML), styleObjectFunction(defaultInlineHTML));
70
- }
71
- var result = '';
72
- var styleStack = [];
73
- var sortedRanges = rawBlock.inlineStyleRanges.sort(rangeSort);
74
- var originalTextArray = _toConsumableArray(rawBlock.text);
75
- for (var i = 0; i < originalTextArray.length; i += 1) {
76
- var styles = characterStyles(i, sortedRanges);
77
- var endingStyles = subtractStyles(styleStack, styles);
78
- var newStyles = subtractStyles(styles, styleStack);
79
- var remainingStyles = subtractStyles(styleStack, endingStyles);
80
-
81
- // reset styles: look for any already existing styles that will need to
82
- // end before styles that are being added on this character. to solve this
83
- // close out those current tags and all nested children,
84
- // then open new ones nested within the new styles.
85
- var resetStyles = getStylesToReset(remainingStyles, newStyles);
86
- var openingStyles = resetStyles.concat(newStyles).sort(latestStyleLast);
87
- var openingStyleTags = openingStyles.reduce(appendStartMarkup.bind(null, inlineHTML), '');
88
- var endingStyleTags = endingStyles.concat(resetStyles).reduce(prependEndMarkup.bind(null, inlineHTML), '');
89
- result += endingStyleTags + openingStyleTags + originalTextArray[i];
90
- styleStack = popEndingStyles(styleStack, resetStyles.concat(endingStyles));
91
- styleStack = styleStack.concat(openingStyles);
92
- invariant(styleStack.length === styles.length, "Character ".concat(i, ": ").concat(styleStack.length - styles.length, " styles left on stack that should no longer be there"));
93
- }
94
- result = styleStack.reduceRight(function (res, openStyle) {
95
- return res + getElementHTML(inlineHTML(openStyle.style)).end;
96
- }, result);
97
- return result;
98
- });
41
+ export default ((rawBlock, customInlineHTML = defaultCustomInlineHTML)=>{
42
+ invariant(rawBlock !== null && rawBlock !== undefined, 'Expected raw block to be non-null');
43
+ let inlineHTML;
44
+ if (customInlineHTML.__isMiddleware === true) {
45
+ inlineHTML = customInlineHTML(defaultInlineHTML);
46
+ } else {
47
+ inlineHTML = accumulateFunction(styleObjectFunction(customInlineHTML), styleObjectFunction(defaultInlineHTML));
48
+ }
49
+ let result = '';
50
+ let styleStack = [];
51
+ const sortedRanges = rawBlock.inlineStyleRanges.sort(rangeSort);
52
+ const originalTextArray = [
53
+ ...rawBlock.text
54
+ ];
55
+ for(let i = 0; i < originalTextArray.length; i += 1){
56
+ const styles = characterStyles(i, sortedRanges);
57
+ const endingStyles = subtractStyles(styleStack, styles);
58
+ const newStyles = subtractStyles(styles, styleStack);
59
+ const remainingStyles = subtractStyles(styleStack, endingStyles);
60
+ // reset styles: look for any already existing styles that will need to
61
+ // end before styles that are being added on this character. to solve this
62
+ // close out those current tags and all nested children,
63
+ // then open new ones nested within the new styles.
64
+ const resetStyles = getStylesToReset(remainingStyles, newStyles);
65
+ const openingStyles = resetStyles.concat(newStyles).sort(latestStyleLast);
66
+ const openingStyleTags = openingStyles.reduce(appendStartMarkup.bind(null, inlineHTML), '');
67
+ const endingStyleTags = endingStyles.concat(resetStyles).reduce(prependEndMarkup.bind(null, inlineHTML), '');
68
+ result += endingStyleTags + openingStyleTags + originalTextArray[i];
69
+ styleStack = popEndingStyles(styleStack, resetStyles.concat(endingStyles));
70
+ styleStack = styleStack.concat(openingStyles);
71
+ invariant(styleStack.length === styles.length, `Character ${i}: ${styleStack.length - styles.length} styles left on stack that should no longer be there`);
72
+ }
73
+ result = styleStack.reduceRight((res, openStyle)=>res + getElementHTML(inlineHTML(openStyle.style)).end, result);
74
+ return result;
75
+ });