UNPKG

10 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.js","sources":["../src/dropcursor.js"],"sourcesContent":["import {Plugin} from \"prosemirror-state\"\nimport {dropPoint} from \"prosemirror-transform\"\n\n// :: (options: ?Object) → Plugin\n// Create a plugin that, when added to a ProseMirror instance,\n// causes a decoration to show up at the drop position when something\n// is dragged over the editor.\n//\n// options::- These options are supported:\n//\n// color:: ?string\n// The color of the cursor. Defaults to `black`.\n//\n// width:: ?number\n// The precise width of the cursor in pixels. Defaults to 1.\n//\n// class:: ?string\n// A CSS class name to add to the cursor element.\nexport function dropCursor(options = {}) {\n return new Plugin({\n view(editorView) { return new DropCursorView(editorView, options) }\n })\n}\n\nclass DropCursorView {\n constructor(editorView, options) {\n this.editorView = editorView\n this.width = options.width || 1\n this.color = options.color || \"black\"\n this.class = options.class\n this.cursorPos = null\n this.element = null\n this.timeout = null\n\n this.handlers = [\"dragover\", \"dragend\", \"drop\", \"dragleave\"].map(name => {\n let handler = e => this[name](e)\n editorView.dom.addEventListener(name, handler)\n return {name, handler}\n })\n }\n\n destroy() {\n this.handlers.forEach(({name, handler}) => this.editorView.dom.removeEventListener(name, handler))\n }\n\n update(editorView, prevState) {\n if (this.cursorPos != null && prevState.doc != editorView.state.doc) {\n if (this.cursorPos > editorView.state.doc.content.size) this.setCursor(null)\n else this.updateOverlay()\n }\n }\n\n setCursor(pos) {\n if (pos == this.cursorPos) return\n this.cursorPos = pos\n if (pos == null) {\n this.element.parentNode.removeChild(this.element)\n this.element = null\n } else {\n this.updateOverlay()\n }\n }\n\n updateOverlay() {\n let $pos = this.editorView.state.doc.resolve(this.cursorPos), rect\n if (!$pos.parent.inlineContent) {\n let before = $pos.nodeBefore, after = $pos.nodeAfter\n if (before || after) {\n let nodeRect = this.editorView.nodeDOM(this.cursorPos - (before ? before.nodeSize : 0)).getBoundingClientRect()\n let top = before ? nodeRect.bottom : nodeRect.top\n if (before && after)\n top = (top + this.editorView.nodeDOM(this.cursorPos).getBoundingClientRect().top) / 2\n rect = {left: nodeRect.left, right: nodeRect.right, top: top - this.width / 2, bottom: top + this.width / 2}\n }\n }\n if (!rect) {\n let coords = this.editorView.coordsAtPos(this.cursorPos)\n rect = {left: coords.left - this.width / 2, right: coords.left + this.width / 2, top: coords.top, bottom: coords.bottom}\n }\n\n let parent = this.editorView.dom.offsetParent\n if (!this.element) {\n this.element = parent.appendChild(document.createElement(\"div\"))\n if (this.class) this.element.className = this.class\n this.element.style.cssText = \"position: absolute; z-index: 50; pointer-events: none; background-color: \" + this.color\n }\n let parentLeft, parentTop\n if (!parent || parent == document.body && getComputedStyle(parent).position == \"static\") {\n parentLeft = -pageXOffset\n parentTop = -pageYOffset\n } else {\n let rect = parent.getBoundingClientRect()\n parentLeft = rect.left - parent.scrollLeft\n parentTop = rect.top - parent.scrollTop\n }\n this.element.style.left = (rect.left - parentLeft) + \"px\"\n this.element.style.top = (rect.top - parentTop) + \"px\"\n this.element.style.width = (rect.right - rect.left) + \"px\"\n this.element.style.height = (rect.bottom - rect.top) + \"px\"\n }\n\n scheduleRemoval(timeout) {\n clearTimeout(this.timeout)\n this.timeout = setTimeout(() => this.setCursor(null), timeout)\n }\n\n dragover(event) {\n if (!this.editorView.editable) return\n let pos = this.editorView.posAtCoords({left: event.clientX, top: event.clientY})\n if (pos) {\n let target = pos.pos\n if (this.editorView.dragging && this.editorView.dragging.slice) {\n target = dropPoint(this.editorView.state.doc, target, this.editorView.dragging.slice)\n if (target == null) return this.setCursor(null)\n }\n this.setCursor(target)\n this.scheduleRemoval(5000)\n }\n }\n\n dragend() {\n this.scheduleRemoval(20)\n }\n\n drop() {\n this.scheduleRemoval(20)\n }\n\n dragleave(event) {\n if (event.target == this.editorView.dom || !this.editorView.dom.contains(event.relatedTarget))\n this.setCursor(null)\n }\n}\n"],"names":["Plugin","let","this","rect","dropPoint"],"mappings":";;;;;;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,S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