Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix For When "/" Overides external text #2894

6 changes: 6 additions & 0 deletions src/components/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ export default class UI extends Module<UINodes> {
* @param {KeyboardEvent} event - keyboard event
*/
private documentKeydown(event: KeyboardEvent): void {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
const wasEventTriggeredInsideEditor = this.nodes.wrapper.contains(event.target as Node);

if (!wasEventTriggeredInsideEditor && !this.someToolbarOpened) {
return;
}

switch (event.keyCode) {
case _.keyCodes.ENTER:
this.enterPressed(event);
Expand Down
72 changes: 72 additions & 0 deletions test/cypress/tests/modules/Ui.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,78 @@ import type EditorJS from '../../../../types/index';

describe('Ui module', function () {
describe('documentKeydown', function () {
describe('Events outside editor', function () {
it('should ignore keyboard events when target is outside editor', function () {
// Create editor with a paragraph block
cy.createEditor({
data: {
blocks: [
{
type: 'paragraph',
data: {
text: 'Editor content',
},
},
],
},
});

// Create a contenteditable div outside the editor
cy.get('[data-cy=editorjs]')
.parent()
.then(($parent) => {
const outsideDiv = document.createElement('div');

outsideDiv.contentEditable = 'true';
outsideDiv.textContent = 'Text outside editor';
outsideDiv.setAttribute('data-cy', 'outside-editor');
$parent.append(outsideDiv);
});

// Select text outside editor and press slash
cy.get('[data-cy=outside-editor]')
.type('{selectall}') // Select all text in the outside div
.trigger('keydown', { key: '/' }); // Trigger slash key

// Verify the text outside editor wasn't changed
cy.get('[data-cy=outside-editor]')
.should('have.text', 'Text outside editor');

// Verify editor content wasn't affected
cy.get('[data-cy=editorjs]')
.find('.ce-paragraph')
.should('have.text', 'Editor content');
});

it('should handle keyboard events when target is inside editor', function () {
// Create editor with a paragraph block
cy.createEditor({
data: {
blocks: [
{
type: 'paragraph',
data: {
text: 'Editor content',
},
},
],
},
});

// Select text inside editor and press slash
cy.get('[data-cy=editorjs]')
.find('.ce-paragraph')
.click()
.type('{selectall}') // Select all text in the paragraph
.type('/'); // Type slash directly instead of triggering keydown

// Verify the text inside editor was changed
cy.get('[data-cy=editorjs]')
.find('.ce-paragraph')
.should('have.text', '/');
});
});

describe('Backspace', function () {
it('should remove selected blocks', function () {
cy.createEditor({
Expand Down
Loading