CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/components/heading-menu.tsx
Views: 687
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 { range } from "lodash";
7
import React, { CSSProperties, useMemo } from "react";
8
9
import { DropdownMenu, Icon } from "@cocalc/frontend/components";
10
import { MenuItems } from "./dropdown-menu";
11
12
interface Props {
13
onClick: (heading: number) => void;
14
style?: CSSProperties;
15
markdown?: boolean; // if it is markdown we can document the shortcuts.
16
}
17
18
export default function HeadingMenu({ onClick, style, markdown }: Props) {
19
const items = useMemo((): MenuItems => {
20
return range(1, 7).map((heading) => {
21
return {
22
key: heading,
23
onClick: () => onClick(heading),
24
label: <HeadingContent heading={heading} markdown={markdown} />,
25
};
26
});
27
}, [onClick, markdown]);
28
29
return (
30
<DropdownMenu
31
button={true}
32
title={<Icon name={"header"} />}
33
key={"heading"}
34
style={style}
35
items={items}
36
/>
37
);
38
}
39
40
export function HeadingContent({
41
heading,
42
markdown,
43
}: {
44
heading: number;
45
markdown?: boolean;
46
}): JSX.Element {
47
const hashes = markdown
48
? range(heading)
49
.map(() => "#")
50
.join("")
51
: "";
52
53
const label =
54
heading == 0
55
? "Paragraph"
56
: `Heading ${heading}${
57
markdown ? " (shortcut: " + hashes + "␣Foo…)" : ""
58
}`;
59
if (heading === 0) {
60
return <span>{label}</span>;
61
} else {
62
return React.createElement(`h${heading}`, { style: { margin: 0 } }, label);
63
}
64
}
65
66