Repository navigation
Expand file tree
/
Copy pathdocument-link-control.tsx
More file actions
139 lines (139 loc) · 4.47 KB
/
Copy pathdocument-link-control.tsx
File metadata and controls
139 lines (139 loc) · 4.47 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { useEffect, useRef, useState } from "react";
import { Link2, Unlink } from "lucide-react";
import { Editor, Range, Transforms, type RangeRef } from "slate";
import type { RichTextEditorHandle } from "@smartdoca/slate";
import { workspaceDocumentId } from "@web/features/documents/internal-document-id.js";
import { api, type Detail } from "@web/shared/api.js";
export function DocumentLinkControl({
handle,
disabled,
}: {
handle: RichTextEditorHandle | null;
disabled: boolean;
}) {
const range = useRef<RangeRef | null>(null),
menu = useRef<HTMLDetailsElement>(null);
const [url, setUrl] = useState(""),
[error, setError] = useState("");
useEffect(
() => () => {
range.current?.unref();
},
[],
);
return (
<details
ref={menu}
className="menu"
onToggle={(e) => {
if (!e.currentTarget.open) {
range.current?.unref();
range.current = null;
}
}}
>
<summary
aria-label="设置超链接"
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
e.preventDefault();
if (!menu.current || disabled) return;
if (!menu.current.open) {
range.current?.unref();
range.current = handle?.editor.selection
? Editor.rangeRef(handle.editor, handle.editor.selection)
: null;
const link =
handle &&
Editor.above(handle.editor, {
match: (n) => "type" in n && n.type === "link",
});
setUrl(link && "url" in link[0] ? String(link[0].url) : "");
}
menu.current.open = !menu.current.open;
}}
>
<Link2 size={16} />
</summary>
<form
className="toolbar-link-form"
onClick={(e) => e.stopPropagation()}
onMouseDown={(e) => e.stopPropagation()}
onSubmit={async (e) => {
e.preventDefault();
if (!handle || disabled) return;
const href = url.trim(),
id = workspaceDocumentId(href);
if (!id && !/^(https?:\/\/|mailto:)[^\s]+$/i.test(href)) {
setError("请输入 HTTP(S)、邮箱或本站文档链接");
return;
}
let label: string | undefined;
if (id) {
try {
const d = await api<Detail>(`/resources/${id}`);
if (d.resource.kind !== "document")
throw new Error("请选择文档链接");
label = d.resource.title;
} catch (e) {
setError((e as Error).message);
return;
}
}
if (!range.current?.current) {
setError("选区已变化,请重新选择插入位置");
return;
}
Transforms.select(handle.editor, range.current.current);
if (id) {
Editor.withoutNormalizing(handle.editor, () => {
if (
handle.editor.selection &&
Range.isExpanded(handle.editor.selection)
)
Transforms.delete(handle.editor);
Transforms.insertNodes(handle.editor, {
id: crypto.randomUUID(),
type: "custom:document-reference",
documentId: id,
label: label ?? "文档链接",
children: [{ text: "" }],
});
Transforms.move(handle.editor);
});
} else handle.commands.insertLink(href, label);
range.current?.unref();
range.current = null;
if (menu.current) menu.current.open = false;
setError("");
setUrl("");
}}
>
<input
aria-label="超链接地址"
placeholder="输入链接地址"
value={url}
onChange={(e) => setUrl(e.target.value)}
/>
<button className="primary" disabled={disabled}>
应用
</button>
<button
type="button"
disabled={disabled}
title="移除超链接"
aria-label="移除超链接"
onClick={() => {
if (range.current?.current && handle)
Transforms.select(handle.editor, range.current.current);
handle?.commands.removeLink();
if (menu.current) menu.current.open = false;
}}
>
<Unlink size={16} />
</button>
{error && <small role="alert">{error}</small>}
</form>
</details>
);
}