Skip to main content

Overview

The getElements method retrieves all elements of a specific type or matching a condition from a block.

Signature

editor.getElements(options: GetElementsOptions): SlateElement[]

Parameters

options
GetElementsOptions
required

Return Value

Returns an array of SlateElement[]. Returns empty array if no elements found.

Examples

// Get all accordion items
const items = editor.getElements({
  blockId: 'accordion-1',
  type: 'accordion-list-item'
});

// Get expanded items only
const expandedItems = editor.getElements({
  blockId: 'accordion-1',
  match: (el) => el.type === 'accordion-list-item' && el.props?.isExpanded
});

// Get all table rows
const rows = editor.getElements({
  blockId: 'table-1',
  type: 'table-row'
});

console.log(`Found ${rows.length} rows`);

Use Cases

Batch Operations

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

  items.forEach((item, index) => {
    editor.updateElement({
      blockId,
      type: 'accordion-list-item',
      props: { isExpanded: true },
      path: [0, 0, index]
    });
  });
};

Count Elements

const countTabs = (blockId: string): number => {
  const tabs = editor.getElements({
    blockId,
    type: 'tabs-item-heading'
  });

  return tabs.length;
};

Type Definition

type GetElementsOptions = {
  blockId: string;
  type?: string;
  match?: (element: SlateElement) => boolean;
};