mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-27 10:52:40 +08:00
Closes: [BS-2216](https://linear.app/affine-design/issue/BS-2216/remove-global-types-in-command)
36 lines
820 B
TypeScript
36 lines
820 B
TypeScript
import type { BlockComponent, Command } from '@blocksuite/block-std';
|
|
|
|
export const getBlockIndexCommand: Command<
|
|
{
|
|
currentSelectionPath?: string;
|
|
path?: string;
|
|
},
|
|
{
|
|
blockIndex?: number;
|
|
parentBlock?: BlockComponent;
|
|
}
|
|
> = (ctx, next) => {
|
|
const path = ctx.path ?? ctx.currentSelectionPath;
|
|
if (!path) {
|
|
console.error(
|
|
'`path` is required, you need to pass it in args or ctx before adding this command to the pipeline.'
|
|
);
|
|
return;
|
|
}
|
|
|
|
const parentModel = ctx.std.store.getParent(path);
|
|
if (!parentModel) return;
|
|
|
|
const parent = ctx.std.view.getBlock(parentModel.id);
|
|
if (!parent) return;
|
|
|
|
const index = parent.childBlocks.findIndex(x => {
|
|
return x.blockId === path;
|
|
});
|
|
|
|
next({
|
|
blockIndex: index,
|
|
parentBlock: parent as BlockComponent,
|
|
});
|
|
};
|