index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { Plugin } from 'prosemirror-state';
  2. import { dropPoint } from 'prosemirror-transform';
  3. /**
  4. Create a plugin that, when added to a ProseMirror instance,
  5. causes a decoration to show up at the drop position when something
  6. is dragged over the editor.
  7. Nodes may add a `disableDropCursor` property to their spec to
  8. control the showing of a drop cursor inside them. This may be a
  9. boolean or a function, which will be called with a view and a
  10. position, and should return a boolean.
  11. */
  12. function dropCursor(options = {}) {
  13. return new Plugin({
  14. view(editorView) { return new DropCursorView(editorView, options); }
  15. });
  16. }
  17. class DropCursorView {
  18. constructor(editorView, options) {
  19. var _a;
  20. this.editorView = editorView;
  21. this.cursorPos = null;
  22. this.element = null;
  23. this.timeout = -1;
  24. this.width = (_a = options.width) !== null && _a !== void 0 ? _a : 1;
  25. this.color = options.color === false ? undefined : (options.color || "black");
  26. this.class = options.class;
  27. this.handlers = ["dragover", "dragend", "drop", "dragleave"].map(name => {
  28. let handler = (e) => { this[name](e); };
  29. editorView.dom.addEventListener(name, handler);
  30. return { name, handler };
  31. });
  32. }
  33. destroy() {
  34. this.handlers.forEach(({ name, handler }) => this.editorView.dom.removeEventListener(name, handler));
  35. }
  36. update(editorView, prevState) {
  37. if (this.cursorPos != null && prevState.doc != editorView.state.doc) {
  38. if (this.cursorPos > editorView.state.doc.content.size)
  39. this.setCursor(null);
  40. else
  41. this.updateOverlay();
  42. }
  43. }
  44. setCursor(pos) {
  45. if (pos == this.cursorPos)
  46. return;
  47. this.cursorPos = pos;
  48. if (pos == null) {
  49. this.element.parentNode.removeChild(this.element);
  50. this.element = null;
  51. }
  52. else {
  53. this.updateOverlay();
  54. }
  55. }
  56. updateOverlay() {
  57. let $pos = this.editorView.state.doc.resolve(this.cursorPos);
  58. let isBlock = !$pos.parent.inlineContent, rect;
  59. if (isBlock) {
  60. let before = $pos.nodeBefore, after = $pos.nodeAfter;
  61. if (before || after) {
  62. let node = this.editorView.nodeDOM(this.cursorPos - (before ? before.nodeSize : 0));
  63. if (node) {
  64. let nodeRect = node.getBoundingClientRect();
  65. let top = before ? nodeRect.bottom : nodeRect.top;
  66. if (before && after)
  67. top = (top + this.editorView.nodeDOM(this.cursorPos).getBoundingClientRect().top) / 2;
  68. rect = { left: nodeRect.left, right: nodeRect.right, top: top - this.width / 2, bottom: top + this.width / 2 };
  69. }
  70. }
  71. }
  72. if (!rect) {
  73. let coords = this.editorView.coordsAtPos(this.cursorPos);
  74. rect = { left: coords.left - this.width / 2, right: coords.left + this.width / 2, top: coords.top, bottom: coords.bottom };
  75. }
  76. let parent = this.editorView.dom.offsetParent;
  77. if (!this.element) {
  78. this.element = parent.appendChild(document.createElement("div"));
  79. if (this.class)
  80. this.element.className = this.class;
  81. this.element.style.cssText = "position: absolute; z-index: 50; pointer-events: none;";
  82. if (this.color) {
  83. this.element.style.backgroundColor = this.color;
  84. }
  85. }
  86. this.element.classList.toggle("prosemirror-dropcursor-block", isBlock);
  87. this.element.classList.toggle("prosemirror-dropcursor-inline", !isBlock);
  88. let parentLeft, parentTop;
  89. if (!parent || parent == document.body && getComputedStyle(parent).position == "static") {
  90. parentLeft = -pageXOffset;
  91. parentTop = -pageYOffset;
  92. }
  93. else {
  94. let rect = parent.getBoundingClientRect();
  95. parentLeft = rect.left - parent.scrollLeft;
  96. parentTop = rect.top - parent.scrollTop;
  97. }
  98. this.element.style.left = (rect.left - parentLeft) + "px";
  99. this.element.style.top = (rect.top - parentTop) + "px";
  100. this.element.style.width = (rect.right - rect.left) + "px";
  101. this.element.style.height = (rect.bottom - rect.top) + "px";
  102. }
  103. scheduleRemoval(timeout) {
  104. clearTimeout(this.timeout);
  105. this.timeout = setTimeout(() => this.setCursor(null), timeout);
  106. }
  107. dragover(event) {
  108. if (!this.editorView.editable)
  109. return;
  110. let pos = this.editorView.posAtCoords({ left: event.clientX, top: event.clientY });
  111. let node = pos && pos.inside >= 0 && this.editorView.state.doc.nodeAt(pos.inside);
  112. let disableDropCursor = node && node.type.spec.disableDropCursor;
  113. let disabled = typeof disableDropCursor == "function" ? disableDropCursor(this.editorView, pos, event) : disableDropCursor;
  114. if (pos && !disabled) {
  115. let target = pos.pos;
  116. if (this.editorView.dragging && this.editorView.dragging.slice) {
  117. let point = dropPoint(this.editorView.state.doc, target, this.editorView.dragging.slice);
  118. if (point != null)
  119. target = point;
  120. }
  121. this.setCursor(target);
  122. this.scheduleRemoval(5000);
  123. }
  124. }
  125. dragend() {
  126. this.scheduleRemoval(20);
  127. }
  128. drop() {
  129. this.scheduleRemoval(20);
  130. }
  131. dragleave(event) {
  132. if (event.target == this.editorView.dom || !this.editorView.dom.contains(event.relatedTarget))
  133. this.setCursor(null);
  134. }
  135. }
  136. export { dropCursor };