refactor(editor): remove dependency of command global types (#9903)

Closes: [BS-2216](https://linear.app/affine-design/issue/BS-2216/remove-global-types-in-command)
This commit is contained in:
Saul-Mirone
2025-01-27 12:28:46 +00:00
parent 4b549e0484
commit 17bf75e843
170 changed files with 1461 additions and 2124 deletions
@@ -1,4 +1,8 @@
/// <reference types="@blocksuite/affine-shared/commands" />
import {
getBlockSelectionsCommand,
getSelectedBlocksCommand,
getTextSelectionCommand,
} from '@blocksuite/affine-shared/commands';
import type {
BlockComponent,
Command,
@@ -10,15 +14,22 @@ import { isPeekable, peek } from './peekable.js';
const getSelectedPeekableBlocks = (cmd: InitCommandCtx) => {
const [result, ctx] = cmd.std.command
.chain()
.tryAll(chain => [chain.getTextSelection(), chain.getBlockSelections()])
.getSelectedBlocks({ types: ['text', 'block'] })
.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',
'selectedPeekableBlocks'
{
selectedBlocks: BlockComponent[];
},
{
selectedPeekableBlocks: BlockComponent[];
}
> = (ctx, next) => {
const selectedPeekableBlocks = getSelectedPeekableBlocks(ctx);
if (selectedPeekableBlocks.length > 0) {
@@ -26,10 +37,9 @@ export const getSelectedPeekableBlocksCommand: Command<
}
};
export const peekSelectedBlockCommand: Command<'selectedBlocks'> = (
ctx,
next
) => {
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);
@@ -39,17 +49,3 @@ export const peekSelectedBlockCommand: Command<'selectedBlocks'> = (
next();
}
};
declare global {
namespace BlockSuite {
interface CommandContext {
selectedPeekableBlocks?: BlockComponent[];
}
interface Commands {
peekSelectedBlock: typeof peekSelectedBlockCommand;
getSelectedPeekableBlocks: typeof getSelectedPeekableBlocksCommand;
// todo: add command for peek an inline element?
}
}
}