@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.
- package/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/_virtual/prism.js +5 -0
- package/dist/cjs/editor/Lexical.module.css +1 -0
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +432 -0
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +21 -0
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +27 -0
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +1139 -0
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +25 -0
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +50 -0
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +245 -0
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +19 -0
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +18 -0
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +471 -0
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +17 -0
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +24 -0
- package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +1275 -0
- package/dist/cjs/node_modules/@lexical/list/LexicalList.js +29 -0
- package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +29 -0
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +789 -0
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +29 -0
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +26 -0
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +976 -0
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +35 -0
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +35 -0
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2792 -0
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +23 -0
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +55 -0
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +888 -0
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +35 -0
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +66 -0
- package/dist/cjs/node_modules/@mdi/js/mdi.js +8 -0
- package/dist/cjs/node_modules/prismjs/components/prism-c.js +82 -0
- package/dist/cjs/node_modules/prismjs/components/prism-clike.js +33 -0
- package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +101 -0
- package/dist/cjs/node_modules/prismjs/components/prism-css.js +66 -0
- package/dist/cjs/node_modules/prismjs/components/prism-java.js +126 -0
- package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +174 -0
- package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +417 -0
- package/dist/cjs/node_modules/prismjs/components/prism-markup.js +188 -0
- package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +14 -0
- package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +60 -0
- package/dist/cjs/node_modules/prismjs/components/prism-python.js +67 -0
- package/dist/cjs/node_modules/prismjs/components/prism-rust.js +130 -0
- package/dist/cjs/node_modules/prismjs/components/prism-sql.js +34 -0
- package/dist/cjs/node_modules/prismjs/components/prism-swift.js +150 -0
- package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +62 -0
- package/dist/cjs/node_modules/prismjs/prism.js +1951 -0
- package/dist/cjs/packages/lexical/src/components/StickyComponent.js +221 -0
- package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +156 -0
- package/dist/cjs/packages/lexical/src/editor/theme.js +97 -0
- package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +451 -0
- package/dist/cjs/packages/lexical/src/index.js +7 -0
- package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +218 -0
- package/dist/cjs/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
- package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +20 -0
- package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
- package/dist/cjs/packages/lexical/src/utils/index.js +214 -0
- package/dist/esm/_virtual/prism.js +5 -0
- package/dist/esm/editor/Lexical.module.css +1 -0
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +423 -0
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +19 -0
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +18 -0
- package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +1121 -0
- package/dist/esm/node_modules/@lexical/code/LexicalCode.js +21 -0
- package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +32 -0
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +244 -0
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +18 -0
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +17 -0
- package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +463 -0
- package/dist/esm/node_modules/@lexical/link/LexicalLink.js +17 -0
- package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +16 -0
- package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +1262 -0
- package/dist/esm/node_modules/@lexical/list/LexicalList.js +23 -0
- package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +16 -0
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +781 -0
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +23 -0
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +18 -0
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +957 -0
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +26 -0
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +16 -0
- package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2754 -0
- package/dist/esm/node_modules/@lexical/table/LexicalTable.js +20 -0
- package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +17 -0
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +839 -0
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +26 -0
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +17 -0
- package/dist/esm/node_modules/@mdi/js/mdi.js +7 -0
- package/dist/esm/node_modules/prismjs/components/prism-c.js +82 -0
- package/dist/esm/node_modules/prismjs/components/prism-clike.js +33 -0
- package/dist/esm/node_modules/prismjs/components/prism-cpp.js +101 -0
- package/dist/esm/node_modules/prismjs/components/prism-css.js +66 -0
- package/dist/esm/node_modules/prismjs/components/prism-java.js +126 -0
- package/dist/esm/node_modules/prismjs/components/prism-javascript.js +174 -0
- package/dist/esm/node_modules/prismjs/components/prism-markdown.js +417 -0
- package/dist/esm/node_modules/prismjs/components/prism-markup.js +188 -0
- package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +14 -0
- package/dist/esm/node_modules/prismjs/components/prism-powershell.js +60 -0
- package/dist/esm/node_modules/prismjs/components/prism-python.js +67 -0
- package/dist/esm/node_modules/prismjs/components/prism-rust.js +130 -0
- package/dist/esm/node_modules/prismjs/components/prism-sql.js +34 -0
- package/dist/esm/node_modules/prismjs/components/prism-swift.js +150 -0
- package/dist/esm/node_modules/prismjs/components/prism-typescript.js +62 -0
- package/dist/esm/node_modules/prismjs/prism.js +1951 -0
- package/dist/esm/packages/lexical/src/components/StickyComponent.js +221 -0
- package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +156 -0
- package/dist/esm/packages/lexical/src/editor/theme.js +97 -0
- package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +450 -0
- package/dist/esm/packages/lexical/src/index.js +3 -0
- package/dist/esm/packages/lexical/src/nodes/StickyNode.js +216 -0
- package/dist/esm/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
- package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +20 -0
- package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
- package/dist/esm/packages/lexical/src/utils/index.js +211 -0
- package/dist/types/components/StickyComponent.d.ts +9 -0
- package/dist/types/editor/LexicalEditor.d.ts +13 -0
- package/dist/types/editor/theme.d.ts +70 -0
- package/dist/types/hooks/useFormatWatcher.d.ts +40 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/nodes/StickyNode.d.ts +35 -0
- package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -0
- package/dist/types/plugins/StickyPlugin.d.ts +1 -0
- package/dist/types/plugins/ToolbarPlugin.d.ts +1 -0
- package/dist/types/utils/index.d.ts +57 -0
- package/package.json +55 -0
- package/src/components/StickyComponent.tsx +271 -0
- package/src/editor/Lexical.module.css +218 -0
- package/src/editor/LexicalEditor.tsx +222 -0
- package/src/editor/theme.ts +93 -0
- package/src/hooks/useFormatWatcher.ts +504 -0
- package/src/index.ts +24 -0
- package/src/nodes/StickyNode.tsx +249 -0
- package/src/plugins/DraggableBlockPlugin.tsx +418 -0
- package/src/plugins/StickyPlugin.ts +38 -0
- package/src/plugins/ToolbarPlugin.tsx +279 -0
- package/src/utils/index.ts +303 -0
|
@@ -0,0 +1,271 @@
|
|
|
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/StickyComponent.tsx
|
|
25
|
+
|
|
26
|
+
import { AutoFocusPlugin } from "@lexical/react/LexicalAutoFocusPlugin";
|
|
27
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
28
|
+
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
|
29
|
+
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
|
|
30
|
+
import { LexicalNestedComposer } from "@lexical/react/LexicalNestedComposer";
|
|
31
|
+
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
|
|
32
|
+
import { mdiClose, mdiFormatColorFill } from "@mdi/js";
|
|
33
|
+
import { Icon } from "@react-fabric/core";
|
|
34
|
+
import { $getNodeByKey, createEditor, type NodeKey } from "lexical";
|
|
35
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
36
|
+
import { $isStickyNode } from "../nodes/StickyNode";
|
|
37
|
+
|
|
38
|
+
interface Positioning {
|
|
39
|
+
isDragging: boolean;
|
|
40
|
+
offsetX: number;
|
|
41
|
+
offsetY: number;
|
|
42
|
+
rootElementRect: null | { left: number; top: number };
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function positionSticky(
|
|
48
|
+
stickyElem: HTMLElement,
|
|
49
|
+
positioning: Positioning,
|
|
50
|
+
): void {
|
|
51
|
+
const style = stickyElem.style;
|
|
52
|
+
style.top = `${positioning.y}px`;
|
|
53
|
+
style.left = `${positioning.x}px`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const StickyComponent = ({
|
|
57
|
+
x = 0,
|
|
58
|
+
y = 0,
|
|
59
|
+
nodeKey,
|
|
60
|
+
color,
|
|
61
|
+
state,
|
|
62
|
+
user,
|
|
63
|
+
}: {
|
|
64
|
+
user: string;
|
|
65
|
+
state?: AnyObject;
|
|
66
|
+
color: "pink" | "yellow";
|
|
67
|
+
nodeKey: NodeKey;
|
|
68
|
+
x: number;
|
|
69
|
+
y: number;
|
|
70
|
+
}) => {
|
|
71
|
+
const [editor] = useLexicalComposerContext();
|
|
72
|
+
const stickyContainerRef = useRef<null | HTMLDivElement>(null);
|
|
73
|
+
const positioningRef = useRef<Positioning>({
|
|
74
|
+
isDragging: false,
|
|
75
|
+
offsetX: 0,
|
|
76
|
+
offsetY: 0,
|
|
77
|
+
rootElementRect: { left: 8, top: 8 },
|
|
78
|
+
x: 8,
|
|
79
|
+
y: 8,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const position = positioningRef.current;
|
|
84
|
+
position.x = x;
|
|
85
|
+
position.y = y;
|
|
86
|
+
|
|
87
|
+
const stickyContainer = stickyContainerRef.current;
|
|
88
|
+
if (stickyContainer !== null) {
|
|
89
|
+
positioningRef.current.rootElementRect = {
|
|
90
|
+
left: x,
|
|
91
|
+
top: y,
|
|
92
|
+
};
|
|
93
|
+
positionSticky(stickyContainer, position);
|
|
94
|
+
}
|
|
95
|
+
}, [x, y]);
|
|
96
|
+
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
const stickyContainer = stickyContainerRef.current;
|
|
99
|
+
if (stickyContainer !== null) {
|
|
100
|
+
// Delay adding transition so we don't trigger the
|
|
101
|
+
// transition on load of the sticky.
|
|
102
|
+
setTimeout(() => {
|
|
103
|
+
stickyContainer.style.setProperty(
|
|
104
|
+
"transition",
|
|
105
|
+
"top 300ms ease 0s, left 300ms ease 0s",
|
|
106
|
+
);
|
|
107
|
+
}, 500);
|
|
108
|
+
}
|
|
109
|
+
}, []);
|
|
110
|
+
|
|
111
|
+
const handlePointerMove = (event: PointerEvent) => {
|
|
112
|
+
const stickyContainer = stickyContainerRef.current;
|
|
113
|
+
const positioning = positioningRef.current;
|
|
114
|
+
const rootElementRect = positioning.rootElementRect;
|
|
115
|
+
if (
|
|
116
|
+
stickyContainer !== null &&
|
|
117
|
+
positioning.isDragging &&
|
|
118
|
+
rootElementRect !== null
|
|
119
|
+
) {
|
|
120
|
+
positioning.x =
|
|
121
|
+
rootElementRect.left + (event.pageX - positioning.offsetX);
|
|
122
|
+
positioning.y = rootElementRect.top + (event.pageY - positioning.offsetY);
|
|
123
|
+
if (positioning.x < 0) positioning.x = 0;
|
|
124
|
+
if (positioning.x > 642)
|
|
125
|
+
positioning.x = 642 - stickyContainer.offsetWidth;
|
|
126
|
+
if (positioning.y < 0) positioning.y = 0;
|
|
127
|
+
positionSticky(stickyContainer, positioning);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const handlePointerUp = (event: PointerEvent) => {
|
|
132
|
+
const stickyContainer = stickyContainerRef.current;
|
|
133
|
+
const positioning = positioningRef.current;
|
|
134
|
+
if (stickyContainer !== null) {
|
|
135
|
+
positioning.isDragging = false;
|
|
136
|
+
stickyContainer.classList.remove("dragging");
|
|
137
|
+
editor.update(() => {
|
|
138
|
+
const node = $getNodeByKey(nodeKey);
|
|
139
|
+
if ($isStickyNode(node)) {
|
|
140
|
+
node.setPosition(positioning.x, positioning.y);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
document.removeEventListener("pointermove", handlePointerMove);
|
|
145
|
+
document.removeEventListener("pointerup", handlePointerUp);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const handleDelete = () => {
|
|
149
|
+
editor.update(() => {
|
|
150
|
+
const node = $getNodeByKey(nodeKey);
|
|
151
|
+
if ($isStickyNode(node)) {
|
|
152
|
+
node.remove();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const handleColorChange = () => {
|
|
158
|
+
editor.update(() => {
|
|
159
|
+
const node = $getNodeByKey(nodeKey);
|
|
160
|
+
if ($isStickyNode(node)) {
|
|
161
|
+
node.toggleColor();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const innerEditor = useMemo(() => {
|
|
167
|
+
const editor = createEditor({ editable: true });
|
|
168
|
+
if (state) {
|
|
169
|
+
const editorState = editor.parseEditorState(state);
|
|
170
|
+
editor.setEditorState(editorState);
|
|
171
|
+
}
|
|
172
|
+
return editor;
|
|
173
|
+
}, []);
|
|
174
|
+
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
if (innerEditor._editable) {
|
|
177
|
+
const dispose = innerEditor.registerUpdateListener(({ editorState }) => {
|
|
178
|
+
editor.update(() => {
|
|
179
|
+
const node = $getNodeByKey(nodeKey);
|
|
180
|
+
if ($isStickyNode(node)) {
|
|
181
|
+
node.updateState(innerEditor.getEditorState().toJSON());
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return () => {
|
|
187
|
+
dispose();
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}, [innerEditor._editable]);
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<div ref={stickyContainerRef} className="sticky__container">
|
|
194
|
+
<div
|
|
195
|
+
className="sticky__handle"
|
|
196
|
+
onPointerDown={(event) => {
|
|
197
|
+
const stickyContainer = stickyContainerRef.current;
|
|
198
|
+
if (
|
|
199
|
+
stickyContainer == null ||
|
|
200
|
+
event.button === 2 ||
|
|
201
|
+
event.target !== stickyContainer.firstChild
|
|
202
|
+
) {
|
|
203
|
+
// Right click or click on editor should not work
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const stickContainer = stickyContainer;
|
|
207
|
+
const positioning = positioningRef.current;
|
|
208
|
+
if (stickContainer !== null) {
|
|
209
|
+
positioning.offsetX = event.clientX;
|
|
210
|
+
positioning.offsetY = event.clientY;
|
|
211
|
+
positioning.rootElementRect = {
|
|
212
|
+
left: positioning.x,
|
|
213
|
+
top: positioning.y,
|
|
214
|
+
};
|
|
215
|
+
positioning.isDragging = true;
|
|
216
|
+
stickContainer.classList.add("dragging");
|
|
217
|
+
document.addEventListener("pointermove", handlePointerMove);
|
|
218
|
+
document.addEventListener("pointerup", handlePointerUp);
|
|
219
|
+
event.preventDefault();
|
|
220
|
+
}
|
|
221
|
+
}}
|
|
222
|
+
/>
|
|
223
|
+
<div className={`sticky-note ${color}`}>
|
|
224
|
+
<div className="sticky__tools">
|
|
225
|
+
<button
|
|
226
|
+
onClick={handleColorChange}
|
|
227
|
+
className="sticky--color"
|
|
228
|
+
aria-label="Change sticky note color"
|
|
229
|
+
title="Color"
|
|
230
|
+
>
|
|
231
|
+
<Icon icon={mdiFormatColorFill} />
|
|
232
|
+
</button>
|
|
233
|
+
<button
|
|
234
|
+
onClick={handleDelete}
|
|
235
|
+
className="sticky--delete"
|
|
236
|
+
aria-label="Delete sticky note"
|
|
237
|
+
title="Delete"
|
|
238
|
+
>
|
|
239
|
+
<Icon icon={mdiClose} />
|
|
240
|
+
</button>
|
|
241
|
+
</div>
|
|
242
|
+
<div className="text-sm font-semibold">{user}</div>
|
|
243
|
+
<LexicalNestedComposer
|
|
244
|
+
initialEditor={innerEditor}
|
|
245
|
+
initialTheme={{
|
|
246
|
+
paragraph: "sticky__paragraph",
|
|
247
|
+
text: {
|
|
248
|
+
bold: "editor-textBold",
|
|
249
|
+
code: "editor-textCode",
|
|
250
|
+
italic: "editor-textItalic",
|
|
251
|
+
strikethrough: "editor-textStrikethrough",
|
|
252
|
+
subscript: "editor-textSubscript",
|
|
253
|
+
superscript: "editor-textSuperscript",
|
|
254
|
+
underline: "editor-textUnderline",
|
|
255
|
+
underlineStrikethrough: "editor-textUnderlineStrikethrough",
|
|
256
|
+
},
|
|
257
|
+
}}
|
|
258
|
+
>
|
|
259
|
+
<RichTextPlugin
|
|
260
|
+
contentEditable={<ContentEditable className="sticky__editor" />}
|
|
261
|
+
placeholder={
|
|
262
|
+
<div className="sticky-placeholder">Sticky note...</div>
|
|
263
|
+
}
|
|
264
|
+
ErrorBoundary={LexicalErrorBoundary}
|
|
265
|
+
/>
|
|
266
|
+
<AutoFocusPlugin />
|
|
267
|
+
</LexicalNestedComposer>
|
|
268
|
+
</div>
|
|
269
|
+
</div>
|
|
270
|
+
);
|
|
271
|
+
};
|
|
@@ -0,0 +1,218 @@
|
|
|
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
|
+
/* stylelint-disable custom-property-pattern */
|
|
25
|
+
|
|
26
|
+
@layer util {
|
|
27
|
+
.lexicalEditor,
|
|
28
|
+
.lexicalToolbar {
|
|
29
|
+
width: 21cm;
|
|
30
|
+
margin: 0 auto;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.lexicalEditor :global {
|
|
34
|
+
.editor-container.editor-container.editor-container {
|
|
35
|
+
/* stylelint-disable-next-line font-family-no-missing-generic-family-keyword */
|
|
36
|
+
font-family: "Sakkal Majalla";
|
|
37
|
+
font-size: 18px;
|
|
38
|
+
color: #0f172a;
|
|
39
|
+
background-color: #fff;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.editor-heading-h1 {
|
|
43
|
+
margin-top: 1.5em;
|
|
44
|
+
margin-bottom: 1.5em;
|
|
45
|
+
padding: 0 4px;
|
|
46
|
+
|
|
47
|
+
font-size: 1.5em;
|
|
48
|
+
font-weight: 600;
|
|
49
|
+
line-height: 1.5;
|
|
50
|
+
text-transform: capitalize;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.editor-heading-h2 {
|
|
54
|
+
margin-top: 1.5em;
|
|
55
|
+
margin-bottom: 1em;
|
|
56
|
+
padding: 0 4px;
|
|
57
|
+
|
|
58
|
+
font-size: 1.25em;
|
|
59
|
+
font-weight: 600;
|
|
60
|
+
line-height: 1.5;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.editor-heading-h6 {
|
|
64
|
+
margin-top: 1.5em;
|
|
65
|
+
margin-bottom: 1.5em;
|
|
66
|
+
|
|
67
|
+
font-size: 0.875em;
|
|
68
|
+
font-weight: 600;
|
|
69
|
+
line-height: 1.25;
|
|
70
|
+
color: #b45309;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.editor-paragraph {
|
|
74
|
+
margin-top: 1.5em;
|
|
75
|
+
margin-bottom: 1.5em;
|
|
76
|
+
font-size: 1em;
|
|
77
|
+
line-height: 1.5;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.editor-quote {
|
|
81
|
+
display: block;
|
|
82
|
+
|
|
83
|
+
margin-top: 1.5em;
|
|
84
|
+
margin-bottom: 1.5em;
|
|
85
|
+
padding-inline-start: 1em;
|
|
86
|
+
|
|
87
|
+
font-size: 1.125em;
|
|
88
|
+
line-height: 1.5;
|
|
89
|
+
color: #1e293b;
|
|
90
|
+
|
|
91
|
+
border-color: #64748b;
|
|
92
|
+
border-inline-start-width: 4px;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.editor-code {
|
|
96
|
+
display: block;
|
|
97
|
+
|
|
98
|
+
margin-top: 1.5em;
|
|
99
|
+
margin-bottom: 1.5em;
|
|
100
|
+
padding: 1em;
|
|
101
|
+
|
|
102
|
+
font-size: 1.125em;
|
|
103
|
+
line-height: 1.5;
|
|
104
|
+
color: #1e293b;
|
|
105
|
+
|
|
106
|
+
background-color: #e2e8f0;
|
|
107
|
+
border-inline-start-width: 4px;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.editor-textUnderline {
|
|
111
|
+
text-decoration: underline;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.editor-textStrikethrough {
|
|
115
|
+
text-decoration: line-through;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.editor-textUnderlineStrikethrough {
|
|
119
|
+
text-decoration: underline line-through;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.editor-page-break {
|
|
123
|
+
page-break-after: always;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.editor-no-break {
|
|
127
|
+
page-break-inside: avoid;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.editor-textSmall {
|
|
131
|
+
font-size: 0.875em;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.editor-textBold {
|
|
135
|
+
font-weight: 600;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.editor-textItalic {
|
|
139
|
+
font-style: italic;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.editor-textMuted {
|
|
143
|
+
color: #4b5563;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
.editor-list-ul {
|
|
147
|
+
padding-inline-start: 0.5em;
|
|
148
|
+
list-style: disc;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.editor-list-ol {
|
|
152
|
+
padding-inline-start: 0.5em;
|
|
153
|
+
list-style: decimal;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.editor-listItem {
|
|
157
|
+
margin-inline-start: 1em;
|
|
158
|
+
text-align: start;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
@media print {
|
|
162
|
+
html,
|
|
163
|
+
body {
|
|
164
|
+
min-height: 100vh;
|
|
165
|
+
margin: 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
div,
|
|
169
|
+
p,
|
|
170
|
+
span,
|
|
171
|
+
h1,
|
|
172
|
+
h2,
|
|
173
|
+
h3,
|
|
174
|
+
h4,
|
|
175
|
+
h5,
|
|
176
|
+
h6 {
|
|
177
|
+
box-sizing: border-box;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
body.report-template {
|
|
181
|
+
min-height: 100vh;
|
|
182
|
+
background-size: 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
body::after {
|
|
186
|
+
content: "";
|
|
187
|
+
|
|
188
|
+
position: fixed;
|
|
189
|
+
z-index: 10;
|
|
190
|
+
inset: 0;
|
|
191
|
+
|
|
192
|
+
width: 100vw;
|
|
193
|
+
max-width: 21cm;
|
|
194
|
+
height: 100vh;
|
|
195
|
+
margin: 0 auto;
|
|
196
|
+
|
|
197
|
+
background-image: inherit;
|
|
198
|
+
|
|
199
|
+
-webkit-print-color-adjust: exact;
|
|
200
|
+
print-color-adjust: exact;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.sticky__container,
|
|
204
|
+
.editor-page-break > div {
|
|
205
|
+
display: none;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.editor-container {
|
|
209
|
+
position: relative;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
.editor-editor {
|
|
213
|
+
box-sizing: border-box;
|
|
214
|
+
padding: 0 2cm;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
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
|
+
import { CodeHighlightNode, CodeNode } from "@lexical/code";
|
|
25
|
+
import { ListItemNode, ListNode } from "@lexical/list";
|
|
26
|
+
import { AutoFocusPlugin } from "@lexical/react/LexicalAutoFocusPlugin";
|
|
27
|
+
import { CheckListPlugin } from "@lexical/react/LexicalCheckListPlugin";
|
|
28
|
+
import { LexicalComposer } from "@lexical/react/LexicalComposer";
|
|
29
|
+
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
|
30
|
+
import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
|
|
31
|
+
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
|
|
32
|
+
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
|
|
33
|
+
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
|
|
34
|
+
import { TablePlugin } from "@lexical/react/LexicalTablePlugin";
|
|
35
|
+
import { HeadingNode, QuoteNode } from "@lexical/rich-text";
|
|
36
|
+
import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";
|
|
37
|
+
import { ErrorBoundary, Header, ThemeProvider } from "@react-fabric/core";
|
|
38
|
+
import { debounce } from "@react-fabric/utilities";
|
|
39
|
+
import classNames from "classnames";
|
|
40
|
+
import { type LexicalEditor as LXE } from "lexical";
|
|
41
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
42
|
+
import { StickyNode } from "../nodes/StickyNode";
|
|
43
|
+
import { DraggableBlockPlugin } from "../plugins/DraggableBlockPlugin";
|
|
44
|
+
import { StickyPlugin } from "../plugins/StickyPlugin";
|
|
45
|
+
import classes from "./Lexical.module.css";
|
|
46
|
+
import { LexicalTheme } from "./theme";
|
|
47
|
+
import { ToolbarPlugin } from "../plugins/ToolbarPlugin";
|
|
48
|
+
|
|
49
|
+
export interface EditorProps {
|
|
50
|
+
value?: string | AnyObject;
|
|
51
|
+
|
|
52
|
+
readOnly?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* publish mode removes all wrapping and renders plain lexical view for exporting to pdf using puppeteer
|
|
55
|
+
*/
|
|
56
|
+
publishMode?: boolean;
|
|
57
|
+
|
|
58
|
+
header?: string;
|
|
59
|
+
footer?: string;
|
|
60
|
+
|
|
61
|
+
onChange?: (value: KeyValue) => void;
|
|
62
|
+
onDirty?: (isDirty: boolean) => void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const LexicalEditor = ({
|
|
66
|
+
value,
|
|
67
|
+
readOnly,
|
|
68
|
+
publishMode,
|
|
69
|
+
onChange,
|
|
70
|
+
onDirty,
|
|
71
|
+
}: EditorProps) => {
|
|
72
|
+
const editorRef = useRef<LXE>();
|
|
73
|
+
const [editorContainer, setEditorContainer] = useState<HTMLDivElement | null>(
|
|
74
|
+
null,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// Catch any errors that occur during Lexical updates and log them
|
|
78
|
+
// or throw them as needed. If you don't throw them, Lexical will
|
|
79
|
+
// try to recover gracefully without losing user data.
|
|
80
|
+
const onError = (error: AnyObject) => {
|
|
81
|
+
console.error("ERROR", error);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const initEditor = useCallback((editor: LXE) => {
|
|
85
|
+
let dispose: AnyObject;
|
|
86
|
+
editorRef.current = editor;
|
|
87
|
+
const handler = debounce(({ editorState, dirtyElements, dirtyLeaves }) => {
|
|
88
|
+
editorState.read(() => {
|
|
89
|
+
if (dirtyElements.size > 0) {
|
|
90
|
+
onDirty?.(true);
|
|
91
|
+
}
|
|
92
|
+
onChange?.(editorState.toJSON());
|
|
93
|
+
});
|
|
94
|
+
}, 500);
|
|
95
|
+
const handle = editor.registerUpdateListener(handler);
|
|
96
|
+
|
|
97
|
+
// const cb = mergeRegister(
|
|
98
|
+
// editor.registerCommand<DragEvent>(
|
|
99
|
+
// DRAGOVER_COMMAND,
|
|
100
|
+
// (event) => {
|
|
101
|
+
// return onDragover(event);
|
|
102
|
+
// },
|
|
103
|
+
// COMMAND_PRIORITY_LOW,
|
|
104
|
+
// ),
|
|
105
|
+
// editor.registerCommand<DragEvent>(
|
|
106
|
+
// DROP_COMMAND,
|
|
107
|
+
// (event) => {
|
|
108
|
+
// return onDrop(event, editor);
|
|
109
|
+
// },
|
|
110
|
+
// COMMAND_PRIORITY_HIGH,
|
|
111
|
+
// ),
|
|
112
|
+
// );
|
|
113
|
+
|
|
114
|
+
return () => {
|
|
115
|
+
dispose?.();
|
|
116
|
+
handle?.();
|
|
117
|
+
// cb?.();
|
|
118
|
+
};
|
|
119
|
+
}, []);
|
|
120
|
+
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
editorRef.current?.setEditorState(
|
|
123
|
+
editorRef.current?.parseEditorState(value ?? ""),
|
|
124
|
+
);
|
|
125
|
+
}, [value]);
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
editorRef.current?.registerRootListener((rootElement) => {
|
|
129
|
+
rootElement &&
|
|
130
|
+
setTimeout(
|
|
131
|
+
() => (rootElement.contentEditable = `${!readOnly && !publishMode}`),
|
|
132
|
+
100,
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
}, [readOnly, publishMode]);
|
|
136
|
+
|
|
137
|
+
const initialConfig = useMemo(
|
|
138
|
+
() => ({
|
|
139
|
+
editorState: initEditor,
|
|
140
|
+
namespace: "MyEditor",
|
|
141
|
+
editable: !readOnly && !publishMode,
|
|
142
|
+
theme: LexicalTheme,
|
|
143
|
+
nodes: [
|
|
144
|
+
QuoteNode,
|
|
145
|
+
HeadingNode,
|
|
146
|
+
CodeHighlightNode,
|
|
147
|
+
CodeNode,
|
|
148
|
+
ListItemNode,
|
|
149
|
+
ListNode,
|
|
150
|
+
TableCellNode,
|
|
151
|
+
TableNode,
|
|
152
|
+
TableRowNode,
|
|
153
|
+
StickyNode,
|
|
154
|
+
],
|
|
155
|
+
onError,
|
|
156
|
+
}),
|
|
157
|
+
[],
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const Wrapper = useCallback(
|
|
161
|
+
({ children }: KeyValue) => {
|
|
162
|
+
return !publishMode ? (
|
|
163
|
+
<div className="lexical-scroller area-content overflow-auto">
|
|
164
|
+
<div
|
|
165
|
+
className={classNames(classes.lexicalToolbar, "sticky top-0 z-1")}
|
|
166
|
+
>
|
|
167
|
+
<Header className="bg-base">
|
|
168
|
+
<ToolbarPlugin />
|
|
169
|
+
</Header>
|
|
170
|
+
</div>
|
|
171
|
+
{children}
|
|
172
|
+
</div>
|
|
173
|
+
) : (
|
|
174
|
+
children
|
|
175
|
+
);
|
|
176
|
+
},
|
|
177
|
+
[publishMode],
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
return (
|
|
181
|
+
<ErrorBoundary>
|
|
182
|
+
<LexicalComposer initialConfig={initialConfig}>
|
|
183
|
+
<Wrapper>
|
|
184
|
+
<ThemeProvider colorScheme="light">
|
|
185
|
+
<div
|
|
186
|
+
role="none"
|
|
187
|
+
className={classNames(
|
|
188
|
+
classes.lexicalEditor,
|
|
189
|
+
"lexical-container relative bg-white min-h-full mx-auto shadow-md print:shadow-none",
|
|
190
|
+
)}
|
|
191
|
+
onDragOver={(e) => e.stopPropagation()}
|
|
192
|
+
onKeyDown={(e) => e.stopPropagation()}
|
|
193
|
+
ref={setEditorContainer}
|
|
194
|
+
>
|
|
195
|
+
<RichTextPlugin
|
|
196
|
+
contentEditable={
|
|
197
|
+
<ContentEditable className="lexical-editor px-[2cm] py-[1cm] min-h-screen" />
|
|
198
|
+
}
|
|
199
|
+
placeholder={
|
|
200
|
+
<div className="text-muted overflow-hidden absolute mt-6 top-[1cm] left-[2cm] select-none pointer-events-none">
|
|
201
|
+
Enter some text...
|
|
202
|
+
</div>
|
|
203
|
+
}
|
|
204
|
+
ErrorBoundary={LexicalErrorBoundary}
|
|
205
|
+
/>
|
|
206
|
+
<ListPlugin />
|
|
207
|
+
<CheckListPlugin />
|
|
208
|
+
<TablePlugin />
|
|
209
|
+
<HistoryPlugin />
|
|
210
|
+
<StickyPlugin />
|
|
211
|
+
<AutoFocusPlugin defaultSelection="rootStart" />
|
|
212
|
+
{/** Custom plugins **/}
|
|
213
|
+
{editorContainer && (
|
|
214
|
+
<DraggableBlockPlugin anchorElem={editorContainer} />
|
|
215
|
+
)}
|
|
216
|
+
</div>
|
|
217
|
+
</ThemeProvider>
|
|
218
|
+
</Wrapper>
|
|
219
|
+
</LexicalComposer>
|
|
220
|
+
</ErrorBoundary>
|
|
221
|
+
);
|
|
222
|
+
};
|