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

fix: properly handle visible whitespaces in empty blocks #2865

Open
wants to merge 9 commits into
base: next
Choose a base branch
from
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `Fix` - Several toolbox items exported by the one tool have the same shortcut displayed in toolbox
- `Improvement` - The current block reference will be updated in read-only mode when blocks are clicked
- `Fix` - codex-notifier and codex-tooltip moved from devDependencies to dependencies in package.json to solve type errors
- `Fix` - Handle whitespace input in empty placeholder elements to prevent caret from moving unexpectedly to the end of the placeholder

### 2.30.6

Expand Down
1 change: 0 additions & 1 deletion example/tools/code
neSpecc marked this conversation as resolved.
Outdated
Show resolved Hide resolved
Submodule code deleted from f28199
1 change: 0 additions & 1 deletion example/tools/delimiter
Submodule delimiter deleted from 4ca1c1
1 change: 0 additions & 1 deletion example/tools/embed
Submodule embed deleted from f2585a
1 change: 0 additions & 1 deletion example/tools/header
Submodule header deleted from 477853
1 change: 0 additions & 1 deletion example/tools/image
Submodule image deleted from 25d46c
1 change: 0 additions & 1 deletion example/tools/inline-code
Submodule inline-code deleted from dcd4c1
1 change: 0 additions & 1 deletion example/tools/list
Submodule list deleted from a6dc6a
1 change: 0 additions & 1 deletion example/tools/quote
Submodule quote deleted from 9377ca
1 change: 0 additions & 1 deletion example/tools/table
Submodule table deleted from 2948cd
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/editorjs",
"version": "2.31.0-rc.4",
"version": "2.31.0-rc.5",
"description": "Editor.js — open source block-style WYSIWYG editor with JSON output",
"main": "dist/editorjs.umd.js",
"module": "dist/editorjs.mjs",
Expand Down
21 changes: 16 additions & 5 deletions src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class Dom {
public static swap(el1: HTMLElement, el2: HTMLElement): void {
// create marker element and insert it where el1 is
const temp = document.createElement('div'),
parent = el1.parentNode;
parent = el1.parentNode;

parent.insertBefore(temp, el1);

Expand Down Expand Up @@ -225,7 +225,7 @@ export default class Dom {
* @type {string}
*/
const child = atLast ? 'lastChild' : 'firstChild',
sibling = atLast ? 'previousSibling' : 'nextSibling';
sibling = atLast ? 'previousSibling' : 'nextSibling';

if (node && node.nodeType === Node.ELEMENT_NODE && node[child]) {
let nodeChild = node[child] as Node;
Expand Down Expand Up @@ -405,7 +405,7 @@ export default class Dom {
*/
node.normalize();

const treeWalker = [ node ];
const treeWalker = [node];

while (treeWalker.length > 0) {
node = treeWalker.shift();
Expand Down Expand Up @@ -539,7 +539,7 @@ export default class Dom {
*/
public static getDeepestBlockElements(parent: HTMLElement): HTMLElement[] {
if (Dom.containsOnlyInlineElements(parent)) {
return [ parent ];
return [parent];
}

return Array.from(parent.children).reduce((result, element) => {
Expand Down Expand Up @@ -670,5 +670,16 @@ export function calculateBaseline(element: Element): number {
* @param element - The element to toggle the [data-empty] attribute on
*/
export function toggleEmptyMark(element: HTMLElement): void {
element.dataset.empty = Dom.isEmpty(element) ? 'true' : 'false';
const isOnlyWhitespace = isCollapsedWhitespaces(element.textContent || '');

const isElementEmpty = Dom.isEmpty(element) && isOnlyWhitespace;

element.dataset.empty = isElementEmpty ? 'true' : 'false';

if (isElementEmpty) {
const lastChild = element.lastChild;
if (lastChild && lastChild.nodeType === Node.ELEMENT_NODE && (lastChild as HTMLElement).tagName === 'BR') {
element.removeChild(lastChild);
}
}
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
}
Loading