5htp-core 0.4.8 → 0.4.9

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 (187) hide show
  1. package/package.json +5 -1
  2. package/src/client/assets/css/components/table.less +2 -0
  3. package/src/client/components/Form.ts +1 -1
  4. package/src/client/components/button.tsx +2 -1
  5. package/src/client/components/containers/Popover/index.tsx +2 -2
  6. package/src/client/components/dropdown/index.tsx +16 -6
  7. package/src/client/components/input/Slider/index.tsx +0 -2
  8. package/src/client/components/inputv3/Rte/Editor.tsx +271 -0
  9. package/src/client/components/inputv3/Rte/ToolbarPlugin/BlockFormat.tsx +220 -0
  10. package/src/client/components/inputv3/Rte/ToolbarPlugin/ElementFormat.tsx +107 -0
  11. package/src/client/components/inputv3/Rte/ToolbarPlugin/index.tsx +768 -0
  12. package/src/client/components/inputv3/Rte/appSettings.ts +36 -0
  13. package/src/client/components/inputv3/Rte/context/FlashMessageContext.tsx +68 -0
  14. package/src/client/components/inputv3/Rte/context/SettingsContext.tsx +71 -0
  15. package/src/client/components/inputv3/Rte/context/SharedAutocompleteContext.tsx +71 -0
  16. package/src/client/components/inputv3/Rte/context/SharedHistoryContext.tsx +35 -0
  17. package/src/client/components/inputv3/Rte/currentEditor.ts +42 -0
  18. package/src/client/components/inputv3/Rte/hooks/useFlashMessage.tsx +16 -0
  19. package/src/client/components/inputv3/Rte/hooks/useReport.ts +67 -0
  20. package/src/client/components/inputv3/Rte/images/emoji/1F600.png +0 -0
  21. package/src/client/components/inputv3/Rte/images/emoji/1F641.png +0 -0
  22. package/src/client/components/inputv3/Rte/images/emoji/1F642.png +0 -0
  23. package/src/client/components/inputv3/Rte/images/emoji/2764.png +0 -0
  24. package/src/client/components/inputv3/Rte/images/emoji/LICENSE.md +5 -0
  25. package/src/client/components/inputv3/Rte/images/icons/draggable-block-menu.svg +1 -0
  26. package/src/client/components/inputv3/Rte/images/icons/prettier-error.svg +1 -0
  27. package/src/client/components/inputv3/Rte/images/icons/prettier.svg +1 -0
  28. package/src/client/components/inputv3/Rte/images/image/LICENSE.md +5 -0
  29. package/src/client/components/inputv3/Rte/images/image-broken.svg +4 -0
  30. package/src/client/components/inputv3/Rte/images/logo.svg +1 -0
  31. package/src/client/components/inputv3/Rte/index.tsx +63 -79
  32. package/src/client/components/inputv3/Rte/nodes/AutocompleteNode.tsx +119 -0
  33. package/src/client/components/inputv3/Rte/nodes/EmojiNode.tsx +102 -0
  34. package/src/client/components/inputv3/Rte/nodes/EquationComponent.tsx +141 -0
  35. package/src/client/components/inputv3/Rte/nodes/EquationNode.tsx +174 -0
  36. package/src/client/components/inputv3/Rte/nodes/FigmaNode.tsx +135 -0
  37. package/src/client/components/inputv3/Rte/nodes/ImageComponent.tsx +468 -0
  38. package/src/client/components/inputv3/Rte/nodes/ImageNode.css +43 -0
  39. package/src/client/components/inputv3/Rte/nodes/ImageNode.tsx +266 -0
  40. package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageComponent.tsx +402 -0
  41. package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageNode.css +94 -0
  42. package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageNode.tsx +294 -0
  43. package/src/client/components/inputv3/Rte/nodes/KeywordNode.ts +67 -0
  44. package/src/client/components/inputv3/Rte/nodes/LayoutContainerNode.ts +137 -0
  45. package/src/client/components/inputv3/Rte/nodes/LayoutItemNode.ts +71 -0
  46. package/src/client/components/inputv3/Rte/nodes/MentionNode.ts +130 -0
  47. package/src/client/components/inputv3/Rte/nodes/PageBreakNode/index.css +62 -0
  48. package/src/client/components/inputv3/Rte/nodes/PageBreakNode/index.tsx +170 -0
  49. package/src/client/components/inputv3/Rte/nodes/PlaygroundNodes.ts +76 -0
  50. package/src/client/components/inputv3/Rte/nodes/PollComponent.tsx +249 -0
  51. package/src/client/components/inputv3/Rte/nodes/PollNode.css +187 -0
  52. package/src/client/components/inputv3/Rte/nodes/PollNode.tsx +209 -0
  53. package/src/client/components/inputv3/Rte/nodes/StickyComponent.tsx +261 -0
  54. package/src/client/components/inputv3/Rte/nodes/StickyNode.css +37 -0
  55. package/src/client/components/inputv3/Rte/nodes/StickyNode.tsx +150 -0
  56. package/src/client/components/inputv3/Rte/nodes/TweetNode.tsx +223 -0
  57. package/src/client/components/inputv3/Rte/nodes/YouTubeNode.tsx +184 -0
  58. package/src/client/components/inputv3/Rte/plugins/ActionsPlugin/index.tsx +334 -0
  59. package/src/client/components/inputv3/Rte/plugins/AutoEmbedPlugin/index.tsx +352 -0
  60. package/src/client/components/inputv3/Rte/plugins/AutoLinkPlugin/index.tsx +32 -0
  61. package/src/client/components/inputv3/Rte/plugins/AutocompletePlugin/index.tsx +2529 -0
  62. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/CopyButton/index.tsx +70 -0
  63. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/PrettierButton/index.css +14 -0
  64. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/PrettierButton/index.tsx +156 -0
  65. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/index.css +54 -0
  66. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/index.tsx +190 -0
  67. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/utils.ts +33 -0
  68. package/src/client/components/inputv3/Rte/plugins/CodeHighlightPlugin/index.ts +21 -0
  69. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/Collapsible.css +57 -0
  70. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleContainerNode.ts +168 -0
  71. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleContentNode.ts +127 -0
  72. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleTitleNode.ts +152 -0
  73. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleUtils.ts +17 -0
  74. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/index.ts +284 -0
  75. package/src/client/components/inputv3/Rte/plugins/ComponentPickerPlugin/index.tsx +370 -0
  76. package/src/client/components/inputv3/Rte/plugins/ContextMenuPlugin/index.tsx +270 -0
  77. package/src/client/components/inputv3/Rte/plugins/DocsPlugin/index.tsx +20 -0
  78. package/src/client/components/inputv3/Rte/plugins/DragDropPastePlugin/index.ts +51 -0
  79. package/src/client/components/inputv3/Rte/plugins/DraggableBlockPlugin/index.css +36 -0
  80. package/src/client/components/inputv3/Rte/plugins/DraggableBlockPlugin/index.tsx +43 -0
  81. package/src/client/components/inputv3/Rte/plugins/EmojiPickerPlugin/index.tsx +198 -0
  82. package/src/client/components/inputv3/Rte/plugins/EmojisPlugin/index.ts +75 -0
  83. package/src/client/components/inputv3/Rte/plugins/EquationsPlugin/index.tsx +82 -0
  84. package/src/client/components/inputv3/Rte/plugins/FigmaPlugin/index.tsx +40 -0
  85. package/src/client/components/inputv3/Rte/plugins/FloatingLinkEditorPlugin/index.css +41 -0
  86. package/src/client/components/inputv3/Rte/plugins/FloatingLinkEditorPlugin/index.tsx +393 -0
  87. package/src/client/components/inputv3/Rte/plugins/FloatingTextFormatToolbarPlugin/index.css +141 -0
  88. package/src/client/components/inputv3/Rte/plugins/FloatingTextFormatToolbarPlugin/index.tsx +388 -0
  89. package/src/client/components/inputv3/Rte/plugins/ImagesPlugin/index.tsx +350 -0
  90. package/src/client/components/inputv3/Rte/plugins/InlineImagePlugin/index.tsx +336 -0
  91. package/src/client/components/inputv3/Rte/plugins/KeywordsPlugin/index.ts +56 -0
  92. package/src/client/components/inputv3/Rte/plugins/LayoutPlugin/InsertLayoutDialog.tsx +58 -0
  93. package/src/client/components/inputv3/Rte/plugins/LayoutPlugin/LayoutPlugin.tsx +219 -0
  94. package/src/client/components/inputv3/Rte/plugins/LinkPlugin/index.tsx +34 -0
  95. package/src/client/components/inputv3/Rte/plugins/ListMaxIndentLevelPlugin/index.ts +85 -0
  96. package/src/client/components/inputv3/Rte/plugins/MarkdownShortcutPlugin/index.tsx +16 -0
  97. package/src/client/components/inputv3/Rte/plugins/MarkdownTransformers/index.ts +324 -0
  98. package/src/client/components/inputv3/Rte/plugins/MaxLengthPlugin/index.tsx +53 -0
  99. package/src/client/components/inputv3/Rte/plugins/MentionsPlugin/index.tsx +696 -0
  100. package/src/client/components/inputv3/Rte/plugins/PageBreakPlugin/index.tsx +57 -0
  101. package/src/client/components/inputv3/Rte/plugins/PasteLogPlugin/index.tsx +54 -0
  102. package/src/client/components/inputv3/Rte/plugins/PollPlugin/index.tsx +86 -0
  103. package/src/client/components/inputv3/Rte/plugins/SpeechToTextPlugin/index.ts +125 -0
  104. package/src/client/components/inputv3/Rte/plugins/StickyPlugin/index.ts +22 -0
  105. package/src/client/components/inputv3/Rte/plugins/TabFocusPlugin/index.tsx +65 -0
  106. package/src/client/components/inputv3/Rte/plugins/TableActionMenuPlugin/index.tsx +773 -0
  107. package/src/client/components/inputv3/Rte/plugins/TableCellResizer/index.css +12 -0
  108. package/src/client/components/inputv3/Rte/plugins/TableCellResizer/index.tsx +436 -0
  109. package/src/client/components/inputv3/Rte/plugins/TableHoverActionsPlugin/index.tsx +287 -0
  110. package/src/client/components/inputv3/Rte/plugins/TableOfContentsPlugin/index.css +95 -0
  111. package/src/client/components/inputv3/Rte/plugins/TableOfContentsPlugin/index.tsx +197 -0
  112. package/src/client/components/inputv3/Rte/plugins/TablePlugin.tsx +178 -0
  113. package/src/client/components/inputv3/Rte/plugins/TestRecorderPlugin/index.tsx +468 -0
  114. package/src/client/components/inputv3/Rte/plugins/TreeViewPlugin/index.tsx +26 -0
  115. package/src/client/components/inputv3/Rte/plugins/TwitterPlugin/index.ts +41 -0
  116. package/src/client/components/inputv3/Rte/plugins/TypingPerfPlugin/index.ts +117 -0
  117. package/src/client/components/inputv3/Rte/plugins/YouTubePlugin/index.ts +41 -0
  118. package/src/client/components/inputv3/Rte/shared/canUseDOM.ts +4 -0
  119. package/src/client/components/inputv3/Rte/shared/caretFromPoint.ts +40 -0
  120. package/src/client/components/inputv3/Rte/shared/environment.ts +56 -0
  121. package/src/client/components/inputv3/Rte/shared/invariant.ts +26 -0
  122. package/src/client/components/inputv3/Rte/shared/normalizeClassNames.ts +21 -0
  123. package/src/client/components/inputv3/Rte/shared/react-test-utils.ts +18 -0
  124. package/src/client/components/inputv3/Rte/shared/reactPatches.ts +22 -0
  125. package/src/client/components/inputv3/Rte/shared/simpleDiffWithCursor.ts +49 -0
  126. package/src/client/components/inputv3/Rte/shared/useLayoutEffect.ts +19 -0
  127. package/src/client/components/inputv3/Rte/shared/warnOnlyOnce.ts +20 -0
  128. package/src/client/components/inputv3/Rte/style.less +30 -60
  129. package/src/client/components/inputv3/Rte/themes/CommentEditorTheme.css +13 -0
  130. package/src/client/components/inputv3/Rte/themes/CommentEditorTheme.ts +20 -0
  131. package/src/client/components/inputv3/Rte/themes/PlaygroundEditorTheme.css +447 -0
  132. package/src/client/components/inputv3/Rte/themes/PlaygroundEditorTheme.ts +120 -0
  133. package/src/client/components/inputv3/Rte/themes/StickyEditorTheme.css +13 -0
  134. package/src/client/components/inputv3/Rte/themes/StickyEditorTheme.ts +20 -0
  135. package/src/client/components/inputv3/Rte/ui/ColorPicker.css +88 -0
  136. package/src/client/components/inputv3/Rte/ui/ColorPicker.tsx +365 -0
  137. package/src/client/components/inputv3/Rte/ui/ContentEditable.css +44 -0
  138. package/src/client/components/inputv3/Rte/ui/ContentEditable.tsx +36 -0
  139. package/src/client/components/inputv3/Rte/ui/DropDown.tsx +259 -0
  140. package/src/client/components/inputv3/Rte/ui/DropdownColorPicker.tsx +41 -0
  141. package/src/client/components/inputv3/Rte/ui/EquationEditor.css +38 -0
  142. package/src/client/components/inputv3/Rte/ui/EquationEditor.tsx +56 -0
  143. package/src/client/components/inputv3/Rte/ui/FileInput.tsx +38 -0
  144. package/src/client/components/inputv3/Rte/ui/FlashMessage.css +28 -0
  145. package/src/client/components/inputv3/Rte/ui/FlashMessage.tsx +29 -0
  146. package/src/client/components/inputv3/Rte/ui/ImageResizer.tsx +316 -0
  147. package/src/client/components/inputv3/Rte/ui/Input.css +32 -0
  148. package/src/client/components/inputv3/Rte/ui/KatexRenderer.tsx +54 -0
  149. package/src/client/components/inputv3/Rte/ui/Switch.tsx +36 -0
  150. package/src/client/components/inputv3/Rte/utils/docSerialization.ts +77 -0
  151. package/src/client/components/inputv3/Rte/utils/emoji-list.ts +16615 -0
  152. package/src/client/components/inputv3/Rte/utils/getDOMRangeRect.ts +27 -0
  153. package/src/client/components/inputv3/Rte/utils/getSelectedNode.ts +27 -0
  154. package/src/client/components/inputv3/Rte/utils/guard.ts +10 -0
  155. package/src/client/components/inputv3/Rte/utils/isMobileWidth.ts +7 -0
  156. package/src/client/components/inputv3/Rte/utils/joinClasses.ts +13 -0
  157. package/src/client/components/inputv3/Rte/utils/setFloatingElemPosition.ts +51 -0
  158. package/src/client/components/inputv3/Rte/utils/setFloatingElemPositionForLinkEditor.ts +46 -0
  159. package/src/client/components/inputv3/Rte/utils/swipe.ts +127 -0
  160. package/src/client/components/inputv3/Rte/utils/url.ts +38 -0
  161. package/src/client/components/inputv3/base.tsx +8 -5
  162. package/src/client/components/inputv3/file/index.tsx +11 -5
  163. package/src/common/data/rte/nodes.ts +60 -9
  164. package/src/common/validation/index.ts +21 -2
  165. package/src/common/validation/schema.ts +42 -10
  166. package/src/common/validation/validator.ts +12 -4
  167. package/src/common/validation/validators.ts +82 -53
  168. package/src/server/services/router/http/multipart.ts +0 -1
  169. package/src/server/services/schema/index.ts +24 -2
  170. package/src/server/services/schema/request.ts +3 -2
  171. package/src/server/services/schema/rte.ts +110 -0
  172. package/src/{common/data/rte/index.ts → server/utils/rte.ts} +27 -16
  173. package/src/client/components/inputv3/Rte/ExampleTheme.tsx +0 -42
  174. package/src/client/components/inputv3/Rte/ToolbarPlugin.tsx +0 -167
  175. package/src/client/components/inputv3/Rte/icons/LICENSE.md +0 -5
  176. package/src/client/components/inputv3/Rte/icons/arrow-clockwise.svg +0 -4
  177. package/src/client/components/inputv3/Rte/icons/arrow-counterclockwise.svg +0 -4
  178. package/src/client/components/inputv3/Rte/icons/journal-text.svg +0 -5
  179. package/src/client/components/inputv3/Rte/icons/justify.svg +0 -3
  180. package/src/client/components/inputv3/Rte/icons/text-center.svg +0 -3
  181. package/src/client/components/inputv3/Rte/icons/text-left.svg +0 -3
  182. package/src/client/components/inputv3/Rte/icons/text-paragraph.svg +0 -3
  183. package/src/client/components/inputv3/Rte/icons/text-right.svg +0 -3
  184. package/src/client/components/inputv3/Rte/icons/type-bold.svg +0 -3
  185. package/src/client/components/inputv3/Rte/icons/type-italic.svg +0 -3
  186. package/src/client/components/inputv3/Rte/icons/type-strikethrough.svg +0 -3
  187. package/src/client/components/inputv3/Rte/icons/type-underline.svg +0 -3
@@ -0,0 +1,130 @@
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
+ $applyNodeReplacement,
11
+ type DOMConversionMap,
12
+ type DOMConversionOutput,
13
+ type DOMExportOutput,
14
+ type EditorConfig,
15
+ type LexicalNode,
16
+ type NodeKey,
17
+ type SerializedTextNode,
18
+ type Spread,
19
+ TextNode,
20
+ } from 'lexical';
21
+
22
+ export type SerializedMentionNode = Spread<
23
+ {
24
+ mentionName: string;
25
+ },
26
+ SerializedTextNode
27
+ >;
28
+
29
+ function $convertMentionElement(
30
+ domNode: HTMLElement,
31
+ ): DOMConversionOutput | null {
32
+ const textContent = domNode.textContent;
33
+
34
+ if (textContent !== null) {
35
+ const node = $createMentionNode(textContent);
36
+ return {
37
+ node,
38
+ };
39
+ }
40
+
41
+ return null;
42
+ }
43
+
44
+ const mentionStyle = 'background-color: rgba(24, 119, 232, 0.2)';
45
+ export class MentionNode extends TextNode {
46
+ __mention: string;
47
+
48
+ static getType(): string {
49
+ return 'mention';
50
+ }
51
+
52
+ static clone(node: MentionNode): MentionNode {
53
+ return new MentionNode(node.__mention, node.__text, node.__key);
54
+ }
55
+ static importJSON(serializedNode: SerializedMentionNode): MentionNode {
56
+ const node = $createMentionNode(serializedNode.mentionName);
57
+ node.setTextContent(serializedNode.text);
58
+ node.setFormat(serializedNode.format);
59
+ node.setDetail(serializedNode.detail);
60
+ node.setMode(serializedNode.mode);
61
+ node.setStyle(serializedNode.style);
62
+ return node;
63
+ }
64
+
65
+ constructor(mentionName: string, text?: string, key?: NodeKey) {
66
+ super(text ?? mentionName, key);
67
+ this.__mention = mentionName;
68
+ }
69
+
70
+ exportJSON(): SerializedMentionNode {
71
+ return {
72
+ ...super.exportJSON(),
73
+ mentionName: this.__mention,
74
+ type: 'mention',
75
+ version: 1,
76
+ };
77
+ }
78
+
79
+ createDOM(config: EditorConfig): HTMLElement {
80
+ const dom = super.createDOM(config);
81
+ dom.style.cssText = mentionStyle;
82
+ dom.className = 'mention';
83
+ return dom;
84
+ }
85
+
86
+ exportDOM(): DOMExportOutput {
87
+ const element = document.createElement('span');
88
+ element.setAttribute('data-lexical-mention', 'true');
89
+ element.textContent = this.__text;
90
+ return {element};
91
+ }
92
+
93
+ static importDOM(): DOMConversionMap | null {
94
+ return {
95
+ span: (domNode: HTMLElement) => {
96
+ if (!domNode.hasAttribute('data-lexical-mention')) {
97
+ return null;
98
+ }
99
+ return {
100
+ conversion: $convertMentionElement,
101
+ priority: 1,
102
+ };
103
+ },
104
+ };
105
+ }
106
+
107
+ isTextEntity(): true {
108
+ return true;
109
+ }
110
+
111
+ canInsertTextBefore(): boolean {
112
+ return false;
113
+ }
114
+
115
+ canInsertTextAfter(): boolean {
116
+ return false;
117
+ }
118
+ }
119
+
120
+ export function $createMentionNode(mentionName: string): MentionNode {
121
+ const mentionNode = new MentionNode(mentionName);
122
+ mentionNode.setMode('segmented').toggleDirectionless();
123
+ return $applyNodeReplacement(mentionNode);
124
+ }
125
+
126
+ export function $isMentionNode(
127
+ node: LexicalNode | null | undefined,
128
+ ): node is MentionNode {
129
+ return node instanceof MentionNode;
130
+ }
@@ -0,0 +1,62 @@
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 url('assets/styles/variables.css'); */
10
+
11
+ [type='page-break'] {
12
+ position: relative;
13
+ display: block;
14
+ width: calc(100% + var(--editor-input-padding, 28px) * 2);
15
+ overflow: unset;
16
+ margin-left: calc(var(--editor-input-padding, 28px) * -1);
17
+ margin-top: var(--editor-input-padding, 28px);
18
+ margin-bottom: var(--editor-input-padding, 28px);
19
+
20
+ border: none;
21
+ border-top: 1px dashed var(--editor-color-secondary, #eeeeee);
22
+ border-bottom: 1px dashed var(--editor-color-secondary, #eeeeee);
23
+ background-color: var(--editor-color-secondary, #eeeeee);
24
+ }
25
+
26
+ [type='page-break']::before {
27
+ content: '';
28
+
29
+ position: absolute;
30
+ top: 50%;
31
+ left: calc(var(--editor-input-padding, 28px) + 12px);
32
+ transform: translateY(-50%);
33
+ opacity: 0.5;
34
+
35
+ width: 16px;
36
+ height: 16px;
37
+ }
38
+
39
+ [type='page-break']::after {
40
+ position: absolute;
41
+ top: 50%;
42
+ left: 50%;
43
+ transform: translate(-50%, -50%);
44
+
45
+ display: block;
46
+ padding: 2px 6px;
47
+ border: 1px solid #ccc;
48
+ background-color: #fff;
49
+
50
+ content: 'PAGE BREAK';
51
+ font-size: 12px;
52
+ color: #000;
53
+ font-weight: 600;
54
+ }
55
+
56
+ .selected[type='page-break'] {
57
+ border-color: var(--editor-color-primary, #4766cb);
58
+ }
59
+
60
+ .selected[type='page-break']::before {
61
+ opacity: 1;
62
+ }
@@ -0,0 +1,170 @@
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
+ import './index.css';
9
+
10
+ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
11
+ import {useLexicalNodeSelection} from '@lexical/react/useLexicalNodeSelection';
12
+ import {mergeRegister} from '@lexical/utils';
13
+ import {
14
+ $getSelection,
15
+ $isNodeSelection,
16
+ CLICK_COMMAND,
17
+ COMMAND_PRIORITY_HIGH,
18
+ COMMAND_PRIORITY_LOW,
19
+ DecoratorNode,
20
+ DOMConversionMap,
21
+ DOMConversionOutput,
22
+ KEY_BACKSPACE_COMMAND,
23
+ KEY_DELETE_COMMAND,
24
+ LexicalNode,
25
+ NodeKey,
26
+ SerializedLexicalNode,
27
+ } from 'lexical';
28
+ import * as React from 'react';
29
+ import {useCallback, useEffect} from 'react';
30
+
31
+ export type SerializedPageBreakNode = SerializedLexicalNode;
32
+
33
+ function PageBreakComponent({nodeKey}: {nodeKey: NodeKey}) {
34
+ const [editor] = useLexicalComposerContext();
35
+ const [isSelected, setSelected, clearSelection] =
36
+ useLexicalNodeSelection(nodeKey);
37
+
38
+ const $onDelete = useCallback(
39
+ (event: KeyboardEvent) => {
40
+ event.preventDefault();
41
+ const deleteSelection = $getSelection();
42
+ if (isSelected && $isNodeSelection(deleteSelection)) {
43
+ editor.update(() => {
44
+ deleteSelection.getNodes().forEach((node) => {
45
+ if ($isPageBreakNode(node)) {
46
+ node.remove();
47
+ }
48
+ });
49
+ });
50
+ }
51
+ return false;
52
+ },
53
+ [editor, isSelected],
54
+ );
55
+
56
+ useEffect(() => {
57
+ return mergeRegister(
58
+ editor.registerCommand(
59
+ CLICK_COMMAND,
60
+ (event: MouseEvent) => {
61
+ const pbElem = editor.getElementByKey(nodeKey);
62
+
63
+ if (event.target === pbElem) {
64
+ if (!event.shiftKey) {
65
+ clearSelection();
66
+ }
67
+ setSelected(!isSelected);
68
+ return true;
69
+ }
70
+
71
+ return false;
72
+ },
73
+ COMMAND_PRIORITY_LOW,
74
+ ),
75
+ editor.registerCommand(
76
+ KEY_DELETE_COMMAND,
77
+ $onDelete,
78
+ COMMAND_PRIORITY_LOW,
79
+ ),
80
+ editor.registerCommand(
81
+ KEY_BACKSPACE_COMMAND,
82
+ $onDelete,
83
+ COMMAND_PRIORITY_LOW,
84
+ ),
85
+ );
86
+ }, [clearSelection, editor, isSelected, nodeKey, $onDelete, setSelected]);
87
+
88
+ useEffect(() => {
89
+ const pbElem = editor.getElementByKey(nodeKey);
90
+ if (pbElem !== null) {
91
+ pbElem.className = isSelected ? 'selected' : '';
92
+ }
93
+ }, [editor, isSelected, nodeKey]);
94
+
95
+ return null;
96
+ }
97
+
98
+ export class PageBreakNode extends DecoratorNode<JSX.Element> {
99
+ static getType(): string {
100
+ return 'page-break';
101
+ }
102
+
103
+ static clone(node: PageBreakNode): PageBreakNode {
104
+ return new PageBreakNode(node.__key);
105
+ }
106
+
107
+ static importJSON(serializedNode: SerializedPageBreakNode): PageBreakNode {
108
+ return $createPageBreakNode();
109
+ }
110
+
111
+ static importDOM(): DOMConversionMap | null {
112
+ return {
113
+ figure: (domNode: HTMLElement) => {
114
+ const tp = domNode.getAttribute('type');
115
+ if (tp !== this.getType()) {
116
+ return null;
117
+ }
118
+
119
+ return {
120
+ conversion: $convertPageBreakElement,
121
+ priority: COMMAND_PRIORITY_HIGH,
122
+ };
123
+ },
124
+ };
125
+ }
126
+
127
+ exportJSON(): SerializedLexicalNode {
128
+ return {
129
+ type: this.getType(),
130
+ version: 1,
131
+ };
132
+ }
133
+
134
+ createDOM(): HTMLElement {
135
+ const el = document.createElement('figure');
136
+ el.style.pageBreakAfter = 'always';
137
+ el.setAttribute('type', this.getType());
138
+ return el;
139
+ }
140
+
141
+ getTextContent(): string {
142
+ return '\n';
143
+ }
144
+
145
+ isInline(): false {
146
+ return false;
147
+ }
148
+
149
+ updateDOM(): boolean {
150
+ return false;
151
+ }
152
+
153
+ decorate(): JSX.Element {
154
+ return <PageBreakComponent nodeKey={this.__key} />;
155
+ }
156
+ }
157
+
158
+ function $convertPageBreakElement(): DOMConversionOutput {
159
+ return {node: $createPageBreakNode()};
160
+ }
161
+
162
+ export function $createPageBreakNode(): PageBreakNode {
163
+ return new PageBreakNode();
164
+ }
165
+
166
+ export function $isPageBreakNode(
167
+ node: LexicalNode | null | undefined,
168
+ ): node is PageBreakNode {
169
+ return node instanceof PageBreakNode;
170
+ }
@@ -0,0 +1,76 @@
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 {Klass, LexicalNode} from 'lexical';
10
+
11
+ import {CodeHighlightNode, CodeNode} from '@lexical/code';
12
+ import {HashtagNode} from '@lexical/hashtag';
13
+ import {AutoLinkNode, LinkNode} from '@lexical/link';
14
+ import {ListItemNode, ListNode} from '@lexical/list';
15
+ import {MarkNode} from '@lexical/mark';
16
+ import {OverflowNode} from '@lexical/overflow';
17
+ import {HorizontalRuleNode} from '@lexical/react/LexicalHorizontalRuleNode';
18
+ import {HeadingNode, QuoteNode} from '@lexical/rich-text';
19
+ import {TableCellNode, TableNode, TableRowNode} from '@lexical/table';
20
+
21
+ import {CollapsibleContainerNode} from '../plugins/CollapsiblePlugin/CollapsibleContainerNode';
22
+ import {CollapsibleContentNode} from '../plugins/CollapsiblePlugin/CollapsibleContentNode';
23
+ import {CollapsibleTitleNode} from '../plugins/CollapsiblePlugin/CollapsibleTitleNode';
24
+ import {AutocompleteNode} from './AutocompleteNode';
25
+ import {EmojiNode} from './EmojiNode';
26
+ import {EquationNode} from './EquationNode';
27
+ import {FigmaNode} from './FigmaNode';
28
+ import {ImageNode} from './ImageNode';
29
+ import {InlineImageNode} from './InlineImageNode/InlineImageNode';
30
+ import {KeywordNode} from './KeywordNode';
31
+ import {LayoutContainerNode} from './LayoutContainerNode';
32
+ import {LayoutItemNode} from './LayoutItemNode';
33
+ import {MentionNode} from './MentionNode';
34
+ import {PageBreakNode} from './PageBreakNode';
35
+ import {PollNode} from './PollNode';
36
+ import {StickyNode} from './StickyNode';
37
+ import {TweetNode} from './TweetNode';
38
+ import {YouTubeNode} from './YouTubeNode';
39
+
40
+ const PlaygroundNodes: Array<Klass<LexicalNode>> = [
41
+ HeadingNode,
42
+ ListNode,
43
+ ListItemNode,
44
+ QuoteNode,
45
+ CodeNode,
46
+ TableNode,
47
+ TableCellNode,
48
+ TableRowNode,
49
+ HashtagNode,
50
+ CodeHighlightNode,
51
+ AutoLinkNode,
52
+ LinkNode,
53
+ OverflowNode,
54
+ PollNode,
55
+ StickyNode,
56
+ ImageNode,
57
+ InlineImageNode,
58
+ MentionNode,
59
+ EmojiNode,
60
+ EquationNode,
61
+ AutocompleteNode,
62
+ KeywordNode,
63
+ HorizontalRuleNode,
64
+ TweetNode,
65
+ YouTubeNode,
66
+ FigmaNode,
67
+ MarkNode,
68
+ CollapsibleContainerNode,
69
+ CollapsibleContentNode,
70
+ CollapsibleTitleNode,
71
+ PageBreakNode,
72
+ LayoutContainerNode,
73
+ LayoutItemNode,
74
+ ];
75
+
76
+ export default PlaygroundNodes;
@@ -0,0 +1,249 @@
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
+ // Core
10
+ import Button from '@client/components/button';
11
+ import Input from '@client/components/inputv3';
12
+
13
+ import type { Option, Options, PollNode } from './PollNode';
14
+
15
+ import './PollNode.css';
16
+
17
+ import { useCollaborationContext } from '@lexical/react/LexicalCollaborationContext';
18
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
19
+ import { useLexicalNodeSelection } from '@lexical/react/useLexicalNodeSelection';
20
+ import { mergeRegister } from '@lexical/utils';
21
+ import {
22
+ $getNodeByKey,
23
+ $getSelection,
24
+ $isNodeSelection,
25
+ BaseSelection,
26
+ CLICK_COMMAND,
27
+ COMMAND_PRIORITY_LOW,
28
+ KEY_BACKSPACE_COMMAND,
29
+ KEY_DELETE_COMMAND,
30
+ NodeKey,
31
+ } from 'lexical';
32
+ import * as React from 'react';
33
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
34
+
35
+ import joinClasses from '../utils/joinClasses';
36
+ import { $isPollNode, createPollOption } from './PollNode';
37
+
38
+ function getTotalVotes(options: Options): number {
39
+ return options.reduce((totalVotes, next) => {
40
+ return totalVotes + next.votes.length;
41
+ }, 0);
42
+ }
43
+
44
+ function PollOptionComponent({
45
+ option,
46
+ index,
47
+ options,
48
+ totalVotes,
49
+ withPollNode,
50
+ }: {
51
+ index: number;
52
+ option: Option;
53
+ options: Options;
54
+ totalVotes: number;
55
+ withPollNode: (
56
+ cb: (pollNode: PollNode) => void,
57
+ onSelect?: () => void,
58
+ ) => void;
59
+ }): JSX.Element {
60
+
61
+ const { clientID } = useCollaborationContext();
62
+ const checkboxRef = useRef(null);
63
+ const votesArray = option.votes;
64
+ const checkedIndex = votesArray.indexOf(clientID);
65
+ const checked = checkedIndex !== -1;
66
+ const votes = votesArray.length;
67
+ const text = option.text;
68
+
69
+ return (
70
+ <div className="row sp-05" style={{ position: 'relative' }}>
71
+ <div
72
+ className={joinClasses(
73
+ 'PollNode__optionCheckboxWrapper',
74
+ checked && 'PollNode__optionCheckboxChecked',
75
+ )}>
76
+ <input
77
+ ref={checkboxRef}
78
+ className="PollNode__optionCheckbox"
79
+ type="checkbox"
80
+ onChange={(e) => {
81
+ withPollNode((node) => {
82
+ node.toggleVote(option, clientID);
83
+ });
84
+ }}
85
+ checked={checked}
86
+ />
87
+ </div>
88
+ <div
89
+ className="PollNode__optionInputVotes"
90
+ style={{ width: `${votes === 0 ? 0 : (votes / totalVotes) * 100}%` }}
91
+ />
92
+ <span className="PollNode__optionInputVotesCount">
93
+ {votes > 0 && (votes === 1 ? '1 vote' : `${votes} votes`)}
94
+ </span>
95
+ <Input
96
+ value={text}
97
+ onChange={(e) => {
98
+ const target = e.target;
99
+ const value = target.value;
100
+ const selectionStart = target.selectionStart;
101
+ const selectionEnd = target.selectionEnd;
102
+ withPollNode(
103
+ (node) => {
104
+ node.setOptionText(option, value);
105
+ },
106
+ () => {
107
+ target.selectionStart = selectionStart;
108
+ target.selectionEnd = selectionEnd;
109
+ },
110
+ );
111
+ }}
112
+ placeholder={`Option ${index + 1}`}
113
+ className="col-1"
114
+ />
115
+ <Button
116
+ disabled={options.length < 3}
117
+ icon="times"
118
+ onClick={() => {
119
+ withPollNode((node) => {
120
+ node.deleteOption(option);
121
+ });
122
+ }}
123
+ />
124
+ </div>
125
+ );
126
+ }
127
+
128
+ export default function PollComponent({
129
+ question,
130
+ options,
131
+ nodeKey,
132
+ }: {
133
+ nodeKey: NodeKey;
134
+ options: Options;
135
+ question: string;
136
+ }): JSX.Element {
137
+
138
+ const [editor] = useLexicalComposerContext();
139
+ const totalVotes = useMemo(() => getTotalVotes(options), [options]);
140
+ const [isSelected, setSelected, clearSelection] =
141
+ useLexicalNodeSelection(nodeKey);
142
+ const [selection, setSelection] = useState<BaseSelection | null>(null);
143
+ const ref = useRef(null);
144
+
145
+ const $onDelete = useCallback(
146
+ (payload: KeyboardEvent) => {
147
+ const deleteSelection = $getSelection();
148
+ if (isSelected && $isNodeSelection(deleteSelection)) {
149
+ const event: KeyboardEvent = payload;
150
+ event.preventDefault();
151
+ editor.update(() => {
152
+ deleteSelection.getNodes().forEach((node) => {
153
+ if ($isPollNode(node)) {
154
+ node.remove();
155
+ }
156
+ });
157
+ });
158
+ }
159
+ return false;
160
+ },
161
+ [editor, isSelected],
162
+ );
163
+
164
+ useEffect(() => {
165
+ return mergeRegister(
166
+ editor.registerUpdateListener(({ editorState }) => {
167
+ setSelection(editorState.read(() => $getSelection()));
168
+ }),
169
+ editor.registerCommand<MouseEvent>(
170
+ CLICK_COMMAND,
171
+ (payload) => {
172
+ const event = payload;
173
+
174
+ if (event.target === ref.current) {
175
+ if (!event.shiftKey) {
176
+ clearSelection();
177
+ }
178
+ setSelected(!isSelected);
179
+ return true;
180
+ }
181
+
182
+ return false;
183
+ },
184
+ COMMAND_PRIORITY_LOW,
185
+ ),
186
+ editor.registerCommand(
187
+ KEY_DELETE_COMMAND,
188
+ $onDelete,
189
+ COMMAND_PRIORITY_LOW,
190
+ ),
191
+ editor.registerCommand(
192
+ KEY_BACKSPACE_COMMAND,
193
+ $onDelete,
194
+ COMMAND_PRIORITY_LOW,
195
+ ),
196
+ );
197
+ }, [clearSelection, editor, isSelected, nodeKey, $onDelete, setSelected]);
198
+
199
+ const withPollNode = (
200
+ cb: (node: PollNode) => void,
201
+ onUpdate?: () => void,
202
+ ): void => {
203
+ editor.update(
204
+ () => {
205
+ const node = $getNodeByKey(nodeKey);
206
+ if ($isPollNode(node)) {
207
+ cb(node);
208
+ }
209
+ },
210
+ { onUpdate },
211
+ );
212
+ };
213
+
214
+ const addOption = () => {
215
+ withPollNode((node) => {
216
+ node.addOption(createPollOption());
217
+ });
218
+ };
219
+
220
+ const isFocused = $isNodeSelection(selection) && isSelected;
221
+
222
+ return (
223
+ <div
224
+ className={`PollNode__container ${isFocused ? 'focused' : ''}`}
225
+ ref={ref}>
226
+ <div className="PollNode__inner">
227
+ <h2 className="PollNode__heading">{question}</h2>
228
+ {options.map((option, index) => {
229
+ const key = option.uid;
230
+ return (
231
+ <PollOptionComponent
232
+ key={key}
233
+ withPollNode={withPollNode}
234
+ option={option}
235
+ index={index}
236
+ options={options}
237
+ totalVotes={totalVotes}
238
+ />
239
+ );
240
+ })}
241
+ <div className="PollNode__footer">
242
+ <Button icon="plus" onClick={addOption}>
243
+ Add Option
244
+ </Button>
245
+ </div>
246
+ </div>
247
+ </div>
248
+ );
249
+ }