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