mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00: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,18 +1,22 @@
|
||||
import type { BlockComponent, Command } from '@blocksuite/block-std';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
|
||||
export const getBlockIndexCommand: Command<
|
||||
'currentSelectionPath',
|
||||
'blockIndex' | 'parentBlock',
|
||||
{
|
||||
currentSelectionPath?: string;
|
||||
path?: string;
|
||||
},
|
||||
{
|
||||
blockIndex?: number;
|
||||
parentBlock?: BlockComponent;
|
||||
}
|
||||
> = (ctx, next) => {
|
||||
const path = ctx.path ?? ctx.currentSelectionPath;
|
||||
assertExists(
|
||||
path,
|
||||
'`path` is required, you need to pass it in args or ctx before adding this command to the pipeline.'
|
||||
);
|
||||
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;
|
||||
@@ -29,16 +33,3 @@ export const getBlockIndexCommand: Command<
|
||||
parentBlock: parent as BlockComponent,
|
||||
});
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
blockIndex?: number;
|
||||
parentBlock?: BlockComponent;
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
getBlockIndex: typeof getBlockIndexCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { BlockComponent, Command } from '@blocksuite/block-std';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
|
||||
import { getNextContentBlock } from '../../utils/index.js';
|
||||
|
||||
@@ -13,17 +12,21 @@ function getNextBlock(std: BlockSuite.Std, path: string) {
|
||||
}
|
||||
|
||||
export const getNextBlockCommand: Command<
|
||||
'currentSelectionPath',
|
||||
'nextBlock',
|
||||
{
|
||||
currentSelectionPath?: string;
|
||||
path?: string;
|
||||
},
|
||||
{
|
||||
nextBlock?: BlockComponent;
|
||||
}
|
||||
> = (ctx, next) => {
|
||||
const path = ctx.path ?? ctx.currentSelectionPath;
|
||||
assertExists(
|
||||
path,
|
||||
'`path` is required, you need to pass it in args or ctx before adding this command to the pipeline.'
|
||||
);
|
||||
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 nextBlock = getNextBlock(ctx.std, path);
|
||||
|
||||
@@ -31,15 +34,3 @@ export const getNextBlockCommand: Command<
|
||||
next({ nextBlock });
|
||||
}
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
nextBlock?: BlockComponent;
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
getNextBlock: typeof getNextBlockCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { BlockComponent, Command } from '@blocksuite/block-std';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
|
||||
import { getPrevContentBlock } from '../../utils/index.js';
|
||||
|
||||
@@ -14,17 +13,21 @@ function getPrevBlock(std: BlockSuite.Std, path: string) {
|
||||
}
|
||||
|
||||
export const getPrevBlockCommand: Command<
|
||||
'currentSelectionPath',
|
||||
'prevBlock',
|
||||
{
|
||||
currentSelectionPath?: string;
|
||||
path?: string;
|
||||
},
|
||||
{
|
||||
prevBlock?: BlockComponent;
|
||||
}
|
||||
> = (ctx, next) => {
|
||||
const path = ctx.path ?? ctx.currentSelectionPath;
|
||||
assertExists(
|
||||
path,
|
||||
'`path` is required, you need to pass it in args or ctx before adding this command to the pipeline.'
|
||||
);
|
||||
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 prevBlock = getPrevBlock(ctx.std, path);
|
||||
|
||||
@@ -32,15 +35,3 @@ export const getPrevBlockCommand: Command<
|
||||
next({ prevBlock });
|
||||
}
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
prevBlock?: BlockComponent;
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
getPrevBlock: typeof getPrevBlockCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,10 @@ import type { RoleType } from '@blocksuite/store';
|
||||
import type { ImageSelection } from '../../selection/index.js';
|
||||
|
||||
export const getSelectedBlocksCommand: Command<
|
||||
'currentTextSelection' | 'currentBlockSelections' | 'currentImageSelections',
|
||||
'selectedBlocks',
|
||||
{
|
||||
currentTextSelection?: TextSelection;
|
||||
currentBlockSelections?: BlockSelection[];
|
||||
currentImageSelections?: ImageSelection[];
|
||||
textSelection?: TextSelection;
|
||||
blockSelections?: BlockSelection[];
|
||||
imageSelections?: ImageSelection[];
|
||||
@@ -19,6 +20,9 @@ export const getSelectedBlocksCommand: Command<
|
||||
types?: Array<'image' | 'text' | 'block'>;
|
||||
roles?: RoleType[];
|
||||
mode?: 'all' | 'flat' | 'highest';
|
||||
},
|
||||
{
|
||||
selectedBlocks: BlockComponent[];
|
||||
}
|
||||
> = (ctx, next) => {
|
||||
const {
|
||||
@@ -146,15 +150,3 @@ export const getSelectedBlocksCommand: Command<
|
||||
selectedBlocks: result,
|
||||
});
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
selectedBlocks?: BlockComponent[];
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
getSelectedBlocks: typeof getSelectedBlocksCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,13 +21,3 @@ export {
|
||||
getTextSelectionCommand,
|
||||
type SelectionRect,
|
||||
} from './selection/index.js';
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
// if we use `with` or `inline` to add command data either then use a command we
|
||||
// need to update this interface
|
||||
interface CommandContext {
|
||||
currentSelectionPath?: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,10 @@
|
||||
import { BlockSelection, type Command } from '@blocksuite/block-std';
|
||||
import { BlockSelection } from '@blocksuite/block-std';
|
||||
|
||||
export const getBlockSelectionsCommand: Command<
|
||||
never,
|
||||
'currentBlockSelections'
|
||||
> = (ctx, next) => {
|
||||
import type { GetSelectionCommand } from './types';
|
||||
|
||||
export const getBlockSelectionsCommand: GetSelectionCommand = (ctx, next) => {
|
||||
const currentBlockSelections = ctx.std.selection.filter(BlockSelection);
|
||||
if (currentBlockSelections.length === 0) return;
|
||||
|
||||
next({ currentBlockSelections });
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
currentBlockSelections?: BlockSelection[];
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
getBlockSelections: typeof getBlockSelectionsCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,9 @@
|
||||
import type { Command } from '@blocksuite/block-std';
|
||||
|
||||
import { ImageSelection } from '../../selection/index.js';
|
||||
import type { GetSelectionCommand } from './types.js';
|
||||
|
||||
export const getImageSelectionsCommand: Command<
|
||||
never,
|
||||
'currentImageSelections'
|
||||
> = (ctx, next) => {
|
||||
export const getImageSelectionsCommand: GetSelectionCommand = (ctx, next) => {
|
||||
const currentImageSelections = ctx.std.selection.filter(ImageSelection);
|
||||
if (currentImageSelections.length === 0) return;
|
||||
|
||||
next({ currentImageSelections });
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
currentImageSelections?: ImageSelection[];
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
getImageSelections: typeof getImageSelectionsCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,11 +19,14 @@ export interface SelectionRect {
|
||||
}
|
||||
|
||||
export const getSelectionRectsCommand: Command<
|
||||
'currentTextSelection' | 'currentBlockSelections',
|
||||
'selectionRects',
|
||||
{
|
||||
currentTextSelection?: TextSelection;
|
||||
currentBlockSelections?: BlockSelection[];
|
||||
textSelection?: TextSelection;
|
||||
blockSelections?: BlockSelection[];
|
||||
},
|
||||
{
|
||||
selectionRects: SelectionRect[];
|
||||
}
|
||||
> = (ctx, next) => {
|
||||
let textSelection;
|
||||
@@ -86,26 +89,6 @@ export const getSelectionRectsCommand: Command<
|
||||
return;
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
selectionRects?: SelectionRect[];
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
/**
|
||||
* Get the selection rects of the current selection or given selections.
|
||||
*
|
||||
* @chain may be `getTextSelection`, `getBlockSelections`, or nothing.
|
||||
* @param textSelection The provided text selection.
|
||||
* @param blockSelections The provided block selections. If `textSelection` is provided, this will be ignored.
|
||||
* @returns The selection rects.
|
||||
*/
|
||||
getSelectionRects: typeof getSelectionRectsCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function covers(rect1: SelectionRect, rect2: SelectionRect): boolean {
|
||||
return (
|
||||
rect1.left <= rect2.left &&
|
||||
|
||||
@@ -1,23 +1,10 @@
|
||||
import { type Command, TextSelection } from '@blocksuite/block-std';
|
||||
import { TextSelection } from '@blocksuite/block-std';
|
||||
|
||||
export const getTextSelectionCommand: Command<never, 'currentTextSelection'> = (
|
||||
ctx,
|
||||
next
|
||||
) => {
|
||||
import type { GetSelectionCommand } from './types';
|
||||
|
||||
export const getTextSelectionCommand: GetSelectionCommand = (ctx, next) => {
|
||||
const currentTextSelection = ctx.std.selection.find(TextSelection);
|
||||
if (!currentTextSelection) return;
|
||||
|
||||
next({ currentTextSelection });
|
||||
};
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
currentTextSelection?: TextSelection;
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
getTextSelection: typeof getTextSelectionCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
blocksuite/affine/shared/src/commands/selection/types.ts
Normal file
16
blocksuite/affine/shared/src/commands/selection/types.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import type {
|
||||
BlockSelection,
|
||||
Command,
|
||||
TextSelection,
|
||||
} from '@blocksuite/block-std';
|
||||
|
||||
import type { ImageSelection } from '../../selection/image';
|
||||
|
||||
export type GetSelectionCommand = Command<
|
||||
{},
|
||||
{
|
||||
currentTextSelection?: TextSelection;
|
||||
currentBlockSelections?: BlockSelection[];
|
||||
currentImageSelections?: ImageSelection[];
|
||||
}
|
||||
>;
|
||||
@@ -50,13 +50,5 @@ export class HighlightSelection extends BaseSelection {
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface Selection {
|
||||
highlight: typeof HighlightSelection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const HighlightSelectionExtension =
|
||||
SelectionExtension(HighlightSelection);
|
||||
|
||||
@@ -30,12 +30,4 @@ export class ImageSelection extends BaseSelection {
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface Selection {
|
||||
image: typeof ImageSelection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const ImageSelectionExtension = SelectionExtension(ImageSelection);
|
||||
|
||||
Reference in New Issue
Block a user