]> BookStack Code Mirror - bookstack/commitdiff
Attachments: Fixed drag into editor in Chrome
authorDan Brown <redacted>
Mon, 29 Apr 2024 18:21:13 +0000 (19:21 +0100)
committerDan Brown <redacted>
Mon, 29 Apr 2024 18:21:13 +0000 (19:21 +0100)
Seemed to be chrome specific from testing.
Required editors to have preventDefault called on dragover.
Tested in Chrome, FF, & Safari.
Tested in both editors, and re-tested text/image drop to ensure still
works.

Fixed #4975

resources/js/markdown/codemirror.js
resources/js/wysiwyg/drop-paste-handling.js

index 9d54c19d754b52b6489bc9aad5bdb7299f4d8542..2ea944865c12d90768956594364eb8901960eb56 100644 (file)
@@ -44,6 +44,10 @@ export async function init(editor) {
                 editor.actions.insertClipboardImages(clipboardImages, event.pageX, event.pageY);
             }
         },
+        // Handle dragover event to allow as drop-target in chrome
+        dragover: event => {
+            event.preventDefault();
+        },
         // Handle image paste
         paste: event => {
             const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
index 9668692c81d1e162cf6f73d954c6cd307bd29735..172ad970f0de8a5e51f495a012b88bf157107b85 100644 (file)
@@ -149,11 +149,26 @@ function drop(editor, options, event) {
     wrap = null;
 }
 
+/**
+ * @param {Editor} editor
+ * @param {DragEvent} event
+ */
+function dragOver(editor, event) {
+    // This custom handling essentially emulates the default TinyMCE behaviour while allowing us
+    // to specifically call preventDefault on the event to allow the drop of custom elements.
+    event.preventDefault();
+    editor.focus();
+    const rangeUtils = window.tinymce.dom.RangeUtils;
+    const range = rangeUtils.getCaretRangeFromPoint(event.clientX ?? 0, event.clientY ?? 0, editor.getDoc());
+    editor.selection.setRng(range);
+}
+
 /**
  * @param {Editor} editor
  * @param {WysiwygConfigOptions} options
  */
 export function listenForDragAndPaste(editor, options) {
+    editor.on('dragover', event => dragOver(editor, event));
     editor.on('dragstart', () => dragStart(editor));
     editor.on('drop', event => drop(editor, options, event));
     editor.on('paste', event => paste(editor, options, event));