forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dom.ts
357 lines (300 loc) · 9.09 KB
/
dom.ts
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* Types.
*/
// COMPAT: This is required to prevent TypeScript aliases from doing some very
// weird things for Slate's types with the same name as globals. (2019/11/27)
// https://github.com/microsoft/TypeScript/issues/35002
import DOMNode = globalThis.Node
import DOMComment = globalThis.Comment
import DOMElement = globalThis.Element
import DOMText = globalThis.Text
import DOMRange = globalThis.Range
import DOMSelection = globalThis.Selection
import DOMStaticRange = globalThis.StaticRange
import { ReactEditor } from '../plugin/react-editor'
export {
DOMNode,
DOMComment,
DOMElement,
DOMText,
DOMRange,
DOMSelection,
DOMStaticRange,
}
declare global {
interface Window {
Selection: (typeof Selection)['constructor']
DataTransfer: (typeof DataTransfer)['constructor']
Node: (typeof Node)['constructor']
}
}
export type DOMPoint = [Node, number]
/**
* Returns the host window of a DOM node
*/
export const getDefaultView = (value: any): Window | null => {
return (
(value && value.ownerDocument && value.ownerDocument.defaultView) || null
)
}
/**
* Check if a DOM node is a comment node.
*/
export const isDOMComment = (value: any): value is DOMComment => {
return isDOMNode(value) && value.nodeType === 8
}
/**
* Check if a DOM node is an element node.
*/
export const isDOMElement = (value: any): value is DOMElement => {
return isDOMNode(value) && value.nodeType === 1
}
/**
* Check if a value is a DOM node.
*/
export const isDOMNode = (value: any): value is DOMNode => {
const window = getDefaultView(value)
return !!window && value instanceof window.Node
}
/**
* Check if a value is a DOM selection.
*/
export const isDOMSelection = (value: any): value is DOMSelection => {
const window = value && value.anchorNode && getDefaultView(value.anchorNode)
return !!window && value instanceof window.Selection
}
/**
* Check if a DOM node is an element node.
*/
export const isDOMText = (value: any): value is DOMText => {
return isDOMNode(value) && value.nodeType === 3
}
/**
* Checks whether a paste event is a plaintext-only event.
*/
export const isPlainTextOnlyPaste = (event: ClipboardEvent) => {
return (
event.clipboardData &&
event.clipboardData.getData('text/plain') !== '' &&
event.clipboardData.types.length === 1
)
}
/**
* Normalize a DOM point so that it always refers to a text node.
*/
export const normalizeDOMPoint = (domPoint: DOMPoint): DOMPoint => {
let [node, offset] = domPoint
// If it's an element node, its offset refers to the index of its children
// including comment nodes, so try to find the right text child node.
if (isDOMElement(node) && node.childNodes.length) {
let isLast = offset === node.childNodes.length
let index = isLast ? offset - 1 : offset
;[node, index] = getEditableChildAndIndex(
node,
index,
isLast ? 'backward' : 'forward'
)
// If the editable child found is in front of input offset, we instead seek to its end
isLast = index < offset
// If the node has children, traverse until we have a leaf node. Leaf nodes
// can be either text nodes, or other void DOM nodes.
while (isDOMElement(node) && node.childNodes.length) {
const i = isLast ? node.childNodes.length - 1 : 0
node = getEditableChild(node, i, isLast ? 'backward' : 'forward')
}
// Determine the new offset inside the text node.
offset = isLast && node.textContent != null ? node.textContent.length : 0
}
// Return the node and offset.
return [node, offset]
}
/**
* Determines whether the active element is nested within a shadowRoot
*/
export const hasShadowRoot = (node: Node | null) => {
let parent = node && node.parentNode
while (parent) {
if (parent.toString() === '[object ShadowRoot]') {
return true
}
parent = parent.parentNode
}
return false
}
/**
* Get the nearest editable child and index at `index` in a `parent`, preferring
* `direction`.
*/
export const getEditableChildAndIndex = (
parent: DOMElement,
index: number,
direction: 'forward' | 'backward'
): [DOMNode, number] => {
const { childNodes } = parent
let child = childNodes[index]
let i = index
let triedForward = false
let triedBackward = false
// While the child is a comment node, or an element node with no children,
// keep iterating to find a sibling non-void, non-comment node.
while (
isDOMComment(child) ||
(isDOMElement(child) && child.childNodes.length === 0) ||
(isDOMElement(child) && child.getAttribute('contenteditable') === 'false')
) {
if (triedForward && triedBackward) {
break
}
if (i >= childNodes.length) {
triedForward = true
i = index - 1
direction = 'backward'
continue
}
if (i < 0) {
triedBackward = true
i = index + 1
direction = 'forward'
continue
}
child = childNodes[i]
index = i
i += direction === 'forward' ? 1 : -1
}
return [child, index]
}
/**
* Get the nearest editable child at `index` in a `parent`, preferring
* `direction`.
*/
export const getEditableChild = (
parent: DOMElement,
index: number,
direction: 'forward' | 'backward'
): DOMNode => {
const [child] = getEditableChildAndIndex(parent, index, direction)
return child
}
/**
* Get a plaintext representation of the content of a node, accounting for block
* elements which get a newline appended.
*
* The domNode must be attached to the DOM.
*/
export const getPlainText = (domNode: DOMNode) => {
let text = ''
if (isDOMText(domNode) && domNode.nodeValue) {
return domNode.nodeValue
}
if (isDOMElement(domNode)) {
for (const childNode of Array.from(domNode.childNodes)) {
text += getPlainText(childNode)
}
const display = getComputedStyle(domNode).getPropertyValue('display')
if (display === 'block' || display === 'list' || domNode.tagName === 'BR') {
text += '\n'
}
}
return text
}
/**
* Get x-slate-fragment attribute from data-slate-fragment
*/
const catchSlateFragment = /data-slate-fragment="(.+?)"/m
export const getSlateFragmentAttribute = (
dataTransfer: DataTransfer
): string | void => {
const htmlData = dataTransfer.getData('text/html')
const [, fragment] = htmlData.match(catchSlateFragment) || []
return fragment
}
/**
* Get the x-slate-fragment attribute that exist in text/html data
* and append it to the DataTransfer object
*/
export const getClipboardData = (
dataTransfer: DataTransfer,
clipboardFormatKey = 'x-slate-fragment'
): DataTransfer => {
if (!dataTransfer.getData(`application/${clipboardFormatKey}`)) {
const fragment = getSlateFragmentAttribute(dataTransfer)
if (fragment) {
const clipboardData = new DataTransfer()
dataTransfer.types.forEach(type => {
clipboardData.setData(type, dataTransfer.getData(type))
})
clipboardData.setData(`application/${clipboardFormatKey}`, fragment)
return clipboardData
}
}
return dataTransfer
}
/**
* Get the dom selection from Shadow Root if possible, otherwise from the document
*/
export const getSelection = (root: Document | ShadowRoot): Selection | null => {
if (root.getSelection != null) {
return root.getSelection()
}
return document.getSelection()
}
/**
* Check whether a mutation originates from a editable element inside the editor.
*/
export const isTrackedMutation = (
editor: ReactEditor,
mutation: MutationRecord,
batch: MutationRecord[]
): boolean => {
const { target } = mutation
if (isDOMElement(target) && target.matches('[contentEditable="false"]')) {
return false
}
const { document } = ReactEditor.getWindow(editor)
if (document.contains(target)) {
return ReactEditor.hasDOMNode(editor, target, { editable: true })
}
const parentMutation = batch.find(({ addedNodes, removedNodes }) => {
for (const node of addedNodes) {
if (node === target || node.contains(target)) {
return true
}
}
for (const node of removedNodes) {
if (node === target || node.contains(target)) {
return true
}
}
})
if (!parentMutation || parentMutation === mutation) {
return false
}
// Target add/remove is tracked. Track the mutation if we track the parent mutation.
return isTrackedMutation(editor, parentMutation, batch)
}
/**
* Retrieves the deepest active element in the DOM, considering nested shadow DOMs.
*/
export const getActiveElement = () => {
let activeElement = document.activeElement
while (activeElement?.shadowRoot && activeElement.shadowRoot?.activeElement) {
activeElement = activeElement?.shadowRoot?.activeElement
}
return activeElement
}
/**
* @returns `true` if `otherNode` is before `node` in the document; otherwise, `false`.
*/
export const isBefore = (node: DOMNode, otherNode: DOMNode): boolean =>
Boolean(
node.compareDocumentPosition(otherNode) &
DOMNode.DOCUMENT_POSITION_PRECEDING
)
/**
* @returns `true` if `otherNode` is after `node` in the document; otherwise, `false`.
*/
export const isAfter = (node: DOMNode, otherNode: DOMNode): boolean =>
Boolean(
node.compareDocumentPosition(otherNode) &
DOMNode.DOCUMENT_POSITION_FOLLOWING
)