Skip to content

Commit

Permalink
feat: adding a link to image when pasting a link on selected image no…
Browse files Browse the repository at this point in the history
…de (#194)
  • Loading branch information
smsochneg authored Feb 20, 2024
1 parent c7f882d commit ecb91e1
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/extensions/markdown/Link/paste-plugin.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
},
},
Expand Down

0 comments on commit ecb91e1

Please sign in to comment.