Skip to content

Commit 9f942ca

Browse files
authored
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 c68ae54. * Add fix entry to changelog * Bump version * Revert "Add fix entry to changelog" This reverts commit 7e537d6. * Add fix entry to changelog without formatting * Refactor test for compatibility with firefox
1 parent 90d6dec commit 9f942ca

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 2.31.2
4+
5+
- `Fix` - Prevent link removal when applying bold to linked text
6+
37
### 2.31.1
48

59
- `Fix` - Prevent the warning from appearing when `readOnly` mode is initially set to `true`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/editorjs",
3-
"version": "2.31.1",
3+
"version": "2.31.2",
44
"description": "Editor.js — open source block-style WYSIWYG editor with JSON output",
55
"main": "dist/editorjs.umd.js",
66
"module": "dist/editorjs.mjs",

src/components/inline-tools/inline-tool-link.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,21 @@ export default class LinkInlineTool implements InlineTool {
172172
* Unlink icon pressed
173173
*/
174174
if (parentAnchor) {
175-
this.selection.expandToTag(parentAnchor);
176-
this.unlink();
177-
this.closeActions();
178-
this.checkState();
179-
this.toolbar.close();
175+
/**
176+
* If input is not opened, treat click as explicit unlink action.
177+
* If input is opened (e.g., programmatic close when switching tools), avoid unlinking.
178+
*/
179+
if (!this.inputOpened) {
180+
this.selection.expandToTag(parentAnchor);
181+
this.unlink();
182+
this.closeActions();
183+
this.checkState();
184+
this.toolbar.close();
185+
} else {
186+
/** Only close actions without clearing saved selection to preserve user state */
187+
this.closeActions(false);
188+
this.checkState();
189+
}
180190

181191
return;
182192
}

test/cypress/tests/inline-tools/link.cy.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,54 @@ describe('Inline Tool Link', () => {
7171
.find('.ce-paragraph span[style]')
7272
.should('not.exist');
7373
});
74+
75+
it('should preserve link when applying bold to linked text', () => {
76+
cy.createEditor({
77+
data: {
78+
blocks: [
79+
{
80+
type: 'paragraph',
81+
data: {
82+
text: 'Text with link',
83+
},
84+
},
85+
],
86+
},
87+
});
88+
89+
cy.get('[data-cy=editorjs]')
90+
.find('.ce-paragraph')
91+
.selectText('Text with link');
92+
93+
cy.get('[data-cy=editorjs]')
94+
.find('[data-item-name=link]')
95+
.click();
96+
97+
cy.get('[data-cy=editorjs]')
98+
.find('.ce-inline-tool-input')
99+
.type('https://editorjs.io')
100+
.type('{enter}');
101+
102+
cy.get('[data-cy=editorjs]')
103+
.find('div.ce-block')
104+
.find('a')
105+
.should('have.attr', 'href', 'https://editorjs.io');
106+
107+
cy.get('[data-cy=editorjs]')
108+
.find('div.ce-block')
109+
.find('a')
110+
.selectText('Text with link');
111+
112+
cy.get('[data-cy=editorjs]')
113+
.find('[data-item-name=bold]')
114+
.click();
115+
116+
cy.get('[data-cy=editorjs]')
117+
.find('div.ce-block')
118+
.find('a')
119+
.should('have.attr', 'href', 'https://editorjs.io')
120+
.find('b')
121+
.should('exist')
122+
.should('contain', 'Text with link');
123+
});
74124
});

0 commit comments

Comments
 (0)