X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/fd07aa0f05389055840a3d41c19cdfc1c81b4d0b..refs/pull/5681/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 07e35d443..57f9a80ae 100644 --- a/resources/js/wysiwyg/services/drop-paste-handling.ts +++ b/resources/js/wysiwyg/services/drop-paste-handling.ts @@ -1,4 +1,5 @@ import { + $createParagraphNode, $insertNodes, $isDecoratorNode, COMMAND_PRIORITY_HIGH, DROP_COMMAND, LexicalEditor, @@ -7,8 +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 {$createCustomParagraphNode} from "../nodes/custom-paragraph"; +import {$createImageNode} from "@lexical/rich-text/LexicalImageNode"; import {$createLinkNode} from "@lexical/link"; import {EditorImageData, uploadImageFile} from "../utils/images"; import {EditorUiContext} from "../ui/framework/core"; @@ -67,7 +67,7 @@ function handleMediaInsert(data: DataTransfer, context: EditorUiContext): boolea for (const imageFile of images) { const loadingImage = window.baseUrl('/loading.gif'); const loadingNode = $createImageNode(loadingImage); - const imageWrap = $createCustomParagraphNode(); + const imageWrap = $createParagraphNode(); imageWrap.append(loadingNode); $insertNodes([imageWrap]); @@ -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(); }