diff --git a/blocks/edit/prose/plugins/linkConverter.js b/blocks/edit/prose/plugins/linkConverter.js index e977c8ce..4d105cb7 100644 --- a/blocks/edit/prose/plugins/linkConverter.js +++ b/blocks/edit/prose/plugins/linkConverter.js @@ -25,11 +25,22 @@ export default function linkConverter(schema) { const linkMark = schema.marks.link.create( { href: slice.content.content[0].content.content[0].text }, ); - const { from } = view.state.selection; + const { from, to } = view.state.selection; const { size } = slice.content.content[0].content; + const selectedSlice = view.state.doc.slice(from, to); + + // if we have some text selected, add the link to the selected text + if (selectedSlice.size > 0 + && selectedSlice.content.content.length === 1 + && selectedSlice.content.content[0].type.name === 'text') { + const addLinkMark = view.state.tr.addMark(from, to, linkMark); + view.dispatch(addLinkMark); + return true; + } + const addLinkMark = view.state.tr - .insert(from, slice.content.content[0].content) + .replaceWith(from, to, slice.content.content[0].content) .addMark(from, from + size, linkMark); view.dispatch(addLinkMark); diff --git a/blocks/edit/prose/plugins/menu/linkItem.js b/blocks/edit/prose/plugins/menu/linkItem.js index c5d7978b..e5f8dd96 100644 --- a/blocks/edit/prose/plugins/menu/linkItem.js +++ b/blocks/edit/prose/plugins/menu/linkItem.js @@ -247,10 +247,15 @@ export function linkItem(linkMarkType) { title: label, fields: promptFieldsConfiguration, callback, - saveOnClose: true, + saveOnClose: false, useLabelsAbove: true, }; linkPromptState.lastPrompt = openPrompt(promptOptions); + linkPromptState.lastPrompt.addEventListener('closed', () => { + const { tr } = view.state; + tr.removeMark(currentRangeStart, currentRangeEnd, contextHighlightingMarkType); + dispatch(tr); + }); }, }); } diff --git a/test/unit/blocks/edit/prose/plugins/linkConverter.test.js b/test/unit/blocks/edit/prose/plugins/linkConverter.test.js index 67f47c2c..2949d213 100644 --- a/test/unit/blocks/edit/prose/plugins/linkConverter.test.js +++ b/test/unit/blocks/edit/prose/plugins/linkConverter.test.js @@ -60,13 +60,24 @@ describe('Link converter plugin', () => { addedMark = mark; return this; }, + replaceWith: function replaceWith() { + return this; + }, + }; + + const mockDoc = { + slice: () => ({ + size: 0, + content: { content: [] }, + }), }; const mockView = { state: { - selection: { from: 10 }, + selection: { from: 10, to: 10 }, schema: baseSchema, tr, + doc: mockDoc, }, dispatch: () => {}, }; @@ -191,13 +202,24 @@ describe('Link converter plugin', () => { addedMark = mark; return this; }, + replaceWith: function replaceWith() { + return this; + }, + }; + + const mockDoc = { + slice: () => ({ + size: 0, + content: { content: [] }, + }), }; const mockView = { state: { - selection: { from: 10 }, + selection: { from: 10, to: 10 }, schema: baseSchema, tr, + doc: mockDoc, }, dispatch: () => {}, }; @@ -230,13 +252,24 @@ describe('Link converter plugin', () => { addedMark = mark; return this; }, + replaceWith: function replaceWith() { + return this; + }, + }; + + const mockDoc = { + slice: () => ({ + size: 0, + content: { content: [] }, + }), }; const mockView = { state: { - selection: { from: 10 }, + selection: { from: 10, to: 10 }, schema: baseSchema, tr, + doc: mockDoc, }, dispatch: () => {}, };