From f0203637d335584601acb425fe874977485557dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A2=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BD=D1=86=D0=B5=D0=B2?= Date: Mon, 19 Feb 2024 19:07:37 +0100 Subject: [PATCH] feat: adding a link to image when pasting a link on selected image node --- src/extensions/markdown/Link/paste-plugin.ts | 38 +++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/extensions/markdown/Link/paste-plugin.ts b/src/extensions/markdown/Link/paste-plugin.ts index b1af43a6..f85942c0 100644 --- a/src/extensions/markdown/Link/paste-plugin.ts +++ b/src/extensions/markdown/Link/paste-plugin.ts @@ -1,8 +1,9 @@ import {Plugin, TextSelection} from 'prosemirror-state'; import type {ExtensionDeps, Parser} from '../../../core'; -import {isTextSelection} from '../../../utils/selection'; +import {isNodeSelection, isTextSelection} from '../../../utils/selection'; import {DataTransferType, isIosSafariShare} from '../../behavior/Clipboard/utils'; +import {imageType} from '../Image'; import {LinkAttr, linkType} from './index'; @@ -13,23 +14,28 @@ export function linkPasteEnhance({parser}: ExtensionDeps) { paste(view, e): boolean { const {state, dispatch} = view; const sel = state.selection; - if (!isTextSelection(sel)) return false; - const {$from, $to} = sel; - if ($from.pos !== $to.pos && $from.sameParent($to)) { - const url = getUrl(e.clipboardData, parser); - if (url) { - const tr = state.tr.addMark( - $from.pos, - $to.pos, - linkType(state.schema).create({ - [LinkAttr.Href]: url, - }), - ); - dispatch(tr.setSelection(TextSelection.create(tr.doc, $to.pos))); - e.preventDefault(); - return true; + if ( + isTextSelection(sel) || + (isNodeSelection(sel) && sel.node.type === imageType(state.schema)) + ) { + const {$from, $to} = sel; + if ($from.pos !== $to.pos && $from.sameParent($to)) { + const url = getUrl(e.clipboardData, parser); + if (url) { + const tr = state.tr.addMark( + $from.pos, + $to.pos, + linkType(state.schema).create({ + [LinkAttr.Href]: url, + }), + ); + dispatch(tr.setSelection(TextSelection.create(tr.doc, $to.pos))); + e.preventDefault(); + return true; + } } } + return false; }, },