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

fixed Caret.isAtStart #2448

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/components/modules/caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ export default class Caret extends Module {
return false;
}

/**
* Workaround case when caret in the text like " |Hello!"
* selection.anchorOffset is 1, but real caret visible position is 0
*
* @type {number}
*/

let firstLetterPosition = focusNode.textContent.search(/\S/);

if (firstLetterPosition === -1) { // empty text
firstLetterPosition = 0;
}

/**
* If caret was set by external code, it might be set to text node wrapper.
Expand Down Expand Up @@ -120,7 +108,7 @@ export default class Caret extends Module {
return $.isEmpty(node) && !isLineBreak;
});

if (nothingAtLeft && focusOffset === firstLetterPosition) {
if (nothingAtLeft && focusOffset === 0) {
return true;
}
}
Expand All @@ -129,7 +117,8 @@ export default class Caret extends Module {
* We use <= comparison for case:
* "| Hello" <--- selection.anchorOffset is 0, but firstLetterPosition is 1
*/
return firstNode === null || (focusNode === firstNode && focusOffset <= firstLetterPosition);

return firstNode === null || (focusNode === firstNode && focusOffset === 0);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/cypress/tests/modules/BlockEvents/Backspace.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ describe('Backspace keydown', function () {
.should('have.text', 'The second bloc'); // last char is removed
});

it('should just delete preceding white spaces (native behaviour) on click of backspace when Caret is not at the start of the Block', function () {
createEditorWithTextBlocks([
'The first block',
'The second block',
]);

cy.get('[data-cy=editorjs]')
.find('.ce-paragraph')
.last()
.click()
.type('{home}')
.type(' ') // adding space at the start of the block
.type('{backspace}');

cy.get('[data-cy=editorjs]')
.find('div.ce-block')
.last()
.should('have.text', `The second block`);

cy.get('[data-cy=editorjs]')
.find('div.ce-block')
.first()
.should('have.text', `The first block`);
});

it('should navigate previous input when Caret is not at the first input', function () {
/**
* Mock of tool with several inputs
Expand Down
26 changes: 26 additions & 0 deletions test/cypress/tests/modules/BlockEvents/Delete.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ describe('Delete keydown', function () {
.should('have.text', 'The first bloc'); // last char is removed
});

it('should just delete preceding white space (native behaviour) when Caret is not at the end of the Block', function () {
createEditorWithTextBlocks([
'The first block',
'The second block',
]);

cy.get('[data-cy=editorjs]')
.find('.ce-paragraph')
.last()
.click()
.type('{home}')
.type(' ') // adding space at the start of the block
.type('{leftarrow}') // now caret is at the start of the block
.type('{del}');

cy.get('[data-cy=editorjs]')
.find('div.ce-block')
.last()
.should('have.text', `The second block`);

cy.get('[data-cy=editorjs]')
.find('div.ce-block')
.first()
.should('have.text', `The first block`);
});

it('should navigate next input when Caret is not at the last input', function () {
/**
* Mock of tool with several inputs
Expand Down
Loading