Skip to content

Commit

Permalink
fix: unset link only when removed
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed May 12, 2024
1 parent 041c167 commit c038772
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions projects/ngx-editor/commands/removeLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { EditorState, Transaction, type Command } from 'prosemirror-state';

export const removeLink = (): Command => {
return (state: EditorState, dispatch?: (tr: Transaction) => void): boolean => {
const { doc, selection, tr } = state;
const { doc, selection, tr, schema } = state;
const { $head: { pos }, from, to } = selection;

const linkMark = schema.marks['link'];

// if the cursor is on the link without any selection
if (from === to) {
const $pos = doc.resolve(pos);
const linkStart = pos - $pos.textOffset;
const linkEnd = linkStart + $pos.parent.child($pos.index()).nodeSize;

tr.removeMark(linkStart, linkEnd);
tr.removeMark(linkStart, linkEnd, linkMark);
} else {
tr.removeMark(from, to);
tr.removeMark(from, to, linkMark);
}

if (!tr.docChanged) {
Expand Down

0 comments on commit c038772

Please sign in to comment.