Skip to content

Commit

Permalink
Merge branch 'main' into feat/toolbars-common-config
Browse files Browse the repository at this point in the history
  • Loading branch information
makhnatkin authored Dec 19, 2024
2 parents f0524e0 + bd52bee commit 471ffe6
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/extensions/markdown/Link/paste-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Plugin, TextSelection} from 'prosemirror-state';
import {Plugin, TextSelection, type Transaction} from 'prosemirror-state';

import type {ExtensionDeps, Parser} from '../../../core';
import {isNodeSelection, isTextSelection} from '../../../utils/selection';
Expand All @@ -14,28 +14,48 @@ export function linkPasteEnhance({markupParser: parser}: ExtensionDeps) {
paste(view, e): boolean {
const {state, dispatch} = view;
const sel = state.selection;
let tr: Transaction | null = null;

if (
isTextSelection(sel) ||
(isNodeSelection(sel) && sel.node.type === imageType(state.schema))
) {
const {$from, $to} = sel;
if ($from.pos !== $to.pos && $from.sameParent($to)) {
if ($from.pos === $to.pos) {
const url = getUrl(e.clipboardData, parser);
if (url) {
const tr = state.tr.addMark(
const linkMarkType = linkType(state.schema);
tr = state.tr.replaceSelectionWith(
state.schema.text(url, [
...$from
.marks()
.filter((mark) => mark.type !== linkMarkType),
linkMarkType.create({[LinkAttr.Href]: url}),
]),
false,
);
}
} else if ($from.sameParent($to)) {
const url = getUrl(e.clipboardData, parser);
if (url) {
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;
tr.setSelection(TextSelection.create(tr.doc, $to.pos));
}
}
}

if (tr) {
dispatch(tr.scrollIntoView());
e.preventDefault();
return true;
}

return false;
},
},
Expand Down

0 comments on commit 471ffe6

Please sign in to comment.