@haklex/rich-plugin-link-edit 0.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.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Innei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
24
+ Additional Terms and Conditions
25
+
26
+ ----------------
27
+
28
+ Use of this software is governed by the terms of MIT and, in addition, by the terms and conditions described in the additional file (ADDITIONAL_TERMS.md). By using this software, you agree to abide by these additional terms and conditions.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # @haklex/rich-plugin-link-edit
2
+
3
+ 链接编辑插件。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ pnpm add @haklex/rich-plugin-link-edit @haklex/rich-editor
9
+ ```
10
+
11
+ ## 导出
12
+
13
+ ```ts
14
+ export { FloatingLinkEditorPlugin } from './FloatingLinkEditorPlugin'
15
+ ```
16
+
17
+ ## 使用
18
+
19
+ ```tsx
20
+ import { FloatingLinkEditorPlugin } from '@haklex/rich-plugin-link-edit'
21
+ import { RichEditor } from '@haklex/rich-editor'
22
+
23
+ <RichEditor>
24
+ <FloatingLinkEditorPlugin />
25
+ </RichEditor>
26
+ ```
27
+
28
+ ## License
29
+
30
+ MIT
@@ -0,0 +1,3 @@
1
+ import { ReactElement } from 'react';
2
+ export declare function FloatingLinkEditorPlugin(): ReactElement | null;
3
+ //# sourceMappingURL=FloatingLinkEditorPlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FloatingLinkEditorPlugin.d.ts","sourceRoot":"","sources":["../src/FloatingLinkEditorPlugin.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAA;AAoBrB,OAAO,KAAK,EAAiB,YAAY,EAAE,MAAM,OAAO,CAAA;AAkBxD,wBAAgB,wBAAwB,IAAI,YAAY,GAAG,IAAI,CAqN9D"}
@@ -0,0 +1,2 @@
1
+ export { FloatingLinkEditorPlugin } from './FloatingLinkEditorPlugin';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,222 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { Popover, PopoverPortal, PopoverPositioner, PopoverPopup } from "@haklex/rich-editor-ui";
3
+ import { $isLinkNode } from "@lexical/link";
4
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
5
+ import { $getSelection, $isRangeSelection, SELECTION_CHANGE_COMMAND, COMMAND_PRIORITY_LOW, $getNodeByKey } from "lexical";
6
+ import { Link, ExternalLink, Unlink } from "lucide-react";
7
+ import { useRef, useState, useCallback, useEffect } from "react";
8
+ function $findLinkNode(node) {
9
+ let current = node;
10
+ while (current) {
11
+ if ($isLinkNode(current)) return current;
12
+ current = current.getParent();
13
+ }
14
+ return null;
15
+ }
16
+ function FloatingLinkEditorPlugin() {
17
+ const [editor] = useLexicalComposerContext();
18
+ const inputRef = useRef(null);
19
+ const panelFocusedRef = useRef(false);
20
+ const [visible, setVisible] = useState(false);
21
+ const [linkState, setLinkState] = useState({
22
+ url: "",
23
+ linkKey: "",
24
+ linkDom: null
25
+ });
26
+ const [inputUrl, setInputUrl] = useState("");
27
+ const updateLink = useCallback(() => {
28
+ if (panelFocusedRef.current) return;
29
+ const selection = $getSelection();
30
+ if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
31
+ setVisible(false);
32
+ return;
33
+ }
34
+ const anchorNode = selection.anchor.getNode();
35
+ const linkNode = $findLinkNode(anchorNode);
36
+ if (!linkNode) {
37
+ setVisible(false);
38
+ return;
39
+ }
40
+ const url = linkNode.getURL();
41
+ const key = linkNode.getKey();
42
+ const dom = editor.getElementByKey(key);
43
+ setLinkState({ url, linkKey: key, linkDom: dom });
44
+ setInputUrl(url);
45
+ setVisible(true);
46
+ }, [editor]);
47
+ useEffect(() => {
48
+ const unregisterCommand = editor.registerCommand(
49
+ SELECTION_CHANGE_COMMAND,
50
+ () => {
51
+ updateLink();
52
+ return false;
53
+ },
54
+ COMMAND_PRIORITY_LOW
55
+ );
56
+ const unregisterUpdate = editor.registerUpdateListener(
57
+ ({ editorState }) => {
58
+ editorState.read(() => {
59
+ updateLink();
60
+ });
61
+ }
62
+ );
63
+ return () => {
64
+ unregisterCommand();
65
+ unregisterUpdate();
66
+ };
67
+ }, [editor, updateLink]);
68
+ useEffect(() => {
69
+ const root = editor.getRootElement();
70
+ if (!root) return;
71
+ const CSS_CLASS = "rich-link-edit-cmd-hover";
72
+ const updateCursor = (mod) => {
73
+ root.classList.toggle(CSS_CLASS, mod);
74
+ };
75
+ const onKeyDown = (e) => {
76
+ if (e.key === "Meta" || e.key === "Control") updateCursor(true);
77
+ };
78
+ const onKeyUp = (e) => {
79
+ if (e.key === "Meta" || e.key === "Control") updateCursor(false);
80
+ };
81
+ const onBlur = () => updateCursor(false);
82
+ const onClick = (e) => {
83
+ if (!(e.metaKey || e.ctrlKey)) return;
84
+ const anchor = e.target.closest("a");
85
+ if (!anchor) return;
86
+ const href = anchor.getAttribute("href");
87
+ if (href) {
88
+ e.preventDefault();
89
+ window.open(href, "_blank", "noopener,noreferrer");
90
+ }
91
+ };
92
+ root.addEventListener("keydown", onKeyDown);
93
+ root.addEventListener("keyup", onKeyUp);
94
+ root.addEventListener("blur", onBlur);
95
+ root.addEventListener("click", onClick);
96
+ return () => {
97
+ root.removeEventListener("keydown", onKeyDown);
98
+ root.removeEventListener("keyup", onKeyUp);
99
+ root.removeEventListener("blur", onBlur);
100
+ root.removeEventListener("click", onClick);
101
+ root.classList.remove(CSS_CLASS);
102
+ };
103
+ }, [editor]);
104
+ const commitUrl = useCallback(() => {
105
+ const trimmed = inputUrl.trim();
106
+ if (!trimmed) return;
107
+ editor.update(() => {
108
+ const node = $getNodeByKey(linkState.linkKey);
109
+ if ($isLinkNode(node) && node.getURL() !== trimmed) {
110
+ node.setURL(trimmed);
111
+ }
112
+ });
113
+ }, [editor, inputUrl, linkState.linkKey]);
114
+ const handleKeyDown = useCallback(
115
+ (e) => {
116
+ if (e.key === "Enter") {
117
+ e.preventDefault();
118
+ commitUrl();
119
+ editor.focus();
120
+ } else if (e.key === "Escape") {
121
+ e.preventDefault();
122
+ setInputUrl(linkState.url);
123
+ editor.focus();
124
+ }
125
+ },
126
+ [commitUrl, editor, linkState.url]
127
+ );
128
+ const handleOpen = useCallback(() => {
129
+ window.open(linkState.url, "_blank", "noopener,noreferrer");
130
+ }, [linkState.url]);
131
+ const handleUnlink = useCallback(() => {
132
+ editor.update(() => {
133
+ const node = $getNodeByKey(linkState.linkKey);
134
+ if ($isLinkNode(node)) {
135
+ const children = node.getChildren();
136
+ for (const child of children) {
137
+ node.insertBefore(child);
138
+ }
139
+ node.remove();
140
+ }
141
+ });
142
+ setVisible(false);
143
+ }, [editor, linkState.linkKey]);
144
+ const handlePanelFocusIn = useCallback(() => {
145
+ panelFocusedRef.current = true;
146
+ }, []);
147
+ const handlePanelFocusOut = useCallback(
148
+ (e) => {
149
+ const panel = e.currentTarget;
150
+ if (!panel.contains(e.relatedTarget)) {
151
+ panelFocusedRef.current = false;
152
+ commitUrl();
153
+ editor.focus();
154
+ }
155
+ },
156
+ [commitUrl, editor]
157
+ );
158
+ if (!editor.isEditable()) return null;
159
+ return /* @__PURE__ */ jsx(Popover, { open: visible, children: /* @__PURE__ */ jsx(PopoverPortal, { children: /* @__PURE__ */ jsx(
160
+ PopoverPositioner,
161
+ {
162
+ anchor: linkState.linkDom,
163
+ side: "bottom",
164
+ sideOffset: 8,
165
+ align: "center",
166
+ children: /* @__PURE__ */ jsxs(
167
+ PopoverPopup,
168
+ {
169
+ className: "rich-link-edit-panel",
170
+ onFocus: handlePanelFocusIn,
171
+ onBlur: handlePanelFocusOut,
172
+ children: [
173
+ /* @__PURE__ */ jsxs("div", { className: "rich-link-edit-url-row", children: [
174
+ /* @__PURE__ */ jsx(Link, { className: "rich-link-edit-link-icon", size: 16 }),
175
+ /* @__PURE__ */ jsx(
176
+ "input",
177
+ {
178
+ ref: inputRef,
179
+ className: "rich-link-edit-input",
180
+ type: "url",
181
+ value: inputUrl,
182
+ onChange: (e) => setInputUrl(e.target.value),
183
+ onKeyDown: handleKeyDown,
184
+ placeholder: "https://..."
185
+ }
186
+ )
187
+ ] }),
188
+ /* @__PURE__ */ jsxs("div", { className: "rich-link-edit-actions", children: [
189
+ /* @__PURE__ */ jsxs(
190
+ "button",
191
+ {
192
+ className: "rich-link-edit-action-btn",
193
+ type: "button",
194
+ onClick: handleOpen,
195
+ children: [
196
+ /* @__PURE__ */ jsx(ExternalLink, { size: 14 }),
197
+ "Open"
198
+ ]
199
+ }
200
+ ),
201
+ /* @__PURE__ */ jsxs(
202
+ "button",
203
+ {
204
+ className: "rich-link-edit-action-btn rich-link-edit-action-btn--end",
205
+ type: "button",
206
+ onClick: handleUnlink,
207
+ children: [
208
+ /* @__PURE__ */ jsx(Unlink, { size: 14 }),
209
+ "Unlink"
210
+ ]
211
+ }
212
+ )
213
+ ] })
214
+ ]
215
+ }
216
+ )
217
+ }
218
+ ) }) });
219
+ }
220
+ export {
221
+ FloatingLinkEditorPlugin
222
+ };
@@ -0,0 +1,67 @@
1
+ .rich-link-edit-panel {
2
+ display: flex;
3
+ flex-direction: column;
4
+ gap: 8px;
5
+ width: 340px;
6
+ padding: 12px;
7
+ font-size: 13px;
8
+ }
9
+ .rich-link-edit-url-row {
10
+ display: flex;
11
+ align-items: center;
12
+ gap: 8px;
13
+ padding: 8px 12px;
14
+ background-color: var(--rc-bg-secondary);
15
+ border-radius: var(--rc-radius-md);
16
+ min-width: 0;
17
+ }
18
+ .rich-link-edit-link-icon {
19
+ flex-shrink: 0;
20
+ color: var(--rc-text-secondary);
21
+ }
22
+ .rich-link-edit-input {
23
+ flex: 1;
24
+ appearance: none;
25
+ border: none;
26
+ background-color: transparent;
27
+ color: var(--rc-text);
28
+ font-family: var(--rc-font-mono);
29
+ font-size: 13px;
30
+ padding: 0;
31
+ outline: none;
32
+ min-width: 0;
33
+ }
34
+ .rich-link-edit-input::placeholder {
35
+ color: var(--rc-text-secondary);
36
+ }
37
+ .rich-link-edit-actions {
38
+ display: flex;
39
+ align-items: center;
40
+ gap: 4px;
41
+ }
42
+ .rich-link-edit-action-btn {
43
+ display: inline-flex;
44
+ align-items: center;
45
+ gap: 6px;
46
+ appearance: none;
47
+ border: none;
48
+ background: none;
49
+ color: var(--rc-text);
50
+ font-size: 13px;
51
+ font-weight: 500;
52
+ cursor: pointer;
53
+ padding: 4px 8px;
54
+ border-radius: var(--rc-radius-sm);
55
+ transition: color 0.15s ease, background-color 0.15s ease;
56
+ white-space: nowrap;
57
+ }
58
+ .rich-link-edit-action-btn:hover {
59
+ background-color: var(--rc-bg-secondary);
60
+ }
61
+ .rich-link-edit-action-btn--end {
62
+ margin-left: auto;
63
+ }
64
+ .rich-link-edit-cmd-hover a {
65
+ cursor: pointer;
66
+ text-decoration-line: underline;
67
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=styles.css.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles.css.d.ts","sourceRoot":"","sources":["../src/styles.css.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@haklex/rich-plugin-link-edit",
3
+ "version": "0.0.1",
4
+ "description": "Link editing plugin",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts"
11
+ },
12
+ "./style.css": "./dist/rich-plugin-link-edit.css"
13
+ },
14
+ "main": "./dist/index.mjs",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@haklex/rich-editor-ui": "0.0.1"
20
+ },
21
+ "devDependencies": {
22
+ "@lexical/link": "^0.39.0",
23
+ "@lexical/react": "^0.39.0",
24
+ "@types/react": "^19.0.0",
25
+ "@types/react-dom": "^19.0.0",
26
+ "@vanilla-extract/css": "^1.17.1",
27
+ "@vanilla-extract/vite-plugin": "^4.0.20",
28
+ "lexical": "^0.39.0",
29
+ "lucide-react": "^0.574.0",
30
+ "react": "19.2.4",
31
+ "react-dom": "19.2.4",
32
+ "typescript": "^5.9.0",
33
+ "vite": "^7.3.1",
34
+ "vite-plugin-dts": "^4.5.0",
35
+ "@haklex/rich-style-token": "0.0.1"
36
+ },
37
+ "peerDependencies": {
38
+ "@lexical/link": "^0.39.0",
39
+ "@lexical/react": "^0.39.0",
40
+ "lexical": "^0.39.0",
41
+ "lucide-react": "^0.574.0",
42
+ "react": ">=19",
43
+ "react-dom": ">=19",
44
+ "@haklex/rich-style-token": "0.0.1"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "scripts": {
50
+ "build": "vite build",
51
+ "dev:build": "vite build --watch"
52
+ },
53
+ "types": "./dist/index.d.ts"
54
+ }