Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/components/heading-menu.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { range } from "lodash";6import React, { CSSProperties, useMemo } from "react";78import { DropdownMenu, Icon } from "@cocalc/frontend/components";9import { MenuItems } from "./dropdown-menu";1011interface Props {12onClick: (heading: number) => void;13style?: CSSProperties;14markdown?: boolean; // if it is markdown we can document the shortcuts.15}1617export default function HeadingMenu({ onClick, style, markdown }: Props) {18const items = useMemo((): MenuItems => {19return range(1, 7).map((heading) => {20return {21key: heading,22onClick: () => onClick(heading),23label: <HeadingContent heading={heading} markdown={markdown} />,24};25});26}, [onClick, markdown]);2728return (29<DropdownMenu30button={true}31title={<Icon name={"header"} />}32key={"heading"}33style={style}34items={items}35/>36);37}3839export function HeadingContent({40heading,41markdown,42}: {43heading: number;44markdown?: boolean;45}): JSX.Element {46const hashes = markdown47? range(heading)48.map(() => "#")49.join("")50: "";5152const label =53heading == 054? "Paragraph"55: `Heading ${heading}${56markdown ? " (shortcut: " + hashes + "␣Foo…)" : ""57}`;58if (heading === 0) {59return <span>{label}</span>;60} else {61return React.createElement(`h${heading}`, { style: { margin: 0 } }, label);62}63}646566