@pie-lib/editable-html-tip-tap 1.0.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 (167) hide show
  1. package/CHANGELOG.json +32 -0
  2. package/CHANGELOG.md +2280 -0
  3. package/lib/__tests__/editor.test.js +470 -0
  4. package/lib/__tests__/serialization.test.js +246 -0
  5. package/lib/__tests__/utils.js +106 -0
  6. package/lib/block-tags.js +25 -0
  7. package/lib/constants.js +16 -0
  8. package/lib/editor.js +1356 -0
  9. package/lib/extensions/MediaView.js +112 -0
  10. package/lib/extensions/characters.js +65 -0
  11. package/lib/extensions/component.js +325 -0
  12. package/lib/extensions/css.js +252 -0
  13. package/lib/extensions/custom-toolbar-wrapper.js +124 -0
  14. package/lib/extensions/image.js +106 -0
  15. package/lib/extensions/math.js +330 -0
  16. package/lib/extensions/media.js +276 -0
  17. package/lib/extensions/responseArea.js +278 -0
  18. package/lib/index.js +1213 -0
  19. package/lib/old-index.js +269 -0
  20. package/lib/parse-html.js +16 -0
  21. package/lib/plugins/characters/custom-popper.js +73 -0
  22. package/lib/plugins/characters/index.js +305 -0
  23. package/lib/plugins/characters/utils.js +381 -0
  24. package/lib/plugins/css/icons/index.js +37 -0
  25. package/lib/plugins/css/index.js +390 -0
  26. package/lib/plugins/customPlugin/index.js +114 -0
  27. package/lib/plugins/html/icons/index.js +38 -0
  28. package/lib/plugins/html/index.js +81 -0
  29. package/lib/plugins/image/__tests__/component.test.js +51 -0
  30. package/lib/plugins/image/__tests__/image-toolbar-logic.test.js +56 -0
  31. package/lib/plugins/image/__tests__/image-toolbar.test.js +26 -0
  32. package/lib/plugins/image/__tests__/index.test.js +98 -0
  33. package/lib/plugins/image/__tests__/insert-image-handler.test.js +125 -0
  34. package/lib/plugins/image/__tests__/mock-change.js +25 -0
  35. package/lib/plugins/image/alt-dialog.js +129 -0
  36. package/lib/plugins/image/component.js +419 -0
  37. package/lib/plugins/image/image-toolbar.js +177 -0
  38. package/lib/plugins/image/index.js +263 -0
  39. package/lib/plugins/image/insert-image-handler.js +117 -0
  40. package/lib/plugins/index.js +413 -0
  41. package/lib/plugins/list/__tests__/index.test.js +79 -0
  42. package/lib/plugins/list/index.js +334 -0
  43. package/lib/plugins/math/__tests__/index.test.js +300 -0
  44. package/lib/plugins/math/index.js +454 -0
  45. package/lib/plugins/media/__tests__/index.test.js +71 -0
  46. package/lib/plugins/media/index.js +387 -0
  47. package/lib/plugins/media/media-dialog.js +709 -0
  48. package/lib/plugins/media/media-toolbar.js +101 -0
  49. package/lib/plugins/media/media-wrapper.js +93 -0
  50. package/lib/plugins/rendering/index.js +46 -0
  51. package/lib/plugins/respArea/drag-in-the-blank/choice.js +289 -0
  52. package/lib/plugins/respArea/drag-in-the-blank/index.js +94 -0
  53. package/lib/plugins/respArea/explicit-constructed-response/index.js +120 -0
  54. package/lib/plugins/respArea/icons/index.js +95 -0
  55. package/lib/plugins/respArea/index.js +341 -0
  56. package/lib/plugins/respArea/inline-dropdown/index.js +126 -0
  57. package/lib/plugins/respArea/math-templated/index.js +130 -0
  58. package/lib/plugins/respArea/utils.js +125 -0
  59. package/lib/plugins/table/CustomTablePlugin.js +133 -0
  60. package/lib/plugins/table/__tests__/index.test.js +442 -0
  61. package/lib/plugins/table/__tests__/table-toolbar.test.js +54 -0
  62. package/lib/plugins/table/icons/index.js +69 -0
  63. package/lib/plugins/table/index.js +483 -0
  64. package/lib/plugins/table/table-toolbar.js +187 -0
  65. package/lib/plugins/textAlign/icons/index.js +194 -0
  66. package/lib/plugins/textAlign/index.js +34 -0
  67. package/lib/plugins/toolbar/__tests__/default-toolbar.test.js +128 -0
  68. package/lib/plugins/toolbar/__tests__/editor-and-toolbar.test.js +51 -0
  69. package/lib/plugins/toolbar/__tests__/toolbar-buttons.test.js +54 -0
  70. package/lib/plugins/toolbar/__tests__/toolbar.test.js +120 -0
  71. package/lib/plugins/toolbar/default-toolbar.js +229 -0
  72. package/lib/plugins/toolbar/done-button.js +53 -0
  73. package/lib/plugins/toolbar/editor-and-toolbar.js +286 -0
  74. package/lib/plugins/toolbar/index.js +34 -0
  75. package/lib/plugins/toolbar/toolbar-buttons.js +194 -0
  76. package/lib/plugins/toolbar/toolbar.js +376 -0
  77. package/lib/plugins/utils.js +62 -0
  78. package/lib/serialization.js +677 -0
  79. package/lib/shared/alert-dialog.js +75 -0
  80. package/lib/theme.js +9 -0
  81. package/package.json +69 -0
  82. package/src/__tests__/editor.test.jsx +363 -0
  83. package/src/__tests__/serialization.test.js +291 -0
  84. package/src/__tests__/utils.js +36 -0
  85. package/src/block-tags.js +17 -0
  86. package/src/constants.js +7 -0
  87. package/src/editor.jsx +1197 -0
  88. package/src/extensions/characters.js +46 -0
  89. package/src/extensions/component.jsx +294 -0
  90. package/src/extensions/css.js +217 -0
  91. package/src/extensions/custom-toolbar-wrapper.jsx +100 -0
  92. package/src/extensions/image.js +55 -0
  93. package/src/extensions/math.js +259 -0
  94. package/src/extensions/media.js +182 -0
  95. package/src/extensions/responseArea.js +205 -0
  96. package/src/index.jsx +1462 -0
  97. package/src/old-index.jsx +162 -0
  98. package/src/parse-html.js +8 -0
  99. package/src/plugins/README.md +27 -0
  100. package/src/plugins/characters/custom-popper.js +48 -0
  101. package/src/plugins/characters/index.jsx +284 -0
  102. package/src/plugins/characters/utils.js +447 -0
  103. package/src/plugins/css/icons/index.jsx +17 -0
  104. package/src/plugins/css/index.jsx +340 -0
  105. package/src/plugins/customPlugin/index.jsx +85 -0
  106. package/src/plugins/html/icons/index.jsx +19 -0
  107. package/src/plugins/html/index.jsx +72 -0
  108. package/src/plugins/image/__tests__/__snapshots__/component.test.jsx.snap +51 -0
  109. package/src/plugins/image/__tests__/__snapshots__/image-toolbar-logic.test.jsx.snap +27 -0
  110. package/src/plugins/image/__tests__/__snapshots__/image-toolbar.test.jsx.snap +44 -0
  111. package/src/plugins/image/__tests__/component.test.jsx +41 -0
  112. package/src/plugins/image/__tests__/image-toolbar-logic.test.jsx +42 -0
  113. package/src/plugins/image/__tests__/image-toolbar.test.jsx +11 -0
  114. package/src/plugins/image/__tests__/index.test.js +95 -0
  115. package/src/plugins/image/__tests__/insert-image-handler.test.js +113 -0
  116. package/src/plugins/image/__tests__/mock-change.js +15 -0
  117. package/src/plugins/image/alt-dialog.jsx +82 -0
  118. package/src/plugins/image/component.jsx +343 -0
  119. package/src/plugins/image/image-toolbar.jsx +100 -0
  120. package/src/plugins/image/index.jsx +227 -0
  121. package/src/plugins/image/insert-image-handler.js +79 -0
  122. package/src/plugins/index.jsx +377 -0
  123. package/src/plugins/list/__tests__/index.test.js +54 -0
  124. package/src/plugins/list/index.jsx +305 -0
  125. package/src/plugins/math/__tests__/__snapshots__/index.test.jsx.snap +48 -0
  126. package/src/plugins/math/__tests__/index.test.jsx +245 -0
  127. package/src/plugins/math/index.jsx +379 -0
  128. package/src/plugins/media/__tests__/index.test.js +75 -0
  129. package/src/plugins/media/index.jsx +325 -0
  130. package/src/plugins/media/media-dialog.js +624 -0
  131. package/src/plugins/media/media-toolbar.jsx +56 -0
  132. package/src/plugins/media/media-wrapper.jsx +43 -0
  133. package/src/plugins/rendering/index.js +31 -0
  134. package/src/plugins/respArea/drag-in-the-blank/choice.jsx +215 -0
  135. package/src/plugins/respArea/drag-in-the-blank/index.jsx +70 -0
  136. package/src/plugins/respArea/explicit-constructed-response/index.jsx +92 -0
  137. package/src/plugins/respArea/icons/index.jsx +71 -0
  138. package/src/plugins/respArea/index.jsx +299 -0
  139. package/src/plugins/respArea/inline-dropdown/index.jsx +108 -0
  140. package/src/plugins/respArea/math-templated/index.jsx +104 -0
  141. package/src/plugins/respArea/utils.jsx +90 -0
  142. package/src/plugins/table/CustomTablePlugin.js +113 -0
  143. package/src/plugins/table/__tests__/__snapshots__/table-toolbar.test.jsx.snap +44 -0
  144. package/src/plugins/table/__tests__/index.test.jsx +401 -0
  145. package/src/plugins/table/__tests__/table-toolbar.test.jsx +42 -0
  146. package/src/plugins/table/icons/index.jsx +53 -0
  147. package/src/plugins/table/index.jsx +427 -0
  148. package/src/plugins/table/table-toolbar.jsx +136 -0
  149. package/src/plugins/textAlign/icons/index.jsx +114 -0
  150. package/src/plugins/textAlign/index.jsx +23 -0
  151. package/src/plugins/toolbar/__tests__/__snapshots__/default-toolbar.test.jsx.snap +923 -0
  152. package/src/plugins/toolbar/__tests__/__snapshots__/editor-and-toolbar.test.jsx.snap +20 -0
  153. package/src/plugins/toolbar/__tests__/__snapshots__/toolbar-buttons.test.jsx.snap +36 -0
  154. package/src/plugins/toolbar/__tests__/__snapshots__/toolbar.test.jsx.snap +46 -0
  155. package/src/plugins/toolbar/__tests__/default-toolbar.test.jsx +94 -0
  156. package/src/plugins/toolbar/__tests__/editor-and-toolbar.test.jsx +37 -0
  157. package/src/plugins/toolbar/__tests__/toolbar-buttons.test.jsx +51 -0
  158. package/src/plugins/toolbar/__tests__/toolbar.test.jsx +106 -0
  159. package/src/plugins/toolbar/default-toolbar.jsx +206 -0
  160. package/src/plugins/toolbar/done-button.jsx +38 -0
  161. package/src/plugins/toolbar/editor-and-toolbar.jsx +257 -0
  162. package/src/plugins/toolbar/index.jsx +23 -0
  163. package/src/plugins/toolbar/toolbar-buttons.jsx +138 -0
  164. package/src/plugins/toolbar/toolbar.jsx +338 -0
  165. package/src/plugins/utils.js +31 -0
  166. package/src/serialization.jsx +621 -0
  167. package/src/theme.js +1 -0
@@ -0,0 +1,377 @@
1
+ import Hotkeys from 'slate-hotkeys';
2
+ import { IS_IOS } from 'slate-dev-environment';
3
+ import { Mark } from 'slate';
4
+ import Bold from '@material-ui/icons/FormatBold';
5
+ import FormatQuote from '@material-ui/icons/FormatQuote';
6
+ //import Code from '@material-ui/icons/Code';
7
+ import BulletedListIcon from '@material-ui/icons/FormatListBulleted';
8
+ import NumberedListIcon from '@material-ui/icons/FormatListNumbered';
9
+ import GridOn from '@material-ui/icons/GridOn';
10
+ import Image from '@material-ui/icons/Image';
11
+ import Redo from '@material-ui/icons/Redo';
12
+ import Undo from '@material-ui/icons/Undo';
13
+ import ImagePlugin from './image';
14
+ import MediaPlugin from './media';
15
+ import CharactersPlugin from './characters';
16
+ import Italic from '@material-ui/icons/FormatItalic';
17
+ import MathPlugin from './math';
18
+ import React from 'react';
19
+ import Strikethrough from '@material-ui/icons/FormatStrikethrough';
20
+ import ToolbarPlugin from './toolbar';
21
+ import Underline from '@material-ui/icons/FormatUnderlined';
22
+ import compact from 'lodash/compact';
23
+ import isEmpty from 'lodash/isEmpty';
24
+ import SoftBreakPlugin from 'slate-soft-break';
25
+ import debug from 'debug';
26
+ import List from './list';
27
+ import TablePlugin from './table';
28
+ import RespAreaPlugin from './respArea';
29
+ import HtmlPlugin from './html';
30
+ import CSSPlugin from './css';
31
+ import CustomPlugin from './customPlugin';
32
+ import RenderingPlugin from './rendering';
33
+ import TextAlign from './textAlign';
34
+
35
+ const log = debug('@pie-lib:editable-html:plugins');
36
+
37
+ export const SuperscriptIcon = () => (
38
+ <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="none">
39
+ <path
40
+ d="M22,7h-2v1h3v1h-4V7c0-0.55,0.45-1,1-1h2V5h-3V4h3c0.55,0,1,0.45,1,1v1C23,6.55,22.55,7,22,7z M5.88,20h2.66l3.4-5.42h0.12 l3.4,5.42h2.66l-4.65-7.27L17.81,6h-2.68l-3.07,4.99h-0.12L8.85,6H6.19l4.32,6.73L5.88,20z"
41
+ fill="currentColor"
42
+ />
43
+ </svg>
44
+ );
45
+
46
+ export const SubscriptIcon = () => (
47
+ <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="none">
48
+ <path
49
+ d="M22,18h-2v1h3v1h-4v-2c0-0.55,0.45-1,1-1h2v-1h-3v-1h3c0.55,0,1,0.45,1,1v1C23,17.55,22.55,18,22,18z M5.88,18h2.66 l3.4-5.42h0.12l3.4,5.42h2.66l-4.65-7.27L17.81,4h-2.68l-3.07,4.99h-0.12L8.85,4H6.19l4.32,6.73L5.88,18z"
50
+ fill="currentColor"
51
+ />
52
+ </svg>
53
+ );
54
+
55
+ export const HeadingIcon = () => (
56
+ <svg
57
+ width="30"
58
+ height="28"
59
+ viewBox="0 0 30 28"
60
+ fill="none"
61
+ xmlns="http://www.w3.org/2000/svg"
62
+ style={{ width: '20px', height: '18px' }}
63
+ >
64
+ <path
65
+ d="M27 4V24H29C29.5 24 30 24.5 30 25V27C30 27.5625 29.5 28 29 28H19C18.4375 28 18 27.5625 18 27V25C18 24.5 18.4375 24 19 24H21V16H9V24H11C11.5 24 12 24.5 12 25V27C12 27.5625 11.5 28 11 28H1C0.4375 28 0 27.5625 0 27V25C0 24.5 0.4375 24 1 24H3V4H1C0.4375 4 0 3.5625 0 3V1C0 0.5 0.4375 0 1 0H11C11.5 0 12 0.5 12 1V3C12 3.5625 11.5 4 11 4H9V12H21V4H19C18.4375 4 18 3.5625 18 3V1C18 0.5 18.4375 0 19 0H29C29.5 0 30 0.5 30 1V3C30 3.5625 29.5 4 29 4H27Z"
66
+ fill="currentColor"
67
+ />
68
+ </svg>
69
+ );
70
+ const STYLES_MAP = {
71
+ h3: {
72
+ fontSize: 'inherit',
73
+ fontWeight: 'inherit',
74
+ },
75
+ blockquote: {
76
+ background: '#f9f9f9',
77
+ borderLeft: '5px solid #ccc',
78
+ margin: '1.5em 10px',
79
+ padding: '.5em 10px',
80
+ },
81
+ };
82
+
83
+ function MarkHotkey(options) {
84
+ const { type, key, icon, tag } = options;
85
+
86
+ // Return our "plugin" object, containing the `onKeyDown` handler.
87
+ return {
88
+ name: type,
89
+ toolbar: {
90
+ isMark: true,
91
+ type,
92
+ icon,
93
+ onToggle: (change) => {
94
+ log('[onToggleMark] type: ', type);
95
+ const { selection } = change.value;
96
+
97
+ if (['blockquote', 'h3'].includes(type)) {
98
+ const texts = change.value.document.getTextsAtRangeAsArray(selection);
99
+ const onlyOneText = texts.length === 1;
100
+ let hasMark = false;
101
+ let sameMark = true;
102
+
103
+ texts.forEach((t) => {
104
+ const marks = t.getMarksAsArray();
105
+ const markIsThere = marks.find((m) => m.type === type);
106
+
107
+ if (!markIsThere) {
108
+ // not all texts have this mark
109
+ sameMark = false;
110
+ } else {
111
+ // at least one mark
112
+ hasMark = true;
113
+ }
114
+ });
115
+
116
+ const shouldContinue = onlyOneText || sameMark || !hasMark;
117
+
118
+ if (!shouldContinue) {
119
+ return change;
120
+ }
121
+
122
+ if (selection.startKey === selection.endKey && selection.anchorOffset === selection.focusOffset) {
123
+ const textNode = change.value.document.getNode(selection.startKey);
124
+
125
+ // select the whole line if there is no selection
126
+ change.moveFocusTo(textNode.key, 0).moveAnchorTo(textNode.key, textNode.text.length);
127
+
128
+ // remove toggle
129
+ const hasMark = change.value.activeMarks.find((entry) => {
130
+ return entry.type === type;
131
+ });
132
+
133
+ if (hasMark) {
134
+ change.removeMark(hasMark);
135
+ } else {
136
+ const newMark = Mark.create(type);
137
+
138
+ change.addMark(newMark);
139
+ }
140
+
141
+ // move focus to end of text
142
+ return change
143
+ .moveFocusTo(textNode.key, textNode.text.length)
144
+ .moveAnchorTo(textNode.key, textNode.text.length);
145
+ }
146
+ }
147
+
148
+ return change.toggleMark(type);
149
+ },
150
+ },
151
+ renderMark(props) {
152
+ if (props.mark.type === type) {
153
+ const { data } = props.node || {};
154
+ const jsonData = data?.toJSON() || {};
155
+ const K = tag || type;
156
+ const additionalStyles = STYLES_MAP[K];
157
+
158
+ if (additionalStyles) {
159
+ if (!jsonData.attributes) {
160
+ jsonData.attributes = {};
161
+ }
162
+
163
+ jsonData.attributes.style = {
164
+ ...jsonData.attributes.style,
165
+ ...additionalStyles,
166
+ };
167
+ }
168
+
169
+ return <K {...jsonData.attributes}>{props.children}</K>;
170
+ }
171
+ },
172
+ onKeyDown(event, change) {
173
+ // Check that the key pressed matches our `key` option.
174
+ if (!event.metaKey || event.key != key) return;
175
+
176
+ // Prevent the default characters from being inserted.
177
+ event.preventDefault();
178
+
179
+ // Toggle the mark `type`.
180
+ change.toggleMark(type);
181
+ return true;
182
+ },
183
+ };
184
+ }
185
+
186
+ export const ALL_PLUGINS = [
187
+ 'bold',
188
+ // 'code',
189
+ 'html',
190
+ 'extraCSSRules',
191
+ 'italic',
192
+ 'underline',
193
+ 'strikethrough',
194
+ 'bulleted-list',
195
+ 'numbered-list',
196
+ 'image',
197
+ 'math',
198
+ 'languageCharacters',
199
+ 'text-align',
200
+ 'blockquote',
201
+ 'h3',
202
+ 'table',
203
+ 'video',
204
+ 'audio',
205
+ 'responseArea',
206
+ 'redo',
207
+ 'undo',
208
+ 'superscript',
209
+ 'subscript',
210
+ ];
211
+
212
+ export const DEFAULT_PLUGINS = ALL_PLUGINS.filter((plug) => !['responseArea', 'h3', 'blockquote'].includes(plug));
213
+
214
+ export const EXTENSIONS_LIST = [
215
+ GridOn,
216
+ Bold,
217
+ Italic,
218
+ Strikethrough,
219
+ Underline,
220
+ SuperscriptIcon,
221
+ SubscriptIcon,
222
+ Image,
223
+ HeadingIcon,
224
+ BulletedListIcon,
225
+ NumberedListIcon,
226
+ FormatQuote,
227
+ ];
228
+
229
+ const ICON_MAP = {
230
+ undo: Undo,
231
+ redo: Redo,
232
+ };
233
+ function UndoRedo(type) {
234
+ const IconToUse = ICON_MAP[type];
235
+
236
+ return {
237
+ name: type,
238
+ toolbar: {
239
+ type,
240
+ icon: <IconToUse />,
241
+ ariaLabel: type === 'undo' ? 'Undo (revert the last action)' : 'Redo (reapply the last undone action)',
242
+ onClick: (value, onChange) => {
243
+ const change = value.change();
244
+
245
+ onChange(change[type]());
246
+ },
247
+ },
248
+ };
249
+ }
250
+
251
+ function EnterHandlingPlugin() {
252
+ return {
253
+ name: 'enterHandling',
254
+ onKeyDown: (event, change) => {
255
+ if (Hotkeys.isSplitBlock(event) && !IS_IOS) {
256
+ if (change.value.isInVoid) {
257
+ return change.collapseToStartOfNextText();
258
+ }
259
+
260
+ change.splitBlock();
261
+
262
+ const range = change.value.selection;
263
+ const newBlock = change.value.document.getClosestBlock(range.startKey);
264
+
265
+ if (newBlock.type !== 'paragraph') {
266
+ change.setNodeByKey(newBlock.key, {
267
+ type: 'paragraph',
268
+ });
269
+ }
270
+
271
+ return change;
272
+ }
273
+
274
+ return undefined;
275
+ },
276
+ };
277
+ }
278
+
279
+ export const buildPlugins = (activePlugins, customPlugins, opts) => {
280
+ log('[buildPlugins] opts: ', opts);
281
+
282
+ activePlugins = activePlugins || DEFAULT_PLUGINS;
283
+
284
+ const addIf = (key, p) => activePlugins.includes(key) && p;
285
+
286
+ const imagePlugin = opts.image && opts.image.onDelete && ImagePlugin(opts.image);
287
+ const mathPlugin = opts.math && MathPlugin(opts.math);
288
+ const respAreaPlugin =
289
+ opts.responseArea && opts.responseArea.type && RespAreaPlugin(opts.responseArea, compact([mathPlugin]));
290
+ const cssPlugin = !isEmpty(opts.extraCSSRules) && CSSPlugin(opts.extraCSSRules);
291
+
292
+ const languageCharactersPlugins = (opts?.languageCharacters || []).map((config) =>
293
+ CharactersPlugin({
294
+ ...config,
295
+ keyPadCharacterRef: opts.keyPadCharacterRef,
296
+ setKeypadInteraction: opts.setKeypadInteraction,
297
+ }),
298
+ );
299
+
300
+ const tablePlugins = [imagePlugin, mathPlugin, respAreaPlugin, ...languageCharactersPlugins];
301
+
302
+ if (opts.responseArea && opts.responseArea.type === 'math-templated') {
303
+ tablePlugins.push(respAreaPlugin);
304
+ }
305
+
306
+ let builtCustomPlugins = [];
307
+
308
+ customPlugins?.forEach((customPlugin) => {
309
+ const { event, icon, iconType, iconAlt } = customPlugin || {};
310
+
311
+ function isValidEventName(eventName) {
312
+ // Check if eventName is a non-empty string
313
+ if (typeof eventName !== 'string' || eventName.length === 0) {
314
+ return false;
315
+ }
316
+
317
+ // Regular expression to match valid event names (only alphanumeric characters and underscore)
318
+ const regex = /^[a-zA-Z0-9_]+$/;
319
+
320
+ // Check if the eventName matches the regular expression
321
+ return regex.test(eventName);
322
+ }
323
+
324
+ if (!isValidEventName(event)) {
325
+ console.error(`The event name: ${event} is not a valid event name!`);
326
+ return;
327
+ }
328
+
329
+ if (!icon && !iconType && !iconAlt) {
330
+ console.error('Your custom button requires icon, iconType and iconAlt');
331
+ return;
332
+ }
333
+
334
+ builtCustomPlugins.push(CustomPlugin('custom-plugin', customPlugin));
335
+ });
336
+
337
+ return compact([
338
+ addIf('table', TablePlugin(opts.table, compact(tablePlugins))),
339
+ addIf('bold', MarkHotkey({ key: 'b', type: 'bold', icon: <Bold />, tag: 'strong' })),
340
+ // addIf('code', MarkHotkey({ key: '`', type: 'code', icon: <Code /> })),
341
+ addIf('italic', MarkHotkey({ key: 'i', type: 'italic', icon: <Italic />, tag: 'em' })),
342
+ addIf(
343
+ 'strikethrough',
344
+ MarkHotkey({
345
+ key: '~',
346
+ type: 'strikethrough',
347
+ icon: <Strikethrough />,
348
+ tag: 'del',
349
+ }),
350
+ ),
351
+ addIf('underline', MarkHotkey({ key: 'u', type: 'underline', icon: <Underline />, tag: 'u' })),
352
+ // icon should be modifies accordingly
353
+ addIf('superscript', MarkHotkey({ type: 'sup', icon: <SuperscriptIcon />, tag: 'sup' })),
354
+ // icon should be modifies accordingly
355
+ addIf('subscript', MarkHotkey({ type: 'sub', icon: <SubscriptIcon />, tag: 'sub' })),
356
+ addIf('image', imagePlugin),
357
+ addIf('video', MediaPlugin('video', opts.media)),
358
+ addIf('audio', MediaPlugin('audio', opts.media)),
359
+ addIf('math', mathPlugin),
360
+ ...languageCharactersPlugins.map((plugin) => addIf('languageCharacters', plugin)),
361
+ addIf('text-align', TextAlign(opts.textAlign)),
362
+ addIf('blockquote', MarkHotkey({ key: 'q', type: 'blockquote', icon: <FormatQuote />, tag: 'blockquote' })),
363
+ addIf('h3', MarkHotkey({ key: 'h3', type: 'h3', icon: <HeadingIcon />, tag: 'h3' })),
364
+ addIf('bulleted-list', List({ key: 'l', type: 'ul_list', icon: <BulletedListIcon /> })),
365
+ addIf('numbered-list', List({ key: 'n', type: 'ol_list', icon: <NumberedListIcon /> })),
366
+ addIf('undo', UndoRedo('undo')),
367
+ addIf('redo', UndoRedo('redo')),
368
+ ToolbarPlugin(opts.toolbar),
369
+ SoftBreakPlugin({ shift: true }),
370
+ ...builtCustomPlugins,
371
+ addIf('responseArea', respAreaPlugin),
372
+ cssPlugin,
373
+ addIf('html', HtmlPlugin(opts.html)),
374
+ EnterHandlingPlugin(),
375
+ RenderingPlugin(),
376
+ ]);
377
+ };
@@ -0,0 +1,54 @@
1
+ import React from 'react';
2
+
3
+ import List, { serialization } from '../index';
4
+ import debug from 'debug';
5
+
6
+ const log = debug('@pie-lib:editable-html:test:plugins:list');
7
+
8
+ describe('ListPlugin', () => {
9
+ let next;
10
+
11
+ describe('deserialize', () => {
12
+ next = jest.fn();
13
+
14
+ const assertDeserialize = (tagName, expectedType) => {
15
+ it(`should deserialize ${tagName} to ${expectedType}`, () => {
16
+ const out = serialization.deserialize({ tagName, children: [], childNodes: [] }, next);
17
+
18
+ expect(out).toMatchObject({ object: 'block', type: expectedType });
19
+ expect(next).toHaveBeenCalledWith([]);
20
+ });
21
+ };
22
+ assertDeserialize('ul', 'ul_list');
23
+ assertDeserialize('ol', 'ol_list');
24
+ assertDeserialize('li', 'list_item');
25
+ });
26
+
27
+ describe('serialize', () => {
28
+ const assertSerialize = (type, expectedType) => {
29
+ it(`should serialize ${type} to ${expectedType}`, () => {
30
+ const out = serialization.serialize({ object: 'block', type }, {});
31
+ log('out: ', out);
32
+ expect(out.type).toMatch(expectedType);
33
+ });
34
+ };
35
+ assertSerialize('ul_list', 'ul');
36
+ assertSerialize('ol_list', 'ol');
37
+ assertSerialize('list_item', 'li');
38
+ });
39
+
40
+ describe('renderNode', () => {
41
+ let plugin = List({});
42
+
43
+ const assertRenderNode = (type, expectedType) => {
44
+ it(`should renderNode ${type} to ${expectedType}`, () => {
45
+ const out = plugin.renderNode({ node: { type } });
46
+ expect(out.type).toMatch(expectedType);
47
+ });
48
+ };
49
+
50
+ assertRenderNode('ul_list', 'ul');
51
+ assertRenderNode('ol_list', 'ol');
52
+ assertRenderNode('list_item', 'li');
53
+ });
54
+ });