Path: blob/master/src/packages/frontend/editors/slate/slate-react/components/leaf.tsx
1698 views
import React from "react";1import { Element, Text } from "slate";2import String from "./string";3import { PLACEHOLDER_SYMBOL } from "../utils/weak-maps";4import { RenderLeafProps } from "./editable";5import { isEqual } from "lodash";67/**8* Individual leaves in a text node with unique formatting.9*/1011const Leaf = (props: {12isLast: boolean;13leaf: Text;14parent: Element;15renderLeaf?: React.FC<RenderLeafProps>;16text: Text;17}) => {18const {19leaf,20isLast,21text,22parent,23renderLeaf = (props: RenderLeafProps) => <DefaultLeaf {...props} />,24} = props;2526let children = (27<String isLast={isLast} leaf={leaf} parent={parent} text={text} />28);2930if (leaf[PLACEHOLDER_SYMBOL]) {31children = (32<React.Fragment>33<span34contentEditable={false}35style={{36pointerEvents: "none",37display: "inline-block",38width: "0",39maxWidth: "100%",40whiteSpace: "nowrap",41opacity: "0.333",42userSelect: "none",43fontStyle: "normal",44fontWeight: "normal",45textDecoration: "none",46}}47>48{leaf["placeholder"]}49</span>50{children}51</React.Fragment>52);53}5455// COMPAT: Having the `data-` attributes on these leaf elements ensures that56// in certain misbehaving browsers they aren't weirdly cloned/destroyed by57// contenteditable behaviors. (2019/05/08)58const attributes: {59"data-slate-leaf": true;60} = {61"data-slate-leaf": true,62};6364return React.createElement(renderLeaf, { attributes, children, leaf, text });65};6667const MemoizedLeaf = React.memo(Leaf, (prev, next) => {68return (69next.parent === prev.parent &&70next.isLast === prev.isLast &&71next.renderLeaf === prev.renderLeaf &&72next.text === prev.text &&73isEqual(next.leaf, prev.leaf)74);75});7677export const DefaultLeaf = (props: RenderLeafProps) => {78const { attributes, children } = props;79return <span {...attributes}>{children}</span>;80};8182export default MemoizedLeaf;838485