Repository navigation
Conversation
| * Caret is at the start of a line inside a contenteditable, and there is another line before it, | ||
| * for example in a Tool with line breaks enabled. Leave native behaviour to remove the line break | ||
| */ | ||
| if (hasContentNextToLineInContentEditable(currentBlock.currentInput, 'left')) { |
There was a problem hiding this comment.
This kicks in for any Tool whose input is a block inside a contenteditable, not only Tools with line breaks. A Tool with several block "fields" under one contenteditable would now get them merged natively instead of moving the caret between them. Could we gate it with currentBlock.tool.isLineBreaksEnabled? That matches #2854 exactly. Also, isVisible should skip contenteditable="false" siblings: Chrome deletes a whole non-editable node on native Backspace.
| { | ||
| "name": "@editorjs/editorjs", | ||
| "version": "2.32.0-rc.3", | ||
| "version": "2.32.0-rc.4", |
There was a problem hiding this comment.
Please drop the version bump — we bump it on release, and next is already at 2.32.0-rc.5, which is what conflicts here. After rebasing, keep the CHANGELOG line at the end of the 2.32.0 section.
| .should('have.text', 'The second bloc'); // last char is removed | ||
| }); | ||
|
|
||
| describe('in a Tool with line breaks enabled', function () { |
There was a problem hiding this comment.
Could you add cases for a bare first text line (abc<div>def</div> — that's what Chrome actually produces), a formatted line (<div><b>de</b>f</div>), whitespace text nodes between line divs, and a Tool without enableLineBreaks that keeps the old navigation? The ToolWithLineBreaks mock is duplicated in both specs — worth moving to a shared fixture.
| * @param direction - 'left' to look for content before the input, 'right' to look for content after it | ||
| * @returns true if there is visible content in the same contenteditable in the passed direction | ||
| */ | ||
| export function hasContentNextToLineInContentEditable(input: HTMLElement | undefined, direction: 'left' | 'right'): boolean { |
There was a problem hiding this comment.
The name is a bit confusing: "next" reads as the following line only (the helper also checks the previous one), and "content" doesn't say what exactly is being looked for. Maybe hasNonEmptySibling(input, direction)?
isVisible needs a JSDoc too: it's not obvious why a <br> or an empty block element counts as visible.
Also, did you consider reusing checkContenteditableSliceForEmptiness from utils/caret.ts instead of the two loops? It already checks everything to the left/right inside a contenteditable via a Range, so there's no need to walk siblings level by level. Something like:
static hasNonEmptySibling(input: HTMLElement, direction: 'left' | 'right'): boolean {
const root = input.parentElement?.closest('[contenteditable="true"]');
if (!root) {
return false;
}
const offset = direction === 'left' ? 0 : input.childNodes.length;
return !checkContenteditableSliceForEmptiness(root, input, offset, direction);
}The catch is that it only looks at text, so an empty line (<div><br></div>) would count as empty. It'd need a small extension, e.g. an option to treat <br> and block elements in the slice as non-empty — then isVisible isn't needed at all. Either way, it'd be nice to keep one way of doing this. And the helper might fit better as a static Dom method, next to findAllInputs.
There was a problem hiding this comment.
On second thought, we don't even need a separate helper — just extend checkContenteditableSliceForEmptiness and call it directly in blockEvents.ts, otherwise we'd have two functions answering the same question.
…ols with line breaks In a Tool with enableLineBreaks, browsers wrap each line created by Enter in a <div>. Dom.findAllInputs() lists those divs as separate inputs, so Backspace at the start of a line was treated as "caret at the start of a non-first input" and moved the caret to the previous line instead of removing the line break. Delete had the same problem at the end of a line. Leave the native behaviour when there is other content before (or after) the line in the same contenteditable. The first line and the last line keep the existing Block behaviour.
…bleLineBreaks Apply the native line break removal only when the Tool enables line breaks. Tools without it keep treating each line as a separate input, so Backspace and Delete still move the Caret between lines there. Check the content before (or after) the line with checkContenteditableSliceForEmptiness() instead of a separate DOM helper. With countLineBreaks it also counts <br> and non-empty block elements, and ignores contenteditable="false" nodes. Cover a bare first text line, formatted lines and whitespace text nodes between lines, and check that a Tool without line breaks still handles the key itself.
48cf9f4 to
1b665f5
Compare
|
Thanks for the detailed review! Reworked it:
One note on the "without |
Fixes #2854. The same bug is reported in #2730, #2685 and #2609.
In a Tool with
enableLineBreaks(Quote, or a custom contenteditable Tool), each Enter makes the browser wrap the new line in a<div>, andDom.findAllInputs()lists every one of those divs as a separate input. So Backspace at the start of any line after the first is caught byBlockEvents.backspace(), which callsCaret.navigatePrevious(): the caret jumps up a line and the line break stays. Delete does the same at the end of a line (navigateNext()). Repro on the editorjs.io demo: add a Quote, type something, press Enter a few times, then Backspace.backspace()anddelete()now leave the key to the browser when the caret's input is a line inside a contenteditable that has visible content (text, a<br>or another line) before it, for Backspace, or after it, for Delete. At the start of the first line and the end of the last line the Block behaviour is unchanged, and separate inputs like the Quote text and caption are not affected.New cases in
Backspace.cy.tsandDelete.cy.tsuse a Tool withenableLineBreaks. The four that remove an empty line or join two lines fail onnextand pass with this change; the two that check the first/last line Block behaviour pass on both. The full Cypress suite passes in Chrome.