Skip to main content

Overview

The isElementEmpty method checks if an element contains any text content.

Signature

editor.isElementEmpty(options: IsElementEmptyOptions): boolean

Parameters

options
IsElementEmptyOptions
required

Return Value

Returns true if element is empty, false otherwise.

Examples

// Check if accordion content is empty
const isEmpty = editor.isElementEmpty({
  blockId: 'accordion-1',
  type: 'accordion-list-item-content',
  path: [0, 1, 1]
});

if (isEmpty) {
  console.log('Content is empty');
}

Use Cases

Conditional Deletion

const deleteIfEmpty = (blockId: string, itemPath: number[]) => {
  const isEmpty = editor.isElementEmpty({
    blockId,
    type: 'accordion-list-item-content',
    path: [...itemPath, 1]
  });

  if (isEmpty) {
    editor.deleteElement({
      blockId,
      type: 'accordion-list-item',
      path: itemPath
    });
  }
};

Validation

const validateContent = (blockId: string): boolean => {
  const items = editor.getElements({
    blockId,
    type: 'accordion-list-item'
  });

  return items.every((item, index) => {
    return !editor.isElementEmpty({
      blockId,
      type: 'accordion-list-item-content',
      path: [0, 0, index, 1]
    });
  });
};

Type Definition

type IsElementEmptyOptions = {
  blockId: string;
  type: string;
  path?: number[];
};