X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/f3fa63a5ae5d671d10c9313965723683608ddc4e..refs/pull/5689/head:/resources/js/wysiwyg/services/drop-paste-handling.ts diff --git a/resources/js/wysiwyg/services/drop-paste-handling.ts b/resources/js/wysiwyg/services/drop-paste-handling.ts index e049d5e7c..57f9a80ae 100644 --- a/resources/js/wysiwyg/services/drop-paste-handling.ts +++ b/resources/js/wysiwyg/services/drop-paste-handling.ts @@ -8,7 +8,7 @@ import { import {$insertNewBlockNodesAtSelection, $selectSingleNode} from "../utils/selection"; import {$getNearestBlockNodeForCoords, $htmlToBlockNodes} from "../utils/nodes"; import {Clipboard} from "../../services/clipboard"; -import {$createImageNode} from "../nodes/image"; +import {$createImageNode} from "@lexical/rich-text/LexicalImageNode"; import {$createLinkNode} from "@lexical/link"; import {EditorImageData, uploadImageFile} from "../utils/images"; import {EditorUiContext} from "../ui/framework/core"; @@ -95,6 +95,21 @@ function handleMediaInsert(data: DataTransfer, context: EditorUiContext): boolea 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 => { @@ -138,7 +153,10 @@ function createPasteListener(context: EditorUiContext): (event: ClipboardEvent) return false; } - const handled = handleMediaInsert(event.clipboardData, context); + const handled = + handleImageLinkInsert(event.clipboardData, context) || + handleMediaInsert(event.clipboardData, context); + if (handled) { event.preventDefault(); }