Skip to content

Commit

Permalink
fix: handle whitespace input in empty placeholders correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
idebenone committed Nov 9, 2024
1 parent 58a7a9e commit 363612c
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 15 deletions.
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
2 changes: 1 addition & 1 deletion example/tools/code
2 changes: 1 addition & 1 deletion example/tools/delimiter
Submodule delimiter updated 5 files
+10 −5 package.json
+40 −12 src/index.ts
+11 −0 tsconfig.json
+9 −2 vite.config.js
+926 −179 yarn.lock
2 changes: 1 addition & 1 deletion example/tools/embed
2 changes: 1 addition & 1 deletion example/tools/header
2 changes: 1 addition & 1 deletion example/tools/image
2 changes: 1 addition & 1 deletion example/tools/inline-code
2 changes: 1 addition & 1 deletion example/tools/list
Submodule list updated from a6dc6a to 297736
2 changes: 1 addition & 1 deletion example/tools/quote
Submodule quote updated 6 files
+13 −6 package.json
+0 −310 src/index.js
+427 −0 src/index.ts
+17 −0 tsconfig.json
+3 −2 vite.config.js
+602 −26 yarn.lock
2 changes: 1 addition & 1 deletion example/tools/table
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);
}
}
}

0 comments on commit 363612c

Please sign in to comment.