mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-23 21:38:44 +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,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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[];
|
||||
}
|
||||
>;
|
||||
Reference in New Issue
Block a user