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

View File

@@ -2,12 +2,13 @@ import { toNumberedList } from '@blocksuite/affine-shared/utils';
import type { Command, EditorHost } from '@blocksuite/block-std';
export const convertToNumberedListCommand: Command<
never,
'listConvertedId',
{
id: string;
order: number; // This parameter may not correspond to the final order.
stopCapturing?: boolean;
},
{
listConvertedId: string;
}
> = (ctx, next) => {
const { std, id, order, stopCapturing = true } = ctx;

View File

@@ -5,9 +5,10 @@ import { type Command, TextSelection } from '@blocksuite/block-std';
import { correctNumberedListsOrderToPrev } from './utils.js';
export const canDedentListCommand: Command<
never,
'indentContext',
Partial<Omit<IndentContext, 'flavour' | 'type'>>
Partial<Omit<IndentContext, 'flavour' | 'type'>>,
{
indentContext: IndentContext;
}
> = (ctx, next) => {
let { blockId, inlineIndex } = ctx;
const { std } = ctx;
@@ -91,7 +92,9 @@ export const canDedentListCommand: Command<
});
};
export const dedentListCommand: Command<'indentContext'> = (ctx, next) => {
export const dedentListCommand: Command<{
indentContext: IndentContext;
}> = (ctx, next) => {
const { indentContext: dedentContext, std } = ctx;
const { store, selection, range, host } = std;

View File

@@ -8,9 +8,10 @@ import { type Command, TextSelection } from '@blocksuite/block-std';
import { correctNumberedListsOrderToPrev } from './utils.js';
export const canIndentListCommand: Command<
never,
'indentContext',
Partial<Omit<IndentContext, 'type' | 'flavour'>>
Partial<Omit<IndentContext, 'type' | 'flavour'>>,
{
indentContext: IndentContext;
}
> = (ctx, next) => {
let { blockId, inlineIndex } = ctx;
const { std } = ctx;
@@ -84,10 +85,9 @@ export const canIndentListCommand: Command<
});
};
export const indentListCommand: Command<'indentContext', never> = (
ctx,
next
) => {
export const indentListCommand: Command<{
indentContext: IndentContext;
}> = (ctx, next) => {
const { indentContext, std } = ctx;
if (
!indentContext ||

View File

@@ -1,17 +1,5 @@
import type { BlockCommands } from '@blocksuite/block-std';
import { convertToNumberedListCommand } from './convert-to-numbered-list.js';
import { canDedentListCommand, dedentListCommand } from './dedent-list.js';
import { canIndentListCommand, indentListCommand } from './indent-list.js';
import { listToParagraphCommand } from './list-to-paragraph.js';
import { splitListCommand } from './split-list.js';
export const commands: BlockCommands = {
convertToNumberedList: convertToNumberedListCommand,
listToParagraph: listToParagraphCommand,
splitList: splitListCommand,
canIndentList: canIndentListCommand,
indentList: indentListCommand,
canDedentList: canDedentListCommand,
dedentList: dedentListCommand,
};
export { convertToNumberedListCommand } from './convert-to-numbered-list.js';
export { canDedentListCommand, dedentListCommand } from './dedent-list.js';
export { canIndentListCommand, indentListCommand } from './indent-list.js';
export { listToParagraphCommand } from './list-to-paragraph.js';
export { splitListCommand } from './split-list.js';

View File

@@ -3,11 +3,12 @@ import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
export const listToParagraphCommand: Command<
never,
'listConvertedId',
{
id: string;
stopCapturing?: boolean;
},
{
listConvertedId: string;
}
> = (ctx, next) => {
const { id, stopCapturing = true } = ctx;

View File

@@ -5,16 +5,13 @@ import {
} from '@blocksuite/affine-shared/utils';
import type { Command, EditorHost } from '@blocksuite/block-std';
import { canDedentListCommand, dedentListCommand } from './dedent-list.js';
import { correctNumberedListsOrderToPrev } from './utils.js';
export const splitListCommand: Command<
never,
never,
{
blockId: string;
inlineIndex: number;
}
> = (ctx, next) => {
export const splitListCommand: Command<{
blockId: string;
inlineIndex: number;
}> = (ctx, next) => {
const { blockId, inlineIndex, std } = ctx;
const host = std.host as EditorHost;
const doc = host.doc;
@@ -100,11 +97,11 @@ export const splitListCommand: Command<
if (parent.role === 'content') {
host.command
.chain()
.canDedentList({
.pipe(canDedentListCommand, {
blockId,
inlineIndex: 0,
})
.dedentList()
.pipe(dedentListCommand)
.run();
next();

View File

@@ -1,16 +1,3 @@
import type { IndentContext } from '@blocksuite/affine-shared/types';
import type { convertToNumberedListCommand } from './commands/convert-to-numbered-list.js';
import type {
canDedentListCommand,
dedentListCommand,
} from './commands/dedent-list.js';
import type {
canIndentListCommand,
indentListCommand,
} from './commands/indent-list.js';
import type { listToParagraphCommand } from './commands/list-to-paragraph.js';
import type { splitListCommand } from './commands/split-list.js';
import { ListBlockComponent } from './list-block.js';
export function effects() {
@@ -18,23 +5,6 @@ export function effects() {
}
declare global {
namespace BlockSuite {
interface CommandContext {
listConvertedId?: string;
indentContext?: IndentContext;
}
interface Commands {
convertToNumberedList: typeof convertToNumberedListCommand;
canDedentList: typeof canDedentListCommand;
canIndentList: typeof canIndentListCommand;
dedentList: typeof dedentListCommand;
indentList: typeof indentListCommand;
listToParagraph: typeof listToParagraphCommand;
splitList: typeof splitListCommand;
}
}
interface HTMLElementTagNameMap {
'affine-list': ListBlockComponent;
}

View File

@@ -1,4 +1,5 @@
export * from './adapters/index.js';
export * from './commands';
export { correctNumberedListsOrderToPrev } from './commands/utils';
export * from './list-block.js';
export * from './list-spec.js';

View File

@@ -3,9 +3,20 @@ import {
textKeymap,
} from '@blocksuite/affine-components/rich-text';
import { ListBlockSchema } from '@blocksuite/affine-model';
import { getSelectedModelsCommand } from '@blocksuite/affine-shared/commands';
import { KeymapExtension, TextSelection } from '@blocksuite/block-std';
import { IS_MAC } from '@blocksuite/global/env';
import {
canDedentListCommand,
dedentListCommand,
} from './commands/dedent-list.js';
import {
canIndentListCommand,
indentListCommand,
} from './commands/indent-list.js';
import { listToParagraphCommand } from './commands/list-to-paragraph.js';
import { splitListCommand } from './commands/split-list.js';
import { forwardDelete } from './utils/forward-delete.js';
export const ListKeymapExtension = KeymapExtension(
@@ -16,10 +27,13 @@ export const ListKeymapExtension = KeymapExtension(
if (!text) return false;
ctx.get('keyboardState').raw.preventDefault();
std.command.exec('splitList', {
blockId: text.from.blockId,
inlineIndex: text.from.index,
});
std.command
.chain()
.pipe(splitListCommand, {
blockId: text.from.blockId,
inlineIndex: text.from.index,
})
.run();
return true;
},
'Mod-Enter': ctx => {
@@ -27,16 +41,22 @@ export const ListKeymapExtension = KeymapExtension(
if (!text) return false;
ctx.get('keyboardState').raw.preventDefault();
std.command.exec('splitList', {
blockId: text.from.blockId,
inlineIndex: text.from.index,
});
std.command
.chain()
.pipe(splitListCommand, {
blockId: text.from.blockId,
inlineIndex: text.from.index,
})
.run();
return true;
},
Tab: ctx => {
const { selectedModels } = std.command.exec('getSelectedModels', {
types: ['text'],
});
const [_, { selectedModels }] = std.command
.chain()
.pipe(getSelectedModelsCommand, {
types: ['text'],
})
.run();
if (selectedModels?.length !== 1) {
return false;
}
@@ -46,18 +66,21 @@ export const ListKeymapExtension = KeymapExtension(
ctx.get('keyboardState').raw.preventDefault();
std.command
.chain()
.canIndentList({
.pipe(canIndentListCommand, {
blockId: text.from.blockId,
inlineIndex: text.from.index,
})
.indentList()
.pipe(indentListCommand)
.run();
return true;
},
'Shift-Tab': ctx => {
const { selectedModels } = std.command.exec('getSelectedModels', {
types: ['text'],
});
const [_, { selectedModels }] = std.command
.chain()
.pipe(getSelectedModelsCommand, {
types: ['text'],
})
.run();
if (selectedModels?.length !== 1) {
return;
}
@@ -67,11 +90,11 @@ export const ListKeymapExtension = KeymapExtension(
ctx.get('keyboardState').raw.preventDefault();
std.command
.chain()
.canDedentList({
.pipe(canDedentListCommand, {
blockId: text.from.blockId,
inlineIndex: text.from.index,
})
.dedentList()
.pipe(dedentListCommand)
.run();
return true;
},
@@ -83,7 +106,12 @@ export const ListKeymapExtension = KeymapExtension(
if (!isStart) return false;
ctx.get('keyboardState').raw.preventDefault();
std.command.exec('listToParagraph', { id: text.from.blockId });
std.command
.chain()
.pipe(listToParagraphCommand, {
id: text.from.blockId,
})
.run();
return true;
},
'Control-d': ctx => {

View File

@@ -1,18 +1,12 @@
import {
BlockViewExtension,
CommandExtension,
FlavourExtension,
} from '@blocksuite/block-std';
import { BlockViewExtension, FlavourExtension } from '@blocksuite/block-std';
import type { ExtensionType } from '@blocksuite/store';
import { literal } from 'lit/static-html.js';
import { ListBlockAdapterExtensions } from './adapters/extension.js';
import { commands } from './commands/index.js';
import { ListKeymapExtension, ListTextKeymapExtension } from './list-keymap.js';
export const ListBlockSpec: ExtensionType[] = [
FlavourExtension('affine:list'),
CommandExtension(commands),
BlockViewExtension('affine:list', literal`affine-list`),
ListKeymapExtension,
ListTextKeymapExtension,