mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-10 05:29:08 +08:00
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:
@@ -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?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user