Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/edit-bar/marks-bar.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 { Button, Tooltip } from "antd";
7
import React, { CSSProperties } from "react";
8
9
import { Icon, IconName } from "@cocalc/frontend/components";
10
import { COLORS } from "@cocalc/util/theme";
11
import { SlateEditor } from "../editable-markdown";
12
import { formatAction } from "../format";
13
import ColorButton from "./color-button";
14
import FontFamily from "./font-family";
15
import FontSize from "./font-size";
16
import Heading from "./heading";
17
import Insert from "./insert";
18
import CodeButton from "./code";
19
import LinkButton from "./link";
20
import { Marks } from "./marks";
21
22
export const BUTTON_STYLE = {
23
color: COLORS.GRAY_M,
24
height: "24px",
25
borderLeft: "1px solid lightgray",
26
borderRight: "1px solid lightgray",
27
borderTop: "none",
28
borderBottom: "none",
29
padding: "0 10px",
30
} as CSSProperties;
31
32
interface MarkButtonProps {
33
mark: IconName;
34
active: boolean;
35
editor: SlateEditor;
36
}
37
38
const MarkButton: React.FC<MarkButtonProps> = ({ mark, active, editor }) => {
39
return (
40
<Tooltip title={TITLES[mark]} mouseEnterDelay={1}>
41
<Button
42
type="text"
43
style={{
44
...BUTTON_STYLE,
45
backgroundColor: active ? "#ccc" : undefined,
46
}}
47
onClick={() => formatAction(editor, mark, [])}
48
>
49
<Icon name={mark} />
50
</Button>
51
</Tooltip>
52
);
53
};
54
55
interface MarksBarProps {
56
marks: Marks;
57
editor: SlateEditor;
58
}
59
60
const MARKS: IconName[] = [
61
"bold",
62
"italic",
63
"underline",
64
"strikethrough",
65
"code",
66
/*"sup",
67
"sub",*/
68
];
69
70
const TITLES = {
71
bold: "Bold (shortcut: **foo**␣)",
72
italic: "Italics (shortcut: *foo*␣)",
73
underline: "Underline (shortcut: _foo_␣)",
74
strikethrough: "Strikethrough (shortcut: ~foo~␣)",
75
code: "Code (shortcut: `foo`␣)",
76
sup: "Superscript",
77
sub: "Subscript",
78
};
79
80
export const MarksBar: React.FC<MarksBarProps> = (props: MarksBarProps) => {
81
const { marks, editor } = props;
82
const v: React.JSX.Element[] = [];
83
v.push(<Insert key="insert" editor={editor} />);
84
for (const mark of MARKS) {
85
v.push(
86
<MarkButton
87
key={mark}
88
mark={mark}
89
active={marks[mark] ?? false}
90
editor={editor}
91
/>
92
);
93
}
94
v.push(<CodeButton key={"code2"} editor={editor} />);
95
v.push(<LinkButton key={"link"} editor={editor} />);
96
v.push(<FontSize key={"size"} editor={editor} size={getSizeMark(marks)} />);
97
v.push(<Heading key="heading" editor={editor} />);
98
v.push(
99
<ColorButton key={"color"} editor={editor} color={getColorMark(marks)} />
100
);
101
v.push(<FontFamily key={"font"} editor={editor} font={getFontMark(marks)} />);
102
return (
103
<div style={{ paddingRight: "10px", flex: 1, whiteSpace: "nowrap" }}>
104
{v}
105
</div>
106
);
107
};
108
109
function getColorMark(marks): string | undefined {
110
for (const key in marks) {
111
if (key.startsWith("color:") && marks[key]) {
112
return key.slice("color:".length);
113
}
114
}
115
}
116
117
function getFontMark(marks): string | undefined {
118
for (const key in marks) {
119
if (key.startsWith("font-family:") && marks[key]) {
120
return key.slice("font-family:".length);
121
}
122
}
123
}
124
125
function getSizeMark(marks): string | undefined {
126
for (const key in marks) {
127
if (key.startsWith("font-size:") && marks[key]) {
128
return key.slice("font-size:".length);
129
}
130
}
131
}
132
133