@innoviatech/lexical-text-editor 2.0.6 → 2.0.7
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/README.md +57 -10
- package/dist/components/ImageComponent.d.ts +1 -0
- package/dist/components/LexicalTextEditor.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/nodes/ImageNode.d.ts +5 -1
- package/dist/plugins/ImagePlugin.d.ts +3 -1
- package/dist/plugins/ToolbarPlugin.d.ts +2 -1
- package/package.json +1 -1
- package/src/components/ImageComponent.tsx +68 -2
- package/src/components/LexicalTextEditor.tsx +5 -5
- package/src/nodes/ExtendedTextNode.tsx +17 -28
- package/src/nodes/ImageNode.tsx +37 -0
- package/src/plugins/ImagePlugin.tsx +85 -14
- package/src/plugins/ToolbarPlugin.tsx +2 -2
|
@@ -8,6 +8,7 @@ export interface SerializedImageNode {
|
|
|
8
8
|
maxWidth: number;
|
|
9
9
|
width?: number | "inherit";
|
|
10
10
|
height?: number | "inherit";
|
|
11
|
+
caption?: string;
|
|
11
12
|
}
|
|
12
13
|
export type ImageNodePayload = {
|
|
13
14
|
src: string;
|
|
@@ -15,6 +16,7 @@ export type ImageNodePayload = {
|
|
|
15
16
|
maxWidth?: number;
|
|
16
17
|
width?: number | "inherit";
|
|
17
18
|
height?: number | "inherit";
|
|
19
|
+
caption?: string;
|
|
18
20
|
};
|
|
19
21
|
export declare const $createImageNode: (payload: ImageNodePayload) => ImageNode;
|
|
20
22
|
export declare class ImageNode extends DecoratorNode<JSX.Element> {
|
|
@@ -23,7 +25,8 @@ export declare class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
23
25
|
__width?: number | "inherit";
|
|
24
26
|
__height?: number | "inherit";
|
|
25
27
|
__maxWidth: number;
|
|
26
|
-
|
|
28
|
+
__caption: string;
|
|
29
|
+
constructor({ src, altText, maxWidth, width, height, caption, key, }?: ImageNodePayload & {
|
|
27
30
|
key?: NodeKey;
|
|
28
31
|
});
|
|
29
32
|
static getType(): string;
|
|
@@ -35,5 +38,6 @@ export declare class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
35
38
|
exportJSON(): SerializedImageNode;
|
|
36
39
|
static importJSON(serializedNode: SerializedImageNode): ImageNode;
|
|
37
40
|
updateDimensions(width: number, height: number): void;
|
|
41
|
+
setCaption(caption: string): void;
|
|
38
42
|
decorate(): JSX.Element;
|
|
39
43
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
2
2
|
import { $getNodeByKey, type NodeKey } from "lexical";
|
|
3
|
-
import { useRef } from "react";
|
|
3
|
+
import { useRef, useState } from "react";
|
|
4
4
|
|
|
5
5
|
type ImageComponentProps = {
|
|
6
6
|
src: string;
|
|
@@ -9,6 +9,7 @@ type ImageComponentProps = {
|
|
|
9
9
|
width?: number | "inherit";
|
|
10
10
|
height?: number | "inherit";
|
|
11
11
|
maxWidth: number;
|
|
12
|
+
caption?: string;
|
|
12
13
|
};
|
|
13
14
|
|
|
14
15
|
export const ImageComponent: React.FC<ImageComponentProps> = ({
|
|
@@ -18,8 +19,12 @@ export const ImageComponent: React.FC<ImageComponentProps> = ({
|
|
|
18
19
|
width,
|
|
19
20
|
height,
|
|
20
21
|
maxWidth,
|
|
22
|
+
caption = "",
|
|
21
23
|
}) => {
|
|
22
24
|
const [editor] = useLexicalComposerContext();
|
|
25
|
+
const [editingCaption, setEditingCaption] = useState(false);
|
|
26
|
+
const [captionText, setCaptionText] = useState(caption);
|
|
27
|
+
const captionInputRef = useRef<HTMLInputElement>(null);
|
|
23
28
|
|
|
24
29
|
const dragSizeRef = useRef<{ width: number; height: number, ratio: number } | null>(null);
|
|
25
30
|
|
|
@@ -102,6 +107,22 @@ export const ImageComponent: React.FC<ImageComponentProps> = ({
|
|
|
102
107
|
window.removeEventListener("mouseup", onMouseUp);
|
|
103
108
|
};
|
|
104
109
|
|
|
110
|
+
const saveCaption = () => {
|
|
111
|
+
setEditingCaption(false);
|
|
112
|
+
editor.update(() => {
|
|
113
|
+
const node = $getNodeByKey(nodeKey);
|
|
114
|
+
if (node && typeof (node as any).setCaption === "function") {
|
|
115
|
+
(node as any).setCaption(captionText);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const startCaptionEdit = () => {
|
|
121
|
+
setCaptionText(caption);
|
|
122
|
+
setEditingCaption(true);
|
|
123
|
+
setTimeout(() => captionInputRef.current?.focus(), 0);
|
|
124
|
+
};
|
|
125
|
+
|
|
105
126
|
return (
|
|
106
127
|
<div
|
|
107
128
|
ref={wrapperRef}
|
|
@@ -121,11 +142,56 @@ export const ImageComponent: React.FC<ImageComponentProps> = ({
|
|
|
121
142
|
display: "block",
|
|
122
143
|
}}
|
|
123
144
|
/>
|
|
145
|
+
{(caption || editingCaption) && (
|
|
146
|
+
<div style={{ textAlign: "center", marginTop: 4 }}>
|
|
147
|
+
{editingCaption ? (
|
|
148
|
+
<input
|
|
149
|
+
ref={captionInputRef}
|
|
150
|
+
className="lte-input"
|
|
151
|
+
style={{ width: "100%", textAlign: "center" }}
|
|
152
|
+
value={captionText}
|
|
153
|
+
onChange={(e) => setCaptionText(e.target.value)}
|
|
154
|
+
onBlur={saveCaption}
|
|
155
|
+
onKeyDown={(e) => {
|
|
156
|
+
if (e.key === "Enter") saveCaption();
|
|
157
|
+
}}
|
|
158
|
+
/>
|
|
159
|
+
) : (
|
|
160
|
+
<span
|
|
161
|
+
style={{
|
|
162
|
+
fontSize: 13,
|
|
163
|
+
color: "#666",
|
|
164
|
+
cursor: "text",
|
|
165
|
+
display: "inline-block",
|
|
166
|
+
minWidth: 20,
|
|
167
|
+
minHeight: 18,
|
|
168
|
+
}}
|
|
169
|
+
onClick={startCaptionEdit}
|
|
170
|
+
>
|
|
171
|
+
{caption}
|
|
172
|
+
</span>
|
|
173
|
+
)}
|
|
174
|
+
</div>
|
|
175
|
+
)}
|
|
176
|
+
{!caption && !editingCaption && (
|
|
177
|
+
<div style={{ textAlign: "center", marginTop: 2 }}>
|
|
178
|
+
<span
|
|
179
|
+
style={{
|
|
180
|
+
fontSize: 12,
|
|
181
|
+
color: "#999",
|
|
182
|
+
cursor: "pointer",
|
|
183
|
+
}}
|
|
184
|
+
onClick={startCaptionEdit}
|
|
185
|
+
>
|
|
186
|
+
+ Add caption
|
|
187
|
+
</span>
|
|
188
|
+
</div>
|
|
189
|
+
)}
|
|
124
190
|
<div
|
|
125
191
|
onMouseDown={onMouseDown}
|
|
126
192
|
style={{
|
|
127
193
|
position: "absolute",
|
|
128
|
-
bottom: 0,
|
|
194
|
+
bottom: caption || editingCaption ? 40 : 0,
|
|
129
195
|
right: 0,
|
|
130
196
|
width: 14,
|
|
131
197
|
height: 14,
|
|
@@ -5,7 +5,7 @@ import { CodeHighlightNode, CodeNode } from "@lexical/code"
|
|
|
5
5
|
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin"
|
|
6
6
|
import { ContentEditable } from "@lexical/react/LexicalContentEditable"
|
|
7
7
|
import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary"
|
|
8
|
-
import {
|
|
8
|
+
import { TextNode, type EditorThemeClasses } from "lexical"
|
|
9
9
|
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin"
|
|
10
10
|
import { ListPlugin } from "@lexical/react/LexicalListPlugin"
|
|
11
11
|
import { ListItemNode, ListNode } from "@lexical/list"
|
|
@@ -26,6 +26,7 @@ export interface LexiTexProps {
|
|
|
26
26
|
name: string,
|
|
27
27
|
height?: string | null | undefined;
|
|
28
28
|
maxWidth?: string | null | undefined;
|
|
29
|
+
onImageUpload?: (file: File) => Promise<string>;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
const theme: EditorThemeClasses = {
|
|
@@ -51,7 +52,7 @@ const theme: EditorThemeClasses = {
|
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
export const LexicalTextEditor: React.FC<LexiTexProps> = React.memo(
|
|
54
|
-
function LexicalTextEditor({ name, value, onChange, placeholder, maxWidth = null, height = null }) {
|
|
55
|
+
function LexicalTextEditor({ name, value, onChange, placeholder, maxWidth = null, height = null, onImageUpload }) {
|
|
55
56
|
|
|
56
57
|
const initialConfig = useMemo(
|
|
57
58
|
() => ({
|
|
@@ -76,8 +77,7 @@ export const LexicalTextEditor: React.FC<LexiTexProps> = React.memo(
|
|
|
76
77
|
LinkNode,
|
|
77
78
|
ImageNode,
|
|
78
79
|
YoutubeNode,
|
|
79
|
-
VideoNode
|
|
80
|
-
LineBreakNode
|
|
80
|
+
VideoNode
|
|
81
81
|
],
|
|
82
82
|
}) as any,
|
|
83
83
|
[name]
|
|
@@ -91,7 +91,7 @@ export const LexicalTextEditor: React.FC<LexiTexProps> = React.memo(
|
|
|
91
91
|
>
|
|
92
92
|
<LexicalComposer initialConfig={initialConfig}>
|
|
93
93
|
<div className="lexical-toolbar-container">
|
|
94
|
-
<ToolbarPlugin value={value} />
|
|
94
|
+
<ToolbarPlugin value={value} onImageUpload={onImageUpload} />
|
|
95
95
|
</div>
|
|
96
96
|
<div className="lexical-editor-inner">
|
|
97
97
|
<RichTextPlugin
|
|
@@ -7,9 +7,7 @@ import {
|
|
|
7
7
|
NodeKey,
|
|
8
8
|
TextNode,
|
|
9
9
|
SerializedTextNode,
|
|
10
|
-
LexicalNode
|
|
11
|
-
LineBreakNode,
|
|
12
|
-
$createLineBreakNode,
|
|
10
|
+
LexicalNode
|
|
13
11
|
} from 'lexical';
|
|
14
12
|
|
|
15
13
|
export class ExtendedTextNode extends TextNode {
|
|
@@ -29,58 +27,50 @@ export class ExtendedTextNode extends TextNode {
|
|
|
29
27
|
const importers = TextNode.importDOM();
|
|
30
28
|
return {
|
|
31
29
|
...importers,
|
|
32
|
-
|
|
33
|
-
br: () => ({
|
|
34
|
-
conversion: () => ({
|
|
35
|
-
node: $createLineBreakNode(),
|
|
36
|
-
}),
|
|
37
|
-
priority: 1,
|
|
38
|
-
}),
|
|
39
|
-
|
|
40
30
|
span: () => ({
|
|
41
31
|
conversion: patchStyleConversion(importers?.span),
|
|
42
|
-
priority: 1
|
|
32
|
+
priority: 1
|
|
43
33
|
}),
|
|
44
34
|
div: () => ({
|
|
45
35
|
conversion: patchStyleConversion(importers?.div),
|
|
46
|
-
priority: 1
|
|
36
|
+
priority: 1
|
|
47
37
|
}),
|
|
48
38
|
p: () => ({
|
|
49
39
|
conversion: patchStyleConversion(importers?.p),
|
|
50
|
-
priority: 1
|
|
40
|
+
priority: 1
|
|
51
41
|
}),
|
|
52
42
|
b: () => ({
|
|
53
43
|
conversion: patchStyleConversion(importers?.b),
|
|
54
|
-
priority: 1
|
|
44
|
+
priority: 1
|
|
55
45
|
}),
|
|
56
46
|
strong: () => ({
|
|
57
47
|
conversion: patchStyleConversion(importers?.strong),
|
|
58
|
-
priority: 1
|
|
48
|
+
priority: 1
|
|
59
49
|
}),
|
|
60
50
|
i: () => ({
|
|
61
51
|
conversion: patchStyleConversion(importers?.i),
|
|
62
|
-
priority: 1
|
|
52
|
+
priority: 1
|
|
63
53
|
}),
|
|
64
54
|
em: () => ({
|
|
65
55
|
conversion: patchStyleConversion(importers?.em),
|
|
66
|
-
priority: 1
|
|
56
|
+
priority: 1
|
|
67
57
|
}),
|
|
68
58
|
u: () => ({
|
|
69
59
|
conversion: patchStyleConversion(importers?.u),
|
|
70
|
-
priority: 1
|
|
60
|
+
priority: 1
|
|
71
61
|
}),
|
|
72
62
|
s: () => ({
|
|
73
63
|
conversion: patchStyleConversion(importers?.s),
|
|
74
|
-
priority: 1
|
|
64
|
+
priority: 1
|
|
75
65
|
}),
|
|
76
66
|
strike: () => ({
|
|
77
67
|
conversion: patchStyleConversion(importers?.strike),
|
|
78
|
-
priority: 1
|
|
68
|
+
priority: 1
|
|
79
69
|
}),
|
|
80
70
|
code: () => ({
|
|
81
71
|
conversion: patchStyleConversion(importers?.code),
|
|
82
|
-
priority: 1
|
|
83
|
-
})
|
|
72
|
+
priority: 1
|
|
73
|
+
})
|
|
84
74
|
};
|
|
85
75
|
}
|
|
86
76
|
|
|
@@ -97,9 +87,7 @@ export function $createExtendedTextNode(text: string = ''): ExtendedTextNode {
|
|
|
97
87
|
return $applyNodeReplacement(new ExtendedTextNode(text));
|
|
98
88
|
}
|
|
99
89
|
|
|
100
|
-
export function $isExtendedTextNode(
|
|
101
|
-
node: LexicalNode | null | undefined
|
|
102
|
-
): node is ExtendedTextNode {
|
|
90
|
+
export function $isExtendedTextNode(node: LexicalNode | null | undefined): node is ExtendedTextNode {
|
|
103
91
|
return node instanceof ExtendedTextNode;
|
|
104
92
|
}
|
|
105
93
|
|
|
@@ -160,6 +148,7 @@ function patchStyleConversion(
|
|
|
160
148
|
|
|
161
149
|
const inlineStyle = node.getAttribute('style');
|
|
162
150
|
if (inlineStyle) {
|
|
151
|
+
// Parse the style string to avoid duplicates
|
|
163
152
|
const stylePairs = inlineStyle.split(';').filter(pair => pair.trim());
|
|
164
153
|
stylePairs.forEach(pair => {
|
|
165
154
|
const [property] = pair.split(':').map(s => s.trim());
|
|
@@ -179,7 +168,7 @@ function patchStyleConversion(
|
|
|
179
168
|
return result.setStyle(styleString);
|
|
180
169
|
}
|
|
181
170
|
return result;
|
|
182
|
-
}
|
|
171
|
+
}
|
|
183
172
|
};
|
|
184
173
|
};
|
|
185
|
-
}
|
|
174
|
+
}
|
package/src/nodes/ImageNode.tsx
CHANGED
|
@@ -18,6 +18,7 @@ export interface SerializedImageNode {
|
|
|
18
18
|
maxWidth: number;
|
|
19
19
|
width?: number | "inherit";
|
|
20
20
|
height?: number | "inherit";
|
|
21
|
+
caption?: string;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export type ImageNodePayload = {
|
|
@@ -26,6 +27,7 @@ export type ImageNodePayload = {
|
|
|
26
27
|
maxWidth?: number;
|
|
27
28
|
width?: number | "inherit";
|
|
28
29
|
height?: number | "inherit";
|
|
30
|
+
caption?: string;
|
|
29
31
|
};
|
|
30
32
|
|
|
31
33
|
|
|
@@ -76,6 +78,7 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
76
78
|
__width?: number | "inherit";
|
|
77
79
|
__height?: number | "inherit";
|
|
78
80
|
__maxWidth: number;
|
|
81
|
+
__caption: string;
|
|
79
82
|
|
|
80
83
|
constructor(
|
|
81
84
|
{
|
|
@@ -84,6 +87,7 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
84
87
|
maxWidth = 1080,
|
|
85
88
|
width = "inherit",
|
|
86
89
|
height = "inherit",
|
|
90
|
+
caption = "",
|
|
87
91
|
key,
|
|
88
92
|
}: ImageNodePayload & { key?: NodeKey } = {
|
|
89
93
|
src: "",
|
|
@@ -96,6 +100,7 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
96
100
|
this.__maxWidth = maxWidth;
|
|
97
101
|
this.__width = width;
|
|
98
102
|
this.__height = height;
|
|
103
|
+
this.__caption = caption;
|
|
99
104
|
}
|
|
100
105
|
|
|
101
106
|
static getType(): string {
|
|
@@ -109,6 +114,7 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
109
114
|
width: node.__width,
|
|
110
115
|
height: node.__height,
|
|
111
116
|
maxWidth: node.__maxWidth,
|
|
117
|
+
caption: node.__caption,
|
|
112
118
|
key: node.getKey(),
|
|
113
119
|
});
|
|
114
120
|
}
|
|
@@ -139,6 +145,15 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
139
145
|
img.height = this.__height ?? 0;
|
|
140
146
|
}
|
|
141
147
|
|
|
148
|
+
if (this.__caption) {
|
|
149
|
+
const figure = document.createElement("figure");
|
|
150
|
+
const figcaption = document.createElement("figcaption");
|
|
151
|
+
figcaption.textContent = this.__caption;
|
|
152
|
+
figure.appendChild(img);
|
|
153
|
+
figure.appendChild(figcaption);
|
|
154
|
+
return { element: figure };
|
|
155
|
+
}
|
|
156
|
+
|
|
142
157
|
return { element: img };
|
|
143
158
|
}
|
|
144
159
|
|
|
@@ -148,6 +163,20 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
148
163
|
conversion: convertImageElement,
|
|
149
164
|
priority: 1,
|
|
150
165
|
}),
|
|
166
|
+
figure: () => ({
|
|
167
|
+
conversion: (node: HTMLElement) => {
|
|
168
|
+
const img = node.querySelector("img");
|
|
169
|
+
if (!img) return null;
|
|
170
|
+
const figcaption = node.querySelector("figcaption");
|
|
171
|
+
const caption = figcaption?.textContent || "";
|
|
172
|
+
const result = convertImageElement(img);
|
|
173
|
+
if (result?.node instanceof ImageNode && caption) {
|
|
174
|
+
result.node.__caption = caption;
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
},
|
|
178
|
+
priority: 1,
|
|
179
|
+
}),
|
|
151
180
|
};
|
|
152
181
|
}
|
|
153
182
|
|
|
@@ -160,6 +189,7 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
160
189
|
maxWidth: this.__maxWidth,
|
|
161
190
|
width: this.__width,
|
|
162
191
|
height: this.__height,
|
|
192
|
+
caption: this.__caption,
|
|
163
193
|
};
|
|
164
194
|
}
|
|
165
195
|
|
|
@@ -170,6 +200,7 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
170
200
|
maxWidth: serializedNode.maxWidth,
|
|
171
201
|
width: serializedNode.width,
|
|
172
202
|
height: serializedNode.height,
|
|
203
|
+
caption: serializedNode.caption,
|
|
173
204
|
});
|
|
174
205
|
}
|
|
175
206
|
|
|
@@ -179,6 +210,11 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
179
210
|
writable.__height = height;
|
|
180
211
|
}
|
|
181
212
|
|
|
213
|
+
setCaption(caption: string) {
|
|
214
|
+
const writable = this.getWritable();
|
|
215
|
+
writable.__caption = caption;
|
|
216
|
+
}
|
|
217
|
+
|
|
182
218
|
|
|
183
219
|
decorate(): JSX.Element {
|
|
184
220
|
return (
|
|
@@ -189,6 +225,7 @@ export class ImageNode extends DecoratorNode<JSX.Element> {
|
|
|
189
225
|
width={this.__width}
|
|
190
226
|
height={this.__height}
|
|
191
227
|
maxWidth={this.__maxWidth}
|
|
228
|
+
caption={this.__caption}
|
|
192
229
|
/>
|
|
193
230
|
);
|
|
194
231
|
}
|
|
@@ -1,35 +1,99 @@
|
|
|
1
1
|
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
2
|
-
import { $insertNodes } from "lexical";
|
|
2
|
+
import { $insertNodes, COMMAND_PRIORITY_LOW, PASTE_COMMAND } from "lexical";
|
|
3
3
|
import { $createImageNode } from "../nodes/ImageNode";
|
|
4
|
-
import { useRef, useState } from "react";
|
|
4
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
5
|
import { IoImageOutline } from "react-icons/io5";
|
|
6
6
|
import RawDialog from "../components/common/Dialog";
|
|
7
7
|
import { LuUpload } from "react-icons/lu";
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
type InsertImageParams = {
|
|
10
|
+
src: string;
|
|
11
|
+
altText?: string;
|
|
12
|
+
caption?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
async function insertImage(editor: any, { src, altText = "", caption = "" }: InsertImageParams) {
|
|
16
|
+
editor.update(() => {
|
|
17
|
+
const node = $createImageNode({ src, altText, caption });
|
|
18
|
+
$insertNodes([node]);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default function ImagePlugin({ onImageUpload }: { onImageUpload?: (file: File) => Promise<string> }) {
|
|
10
23
|
const [editor] = useLexicalComposerContext();
|
|
11
24
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
12
25
|
|
|
13
26
|
const [isOpen, setIsOpen] = useState(false);
|
|
14
27
|
const [url, setURL] = useState("");
|
|
15
28
|
const [altText, setAltText] = useState("");
|
|
29
|
+
const [caption, setCaption] = useState("");
|
|
16
30
|
const [file, setFile] = useState<File>();
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
const [isUploading, setIsUploading] = useState(false);
|
|
32
|
+
|
|
33
|
+
const handleFileUpload = useCallback(async (file: File, alt: string = "", cap: string = "") => {
|
|
34
|
+
let src: string;
|
|
35
|
+
|
|
36
|
+
if (onImageUpload) {
|
|
37
|
+
src = await onImageUpload(file);
|
|
38
|
+
} else {
|
|
39
|
+
src = URL.createObjectURL(file);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
insertImage(editor, { src, altText: alt, caption: cap });
|
|
43
|
+
}, [editor, onImageUpload]);
|
|
44
|
+
|
|
45
|
+
const addImage = async () => {
|
|
46
|
+
if (isUploading) return;
|
|
47
|
+
|
|
48
|
+
if (file) {
|
|
49
|
+
setIsUploading(true);
|
|
50
|
+
try {
|
|
51
|
+
await handleFileUpload(file, altText, caption);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error("Image upload failed:", err);
|
|
54
|
+
setIsUploading(false);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
setIsUploading(false);
|
|
58
|
+
} else if (url) {
|
|
59
|
+
insertImage(editor, { src: url, altText, caption });
|
|
60
|
+
} else {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
26
63
|
|
|
27
64
|
setFile(undefined);
|
|
28
65
|
setAltText("");
|
|
66
|
+
setCaption("");
|
|
29
67
|
setURL("");
|
|
30
68
|
setIsOpen(false);
|
|
31
69
|
};
|
|
32
70
|
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const unregister = editor.registerCommand(
|
|
73
|
+
PASTE_COMMAND,
|
|
74
|
+
(payload: ClipboardEvent | InputEvent) => {
|
|
75
|
+
if (payload instanceof ClipboardEvent) {
|
|
76
|
+
const items = payload.clipboardData?.items;
|
|
77
|
+
if (!items) return false;
|
|
78
|
+
|
|
79
|
+
for (const item of items) {
|
|
80
|
+
if (item.type.startsWith("image/")) {
|
|
81
|
+
payload.preventDefault();
|
|
82
|
+
const blob = item.getAsFile();
|
|
83
|
+
if (blob) {
|
|
84
|
+
handleFileUpload(blob, "", "").catch(console.error);
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
},
|
|
92
|
+
COMMAND_PRIORITY_LOW,
|
|
93
|
+
);
|
|
94
|
+
return () => unregister();
|
|
95
|
+
}, [editor, handleFileUpload]);
|
|
96
|
+
|
|
33
97
|
return (
|
|
34
98
|
<div className="lexical-text-editor-dialog">
|
|
35
99
|
{/* hidden file input */}
|
|
@@ -67,6 +131,13 @@ export default function ImagePlugin() {
|
|
|
67
131
|
onChange={(e) => setAltText(e.target.value)}
|
|
68
132
|
/>
|
|
69
133
|
|
|
134
|
+
<input
|
|
135
|
+
className="lte-input"
|
|
136
|
+
placeholder="Caption"
|
|
137
|
+
value={caption}
|
|
138
|
+
onChange={(e) => setCaption(e.target.value)}
|
|
139
|
+
/>
|
|
140
|
+
|
|
70
141
|
<input
|
|
71
142
|
className="lte-input"
|
|
72
143
|
placeholder="Add image by URL"
|
|
@@ -106,10 +177,10 @@ export default function ImagePlugin() {
|
|
|
106
177
|
<div className="lte-dialog-footer">
|
|
107
178
|
<button type="button"
|
|
108
179
|
className="lte-primary-btn"
|
|
109
|
-
disabled={!url && !file}
|
|
180
|
+
disabled={(!url && !file) || isUploading}
|
|
110
181
|
onClick={addImage}
|
|
111
182
|
>
|
|
112
|
-
Add Image
|
|
183
|
+
{isUploading ? "Uploading..." : "Add Image"}
|
|
113
184
|
</button>
|
|
114
185
|
</div>
|
|
115
186
|
</RawDialog>
|
|
@@ -29,7 +29,7 @@ import VideoPlugin from "./VideoPlugin"
|
|
|
29
29
|
import ContentPreviewDialog from "../components/ContentPreview"
|
|
30
30
|
import TooltipX from "../components/common/Tooltip"
|
|
31
31
|
|
|
32
|
-
export const ToolbarPlugin = ({ value }: { value: string }) => {
|
|
32
|
+
export const ToolbarPlugin = ({ value, onImageUpload }: { value: string; onImageUpload?: (file: File) => Promise<string> }) => {
|
|
33
33
|
|
|
34
34
|
const [editor] = useLexicalComposerContext();
|
|
35
35
|
const [disableMap, setDisableMap] = useState<{ [id: string]: boolean }>({
|
|
@@ -485,7 +485,7 @@ export const ToolbarPlugin = ({ value }: { value: string }) => {
|
|
|
485
485
|
/>}
|
|
486
486
|
|
|
487
487
|
|
|
488
|
-
<ImagePlugin />
|
|
488
|
+
<ImagePlugin onImageUpload={onImageUpload} />
|
|
489
489
|
|
|
490
490
|
<VideoPlugin />
|
|
491
491
|
|