Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/edit-bar/link-edit.tsx
1697 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import React from "react";
7
import { useState } from "@cocalc/frontend/app-framework";
8
import { Icon } from "@cocalc/frontend/components";
9
import { Editor } from "slate";
10
import { open_new_tab } from "@cocalc/frontend/misc";
11
import { Input } from "antd";
12
import { setLinkURL } from "./link-url";
13
14
interface Props {
15
linkURL: string | undefined;
16
editor: Editor;
17
}
18
19
export const LinkEdit: React.FC<Props> = ({ linkURL, editor }) => {
20
const [edit, setEdit] = useState<boolean>(false);
21
const [saveValue, setSaveValue] = useState<string>("");
22
23
const icon = (
24
<a
25
onClick={() => (linkURL ? open_new_tab(linkURL) : undefined)}
26
style={{ marginRight: "5px" }}
27
>
28
<Icon name="link" />
29
</a>
30
);
31
if (linkURL == null) {
32
return <></>;
33
}
34
if (edit) {
35
return (
36
<Input
37
autoFocus
38
style={{
39
width: "100%",
40
maxWidth: "50ex",
41
}}
42
addonBefore={icon}
43
addonAfter={
44
saveValue == linkURL ? (
45
<Icon name={"check"} style={{ width: "2em", color: "#5cb85c" }} />
46
) : (
47
<span style={{ width: "2em" }}>...</span>
48
)
49
}
50
size="small"
51
placeholder="Link target..."
52
defaultValue={linkURL}
53
onChange={(e) => {
54
setLinkURL(editor, e.target.value);
55
setSaveValue(e.target.value);
56
}}
57
onBlur={() => setEdit(false)}
58
/>
59
);
60
} else {
61
return (
62
<div
63
onClick={() => setEdit(true)}
64
style={{
65
whiteSpace: "nowrap",
66
overflow: "hidden",
67
textOverflow: "ellipsis",
68
padding: "0 5px",
69
margin: "0 5px",
70
borderLeft: "1px solid lightgray",
71
borderRight: "1px solid lightgray",
72
maxWidth: "50ex",
73
opacity: !linkURL ? "0.5" : undefined,
74
color: "#666",
75
}}
76
>
77
{icon} {linkURL ? linkURL : "Link target..."}
78
</div>
79
);
80
}
81
};
82
83