@quadrats/common 0.6.7 → 0.7.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 (85) hide show
  1. package/blockquote/createBlockquote.d.ts +1 -1
  2. package/blockquote/createBlockquote.js +33 -33
  3. package/blockquote/index.cjs.js +32 -34
  4. package/bold/createBold.d.ts +2 -1
  5. package/bold/createBold.js +6 -4
  6. package/bold/index.cjs.js +6 -6
  7. package/divider/createDivider.d.ts +1 -1
  8. package/divider/createDivider.js +29 -29
  9. package/divider/index.cjs.js +29 -31
  10. package/embed/createEmbed.js +40 -40
  11. package/embed/deserializeEmbedElementToData.js +6 -6
  12. package/embed/index.cjs.js +54 -56
  13. package/embed/serializeEmbedCode.js +8 -8
  14. package/embed/strategies/facebook/index.cjs.js +45 -47
  15. package/embed/strategies/facebook/index.d.ts +5 -5
  16. package/embed/strategies/facebook/index.js +45 -45
  17. package/embed/strategies/instagram/index.cjs.js +29 -31
  18. package/embed/strategies/instagram/index.d.ts +3 -3
  19. package/embed/strategies/instagram/index.js +29 -29
  20. package/embed/strategies/podcast-apple/index.cjs.js +16 -18
  21. package/embed/strategies/podcast-apple/index.d.ts +4 -4
  22. package/embed/strategies/podcast-apple/index.js +16 -16
  23. package/embed/strategies/spotify/index.cjs.js +14 -16
  24. package/embed/strategies/spotify/index.d.ts +4 -4
  25. package/embed/strategies/spotify/index.js +14 -14
  26. package/embed/strategies/twitter/index.cjs.js +41 -43
  27. package/embed/strategies/twitter/index.d.ts +4 -4
  28. package/embed/strategies/twitter/index.js +41 -41
  29. package/embed/strategies/vimeo/index.cjs.js +14 -16
  30. package/embed/strategies/vimeo/index.d.ts +3 -3
  31. package/embed/strategies/vimeo/index.js +14 -14
  32. package/embed/strategies/youtube/index.cjs.js +14 -16
  33. package/embed/strategies/youtube/index.d.ts +3 -3
  34. package/embed/strategies/youtube/index.js +14 -14
  35. package/embed/typings.d.ts +1 -1
  36. package/file-uploader/_virtual/_tslib.js +3 -1
  37. package/file-uploader/createFileUploader.js +76 -76
  38. package/file-uploader/getFilesFromInput.js +24 -24
  39. package/file-uploader/index.cjs.js +102 -102
  40. package/file-uploader/typings.d.ts +6 -6
  41. package/footnote/createFootnote.d.ts +1 -1
  42. package/footnote/createFootnote.js +65 -65
  43. package/footnote/index.cjs.js +65 -67
  44. package/footnote/typings.d.ts +1 -1
  45. package/heading/constants.js +1 -1
  46. package/heading/createHeading.d.ts +1 -1
  47. package/heading/createHeading.js +45 -45
  48. package/heading/index.cjs.js +46 -48
  49. package/heading/typings.d.ts +1 -1
  50. package/highlight/createHighlight.d.ts +2 -1
  51. package/highlight/createHighlight.js +6 -4
  52. package/highlight/index.cjs.js +6 -6
  53. package/image/constants.js +4 -4
  54. package/image/createImage.js +195 -195
  55. package/image/getImageElementCommonProps.js +6 -6
  56. package/image/getImageFigureElementCommonProps.js +5 -5
  57. package/image/index.cjs.js +211 -213
  58. package/image/isHostingNotRequired.js +2 -2
  59. package/image/typings.d.ts +8 -8
  60. package/input-block/createInputBlock.js +37 -37
  61. package/input-block/index.cjs.js +37 -39
  62. package/input-widget/typings.d.ts +1 -1
  63. package/italic/createItalic.d.ts +2 -1
  64. package/italic/createItalic.js +6 -4
  65. package/italic/index.cjs.js +6 -6
  66. package/link/createLink.js +147 -147
  67. package/link/index.cjs.js +146 -148
  68. package/link/typings.d.ts +2 -2
  69. package/list/constants.js +4 -4
  70. package/list/createList.js +185 -185
  71. package/list/index.cjs.js +188 -190
  72. package/list/typings.d.ts +4 -4
  73. package/package.json +4 -4
  74. package/read-more/createReadMore.d.ts +1 -1
  75. package/read-more/createReadMore.js +51 -51
  76. package/read-more/index.cjs.js +50 -52
  77. package/strikethrough/createStrikethrough.d.ts +2 -1
  78. package/strikethrough/createStrikethrough.js +6 -4
  79. package/strikethrough/index.cjs.js +6 -6
  80. package/toggle-mark/createToggleMarkCreator.d.ts +4 -4
  81. package/toggle-mark/createToggleMarkCreator.js +36 -36
  82. package/toggle-mark/index.cjs.js +36 -38
  83. package/underline/createUnderline.d.ts +2 -1
  84. package/underline/createUnderline.js +6 -4
  85. package/underline/index.cjs.js +6 -6
@@ -1,201 +1,201 @@
1
1
  import { isImageUrl } from '@quadrats/utils';
2
- import { getAboveByTypes, Editor, Transforms, createParagraphElement, Element, isNodesTypeIn, normalizeVoidElementChildren, normalizeOnlyInlineOrTextInChildren, Range } from '@quadrats/core';
2
+ import { Editor, Transforms, createParagraphElement, Element, normalizeVoidElementChildren, normalizeOnlyInlineOrTextInChildren, getAboveByTypes, isNodesTypeIn, Range } from '@quadrats/core';
3
3
  import { IMAGE_TYPES } from './constants.js';
4
4
 
5
- function resolveSizeSteps(steps) {
6
- let sortedSteps = steps.filter(step => step > 0 && step < 100).sort();
7
- if (!sortedSteps.includes(100)) {
8
- sortedSteps = [...sortedSteps, 100];
9
- }
10
- return sortedSteps;
11
- }
12
- function createImage(options = {}) {
13
- const { types: typesOptions, hostingResolvers, sizeSteps: unresolvedSizeSteps, isImageUrl: isImageUrl$1 = isImageUrl, } = options;
14
- const types = Object.assign(Object.assign({}, IMAGE_TYPES), typesOptions);
15
- const sizeSteps = unresolvedSizeSteps && resolveSizeSteps(unresolvedSizeSteps);
16
- const getAboveImageFigure = (editor, options) => getAboveByTypes(editor, [types.figure], options);
17
- const getAboveImageCaption = (editor, options) => getAboveByTypes(editor, [types.caption], options);
18
- const isNodesInImage = (editor, options) => isNodesTypeIn(editor, [types.image], options);
19
- const isSelectionInImage = editor => isNodesInImage(editor);
20
- const isSelectionInImageCaption = editor => isNodesTypeIn(editor, [types.caption]);
21
- const isCollapsedOnImage = editor => !!editor.selection && Range.isCollapsed(editor.selection) && isSelectionInImage(editor);
22
- const previousNodeIsCaption = (editor) => {
23
- const previous = Editor.previous(editor);
24
- if (!previous)
25
- return false;
26
- const [, previousLocation] = previous;
27
- const previousWrapper = Editor.parent(editor, previousLocation);
28
- if (!previousWrapper)
29
- return false;
30
- const [previousWrapperNode] = previousWrapper;
31
- return previousWrapperNode.type === types.caption;
32
- };
33
- const createImageElement = (src, hosting) => {
34
- const imageElement = {
35
- type: types.image,
36
- src,
37
- hosting,
38
- children: [{ text: '' }],
39
- };
40
- const captionElement = {
41
- type: types.caption,
42
- children: [{ text: '' }],
43
- };
44
- return {
45
- type: types.figure,
46
- width: 100,
47
- children: [imageElement, captionElement],
48
- };
49
- };
50
- const insertImage = (editor, src, options = {}) => {
51
- const { hosting, at } = options;
52
- const imageElement = createImageElement(src, hosting);
53
- Transforms.insertNodes(editor, [imageElement, createParagraphElement()], { at });
54
- };
55
- const adjustWidthPercentage = (percentage) => {
56
- if (percentage < 0) {
57
- percentage = 0;
58
- }
59
- else if (percentage > 100) {
60
- percentage = 100;
61
- }
62
- if (!sizeSteps) {
63
- return percentage;
64
- }
65
- const lowerIndex = sizeSteps.findIndex(step => step >= percentage) - 1;
66
- const upperIndex = lowerIndex + 1;
67
- if (lowerIndex < 0) {
68
- return sizeSteps[0];
69
- }
70
- const lower = sizeSteps[lowerIndex];
71
- if (upperIndex === sizeSteps.length) {
72
- return lower;
73
- }
74
- const upper = sizeSteps[upperIndex];
75
- return Math.abs(percentage - lower) <= Math.abs(upper - percentage) ? lower : upper;
76
- };
77
- const resizeImage = (editor, [, path], width) => {
78
- const [figure, figurePath] = getAboveImageFigure(editor, { at: path }) || [];
79
- if (figure) {
80
- width = adjustWidthPercentage(width);
81
- if (figure.width !== width) {
82
- const resizedElement = Object.assign(Object.assign({}, figure), { width });
83
- Transforms.setNodes(editor, resizedElement, {
84
- at: figurePath,
85
- });
86
- }
87
- }
88
- };
89
- return {
90
- types,
91
- hostingResolvers,
92
- sizeSteps,
93
- isImageUrl: isImageUrl$1,
94
- getAboveImageFigure,
95
- getAboveImageCaption,
96
- isSelectionInImage,
97
- isSelectionInImageCaption,
98
- isCollapsedOnImage,
99
- createImageElement,
100
- insertImage,
101
- resizeImage,
102
- with(editor) {
103
- const { deleteBackward, deleteForward, insertBreak, isVoid, normalizeNode, } = editor;
104
- const deleteCollapsed = (origin, isEdgeMethodName) => {
105
- const { selection } = editor;
106
- /**
107
- * Avoid from delete fragment outside caption while collapsed in caption.
108
- */
109
- if (selection && Range.isCollapsed(selection)) {
110
- const [, captionPath] = getAboveImageCaption(editor) || [];
111
- if ((captionPath && Editor[isEdgeMethodName](editor, selection.focus, captionPath))) {
112
- return;
113
- }
114
- // Remove image element when backwards from external
115
- if (!captionPath && previousNodeIsCaption(editor)) {
116
- const previous = Editor.previous(editor);
117
- if (!previous)
118
- return false;
119
- const [, previousLocation] = previous;
120
- const previousWrapper = Editor.parent(editor, previousLocation);
121
- if (!previousWrapper)
122
- return false;
123
- const [previousWrapperNode, wrapperLocation] = previousWrapper;
124
- if (previousWrapperNode.type === types.caption) {
125
- const imageContainer = Editor.parent(editor, wrapperLocation);
126
- if (imageContainer) {
127
- const [imageElement, imageLocation] = imageContainer;
128
- if (imageElement.type === types.figure) {
129
- Transforms.removeNodes(editor, { at: imageLocation });
130
- }
131
- }
132
- }
133
- return;
134
- }
135
- }
136
- origin();
137
- };
138
- editor.deleteBackward = (unit) => {
139
- deleteCollapsed(() => deleteBackward(unit), 'isStart');
140
- };
141
- editor.deleteForward = (unit) => {
142
- deleteCollapsed(() => deleteForward(unit), 'isEnd');
143
- };
144
- editor.insertBreak = () => {
145
- const captionEntry = getAboveImageCaption(editor);
146
- /**
147
- * Avoid from splitting children of caption.
148
- */
149
- if (captionEntry) {
150
- const [, captionLocation] = captionEntry;
151
- const imageEntry = Editor.parent(editor, captionLocation);
152
- if (imageEntry) {
153
- const [imageElement, imagePosition] = imageEntry;
154
- if (imageElement.type === types.figure) {
155
- Transforms.insertNodes(editor, createParagraphElement(), { at: Editor.after(editor, imagePosition) });
156
- Transforms.move(editor);
157
- }
158
- }
159
- return;
160
- }
161
- insertBreak();
162
- };
163
- editor.isVoid = element => element.type === types.image || isVoid(element);
164
- editor.normalizeNode = (entry) => {
165
- const [node, path] = entry;
166
- if (Element.isElement(node)) {
167
- if (node.type === types.figure) {
168
- if (!isNodesInImage(editor, { at: path })) {
169
- Transforms.removeNodes(editor, { at: path });
170
- return;
171
- }
172
- }
173
- else if (node.type === types.image) {
174
- const { src, width, hosting } = node;
175
- if (typeof src !== 'string'
176
- || !(width == null || typeof width === 'number')
177
- || !(hosting == null || typeof hosting === 'string')) {
178
- const [, figurePath] = getAboveImageFigure(editor, { at: path }) || [];
179
- if (figurePath) {
180
- Transforms.removeNodes(editor, { at: figurePath });
181
- return;
182
- }
183
- }
184
- if (normalizeVoidElementChildren(editor, [node, path])) {
185
- return;
186
- }
187
- }
188
- else if (node.type === types.caption) {
189
- if (normalizeOnlyInlineOrTextInChildren(editor, entry)) {
190
- return;
191
- }
192
- }
193
- }
194
- normalizeNode(entry);
195
- };
196
- return editor;
197
- },
198
- };
5
+ function resolveSizeSteps(steps) {
6
+ let sortedSteps = steps.filter(step => step > 0 && step < 100).sort();
7
+ if (!sortedSteps.includes(100)) {
8
+ sortedSteps = [...sortedSteps, 100];
9
+ }
10
+ return sortedSteps;
11
+ }
12
+ function createImage(options = {}) {
13
+ const { types: typesOptions, hostingResolvers, sizeSteps: unresolvedSizeSteps, isImageUrl: isImageUrl$1 = isImageUrl, } = options;
14
+ const types = Object.assign(Object.assign({}, IMAGE_TYPES), typesOptions);
15
+ const sizeSteps = unresolvedSizeSteps && resolveSizeSteps(unresolvedSizeSteps);
16
+ const getAboveImageFigure = (editor, options) => getAboveByTypes(editor, [types.figure], options);
17
+ const getAboveImageCaption = (editor, options) => getAboveByTypes(editor, [types.caption], options);
18
+ const isNodesInImage = (editor, options) => isNodesTypeIn(editor, [types.image], options);
19
+ const isSelectionInImage = editor => isNodesInImage(editor);
20
+ const isSelectionInImageCaption = editor => isNodesTypeIn(editor, [types.caption]);
21
+ const isCollapsedOnImage = editor => !!editor.selection && Range.isCollapsed(editor.selection) && isSelectionInImage(editor);
22
+ const previousNodeIsCaption = (editor) => {
23
+ const previous = Editor.previous(editor);
24
+ if (!previous)
25
+ return false;
26
+ const [, previousLocation] = previous;
27
+ const previousWrapper = Editor.parent(editor, previousLocation);
28
+ if (!previousWrapper)
29
+ return false;
30
+ const [previousWrapperNode] = previousWrapper;
31
+ return previousWrapperNode.type === types.caption;
32
+ };
33
+ const createImageElement = (src, hosting) => {
34
+ const imageElement = {
35
+ type: types.image,
36
+ src,
37
+ hosting,
38
+ children: [{ text: '' }],
39
+ };
40
+ const captionElement = {
41
+ type: types.caption,
42
+ children: [{ text: '' }],
43
+ };
44
+ return {
45
+ type: types.figure,
46
+ width: 100,
47
+ children: [imageElement, captionElement],
48
+ };
49
+ };
50
+ const insertImage = (editor, src, options = {}) => {
51
+ const { hosting, at } = options;
52
+ const imageElement = createImageElement(src, hosting);
53
+ Transforms.insertNodes(editor, [imageElement, createParagraphElement()], { at });
54
+ };
55
+ const adjustWidthPercentage = (percentage) => {
56
+ if (percentage < 0) {
57
+ percentage = 0;
58
+ }
59
+ else if (percentage > 100) {
60
+ percentage = 100;
61
+ }
62
+ if (!sizeSteps) {
63
+ return percentage;
64
+ }
65
+ const lowerIndex = sizeSteps.findIndex(step => step >= percentage) - 1;
66
+ const upperIndex = lowerIndex + 1;
67
+ if (lowerIndex < 0) {
68
+ return sizeSteps[0];
69
+ }
70
+ const lower = sizeSteps[lowerIndex];
71
+ if (upperIndex === sizeSteps.length) {
72
+ return lower;
73
+ }
74
+ const upper = sizeSteps[upperIndex];
75
+ return Math.abs(percentage - lower) <= Math.abs(upper - percentage) ? lower : upper;
76
+ };
77
+ const resizeImage = (editor, [, path], width) => {
78
+ const [figure, figurePath] = getAboveImageFigure(editor, { at: path }) || [];
79
+ if (figure) {
80
+ width = adjustWidthPercentage(width);
81
+ if (figure.width !== width) {
82
+ const resizedElement = Object.assign(Object.assign({}, figure), { width });
83
+ Transforms.setNodes(editor, resizedElement, {
84
+ at: figurePath,
85
+ });
86
+ }
87
+ }
88
+ };
89
+ return {
90
+ types,
91
+ hostingResolvers,
92
+ sizeSteps,
93
+ isImageUrl: isImageUrl$1,
94
+ getAboveImageFigure,
95
+ getAboveImageCaption,
96
+ isSelectionInImage,
97
+ isSelectionInImageCaption,
98
+ isCollapsedOnImage,
99
+ createImageElement,
100
+ insertImage,
101
+ resizeImage,
102
+ with(editor) {
103
+ const { deleteBackward, deleteForward, insertBreak, isVoid, normalizeNode, } = editor;
104
+ const deleteCollapsed = (origin, isEdgeMethodName) => {
105
+ const { selection } = editor;
106
+ /**
107
+ * Avoid from delete fragment outside caption while collapsed in caption.
108
+ */
109
+ if (selection && Range.isCollapsed(selection)) {
110
+ const [, captionPath] = getAboveImageCaption(editor) || [];
111
+ if ((captionPath && Editor[isEdgeMethodName](editor, selection.focus, captionPath))) {
112
+ return;
113
+ }
114
+ // Remove image element when backwards from external
115
+ if (!captionPath && previousNodeIsCaption(editor)) {
116
+ const previous = Editor.previous(editor);
117
+ if (!previous)
118
+ return false;
119
+ const [, previousLocation] = previous;
120
+ const previousWrapper = Editor.parent(editor, previousLocation);
121
+ if (!previousWrapper)
122
+ return false;
123
+ const [previousWrapperNode, wrapperLocation] = previousWrapper;
124
+ if (previousWrapperNode.type === types.caption) {
125
+ const imageContainer = Editor.parent(editor, wrapperLocation);
126
+ if (imageContainer) {
127
+ const [imageElement, imageLocation] = imageContainer;
128
+ if (imageElement.type === types.figure) {
129
+ Transforms.removeNodes(editor, { at: imageLocation });
130
+ }
131
+ }
132
+ }
133
+ return;
134
+ }
135
+ }
136
+ origin();
137
+ };
138
+ editor.deleteBackward = (unit) => {
139
+ deleteCollapsed(() => deleteBackward(unit), 'isStart');
140
+ };
141
+ editor.deleteForward = (unit) => {
142
+ deleteCollapsed(() => deleteForward(unit), 'isEnd');
143
+ };
144
+ editor.insertBreak = () => {
145
+ const captionEntry = getAboveImageCaption(editor);
146
+ /**
147
+ * Avoid from splitting children of caption.
148
+ */
149
+ if (captionEntry) {
150
+ const [, captionLocation] = captionEntry;
151
+ const imageEntry = Editor.parent(editor, captionLocation);
152
+ if (imageEntry) {
153
+ const [imageElement, imagePosition] = imageEntry;
154
+ if (imageElement.type === types.figure) {
155
+ Transforms.insertNodes(editor, createParagraphElement(), { at: Editor.after(editor, imagePosition) });
156
+ Transforms.move(editor);
157
+ }
158
+ }
159
+ return;
160
+ }
161
+ insertBreak();
162
+ };
163
+ editor.isVoid = element => element.type === types.image || isVoid(element);
164
+ editor.normalizeNode = (entry) => {
165
+ const [node, path] = entry;
166
+ if (Element.isElement(node)) {
167
+ if (node.type === types.figure) {
168
+ if (!isNodesInImage(editor, { at: path })) {
169
+ Transforms.removeNodes(editor, { at: path });
170
+ return;
171
+ }
172
+ }
173
+ else if (node.type === types.image) {
174
+ const { src, width, hosting } = node;
175
+ if (typeof src !== 'string'
176
+ || !(width == null || typeof width === 'number')
177
+ || !(hosting == null || typeof hosting === 'string')) {
178
+ const [, figurePath] = getAboveImageFigure(editor, { at: path }) || [];
179
+ if (figurePath) {
180
+ Transforms.removeNodes(editor, { at: figurePath });
181
+ return;
182
+ }
183
+ }
184
+ if (normalizeVoidElementChildren(editor, [node, path])) {
185
+ return;
186
+ }
187
+ }
188
+ else if (node.type === types.caption) {
189
+ if (normalizeOnlyInlineOrTextInChildren(editor, entry)) {
190
+ return;
191
+ }
192
+ }
193
+ }
194
+ normalizeNode(entry);
195
+ };
196
+ return editor;
197
+ },
198
+ };
199
199
  }
200
200
 
201
201
  export { createImage };
@@ -1,11 +1,11 @@
1
1
  import { isHostingNotRequired } from './isHostingNotRequired.js';
2
2
 
3
- function getImageElementCommonProps(element, hostingResolvers) {
4
- const { src, hosting } = element;
5
- const resolver = !isHostingNotRequired(src) && hosting && hostingResolvers ? hostingResolvers[hosting] : undefined;
6
- return {
7
- src: resolver ? resolver(src) : src,
8
- };
3
+ function getImageElementCommonProps(element, hostingResolvers) {
4
+ const { src, hosting } = element;
5
+ const resolver = !isHostingNotRequired(src) && hosting && hostingResolvers ? hostingResolvers[hosting] : undefined;
6
+ return {
7
+ src: resolver ? resolver(src) : src,
8
+ };
9
9
  }
10
10
 
11
11
  export { getImageElementCommonProps };
@@ -1,8 +1,8 @@
1
- function getImageFigureElementCommonProps(element) {
2
- const { width } = element;
3
- return {
4
- style: typeof width === 'number' ? { width: `${width}%` } : undefined,
5
- };
1
+ function getImageFigureElementCommonProps(element) {
2
+ const { width } = element;
3
+ return {
4
+ style: typeof width === 'number' ? { width: `${width}%` } : undefined,
5
+ };
6
6
  }
7
7
 
8
8
  export { getImageFigureElementCommonProps };