Path: blob/master/src/packages/frontend/editors/slate/leaf.tsx
1691 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import React, { CSSProperties } from "react";6import type { RenderLeafProps } from "./slate-react";7import { startswith } from "@cocalc/util/misc";89// CODE_STYLE -- copied from GitHub10const CODE_STYLE = {11padding: ".2em .4em",12margin: 0,13fontSize: "85%",14borderRadius: "6px",15} as CSSProperties;1617const Leaf: React.FC<RenderLeafProps> = React.memo(18({ attributes, children, leaf }) => {19if (leaf.bold) {20children = <strong>{children}</strong>;21}22if (leaf.italic) {23children = <em>{children}</em>;24}25if (leaf.strikethrough) {26children = <s>{children}</s>;27}28if (leaf.underline) {29children = <u>{children}</u>;30}31if (leaf.sup) {32children = <sup>{children}</sup>;33}34if (leaf.sub) {35children = <sub>{children}</sub>;36}37if (leaf.code) {38children = <code style={CODE_STYLE}>{children}</code>;39}40if (leaf.small) {41children = <small>{children}</small>;42}43if (leaf.tt) {44children = <span style={{ fontFamily: "monospace" }}>{children}</span>;45}46// check for colors, fonts, etc.47for (const mark in leaf) {48if (!leaf[mark]) continue; // only if it is true49if (startswith(mark, "color:")) {50children = (51<span style={{ color: mark.split(":")[1] }}>{children}</span>52);53}54if (startswith(mark, "font-family:")) {55children = (56<span style={{ fontFamily: mark.split(":")[1] }}>{children}</span>57);58}59if (startswith(mark, "font-size:")) {60children = (61<span style={{ fontSize: mark.split(":")[1] }}>{children}</span>62);63}64}65if (leaf.search) {66// Search highlighting of text nodes.67// Same color as CodeMirror's default.68children = <span style={{ backgroundColor: "#ffa" }}>{children}</span>;69}7071return <span {...attributes}>{children}</span>;72}73);7475export default Leaf;767778