The Selection namespace provides methods for managing both block-level selection (which blocks are selected) and Slate-level selection (text selection within blocks).
// Get current block orderconst current = Selection.getCurrent(editor);console.log(current); // 0// Same as accessing editor.path.currentconst current = editor.path.current;
// Set current block to order 0Selection.setCurrent(editor, { at: 0 });// Set with source trackingSelection.setCurrent(editor, { at: 1, source: 'keyboard'});// Same aseditor.setPath({ current: 1 });
// Get all selected block ordersconst selected = Selection.getSelected(editor);console.log(selected); // [0, 1, 2] or null// Check if specific block is selectedconst isSelected = Selection.getSelected(editor, { at: 0 });console.log(isSelected); // true or false// Same asconst selected = editor.path.selected;
// Get next block from currentconst next = Selection.getNext(editor);// Get next block from specific positionconst next = Selection.getNext(editor, { at: 0 });if (next !== null) { Selection.setCurrent(editor, { at: next });}
// Get previous block from currentconst prev = Selection.getPrevious(editor);// Get previous block from specific positionconst prev = Selection.getPrevious(editor, { at: 5 });
// Get selection from current blockconst selection = Selection.getSlateSelection(editor);// Get selection from specific blockconst selection = Selection.getSlateSelection(editor, { at: 0 });// Get selection by block IDconst selection = Selection.getSlateSelection(editor, { blockId: 'block-123'});if (selection) { console.log('Anchor:', selection.anchor); console.log('Focus:', selection.focus);}
// Check if current selection is expandedconst isExpanded = Selection.isExpanded(editor);if (isExpanded) { console.log('Text is selected');} else { console.log('Cursor is at a point');}// Check specific blockconst isExpanded = Selection.isExpanded(editor, { at: 0 });
// Get anchor from current selectionconst anchor = Selection.getAnchor(editor);if (anchor) { console.log('Path:', anchor.path); console.log('Offset:', anchor.offset);}
// Get first point in current blockconst firstPoint = Selection.getFirstPoint(editor);// Get first point in specific blockconst firstPoint = Selection.getFirstPoint(editor, { at: 0 });// Use to move cursor to startif (firstPoint) { Selection.setSlateSelection(editor, { selection: { anchor: firstPoint, focus: firstPoint } });}
// Get last point in current blockconst lastPoint = Selection.getLastPoint(editor);// Use to move cursor to endif (lastPoint) { Selection.setSlateSelection(editor, { selection: { anchor: lastPoint, focus: lastPoint } });}
// Convert current selection to DOM rangeconst selection = Selection.getSlateSelection(editor);if (selection) { const domRange = Selection.toDOMRange(editor, { selection }); if (domRange) { // Use DOM range for operations const rect = domRange.getBoundingClientRect(); console.log('Selection position:', rect); }}