on the mouseover event I am adding a widget and node decoration to the editor at the specified position.
I set a data-drag-handle and dragging appears to work but it does not allow me to drop.
Here is the code:
import {Extension, Plugin} from "tiptap"
import { Decoration, DecorationSet } from 'prosemirror-view'
class DecorationState {
private static _instance: DecorationState
private _decorations:DecorationSet
protected constructor() {
this._decorations = DecorationSet.empty
}
private static get Instance () :DecorationState {
if(!DecorationState._instance) {
DecorationState._instance = new DecorationState()
}
return DecorationState._instance
}
public static get decorations ():DecorationSet {
return DecorationState.Instance._decorations
}
public static update(decoration:DecorationSet): void {
DecorationState.Instance._decorations = decoration
}
}
function getPos(view,{target}) {
const pos = view.posAtDOM(target)
const { doc } = view.state
const re = doc.resolve(pos)
return {before: re.before(), after: re.after()}
}
const prop = {
"data-type":"drag_item",
"draggable": "true"
}
const dragPlugin = new Plugin({
props: {
handleDOMEvents: {
mouseover (view,event) {
if (event.target.className.includes('ProseMirror')) return
const {before, after} = getPos(view,event)
const decorations: Array<Decoration> = []
const handler = document.createElement('div')
handler.setAttribute('style', 'width:10px; height: 10px; background:black;')
handler.setAttribute('data-drag-handle','')
decorations.push(Decoration.node(before,after, prop))
decorations.push(Decoration.widget(before+1, handler))
const set = DecorationSet.create(view.state.doc, decorations)
DecorationState.update(set)
view.update(view.props)
}
},
decorations () {
return DecorationState.decorations
}
}
})
export default class DragHandler extends Extension {
get name() {
return 'drag_handler'
}
get plugins(): Array<any> {
return [dragPlugin]
}
}
For some reason the slice content that would normally be present on the drop event is missing and I don’t understand why.
Here is some of PM’s code. It seems the selection is empty somehow:
sel.empty returns true