Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/leaf.tsx
1691 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, { CSSProperties } from "react";
7
import type { RenderLeafProps } from "./slate-react";
8
import { startswith } from "@cocalc/util/misc";
9
10
// CODE_STYLE -- copied from GitHub
11
const CODE_STYLE = {
12
padding: ".2em .4em",
13
margin: 0,
14
fontSize: "85%",
15
borderRadius: "6px",
16
} as CSSProperties;
17
18
const Leaf: React.FC<RenderLeafProps> = React.memo(
19
({ attributes, children, leaf }) => {
20
if (leaf.bold) {
21
children = <strong>{children}</strong>;
22
}
23
if (leaf.italic) {
24
children = <em>{children}</em>;
25
}
26
if (leaf.strikethrough) {
27
children = <s>{children}</s>;
28
}
29
if (leaf.underline) {
30
children = <u>{children}</u>;
31
}
32
if (leaf.sup) {
33
children = <sup>{children}</sup>;
34
}
35
if (leaf.sub) {
36
children = <sub>{children}</sub>;
37
}
38
if (leaf.code) {
39
children = <code style={CODE_STYLE}>{children}</code>;
40
}
41
if (leaf.small) {
42
children = <small>{children}</small>;
43
}
44
if (leaf.tt) {
45
children = <span style={{ fontFamily: "monospace" }}>{children}</span>;
46
}
47
// check for colors, fonts, etc.
48
for (const mark in leaf) {
49
if (!leaf[mark]) continue; // only if it is true
50
if (startswith(mark, "color:")) {
51
children = (
52
<span style={{ color: mark.split(":")[1] }}>{children}</span>
53
);
54
}
55
if (startswith(mark, "font-family:")) {
56
children = (
57
<span style={{ fontFamily: mark.split(":")[1] }}>{children}</span>
58
);
59
}
60
if (startswith(mark, "font-size:")) {
61
children = (
62
<span style={{ fontSize: mark.split(":")[1] }}>{children}</span>
63
);
64
}
65
}
66
if (leaf.search) {
67
// Search highlighting of text nodes.
68
// Same color as CodeMirror's default.
69
children = <span style={{ backgroundColor: "#ffa" }}>{children}</span>;
70
}
71
72
return <span {...attributes}>{children}</span>;
73
}
74
);
75
76
export default Leaf;
77
78