Skip to main content

Overview

The getBlockSlate method retrieves the underlying Slate editor instance for a specific block.

Signature

Blocks.getBlockSlate(
  editor: YooEditor,
  options: GetBlockSlateOptions
): SlateEditor | null

Parameters

editor
YooEditor
required
The editor instance
options
GetBlockSlateOptions
required
Configuration object

Return Value

Returns the SlateEditor instance if found, or null if the block doesn’t exist or doesn’t have a Slate editor.

Examples

Basic Usage

import { Blocks } from '@yoopta/editor';
import { Editor } from 'slate';

// Get Slate editor by block ID
const slate = Blocks.getBlockSlate(editor, { id: 'block-123' });

if (slate) {
  // Use Slate API directly
  const text = Editor.string(slate, []);
  console.log('Block text:', text);
}

// Get Slate editor by path
const slateAtPath = Blocks.getBlockSlate(editor, { at: 3 });

Access Slate API Directly

const getBlockText = (editor: YooEditor, blockId: string): string => {
  const slate = Blocks.getBlockSlate(editor, { id: blockId });
  
  if (!slate) return '';
  
  return Editor.string(slate, []);
};

Check if Block is Empty

import { Editor } from 'slate';

const isBlockEmpty = (editor: YooEditor, blockId: string): boolean => {
  const slate = Blocks.getBlockSlate(editor, { id: blockId });
  
  if (!slate) return true;
  
  const text = Editor.string(slate, []);
  return text.trim().length === 0;
};

Use Cases

Direct Slate Operations

import { Transforms, Editor } from 'slate';

const insertTextAtStart = (editor: YooEditor, blockId: string, text: string) => {
  const slate = Blocks.getBlockSlate(editor, { id: blockId });
  
  if (!slate) return;
  
  Transforms.insertText(slate, text, {
    at: Editor.start(slate, []),
  });
};

Get Selection in Block

const getBlockSelection = (editor: YooEditor, blockId: string) => {
  const slate = Blocks.getBlockSlate(editor, { id: blockId });
  
  if (!slate) return null;
  
  return slate.selection;
};

Notes

You must provide either at or id. If neither is provided, the method returns null.
The id parameter takes precedence over at if both are provided.
Not all blocks may have Slate editors. Some void blocks might return null.
This method is useful when you need to perform low-level Slate operations that aren’t available through the Blocks or Elements API.

Type Definition

type GetBlockSlateOptions = {
  at?: number; // YooptaPathIndex
  id?: string;
};