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