@innoviatech/lexical-text-editor 1.0.6 → 2.0.1

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 (46) hide show
  1. package/README.md +107 -36
  2. package/dist/components/ContentPreview.d.ts +1 -1
  3. package/dist/components/HeadingSelectionList.d.ts +1 -1
  4. package/dist/components/LexicalTextEditor.d.ts +1 -1
  5. package/dist/components/common/Dialog.d.ts +9 -0
  6. package/dist/components/common/Popover.d.ts +21 -0
  7. package/dist/components/common/Tooltip.d.ts +10 -0
  8. package/dist/components/ui/button.d.ts +1 -1
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.esm.js +1 -1
  11. package/dist/index.esm.js.map +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/lexical-editor.css +1 -0
  15. package/dist/nodes/ExtendedTextNode.d.ts +11 -0
  16. package/dist/nodes/ImageNode.d.ts +10 -14
  17. package/dist/plugins/YoutubePlugin.d.ts +1 -0
  18. package/editor_screenshot.png +0 -0
  19. package/innoviatech-lexical-text-editor-2.0.0.tgz +0 -0
  20. package/package.json +20 -16
  21. package/rollup.config.js +3 -4
  22. package/src/components/AlignmentSelectionList.tsx +41 -24
  23. package/src/components/ColorPicker.tsx +7 -21
  24. package/src/components/ContentPreview.tsx +26 -20
  25. package/src/components/DropdownList.tsx +39 -24
  26. package/src/components/HeadingSelectionList.tsx +48 -32
  27. package/src/components/ImageComponent.tsx +52 -19
  28. package/src/components/InsertDropdownList.tsx +1 -1
  29. package/src/components/LexicalTextEditor.tsx +33 -28
  30. package/src/components/LinkModal.tsx +42 -45
  31. package/src/components/common/Dialog.tsx +47 -0
  32. package/src/components/common/Popover.tsx +87 -0
  33. package/src/components/common/Tooltip.tsx +43 -0
  34. package/src/components/ui/command.tsx +0 -1
  35. package/src/nodes/ExtendedTextNode.tsx +174 -0
  36. package/src/nodes/ImageNode.tsx +87 -48
  37. package/src/plugins/ColorPlugin.tsx +19 -14
  38. package/src/plugins/ImagePlugin.tsx +79 -84
  39. package/src/plugins/ListPlugin.tsx +20 -38
  40. package/src/plugins/ToolbarPlugin.tsx +76 -78
  41. package/src/plugins/VideoPlugin.tsx +46 -55
  42. package/src/plugins/YoutubePlugin.tsx +83 -0
  43. package/src/styles/lexical-editor.css +961 -3
  44. package/dist/style.css +0 -3
  45. package/postcss.config.js +0 -6
  46. package/src/assets/react.svg +0 -1
@@ -1,6 +1,6 @@
1
1
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
2
2
  import { $getNodeByKey, type NodeKey } from "lexical";
3
- import { useRef, useState } from "react";
3
+ import { useRef } from "react";
4
4
 
5
5
  type ImageComponentProps = {
6
6
  src: string;
@@ -20,10 +20,9 @@ export const ImageComponent: React.FC<ImageComponentProps> = ({
20
20
  maxWidth,
21
21
  }) => {
22
22
  const [editor] = useLexicalComposerContext();
23
- const [currentSize, setCurrentSize] = useState({
24
- width: typeof width === "number" ? width : maxWidth,
25
- height: typeof height === "number" ? height : "auto",
26
- });
23
+
24
+ const dragSizeRef = useRef<{ width: number; height: number, ratio: number } | null>(null);
25
+
27
26
  const wrapperRef = useRef<HTMLDivElement>(null);
28
27
  const startPos = useRef<{
29
28
  x: number;
@@ -35,36 +34,70 @@ export const ImageComponent: React.FC<ImageComponentProps> = ({
35
34
  const onMouseDown = (e: React.MouseEvent) => {
36
35
  e.preventDefault();
37
36
  e.stopPropagation();
37
+
38
+ const img = wrapperRef.current?.querySelector("img");
39
+ if (!img) return;
40
+
41
+ const renderedWidth = img.getBoundingClientRect().width;
42
+ const naturalRatio = img.naturalHeight / img.naturalWidth;
43
+ const renderedHeight = renderedWidth * naturalRatio;
44
+
38
45
  startPos.current = {
39
46
  x: e.clientX,
40
47
  y: e.clientY,
41
- width: currentSize.width as number,
42
- height: wrapperRef.current?.offsetHeight ?? 0,
48
+ width: renderedWidth,
49
+ height: renderedHeight,
43
50
  };
51
+
52
+ dragSizeRef.current = {
53
+ width: renderedWidth,
54
+ height: renderedHeight,
55
+ ratio: naturalRatio,
56
+ };
57
+
44
58
  window.addEventListener("mousemove", onMouseMove);
45
59
  window.addEventListener("mouseup", onMouseUp);
46
60
  };
47
61
 
62
+
63
+
48
64
  const onMouseMove = (e: MouseEvent) => {
49
- if (!startPos.current) return;
65
+ if (!startPos.current || !dragSizeRef.current) return;
66
+
50
67
  const deltaX = e.clientX - startPos.current.x;
51
- const newWidth = Math.min(maxWidth, Math.max(50, startPos.current.width + deltaX));
52
- const newHeight =
53
- (newWidth / startPos.current.width) * startPos.current.height || "auto";
54
- setCurrentSize({ width: newWidth, height: newHeight });
68
+ const newWidth = Math.min(
69
+ maxWidth,
70
+ Math.max(50, startPos.current.width + deltaX)
71
+ );
72
+
73
+ const newHeight = newWidth * dragSizeRef.current.ratio;
74
+
75
+ dragSizeRef.current.width = newWidth;
76
+ dragSizeRef.current.height = newHeight;
77
+
78
+ const img = wrapperRef.current?.querySelector("img");
79
+ if (img) {
80
+ img.style.width = newWidth + "px";
81
+ img.style.height = newHeight + "px";
82
+ }
55
83
  };
56
84
 
85
+
57
86
  const onMouseUp = () => {
58
- if (!startPos.current) return;
87
+ if (!dragSizeRef.current) return;
88
+
89
+ const { width: finalWidth, height: finalHeight } = dragSizeRef.current;
59
90
 
60
91
  editor.update(() => {
61
92
  const node = $getNodeByKey(nodeKey);
62
- // Use the node directly without importing ImageNode
63
- if (node && typeof (node as any).updateDimensions === 'function') {
64
- (node as any).updateDimensions(currentSize.width as number, currentSize.height as number);
93
+ if (node && typeof (node as any).updateDimensions === "function") {
94
+ (node as any).updateDimensions(finalWidth, finalHeight);
65
95
  }
66
96
  });
67
97
 
98
+ dragSizeRef.current = null;
99
+ startPos.current = null;
100
+
68
101
  window.removeEventListener("mousemove", onMouseMove);
69
102
  window.removeEventListener("mouseup", onMouseUp);
70
103
  };
@@ -81,9 +114,9 @@ export const ImageComponent: React.FC<ImageComponentProps> = ({
81
114
  <img
82
115
  src={src}
83
116
  alt={altText}
117
+ width={width === "inherit" ? undefined : width}
118
+ height={height === "inherit" ? undefined : height}
84
119
  style={{
85
- width: currentSize.width,
86
- height: currentSize.height,
87
120
  maxWidth: "100%",
88
121
  display: "block",
89
122
  }}
@@ -104,4 +137,4 @@ export const ImageComponent: React.FC<ImageComponentProps> = ({
104
137
  />
105
138
  </div>
106
139
  );
107
- };
140
+ };
@@ -4,7 +4,7 @@ import {
4
4
  PopoverContent,
5
5
  PopoverTrigger,
6
6
  } from "./ui/popover"
7
- // import { RichTextAction } from "@/constants";
7
+ // import { RichTextAction } from "constants";
8
8
  import { useState } from "react"
9
9
  import { BsPlus } from "react-icons/bs";
10
10
  import { HiChevronDown } from "react-icons/hi2";
@@ -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 type { EditorThemeClasses } from "lexical"
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"
@@ -17,6 +17,7 @@ import { ImageNode } from "../nodes/ImageNode"
17
17
  import { YoutubeNode } from "../nodes/YoutubeNode"
18
18
  import { VideoNode } from "../nodes/VideoNode"
19
19
  import "../styles/lexical-editor.css"
20
+ import { ExtendedTextNode } from "../nodes/ExtendedTextNode"
20
21
 
21
22
  export interface LexiTexProps {
22
23
  value: string
@@ -24,35 +25,33 @@ export interface LexiTexProps {
24
25
  placeholder?: string
25
26
  name: string,
26
27
  height?: string | null | undefined;
27
- width?: string | null | undefined;
28
+ maxWidth?: string | null | undefined;
28
29
  }
29
30
 
30
- // ✅ Theme config stays same
31
31
  const theme: EditorThemeClasses = {
32
32
  text: {
33
- bold: "font-bold",
34
- underline: "underline",
35
- italic: "italic",
36
- strikethrough: "line-through",
37
- underlineStrikethrough: "underline line-through",
38
- uppercase: "uppercase",
39
- lowercase: "lowercase",
40
- capitalize: "capitalize",
41
- code: "bg-gray-100 border border-gray-300 px-1 rounded text-sm font-mono text-black",
33
+ bold: "lexical-bold",
34
+ underline: "lexical-underline",
35
+ italic: "lexical-italic",
36
+ strikethrough: "lexical-strikethrough",
37
+ underlineStrikethrough: "lexical-underline-strikethrough",
38
+ uppercase: "lexical-uppercase",
39
+ lowercase: "lexical-lowercase",
40
+ capitalize: "lexical-capitalize",
41
+ code: "lexical-code",
42
42
  },
43
- paragraph: "mb-2",
43
+ paragraph: "lexical-paragraph",
44
44
  list: {
45
- ul: "list-disc list-inside pl-6",
46
- ol: "list-decimal list-inside pl-6",
47
- listitem: "mb-1",
45
+ ul: "lexical-list-ul",
46
+ ol: "lexical-list-ol",
47
+ listitem: "lexical-listitem",
48
48
  },
49
- quote: "border-l-4 border-gray-400 pl-4 italic text-gray-800 bg-gray-50 py-2 rounded-r",
50
- link: "text-blue-600 underline hover:text-blue-800",
49
+ quote: "lexical-quote",
50
+ link: "lexical-link",
51
51
  }
52
- // ... your imports
53
52
 
54
53
  export const LexicalTextEditor: React.FC<LexiTexProps> = React.memo(
55
- function LexicalTextEditor({ name, value, onChange, placeholder, width = null, height = null }) {
54
+ function LexicalTextEditor({ name, value, onChange, placeholder, maxWidth = null, height = null }) {
56
55
 
57
56
  const initialConfig = useMemo(
58
57
  () => ({
@@ -62,6 +61,12 @@ export const LexicalTextEditor: React.FC<LexiTexProps> = React.memo(
62
61
  console.error("Lexical error:", error);
63
62
  },
64
63
  nodes: [
64
+ ExtendedTextNode,
65
+ {
66
+ replace: TextNode,
67
+ with: (node: TextNode) => new ExtendedTextNode(node.__text),
68
+ withKlass: ExtendedTextNode,
69
+ },
65
70
  HeadingNode,
66
71
  CodeHighlightNode,
67
72
  CodeNode,
@@ -78,24 +83,24 @@ export const LexicalTextEditor: React.FC<LexiTexProps> = React.memo(
78
83
  );
79
84
 
80
85
  return (
81
- <div className="lexical-text-editor border-[1px] rounded-lg pb-2 scrollbar-thin"
86
+ <div className="lexical-text-editor lexical-editor-container"
82
87
  style={{
83
- width: width ? width : '',
84
- maxWidth: width ? '' : '1090px',
85
- height: height ? height : '500px',
88
+ maxWidth: maxWidth ? maxWidth : '1090px',
86
89
  }}
87
90
  >
88
91
  <LexicalComposer initialConfig={initialConfig}>
89
- <div className="px-2">
92
+ <div className="lexical-toolbar-container">
90
93
  <ToolbarPlugin value={value} />
91
94
  </div>
92
- <div className="relative h-full">
95
+ <div className="lexical-editor-inner">
93
96
  <RichTextPlugin
94
97
  contentEditable={
95
- <ContentEditable className={`text-start w-full h-full text-[14px] p-[8px] overflow-auto outline-0`} />
98
+ <ContentEditable className="lexical-content-editable" style={{
99
+ height: height ? height : '500px',
100
+ }} />
96
101
  }
97
102
  placeholder={
98
- <div className="absolute text-[#999] top-[8px] left-[10px] text-[14px]">
103
+ <div className="lexical-placeholder">
99
104
  {placeholder || "Some Text"}
100
105
  </div>
101
106
  }
@@ -1,10 +1,7 @@
1
- // FloatingLinkEditor.tsx
2
1
  import { useEffect, useRef, useState } from "react";
3
- import { Input } from "../components/ui/input";
4
- import { Button } from "../components/ui/button";
5
- import { Check, Pencil } from "lucide-react";
6
- import { cn } from "../lib/utils";
7
- import { TiDeleteOutline } from "react-icons/ti";
2
+ import { BsPencilSquare } from "react-icons/bs";
3
+ import { MdCheck } from "react-icons/md";
4
+ import { BiUnlink } from "react-icons/bi";
8
5
 
9
6
  interface LinkEditorProps {
10
7
  linkUrl: string;
@@ -25,7 +22,6 @@ export default function LinkModal({
25
22
  const editorRef = useRef<HTMLDivElement | null>(null);
26
23
  const inputRef = useRef<HTMLInputElement | null>(null);
27
24
 
28
-
29
25
  useEffect(() => {
30
26
  if (linkUrl === "https://") setEditMode(true);
31
27
  }, [linkUrl]);
@@ -45,26 +41,16 @@ export default function LinkModal({
45
41
  };
46
42
 
47
43
  document.addEventListener("mousedown", handleClickOutside);
48
- return () => {
49
- document.removeEventListener("mousedown", handleClickOutside);
50
- };
44
+ return () => document.removeEventListener("mousedown", handleClickOutside);
51
45
  }, [onCancel]);
52
46
 
53
47
  return (
54
- <div
55
- ref={editorRef}
56
- className={cn(
57
- "absolute z-[9999] bottom-[-50px] sm:bottom-[-60px] right-[20px]",
58
- "bg-white shadow-lg rounded-xl px-1.5 py-1 sm:px-3 sm:py-2 flex items-center space-x-2",
59
- "border border-gray-200"
60
- )}
61
-
62
- >
63
- <div className="flex-1 min-w-[200px]">
48
+ <div ref={editorRef} className="lexical-text-editor-link link-editor-container">
49
+ <div className="link-editor-input-wrapper">
64
50
  {isEditMode ? (
65
- <Input
51
+ <input
66
52
  ref={inputRef}
67
- className="h-8 text-sm"
53
+ className="link-editor-input"
68
54
  value={linkUrl}
69
55
  onChange={(e) => setLinkUrl(e.target.value)}
70
56
  onKeyDown={(e) => {
@@ -84,47 +70,58 @@ export default function LinkModal({
84
70
  href={linkUrl}
85
71
  target="_blank"
86
72
  rel="noopener noreferrer"
87
- className="text-blue-600 text-sm underline truncate block max-w-full text-start"
73
+ className="link-editor-url"
88
74
  >
89
75
  {linkUrl}
90
76
  </a>
91
77
  )}
92
78
  </div>
93
79
 
94
- <div>
95
- <Button
96
- variant="ghost"
97
- size="icon"
80
+ <div className="link-editor-buttons">
81
+
82
+ <button
83
+ className={`lexical-btn heading-trigger toolbar`}
84
+ onClick={() => {
85
+ if (isEditMode) {
86
+ onConfirm(linkUrl);
87
+ setEditMode(false);
88
+ } else setEditMode(true);
89
+ }}
90
+ >
91
+ {isEditMode ? <MdCheck size={18} /> : <BsPencilSquare size={14} />}
92
+ </button>
93
+
94
+ <button
95
+ className={`lexical-btn heading-trigger toolbar`}
96
+ onMouseDown={(e) => e.preventDefault()}
97
+ onClick={removeLink}
98
+ >
99
+ <BiUnlink size={14} style={{ stroke: 'black' }} />
100
+ </button>
101
+
102
+
103
+
104
+ {/* <button
98
105
  onMouseDown={(e) => e.preventDefault()}
99
106
  onClick={() => {
100
107
  if (isEditMode) {
101
108
  onConfirm(linkUrl);
102
109
  setEditMode(false);
103
- } else {
104
- setEditMode(true);
105
- }
110
+ } else setEditMode(true);
106
111
  }}
107
- className="h-8 w-8"
112
+ className={`size-8 toolbar icon`}
108
113
  >
109
- {isEditMode ? (
110
- <Check className="h-4 w-4" />
111
- ) : (
112
- <Pencil className="h-4 w-4" />
113
- )}
114
- </Button>
114
+ {isEditMode ? <MdCheck size={18} /> : <BsPencilSquare size={14} />}
115
+ </button> */}
115
116
 
116
- <Button
117
- variant="ghost"
118
- size="icon"
117
+ {/* <button
119
118
  onMouseDown={(e) => e.preventDefault()}
120
119
  onClick={removeLink}
121
- className="h-8 w-8"
120
+ className={`size-8 toolbar icon`}
122
121
  >
123
- <TiDeleteOutline className="h-4 w-4" />
124
- </Button>
122
+ <BiUnlink size={14} style={{ stroke: 'black' }} />
123
+ </button> */}
125
124
  </div>
126
-
127
125
  </div>
128
126
  );
129
127
  }
130
-
@@ -0,0 +1,47 @@
1
+ import { ReactNode, useEffect, useRef } from "react";
2
+
3
+ interface Props {
4
+ open: boolean;
5
+ onClose: () => void;
6
+ children: ReactNode;
7
+ forPreview?: boolean;
8
+ }
9
+
10
+ export default function RawDialog({ open, onClose, forPreview = false, children }: Props) {
11
+ const dialogRef = useRef<HTMLDivElement>(null);
12
+
13
+ useEffect(() => {
14
+ if (!open) return;
15
+
16
+ const handleOutsideClick = (e: MouseEvent) => {
17
+ if (dialogRef.current && !dialogRef.current.contains(e.target as Node)) {
18
+ onClose();
19
+ }
20
+ };
21
+
22
+ const handleEsc = (e: KeyboardEvent) => {
23
+ if (e.key === "Escape") onClose();
24
+ };
25
+
26
+ document.addEventListener("mousedown", handleOutsideClick);
27
+ document.addEventListener("keydown", handleEsc);
28
+
29
+ return () => {
30
+ document.removeEventListener("mousedown", handleOutsideClick);
31
+ document.removeEventListener("keydown", handleEsc);
32
+ };
33
+ }, [open, onClose]);
34
+
35
+ if (!open) return null;
36
+
37
+ return (
38
+ <div className="lexical-text-editor-dialog lte-dialog-overlay">
39
+ <div ref={dialogRef} className={`lte-dialog ${forPreview ? 'preview' : ''}`}>
40
+ <button className="lte-dialog-close" onClick={onClose}>
41
+ ×
42
+ </button>
43
+ {children}
44
+ </div>
45
+ </div>
46
+ );
47
+ }
@@ -0,0 +1,87 @@
1
+ import React, { useEffect, useRef, cloneElement } from "react";
2
+
3
+ interface PopoverProps {
4
+ open: boolean;
5
+ onOpenChange: (open: boolean) => void;
6
+ children: React.ReactNode;
7
+ }
8
+
9
+ export function Popover({ open, onOpenChange, children }: PopoverProps) {
10
+ const triggerRef = useRef<HTMLElement | null>(null);
11
+ const contentRef = useRef<HTMLDivElement | null>(null);
12
+
13
+ // Close on outside click
14
+ useEffect(() => {
15
+ function handleClick(e: MouseEvent) {
16
+ if (
17
+ !triggerRef.current?.contains(e.target as Node) &&
18
+ !contentRef.current?.contains(e.target as Node)
19
+ ) {
20
+ onOpenChange(false);
21
+ }
22
+ }
23
+ if (open) document.addEventListener("mousedown", handleClick);
24
+ return () => document.removeEventListener("mousedown", handleClick);
25
+ }, [open, onOpenChange]);
26
+
27
+ return (
28
+ <div className="popover-root">
29
+ {/*
30
+ Pass refs to trigger/content via props
31
+ so child components can attach them
32
+ */}
33
+ {React.Children.map(children, (child) => {
34
+ if (!child || typeof child !== "object") return child;
35
+
36
+ if ((child as any).type === PopoverTrigger) {
37
+ return cloneElement(child as any, { triggerRef });
38
+ }
39
+
40
+ if ((child as any).type === PopoverContent) {
41
+ return cloneElement(child as any, { contentRef });
42
+ }
43
+
44
+ return child;
45
+ })}
46
+ </div>
47
+ );
48
+ }
49
+
50
+ interface PopoverTriggerProps {
51
+ children: React.ReactNode;
52
+ triggerRef: React.RefObject<HTMLDivElement | null>;
53
+ }
54
+
55
+ export function PopoverTrigger({ children, triggerRef }: PopoverTriggerProps) {
56
+ return (
57
+ <div className="popover-trigger" ref={triggerRef}>
58
+ {children}
59
+ </div>
60
+ );
61
+ }
62
+
63
+ interface PopoverContentProps {
64
+ open: boolean;
65
+ align?: "start" | "center" | "end";
66
+ onClose: () => void;
67
+ children: React.ReactNode;
68
+ contentRef: React.RefObject<HTMLDivElement | null>;
69
+ }
70
+
71
+ export function PopoverContent({
72
+ open,
73
+ align = "start",
74
+ children,
75
+ contentRef,
76
+ }: PopoverContentProps) {
77
+ if (!open) return null;
78
+
79
+ return (
80
+ <div
81
+ ref={contentRef}
82
+ className={`popover-content align-${align}`}
83
+ >
84
+ {children}
85
+ </div>
86
+ );
87
+ }
@@ -0,0 +1,43 @@
1
+ import React, { useState, useRef, ReactNode } from "react";
2
+ import '../../styles/lexical-editor.css'
3
+ interface TooltipProps {
4
+ content: ReactNode;
5
+ direction?: "top" | "right" | "bottom" | "left";
6
+ delay?: number;
7
+ children: ReactNode;
8
+ }
9
+
10
+ const TooltipX: React.FC<TooltipProps> = ({ content, direction = "top", delay = 400, children }) => {
11
+ const [active, setActive] = useState(false);
12
+ const timeout = useRef<number | null>(null); // <-- browser setTimeout returns number
13
+
14
+ const showTip = () => {
15
+ timeout.current = window.setTimeout(() => {
16
+ setActive(true);
17
+ }, delay);
18
+ };
19
+
20
+ const hideTip = () => {
21
+ if (timeout.current) {
22
+ clearTimeout(timeout.current);
23
+ }
24
+ setActive(false);
25
+ };
26
+
27
+ return (
28
+ <div
29
+ className="Lexical-Tooltip-Wrapper"
30
+ onMouseEnter={showTip}
31
+ onMouseLeave={hideTip}
32
+ >
33
+ {children}
34
+ {active && (
35
+ <div className={`Lexical-Tooltip-Tip ${direction}`}>
36
+ {content}
37
+ </div>
38
+ )}
39
+ </div>
40
+ );
41
+ };
42
+
43
+ export default TooltipX;
@@ -1,4 +1,3 @@
1
- "use client"
2
1
 
3
2
  import * as React from "react"
4
3
  import { Command as CommandPrimitive } from "cmdk"