fix(LinkInlineTool): improve unlink behavior based on input state (#2979)

* fix(LinkInlineTool): improve unlink behavior based on input state

* 2.31.2-hotfix.0

* fix(linkTool): Add test case to ensure link preservation when applying bold to linked text

* Revert "2.31.2-hotfix.0"

This reverts commit c68ae54c77.

* Add fix entry to changelog

* Bump version

* Revert "Add fix entry to changelog"

This reverts commit 7e537d662a.

* Add fix entry to changelog without formatting

* Refactor test for compatibility with firefox
This commit is contained in:
Alex Gaillard 2026-02-12 10:45:21 -05:00 committed by GitHub
commit 9f942ca72a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 70 additions and 6 deletions

View file

@ -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`

View file

@ -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",

View file

@ -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;
}

View file

@ -71,4 +71,54 @@ 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('.ce-paragraph')
.selectText('Text with link');
cy.get('[data-cy=editorjs]')
.find('[data-item-name=link]')
.click();
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')
.selectText('Text with link');
cy.get('[data-cy=editorjs]')
.find('[data-item-name=bold]')
.click();
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');
});
});