forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.tsx
166 lines (149 loc) · 4.2 KB
/
element.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React, { useCallback } from 'react'
import getDirection from 'direction'
import { Editor, Node, Range, Element as SlateElement } from 'slate'
import Text from './text'
import useChildren from '../hooks/use-children'
import { ReactEditor, useSlateStatic, useReadOnly } from '..'
import {
NODE_TO_ELEMENT,
ELEMENT_TO_NODE,
NODE_TO_PARENT,
NODE_TO_INDEX,
EDITOR_TO_KEY_TO_ELEMENT,
} from '../utils/weak-maps'
import { isElementDecorationsEqual } from '../utils/range-list'
import {
RenderElementProps,
RenderLeafProps,
RenderPlaceholderProps,
} from './editable'
/**
* Element.
*/
const Element = (props: {
decorations: Range[]
element: SlateElement
renderElement?: (props: RenderElementProps) => JSX.Element
renderPlaceholder: (props: RenderPlaceholderProps) => JSX.Element
renderLeaf?: (props: RenderLeafProps) => JSX.Element
selection: Range | null
}) => {
const {
decorations,
element,
renderElement = (p: RenderElementProps) => <DefaultElement {...p} />,
renderPlaceholder,
renderLeaf,
selection,
} = props
const editor = useSlateStatic()
const readOnly = useReadOnly()
const isInline = editor.isInline(element)
const key = ReactEditor.findKey(editor, element)
const ref = useCallback(
(ref: HTMLElement | null) => {
// Update element-related weak maps with the DOM element ref.
const KEY_TO_ELEMENT = EDITOR_TO_KEY_TO_ELEMENT.get(editor)
if (ref) {
KEY_TO_ELEMENT?.set(key, ref)
NODE_TO_ELEMENT.set(element, ref)
ELEMENT_TO_NODE.set(ref, element)
} else {
KEY_TO_ELEMENT?.delete(key)
NODE_TO_ELEMENT.delete(element)
}
},
[editor, key, element]
)
let children: React.ReactNode = useChildren({
decorations,
node: element,
renderElement,
renderPlaceholder,
renderLeaf,
selection,
})
// Attributes that the developer must mix into the element in their
// custom node renderer component.
const attributes: {
'data-slate-node': 'element'
'data-slate-void'?: true
'data-slate-inline'?: true
contentEditable?: false
dir?: 'rtl'
ref: any
} = {
'data-slate-node': 'element',
ref,
}
if (isInline) {
attributes['data-slate-inline'] = true
}
// If it's a block node with inline children, add the proper `dir` attribute
// for text direction.
if (!isInline && Editor.hasInlines(editor, element)) {
const text = Node.string(element)
const dir = getDirection(text)
if (dir === 'rtl') {
attributes.dir = dir
}
}
// If it's a void node, wrap the children in extra void-specific elements.
if (Editor.isVoid(editor, element)) {
attributes['data-slate-void'] = true
if (!readOnly && isInline) {
attributes.contentEditable = false
}
const Tag = isInline ? 'span' : 'div'
const [[text]] = Node.texts(element)
children = (
<Tag
data-slate-spacer
style={{
height: '0',
color: 'transparent',
outline: 'none',
position: 'absolute',
}}
>
<Text
renderPlaceholder={renderPlaceholder}
decorations={[]}
isLast={false}
parent={element}
text={text}
/>
</Tag>
)
NODE_TO_INDEX.set(text, 0)
NODE_TO_PARENT.set(text, element)
}
return renderElement({ attributes, children, element })
}
const MemoizedElement = React.memo(Element, (prev, next) => {
return (
prev.element === next.element &&
prev.renderElement === next.renderElement &&
prev.renderLeaf === next.renderLeaf &&
prev.renderPlaceholder === next.renderPlaceholder &&
isElementDecorationsEqual(prev.decorations, next.decorations) &&
(prev.selection === next.selection ||
(!!prev.selection &&
!!next.selection &&
Range.equals(prev.selection, next.selection)))
)
})
/**
* The default element renderer.
*/
export const DefaultElement = (props: RenderElementProps) => {
const { attributes, children, element } = props
const editor = useSlateStatic()
const Tag = editor.isInline(element) ? 'span' : 'div'
return (
<Tag {...attributes} style={{ position: 'relative' }}>
{children}
</Tag>
)
}
export default MemoizedElement