@innoviatech/lexical-text-editor 2.0.5 → 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.
@@ -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
- constructor({ src, altText, maxWidth, width, height, key, }?: ImageNodePayload & {
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
  }
@@ -1 +1,3 @@
1
- export default function ImagePlugin(): import("react/jsx-runtime").JSX.Element;
1
+ export default function ImagePlugin({ onImageUpload }: {
2
+ onImageUpload?: (file: File) => Promise<string>;
3
+ }): import("react/jsx-runtime").JSX.Element;
@@ -1,3 +1,4 @@
1
- export declare const ToolbarPlugin: ({ value }: {
1
+ export declare const ToolbarPlugin: ({ value, onImageUpload }: {
2
2
  value: string;
3
+ onImageUpload?: (file: File) => Promise<string>;
3
4
  }) => import("react/jsx-runtime").JSX.Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innoviatech/lexical-text-editor",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -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,
@@ -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
  () => ({
@@ -90,7 +91,7 @@ export const LexicalTextEditor: React.FC<LexiTexProps> = React.memo(
90
91
  >
91
92
  <LexicalComposer initialConfig={initialConfig}>
92
93
  <div className="lexical-toolbar-container">
93
- <ToolbarPlugin value={value} />
94
+ <ToolbarPlugin value={value} onImageUpload={onImageUpload} />
94
95
  </div>
95
96
  <div className="lexical-editor-inner">
96
97
  <RichTextPlugin
@@ -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
- export default function ImagePlugin() {
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
- const addImage = () => {
19
- let src = url;
20
- if (file) src = URL.createObjectURL(file);
21
-
22
- editor.update(() => {
23
- const node = $createImageNode({ src, altText });
24
- $insertNodes([node]);
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