]> BookStack Code Mirror - bookstack/commitdiff
Lexical: Added image insert via image link paste
authorDan Brown <redacted>
Mon, 26 May 2025 17:02:53 +0000 (18:02 +0100)
committerDan Brown <redacted>
Mon, 26 May 2025 17:02:53 +0000 (18:02 +0100)
Specifically added to align with existing TinyMCE behaviour which was
used by some users based upon new editor feedback.

resources/js/wysiwyg/services/drop-paste-handling.ts

index 2ee831d74fc0e541a7f592dcbcfbc5bfd6a94914..57f9a80ae18fa8076cc1288c31b61eebb9f6c7fa 100644 (file)
@@ -95,6 +95,21 @@ function handleMediaInsert(data: DataTransfer, context: EditorUiContext): boolea
     return handled;
 }
 
     return handled;
 }
 
+function handleImageLinkInsert(data: DataTransfer, context: EditorUiContext): boolean {
+    const regex = /https?:\/\/([^?#]*?)\.(png|jpeg|jpg|gif|webp|bmp|avif)/i
+    const text = data.getData('text/plain');
+    if (text && regex.test(text)) {
+        context.editor.update(() => {
+            const image = $createImageNode(text);
+            $insertNodes([image]);
+            image.select();
+        });
+        return true;
+    }
+
+    return false;
+}
+
 function createDropListener(context: EditorUiContext): (event: DragEvent) => boolean {
     const editor = context.editor;
     return (event: DragEvent): boolean => {
 function createDropListener(context: EditorUiContext): (event: DragEvent) => boolean {
     const editor = context.editor;
     return (event: DragEvent): boolean => {
@@ -138,7 +153,10 @@ function createPasteListener(context: EditorUiContext): (event: ClipboardEvent)
             return false;
         }
 
             return false;
         }
 
-        const handled = handleMediaInsert(event.clipboardData, context);
+        const handled =
+            handleImageLinkInsert(event.clipboardData, context) ||
+            handleMediaInsert(event.clipboardData, context);
+
         if (handled) {
             event.preventDefault();
         }
         if (handled) {
             event.preventDefault();
         }