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,9 +1,9 @@
import { type Command, TextSelection } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
export const clearAndSelectFirstModelCommand: Command<'selectedModels'> = (
ctx,
next
) => {
export const clearAndSelectFirstModelCommand: Command<{
selectedModels?: BlockModel[];
}> = (ctx, next) => {
const models = ctx.selectedModels;
if (!models) {
@@ -31,11 +31,3 @@ export const clearAndSelectFirstModelCommand: Command<'selectedModels'> = (
return next();
};
declare global {
namespace BlockSuite {
interface Commands {
clearAndSelectFirstModel: typeof clearAndSelectFirstModelCommand;
}
}
}
@@ -1,10 +1,10 @@
import type { Command } from '@blocksuite/block-std';
import { Slice } from '@blocksuite/store';
import { type BlockModel, type DraftModel, Slice } from '@blocksuite/store';
export const copySelectedModelsCommand: Command<'draftedModels' | 'onCopy'> = (
ctx,
next
) => {
export const copySelectedModelsCommand: Command<{
draftedModels?: Promise<DraftModel<BlockModel<object>>[]>;
onCopy?: () => void;
}> = (ctx, next) => {
const models = ctx.draftedModels;
if (!models) {
console.error(
@@ -23,14 +23,3 @@ export const copySelectedModelsCommand: Command<'draftedModels' | 'onCopy'> = (
.catch(console.error);
return next();
};
declare global {
namespace BlockSuite {
interface CommandContext {
onCopy?: () => void;
}
interface Commands {
copySelectedModels: typeof copySelectedModelsCommand;
}
}
}
@@ -1,9 +1,9 @@
import type { Command } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
export const deleteSelectedModelsCommand: Command<'selectedModels'> = (
ctx,
next
) => {
export const deleteSelectedModelsCommand: Command<{
selectedModels?: BlockModel[];
}> = (ctx, next) => {
const models = ctx.selectedModels;
if (!models) {
@@ -19,11 +19,3 @@ export const deleteSelectedModelsCommand: Command<'selectedModels'> = (
return next();
};
declare global {
namespace BlockSuite {
interface Commands {
deleteSelectedModels: typeof deleteSelectedModelsCommand;
}
}
}
@@ -6,8 +6,12 @@ import {
} from '@blocksuite/store';
export const draftSelectedModelsCommand: Command<
'selectedModels',
'draftedModels'
{
selectedModels?: BlockModel[];
},
{
draftedModels: Promise<DraftModel<BlockModel<object>>[]>;
}
> = (ctx, next) => {
const models = ctx.selectedModels;
if (!models) {
@@ -44,15 +48,3 @@ export const draftSelectedModelsCommand: Command<
return next({ draftedModels: draftedModelsPromise });
};
declare global {
namespace BlockSuite {
interface CommandContext {
draftedModels?: Promise<DraftModel<BlockModel<object>>[]>;
}
interface Commands {
draftSelectedModels: typeof draftSelectedModelsCommand;
}
}
}
@@ -1,9 +1,10 @@
import type { Command } from '@blocksuite/block-std';
import { Slice } from '@blocksuite/store';
import { type BlockModel, type DraftModel, Slice } from '@blocksuite/store';
export const duplicateSelectedModelsCommand: Command<
'draftedModels' | 'selectedModels'
> = (ctx, next) => {
export const duplicateSelectedModelsCommand: Command<{
draftedModels?: Promise<DraftModel<BlockModel<object>>[]>;
selectedModels?: BlockModel[];
}> = (ctx, next) => {
const { std, draftedModels, selectedModels } = ctx;
if (!draftedModels || !selectedModels) return;
@@ -28,11 +29,3 @@ export const duplicateSelectedModelsCommand: Command<
return next();
};
declare global {
namespace BlockSuite {
interface Commands {
duplicateSelectedModels: typeof duplicateSelectedModelsCommand;
}
}
}
@@ -1,6 +1,13 @@
import type { Command } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
import { getSelectedBlocksCommand } from '../block-crud/get-selected-blocks';
import {
getBlockSelectionsCommand,
getImageSelectionsCommand,
getTextSelectionCommand,
} from '../selection';
/**
* Retrieves the selected models based on the provided selection types and mode.
*
@@ -29,11 +36,12 @@ import type { BlockModel } from '@blocksuite/store';
* @returns An object containing the selected models as an array of BlockModel instances.
*/
export const getSelectedModelsCommand: Command<
never,
'selectedModels',
{
types?: Array<'image' | 'text' | 'block'>;
mode?: 'all' | 'flat' | 'highest';
},
{
selectedModels: BlockModel[];
}
> = (ctx, next) => {
const types = ctx.types ?? ['block', 'text', 'image'];
@@ -42,15 +50,12 @@ export const getSelectedModelsCommand: Command<
ctx.std.command
.chain()
.tryAll(chain => [
chain.getTextSelection(),
chain.getBlockSelections(),
chain.getImageSelections(),
chain.pipe(getTextSelectionCommand),
chain.pipe(getBlockSelectionsCommand),
chain.pipe(getImageSelectionsCommand),
])
.getSelectedBlocks({
types,
mode,
})
.inline(ctx => {
.pipe(getSelectedBlocksCommand, { types, mode })
.pipe(ctx => {
const { selectedBlocks = [] } = ctx;
selectedModels.push(...selectedBlocks.map(el => el.model));
})
@@ -58,15 +63,3 @@ export const getSelectedModelsCommand: Command<
next({ selectedModels });
};
declare global {
namespace BlockSuite {
interface CommandContext {
selectedModels?: BlockModel[];
}
interface Commands {
getSelectedModels: typeof getSelectedModelsCommand;
}
}
}
@@ -1,9 +1,9 @@
import type { Command } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
export const retainFirstModelCommand: Command<'selectedModels'> = (
ctx,
next
) => {
export const retainFirstModelCommand: Command<{
selectedModels?: BlockModel[];
}> = (ctx, next) => {
if (!ctx.selectedModels) {
console.error(
'`selectedModels` is required, you need to use `getSelectedModels` command before adding this command to the pipeline.'
@@ -17,11 +17,3 @@ export const retainFirstModelCommand: Command<'selectedModels'> = (
return next();
};
declare global {
namespace BlockSuite {
interface Commands {
retainFirstModel: typeof retainFirstModelCommand;
}
}
}