diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0a85fb772..1fc371f97 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### 2.31.2 + +- `Fix` - Prevent link removal when applying bold to linked text + ### 2.31.1 - `Fix` - Prevent the warning from appearing when `readOnly` mode is initially set to `true` diff --git a/package.json b/package.json index 386aee8c4..1005b9fd6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@editorjs/editorjs", - "version": "2.31.1", + "version": "2.31.2", "description": "Editor.js — open source block-style WYSIWYG editor with JSON output", "main": "dist/editorjs.umd.js", "module": "dist/editorjs.mjs", diff --git a/src/components/inline-tools/inline-tool-link.ts b/src/components/inline-tools/inline-tool-link.ts index 9b413a564..999a30c4c 100644 --- a/src/components/inline-tools/inline-tool-link.ts +++ b/src/components/inline-tools/inline-tool-link.ts @@ -172,11 +172,21 @@ export default class LinkInlineTool implements InlineTool { * Unlink icon pressed */ if (parentAnchor) { - this.selection.expandToTag(parentAnchor); - this.unlink(); - this.closeActions(); - this.checkState(); - this.toolbar.close(); + /** + * If input is not opened, treat click as explicit unlink action. + * If input is opened (e.g., programmatic close when switching tools), avoid unlinking. + */ + if (!this.inputOpened) { + this.selection.expandToTag(parentAnchor); + this.unlink(); + this.closeActions(); + this.checkState(); + this.toolbar.close(); + } else { + /** Only close actions without clearing saved selection to preserve user state */ + this.closeActions(false); + this.checkState(); + } return; } diff --git a/test/cypress/tests/inline-tools/link.cy.ts b/test/cypress/tests/inline-tools/link.cy.ts index 3077e7d82..9b76a7660 100644 --- a/test/cypress/tests/inline-tools/link.cy.ts +++ b/test/cypress/tests/inline-tools/link.cy.ts @@ -71,4 +71,52 @@ describe('Inline Tool Link', () => { .find('.ce-paragraph span[style]') .should('not.exist'); }); + + it('should preserve link when applying bold to linked text', () => { + cy.createEditor({ + data: { + blocks: [ + { + type: 'paragraph', + data: { + text: 'Text with link', + }, + }, + ], + }, + }); + + cy.get('[data-cy=editorjs]') + .find('div.ce-block') + .click() + .type('{selectall}') + .wait(200) + .type('{ctrl}K'); + + cy.get('[data-cy=editorjs]') + .find('.ce-inline-tool-input') + .type('https://editorjs.io') + .type('{enter}'); + + cy.get('[data-cy=editorjs]') + .find('div.ce-block') + .find('a') + .should('have.attr', 'href', 'https://editorjs.io'); + + cy.get('[data-cy=editorjs]') + .find('div.ce-block') + .find('a') + .click() + .type('{selectall}') + .wait(200) + .type('{ctrl}B'); + + cy.get('[data-cy=editorjs]') + .find('div.ce-block') + .find('a') + .should('have.attr', 'href', 'https://editorjs.io') + .find('b') + .should('exist') + .should('contain', 'Text with link'); + }); });