@innoviatech/lexical-text-editor 2.0.6 → 3.0.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/README.md +390 -241
- package/dist/components/AlignmentSelectionList.js +19 -0
- package/dist/components/ColorPicker.js +30 -0
- package/dist/components/ContentPreview.js +11 -0
- package/dist/components/DisplayControls.d.ts +16 -0
- package/dist/components/DropdownList.js +18 -0
- package/dist/components/FindReplaceModal.d.ts +11 -0
- package/dist/components/FindReplaceModal.js +120 -0
- package/dist/components/FontSelectionList.d.ts +17 -0
- package/dist/components/FontSelectionList.js +17 -0
- package/dist/components/HeadingSelectionList.js +19 -0
- package/dist/components/ImageComponent.d.ts +1 -0
- package/dist/components/ImageComponent.js +111 -0
- package/dist/components/InsertDropdownList.js +17 -0
- package/dist/components/LexicalTextEditor.d.ts +11 -0
- package/dist/components/LexicalTextEditor.js +128 -0
- package/dist/components/LinkModal.js +47 -0
- package/dist/components/Separator.js +4 -0
- package/dist/components/SourceView.d.ts +13 -0
- package/dist/components/SourceView.js +50 -0
- package/dist/components/StatusBar.d.ts +14 -0
- package/dist/components/StatusBar.js +30 -0
- package/dist/components/ToolbarOverflowModal.d.ts +15 -0
- package/dist/components/common/Dialog.js +27 -0
- package/dist/components/common/Popover.d.ts +10 -1
- package/dist/components/common/Popover.js +37 -0
- package/dist/components/common/Tooltip.d.ts +7 -0
- package/dist/components/common/Tooltip.js +20 -0
- package/dist/components/ui/button.js +35 -0
- package/dist/components/ui/command.js +33 -0
- package/dist/components/ui/dialog.js +35 -0
- package/dist/components/ui/input.js +6 -0
- package/dist/components/ui/popover.js +16 -0
- package/dist/components/ui/scroll-area.js +12 -0
- package/dist/components/ui/tooltip.js +16 -0
- package/dist/constants/index.d.ts +23 -1
- package/dist/constants/index.js +224 -0
- package/dist/index.d.ts +11 -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/lexical-editor.css +1 -1
- package/dist/lib/utils.js +5 -0
- package/dist/nodes/ExtendedTextNode.js +140 -0
- package/dist/nodes/ImageNode.d.ts +5 -1
- package/dist/nodes/ImageNode.js +149 -0
- package/dist/nodes/VideoNode.js +80 -0
- package/dist/nodes/YoutubeNode.js +64 -0
- package/dist/plugins/CodeBlockPlugin.d.ts +1 -0
- package/dist/plugins/CodeBlockPlugin.js +88 -0
- package/dist/plugins/ColorPlugin.js +47 -0
- package/dist/plugins/CustomOnChangePlugin.js +33 -0
- package/dist/plugins/FontFamilyPlugin.d.ts +1 -0
- package/dist/plugins/FontFamilyPlugin.js +53 -0
- package/dist/plugins/FontSizePlugin.d.ts +1 -0
- package/dist/plugins/FontSizePlugin.js +48 -0
- package/dist/plugins/ImagePlugin.d.ts +3 -1
- package/dist/plugins/ImagePlugin.js +91 -0
- package/dist/plugins/ListPlugin.js +44 -0
- package/dist/plugins/TablePlugin.d.ts +1 -0
- package/dist/plugins/TablePlugin.js +92 -0
- package/dist/plugins/ToolbarPlugin.d.ts +14 -1
- package/dist/plugins/ToolbarPlugin.js +381 -0
- package/dist/plugins/VideoPlugin.js +49 -0
- package/dist/plugins/YoutubePlugin.js +30 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/editor_screenshot.png +0 -0
- package/package.json +11 -4
- package/googlebd703883f12c69e4.html +0 -1
- package/rollup.config.js +0 -55
- package/src/components/AlignmentSelectionList.tsx +0 -61
- package/src/components/ColorPicker.tsx +0 -66
- package/src/components/ContentPreview.tsx +0 -51
- package/src/components/DropdownList.tsx +0 -57
- package/src/components/HeadingSelectionList.tsx +0 -61
- package/src/components/ImageComponent.tsx +0 -140
- package/src/components/InsertDropdownList.tsx +0 -36
- package/src/components/LexicalTextEditor.tsx +0 -119
- package/src/components/LinkModal.tsx +0 -127
- package/src/components/Separator.tsx +0 -7
- package/src/components/common/Dialog.tsx +0 -47
- package/src/components/common/Popover.tsx +0 -87
- package/src/components/common/Tooltip.tsx +0 -43
- package/src/components/ui/button.tsx +0 -58
- package/src/components/ui/command.tsx +0 -183
- package/src/components/ui/dialog.tsx +0 -142
- package/src/components/ui/input.tsx +0 -21
- package/src/components/ui/popover.tsx +0 -46
- package/src/components/ui/scroll-area.tsx +0 -56
- package/src/components/ui/tooltip.tsx +0 -59
- package/src/constants/index.tsx +0 -177
- package/src/index.ts +0 -6
- package/src/lib/utils.ts +0 -6
- package/src/nodes/ExtendedTextNode.tsx +0 -185
- package/src/nodes/ImageNode.tsx +0 -195
- package/src/nodes/VideoNode.tsx +0 -134
- package/src/nodes/YoutubeNode.tsx +0 -101
- package/src/plugins/ColorPlugin.tsx +0 -85
- package/src/plugins/CustomOnChangePlugin.tsx +0 -50
- package/src/plugins/ImagePlugin.tsx +0 -118
- package/src/plugins/ListPlugin.tsx +0 -58
- package/src/plugins/ToolbarPlugin.tsx +0 -500
- package/src/plugins/VideoPlugin.tsx +0 -100
- package/src/plugins/YoutubePlugin.tsx +0 -83
- package/src/styles/lexical-editor.css +0 -1053
- package/src/types/global.d.ts +0 -34
- package/src/types/other.d.ts +0 -11
- package/src/types/react-icons.d.ts +0 -11
- package/tailwind.config.js +0 -10
- package/tsconfig.json +0 -32
|
@@ -1,500 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { HeadingAction, LOW_PRIORIRTY, RICH_REDO_UNDO_OPTIONS, RICH_TEXT_OPTIONS, RichTextAction } from "../constants/index"
|
|
4
|
-
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"
|
|
5
|
-
import { $createParagraphNode, $getSelection, $isElementNode, $isRangeSelection, CAN_REDO_COMMAND, CAN_UNDO_COMMAND, FORMAT_ELEMENT_COMMAND, FORMAT_TEXT_COMMAND, REDO_COMMAND, SELECTION_CHANGE_COMMAND, UNDO_COMMAND } from "lexical"
|
|
6
|
-
import { useCallback, useEffect, useState } from "react"
|
|
7
|
-
import { $getNearestNodeOfType, mergeRegister } from '@lexical/utils'
|
|
8
|
-
import { type HeadingTagType, $createHeadingNode, $isHeadingNode } from '@lexical/rich-text'
|
|
9
|
-
import { $wrapNodes } from '@lexical/selection'
|
|
10
|
-
import { $createQuoteNode, $isQuoteNode } from "@lexical/rich-text";
|
|
11
|
-
import { $setBlocksType } from "@lexical/selection";
|
|
12
|
-
import {
|
|
13
|
-
TOGGLE_LINK_COMMAND,
|
|
14
|
-
$isLinkNode,
|
|
15
|
-
} from "@lexical/link";
|
|
16
|
-
|
|
17
|
-
import { $isListNode, ListNode } from '@lexical/list'
|
|
18
|
-
import { Tooltip, TooltipContent, TooltipTrigger } from "../components/ui/tooltip"
|
|
19
|
-
import { DropdownList } from "../components/DropdownList"
|
|
20
|
-
import Separator from "../components/Separator"
|
|
21
|
-
import { HeadingSelectionList } from "../components/HeadingSelectionList"
|
|
22
|
-
import { IoIosLink } from "react-icons/io"
|
|
23
|
-
import LinkModal from "../components/LinkModal"
|
|
24
|
-
import { AlignmentSelectionList } from "../components/AlignmentSelectionList"
|
|
25
|
-
import ColorPlugin from "./ColorPlugin"
|
|
26
|
-
import ListPlugin from "./ListPlugin"
|
|
27
|
-
import ImagePlugin from "./ImagePlugin"
|
|
28
|
-
import VideoPlugin from "./VideoPlugin"
|
|
29
|
-
import ContentPreviewDialog from "../components/ContentPreview"
|
|
30
|
-
import TooltipX from "../components/common/Tooltip"
|
|
31
|
-
|
|
32
|
-
export const ToolbarPlugin = ({ value }: { value: string }) => {
|
|
33
|
-
|
|
34
|
-
const [editor] = useLexicalComposerContext();
|
|
35
|
-
const [disableMap, setDisableMap] = useState<{ [id: string]: boolean }>({
|
|
36
|
-
[RichTextAction.Undo]: true,
|
|
37
|
-
[RichTextAction.Redo]: true
|
|
38
|
-
})
|
|
39
|
-
const [linkModalOpen, setLinkModalOpen] = useState(false);
|
|
40
|
-
const [selectionMap, setSelectionMap] = useState<{ [id: string]: boolean }>({
|
|
41
|
-
[RichTextAction.LeftAlign]: true,
|
|
42
|
-
})
|
|
43
|
-
const [headingSelectionMap, setHeadingSelectionMap] = useState<{ [id: string]: boolean }>({
|
|
44
|
-
[HeadingAction.Normal]: true,
|
|
45
|
-
})
|
|
46
|
-
const [blockType, setBlockType] = useState('paragraph');
|
|
47
|
-
const [linkUrl, setLinkUrl] = useState("https://");
|
|
48
|
-
let initialLoad = true
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const updateToolbar = () => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (initialLoad) {
|
|
57
|
-
const root = editor.getRootElement();
|
|
58
|
-
if (root) {
|
|
59
|
-
root.scrollTop = 0;
|
|
60
|
-
initialLoad = false
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const selection = $getSelection();
|
|
66
|
-
|
|
67
|
-
if ($isRangeSelection(selection)) {
|
|
68
|
-
const newSelectionMap = {
|
|
69
|
-
[RichTextAction.Bold]: selection.hasFormat("bold"),
|
|
70
|
-
[RichTextAction.Italics]: selection.hasFormat("italic"),
|
|
71
|
-
[RichTextAction.Underline]: selection.hasFormat("underline"),
|
|
72
|
-
[RichTextAction.Uppercase]: selection.hasFormat("uppercase"),
|
|
73
|
-
[RichTextAction.Lowercase]: selection.hasFormat("lowercase"),
|
|
74
|
-
[RichTextAction.Capitalize]: selection.hasFormat("capitalize"),
|
|
75
|
-
[RichTextAction.Strikethrough]: selection.hasFormat("strikethrough"),
|
|
76
|
-
[RichTextAction.Superscript]: selection.hasFormat("superscript"),
|
|
77
|
-
[RichTextAction.Subscript]: selection.hasFormat("subscript"),
|
|
78
|
-
[RichTextAction.Code]: selection.hasFormat("code"),
|
|
79
|
-
[RichTextAction.Highlight]: selection.hasFormat("highlight"),
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
setSelectionMap(newSelectionMap);
|
|
83
|
-
|
|
84
|
-
// headings------------------
|
|
85
|
-
|
|
86
|
-
const anchorNode = selection.anchor.getNode();
|
|
87
|
-
const element = anchorNode.getKey() === 'root' ? anchorNode : anchorNode.getTopLevelElementOrThrow();
|
|
88
|
-
const elementKey = element.getKey();
|
|
89
|
-
const elementDom = editor.getElementByKey(elementKey);
|
|
90
|
-
|
|
91
|
-
if (!elementDom) return;
|
|
92
|
-
if ($isListNode(element)) {
|
|
93
|
-
const parentList = $getNearestNodeOfType(anchorNode, ListNode)
|
|
94
|
-
const type = parentList ? parentList.getTag() : element.getTag();
|
|
95
|
-
setBlockType(type)
|
|
96
|
-
}
|
|
97
|
-
else if (element.getType() === "paragraph" || $isHeadingNode(element)) {
|
|
98
|
-
let tag: string | null = null;
|
|
99
|
-
if ($isHeadingNode(element)) {
|
|
100
|
-
tag = element.getTag();
|
|
101
|
-
}
|
|
102
|
-
const newHeadingSelectionMap = {
|
|
103
|
-
[HeadingAction.H1]: tag === "h1",
|
|
104
|
-
[HeadingAction.H2]: tag === "h2",
|
|
105
|
-
[HeadingAction.H3]: tag === "h3",
|
|
106
|
-
[HeadingAction.H4]: tag === "h4",
|
|
107
|
-
[HeadingAction.H5]: tag === "h5",
|
|
108
|
-
[HeadingAction.H6]: tag === "h6",
|
|
109
|
-
[HeadingAction.Normal]: element.getType() === "paragraph",
|
|
110
|
-
};
|
|
111
|
-
setBlockType(element.getType())
|
|
112
|
-
setHeadingSelectionMap(newHeadingSelectionMap);
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
setBlockType(element.getType());
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// ----------------------- alignments
|
|
119
|
-
|
|
120
|
-
const target = $isElementNode(anchorNode) ? anchorNode : element;
|
|
121
|
-
|
|
122
|
-
const alignment = target.getFormat();
|
|
123
|
-
// returns: 'left' = 1 | 'right' = 3 | 'center' = 2 | 'justify' = 4 | '' = 0 (default)
|
|
124
|
-
setSelectionMap((prev) => ({
|
|
125
|
-
...prev,
|
|
126
|
-
[RichTextAction.LeftAlign]: alignment === 0 || alignment === 1,
|
|
127
|
-
[RichTextAction.CenterAlign]: alignment === 2,
|
|
128
|
-
[RichTextAction.RightAlign]: alignment === 3,
|
|
129
|
-
[RichTextAction.JustifyAlign]: alignment === 4,
|
|
130
|
-
}));
|
|
131
|
-
|
|
132
|
-
// ----------------------- quotes
|
|
133
|
-
|
|
134
|
-
if (element.getType() === "quote") {
|
|
135
|
-
setSelectionMap((prev) => ({
|
|
136
|
-
...prev,
|
|
137
|
-
[RichTextAction.Quote]: true,
|
|
138
|
-
}));
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
setSelectionMap((prev) => ({
|
|
142
|
-
...prev,
|
|
143
|
-
[RichTextAction.Quote]: false,
|
|
144
|
-
}));
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Update links
|
|
148
|
-
const node = selection.anchor.getNode();
|
|
149
|
-
const parent = node.getParent();
|
|
150
|
-
if ($isLinkNode(parent)) {
|
|
151
|
-
setSelectionMap((prev) => ({
|
|
152
|
-
...prev,
|
|
153
|
-
[RichTextAction.Link]: true,
|
|
154
|
-
}));
|
|
155
|
-
|
|
156
|
-
setLinkUrl(parent.getURL());
|
|
157
|
-
setLinkModalOpen(true);
|
|
158
|
-
} else if ($isLinkNode(node)) {
|
|
159
|
-
setSelectionMap((prev) => ({
|
|
160
|
-
...prev,
|
|
161
|
-
[RichTextAction.Link]: true,
|
|
162
|
-
}));
|
|
163
|
-
setLinkUrl(node.getURL());
|
|
164
|
-
setLinkModalOpen(true);
|
|
165
|
-
} else {
|
|
166
|
-
setSelectionMap((prev) => ({
|
|
167
|
-
...prev,
|
|
168
|
-
[RichTextAction.Link]: false,
|
|
169
|
-
}));
|
|
170
|
-
setLinkUrl("https://")
|
|
171
|
-
setLinkModalOpen(false);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
useEffect(() => {
|
|
180
|
-
return mergeRegister(
|
|
181
|
-
editor.registerUpdateListener(({ editorState }) => {
|
|
182
|
-
editorState.read(() => {
|
|
183
|
-
updateToolbar();
|
|
184
|
-
})
|
|
185
|
-
}),
|
|
186
|
-
editor.registerCommand(
|
|
187
|
-
SELECTION_CHANGE_COMMAND,
|
|
188
|
-
() => {
|
|
189
|
-
updateToolbar();
|
|
190
|
-
return false;
|
|
191
|
-
},
|
|
192
|
-
LOW_PRIORIRTY
|
|
193
|
-
),
|
|
194
|
-
editor.registerCommand(
|
|
195
|
-
CAN_UNDO_COMMAND,
|
|
196
|
-
(payload) => {
|
|
197
|
-
setDisableMap(prevDisableMap => ({
|
|
198
|
-
...prevDisableMap, undo: !payload
|
|
199
|
-
}))
|
|
200
|
-
return false;
|
|
201
|
-
},
|
|
202
|
-
LOW_PRIORIRTY
|
|
203
|
-
|
|
204
|
-
),
|
|
205
|
-
editor.registerCommand(
|
|
206
|
-
CAN_REDO_COMMAND,
|
|
207
|
-
(payload) => {
|
|
208
|
-
setDisableMap(prevDisableMap => ({
|
|
209
|
-
...prevDisableMap, redo: !payload
|
|
210
|
-
}))
|
|
211
|
-
return false;
|
|
212
|
-
},
|
|
213
|
-
LOW_PRIORIRTY
|
|
214
|
-
)
|
|
215
|
-
)
|
|
216
|
-
}, [])
|
|
217
|
-
|
|
218
|
-
const onAction = (id: RichTextAction) => {
|
|
219
|
-
switch (id) {
|
|
220
|
-
case RichTextAction.Bold: {
|
|
221
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "bold")
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
case RichTextAction.Italics: {
|
|
225
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "italic")
|
|
226
|
-
break;
|
|
227
|
-
}
|
|
228
|
-
case RichTextAction.Underline: {
|
|
229
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "underline")
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
case RichTextAction.Lowercase: {
|
|
233
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "lowercase")
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
case RichTextAction.Uppercase: {
|
|
237
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "uppercase")
|
|
238
|
-
break;
|
|
239
|
-
}
|
|
240
|
-
case RichTextAction.Capitalize: {
|
|
241
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "capitalize")
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
case RichTextAction.Strikethrough: {
|
|
245
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "strikethrough")
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
case RichTextAction.Superscript: {
|
|
249
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "superscript")
|
|
250
|
-
break;
|
|
251
|
-
}
|
|
252
|
-
case RichTextAction.Subscript: {
|
|
253
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "subscript")
|
|
254
|
-
break;
|
|
255
|
-
}
|
|
256
|
-
case RichTextAction.Highlight: {
|
|
257
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "highlight")
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
case RichTextAction.Code: {
|
|
261
|
-
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "code")
|
|
262
|
-
break;
|
|
263
|
-
}
|
|
264
|
-
case RichTextAction.Quote: {
|
|
265
|
-
editor.update(() => {
|
|
266
|
-
const selection = $getSelection();
|
|
267
|
-
if ($isRangeSelection(selection)) {
|
|
268
|
-
const anchorNode = selection.anchor.getNode();
|
|
269
|
-
const parent = anchorNode.getParent();
|
|
270
|
-
|
|
271
|
-
if (parent && $isQuoteNode(parent)) {
|
|
272
|
-
$setBlocksType(selection, () => $createParagraphNode());
|
|
273
|
-
} else {
|
|
274
|
-
$wrapNodes(selection, () => $createQuoteNode());
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
break;
|
|
279
|
-
}
|
|
280
|
-
case RichTextAction.Link: {
|
|
281
|
-
insertLink();
|
|
282
|
-
setLinkModalOpen(true);
|
|
283
|
-
break;
|
|
284
|
-
}
|
|
285
|
-
case RichTextAction.LeftAlign: {
|
|
286
|
-
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'left')
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
case RichTextAction.RightAlign: {
|
|
290
|
-
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'right')
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
case RichTextAction.CenterAlign: {
|
|
294
|
-
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'center')
|
|
295
|
-
break;
|
|
296
|
-
}
|
|
297
|
-
case RichTextAction.JustifyAlign: {
|
|
298
|
-
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'justify')
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
301
|
-
case RichTextAction.Undo: {
|
|
302
|
-
editor.dispatchCommand(UNDO_COMMAND, undefined)
|
|
303
|
-
break;
|
|
304
|
-
}
|
|
305
|
-
case RichTextAction.Redo: {
|
|
306
|
-
editor.dispatchCommand(REDO_COMMAND, undefined)
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// useKeyBindings({ onAction });
|
|
313
|
-
|
|
314
|
-
// const getSelectedBtnProps = (
|
|
315
|
-
// isSelected: boolean
|
|
316
|
-
// ): Partial<IconButtonProps> =>
|
|
317
|
-
// isSelected
|
|
318
|
-
// ? {
|
|
319
|
-
// colorScheme: "blue",
|
|
320
|
-
// variant: "solid",
|
|
321
|
-
// }
|
|
322
|
-
// : {};
|
|
323
|
-
// const ref = useRef<HTMLSelectElement | null>(null)
|
|
324
|
-
|
|
325
|
-
const updateHeading = (heading: HeadingTagType | 'normal') => {
|
|
326
|
-
editor.update(() => {
|
|
327
|
-
const selection = $getSelection();
|
|
328
|
-
if ($isRangeSelection(selection)) {
|
|
329
|
-
if (heading === "normal") {
|
|
330
|
-
$wrapNodes(selection, () => $createParagraphNode());
|
|
331
|
-
} else {
|
|
332
|
-
$wrapNodes(selection, () => $createHeadingNode(heading));
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
});
|
|
336
|
-
};
|
|
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
|
-
|
|
355
|
-
const insertLink = useCallback(() => {
|
|
356
|
-
editor.update(() => {
|
|
357
|
-
const selection = $getSelection();
|
|
358
|
-
if (!$isRangeSelection(selection) || selection.isCollapsed()) {
|
|
359
|
-
// Nothing selected → do nothing
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
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
|
-
|
|
373
|
-
|
|
374
|
-
const removeLink = useCallback(() => {
|
|
375
|
-
setLinkUrl("")
|
|
376
|
-
editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
|
|
377
|
-
setLinkModalOpen(false)
|
|
378
|
-
}, [editor])
|
|
379
|
-
|
|
380
|
-
|
|
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">
|
|
382
|
-
|
|
383
|
-
return (
|
|
384
|
-
<div
|
|
385
|
-
className="lexical-toolbar-wrapper"
|
|
386
|
-
style={{
|
|
387
|
-
position: 'relative',
|
|
388
|
-
padding: 12, marginBottom: 4, borderBottom: '1px solid #e5e7eb'
|
|
389
|
-
}}
|
|
390
|
-
>
|
|
391
|
-
{
|
|
392
|
-
RICH_REDO_UNDO_OPTIONS.map(({ id, label, icon }) => (
|
|
393
|
-
id === RichTextAction.Divider ? <Separator key={id} /> :
|
|
394
|
-
<TooltipX content={label} direction="bottom" key={label}>
|
|
395
|
-
<button type="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>
|
|
404
|
-
))
|
|
405
|
-
}
|
|
406
|
-
<div style={{ display: 'flex', gap: 6, justifyContent: 'start', alignItems: 'center' }}>
|
|
407
|
-
|
|
408
|
-
<HeadingSelectionList updateHeading={updateHeading} headingSelectionMap={headingSelectionMap} />
|
|
409
|
-
<Separator />
|
|
410
|
-
</div>
|
|
411
|
-
{
|
|
412
|
-
RICH_TEXT_OPTIONS.map(({ id, label, icon }) => (
|
|
413
|
-
id === RichTextAction.Divider ? <Separator key={id} /> :
|
|
414
|
-
<TooltipX key={id} content={label} direction="bottom">
|
|
415
|
-
<button type="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
|
-
|
|
424
|
-
|
|
425
|
-
))
|
|
426
|
-
}
|
|
427
|
-
<div style={{ display: 'flex', gap: 6, justifyContent: 'start', alignItems: 'center' }}>
|
|
428
|
-
|
|
429
|
-
<ListPlugin blockType={blockType} />
|
|
430
|
-
|
|
431
|
-
<Separator />
|
|
432
|
-
</div>
|
|
433
|
-
|
|
434
|
-
<ColorPlugin />
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
{/* alignment in dropdown */}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
<DropdownList onAction={onAction} selectionMap={selectionMap} />
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
<div style={{ display: 'flex', gap: 6, justifyContent: 'start', alignItems: 'center' }}>
|
|
444
|
-
<AlignmentSelectionList onAction={onAction} alignmentSelectionMap={selectionMap} />
|
|
445
|
-
<Separator />
|
|
446
|
-
</div>
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
<TooltipX content="Link" direction="bottom">
|
|
451
|
-
<button type="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>
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
{linkModalOpen &&
|
|
463
|
-
<LinkModal
|
|
464
|
-
linkUrl={linkUrl}
|
|
465
|
-
setLinkUrl={setLinkUrl}
|
|
466
|
-
removeLink={removeLink}
|
|
467
|
-
onConfirm={(url) => {
|
|
468
|
-
// Dispatch TOGGLE_LINK_COMMAND with the URL directly
|
|
469
|
-
editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
|
|
470
|
-
url: url,
|
|
471
|
-
target: "_blank",
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
// Update state for modal and UI
|
|
476
|
-
setLinkUrl(url);
|
|
477
|
-
setLinkModalOpen(false);
|
|
478
|
-
}}
|
|
479
|
-
|
|
480
|
-
onCancel={() => {
|
|
481
|
-
setLinkUrl("")
|
|
482
|
-
// editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
|
|
483
|
-
setLinkModalOpen(false)
|
|
484
|
-
}}
|
|
485
|
-
/>}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
<ImagePlugin />
|
|
489
|
-
|
|
490
|
-
<VideoPlugin />
|
|
491
|
-
|
|
492
|
-
<ContentPreviewDialog value={value} />
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
{/* <YoutubePlugin /> */}
|
|
496
|
-
</div>
|
|
497
|
-
)
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
2
|
-
import { FaYoutube } from "react-icons/fa";
|
|
3
|
-
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
4
|
-
import { $insertNodes } from "lexical";
|
|
5
|
-
import { $createVideoNode } from "../nodes/VideoNode";
|
|
6
|
-
import RawDialog from "../components/common/Dialog";
|
|
7
|
-
|
|
8
|
-
export default function VideoPlugin() {
|
|
9
|
-
const [isOpen, setIsOpen] = useState(false);
|
|
10
|
-
const [showMessage, setShowMessage] = useState(false);
|
|
11
|
-
const [url, setURL] = useState("");
|
|
12
|
-
|
|
13
|
-
const [editor] = useLexicalComposerContext();
|
|
14
|
-
|
|
15
|
-
const onEmbed = () => {
|
|
16
|
-
if (!url) return;
|
|
17
|
-
|
|
18
|
-
let provider: "youtube" | "drive" | null = null;
|
|
19
|
-
let id: string | null = null;
|
|
20
|
-
|
|
21
|
-
// youtube
|
|
22
|
-
const ytMatch =
|
|
23
|
-
/^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/.exec(url);
|
|
24
|
-
|
|
25
|
-
if (ytMatch && ytMatch[2]?.length === 11) {
|
|
26
|
-
provider = "youtube";
|
|
27
|
-
id = ytMatch[2];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// drive
|
|
31
|
-
const driveMatch = /drive\.google\.com\/file\/d\/([a-zA-Z0-9_-]+)\//.exec(url);
|
|
32
|
-
if (driveMatch && driveMatch[1]) {
|
|
33
|
-
provider = "drive";
|
|
34
|
-
id = driveMatch[1];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!id || !provider) {
|
|
38
|
-
setShowMessage(true);
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
editor.update(() => {
|
|
43
|
-
const node = $createVideoNode({ id, provider });
|
|
44
|
-
$insertNodes([node]);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
setURL("");
|
|
48
|
-
setShowMessage(false);
|
|
49
|
-
setIsOpen(false);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
useEffect(() => {
|
|
53
|
-
if (!isOpen) {
|
|
54
|
-
setURL("");
|
|
55
|
-
setShowMessage(false);
|
|
56
|
-
}
|
|
57
|
-
}, [isOpen]);
|
|
58
|
-
|
|
59
|
-
return (
|
|
60
|
-
<div className="lexical-text-editor-dialog">
|
|
61
|
-
{/* Open dialog button */}
|
|
62
|
-
<button type="button"
|
|
63
|
-
className={`size-8 lexical-btn ${isOpen ? "toolbarActive" : "toolbar"} icon`}
|
|
64
|
-
onClick={() => setIsOpen(true)}
|
|
65
|
-
>
|
|
66
|
-
<FaYoutube size={18} color="red" />
|
|
67
|
-
</button>
|
|
68
|
-
|
|
69
|
-
{/* Dialog */}
|
|
70
|
-
<RawDialog open={isOpen} onClose={() => setIsOpen(false)}>
|
|
71
|
-
<div className="lte-dialog-header">Embed Video</div>
|
|
72
|
-
|
|
73
|
-
<div className="lte-dialog-body">
|
|
74
|
-
<input
|
|
75
|
-
className="lte-input"
|
|
76
|
-
placeholder="Add YouTube or Google Drive URL"
|
|
77
|
-
value={url}
|
|
78
|
-
onChange={(e) => setURL(e.target.value)}
|
|
79
|
-
/>
|
|
80
|
-
|
|
81
|
-
{showMessage && (
|
|
82
|
-
<p className="lte-error-text">
|
|
83
|
-
It's not a valid YouTube or Google Drive URL
|
|
84
|
-
</p>
|
|
85
|
-
)}
|
|
86
|
-
</div>
|
|
87
|
-
|
|
88
|
-
<div className="lte-dialog-footer">
|
|
89
|
-
<button type="button"
|
|
90
|
-
className="lte-primary-btn"
|
|
91
|
-
disabled={!url}
|
|
92
|
-
onClick={onEmbed}
|
|
93
|
-
>
|
|
94
|
-
Embed
|
|
95
|
-
</button>
|
|
96
|
-
</div>
|
|
97
|
-
</RawDialog>
|
|
98
|
-
</div>
|
|
99
|
-
);
|
|
100
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
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
|
-
}
|