forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaf.tsx
84 lines (73 loc) · 1.93 KB
/
leaf.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React from 'react'
import { Text, Element } from 'slate'
import String from './string'
import { PLACEHOLDER_SYMBOL } from '../utils/weak-maps'
import { RenderLeafProps } from './editable'
/**
* Individual leaves in a text node with unique formatting.
*/
const Leaf = (props: {
isLast: boolean
leaf: Text
parent: Element
renderLeaf?: (props: RenderLeafProps) => JSX.Element
text: Text
}) => {
const {
leaf,
isLast,
text,
parent,
renderLeaf = (props: RenderLeafProps) => <DefaultLeaf {...props} />,
} = props
let children = (
<String isLast={isLast} leaf={leaf} parent={parent} text={text} />
)
if (leaf[PLACEHOLDER_SYMBOL]) {
children = (
<React.Fragment>
<span
contentEditable={false}
style={{
pointerEvents: 'none',
display: 'inline-block',
verticalAlign: 'text-top',
width: '0',
maxWidth: '100%',
whiteSpace: 'nowrap',
opacity: '0.333',
}}
>
{leaf.placeholder}
</span>
{children}
</React.Fragment>
)
}
// COMPAT: Having the `data-` attributes on these leaf elements ensures that
// in certain misbehaving browsers they aren't weirdly cloned/destroyed by
// contenteditable behaviors. (2019/05/08)
const attributes: {
'data-slate-leaf': true
} = {
'data-slate-leaf': true,
}
return renderLeaf({ attributes, children, leaf, text })
}
const MemoizedLeaf = React.memo(Leaf, (prev, next) => {
return (
next.parent === prev.parent &&
next.isLast === prev.isLast &&
next.renderLeaf === prev.renderLeaf &&
next.text === prev.text &&
Text.matches(next.leaf, prev.leaf)
)
})
/**
* The default custom leaf renderer.
*/
export const DefaultLeaf = (props: RenderLeafProps) => {
const { attributes, children } = props
return <span {...attributes}>{children}</span>
}
export default MemoizedLeaf