Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/search/replace.tsx
1698 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 { Button, Input, Popconfirm } from "antd";
7
import React from "react";
8
const { useRef, useState } = React;
9
import { replaceAll, replaceOne } from "./replace-matches";
10
import { ReactEditor } from "../slate-react";
11
12
interface Props {
13
editor: ReactEditor;
14
decorate;
15
search: string;
16
cancel: () => void;
17
}
18
19
export const Replace: React.FC<Props> = ({ cancel, editor, search, decorate }) => {
20
const [replace, setReplace] = useState<string>("");
21
const inputRef = useRef<any>(null);
22
return (
23
<div style={{ display: "flex" }}>
24
<Input
25
ref={inputRef}
26
placeholder="Replace..."
27
value={replace}
28
onChange={(e) => setReplace(e.target.value)}
29
allowClear={true}
30
size="small"
31
style={{ border: 0, flex: 1 }}
32
onKeyDown={async (event) => {
33
if (event.key == "Escape") {
34
event.preventDefault();
35
cancel();
36
return;
37
}
38
}}
39
/>
40
{replace.trim() && (
41
<>
42
<Button
43
size="small"
44
onClick={() => replaceOne(editor, decorate, replace)}
45
>
46
One
47
</Button>{" "}
48
<Popconfirm
49
title={`Replace all instances of '${search}' by '${replace}' across the entire document?`}
50
onConfirm={() => replaceAll(editor, decorate, replace)}
51
okText={"Yes, replace all"}
52
cancelText={"Cancel"}
53
>
54
<Button size="small">All</Button>
55
</Popconfirm>
56
</>
57
)}
58
</div>
59
);
60
};
61
62