-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
LexicalLinkPlugin.ts
105 lines (100 loc) · 3.04 KB
/
LexicalLinkPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import {
$toggleLink,
LinkAttributes,
LinkNode,
TOGGLE_LINK_COMMAND,
} from '@lexical/link';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {mergeRegister, objectKlassEquals} from '@lexical/utils';
import {
$getSelection,
$isElementNode,
$isRangeSelection,
COMMAND_PRIORITY_LOW,
PASTE_COMMAND,
} from 'lexical';
import {useEffect} from 'react';
type Props = {
validateUrl?: (url: string) => boolean;
attributes?: LinkAttributes;
};
export function LinkPlugin({validateUrl, attributes}: Props): null {
const [editor] = useLexicalComposerContext();
useEffect(() => {
if (!editor.hasNodes([LinkNode])) {
throw new Error('LinkPlugin: LinkNode not registered on editor');
}
return mergeRegister(
editor.registerCommand(
TOGGLE_LINK_COMMAND,
(payload) => {
if (payload === null) {
$toggleLink(payload);
return true;
} else if (typeof payload === 'string') {
if (validateUrl === undefined || validateUrl(payload)) {
$toggleLink(payload, attributes);
return true;
}
return false;
} else {
const {url, target, rel, title} = payload;
$toggleLink(url, {
...attributes,
rel,
target,
title,
});
return true;
}
},
COMMAND_PRIORITY_LOW,
),
validateUrl !== undefined
? editor.registerCommand(
PASTE_COMMAND,
(event) => {
const selection = $getSelection();
if (
!$isRangeSelection(selection) ||
selection.isCollapsed() ||
!objectKlassEquals(event, ClipboardEvent)
) {
return false;
}
const clipboardEvent = event as ClipboardEvent;
if (clipboardEvent.clipboardData === null) {
return false;
}
const clipboardText =
clipboardEvent.clipboardData.getData('text');
if (!validateUrl(clipboardText)) {
return false;
}
// If we select nodes that are elements then avoid applying the link.
if (!selection.getNodes().some((node) => $isElementNode(node))) {
editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
...attributes,
url: clipboardText,
});
event.preventDefault();
return true;
}
return false;
},
COMMAND_PRIORITY_LOW,
)
: () => {
// Don't paste arbitrary text as a link when there's no validate function
},
);
}, [editor, validateUrl, attributes]);
return null;
}