Skip to content

Commit 530ec56

Browse files
akulistusneSpecc
andauthored
fix(link-tool): open new window with url when formatted link clicked via ctrl key (#2996)
* fix(link-tool): open new window with url when formatted link clicked via ctrl key * add test * fix lint * bump version and add changelog * Apply suggestions from code review Co-authored-by: Peter <specc.dev@gmail.com> Co-authored-by: KoshaevEugeny <103786108+akulistus@users.noreply.github.com> * Update test/cypress/tests/inline-tools/link.cy.ts Co-authored-by: Peter <specc.dev@gmail.com> --------- Co-authored-by: Peter <specc.dev@gmail.com>
1 parent b69aa1e commit 530ec56

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
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.5
4+
5+
- `Fix` - Handle __Ctrl + click__ on links with inline styles applied (e.g., bold, italic)
6+
37
### 2.31.4
48

59
- `Fix` - Prevent inline-toolbar re-renders when linked text is selected

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.4",
3+
"version": "2.31.5",
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/dom.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,16 @@ export default class Dom {
566566
return element.tagName.toLowerCase() === 'a';
567567
}
568568

569+
/**
570+
* Returns the closest ancestor anchor (A tag) of the given element (including itself)
571+
*
572+
* @param element - element to check
573+
* @returns {HTMLAnchorElement | null}
574+
*/
575+
public static getClosestAnchor(element: Element): HTMLAnchorElement | null {
576+
return element.closest("a");
577+
}
578+
569579
/**
570580
* Return element's offset related to the document
571581
*

src/components/modules/ui.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,13 @@ export default class UI extends Module<UINodes> {
773773
*/
774774
const element = event.target as Element;
775775
const ctrlKey = event.metaKey || event.ctrlKey;
776-
777-
if ($.isAnchor(element) && ctrlKey) {
776+
const anchor = $.getClosestAnchor(element);
777+
778+
if (anchor && ctrlKey) {
778779
event.stopImmediatePropagation();
779780
event.stopPropagation();
780781

781-
const href = element.getAttribute('href');
782+
const href = anchor.getAttribute('href');
782783
const validUrl = _.getValidUrl(href);
783784

784785
_.openTab(validUrl);

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,50 @@ describe('Inline Tool Link', () => {
192192
.should('have.attr', 'href', 'https://editorjs.io')
193193
.should('contain', 'Bold and italic text');
194194
});
195+
196+
it('should open a link if it is wrapped in another formatting', () => {
197+
cy.createEditor({
198+
data: {
199+
blocks: [
200+
{
201+
type: 'paragraph',
202+
data: {
203+
text: 'Link text',
204+
},
205+
},
206+
],
207+
},
208+
});
209+
210+
cy.get('[data-cy=editorjs]')
211+
.find('.ce-paragraph')
212+
.selectText('Link text');
213+
214+
cy.get('[data-cy=editorjs]')
215+
.find('[data-item-name=link]')
216+
.click();
217+
218+
cy.get('[data-cy=editorjs]')
219+
.find('.ce-inline-tool-input')
220+
.type('https://test.io/')
221+
.type('{enter}');
222+
223+
cy.get('[data-cy=editorjs]')
224+
.find('div.ce-block')
225+
.find('a')
226+
.selectText('Link text');
227+
228+
cy.get('[data-cy=editorjs]')
229+
.find('[data-item-name=italic]')
230+
.click();
231+
232+
cy.window().then((win) => {
233+
cy.stub(win, 'open').as('windowOpen');
234+
});
235+
236+
cy.contains('[data-cy=editorjs] div.ce-block i', 'Link text')
237+
.click({ ctrlKey: true });
238+
239+
cy.get('@windowOpen').should('be.calledWith', 'https://test.io/');
240+
});
195241
});

0 commit comments

Comments
 (0)