Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/search/search-control.tsx
1700 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 { delay } from "awaiting";
8
import { Button } from "antd";
9
import { Icon } from "@cocalc/frontend/components";
10
import { ReactEditor } from "../slate-react";
11
import { selectNextMatch, selectPreviousMatch } from "./find-matches";
12
13
interface Props {
14
decorate;
15
editor: ReactEditor;
16
disabled: boolean;
17
}
18
19
export const SearchControlButtons: React.FC<Props> = ({
20
decorate,
21
editor,
22
disabled,
23
}) => {
24
return (
25
<div style={{ height: "23px" }}>
26
<Button
27
shape="round"
28
type="text"
29
size="small"
30
disabled={disabled}
31
style={{ padding: "0 5px" }}
32
onClick={() => previousMatch(editor, decorate)}
33
>
34
<Icon name="chevron-up" />
35
</Button>{" "}
36
<Button
37
shape="round"
38
size="small"
39
type="text"
40
disabled={disabled}
41
style={{ padding: "0 5px" }}
42
onClick={() => nextMatch(editor, decorate)}
43
>
44
<Icon name="chevron-down" />
45
</Button>
46
</div>
47
);
48
};
49
50
export async function nextMatch(editor, decorate) {
51
const focused = ReactEditor.isFocused(editor);
52
if (!focused) {
53
ReactEditor.focus(editor);
54
await delay(0);
55
}
56
selectNextMatch(editor, decorate);
57
editor.scrollCaretIntoView();
58
await delay(100);
59
editor.scrollCaretIntoView();
60
}
61
62
export async function previousMatch(editor, decorate) {
63
const focused = ReactEditor.isFocused(editor);
64
if (!focused) {
65
ReactEditor.focus(editor);
66
await delay(0);
67
}
68
selectPreviousMatch(editor, decorate);
69
editor.scrollCaretIntoView();
70
await delay(100);
71
editor.scrollCaretIntoView();
72
}
73
74