import type { Command } from '@blockeditor/block-std'; import { PathFinder } from '@blockeditor/block-std'; import { assertExists } from '@blockeditor/global/utils'; import type { BlockElement } from '@blockeditor/lit'; export const getBlockIndexCommand: Command< 'currentSelectionPath', 'blockIndex' | 'parentBlock', { path?: string[]; } > = (ctx, next) => { const path = ctx.path ?? ctx.currentSelectionPath; assertExists( path, '`path` is required, you need to pass it in args or ctx before adding this command to the pipeline.' ); const parent = ctx.std.view.getParent(path); if (!parent) { return; } const index = parent.children.findIndex(x => { return PathFinder.equals(x.path, path); }); next({ blockIndex: index, parentBlock: parent.view as BlockElement, }); }; declare global { namespace BlockEditor { interface CommandContext { blockIndex?: number; parentBlock?: BlockElement; } interface Commands { getBlockIndex: typeof getBlockIndexCommand; } } }