fix(widget-markdown): properly check for selection when inserting links (#4075)
This commit is contained in:
parent
04ccb56e66
commit
a4b7481a99
72
cypress/integration/markdown_widget_link_spec.js
Normal file
72
cypress/integration/markdown_widget_link_spec.js
Normal file
@ -0,0 +1,72 @@
|
||||
import '../utils/dismiss-local-backup';
|
||||
|
||||
describe('Markdown widget link', () => {
|
||||
before(() => {
|
||||
Cypress.config('defaultCommandTimeout', 4000);
|
||||
cy.task('setupBackend', { backend: 'test' });
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
cy.clearLocalStorage();
|
||||
cy.reload();
|
||||
cy.loginAndNewPost();
|
||||
cy.clearMarkdownEditorContent();
|
||||
});
|
||||
|
||||
after(() => {
|
||||
cy.task('teardownBackend', { backend: 'test' });
|
||||
});
|
||||
|
||||
describe('pressing backspace', () => {
|
||||
it('can add a new valid link', () => {
|
||||
const link = 'https://www.netlifycms.org/';
|
||||
cy.window().then(win => {
|
||||
cy.stub(win, 'prompt').returns(link);
|
||||
});
|
||||
cy.focused().clickLinkButton();
|
||||
|
||||
cy.confirmMarkdownEditorContent(`<p><a>${link}</a></p>`);
|
||||
// eslint-disable-next-line cypress/no-unnecessary-waiting
|
||||
cy.wait(300);
|
||||
cy.clickModeToggle();
|
||||
|
||||
cy.confirmRawEditorContent(`<${link}>`);
|
||||
});
|
||||
|
||||
it('can add a new invalid link', () => {
|
||||
const link = 'www.netlifycms.org';
|
||||
cy.window().then(win => {
|
||||
cy.stub(win, 'prompt').returns(link);
|
||||
});
|
||||
cy.focused().clickLinkButton();
|
||||
|
||||
cy.confirmMarkdownEditorContent(`<p><a>${link}</a></p>`);
|
||||
// eslint-disable-next-line cypress/no-unnecessary-waiting
|
||||
cy.wait(300);
|
||||
cy.clickModeToggle();
|
||||
|
||||
cy.confirmRawEditorContent(`[${link}](${link})`);
|
||||
});
|
||||
|
||||
it('can select existing text as link', () => {
|
||||
const link = 'https://www.netlifycms.org';
|
||||
cy.window().then(win => {
|
||||
cy.stub(win, 'prompt').returns(link);
|
||||
});
|
||||
|
||||
const text = 'Netlify CMS';
|
||||
cy.focused()
|
||||
.getMarkdownEditor()
|
||||
.type(text)
|
||||
.setSelection(text)
|
||||
.clickLinkButton();
|
||||
|
||||
cy.confirmMarkdownEditorContent(`<p><a>${text}</a></p>`);
|
||||
// eslint-disable-next-line cypress/no-unnecessary-waiting
|
||||
cy.wait(300);
|
||||
cy.clickModeToggle();
|
||||
|
||||
cy.confirmRawEditorContent(`[${text}](${link})`);
|
||||
});
|
||||
});
|
||||
});
|
@ -268,6 +268,7 @@ Cypress.Commands.add('insertEditorComponent', title => {
|
||||
['clickCodeButton', 'Code'],
|
||||
['clickItalicButton', 'Italic'],
|
||||
['clickQuoteButton', 'Quote'],
|
||||
['clickLinkButton', 'Link'],
|
||||
].forEach(([commandName, toolbarButtonName]) => {
|
||||
Cypress.Commands.add(commandName, opts => {
|
||||
return cy.clickToolbarButton(toolbarButtonName, opts);
|
||||
@ -316,6 +317,12 @@ Cypress.Commands.add('clearMarkdownEditorContent', () => {
|
||||
.backspace({ times: 2 });
|
||||
});
|
||||
|
||||
Cypress.Commands.add('confirmRawEditorContent', expectedDomString => {
|
||||
cy.get('.cms-editor-raw').within(() => {
|
||||
cy.contains('span', expectedDomString);
|
||||
});
|
||||
});
|
||||
|
||||
function toPlainTree(domString) {
|
||||
return rehype()
|
||||
.use(removeSlateArtifacts)
|
||||
|
@ -7,13 +7,19 @@ const Link = ({ type }) => ({
|
||||
const url = getUrl();
|
||||
if (!url) return;
|
||||
|
||||
const selection = editor.value.selection;
|
||||
const isCollapsed = selection && selection.isCollapsed;
|
||||
if (isCollapsed) {
|
||||
// If no text is selected, use the entered URL as text.
|
||||
if (editor.value.isCollapsed) {
|
||||
editor.insertText(url).moveFocusBackward(0 - url.length);
|
||||
}
|
||||
|
||||
return editor.insertInline({
|
||||
type,
|
||||
data: { url },
|
||||
nodes: [{ object: 'text', text: url }],
|
||||
});
|
||||
} else {
|
||||
return editor.wrapInline({ type, data: { url } }).moveToEnd();
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user