-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathlink_embed.tsx
More file actions
80 lines (74 loc) · 2.93 KB
/
link_embed.tsx
File metadata and controls
80 lines (74 loc) · 2.93 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
import { useRef, useState } from "preact/hooks";
import linkEmbedService from "../../services/link_embed";
import type { LinkPasteMode } from "../../services/link_embed";
import FormGroup from "../react/FormGroup";
import Modal from "../react/Modal";
import Button from "../react/Button";
import { useTriliumEvent } from "../react/hooks";
import type { CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog";
export interface LinkEmbedOpts {
editorApi: CKEditorApi;
}
export default function LinkEmbedDialog() {
const editorApiRef = useRef<CKEditorApi>(null);
const [url, setUrl] = useState("");
const [mode, setMode] = useState<LinkPasteMode>("embed");
const [shown, setShown] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
useTriliumEvent("showLinkEmbedDialog", ({ editorApi }) => {
editorApiRef.current = editorApi;
setUrl("");
setMode("embed");
setShown(true);
});
const handleSubmit = () => {
const trimmedUrl = url.trim();
if (!trimmedUrl || !editorApiRef.current) return;
if (mode === "mention") {
editorApiRef.current.addLinkMention(trimmedUrl);
} else if (mode === "url") {
editorApiRef.current.addLinkToEditor(trimmedUrl, trimmedUrl);
} else {
const embedType = linkEmbedService.detectEmbedType(trimmedUrl);
editorApiRef.current.addLinkEmbed(trimmedUrl, embedType);
}
setShown(false);
};
return (
<Modal
className="link-embed-dialog"
title="Insert link"
size="lg"
onShown={() => inputRef.current?.focus()}
onHidden={() => setShown(false)}
onSubmit={handleSubmit}
footer={<Button text="Insert" keyboardShortcut="Enter" />}
show={shown}
>
<FormGroup name="url" label="URL">
<input
ref={inputRef}
type="url"
className="form-control"
placeholder="https://example.com or https://youtube.com/watch?v=..."
value={url}
onInput={(e) => setUrl((e.target as HTMLInputElement).value)}
/>
</FormGroup>
<FormGroup name="mode" label="Paste as">
<div className="btn-group w-100">
{(["mention", "url", "embed"] as const).map((m) => (
<button
key={m}
type="button"
className={`btn btn-sm ${mode === m ? "btn-primary" : "btn-outline-secondary"}`}
onClick={() => setMode(m)}
>
{m === "mention" ? "@ Mention" : m === "url" ? "URL" : "Embed"}
</button>
))}
</div>
</FormGroup>
</Modal>
);
}