@plone/volto-slate 18.0.0-alpha.4

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 (183) hide show
  1. package/.eslintrc.js +6 -0
  2. package/.release-it.json +25 -0
  3. package/CHANGELOG.md +19 -0
  4. package/LICENSE.md +21 -0
  5. package/README.md +10 -0
  6. package/build/messages/src/blocks/Table/TableBlockEdit.json +90 -0
  7. package/build/messages/src/blocks/Text/DefaultTextBlockEditor.json +6 -0
  8. package/build/messages/src/blocks/Text/DetachedTextBlockEditor.json +6 -0
  9. package/build/messages/src/blocks/Text/SlashMenu.json +6 -0
  10. package/build/messages/src/editor/plugins/AdvancedLink/index.json +10 -0
  11. package/build/messages/src/editor/plugins/Link/index.json +10 -0
  12. package/build/messages/src/editor/plugins/Table/index.json +30 -0
  13. package/build/messages/src/elementEditor/messages.json +10 -0
  14. package/build/messages/src/widgets/HtmlSlateWidget.json +6 -0
  15. package/build/messages/src/widgets/RichTextWidgetView.json +6 -0
  16. package/locales/de/LC_MESSAGES/volto.po +148 -0
  17. package/locales/en/LC_MESSAGES/volto.po +148 -0
  18. package/locales/volto.pot +182 -0
  19. package/package.json +42 -0
  20. package/src/actions/content.js +30 -0
  21. package/src/actions/index.js +3 -0
  22. package/src/actions/plugins.js +9 -0
  23. package/src/actions/selection.js +22 -0
  24. package/src/blocks/Table/Cell.jsx +87 -0
  25. package/src/blocks/Table/Cell.test.js +54 -0
  26. package/src/blocks/Table/TableBlockEdit.jsx +694 -0
  27. package/src/blocks/Table/TableBlockEdit.test.js +40 -0
  28. package/src/blocks/Table/TableBlockView.jsx +150 -0
  29. package/src/blocks/Table/TableBlockView.test.js +49 -0
  30. package/src/blocks/Table/__snapshots__/Cell.test.js.snap +3 -0
  31. package/src/blocks/Table/__snapshots__/TableBlockEdit.test.js.snap +22 -0
  32. package/src/blocks/Table/__snapshots__/TableBlockView.test.js.snap +27 -0
  33. package/src/blocks/Table/deconstruct.js +113 -0
  34. package/src/blocks/Table/extensions/normalizeTable.js +5 -0
  35. package/src/blocks/Table/index.js +60 -0
  36. package/src/blocks/Table/schema.js +122 -0
  37. package/src/blocks/Text/DefaultTextBlockEditor.jsx +304 -0
  38. package/src/blocks/Text/DetachedTextBlockEditor.jsx +77 -0
  39. package/src/blocks/Text/MarkdownIntroduction.jsx +59 -0
  40. package/src/blocks/Text/PluginSidebar.jsx +18 -0
  41. package/src/blocks/Text/ShortcutListing.jsx +28 -0
  42. package/src/blocks/Text/SlashMenu.jsx +203 -0
  43. package/src/blocks/Text/TextBlockEdit.jsx +38 -0
  44. package/src/blocks/Text/TextBlockEdit.test.js +107 -0
  45. package/src/blocks/Text/TextBlockSchema.js +54 -0
  46. package/src/blocks/Text/TextBlockView.jsx +31 -0
  47. package/src/blocks/Text/__snapshots__/TextBlockEdit.test.js.snap +62 -0
  48. package/src/blocks/Text/css/editor.css +18 -0
  49. package/src/blocks/Text/extensions/Readme.md +49 -0
  50. package/src/blocks/Text/extensions/breakList.js +100 -0
  51. package/src/blocks/Text/extensions/index.js +6 -0
  52. package/src/blocks/Text/extensions/insertBreak.js +57 -0
  53. package/src/blocks/Text/extensions/isSelected.js +7 -0
  54. package/src/blocks/Text/extensions/normalizeExternalData.js +7 -0
  55. package/src/blocks/Text/extensions/withDeserializers.js +87 -0
  56. package/src/blocks/Text/extensions/withLists.js +5 -0
  57. package/src/blocks/Text/index.js +171 -0
  58. package/src/blocks/Text/keyboard/backspaceInList.js +58 -0
  59. package/src/blocks/Text/keyboard/breakBlocks.js +3 -0
  60. package/src/blocks/Text/keyboard/cancelEsc.js +7 -0
  61. package/src/blocks/Text/keyboard/indentListItems.js +240 -0
  62. package/src/blocks/Text/keyboard/index.js +52 -0
  63. package/src/blocks/Text/keyboard/joinBlocks.js +180 -0
  64. package/src/blocks/Text/keyboard/moveListItems.js +124 -0
  65. package/src/blocks/Text/keyboard/slashMenu.js +19 -0
  66. package/src/blocks/Text/keyboard/softBreak.js +7 -0
  67. package/src/blocks/Text/keyboard/traverseBlocks.js +81 -0
  68. package/src/blocks/Text/keyboard/unwrapEmptyString.js +26 -0
  69. package/src/blocks/Text/schema.js +39 -0
  70. package/src/constants.js +123 -0
  71. package/src/editor/EditorContext.jsx +5 -0
  72. package/src/editor/EditorReference.jsx +22 -0
  73. package/src/editor/SlateEditor.jsx +375 -0
  74. package/src/editor/config.jsx +344 -0
  75. package/src/editor/decorate.js +68 -0
  76. package/src/editor/deserialize.js +185 -0
  77. package/src/editor/extensions/index.js +6 -0
  78. package/src/editor/extensions/insertBreak.js +15 -0
  79. package/src/editor/extensions/insertData.js +161 -0
  80. package/src/editor/extensions/isInline.js +14 -0
  81. package/src/editor/extensions/normalizeExternalData.js +8 -0
  82. package/src/editor/extensions/normalizeNode.js +48 -0
  83. package/src/editor/extensions/withDeserializers.js +15 -0
  84. package/src/editor/extensions/withTestingFeatures.jsx +84 -0
  85. package/src/editor/index.js +14 -0
  86. package/src/editor/less/editor.less +173 -0
  87. package/src/editor/less/globals.less +18 -0
  88. package/src/editor/less/slate.less +28 -0
  89. package/src/editor/plugins/AdvancedLink/deserialize.js +90 -0
  90. package/src/editor/plugins/AdvancedLink/extensions.js +32 -0
  91. package/src/editor/plugins/AdvancedLink/index.js +50 -0
  92. package/src/editor/plugins/AdvancedLink/render.jsx +37 -0
  93. package/src/editor/plugins/AdvancedLink/schema.js +114 -0
  94. package/src/editor/plugins/AdvancedLink/styles.less +8 -0
  95. package/src/editor/plugins/Blockquote/index.js +30 -0
  96. package/src/editor/plugins/Callout/index.js +34 -0
  97. package/src/editor/plugins/Image/deconstruct.js +30 -0
  98. package/src/editor/plugins/Image/extensions.js +51 -0
  99. package/src/editor/plugins/Image/index.js +11 -0
  100. package/src/editor/plugins/Image/render.jsx +22 -0
  101. package/src/editor/plugins/Link/extensions.js +58 -0
  102. package/src/editor/plugins/Link/index.js +159 -0
  103. package/src/editor/plugins/Link/render.jsx +54 -0
  104. package/src/editor/plugins/Markdown/constants.js +81 -0
  105. package/src/editor/plugins/Markdown/extensions.js +336 -0
  106. package/src/editor/plugins/Markdown/index.js +28 -0
  107. package/src/editor/plugins/Markdown/utils.js +198 -0
  108. package/src/editor/plugins/StyleMenu/StyleMenu.jsx +153 -0
  109. package/src/editor/plugins/StyleMenu/index.js +19 -0
  110. package/src/editor/plugins/StyleMenu/style.less +29 -0
  111. package/src/editor/plugins/StyleMenu/utils.js +168 -0
  112. package/src/editor/plugins/Table/TableButton.jsx +142 -0
  113. package/src/editor/plugins/Table/TableCell.jsx +44 -0
  114. package/src/editor/plugins/Table/TableContainer.jsx +37 -0
  115. package/src/editor/plugins/Table/TableSizePicker.jsx +83 -0
  116. package/src/editor/plugins/Table/extensions.js +87 -0
  117. package/src/editor/plugins/Table/index.js +390 -0
  118. package/src/editor/plugins/Table/less/public.less +29 -0
  119. package/src/editor/plugins/Table/less/table.less +28 -0
  120. package/src/editor/plugins/Table/render.jsx +30 -0
  121. package/src/editor/plugins/index.js +19 -0
  122. package/src/editor/render.jsx +224 -0
  123. package/src/editor/ui/BasicToolbar.jsx +11 -0
  124. package/src/editor/ui/BlockButton.jsx +31 -0
  125. package/src/editor/ui/ClearFormattingButton.jsx +21 -0
  126. package/src/editor/ui/ExpandedToolbar.jsx +18 -0
  127. package/src/editor/ui/Expando.jsx +5 -0
  128. package/src/editor/ui/InlineToolbar.jsx +69 -0
  129. package/src/editor/ui/MarkButton.jsx +23 -0
  130. package/src/editor/ui/MarkElementButton.jsx +30 -0
  131. package/src/editor/ui/Menu.jsx +13 -0
  132. package/src/editor/ui/PositionedToolbar.jsx +32 -0
  133. package/src/editor/ui/Separator.jsx +7 -0
  134. package/src/editor/ui/SlateContextToolbar.jsx +13 -0
  135. package/src/editor/ui/SlateToolbar.jsx +96 -0
  136. package/src/editor/ui/Toolbar.jsx +103 -0
  137. package/src/editor/ui/ToolbarButton.jsx +33 -0
  138. package/src/editor/ui/ToolbarButton.test.js +25 -0
  139. package/src/editor/ui/__snapshots__/ToolbarButton.test.js.snap +16 -0
  140. package/src/editor/ui/index.js +15 -0
  141. package/src/editor/utils.js +248 -0
  142. package/src/elementEditor/ContextButtons.jsx +57 -0
  143. package/src/elementEditor/PluginEditor.jsx +124 -0
  144. package/src/elementEditor/Readme.md +6 -0
  145. package/src/elementEditor/SchemaProvider.jsx +4 -0
  146. package/src/elementEditor/SidebarEditor.jsx +46 -0
  147. package/src/elementEditor/ToolbarButton.jsx +44 -0
  148. package/src/elementEditor/index.js +5 -0
  149. package/src/elementEditor/makeInlineElementPlugin.js +100 -0
  150. package/src/elementEditor/messages.js +14 -0
  151. package/src/elementEditor/utils.js +227 -0
  152. package/src/hooks/index.js +3 -0
  153. package/src/hooks/useEditorContext.js +6 -0
  154. package/src/hooks/useIsomorphicLayoutEffect.js +7 -0
  155. package/src/hooks/useSelectionPosition.js +25 -0
  156. package/src/i18n.js +180 -0
  157. package/src/icons/hashlink.svg +57 -0
  158. package/src/index.js +61 -0
  159. package/src/reducers/content.js +74 -0
  160. package/src/reducers/index.js +3 -0
  161. package/src/reducers/plugins.js +17 -0
  162. package/src/reducers/selection.js +16 -0
  163. package/src/utils/blocks.js +379 -0
  164. package/src/utils/blocks.test.js +138 -0
  165. package/src/utils/editor.js +31 -0
  166. package/src/utils/image.js +25 -0
  167. package/src/utils/index.js +11 -0
  168. package/src/utils/internals.js +46 -0
  169. package/src/utils/lists.js +92 -0
  170. package/src/utils/marks.js +104 -0
  171. package/src/utils/mime-types.js +24 -0
  172. package/src/utils/nodes.js +4 -0
  173. package/src/utils/ops.js +20 -0
  174. package/src/utils/random.js +17 -0
  175. package/src/utils/selection.js +236 -0
  176. package/src/utils/slate-string-utils.js +409 -0
  177. package/src/utils/volto-blocks.js +314 -0
  178. package/src/widgets/ErrorBoundary.jsx +27 -0
  179. package/src/widgets/HtmlSlateWidget.jsx +138 -0
  180. package/src/widgets/ObjectByTypeWidget.jsx +49 -0
  181. package/src/widgets/RichTextWidget.jsx +72 -0
  182. package/src/widgets/RichTextWidgetView.jsx +36 -0
  183. package/src/widgets/style.css +21 -0
@@ -0,0 +1,409 @@
1
+ // NOTE: file taken from Slate.js on 15th of October, 2021
2
+
3
+ /**
4
+ * Constants for string distance checking.
5
+ */
6
+
7
+ const SPACE = /\s/;
8
+ const PUNCTUATION =
9
+ /[\u0021-\u0023\u0025-\u002A\u002C-\u002F\u003A\u003B\u003F\u0040\u005B-\u005D\u005F\u007B\u007D\u00A1\u00A7\u00AB\u00B6\u00B7\u00BB\u00BF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0AF0\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E3B\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]/;
10
+ const CHAMELEON = /['\u2018\u2019]/;
11
+
12
+ /**
13
+ * Get the distance to the end of the first character in a string of text.
14
+ */
15
+
16
+ export const getCharacterDistance = (str, isRTL = false) => {
17
+ const isLTR = !isRTL;
18
+
19
+ let dist = 0;
20
+ // prev types:
21
+ // NSEQ: non sequenceable codepoint.
22
+ // MOD: modifier
23
+ // ZWJ: zero width joiner
24
+ // VAR: variation selector
25
+ // BMP: sequenceable codepoint from basic multilingual plane
26
+ // RI: regional indicator
27
+ // KC: keycap
28
+ // TAG: tag
29
+ let prev = null;
30
+
31
+ const codepoints = isLTR ? str : codepointsIteratorRTL(str);
32
+
33
+ for (const codepoint of codepoints) {
34
+ const code = codepoint.codePointAt(0);
35
+ if (!code) break;
36
+
37
+ // Check if codepoint is part of a sequence.
38
+ if (isZWJ(code)) {
39
+ dist += codepoint.length;
40
+ prev = 'ZWJ';
41
+
42
+ continue;
43
+ }
44
+
45
+ const [isKeycapStart, isKeycapEnd] = isLTR
46
+ ? [isKeycap, isCombiningEnclosingKeycap]
47
+ : [isCombiningEnclosingKeycap, isKeycap];
48
+ if (isKeycapStart(code)) {
49
+ if (prev === 'KC') {
50
+ break;
51
+ }
52
+
53
+ dist += codepoint.length;
54
+ prev = 'KC';
55
+ continue;
56
+ }
57
+ if (isKeycapEnd(code)) {
58
+ dist += codepoint.length;
59
+ break;
60
+ }
61
+
62
+ if (isVariationSelector(code)) {
63
+ dist += codepoint.length;
64
+
65
+ if (isLTR && prev === 'BMP') {
66
+ break;
67
+ }
68
+
69
+ prev = 'VAR';
70
+
71
+ continue;
72
+ }
73
+
74
+ if (isBMPEmoji(code)) {
75
+ if (isLTR && prev && prev !== 'ZWJ' && prev !== 'VAR') {
76
+ break;
77
+ }
78
+
79
+ dist += codepoint.length;
80
+
81
+ if (isRTL && prev === 'VAR') {
82
+ break;
83
+ }
84
+
85
+ prev = 'BMP';
86
+ continue;
87
+ }
88
+
89
+ if (isModifier(code)) {
90
+ dist += codepoint.length;
91
+ prev = 'MOD';
92
+
93
+ continue;
94
+ }
95
+
96
+ const [isTagStart, isTagEnd] = isLTR
97
+ ? [isBlackFlag, isCancelTag]
98
+ : [isCancelTag, isBlackFlag];
99
+ if (isTagStart(code)) {
100
+ if (prev === 'TAG') break;
101
+
102
+ dist += codepoint.length;
103
+ prev = 'TAG';
104
+ continue;
105
+ }
106
+ if (isTagEnd(code)) {
107
+ dist += codepoint.length;
108
+ break;
109
+ }
110
+ if (prev === 'TAG' && isTag(code)) {
111
+ dist += codepoint.length;
112
+ continue;
113
+ }
114
+
115
+ if (isRegionalIndicator(code)) {
116
+ dist += codepoint.length;
117
+
118
+ if (prev === 'RI') {
119
+ break;
120
+ }
121
+
122
+ prev = 'RI';
123
+
124
+ continue;
125
+ }
126
+
127
+ if (!isBMP(code)) {
128
+ // If previous code point is not sequenceable, it means we are not in a
129
+ // sequence.
130
+ if (prev === 'NSEQ') {
131
+ break;
132
+ }
133
+
134
+ dist += codepoint.length;
135
+ prev = 'NSEQ';
136
+
137
+ continue;
138
+ }
139
+
140
+ // Modifier 'groups up' with what ever character is before that (even whitespace), need to
141
+ // look ahead.
142
+ if (isLTR && prev === 'MOD') {
143
+ dist += codepoint.length;
144
+ break;
145
+ }
146
+
147
+ // If while loop ever gets here, we're done (e.g latin chars).
148
+ break;
149
+ }
150
+
151
+ return dist || 1;
152
+ };
153
+
154
+ /**
155
+ * Get the distance to the end of the first word in a string of text.
156
+ */
157
+
158
+ export const getWordDistance = (text, isRTL = false) => {
159
+ let dist = 0;
160
+ let started = false;
161
+
162
+ while (text.length > 0) {
163
+ const charDist = getCharacterDistance(text, isRTL);
164
+ const [char, remaining] = splitByCharacterDistance(text, charDist, isRTL);
165
+
166
+ if (isWordCharacter(char, remaining, isRTL)) {
167
+ started = true;
168
+ dist += charDist;
169
+ } else if (!started) {
170
+ dist += charDist;
171
+ } else {
172
+ break;
173
+ }
174
+
175
+ text = remaining;
176
+ }
177
+
178
+ return dist;
179
+ };
180
+
181
+ /**
182
+ * Split a string in two parts at a given distance starting from the end when
183
+ * `isRTL` is set to `true`.
184
+ */
185
+
186
+ export const splitByCharacterDistance = (str, dist, isRTL) => {
187
+ if (isRTL) {
188
+ const at = str.length - dist;
189
+ return [str.slice(at, str.length), str.slice(0, at)];
190
+ }
191
+
192
+ return [str.slice(0, dist), str.slice(dist)];
193
+ };
194
+
195
+ /**
196
+ * Check if a character is a word character. The `remaining` argument is used
197
+ * because sometimes you must read subsequent characters to truly determine it.
198
+ */
199
+
200
+ const isWordCharacter = (char, remaining, isRTL = false) => {
201
+ if (SPACE.test(char)) {
202
+ return false;
203
+ }
204
+
205
+ // Chameleons count as word characters as long as they're in a word, so
206
+ // recurse to see if the next one is a word character or not.
207
+ if (CHAMELEON.test(char)) {
208
+ const charDist = getCharacterDistance(remaining, isRTL);
209
+ const [nextChar, nextRemaining] = splitByCharacterDistance(
210
+ remaining,
211
+ charDist,
212
+ isRTL,
213
+ );
214
+
215
+ if (isWordCharacter(nextChar, nextRemaining, isRTL)) {
216
+ return true;
217
+ }
218
+ }
219
+
220
+ if (PUNCTUATION.test(char)) {
221
+ return false;
222
+ }
223
+
224
+ return true;
225
+ };
226
+
227
+ /**
228
+ * Does `code` form Modifier with next one.
229
+ *
230
+ * https://emojipedia.org/modifiers/
231
+ */
232
+
233
+ const isModifier = (code) => {
234
+ return code >= 0x1f3fb && code <= 0x1f3ff;
235
+ };
236
+
237
+ /**
238
+ * Is `code` a Variation Selector.
239
+ *
240
+ * https://codepoints.net/variation_selectors
241
+ */
242
+
243
+ const isVariationSelector = (code) => {
244
+ return code <= 0xfe0f && code >= 0xfe00;
245
+ };
246
+
247
+ /**
248
+ * Is `code` a code point used in keycap sequence.
249
+ *
250
+ * https://emojipedia.org/emoji-keycap-sequence/
251
+ */
252
+
253
+ const isKeycap = (code) => {
254
+ return (
255
+ (code >= 0x30 && code <= 0x39) || // digits
256
+ code === 0x23 || // number sign
257
+ code === 0x2a
258
+ );
259
+ };
260
+
261
+ /**
262
+ * Is `code` a Combining Enclosing Keycap.
263
+ *
264
+ * https://emojipedia.org/combining-enclosing-keycap/
265
+ */
266
+
267
+ const isCombiningEnclosingKeycap = (code) => {
268
+ return code === 0x20e3;
269
+ };
270
+
271
+ /**
272
+ * Is `code` one of the BMP codes used in emoji sequences.
273
+ *
274
+ * https://emojipedia.org/emoji-zwj-sequences/
275
+ */
276
+
277
+ const isBMPEmoji = (code) => {
278
+ // This requires tiny bit of maintanance, better ideas?
279
+ // Fortunately it only happens if new Unicode Standard
280
+ // is released. Fails gracefully if upkeep lags behind,
281
+ // same way Slate previously behaved with all emojis.
282
+ return (
283
+ code === 0x2764 || // heart (❤)
284
+ code === 0x2642 || // male (♂)
285
+ code === 0x2640 || // female (♀)
286
+ code === 0x2620 || // scull (☠)
287
+ code === 0x2695 || // medical (⚕)
288
+ code === 0x2708 || // plane (✈️)
289
+ code === 0x25ef || // large circle (◯)
290
+ code === 0x2b06 || // up arrow (⬆)
291
+ code === 0x2197 || // up-right arrow (↗)
292
+ code === 0x27a1 || // right arrow (➡)
293
+ code === 0x2198 || // down-right arrow (↘)
294
+ code === 0x2b07 || // down arrow (⬇)
295
+ code === 0x2199 || // down-left arrow (↙)
296
+ code === 0x2b05 || // left arrow (⬅)
297
+ code === 0x2196 || // up-left arrow (↖)
298
+ code === 0x2195 || // up-down arrow (↕)
299
+ code === 0x2194 || // left-right arrow (↔)
300
+ code === 0x21a9 || // right arrow curving left (↩)
301
+ code === 0x21aa || // left arrow curving right (↪)
302
+ code === 0x2934 || // right arrow curving up (⤴)
303
+ code === 0x2935 // right arrow curving down (⤵)
304
+ );
305
+ };
306
+
307
+ /**
308
+ * Is `code` a Regional Indicator.
309
+ *
310
+ * https://en.wikipedia.org/wiki/Regional_indicator_symbol
311
+ */
312
+
313
+ const isRegionalIndicator = (code) => {
314
+ return code >= 0x1f1e6 && code <= 0x1f1ff;
315
+ };
316
+
317
+ /**
318
+ * Is `code` from basic multilingual plane.
319
+ *
320
+ * https://codepoints.net/basic_multilingual_plane
321
+ */
322
+
323
+ const isBMP = (code) => {
324
+ return code <= 0xffff;
325
+ };
326
+
327
+ /**
328
+ * Is `code` a Zero Width Joiner.
329
+ *
330
+ * https://emojipedia.org/zero-width-joiner/
331
+ */
332
+
333
+ const isZWJ = (code) => {
334
+ return code === 0x200d;
335
+ };
336
+
337
+ /**
338
+ * Is `code` a Black Flag.
339
+ *
340
+ * https://emojipedia.org/black-flag/
341
+ */
342
+
343
+ const isBlackFlag = (code) => {
344
+ return code === 0x1f3f4;
345
+ };
346
+
347
+ /**
348
+ * Is `code` a Tag.
349
+ *
350
+ * https://emojipedia.org/emoji-tag-sequence/
351
+ */
352
+
353
+ const isTag = (code) => {
354
+ return code >= 0xe0000 && code <= 0xe007f;
355
+ };
356
+
357
+ /**
358
+ * Is `code` a Cancel Tag.
359
+ *
360
+ * https://emojipedia.org/cancel-tag/
361
+ */
362
+
363
+ const isCancelTag = (code) => {
364
+ return code === 0xe007f;
365
+ };
366
+
367
+ /**
368
+ * Iterate on codepoints from right to left.
369
+ */
370
+
371
+ export const codepointsIteratorRTL = function* (str) {
372
+ const end = str.length - 1;
373
+
374
+ for (let i = 0; i < str.length; i++) {
375
+ const char1 = str.charAt(end - i);
376
+
377
+ if (isLowSurrogate(char1.charCodeAt(0))) {
378
+ const char2 = str.charAt(end - i - 1);
379
+ if (isHighSurrogate(char2.charCodeAt(0))) {
380
+ yield char2 + char1;
381
+
382
+ i++;
383
+ continue;
384
+ }
385
+ }
386
+
387
+ yield char1;
388
+ }
389
+ };
390
+
391
+ /**
392
+ * Is `charCode` a high surrogate.
393
+ *
394
+ * https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
395
+ */
396
+
397
+ const isHighSurrogate = (charCode) => {
398
+ return charCode >= 0xd800 && charCode <= 0xdbff;
399
+ };
400
+
401
+ /**
402
+ * Is `charCode` a low surrogate.
403
+ *
404
+ * https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
405
+ */
406
+
407
+ const isLowSurrogate = (charCode) => {
408
+ return charCode >= 0xdc00 && charCode <= 0xdfff;
409
+ };
@@ -0,0 +1,314 @@
1
+ import ReactDOM from 'react-dom';
2
+ import { v4 as uuid } from 'uuid';
3
+ import {
4
+ addBlock,
5
+ changeBlock,
6
+ getBlocksFieldname,
7
+ getBlocksLayoutFieldname,
8
+ } from '@plone/volto/helpers';
9
+ import { Transforms, Editor, Node, Text, Path } from 'slate';
10
+ import { serializeNodesToText } from '@plone/volto-slate/editor/render';
11
+ import { omit } from 'lodash';
12
+ import config from '@plone/volto/registry';
13
+
14
+ function fromEntries(pairs) {
15
+ const res = {};
16
+ pairs.forEach((p) => {
17
+ res[p[0]] = p[1];
18
+ });
19
+ return res;
20
+ }
21
+
22
+ // TODO: should be made generic, no need for "prevBlock.value"
23
+ export function mergeSlateWithBlockBackward(editor, prevBlock, event) {
24
+ // To work around current architecture limitations, read the value from
25
+ // previous block. Replace it in the current editor (over which we have
26
+ // control), join with current block value, then use this result for previous
27
+ // block, delete current block
28
+
29
+ const prev = prevBlock.value;
30
+
31
+ // collapse the selection to its start point
32
+ Transforms.collapse(editor, { edge: 'start' });
33
+
34
+ let rangeRef;
35
+ let end;
36
+
37
+ Editor.withoutNormalizing(editor, () => {
38
+ // insert block #0 contents in block #1 contents, at the beginning
39
+ Transforms.insertNodes(editor, prev, {
40
+ at: Editor.start(editor, []),
41
+ });
42
+
43
+ // the contents that should be moved into the `ul`, as the last `li`
44
+ rangeRef = Editor.rangeRef(editor, {
45
+ anchor: Editor.start(editor, [1]),
46
+ focus: Editor.end(editor, [1]),
47
+ });
48
+
49
+ const source = rangeRef.current;
50
+
51
+ end = Editor.end(editor, [0]);
52
+
53
+ let endPoint;
54
+
55
+ Transforms.insertNodes(editor, { text: '' }, { at: end });
56
+
57
+ end = Editor.end(editor, [0]);
58
+
59
+ Transforms.splitNodes(editor, {
60
+ at: end,
61
+ always: true,
62
+ height: 1,
63
+ mode: 'highest',
64
+ match: (n) => n.type === 'li' || Text.isText(n),
65
+ });
66
+
67
+ endPoint = Editor.end(editor, [0]);
68
+
69
+ Transforms.moveNodes(editor, {
70
+ at: source,
71
+ to: endPoint.path,
72
+ mode: 'all',
73
+ match: (n, p) => p.length === 2,
74
+ });
75
+ });
76
+
77
+ const [n] = Editor.node(editor, [1]);
78
+
79
+ if (Editor.isEmpty(editor, n)) {
80
+ Transforms.removeNodes(editor, { at: [1] });
81
+ }
82
+
83
+ rangeRef.unref();
84
+
85
+ const [, lastPath] = Editor.last(editor, [0]);
86
+
87
+ end = Editor.start(editor, Path.parent(lastPath));
88
+
89
+ return end;
90
+ }
91
+
92
+ export function mergeSlateWithBlockForward(editor, nextBlock, event) {
93
+ // To work around current architecture limitations, read the value from next
94
+ // block. Replace it in the current editor (over which we have control), join
95
+ // with current block value, then use this result for next block, delete
96
+ // current block
97
+
98
+ const next = nextBlock.value;
99
+
100
+ // collapse the selection to its start point
101
+ Transforms.collapse(editor, { edge: 'end' });
102
+ Transforms.insertNodes(editor, next, {
103
+ at: Editor.end(editor, []),
104
+ });
105
+
106
+ Editor.deleteForward(editor, { unit: 'character' });
107
+ }
108
+
109
+ export function syncCreateSlateBlock(value) {
110
+ const id = uuid();
111
+ const block = {
112
+ '@type': 'slate',
113
+ value: JSON.parse(JSON.stringify(value)),
114
+ plaintext: serializeNodesToText(value),
115
+ };
116
+ return [id, block];
117
+ }
118
+
119
+ export function createImageBlock(url, index, props) {
120
+ const { properties, onChangeField, onSelectBlock } = props;
121
+ const blocksFieldname = getBlocksFieldname(properties);
122
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
123
+
124
+ const [id, formData] = addBlock(properties, 'image', index + 1);
125
+ const newFormData = changeBlock(formData, id, { '@type': 'image', url });
126
+
127
+ ReactDOM.unstable_batchedUpdates(() => {
128
+ onChangeField(blocksFieldname, newFormData[blocksFieldname]);
129
+ onChangeField(blocksLayoutFieldname, newFormData[blocksLayoutFieldname]);
130
+ onSelectBlock(id);
131
+ });
132
+ }
133
+
134
+ export const createAndSelectNewBlockAfter = (editor, blockValue) => {
135
+ const blockProps = editor.getBlockProps();
136
+
137
+ const { onSelectBlock, properties, index, onChangeField } = blockProps;
138
+
139
+ const [blockId, formData] = addBlock(properties, 'slate', index + 1);
140
+
141
+ const options = {
142
+ '@type': 'slate',
143
+ value: JSON.parse(JSON.stringify(blockValue)),
144
+ plaintext: serializeNodesToText(blockValue),
145
+ };
146
+
147
+ const newFormData = changeBlock(formData, blockId, options);
148
+
149
+ const blocksFieldname = getBlocksFieldname(properties);
150
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
151
+ // console.log('layout', blocksLayoutFieldname, newFormData);
152
+
153
+ ReactDOM.unstable_batchedUpdates(() => {
154
+ blockProps.saveSlateBlockSelection(blockId, 'start');
155
+ onChangeField(blocksFieldname, newFormData[blocksFieldname]);
156
+ onChangeField(blocksLayoutFieldname, newFormData[blocksLayoutFieldname]);
157
+ onSelectBlock(blockId);
158
+ });
159
+ };
160
+
161
+ export function getNextVoltoBlock(index, properties) {
162
+ // TODO: look for any next slate block
163
+ // join this block with previous block, if previous block is slate
164
+ const blocksFieldname = getBlocksFieldname(properties);
165
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
166
+
167
+ const blocks_layout = properties[blocksLayoutFieldname];
168
+
169
+ if (index === blocks_layout.items.length) return;
170
+
171
+ const nextBlockId = blocks_layout.items[index + 1];
172
+ const nextBlock = properties[blocksFieldname][nextBlockId];
173
+
174
+ return [nextBlock, nextBlockId];
175
+ }
176
+
177
+ export function getPreviousVoltoBlock(index, properties) {
178
+ // TODO: look for any prev slate block
179
+ if (index === 0) return;
180
+
181
+ const blocksFieldname = getBlocksFieldname(properties);
182
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
183
+
184
+ const blocks_layout = properties[blocksLayoutFieldname];
185
+ const prevBlockId = blocks_layout.items[index - 1];
186
+ const prevBlock = properties[blocksFieldname][prevBlockId];
187
+
188
+ return [prevBlock, prevBlockId];
189
+ }
190
+
191
+ // //check for existing img children
192
+ // const checkContainImg = (elements) => {
193
+ // var check = false;
194
+ // elements.forEach((e) =>
195
+ // e.children.forEach((c) => {
196
+ // if (c && c.type && c.type === 'img') {
197
+ // check = true;
198
+ // }
199
+ // }),
200
+ // );
201
+ // return check;
202
+ // };
203
+
204
+ // //check for existing table children
205
+ // const checkContainTable = (elements) => {
206
+ // var check = false;
207
+ // elements.forEach((e) => {
208
+ // if (e && e.type && e.type === 'table') {
209
+ // check = true;
210
+ // }
211
+ // });
212
+ // return check;
213
+ // };
214
+
215
+ /**
216
+ * The editor has the properties `dataTransferHandlers` (object) and
217
+ * `dataTransferFormatsOrder` and in `dataTransferHandlers` are functions which
218
+ * sometimes must call this function. Some types of data storeable in Slate
219
+ * documents can be and should be put into separate Volto blocks. The
220
+ * `deconstructToVoltoBlocks` function scans the contents of the Slate document
221
+ * and, through configured Volto block emitters, it outputs separate Volto
222
+ * blocks into the same Volto page form. The `deconstructToVoltoBlocks` function
223
+ * should be called only in key places where it is necessary.
224
+ *
225
+ * @example See the `src/editor/extensions/insertData.js` file.
226
+ *
227
+ * @param {Editor} editor The Slate editor object which should be deconstructed
228
+ * if possible.
229
+ *
230
+ * @returns {Promise}
231
+ */
232
+ export function deconstructToVoltoBlocks(editor) {
233
+ // Explodes editor content into separate blocks
234
+ // If the editor has multiple top-level children, split the current block
235
+ // into multiple slate blocks. This will delete and replace the current
236
+ // block.
237
+ //
238
+ // It returns a promise that, when resolved, will pass a list of Volto block
239
+ // ids that were affected
240
+ //
241
+ // For the Volto blocks manipulation we do low-level changes to the context
242
+ // form state, as that ensures a better performance (no un-needed UI updates)
243
+
244
+ if (!editor.getBlockProps) return;
245
+
246
+ const blockProps = editor.getBlockProps();
247
+ const { slate } = config.settings;
248
+ const { voltoBlockEmiters } = slate;
249
+
250
+ return new Promise((resolve, reject) => {
251
+ if (!editor?.children) return;
252
+
253
+ if (editor.children.length === 1) {
254
+ return resolve([blockProps.block]);
255
+ }
256
+ const { properties, onChangeField, onSelectBlock } = editor.getBlockProps();
257
+ const blocksFieldname = getBlocksFieldname(properties);
258
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
259
+
260
+ const { index } = blockProps;
261
+ let blocks = [];
262
+
263
+ // TODO: should use Editor.levels() instead of Node.children
264
+ const pathRefs = Array.from(Node.children(editor, [])).map(([, path]) =>
265
+ Editor.pathRef(editor, path),
266
+ );
267
+
268
+ for (const pathRef of pathRefs) {
269
+ // extra nodes are always extracted after the text node
270
+ let extras = voltoBlockEmiters
271
+ .map((emit) => emit(editor, pathRef))
272
+ .flat(1);
273
+
274
+ // The node might have been replaced with a Volto block
275
+ if (pathRef.current) {
276
+ const [childNode] = Editor.node(editor, pathRef.current);
277
+ if (childNode && !Editor.isEmpty(editor, childNode))
278
+ blocks.push(syncCreateSlateBlock([childNode]));
279
+ }
280
+ blocks = [...blocks, ...extras];
281
+ }
282
+
283
+ const blockids = blocks.map((b) => b[0]);
284
+
285
+ // TODO: add the placeholder block, because we remove it
286
+ // (when we remove the current block)
287
+
288
+ const blocksData = omit(
289
+ {
290
+ ...properties[blocksFieldname],
291
+ ...fromEntries(blocks),
292
+ },
293
+ blockProps.block,
294
+ );
295
+ const layoutData = {
296
+ ...properties[blocksLayoutFieldname],
297
+ items: [
298
+ ...properties[blocksLayoutFieldname].items.slice(0, index),
299
+ ...blockids,
300
+ ...properties[blocksLayoutFieldname].items.slice(index),
301
+ ].filter((id) => id !== blockProps.block),
302
+ };
303
+
304
+ // TODO: use onChangeFormData instead of this API style
305
+ ReactDOM.unstable_batchedUpdates(() => {
306
+ onChangeField(blocksFieldname, blocksData);
307
+ onChangeField(blocksLayoutFieldname, layoutData);
308
+ onSelectBlock(blockids[blockids.length - 1]);
309
+ // resolve(blockids);
310
+ // or rather this?
311
+ Promise.resolve().then(resolve(blockids));
312
+ });
313
+ });
314
+ }