Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/heading/editable.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 React from "react";
7
import { ReactEditor } from "../../slate-react";
8
import { register } from "../register";
9
import { useSlateStatic } from "../hooks";
10
import { HeadingToggle } from "./toggle";
11
import { mark_block } from "../../util";
12
13
register({
14
slateType: "heading",
15
16
Element: ({ attributes, children, element }) => {
17
const editor = useSlateStatic();
18
if (element.type != "heading") throw Error("bug");
19
const { level } = element;
20
if (!level || level < 1 || level > 6) {
21
// Shouldn't be allowed, but at least we can render it somehow...
22
return <b>{children}</b>;
23
}
24
let x;
25
if (ReactEditor.isUsingWindowing(editor)) {
26
x = [
27
<HeadingToggle element={element} key="toggle" />,
28
<span key="children">{children}</span>,
29
];
30
} else {
31
x = children;
32
}
33
return React.createElement(`h${level}`, attributes, x);
34
},
35
36
fromSlate: ({ node, children }) => {
37
let h = "\n#";
38
for (let n = 1; n < ((node.level as any) ?? 1); n++) {
39
h += "#";
40
}
41
return mark_block(children, h).trim() + "\n\n";
42
},
43
44
rules: { autoFocus: true, autoAdvance: false },
45
});
46
47