@react-fabric/lexical 1.0.0-beta.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 (136) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/_virtual/prism.js +5 -0
  4. package/dist/cjs/editor/Lexical.module.css +1 -0
  5. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +432 -0
  6. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +21 -0
  7. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +27 -0
  8. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +1139 -0
  9. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +25 -0
  10. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +50 -0
  11. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +245 -0
  12. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +19 -0
  13. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +18 -0
  14. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +471 -0
  15. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +17 -0
  16. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +24 -0
  17. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +1275 -0
  18. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +29 -0
  19. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +29 -0
  20. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +789 -0
  21. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +29 -0
  22. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +26 -0
  23. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +976 -0
  24. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +35 -0
  25. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +35 -0
  26. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2792 -0
  27. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +23 -0
  28. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +55 -0
  29. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +888 -0
  30. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +35 -0
  31. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +66 -0
  32. package/dist/cjs/node_modules/@mdi/js/mdi.js +8 -0
  33. package/dist/cjs/node_modules/prismjs/components/prism-c.js +82 -0
  34. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +33 -0
  35. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +101 -0
  36. package/dist/cjs/node_modules/prismjs/components/prism-css.js +66 -0
  37. package/dist/cjs/node_modules/prismjs/components/prism-java.js +126 -0
  38. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +174 -0
  39. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +417 -0
  40. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +188 -0
  41. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +14 -0
  42. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +60 -0
  43. package/dist/cjs/node_modules/prismjs/components/prism-python.js +67 -0
  44. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +130 -0
  45. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +34 -0
  46. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +150 -0
  47. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +62 -0
  48. package/dist/cjs/node_modules/prismjs/prism.js +1951 -0
  49. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +221 -0
  50. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +156 -0
  51. package/dist/cjs/packages/lexical/src/editor/theme.js +97 -0
  52. package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +451 -0
  53. package/dist/cjs/packages/lexical/src/index.js +7 -0
  54. package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +218 -0
  55. package/dist/cjs/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  56. package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  57. package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  58. package/dist/cjs/packages/lexical/src/utils/index.js +214 -0
  59. package/dist/esm/_virtual/prism.js +5 -0
  60. package/dist/esm/editor/Lexical.module.css +1 -0
  61. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +423 -0
  62. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +19 -0
  63. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +18 -0
  64. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +1121 -0
  65. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +21 -0
  66. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +32 -0
  67. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +244 -0
  68. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +18 -0
  69. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +17 -0
  70. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +463 -0
  71. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +17 -0
  72. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +16 -0
  73. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +1262 -0
  74. package/dist/esm/node_modules/@lexical/list/LexicalList.js +23 -0
  75. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +16 -0
  76. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +781 -0
  77. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +23 -0
  78. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +18 -0
  79. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +957 -0
  80. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +26 -0
  81. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +16 -0
  82. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2754 -0
  83. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +20 -0
  84. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +17 -0
  85. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +839 -0
  86. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +26 -0
  87. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +17 -0
  88. package/dist/esm/node_modules/@mdi/js/mdi.js +7 -0
  89. package/dist/esm/node_modules/prismjs/components/prism-c.js +82 -0
  90. package/dist/esm/node_modules/prismjs/components/prism-clike.js +33 -0
  91. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +101 -0
  92. package/dist/esm/node_modules/prismjs/components/prism-css.js +66 -0
  93. package/dist/esm/node_modules/prismjs/components/prism-java.js +126 -0
  94. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +174 -0
  95. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +417 -0
  96. package/dist/esm/node_modules/prismjs/components/prism-markup.js +188 -0
  97. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +14 -0
  98. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +60 -0
  99. package/dist/esm/node_modules/prismjs/components/prism-python.js +67 -0
  100. package/dist/esm/node_modules/prismjs/components/prism-rust.js +130 -0
  101. package/dist/esm/node_modules/prismjs/components/prism-sql.js +34 -0
  102. package/dist/esm/node_modules/prismjs/components/prism-swift.js +150 -0
  103. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +62 -0
  104. package/dist/esm/node_modules/prismjs/prism.js +1951 -0
  105. package/dist/esm/packages/lexical/src/components/StickyComponent.js +221 -0
  106. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +156 -0
  107. package/dist/esm/packages/lexical/src/editor/theme.js +97 -0
  108. package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +450 -0
  109. package/dist/esm/packages/lexical/src/index.js +3 -0
  110. package/dist/esm/packages/lexical/src/nodes/StickyNode.js +216 -0
  111. package/dist/esm/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  112. package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  113. package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  114. package/dist/esm/packages/lexical/src/utils/index.js +211 -0
  115. package/dist/types/components/StickyComponent.d.ts +9 -0
  116. package/dist/types/editor/LexicalEditor.d.ts +13 -0
  117. package/dist/types/editor/theme.d.ts +70 -0
  118. package/dist/types/hooks/useFormatWatcher.d.ts +40 -0
  119. package/dist/types/index.d.ts +1 -0
  120. package/dist/types/nodes/StickyNode.d.ts +35 -0
  121. package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -0
  122. package/dist/types/plugins/StickyPlugin.d.ts +1 -0
  123. package/dist/types/plugins/ToolbarPlugin.d.ts +1 -0
  124. package/dist/types/utils/index.d.ts +57 -0
  125. package/package.json +55 -0
  126. package/src/components/StickyComponent.tsx +271 -0
  127. package/src/editor/Lexical.module.css +218 -0
  128. package/src/editor/LexicalEditor.tsx +222 -0
  129. package/src/editor/theme.ts +93 -0
  130. package/src/hooks/useFormatWatcher.ts +504 -0
  131. package/src/index.ts +24 -0
  132. package/src/nodes/StickyNode.tsx +249 -0
  133. package/src/plugins/DraggableBlockPlugin.tsx +418 -0
  134. package/src/plugins/StickyPlugin.ts +38 -0
  135. package/src/plugins/ToolbarPlugin.tsx +279 -0
  136. package/src/utils/index.ts +303 -0
@@ -0,0 +1,249 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ // REF: https://github.com/facebook/lexical/blob/main/packages/lexical-playground/src/nodes/StickyNode.tsx
25
+
26
+ import {
27
+ $applyNodeReplacement,
28
+ DecoratorNode,
29
+ type DOMConversionMap,
30
+ type DOMConversionOutput,
31
+ type DOMExportOutput,
32
+ type EditorConfig,
33
+ type LexicalEditor,
34
+ type LexicalNode,
35
+ type NodeKey,
36
+ type SerializedLexicalNode,
37
+ type Spread,
38
+ } from "lexical";
39
+ import { lazy, Suspense } from "react";
40
+ import { createPortal } from "react-dom";
41
+
42
+ const StickyComponent = lazy(
43
+ async () =>
44
+ await import("../components/StickyComponent").then(
45
+ ({ StickyComponent }) => ({
46
+ default: StickyComponent,
47
+ }),
48
+ ),
49
+ );
50
+
51
+ type StickyNoteColor = "pink" | "yellow" | "green" | "blue";
52
+
53
+ interface StickyNodeType {
54
+ xOffset: number;
55
+ yOffset: number;
56
+ user?: string;
57
+ color?: StickyNoteColor;
58
+ state?: AnyObject;
59
+ }
60
+ export type SerializedStickyNode = Spread<
61
+ StickyNodeType,
62
+ SerializedLexicalNode
63
+ >;
64
+
65
+ const convertStickyElement = (
66
+ domNode: HTMLElement,
67
+ ): null | DOMConversionOutput => {
68
+ const sticky = domNode.getAttribute("data-lexical-sticky") === "true";
69
+ if (sticky) {
70
+ const node = $createStickyNode({
71
+ xOffset: +(domNode.getAttribute("data-x") ?? "8"),
72
+ yOffset: +(domNode.getAttribute("data-y") ?? "8"),
73
+ user: domNode.getAttribute("data-user") ?? "",
74
+ color: domNode.getAttribute("data-color") as AnyObject,
75
+ state: domNode.getAttribute("data-state") ?? "",
76
+ });
77
+ return { node };
78
+ }
79
+
80
+ return null;
81
+ };
82
+
83
+ const colors: AnyObject = {
84
+ yellow: "green",
85
+ green: "blue",
86
+ blue: "pink",
87
+ pink: "yellow",
88
+ };
89
+
90
+ export class StickyNode extends DecoratorNode<JSX.Element> {
91
+ __x: number;
92
+ __y: number;
93
+ __user: string;
94
+ __state?: AnyObject;
95
+ __color: StickyNoteColor;
96
+
97
+ static getType(): string {
98
+ return "sticky";
99
+ }
100
+
101
+ static clone(node: StickyNode): StickyNode {
102
+ return new StickyNode(
103
+ node.__x,
104
+ node.__y,
105
+ node.__user,
106
+ node.__color,
107
+ node.__state,
108
+ node.__key,
109
+ );
110
+ }
111
+
112
+ static importJSON(serializedNode: SerializedStickyNode): StickyNode {
113
+ const stickyNode = new StickyNode(
114
+ serializedNode.xOffset,
115
+ serializedNode.yOffset,
116
+ serializedNode.user,
117
+ serializedNode.color,
118
+ serializedNode.state,
119
+ );
120
+
121
+ return stickyNode;
122
+ }
123
+
124
+ static importDOM(): DOMConversionMap | null {
125
+ return {
126
+ div: (domNode: HTMLElement) => {
127
+ if (!domNode.hasAttribute("data-lexical-sticky")) {
128
+ return null;
129
+ }
130
+ return {
131
+ conversion: convertStickyElement,
132
+ priority: 2,
133
+ };
134
+ },
135
+ span: (domNode: HTMLElement) => {
136
+ if (!domNode.hasAttribute("data-lexical-sticky")) {
137
+ return null;
138
+ }
139
+ return {
140
+ conversion: convertStickyElement,
141
+ priority: 1,
142
+ };
143
+ },
144
+ };
145
+ }
146
+
147
+ constructor(
148
+ x: number,
149
+ y: number,
150
+ user = "",
151
+ color: StickyNoteColor = "yellow",
152
+ state?: KeyValue,
153
+ key?: NodeKey,
154
+ ) {
155
+ super(key);
156
+ this.__x = x;
157
+ this.__y = y;
158
+ this.__user = user;
159
+ this.__state = state;
160
+ this.__color = color;
161
+ }
162
+
163
+ exportJSON(): SerializedStickyNode {
164
+ return {
165
+ state: this.__state,
166
+ color: this.__color,
167
+ user: this.__user,
168
+ type: "sticky",
169
+ version: 1,
170
+ xOffset: this.__x,
171
+ yOffset: this.__y,
172
+ };
173
+ }
174
+
175
+ createDOM(config: EditorConfig): HTMLElement {
176
+ const div = document.createElement("div");
177
+ div.style.display = "contents";
178
+ return div;
179
+ }
180
+
181
+ updateDOM(): false {
182
+ return false;
183
+ }
184
+
185
+ exportDOM(): DOMExportOutput {
186
+ const writable = this.getWritable();
187
+ const element = document.createElement("div");
188
+ element.setAttribute("data-lexical-sticky", "true");
189
+ element.setAttribute("data-x", `${writable.__x}`);
190
+ element.setAttribute("data-y", `${writable.__y}`);
191
+ element.setAttribute("data-user", `${writable.__user}`);
192
+ element.setAttribute("data-color", `${writable.__color}`);
193
+ element.setAttribute("data-state", `${writable.__state}`);
194
+ return { element };
195
+ }
196
+
197
+ setPosition(x: number, y: number): void {
198
+ const writable = this.getWritable();
199
+ writable.__x = x;
200
+ writable.__y = y;
201
+ }
202
+
203
+ toggleColor(): void {
204
+ const writable = this.getWritable();
205
+ writable.__color = colors[writable.__color ?? "yellow"];
206
+ }
207
+
208
+ updateState(text: KeyValue): void {
209
+ const writable = this.getWritable();
210
+ writable.__state = text;
211
+ }
212
+
213
+ decorate(editor: LexicalEditor, config: EditorConfig): JSX.Element {
214
+ return createPortal(
215
+ <Suspense fallback={null}>
216
+ <StickyComponent
217
+ x={this.__x}
218
+ y={this.__y}
219
+ user={this.__user}
220
+ state={this.__state}
221
+ nodeKey={this.getKey()}
222
+ color={this.__color as AnyObject}
223
+ />
224
+ </Suspense>,
225
+ document.querySelector(".lexical-container") ?? document.body,
226
+ );
227
+ }
228
+
229
+ isIsolated(): true {
230
+ return true;
231
+ }
232
+ }
233
+
234
+ export function $isStickyNode(
235
+ node: LexicalNode | null | undefined,
236
+ ): node is StickyNode {
237
+ return node instanceof StickyNode;
238
+ }
239
+
240
+ export function $createStickyNode({
241
+ xOffset = 650,
242
+ yOffset = 32,
243
+ user = "",
244
+ color = "yellow",
245
+ state,
246
+ }: StickyNodeType): StickyNode {
247
+ const node = new StickyNode(xOffset, yOffset, user, color, state);
248
+ return $applyNodeReplacement(node);
249
+ }
@@ -0,0 +1,418 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ // REF: https://github.com/facebook/lexical/blob/main/packages/lexical-playground/src/plugins/DraggableBlockPlugin/index.tsx
25
+
26
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
27
+ import { eventFiles } from "@lexical/rich-text";
28
+ import { mergeRegister } from "@lexical/utils";
29
+ import {
30
+ $getNearestNodeFromDOMNode,
31
+ $getNodeByKey,
32
+ $getRoot,
33
+ COMMAND_PRIORITY_HIGH,
34
+ COMMAND_PRIORITY_LOW,
35
+ DRAGOVER_COMMAND,
36
+ DROP_COMMAND,
37
+ type LexicalEditor,
38
+ } from "lexical";
39
+ import {
40
+ useEffect,
41
+ useRef,
42
+ useState,
43
+ type DragEvent as ReactDragEvent,
44
+ } from "react";
45
+ import { createPortal } from "react-dom";
46
+ import { Point, Rect, isHTMLElement } from "../utils";
47
+
48
+ const SPACE = 4;
49
+ const TARGET_LINE_HALF_HEIGHT = 2;
50
+ const DRAGGABLE_BLOCK_MENU_CLASSNAME = "draggable-block-menu";
51
+ const DRAG_DATA_FORMAT = "application/x-lexical-drag-block";
52
+ const TEXT_BOX_HORIZONTAL_PADDING = 28;
53
+
54
+ const Downward = 1;
55
+ const Upward = -1;
56
+ const Indeterminate = 0;
57
+
58
+ let prevIndex = Infinity;
59
+
60
+ function getCurrentIndex(keysLength: number): number {
61
+ if (keysLength === 0) {
62
+ return Infinity;
63
+ }
64
+ if (prevIndex >= 0 && prevIndex < keysLength) {
65
+ return prevIndex;
66
+ }
67
+
68
+ return Math.floor(keysLength / 2);
69
+ }
70
+
71
+ function getTopLevelNodeKeys(editor: LexicalEditor): string[] {
72
+ return editor.getEditorState().read(() => $getRoot().getChildrenKeys());
73
+ }
74
+
75
+ function getBlockElement(
76
+ anchorElem: HTMLElement,
77
+ editor: LexicalEditor,
78
+ event: MouseEvent,
79
+ ): HTMLElement | null {
80
+ const anchorElementRect = anchorElem.getBoundingClientRect();
81
+ const topLevelNodeKeys = getTopLevelNodeKeys(editor);
82
+
83
+ let blockElem: HTMLElement | null = null;
84
+
85
+ editor.getEditorState().read(() => {
86
+ let index = getCurrentIndex(topLevelNodeKeys.length);
87
+ let direction = Indeterminate;
88
+
89
+ while (index >= 0 && index < topLevelNodeKeys.length) {
90
+ const key = topLevelNodeKeys[index];
91
+ const elem = editor.getElementByKey(key);
92
+ if (elem === null) {
93
+ break;
94
+ }
95
+ const point = new Point(event.x, event.y);
96
+ const domRect = Rect.fromDOM(elem);
97
+ const { marginTop, marginBottom } = window.getComputedStyle(elem);
98
+
99
+ const rect = domRect.generateNewRect({
100
+ bottom: domRect.bottom + parseFloat(marginBottom),
101
+ left: anchorElementRect.left,
102
+ right: anchorElementRect.right,
103
+ top: domRect.top - parseFloat(marginTop),
104
+ });
105
+
106
+ const {
107
+ result,
108
+ reason: { isOnTopSide, isOnBottomSide },
109
+ } = rect.contains(point);
110
+
111
+ if (result) {
112
+ blockElem = elem;
113
+ prevIndex = index;
114
+ break;
115
+ }
116
+
117
+ if (direction === Indeterminate) {
118
+ if (isOnTopSide) {
119
+ direction = Upward;
120
+ } else if (isOnBottomSide) {
121
+ direction = Downward;
122
+ } else {
123
+ // stop search block element
124
+ direction = Infinity;
125
+ }
126
+ }
127
+
128
+ index += direction;
129
+ }
130
+ });
131
+
132
+ return blockElem;
133
+ }
134
+
135
+ function isOnMenu(element: HTMLElement): boolean {
136
+ return !!element.closest(`.${DRAGGABLE_BLOCK_MENU_CLASSNAME}`);
137
+ }
138
+
139
+ function setMenuPosition(
140
+ targetElem: HTMLElement | null,
141
+ floatingElem: HTMLElement,
142
+ anchorElem: HTMLElement,
143
+ ) {
144
+ if (!targetElem) {
145
+ floatingElem.style.opacity = "0";
146
+ floatingElem.style.transform = "translate(-10000px, -10000px)";
147
+ return;
148
+ }
149
+
150
+ const scrollTop = targetElem.closest(".lexical-scroller")?.scrollTop ?? 0;
151
+
152
+ const targetRect = targetElem.getBoundingClientRect();
153
+ const targetStyle = window.getComputedStyle(targetElem);
154
+ const floatingElemRect = floatingElem.getBoundingClientRect();
155
+ const anchorElementRect = anchorElem.getBoundingClientRect();
156
+
157
+ const top =
158
+ scrollTop +
159
+ targetRect.top +
160
+ (parseInt(targetStyle.lineHeight, 10) - floatingElemRect.height) / 2 -
161
+ anchorElementRect.top;
162
+
163
+ const left = SPACE;
164
+
165
+ floatingElem.style.opacity = "1";
166
+ floatingElem.style.transform = `translate(${left}px, ${top}px)`;
167
+ }
168
+
169
+ function setDragImage(
170
+ dataTransfer: DataTransfer,
171
+ draggableBlockElem: HTMLElement,
172
+ ) {
173
+ const { transform } = draggableBlockElem.style;
174
+
175
+ // Remove dragImage borders
176
+ draggableBlockElem.style.transform = "translateZ(0)";
177
+ dataTransfer.setDragImage(draggableBlockElem, 0, 0);
178
+
179
+ setTimeout(() => {
180
+ draggableBlockElem.style.transform = transform;
181
+ });
182
+ }
183
+
184
+ function setTargetLine(
185
+ targetLineElem: HTMLElement,
186
+ targetBlockElem: HTMLElement,
187
+ mouseY: number,
188
+ anchorElem: HTMLElement,
189
+ ) {
190
+ const targetStyle = window.getComputedStyle(targetBlockElem);
191
+ const { top: targetBlockElemTop, height: targetBlockElemHeight } =
192
+ targetBlockElem.getBoundingClientRect();
193
+ const { top: anchorTop, width: anchorWidth } =
194
+ anchorElem.getBoundingClientRect();
195
+
196
+ const scrollTop =
197
+ targetBlockElem.closest(".editor-container")?.scrollTop ?? 0;
198
+
199
+ let lineTop = targetBlockElemTop;
200
+ // At the bottom of the target
201
+ if (mouseY - targetBlockElemTop > targetBlockElemHeight / 2) {
202
+ lineTop += targetBlockElemHeight + parseFloat(targetStyle.marginBottom);
203
+ } else {
204
+ lineTop -= parseFloat(targetStyle.marginTop);
205
+ }
206
+
207
+ const top = lineTop - anchorTop - TARGET_LINE_HALF_HEIGHT + scrollTop;
208
+ const left = TEXT_BOX_HORIZONTAL_PADDING - SPACE;
209
+
210
+ targetLineElem.style.transform = `translate(${left}px, ${top}px)`;
211
+ targetLineElem.style.width = `${
212
+ anchorWidth - (TEXT_BOX_HORIZONTAL_PADDING - SPACE) * 2
213
+ }px`;
214
+ targetLineElem.style.opacity = ".4";
215
+ }
216
+
217
+ function hideTargetLine(targetLineElem: HTMLElement | null) {
218
+ if (targetLineElem) {
219
+ targetLineElem.style.opacity = "0";
220
+ targetLineElem.style.transform = "translate(-10000px, -10000px)";
221
+ }
222
+ }
223
+
224
+ function useDraggableBlockMenu(
225
+ editor: LexicalEditor,
226
+ anchorElem: HTMLElement,
227
+ isEditable: boolean,
228
+ ): JSX.Element {
229
+ const scrollerElem = anchorElem.parentElement;
230
+
231
+ const menuRef = useRef<HTMLDivElement>(null);
232
+ const targetLineRef = useRef<HTMLDivElement>(null);
233
+ const isDraggingBlockRef = useRef<boolean>(false);
234
+ const [draggableBlockElem, setDraggableBlockElem] =
235
+ useState<HTMLElement | null>(null);
236
+
237
+ useEffect(() => {
238
+ function onMouseMove(event: MouseEvent) {
239
+ const target = event.target as HTMLElement;
240
+ if (!isHTMLElement(target)) {
241
+ setDraggableBlockElem(null);
242
+ return;
243
+ }
244
+
245
+ if (target && isOnMenu(target)) {
246
+ return;
247
+ }
248
+
249
+ const _draggableBlockElem = getBlockElement(anchorElem, editor, event);
250
+
251
+ setDraggableBlockElem(_draggableBlockElem);
252
+ }
253
+
254
+ function onMouseLeave() {
255
+ setDraggableBlockElem(null);
256
+ }
257
+
258
+ scrollerElem?.addEventListener("mousemove", onMouseMove);
259
+ scrollerElem?.addEventListener("mouseleave", onMouseLeave);
260
+
261
+ return () => {
262
+ scrollerElem?.removeEventListener("mousemove", onMouseMove);
263
+ scrollerElem?.removeEventListener("mouseleave", onMouseLeave);
264
+ };
265
+ }, [scrollerElem, anchorElem, editor]);
266
+
267
+ useEffect(() => {
268
+ if (menuRef.current) {
269
+ setMenuPosition(draggableBlockElem, menuRef.current, anchorElem);
270
+ }
271
+ }, [anchorElem, draggableBlockElem]);
272
+
273
+ useEffect(() => {
274
+ function onDragover(event: DragEvent): boolean {
275
+ if (!isDraggingBlockRef.current) {
276
+ return false;
277
+ }
278
+ const [isFileTransfer] = eventFiles(event);
279
+ if (isFileTransfer) {
280
+ return false;
281
+ }
282
+ const { pageY, target } = event;
283
+ if (!isHTMLElement(target)) {
284
+ return false;
285
+ }
286
+ const targetBlockElem = getBlockElement(anchorElem, editor, event);
287
+ const targetLineElem = targetLineRef.current;
288
+ if (targetBlockElem === null || targetLineElem === null) {
289
+ return false;
290
+ }
291
+ setTargetLine(targetLineElem, targetBlockElem, pageY, anchorElem);
292
+ // Prevent default event to be able to trigger onDrop events
293
+ event.preventDefault();
294
+ return true;
295
+ }
296
+
297
+ function onDrop(event: DragEvent): boolean {
298
+ if (!isDraggingBlockRef.current) {
299
+ return false;
300
+ }
301
+ const [isFileTransfer] = eventFiles(event);
302
+ if (isFileTransfer) {
303
+ return false;
304
+ }
305
+ const { target, dataTransfer, pageY } = event;
306
+ const dragData = dataTransfer?.getData(DRAG_DATA_FORMAT) ?? "";
307
+ const draggedNode = $getNodeByKey(dragData);
308
+ if (!draggedNode) {
309
+ return false;
310
+ }
311
+ if (!isHTMLElement(target)) {
312
+ return false;
313
+ }
314
+ const targetBlockElem = getBlockElement(anchorElem, editor, event);
315
+ if (!targetBlockElem) {
316
+ return false;
317
+ }
318
+ const targetNode = $getNearestNodeFromDOMNode(targetBlockElem);
319
+ if (!targetNode) {
320
+ return false;
321
+ }
322
+ if (targetNode === draggedNode) {
323
+ return true;
324
+ }
325
+ const { top, height } = targetBlockElem.getBoundingClientRect();
326
+ const shouldInsertAfter = pageY - top > height / 2;
327
+ if (
328
+ shouldInsertAfter ||
329
+ targetBlockElem.classList.contains("editor-cover-page")
330
+ ) {
331
+ targetNode.insertAfter(draggedNode);
332
+ } else {
333
+ targetNode.insertBefore(draggedNode);
334
+ }
335
+ setDraggableBlockElem(null);
336
+
337
+ return true;
338
+ }
339
+
340
+ return mergeRegister(
341
+ editor.registerCommand(
342
+ DRAGOVER_COMMAND,
343
+ (event) => {
344
+ return onDragover(event);
345
+ },
346
+ COMMAND_PRIORITY_LOW,
347
+ ),
348
+ editor.registerCommand(
349
+ DROP_COMMAND,
350
+ (event) => {
351
+ return onDrop(event);
352
+ },
353
+ COMMAND_PRIORITY_HIGH,
354
+ ),
355
+ );
356
+ }, [anchorElem, editor]);
357
+
358
+ function onDragStart(event: ReactDragEvent<HTMLDivElement>): void {
359
+ const dataTransfer = event.dataTransfer;
360
+ if (!dataTransfer || !draggableBlockElem) {
361
+ return;
362
+ }
363
+ setDragImage(dataTransfer, draggableBlockElem);
364
+ let nodeKey = "";
365
+ editor.update(() => {
366
+ const node = $getNearestNodeFromDOMNode(draggableBlockElem);
367
+ if (node) {
368
+ nodeKey = node.getKey();
369
+ }
370
+ });
371
+ isDraggingBlockRef.current = true;
372
+ dataTransfer.setData(DRAG_DATA_FORMAT, nodeKey);
373
+ }
374
+
375
+ function onDragEnd(): void {
376
+ isDraggingBlockRef.current = false;
377
+ hideTargetLine(targetLineRef.current);
378
+ }
379
+
380
+ if (draggableBlockElem?.classList?.contains("editor-cover-page")) {
381
+ return <div />;
382
+ }
383
+
384
+ return createPortal(
385
+ <>
386
+ <div
387
+ className="rounded px-0.5 py-px select-none cursor-grab opacity-0 absolute left-0 top-0 will-change-transform active:cursor-grabbing hover:bg-base"
388
+ ref={menuRef}
389
+ draggable={true}
390
+ onDragStart={onDragStart}
391
+ onDragEnd={onDragEnd}
392
+ >
393
+ <svg
394
+ xmlns="http://www.w3.org/2000/svg"
395
+ viewBox="0 0 24 24"
396
+ className="w-4 h-4 opacity-30 pointer-events-none"
397
+ >
398
+ <title>dots-grid</title>
399
+ <path d="M12 16C13.1 16 14 16.9 14 18S13.1 20 12 20 10 19.1 10 18 10.9 16 12 16M12 10C13.1 10 14 10.9 14 12S13.1 14 12 14 10 13.1 10 12 10.9 10 12 10M12 4C13.1 4 14 4.9 14 6S13.1 8 12 8 10 7.1 10 6 10.9 4 12 4M6 16C7.1 16 8 16.9 8 18S7.1 20 6 20 4 19.1 4 18 4.9 16 6 16M6 10C7.1 10 8 10.9 8 12S7.1 14 6 14 4 13.1 4 12 4.9 10 6 10M6 4C7.1 4 8 4.9 8 6S7.1 8 6 8 4 7.1 4 6 4.9 4 6 4M18 16C19.1 16 20 16.9 20 18S19.1 20 18 20 16 19.1 16 18 16.9 16 18 16M18 10C19.1 10 20 10.9 20 12S19.1 14 18 14 16 13.1 16 12 16.9 10 18 10M18 4C19.1 4 20 4.9 20 6S19.1 8 18 8 16 7.1 16 6 16.9 4 18 4Z" />
400
+ </svg>
401
+ </div>
402
+ <div
403
+ className="pointer-events-none bg-primary h-1 absolute top-0 left-0 opacity-0 will-change-transform"
404
+ ref={targetLineRef}
405
+ />
406
+ </>,
407
+ anchorElem,
408
+ );
409
+ }
410
+
411
+ export const DraggableBlockPlugin = ({
412
+ anchorElem = document.body,
413
+ }: {
414
+ anchorElem?: HTMLElement;
415
+ }): JSX.Element => {
416
+ const [editor] = useLexicalComposerContext();
417
+ return useDraggableBlockMenu(editor, anchorElem, editor._editable);
418
+ };
@@ -0,0 +1,38 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ // REF: https://github.com/facebook/lexical/blob/main/packages/lexical-playground/src/plugins/StickyPlugin/index.ts
25
+
26
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
27
+ import { useEffect } from "react";
28
+ import { StickyNode } from "../nodes/StickyNode";
29
+
30
+ export const StickyPlugin = (): JSX.Element | null => {
31
+ const [editor] = useLexicalComposerContext();
32
+ useEffect(() => {
33
+ if (!editor.hasNodes([StickyNode])) {
34
+ throw new Error("StickyPlugin: StickyNode not registered on editor");
35
+ }
36
+ }, [editor]);
37
+ return null;
38
+ };