Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions blocks/edit/prose/plugins/linkConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 6 additions & 1 deletion blocks/edit/prose/plugins/menu/linkItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},
});
}
Expand Down
39 changes: 36 additions & 3 deletions test/unit/blocks/edit/prose/plugins/linkConverter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {},
};
Expand Down Expand Up @@ -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: () => {},
};
Expand Down Expand Up @@ -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: () => {},
};
Expand Down
Loading