refactoring
This commit is contained in:
@@ -30,8 +30,6 @@ class VueNodeView implements NodeView {
|
|||||||
this.getPos = props.getPos
|
this.getPos = props.getPos
|
||||||
this.createUniqueId()
|
this.createUniqueId()
|
||||||
this.mount(component)
|
this.mount(component)
|
||||||
|
|
||||||
document.addEventListener('dragend', this.onDragEnd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createUniqueId() {
|
createUniqueId() {
|
||||||
@@ -39,9 +37,8 @@ class VueNodeView implements NodeView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
createNodeViewWrapper() {
|
createNodeViewWrapper() {
|
||||||
const nodeview = this
|
const { handleDragStart } = this
|
||||||
const { view } = this.editor
|
const dragstart = handleDragStart.bind(this)
|
||||||
const { getPos } = this
|
|
||||||
|
|
||||||
return Vue.extend({
|
return Vue.extend({
|
||||||
props: {
|
props: {
|
||||||
@@ -56,29 +53,8 @@ class VueNodeView implements NodeView {
|
|||||||
style: {
|
style: {
|
||||||
whiteSpace: 'normal',
|
whiteSpace: 'normal',
|
||||||
},
|
},
|
||||||
// attrs: {
|
|
||||||
// // draggable: false,
|
|
||||||
// contenteditable: false,
|
|
||||||
// },
|
|
||||||
on: {
|
on: {
|
||||||
dragstart: (event: Event) => {
|
dragstart,
|
||||||
const target = (event.target as HTMLElement)
|
|
||||||
|
|
||||||
if (nodeview.contentDOM?.contains(target)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, getPos())))
|
|
||||||
},
|
|
||||||
dragend: (event: Event) => {
|
|
||||||
const target = (event.target as HTMLElement)
|
|
||||||
|
|
||||||
if (nodeview.contentDOM?.contains(target)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
nodeview.isDragging = false
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
this.$slots.default,
|
this.$slots.default,
|
||||||
@@ -87,6 +63,20 @@ class VueNodeView implements NodeView {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleDragStart(event: Event) {
|
||||||
|
const { view } = this.editor
|
||||||
|
const target = (event.target as HTMLElement)
|
||||||
|
|
||||||
|
if (this.contentDOM?.contains(target)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const selection = NodeSelection.create(view.state.doc, this.getPos())
|
||||||
|
const transaction = view.state.tr.setSelection(selection)
|
||||||
|
|
||||||
|
view.dispatch(transaction)
|
||||||
|
}
|
||||||
|
|
||||||
createNodeViewContent() {
|
createNodeViewContent() {
|
||||||
const { id } = this
|
const { id } = this
|
||||||
|
|
||||||
@@ -157,49 +147,38 @@ class VueNodeView implements NodeView {
|
|||||||
stopEvent(event: Event) {
|
stopEvent(event: Event) {
|
||||||
const target = (event.target as HTMLElement)
|
const target = (event.target as HTMLElement)
|
||||||
const isInElement = this.dom.contains(target) && !this.contentDOM?.contains(target)
|
const isInElement = this.dom.contains(target) && !this.contentDOM?.contains(target)
|
||||||
|
|
||||||
|
// ignore all events from child nodes
|
||||||
|
if (!isInElement) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
const isDraggable = this.node.type.spec.draggable
|
const isDraggable = this.node.type.spec.draggable
|
||||||
const isCopyEvent = event.type === 'copy'
|
const isCopyEvent = event.type === 'copy'
|
||||||
const isPasteEvent = event.type === 'paste'
|
const isPasteEvent = event.type === 'paste'
|
||||||
const isCutEvent = event.type === 'cut'
|
const isCutEvent = event.type === 'cut'
|
||||||
const isDragEvent = event.type.startsWith('drag') || event.type === 'drop'
|
const isDragEvent = event.type.startsWith('drag') || event.type === 'drop'
|
||||||
|
|
||||||
if (!isInElement) {
|
if (isDraggable && isDragEvent && !this.isDragging) {
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isDragEvent && !this.isDragging) {
|
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we have to store that dragging started
|
||||||
if (isDraggable && !this.isDragging && event.type === 'mousedown') {
|
if (isDraggable && !this.isDragging && event.type === 'mousedown') {
|
||||||
const dragHandle = target.closest('[data-drag-handle]')
|
const dragHandle = target.closest('[data-drag-handle]')
|
||||||
// const isValidDragHandle = dragHandle
|
|
||||||
// && (this.dom === dragHandle || (this.dom.contains(dragHandle) && !this.contentDOM?.contains(dragHandle)))
|
|
||||||
const isValidDragHandle = dragHandle
|
const isValidDragHandle = dragHandle
|
||||||
&& (this.dom === dragHandle || (this.dom.contains(dragHandle)))
|
&& (this.dom === dragHandle || (this.dom.contains(dragHandle)))
|
||||||
|
|
||||||
if (isValidDragHandle) {
|
if (isValidDragHandle) {
|
||||||
this.isDragging = true
|
this.isDragging = true
|
||||||
// this.isDragging = true
|
document.addEventListener('dragend', () => {
|
||||||
// document.addEventListener('dragend', (e: Event) => {
|
this.isDragging = false
|
||||||
// console.log('DRAGEEEND')
|
}, { once: true })
|
||||||
// // const t = (e.target as HTMLElement)
|
|
||||||
// // if (t === this.dom) {
|
|
||||||
// // console.log('JEP')
|
|
||||||
// // } else {
|
|
||||||
// // console.log('NOPE')
|
|
||||||
// // }
|
|
||||||
// // // if (t === this.dom) {
|
|
||||||
// // // this.isDragging = false
|
|
||||||
// // // }
|
|
||||||
// this.isDragging = false
|
|
||||||
// }, { once: true })
|
|
||||||
// console.log('BIND EVENT', this.dom)
|
|
||||||
// document.addEventListener('dragend', this.onDragEnd, { once: true })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// these events are handled by prosemirror
|
||||||
if (this.isDragging || isCopyEvent || isPasteEvent || isCutEvent) {
|
if (this.isDragging || isCopyEvent || isPasteEvent || isCutEvent) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -207,18 +186,6 @@ class VueNodeView implements NodeView {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
onDragEnd(event: Event) {
|
|
||||||
// console.log('ONDRAGEND')
|
|
||||||
// const target = (event.target as HTMLElement)
|
|
||||||
|
|
||||||
// if (target === this.dom) {
|
|
||||||
// console.log('JEP')
|
|
||||||
// this.isDragging = false
|
|
||||||
// } else {
|
|
||||||
// console.log('NOPE')
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
ignoreMutation(mutation: MutationRecord | { type: 'selection'; target: Element }) {
|
ignoreMutation(mutation: MutationRecord | { type: 'selection'; target: Element }) {
|
||||||
if (mutation.type === 'selection') {
|
if (mutation.type === 'selection') {
|
||||||
return true
|
return true
|
||||||
@@ -234,21 +201,8 @@ class VueNodeView implements NodeView {
|
|||||||
return contentDOMHasChanged
|
return contentDOMHasChanged
|
||||||
}
|
}
|
||||||
|
|
||||||
selectNode() {
|
|
||||||
// this.updateComponentProps({
|
|
||||||
// selected: true,
|
|
||||||
// })
|
|
||||||
}
|
|
||||||
|
|
||||||
deselectNode() {
|
|
||||||
// this.updateComponentProps({
|
|
||||||
// selected: false,
|
|
||||||
// })
|
|
||||||
}
|
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.vm.$destroy()
|
this.vm.$destroy()
|
||||||
document.removeEventListener('dragend', this.onDragEnd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update(node: ProseMirrorNode, decorations: Decoration[]) {
|
update(node: ProseMirrorNode, decorations: Decoration[]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user