mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-05 03:19:52 +08:00
17bf75e843
Closes: [BS-2216](https://linear.app/affine-design/issue/BS-2216/remove-global-types-in-command)
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import {
|
|
getBlockSelectionsCommand,
|
|
getSelectedBlocksCommand,
|
|
getTextSelectionCommand,
|
|
} from '@blocksuite/affine-shared/commands';
|
|
import type {
|
|
BlockComponent,
|
|
Command,
|
|
InitCommandCtx,
|
|
} from '@blocksuite/block-std';
|
|
|
|
import { isPeekable, peek } from './peekable.js';
|
|
|
|
const getSelectedPeekableBlocks = (cmd: InitCommandCtx) => {
|
|
const [result, ctx] = cmd.std.command
|
|
.chain()
|
|
.tryAll(chain => [
|
|
chain.pipe(getTextSelectionCommand),
|
|
chain.pipe(getBlockSelectionsCommand),
|
|
])
|
|
.pipe(getSelectedBlocksCommand, { types: ['text', 'block'] })
|
|
.run();
|
|
return ((result ? ctx.selectedBlocks : []) || []).filter(isPeekable);
|
|
};
|
|
|
|
export const getSelectedPeekableBlocksCommand: Command<
|
|
{
|
|
selectedBlocks: BlockComponent[];
|
|
},
|
|
{
|
|
selectedPeekableBlocks: BlockComponent[];
|
|
}
|
|
> = (ctx, next) => {
|
|
const selectedPeekableBlocks = getSelectedPeekableBlocks(ctx);
|
|
if (selectedPeekableBlocks.length > 0) {
|
|
next({ selectedPeekableBlocks });
|
|
}
|
|
};
|
|
|
|
export const peekSelectedBlockCommand: Command<{
|
|
selectedBlocks: BlockComponent[];
|
|
}> = (ctx, next) => {
|
|
const peekableBlocks = getSelectedPeekableBlocks(ctx);
|
|
// if there are multiple blocks, peek the first one
|
|
const block = peekableBlocks.at(0);
|
|
|
|
if (block) {
|
|
peek(block);
|
|
next();
|
|
}
|
|
};
|