@crystallize/design-system 1.3.2 → 1.4.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 (246) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/TableComponent-I2YOOYOU.css +281 -0
  3. package/dist/TableComponent-QINOO453.mjs +1377 -0
  4. package/dist/arrow-clockwise-Z2G6UEGP.svg +1 -0
  5. package/dist/arrow-counterclockwise-2O5EYVJT.svg +1 -0
  6. package/dist/bg-color-HB2WDYGO.svg +1 -0
  7. package/dist/camera-CR7D2PNH.svg +1 -0
  8. package/dist/caret-right-fill-FFBNEXVX.svg +1 -0
  9. package/dist/chat-square-quote-CI6PUJHH.svg +1 -0
  10. package/dist/chevron-down-3FRWSIKS.svg +1 -0
  11. package/dist/chunk-VUXQZRSP.mjs +737 -0
  12. package/dist/clipboard-OSEFDF25.svg +1 -0
  13. package/dist/close-FH57ZMJF.svg +1 -0
  14. package/dist/code-SEVR6TIQ.svg +1 -0
  15. package/dist/copy-DMGDODUL.svg +1 -0
  16. package/dist/diagram-2-CEJUD2B4.svg +1 -0
  17. package/dist/download-JXUGIUCX.svg +1 -0
  18. package/dist/draggable-block-menu-KKHDNKJA.svg +1 -0
  19. package/dist/dropdown-more-BHZ5COKX.svg +1 -0
  20. package/dist/file-image-TIQPFJX4.svg +1 -0
  21. package/dist/filetype-gif-OG2BEYYK.svg +1 -0
  22. package/dist/font-color-J4GA3ZJO.svg +1 -0
  23. package/dist/font-family-ZU5N6TTE.svg +1 -0
  24. package/dist/gear-ICMT4NTP.svg +1 -0
  25. package/dist/horizontal-rule-N6RD2V7H.svg +1 -0
  26. package/dist/indent-MJ6JIMCK.svg +1 -0
  27. package/dist/index.css +2711 -315
  28. package/dist/index.d.ts +145 -40
  29. package/dist/index.js +10376 -1481
  30. package/dist/index.mjs +7609 -746
  31. package/dist/journal-code-XUT44HDV.svg +1 -0
  32. package/dist/justify-J7X5JEEX.svg +1 -0
  33. package/dist/link-W52N4JKZ.svg +1 -0
  34. package/dist/list-ol-2ZEUN4Z7.svg +1 -0
  35. package/dist/list-ul-DVKNUP47.svg +1 -0
  36. package/dist/lock-WCYOZOHW.svg +1 -0
  37. package/dist/lock-fill-JZSKOSHK.svg +1 -0
  38. package/dist/markdown-4BGQNLLT.svg +1 -0
  39. package/dist/mic-H5FNOMM7.svg +1 -0
  40. package/dist/outdent-2LUMUMIP.svg +1 -0
  41. package/dist/paint-bucket-VCISMZTH.svg +1 -0
  42. package/dist/palette-SWGFPRWZ.svg +1 -0
  43. package/dist/pencil-fill-STFSC26F.svg +1 -0
  44. package/dist/plug-HGGGEVS3.svg +1 -0
  45. package/dist/plug-fill-OTG3U4TN.svg +1 -0
  46. package/dist/plus-CQISIKEC.svg +1 -0
  47. package/dist/plus-slash-minus-N22JU4TI.svg +1 -0
  48. package/dist/prettier-WUJ7B5NV.svg +1 -0
  49. package/dist/prettier-error-DYJSLYDP.svg +1 -0
  50. package/dist/square-check-UTG6FU6D.svg +1 -0
  51. package/dist/success-YVXUMPEZ.svg +1 -0
  52. package/dist/table-BR6DI4ZQ.svg +1 -0
  53. package/dist/text-center-UQI6PAEF.svg +1 -0
  54. package/dist/text-left-KT2B6TR3.svg +1 -0
  55. package/dist/text-paragraph-MFTUIIQG.svg +1 -0
  56. package/dist/text-right-SKELPISG.svg +1 -0
  57. package/dist/trash-UOM6D7TD.svg +1 -0
  58. package/dist/type-bold-PY7COC3N.svg +1 -0
  59. package/dist/type-h1-6KJP7YOM.svg +1 -0
  60. package/dist/type-h2-VHI2USC3.svg +1 -0
  61. package/dist/type-h3-JIU77CHO.svg +1 -0
  62. package/dist/type-h4-P5EHKDAL.svg +1 -0
  63. package/dist/type-h5-CS2KYVRG.svg +1 -0
  64. package/dist/type-h6-J2O74LJZ.svg +1 -0
  65. package/dist/type-italic-3DSFOSG2.svg +1 -0
  66. package/dist/type-strikethrough-E2KKQFSX.svg +1 -0
  67. package/dist/type-subscript-BMPTRIBU.svg +1 -0
  68. package/dist/type-superscript-EDF6EPAA.svg +1 -0
  69. package/dist/type-underline-CBFA5VLF.svg +1 -0
  70. package/dist/upload-Q6KICGZW.svg +1 -0
  71. package/dist/user-EOI2NEFZ.svg +1 -0
  72. package/package.json +30 -6
  73. package/src/dialog/dialog.tsx +1 -0
  74. package/src/icon-button/icon-button.css +16 -14
  75. package/src/index.ts +4 -4
  76. package/src/input/input.css +1 -1
  77. package/src/input-with-label/input-with-label.css +1 -1
  78. package/src/rich-text-editor/appSettings.ts +28 -0
  79. package/src/rich-text-editor/context/SettingsContext.tsx +71 -0
  80. package/src/rich-text-editor/context/SharedAutocompleteContext.tsx +60 -0
  81. package/src/rich-text-editor/context/SharedHistoryContext.tsx +25 -0
  82. package/src/rich-text-editor/hooks/useReport.ts +64 -0
  83. package/src/rich-text-editor/images/cat-typing.gif +0 -0
  84. package/src/rich-text-editor/images/emoji/1F600.png +0 -0
  85. package/src/rich-text-editor/images/emoji/1F641.png +0 -0
  86. package/src/rich-text-editor/images/emoji/1F642.png +0 -0
  87. package/src/rich-text-editor/images/emoji/2764.png +0 -0
  88. package/src/rich-text-editor/images/emoji/LICENSE.md +5 -0
  89. package/src/rich-text-editor/images/icons/LICENSE.md +5 -0
  90. package/src/rich-text-editor/images/icons/arrow-clockwise.svg +1 -0
  91. package/src/rich-text-editor/images/icons/arrow-counterclockwise.svg +1 -0
  92. package/src/rich-text-editor/images/icons/bg-color.svg +1 -0
  93. package/src/rich-text-editor/images/icons/camera.svg +1 -0
  94. package/src/rich-text-editor/images/icons/card-checklist.svg +1 -0
  95. package/src/rich-text-editor/images/icons/caret-right-fill.svg +1 -0
  96. package/src/rich-text-editor/images/icons/chat-left-text.svg +1 -0
  97. package/src/rich-text-editor/images/icons/chat-right-dots.svg +1 -0
  98. package/src/rich-text-editor/images/icons/chat-right-text.svg +1 -0
  99. package/src/rich-text-editor/images/icons/chat-right.svg +1 -0
  100. package/src/rich-text-editor/images/icons/chat-square-quote.svg +1 -0
  101. package/src/rich-text-editor/images/icons/chevron-down.svg +1 -0
  102. package/src/rich-text-editor/images/icons/clipboard.svg +1 -0
  103. package/src/rich-text-editor/images/icons/close.svg +1 -0
  104. package/src/rich-text-editor/images/icons/code.svg +1 -0
  105. package/src/rich-text-editor/images/icons/comments.svg +1 -0
  106. package/src/rich-text-editor/images/icons/copy.svg +1 -0
  107. package/src/rich-text-editor/images/icons/diagram-2.svg +1 -0
  108. package/src/rich-text-editor/images/icons/download.svg +1 -0
  109. package/src/rich-text-editor/images/icons/draggable-block-menu.svg +1 -0
  110. package/src/rich-text-editor/images/icons/dropdown-more.svg +1 -0
  111. package/src/rich-text-editor/images/icons/figma.svg +1 -0
  112. package/src/rich-text-editor/images/icons/file-image.svg +1 -0
  113. package/src/rich-text-editor/images/icons/filetype-gif.svg +1 -0
  114. package/src/rich-text-editor/images/icons/font-color.svg +1 -0
  115. package/src/rich-text-editor/images/icons/font-family.svg +1 -0
  116. package/src/rich-text-editor/images/icons/gear.svg +1 -0
  117. package/src/rich-text-editor/images/icons/horizontal-rule.svg +1 -0
  118. package/src/rich-text-editor/images/icons/indent.svg +1 -0
  119. package/src/rich-text-editor/images/icons/journal-code.svg +1 -0
  120. package/src/rich-text-editor/images/icons/journal-text.svg +1 -0
  121. package/src/rich-text-editor/images/icons/justify.svg +1 -0
  122. package/src/rich-text-editor/images/icons/link.svg +1 -0
  123. package/src/rich-text-editor/images/icons/list-ol.svg +1 -0
  124. package/src/rich-text-editor/images/icons/list-ul.svg +1 -0
  125. package/src/rich-text-editor/images/icons/lock-fill.svg +1 -0
  126. package/src/rich-text-editor/images/icons/lock.svg +1 -0
  127. package/src/rich-text-editor/images/icons/markdown.svg +1 -0
  128. package/src/rich-text-editor/images/icons/mic.svg +1 -0
  129. package/src/rich-text-editor/images/icons/outdent.svg +1 -0
  130. package/src/rich-text-editor/images/icons/paint-bucket.svg +1 -0
  131. package/src/rich-text-editor/images/icons/palette.svg +1 -0
  132. package/src/rich-text-editor/images/icons/pencil-fill.svg +1 -0
  133. package/src/rich-text-editor/images/icons/plug-fill.svg +1 -0
  134. package/src/rich-text-editor/images/icons/plug.svg +1 -0
  135. package/src/rich-text-editor/images/icons/plus-slash-minus.svg +1 -0
  136. package/src/rich-text-editor/images/icons/plus.svg +1 -0
  137. package/src/rich-text-editor/images/icons/prettier-error.svg +1 -0
  138. package/src/rich-text-editor/images/icons/prettier.svg +1 -0
  139. package/src/rich-text-editor/images/icons/send.svg +1 -0
  140. package/src/rich-text-editor/images/icons/square-check.svg +1 -0
  141. package/src/rich-text-editor/images/icons/sticky.svg +1 -0
  142. package/src/rich-text-editor/images/icons/success.svg +1 -0
  143. package/src/rich-text-editor/images/icons/table.svg +1 -0
  144. package/src/rich-text-editor/images/icons/text-center.svg +1 -0
  145. package/src/rich-text-editor/images/icons/text-left.svg +1 -0
  146. package/src/rich-text-editor/images/icons/text-paragraph.svg +1 -0
  147. package/src/rich-text-editor/images/icons/text-right.svg +1 -0
  148. package/src/rich-text-editor/images/icons/trash.svg +1 -0
  149. package/src/rich-text-editor/images/icons/trash3.svg +1 -0
  150. package/src/rich-text-editor/images/icons/tweet.svg +1 -0
  151. package/src/rich-text-editor/images/icons/type-bold.svg +1 -0
  152. package/src/rich-text-editor/images/icons/type-h1.svg +1 -0
  153. package/src/rich-text-editor/images/icons/type-h2.svg +1 -0
  154. package/src/rich-text-editor/images/icons/type-h3.svg +1 -0
  155. package/src/rich-text-editor/images/icons/type-h4.svg +1 -0
  156. package/src/rich-text-editor/images/icons/type-h5.svg +1 -0
  157. package/src/rich-text-editor/images/icons/type-h6.svg +1 -0
  158. package/src/rich-text-editor/images/icons/type-italic.svg +1 -0
  159. package/src/rich-text-editor/images/icons/type-strikethrough.svg +1 -0
  160. package/src/rich-text-editor/images/icons/type-subscript.svg +1 -0
  161. package/src/rich-text-editor/images/icons/type-superscript.svg +1 -0
  162. package/src/rich-text-editor/images/icons/type-underline.svg +1 -0
  163. package/src/rich-text-editor/images/icons/upload.svg +1 -0
  164. package/src/rich-text-editor/images/icons/user.svg +1 -0
  165. package/src/rich-text-editor/images/icons/youtube.svg +1 -0
  166. package/src/rich-text-editor/images/image/LICENSE.md +5 -0
  167. package/src/rich-text-editor/images/landscape.jpg +0 -0
  168. package/src/rich-text-editor/images/logo.svg +1 -0
  169. package/src/rich-text-editor/images/yellow-flower-small.jpg +0 -0
  170. package/src/rich-text-editor/images/yellow-flower.jpg +0 -0
  171. package/src/rich-text-editor/index.ts +1 -0
  172. package/src/rich-text-editor/model/crystallize-rich-text-types/code.ts +39 -0
  173. package/src/rich-text-editor/model/crystallize-rich-text-types/headings.ts +12 -0
  174. package/src/rich-text-editor/model/crystallize-rich-text-types/index.ts +69 -0
  175. package/src/rich-text-editor/model/crystallize-rich-text-types/link.ts +9 -0
  176. package/src/rich-text-editor/model/crystallize-rich-text-types/table.ts +16 -0
  177. package/src/rich-text-editor/model/crystallize-to-lexical.ts +186 -0
  178. package/src/rich-text-editor/model/lexical-to-crystallize.ts +232 -0
  179. package/src/rich-text-editor/nodes/AutocompleteNode.tsx +96 -0
  180. package/src/rich-text-editor/nodes/BaseNodes.ts +45 -0
  181. package/src/rich-text-editor/nodes/KeywordNode.ts +73 -0
  182. package/src/rich-text-editor/nodes/TableCellNodes.ts +31 -0
  183. package/src/rich-text-editor/nodes/TableComponent.tsx +1547 -0
  184. package/src/rich-text-editor/nodes/TableNode.tsx +398 -0
  185. package/src/rich-text-editor/plugins/ActionsPlugin/index.tsx +83 -0
  186. package/src/rich-text-editor/plugins/AutoLinkPlugin/index.tsx +47 -0
  187. package/src/rich-text-editor/plugins/AutocompletePlugin/index.tsx +2536 -0
  188. package/src/rich-text-editor/plugins/CodeActionMenuPlugin/components/CopyButton/index.tsx +60 -0
  189. package/src/rich-text-editor/plugins/CodeActionMenuPlugin/components/PrettierButton/index.css +14 -0
  190. package/src/rich-text-editor/plugins/CodeActionMenuPlugin/components/PrettierButton/index.tsx +140 -0
  191. package/src/rich-text-editor/plugins/CodeActionMenuPlugin/index.css +46 -0
  192. package/src/rich-text-editor/plugins/CodeActionMenuPlugin/index.tsx +155 -0
  193. package/src/rich-text-editor/plugins/CodeHighlightPlugin/index.ts +21 -0
  194. package/src/rich-text-editor/plugins/ComponentPickerPlugin/index.tsx +320 -0
  195. package/src/rich-text-editor/plugins/DragDropPastePlugin/index.ts +40 -0
  196. package/src/rich-text-editor/plugins/DraggableBlockPlugin/index.css +36 -0
  197. package/src/rich-text-editor/plugins/DraggableBlockPlugin/index.tsx +368 -0
  198. package/src/rich-text-editor/plugins/FloatingLinkEditorPlugin/index.css +40 -0
  199. package/src/rich-text-editor/plugins/FloatingLinkEditorPlugin/index.tsx +305 -0
  200. package/src/rich-text-editor/plugins/FloatingTextFormatToolbarPlugin/index.css +128 -0
  201. package/src/rich-text-editor/plugins/FloatingTextFormatToolbarPlugin/index.tsx +351 -0
  202. package/src/rich-text-editor/plugins/LinkPlugin/index.tsx +16 -0
  203. package/src/rich-text-editor/plugins/ListMaxIndentLevelPlugin/index.ts +86 -0
  204. package/src/rich-text-editor/plugins/MarkdownShortcutPlugin/index.tsx +16 -0
  205. package/src/rich-text-editor/plugins/MarkdownTransformers/index.ts +195 -0
  206. package/src/rich-text-editor/plugins/MaxLengthPlugin/index.tsx +49 -0
  207. package/src/rich-text-editor/plugins/SpeechToTextPlugin/index.ts +113 -0
  208. package/src/rich-text-editor/plugins/TabFocusPlugin/index.tsx +65 -0
  209. package/src/rich-text-editor/plugins/TableActionMenuPlugin/index.tsx +481 -0
  210. package/src/rich-text-editor/plugins/TableCellResizer/index.css +12 -0
  211. package/src/rich-text-editor/plugins/TableCellResizer/index.tsx +386 -0
  212. package/src/rich-text-editor/plugins/TablePlugin.tsx +190 -0
  213. package/src/rich-text-editor/plugins/ToolbarPlugin/index.tsx +726 -0
  214. package/src/rich-text-editor/plugins/TreeViewPlugin/index.tsx +25 -0
  215. package/src/rich-text-editor/plugins/TypingPerfPlugin/index.ts +117 -0
  216. package/src/rich-text-editor/rich-text-editor.css +1396 -0
  217. package/src/rich-text-editor/rich-text-editor.stories.tsx +385 -0
  218. package/src/rich-text-editor/rich-text-editor.tsx +228 -0
  219. package/src/rich-text-editor/tests/rich-text-editor-basic-rendering.test.tsx +47 -0
  220. package/src/rich-text-editor/tests/rich-text-editor-code.test.tsx +39 -0
  221. package/src/rich-text-editor/tests/rich-text-editor-model-basics.test.tsx +56 -0
  222. package/src/rich-text-editor/tests/rich-text-editor-model-conversions.test.tsx +195 -0
  223. package/src/rich-text-editor/tests/rich-text-editor-onchange.test.tsx +37 -0
  224. package/src/rich-text-editor/tests/rich-text-editor-quote.test.tsx +36 -0
  225. package/src/rich-text-editor/tests/rich-text-editor-text-formats.test.tsx +135 -0
  226. package/src/rich-text-editor/tests/rich-text-editor-typing.test.tsx +73 -0
  227. package/src/rich-text-editor/tests/utils.ts +23 -0
  228. package/src/rich-text-editor/themes/PlaygroundEditorTheme.css +433 -0
  229. package/src/rich-text-editor/themes/PlaygroundEditorTheme.ts +113 -0
  230. package/src/rich-text-editor/types.ts +5 -0
  231. package/src/rich-text-editor/ui/ContentEditable.css +13 -0
  232. package/src/rich-text-editor/ui/ContentEditable.tsx +15 -0
  233. package/src/rich-text-editor/ui/LinkPreview.css +57 -0
  234. package/src/rich-text-editor/ui/LinkPreview.tsx +169 -0
  235. package/src/rich-text-editor/utils/environment.ts +1 -0
  236. package/src/rich-text-editor/utils/getDOMRangeRect.ts +42 -0
  237. package/src/rich-text-editor/utils/getSelectedNode.ts +27 -0
  238. package/src/rich-text-editor/utils/guard.ts +10 -0
  239. package/src/rich-text-editor/utils/isMobileWidth.ts +7 -0
  240. package/src/rich-text-editor/utils/joinClasses.ts +13 -0
  241. package/src/rich-text-editor/utils/point.ts +55 -0
  242. package/src/rich-text-editor/utils/rect.ts +158 -0
  243. package/src/rich-text-editor/utils/setFloatingElemPosition.ts +46 -0
  244. package/src/rich-text-editor/utils/swipe.ts +127 -0
  245. package/src/rich-text-editor/utils/url.ts +33 -0
  246. package/src/Tokens.stories.tsx +0 -18
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import {
10
+ $createParagraphNode,
11
+ $createTextNode,
12
+ $isElementNode,
13
+ $isParagraphNode,
14
+ $isTextNode,
15
+ type ElementNode,
16
+ type LexicalNode,
17
+ } from 'lexical';
18
+ import {
19
+ CHECK_LIST,
20
+ ELEMENT_TRANSFORMERS,
21
+ TEXT_FORMAT_TRANSFORMERS,
22
+ TEXT_MATCH_TRANSFORMERS,
23
+ type ElementTransformer,
24
+ type Transformer,
25
+ } from '@lexical/markdown';
26
+ import {
27
+ $createHorizontalRuleNode,
28
+ $isHorizontalRuleNode,
29
+ HorizontalRuleNode,
30
+ } from '@lexical/react/LexicalHorizontalRuleNode';
31
+ import {
32
+ $createTableCellNode,
33
+ $createTableNode,
34
+ $createTableRowNode,
35
+ $isTableNode,
36
+ $isTableRowNode,
37
+ TableCellHeaderStates,
38
+ TableCellNode,
39
+ TableNode,
40
+ TableRowNode,
41
+ } from '@lexical/table';
42
+
43
+ export const HR: ElementTransformer = {
44
+ dependencies: [HorizontalRuleNode],
45
+ export: (node: LexicalNode) => {
46
+ return $isHorizontalRuleNode(node) ? '***' : null;
47
+ },
48
+ regExp: /^(---|\*\*\*|___)\s?$/,
49
+ replace: (parentNode, _1, _2, isImport) => {
50
+ const line = $createHorizontalRuleNode();
51
+
52
+ // TODO: Get rid of isImport flag
53
+ if (isImport || parentNode.getNextSibling() != null) {
54
+ parentNode.replace(line);
55
+ } else {
56
+ parentNode.insertBefore(line);
57
+ }
58
+
59
+ line.selectNext();
60
+ },
61
+ type: 'element',
62
+ };
63
+
64
+ // Very primitive table setup
65
+ const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
66
+
67
+ export const TABLE: ElementTransformer = {
68
+ // TODO: refactor transformer for new TableNode
69
+ dependencies: [TableNode, TableRowNode, TableCellNode],
70
+ export: (node: LexicalNode, exportChildren: (elementNode: ElementNode) => string) => {
71
+ if (!$isTableNode(node)) {
72
+ return null;
73
+ }
74
+
75
+ const output = [];
76
+
77
+ for (const row of node.getChildren()) {
78
+ const rowOutput = [];
79
+
80
+ if ($isTableRowNode(row)) {
81
+ for (const cell of row.getChildren()) {
82
+ // It's TableCellNode (hence ElementNode) so it's just to make flow happy
83
+ if ($isElementNode(cell)) {
84
+ rowOutput.push(exportChildren(cell));
85
+ }
86
+ }
87
+ }
88
+
89
+ output.push(`| ${rowOutput.join(' | ')} |`);
90
+ }
91
+
92
+ return output.join('\n');
93
+ },
94
+ regExp: TABLE_ROW_REG_EXP,
95
+ replace: (parentNode, _1, match) => {
96
+ const matchCells = mapToTableCells(match[0]);
97
+
98
+ if (matchCells == null) {
99
+ return;
100
+ }
101
+
102
+ const rows = [matchCells];
103
+ let sibling = parentNode.getPreviousSibling();
104
+ let maxCells = matchCells.length;
105
+
106
+ while (sibling) {
107
+ if (!$isParagraphNode(sibling)) {
108
+ break;
109
+ }
110
+
111
+ if (sibling.getChildrenSize() !== 1) {
112
+ break;
113
+ }
114
+
115
+ const firstChild = sibling.getFirstChild();
116
+
117
+ if (!$isTextNode(firstChild)) {
118
+ break;
119
+ }
120
+
121
+ const cells = mapToTableCells(firstChild.getTextContent());
122
+
123
+ if (cells == null) {
124
+ break;
125
+ }
126
+
127
+ maxCells = Math.max(maxCells, cells.length);
128
+ rows.unshift(cells);
129
+ const previousSibling = sibling.getPreviousSibling();
130
+ sibling.remove();
131
+ sibling = previousSibling;
132
+ }
133
+
134
+ const table = $createTableNode();
135
+
136
+ for (const cells of rows) {
137
+ const tableRow = $createTableRowNode();
138
+ table.append(tableRow);
139
+
140
+ for (let i = 0; i < maxCells; i++) {
141
+ tableRow.append(i < cells.length ? cells[i] : createTableCell(null));
142
+ }
143
+ }
144
+
145
+ const previousSibling = parentNode.getPreviousSibling();
146
+ if ($isTableNode(previousSibling) && getTableColumnsSize(previousSibling) === maxCells) {
147
+ previousSibling.append(...table.getChildren());
148
+ parentNode.remove();
149
+ } else {
150
+ parentNode.replace(table);
151
+ }
152
+
153
+ table.selectEnd();
154
+ },
155
+ type: 'element',
156
+ };
157
+
158
+ function getTableColumnsSize(table: TableNode) {
159
+ const row = table.getFirstChild();
160
+ return $isTableRowNode(row) ? row.getChildrenSize() : 0;
161
+ }
162
+
163
+ const createTableCell = (textContent: string | null | undefined): TableCellNode => {
164
+ const cell = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
165
+ const paragraph = $createParagraphNode();
166
+
167
+ if (textContent != null) {
168
+ paragraph.append($createTextNode(textContent.trim()));
169
+ }
170
+
171
+ cell.append(paragraph);
172
+ return cell;
173
+ };
174
+
175
+ const mapToTableCells = (textContent: string): Array<TableCellNode> | null => {
176
+ // TODO:
177
+ // For now plain text, single node. Can be expanded to more complex content
178
+ // including formatted text
179
+ const match = textContent.match(TABLE_ROW_REG_EXP);
180
+
181
+ if (!match || !match[1]) {
182
+ return null;
183
+ }
184
+
185
+ return match[1].split('|').map(text => createTableCell(text));
186
+ };
187
+
188
+ export const PLAYGROUND_TRANSFORMERS: Array<Transformer> = [
189
+ TABLE,
190
+ HR,
191
+ CHECK_LIST,
192
+ ...ELEMENT_TRANSFORMERS,
193
+ ...TEXT_FORMAT_TRANSFORMERS,
194
+ ...TEXT_MATCH_TRANSFORMERS,
195
+ ];
@@ -0,0 +1,49 @@
1
+ import { useEffect } from 'react';
2
+ import { $getSelection, $isRangeSelection, EditorState, RootNode } from 'lexical';
3
+ /**
4
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ *
9
+ */
10
+
11
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
12
+ import { trimTextContentFromAnchor } from '@lexical/selection';
13
+ import { $restoreEditorState } from '@lexical/utils';
14
+
15
+ export function MaxLengthPlugin({ maxLength }: { maxLength: number }): null {
16
+ const [editor] = useLexicalComposerContext();
17
+
18
+ useEffect(() => {
19
+ let lastRestoredEditorState: EditorState | null = null;
20
+
21
+ return editor.registerNodeTransform(RootNode, (rootNode: RootNode) => {
22
+ const selection = $getSelection();
23
+ if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
24
+ return;
25
+ }
26
+ const prevEditorState = editor.getEditorState();
27
+ const prevTextContent = prevEditorState.read(() => rootNode.getTextContent());
28
+ const textContent = rootNode.getTextContent();
29
+ if (prevTextContent !== textContent) {
30
+ const textLength = textContent.length;
31
+ const delCount = textLength - maxLength;
32
+ const anchor = selection.anchor;
33
+
34
+ if (delCount > 0) {
35
+ // Restore the old editor state instead if the last
36
+ // text content was already at the limit.
37
+ if (prevTextContent.length === maxLength && lastRestoredEditorState !== prevEditorState) {
38
+ lastRestoredEditorState = prevEditorState;
39
+ $restoreEditorState(editor, prevEditorState);
40
+ } else {
41
+ trimTextContentFromAnchor(editor, anchor, delCount);
42
+ }
43
+ }
44
+ }
45
+ });
46
+ }, [editor, maxLength]);
47
+
48
+ return null;
49
+ }
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import type { LexicalCommand, LexicalEditor, RangeSelection } from 'lexical';
10
+
11
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
12
+ import {
13
+ $getSelection,
14
+ $isRangeSelection,
15
+ COMMAND_PRIORITY_EDITOR,
16
+ createCommand,
17
+ REDO_COMMAND,
18
+ UNDO_COMMAND,
19
+ } from 'lexical';
20
+ import { useEffect, useRef, useState } from 'react';
21
+
22
+ import useReport from '../../hooks/useReport';
23
+
24
+ export const SPEECH_TO_TEXT_COMMAND: LexicalCommand<boolean> = createCommand('SPEECH_TO_TEXT_COMMAND');
25
+
26
+ const VOICE_COMMANDS: Readonly<Record<string, (arg0: { editor: LexicalEditor; selection: RangeSelection }) => void>> = {
27
+ '\n': ({ selection }) => {
28
+ selection.insertParagraph();
29
+ },
30
+ redo: ({ editor }) => {
31
+ editor.dispatchCommand(REDO_COMMAND, undefined);
32
+ },
33
+ undo: ({ editor }) => {
34
+ editor.dispatchCommand(UNDO_COMMAND, undefined);
35
+ },
36
+ };
37
+
38
+ export const SUPPORT_SPEECH_RECOGNITION: boolean =
39
+ typeof window !== 'undefined' && ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window);
40
+
41
+ function SpeechToTextPlugin(): null {
42
+ const [editor] = useLexicalComposerContext();
43
+ const [isEnabled, setIsEnabled] = useState<boolean>(false);
44
+ const SpeechRecognition =
45
+ // @ts-ignore
46
+ window.SpeechRecognition || window.webkitSpeechRecognition;
47
+ const recognition = useRef<typeof SpeechRecognition | null>(null);
48
+ const report = useReport();
49
+
50
+ useEffect(() => {
51
+ if (isEnabled && recognition.current === null) {
52
+ recognition.current = new SpeechRecognition();
53
+ recognition.current.continuous = true;
54
+ recognition.current.interimResults = true;
55
+ recognition.current.addEventListener('result', (event: typeof SpeechRecognition) => {
56
+ const resultItem = event.results.item(event.resultIndex);
57
+ const { transcript } = resultItem.item(0);
58
+ report(transcript);
59
+
60
+ if (!resultItem.isFinal) {
61
+ return;
62
+ }
63
+
64
+ editor.update(() => {
65
+ const selection = $getSelection();
66
+
67
+ if ($isRangeSelection(selection)) {
68
+ const command = VOICE_COMMANDS[transcript.toLowerCase().trim()];
69
+
70
+ if (command) {
71
+ command({
72
+ editor,
73
+ selection,
74
+ });
75
+ } else if (transcript.match(/\s*\n\s*/)) {
76
+ selection.insertParagraph();
77
+ } else {
78
+ selection.insertText(transcript);
79
+ }
80
+ }
81
+ });
82
+ });
83
+ }
84
+
85
+ if (recognition.current) {
86
+ if (isEnabled) {
87
+ recognition.current.start();
88
+ } else {
89
+ recognition.current.stop();
90
+ }
91
+ }
92
+
93
+ return () => {
94
+ if (recognition.current !== null) {
95
+ recognition.current.stop();
96
+ }
97
+ };
98
+ }, [SpeechRecognition, editor, isEnabled, report]);
99
+ useEffect(() => {
100
+ return editor.registerCommand(
101
+ SPEECH_TO_TEXT_COMMAND,
102
+ (_isEnabled: boolean) => {
103
+ setIsEnabled(_isEnabled);
104
+ return true;
105
+ },
106
+ COMMAND_PRIORITY_EDITOR,
107
+ );
108
+ }, [editor]);
109
+
110
+ return null;
111
+ }
112
+
113
+ export default (SUPPORT_SPEECH_RECOGNITION ? SpeechToTextPlugin : () => null) as () => null;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
10
+ import {
11
+ $getSelection,
12
+ $isRangeSelection,
13
+ $setSelection,
14
+ FOCUS_COMMAND,
15
+ } from 'lexical';
16
+ import {useEffect} from 'react';
17
+
18
+ const COMMAND_PRIORITY_LOW = 1;
19
+ const TAB_TO_FOCUS_INTERVAL = 100;
20
+
21
+ let lastTabKeyDownTimestamp = 0;
22
+ let hasRegisteredKeyDownListener = false;
23
+
24
+ function registerKeyTimeStampTracker() {
25
+ window.addEventListener(
26
+ 'keydown',
27
+ (event: KeyboardEvent) => {
28
+ // Tab
29
+ if (event.keyCode === 9) {
30
+ lastTabKeyDownTimestamp = event.timeStamp;
31
+ }
32
+ },
33
+ true,
34
+ );
35
+ }
36
+
37
+ export default function TabFocusPlugin(): null {
38
+ const [editor] = useLexicalComposerContext();
39
+
40
+ useEffect(() => {
41
+ if (!hasRegisteredKeyDownListener) {
42
+ registerKeyTimeStampTracker();
43
+ hasRegisteredKeyDownListener = true;
44
+ }
45
+
46
+ return editor.registerCommand(
47
+ FOCUS_COMMAND,
48
+ (event: FocusEvent) => {
49
+ const selection = $getSelection();
50
+ if ($isRangeSelection(selection)) {
51
+ if (
52
+ lastTabKeyDownTimestamp + TAB_TO_FOCUS_INTERVAL >
53
+ event.timeStamp
54
+ ) {
55
+ $setSelection(selection.clone());
56
+ }
57
+ }
58
+ return false;
59
+ },
60
+ COMMAND_PRIORITY_LOW,
61
+ );
62
+ }, [editor]);
63
+
64
+ return null;
65
+ }