Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/code-block/insert-bar.tsx
6057 views
1
import { Button, Space } from "antd";
2
import { Node, Path, Transforms } from "slate";
3
4
import { Icon } from "@cocalc/frontend/components/icon";
5
import { COLORS } from "@cocalc/util/theme";
6
7
import { findElement } from "../../control";
8
import { ReactEditor } from "../../slate-react";
9
import { toDisplayMath } from "../math/index";
10
import { toSlate } from "./index";
11
12
function InsertButton({ children, onClick }) {
13
return (
14
<Button
15
style={{ color: COLORS.GRAY_M }}
16
size="small"
17
onClick={(e) => {
18
e.stopPropagation(); // keep the editor with the insert bar itself from getting selected
19
e.preventDefault();
20
onClick(e);
21
}}
22
>
23
{children}
24
</Button>
25
);
26
}
27
28
export default function InsertBar({ editor, element, info, above }) {
29
//const { hasLanguageModel } = useFileContext();
30
31
const insert = (node: Node, offset = 0) => {
32
let path = findElement(editor, element);
33
if (path && !above) {
34
path = Path.next(path);
35
}
36
Transforms.insertNodes(editor, node, { at: path });
37
ReactEditor.focus(editor, true, true);
38
if (path) {
39
setTimeout(() => {
40
const sel = {
41
anchor: { path: path!, offset: 0 },
42
focus: { path: path!, offset },
43
};
44
Transforms.setSelection(editor, sel);
45
ReactEditor.focus(editor, true, true);
46
}, 50);
47
}
48
};
49
50
return (
51
<div
52
className="cocalc-slate-insert-cell"
53
style={{
54
height: "28px",
55
cursor: "pointer",
56
paddingTop: "8px",
57
}}
58
>
59
<div className="cocalc-slate-insert-cell-controls">
60
<Space.Compact>
61
<InsertButton
62
onClick={() => {
63
insert(toSlate({ token: { content: "", info, type: "fence" } }));
64
}}
65
>
66
<Icon name="code" /> Code
67
</InsertButton>
68
<InsertButton
69
onClick={() => {
70
insert(
71
{
72
type: "paragraph",
73
children: [{ text: "Text" }],
74
},
75
"Text".length,
76
);
77
}}
78
>
79
<Icon name="pen" /> Text
80
</InsertButton>
81
<InsertButton
82
onClick={() => {
83
insert(toDisplayMath({ token: { content: "x" } }));
84
}}
85
>
86
<Icon name="superscript" /> Math
87
</InsertButton>
88
{/* {hasLanguageModel ? (
89
<InsertButton
90
onClick={() => {
91
console.log("TODO!");
92
}}
93
>
94
<OpenAIAvatar
95
size={16}
96
style={{ marginRight: "5px" }}
97
innerStyle={{ top: "1.5px" }}
98
/>{" "}
99
ChatGPT...
100
</InsertButton>
101
) : undefined}<InsertButton>
102
<Icon name="paste" /> Paste
103
</InsertButton>
104
*/}
105
</Space.Compact>
106
</div>
107
</div>
108
);
109
}
110
111