-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NodeView.ts
307 lines (248 loc) · 8.55 KB
/
NodeView.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
import { NodeSelection } from '@tiptap/pm/state'
import { NodeView as ProseMirrorNodeView } from '@tiptap/pm/view'
import { Editor as CoreEditor } from './Editor.js'
import { DecorationWithType, NodeViewRendererOptions, NodeViewRendererProps } from './types.js'
import { isAndroid } from './utilities/isAndroid.js'
import { isiOS } from './utilities/isiOS.js'
/**
* Node views are used to customize the rendered DOM structure of a node.
* @see https://tiptap.dev/guide/node-views
*/
export class NodeView<
Component,
NodeEditor extends CoreEditor = CoreEditor,
Options extends NodeViewRendererOptions = NodeViewRendererOptions,
> implements ProseMirrorNodeView {
component: Component
editor: NodeEditor
options: Options
extension: NodeViewRendererProps['extension']
node: NodeViewRendererProps['node']
decorations: NodeViewRendererProps['decorations']
innerDecorations: NodeViewRendererProps['innerDecorations']
view: NodeViewRendererProps['view']
getPos: NodeViewRendererProps['getPos']
HTMLAttributes: NodeViewRendererProps['HTMLAttributes']
isDragging = false
constructor(component: Component, props: NodeViewRendererProps, options?: Partial<Options>) {
this.component = component
this.editor = props.editor as NodeEditor
this.options = {
stopEvent: null,
ignoreMutation: null,
...options,
} as Options
this.extension = props.extension
this.node = props.node
this.decorations = props.decorations as DecorationWithType[]
this.innerDecorations = props.innerDecorations
this.view = props.view
this.HTMLAttributes = props.HTMLAttributes
this.getPos = props.getPos
this.mount()
}
mount() {
// eslint-disable-next-line
return
}
get dom(): HTMLElement {
return this.editor.view.dom as HTMLElement
}
get contentDOM(): HTMLElement | null {
return null
}
onDragStart(event: DragEvent) {
const { view } = this.editor
const target = event.target as HTMLElement
// get the drag handle element
// `closest` is not available for text nodes so we may have to use its parent
const dragHandle = target.nodeType === 3
? target.parentElement?.closest('[data-drag-handle]')
: target.closest('[data-drag-handle]')
if (!this.dom || this.contentDOM?.contains(target) || !dragHandle) {
return
}
let x = 0
let y = 0
// calculate offset for drag element if we use a different drag handle element
if (this.dom !== dragHandle) {
const domBox = this.dom.getBoundingClientRect()
const handleBox = dragHandle.getBoundingClientRect()
// In React, we have to go through nativeEvent to reach offsetX/offsetY.
const offsetX = event.offsetX ?? (event as any).nativeEvent?.offsetX
const offsetY = event.offsetY ?? (event as any).nativeEvent?.offsetY
x = handleBox.x - domBox.x + offsetX
y = handleBox.y - domBox.y + offsetY
}
event.dataTransfer?.setDragImage(this.dom, x, y)
const pos = this.getPos()
if (typeof pos !== 'number') {
return
}
// we need to tell ProseMirror that we want to move the whole node
// so we create a NodeSelection
const selection = NodeSelection.create(view.state.doc, pos)
const transaction = view.state.tr.setSelection(selection)
view.dispatch(transaction)
}
stopEvent(event: Event) {
if (!this.dom) {
return false
}
if (typeof this.options.stopEvent === 'function') {
return this.options.stopEvent({ event })
}
const target = event.target as HTMLElement
const isInElement = this.dom.contains(target) && !this.contentDOM?.contains(target)
// any event from child nodes should be handled by ProseMirror
if (!isInElement) {
return false
}
const isDragEvent = event.type.startsWith('drag')
const isDropEvent = event.type === 'drop'
const isInput = ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA'].includes(target.tagName) || target.isContentEditable
// any input event within node views should be ignored by ProseMirror
if (isInput && !isDropEvent && !isDragEvent) {
return true
}
const { isEditable } = this.editor
const { isDragging } = this
const isDraggable = !!this.node.type.spec.draggable
const isSelectable = NodeSelection.isSelectable(this.node)
const isCopyEvent = event.type === 'copy'
const isPasteEvent = event.type === 'paste'
const isCutEvent = event.type === 'cut'
const isClickEvent = event.type === 'mousedown'
// ProseMirror tries to drag selectable nodes
// even if `draggable` is set to `false`
// this fix prevents that
if (!isDraggable && isSelectable && isDragEvent) {
event.preventDefault()
}
if (isDraggable && isDragEvent && !isDragging) {
event.preventDefault()
return false
}
// we have to store that dragging started
if (isDraggable && isEditable && !isDragging && isClickEvent) {
const dragHandle = target.closest('[data-drag-handle]')
const isValidDragHandle = dragHandle && (this.dom === dragHandle || this.dom.contains(dragHandle))
if (isValidDragHandle) {
this.isDragging = true
document.addEventListener(
'dragend',
() => {
this.isDragging = false
},
{ once: true },
)
document.addEventListener(
'drop',
() => {
this.isDragging = false
},
{ once: true },
)
document.addEventListener(
'mouseup',
() => {
this.isDragging = false
},
{ once: true },
)
}
}
// these events are handled by prosemirror
if (
isDragging
|| isDropEvent
|| isCopyEvent
|| isPasteEvent
|| isCutEvent
|| (isClickEvent && isSelectable)
) {
return false
}
return true
}
/**
* Called when a DOM [mutation](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) or a selection change happens within the view.
* @return `false` if the editor should re-read the selection or re-parse the range around the mutation
* @return `true` if it can safely be ignored.
*/
ignoreMutation(mutation: MutationRecord | { type: 'selection'; target: Element }) {
if (!this.dom || !this.contentDOM) {
return true
}
if (typeof this.options.ignoreMutation === 'function') {
return this.options.ignoreMutation({ mutation })
}
// a leaf/atom node is like a black box for ProseMirror
// and should be fully handled by the node view
if (this.node.isLeaf || this.node.isAtom) {
return true
}
// ProseMirror should handle any selections
if (mutation.type === 'selection') {
return false
}
// try to prevent a bug on iOS and Android that will break node views on enter
// this is because ProseMirror can’t preventDispatch on enter
// this will lead to a re-render of the node view on enter
// see: https://github.com/ueberdosis/tiptap/issues/1214
// see: https://github.com/ueberdosis/tiptap/issues/2534
if (
this.dom.contains(mutation.target)
&& mutation.type === 'childList'
&& (isiOS() || isAndroid())
&& this.editor.isFocused
) {
const changedNodes = [
...Array.from(mutation.addedNodes),
...Array.from(mutation.removedNodes),
] as HTMLElement[]
// we’ll check if every changed node is contentEditable
// to make sure it’s probably mutated by ProseMirror
if (changedNodes.every(node => node.isContentEditable)) {
return false
}
}
// we will allow mutation contentDOM with attributes
// so we can for example adding classes within our node view
if (this.contentDOM === mutation.target && mutation.type === 'attributes') {
return true
}
// ProseMirror should handle any changes within contentDOM
if (this.contentDOM.contains(mutation.target)) {
return false
}
return true
}
/**
* Update the attributes of the prosemirror node.
*/
updateAttributes(attributes: Record<string, any>): void {
this.editor.commands.command(({ tr }) => {
const pos = this.getPos()
if (typeof pos !== 'number') {
return false
}
tr.setNodeMarkup(pos, undefined, {
...this.node.attrs,
...attributes,
})
return true
})
}
/**
* Delete the node.
*/
deleteNode(): void {
const from = this.getPos()
if (typeof from !== 'number') {
return
}
const to = from + this.node.nodeSize
this.editor.commands.deleteRange({ from, to })
}
}