@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,10 +1,4 @@
1
1
 
2
- import { Button } from "../components/ui/button";
3
- import {
4
- Tooltip,
5
- TooltipTrigger,
6
- TooltipContent,
7
- } from "../components/ui/tooltip";
8
2
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
9
3
  import {
10
4
  INSERT_ORDERED_LIST_COMMAND,
@@ -12,6 +6,7 @@ import {
12
6
  REMOVE_LIST_COMMAND,
13
7
  } from "@lexical/list";
14
8
  import { BsListOl, BsListUl } from "react-icons/bs";
9
+ import TooltipX from "../components/common/Tooltip";
15
10
 
16
11
  interface ListPluginProps {
17
12
  blockType: string;
@@ -38,39 +33,26 @@ export default function ListPlugin({ blockType }: ListPluginProps) {
38
33
 
39
34
  return (
40
35
  <>
41
- <Tooltip delayDuration={700}>
42
- <TooltipTrigger asChild>
43
- <Button
44
- variant={blockType === "ol" ? "toolbarActive" : "toolbar"}
45
- size="icon"
46
- className="size-8"
47
- aria-label="Add ordered list"
48
- onClick={handleOrderedList}
49
- >
50
- <BsListOl />
51
- </Button>
52
- </TooltipTrigger>
53
- <TooltipContent side="bottom">
54
- Ordered list
55
- </TooltipContent>
56
- </Tooltip>
36
+ <TooltipX content="Ordered list" direction="bottom">
37
+ <button
38
+ className={`size-8 lexical-btn ${blockType === "ol" ? "toolbarActive" : "toolbar"
39
+ } icon`}
40
+ onClick={handleOrderedList}
41
+ >
42
+ <BsListOl />
43
+ </button>
44
+ </TooltipX>
45
+
46
+ <TooltipX content="Unordered list" direction="bottom">
47
+ <button
48
+ className={`size-8 lexical-btn ${blockType === "ul" ? "toolbarActive" : "toolbar"
49
+ } icon`}
50
+ onClick={handleUnorderedList}
51
+ >
52
+ <BsListUl />
53
+ </button>
54
+ </TooltipX>
57
55
 
58
- <Tooltip delayDuration={700}>
59
- <TooltipTrigger asChild>
60
- <Button
61
- variant={blockType === "ul" ? "toolbarActive" : "toolbar"}
62
- size="icon"
63
- className="size-8"
64
- aria-label="Add unordered list"
65
- onClick={handleUnorderedList}
66
- >
67
- <BsListUl />
68
- </Button>
69
- </TooltipTrigger>
70
- <TooltipContent side="bottom">
71
- Unordered list
72
- </TooltipContent>
73
- </Tooltip>
74
56
  </>
75
57
  );
76
58
  }
@@ -14,10 +14,7 @@ import {
14
14
  $isLinkNode,
15
15
  } from "@lexical/link";
16
16
 
17
- // import ColorPlugin from "./ColorPlugin"
18
- // import ListPlugin from "./ListPlugin"
19
17
  import { $isListNode, ListNode } from '@lexical/list'
20
- import { Button } from "../components/ui/button"
21
18
  import { Tooltip, TooltipContent, TooltipTrigger } from "../components/ui/tooltip"
22
19
  import { DropdownList } from "../components/DropdownList"
23
20
  import Separator from "../components/Separator"
@@ -30,8 +27,7 @@ import ListPlugin from "./ListPlugin"
30
27
  import ImagePlugin from "./ImagePlugin"
31
28
  import VideoPlugin from "./VideoPlugin"
32
29
  import ContentPreviewDialog from "../components/ContentPreview"
33
- // import ImagePlugin from "./ImagePlugin"
34
- // import YoutubePlugin from "./YoutubePlugin"
30
+ import TooltipX from "../components/common/Tooltip"
35
31
 
36
32
  export const ToolbarPlugin = ({ value }: { value: string }) => {
37
33
 
@@ -112,6 +108,7 @@ export const ToolbarPlugin = ({ value }: { value: string }) => {
112
108
  [HeadingAction.H6]: tag === "h6",
113
109
  [HeadingAction.Normal]: element.getType() === "paragraph",
114
110
  };
111
+ setBlockType(element.getType())
115
112
  setHeadingSelectionMap(newHeadingSelectionMap);
116
113
  }
117
114
  else {
@@ -338,16 +335,41 @@ export const ToolbarPlugin = ({ value }: { value: string }) => {
338
335
  });
339
336
  };
340
337
 
338
+ // const insertLink = useCallback(() => {
339
+ // console.log('insertlink');
340
+ // const selection = $getSelection();
341
+ // if (!$isRangeSelection(selection) || selection.isCollapsed()) {
342
+ // // Nothing selected → do nothing
343
+ // return;
344
+ // }
345
+
346
+ // if (!linkModalOpen) {
347
+ // if (linkUrl !== "") {
348
+ // editor.dispatchCommand(TOGGLE_LINK_COMMAND, linkUrl);
349
+ // }
350
+ // } else {
351
+ // editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
352
+ // }
353
+ // }, [editor, linkModalOpen]);
354
+
341
355
  const insertLink = useCallback(() => {
342
- console.log('insertlink');
343
- if (!linkModalOpen) {
344
- if (linkUrl !== "") {
345
- editor.dispatchCommand(TOGGLE_LINK_COMMAND, linkUrl);
356
+ editor.update(() => {
357
+ const selection = $getSelection();
358
+ if (!$isRangeSelection(selection) || selection.isCollapsed()) {
359
+ // Nothing selected → do nothing
360
+ return;
346
361
  }
347
- } else {
348
- editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
349
- }
350
- }, [editor, linkModalOpen]);
362
+
363
+ if (!linkModalOpen) {
364
+ if (linkUrl !== "") {
365
+ editor.dispatchCommand(TOGGLE_LINK_COMMAND, linkUrl);
366
+ }
367
+ } else {
368
+ editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
369
+ }
370
+ });
371
+ }, [editor, linkModalOpen, linkUrl]);
372
+
351
373
 
352
374
  const removeLink = useCallback(() => {
353
375
  setLinkUrl("")
@@ -359,27 +381,29 @@ export const ToolbarPlugin = ({ value }: { value: string }) => {
359
381
  // <div className="flex gap-1.5 justify-start items-center p-1.5 pb-2 mb-1 border-b-[1px] overflow-x-auto overflow-y-hidden scrollbar-thin">
360
382
 
361
383
  return (
362
- <div className="relative flex gap-1.5 flex-wrap justify-center items-center p-1.5 pb-2 mb-1 border-b-[1px]">
384
+ <div
385
+ className="lexical-toolbar-wrapper"
386
+ style={{
387
+ position: 'relative',
388
+ padding: 12, marginBottom: 4, borderBottom: '1px solid #e5e7eb'
389
+ }}
390
+ >
363
391
  {
364
392
  RICH_REDO_UNDO_OPTIONS.map(({ id, label, icon }) => (
365
393
  id === RichTextAction.Divider ? <Separator key={id} /> :
366
- <Tooltip key={id} delayDuration={700}>
367
- <TooltipTrigger asChild>
368
- <Button variant={selectionMap[id] ? "toolbarActive" : "toolbar"} size="icon" className="size-8"
369
- onClick={() => onAction(id)}
370
- disabled={!!disableMap[id]}
371
- >
372
- {icon}
373
- </Button>
374
- </TooltipTrigger>
375
- <TooltipContent side="bottom">
376
- {label}
377
- </TooltipContent>
378
- </Tooltip>
379
-
394
+ <TooltipX content={label} direction="bottom" key={label}>
395
+ <button
396
+ className={`size-8 lexical-btn ${selectionMap[id] ? "toolbarActive" : "toolbar"
397
+ } icon`}
398
+ onClick={() => onAction(id)}
399
+ disabled={!!disableMap[id]}
400
+ >
401
+ {icon}
402
+ </button>
403
+ </TooltipX>
380
404
  ))
381
405
  }
382
- <div className="flex gap-1.5 justify-start items-center">
406
+ <div style={{ display: 'flex', gap: 6, justifyContent: 'start', alignItems: 'center' }}>
383
407
 
384
408
  <HeadingSelectionList updateHeading={updateHeading} headingSelectionMap={headingSelectionMap} />
385
409
  <Separator />
@@ -387,23 +411,20 @@ export const ToolbarPlugin = ({ value }: { value: string }) => {
387
411
  {
388
412
  RICH_TEXT_OPTIONS.map(({ id, label, icon }) => (
389
413
  id === RichTextAction.Divider ? <Separator key={id} /> :
390
- <Tooltip key={id} delayDuration={700}>
391
- <TooltipTrigger asChild>
392
- <Button variant={selectionMap[id] ? "toolbarActive" : "toolbar"} size="icon" className="size-8"
393
- onClick={() => onAction(id)}
394
- disabled={!!disableMap[id]}
395
- >
396
- {icon}
397
- </Button>
398
- </TooltipTrigger>
399
- <TooltipContent side="bottom">
400
- {label}
401
- </TooltipContent>
402
- </Tooltip>
414
+ <TooltipX key={id} content={label} direction="bottom">
415
+ <button
416
+ className={`size-8 lexical-btn ${selectionMap[id] ? "toolbarActive" : "toolbar"
417
+ } icon`}
418
+ onClick={() => onAction(id)}
419
+ >
420
+ {icon}
421
+ </button>
422
+ </TooltipX>
423
+
403
424
 
404
425
  ))
405
426
  }
406
- <div className="flex gap-1.5 justify-start items-center">
427
+ <div style={{ display: 'flex', gap: 6, justifyContent: 'start', alignItems: 'center' }}>
407
428
 
408
429
  <ListPlugin blockType={blockType} />
409
430
 
@@ -412,27 +433,6 @@ export const ToolbarPlugin = ({ value }: { value: string }) => {
412
433
 
413
434
  <ColorPlugin />
414
435
 
415
- {/* alignment in single row */}
416
-
417
- {/* {
418
- RICH_ALIGNMENT_OPTIONS.map(({ id, label, icon }) => (
419
- id === RichTextAction.Divider ? <Separator key={id} /> :
420
- <Tooltip key={id} delayDuration={700}>
421
- <TooltipTrigger asChild>
422
- <Button variant={selectionMap[id] ? "toolbarActive" : "toolbar"} size="icon" className="size-8"
423
- onClick={() => onAction(id)}
424
- disabled={!!disableMap[id]}
425
- >
426
- {icon}
427
- </Button>
428
- </TooltipTrigger>
429
- <TooltipContent side="bottom">
430
- {label}
431
- </TooltipContent>
432
- </Tooltip>
433
- ))
434
- }
435
- <Separator /> */}
436
436
 
437
437
  {/* alignment in dropdown */}
438
438
 
@@ -440,25 +440,23 @@ export const ToolbarPlugin = ({ value }: { value: string }) => {
440
440
  <DropdownList onAction={onAction} selectionMap={selectionMap} />
441
441
 
442
442
 
443
- <div className="flex gap-1.5 justify-start items-center">
443
+ <div style={{ display: 'flex', gap: 6, justifyContent: 'start', alignItems: 'center' }}>
444
444
  <AlignmentSelectionList onAction={onAction} alignmentSelectionMap={selectionMap} />
445
445
  <Separator />
446
446
  </div>
447
447
 
448
448
 
449
- <Tooltip delayDuration={700}>
450
- <TooltipTrigger asChild>
451
- <Button variant={selectionMap[RichTextAction.Link] ? "toolbarActive" : "toolbar"} size="icon" className="size-8"
452
- onClick={() => onAction(RichTextAction.Link)}
453
- disabled={!!disableMap[RichTextAction.Link]}
454
- >
455
- {<IoIosLink />}
456
- </Button>
457
- </TooltipTrigger>
458
- <TooltipContent side="bottom">
459
- {"Link"}
460
- </TooltipContent>
461
- </Tooltip>
449
+
450
+ <TooltipX content="Link" direction="bottom">
451
+ <button
452
+ className={`size-8 lexical-btn ${selectionMap[RichTextAction.Link] ? "toolbarActive" : "toolbar"
453
+ } icon`}
454
+ onClick={() => onAction(RichTextAction.Link)}
455
+ disabled={!!disableMap[RichTextAction.Link]}
456
+ >
457
+ {<IoIosLink />}
458
+ </button>
459
+ </TooltipX>
462
460
 
463
461
 
464
462
  {linkModalOpen &&
@@ -3,39 +3,31 @@ import { FaYoutube } from "react-icons/fa";
3
3
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
4
4
  import { $insertNodes } from "lexical";
5
5
  import { $createVideoNode } from "../nodes/VideoNode";
6
- import {
7
- Dialog,
8
- DialogContent,
9
- DialogHeader,
10
- DialogTitle,
11
- DialogFooter,
12
- DialogTrigger,
13
- } from "../components/ui/dialog";
14
- import { Button } from "../components/ui/button";
15
- import { Input } from "../components/ui/input";
6
+ import RawDialog from "../components/common/Dialog";
16
7
 
17
8
  export default function VideoPlugin() {
18
9
  const [isOpen, setIsOpen] = useState(false);
19
10
  const [showMessage, setShowMessage] = useState(false);
20
11
  const [url, setURL] = useState("");
12
+
21
13
  const [editor] = useLexicalComposerContext();
22
14
 
23
15
  const onEmbed = () => {
24
16
  if (!url) return;
25
17
 
26
-
27
18
  let provider: "youtube" | "drive" | null = null;
28
19
  let id: string | null = null;
29
20
 
30
- // Detect YouTube
21
+ // youtube
31
22
  const ytMatch =
32
23
  /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/.exec(url);
24
+
33
25
  if (ytMatch && ytMatch[2]?.length === 11) {
34
26
  provider = "youtube";
35
27
  id = ytMatch[2];
36
28
  }
37
29
 
38
- // Detect Google Drive
30
+ // drive
39
31
  const driveMatch = /drive\.google\.com\/file\/d\/([a-zA-Z0-9_-]+)\//.exec(url);
40
32
  if (driveMatch && driveMatch[1]) {
41
33
  provider = "drive";
@@ -43,7 +35,7 @@ export default function VideoPlugin() {
43
35
  }
44
36
 
45
37
  if (!id || !provider) {
46
- setShowMessage(true)
38
+ setShowMessage(true);
47
39
  return;
48
40
  }
49
41
 
@@ -53,58 +45,57 @@ export default function VideoPlugin() {
53
45
  });
54
46
 
55
47
  setURL("");
56
- setShowMessage(false)
48
+ setShowMessage(false);
57
49
  setIsOpen(false);
58
50
  };
59
51
 
60
52
  useEffect(() => {
61
53
  if (!isOpen) {
62
54
  setURL("");
63
- setShowMessage(false)
55
+ setShowMessage(false);
64
56
  }
65
57
  }, [isOpen]);
66
58
 
67
59
  return (
68
- <div>
69
- <Dialog open={isOpen} onOpenChange={setIsOpen}>
70
- <DialogTrigger asChild>
71
- <Button
72
- size="icon"
73
- variant="ghost"
74
- className="rounded-md text-gray-700 hover:bg-gray-200"
60
+ <div className="lexical-text-editor-dialog">
61
+ {/* Open dialog button */}
62
+ <button
63
+ type="button"
64
+ className={`size-8 lexical-btn ${isOpen ? "toolbarActive" : "toolbar"} icon`}
65
+ onClick={() => setIsOpen(true)}
66
+ >
67
+ <FaYoutube size={18} color="red" />
68
+ </button>
69
+
70
+ {/* Dialog */}
71
+ <RawDialog open={isOpen} onClose={() => setIsOpen(false)}>
72
+ <div className="lte-dialog-header">Embed Video</div>
73
+
74
+ <div className="lte-dialog-body">
75
+ <input
76
+ className="lte-input"
77
+ placeholder="Add YouTube or Google Drive URL"
78
+ value={url}
79
+ onChange={(e) => setURL(e.target.value)}
80
+ />
81
+
82
+ {showMessage && (
83
+ <p className="lte-error-text">
84
+ It's not a valid YouTube or Google Drive URL
85
+ </p>
86
+ )}
87
+ </div>
88
+
89
+ <div className="lte-dialog-footer">
90
+ <button
91
+ className="lte-primary-btn"
92
+ disabled={!url}
93
+ onClick={onEmbed}
75
94
  >
76
- <FaYoutube />
77
- </Button>
78
- </DialogTrigger>
79
-
80
- <DialogContent className="sm:max-w-md">
81
- <DialogHeader>
82
- <DialogTitle className="!text-lg text-gray-800">Embed Video</DialogTitle>
83
- </DialogHeader>
84
-
85
- <div className="py-2">
86
- <Input
87
- value={url}
88
- onChange={(e) => setURL(e.target.value)}
89
- placeholder="Add YouTube or Google Drive URL"
90
- />
91
- {showMessage && <p className="mt-1.5 text-red-800/80 text-[0.8rem]">It's not a valid youtube or google drive URL</p>}
92
- </div>
93
-
94
-
95
-
96
- <DialogFooter>
97
- <Button
98
- onClick={onEmbed}
99
- disabled={!url}
100
- className="bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50"
101
- >
102
- Embed
103
- </Button>
104
- </DialogFooter>
105
- </DialogContent>
106
- </Dialog>
95
+ Embed
96
+ </button>
97
+ </div>
98
+ </RawDialog>
107
99
  </div>
108
100
  );
109
101
  }
110
-
@@ -0,0 +1,83 @@
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+ import { Youtube } from "lucide-react";
5
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
6
+ import { $insertNodes } from "lexical";
7
+ import { $createYoutubeNode } from "../nodes/YoutubeNode";
8
+ import {
9
+ Dialog,
10
+ DialogContent,
11
+ DialogHeader,
12
+ DialogTitle,
13
+ DialogFooter,
14
+ DialogTrigger,
15
+ } from "../components/ui/dialog";
16
+ import { Button } from "../components/ui/button";
17
+ import { Input } from "../components/ui/input";
18
+
19
+ export default function YoutubePlugin() {
20
+ const [isOpen, setIsOpen] = useState(false);
21
+ const [url, setURL] = useState("");
22
+
23
+ const [editor] = useLexicalComposerContext();
24
+
25
+ const onEmbed = () => {
26
+ if (!url) return;
27
+ const match =
28
+ /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/.exec(url);
29
+ const id = match && match?.[2]?.length === 11 ? match?.[2] : null;
30
+ if (!id) return;
31
+
32
+ editor.update(() => {
33
+ const node = $createYoutubeNode({ id });
34
+ $insertNodes([node]);
35
+ });
36
+
37
+ setURL("");
38
+ setIsOpen(false);
39
+ };
40
+
41
+ return (
42
+ <div>
43
+ <Dialog open={isOpen} onOpenChange={setIsOpen}>
44
+ <DialogTrigger asChild>
45
+ <Button
46
+ size="icon"
47
+ variant="ghost"
48
+ className="rounded-md text-gray-700 hover:bg-gray-200"
49
+ >
50
+ <Youtube className="h-4 w-4 text-red-600" />
51
+ </Button>
52
+ </DialogTrigger>
53
+
54
+ <DialogContent className="sm:max-w-md">
55
+ <DialogHeader>
56
+ <DialogTitle className="!text-xl">
57
+ Embed YouTube Video
58
+
59
+ </DialogTitle>
60
+ </DialogHeader>
61
+
62
+ <div className="py-2">
63
+ <Input
64
+ value={url}
65
+ onChange={(e) => setURL(e.target.value)}
66
+ placeholder="Add YouTube URL"
67
+ />
68
+ </div>
69
+
70
+ <DialogFooter>
71
+ <Button
72
+ onClick={onEmbed}
73
+ disabled={!url}
74
+ className="bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50"
75
+ >
76
+ Embed
77
+ </Button>
78
+ </DialogFooter>
79
+ </DialogContent>
80
+ </Dialog>
81
+ </div>
82
+ );
83
+ }