Path: blob/master/src/packages/frontend/editors/slate/elements/code-block/insert-bar.tsx
6057 views
import { Button, Space } from "antd";1import { Node, Path, Transforms } from "slate";23import { Icon } from "@cocalc/frontend/components/icon";4import { COLORS } from "@cocalc/util/theme";56import { findElement } from "../../control";7import { ReactEditor } from "../../slate-react";8import { toDisplayMath } from "../math/index";9import { toSlate } from "./index";1011function InsertButton({ children, onClick }) {12return (13<Button14style={{ color: COLORS.GRAY_M }}15size="small"16onClick={(e) => {17e.stopPropagation(); // keep the editor with the insert bar itself from getting selected18e.preventDefault();19onClick(e);20}}21>22{children}23</Button>24);25}2627export default function InsertBar({ editor, element, info, above }) {28//const { hasLanguageModel } = useFileContext();2930const insert = (node: Node, offset = 0) => {31let path = findElement(editor, element);32if (path && !above) {33path = Path.next(path);34}35Transforms.insertNodes(editor, node, { at: path });36ReactEditor.focus(editor, true, true);37if (path) {38setTimeout(() => {39const sel = {40anchor: { path: path!, offset: 0 },41focus: { path: path!, offset },42};43Transforms.setSelection(editor, sel);44ReactEditor.focus(editor, true, true);45}, 50);46}47};4849return (50<div51className="cocalc-slate-insert-cell"52style={{53height: "28px",54cursor: "pointer",55paddingTop: "8px",56}}57>58<div className="cocalc-slate-insert-cell-controls">59<Space.Compact>60<InsertButton61onClick={() => {62insert(toSlate({ token: { content: "", info, type: "fence" } }));63}}64>65<Icon name="code" /> Code66</InsertButton>67<InsertButton68onClick={() => {69insert(70{71type: "paragraph",72children: [{ text: "Text" }],73},74"Text".length,75);76}}77>78<Icon name="pen" /> Text79</InsertButton>80<InsertButton81onClick={() => {82insert(toDisplayMath({ token: { content: "x" } }));83}}84>85<Icon name="superscript" /> Math86</InsertButton>87{/* {hasLanguageModel ? (88<InsertButton89onClick={() => {90console.log("TODO!");91}}92>93<OpenAIAvatar94size={16}95style={{ marginRight: "5px" }}96innerStyle={{ top: "1.5px" }}97/>{" "}98ChatGPT...99</InsertButton>100) : undefined}<InsertButton>101<Icon name="paste" /> Paste102</InsertButton>103*/}104</Space.Compact>105</div>106</div>107);108}109110111