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
@@ -5,10 +5,11 @@ import { type Command, TextSelection } from '@blocksuite/block-std';
* Add a paragraph next to the current block.
*/
export const addParagraphCommand: Command<
never,
'paragraphConvertedId',
{
blockId?: string;
},
{
paragraphConvertedId: string;
}
> = (ctx, next) => {
const { std } = ctx;
@@ -6,11 +6,10 @@ import { Text } from '@blocksuite/store';
/**
* Append a paragraph block at the end of the whole page.
*/
export const appendParagraphCommand: Command<
never,
never,
{ text?: string }
> = (ctx, next) => {
export const appendParagraphCommand: Command<{ text?: string }> = (
ctx,
next
) => {
const { std, text = '' } = ctx;
const { store } = std;
if (!store.root) return;
@@ -6,9 +6,10 @@ import {
import { type Command, TextSelection } from '@blocksuite/block-std';
export const canDedentParagraphCommand: Command<
never,
'indentContext',
Partial<Omit<IndentContext, 'flavour' | 'type'>>
Partial<Omit<IndentContext, 'flavour' | 'type'>>,
{
indentContext: IndentContext;
}
> = (ctx, next) => {
let { blockId, inlineIndex } = ctx;
const { std } = ctx;
@@ -56,7 +57,9 @@ export const canDedentParagraphCommand: Command<
});
};
export const dedentParagraphCommand: Command<'indentContext'> = (ctx, next) => {
export const dedentParagraphCommand: Command<{
indentContext: IndentContext;
}> = (ctx, next) => {
const { indentContext: dedentContext, std } = ctx;
const { store, selection, range, host } = std;
@@ -8,9 +8,10 @@ import {
import { type Command, TextSelection } from '@blocksuite/block-std';
export const canIndentParagraphCommand: Command<
never,
'indentContext',
Partial<Omit<IndentContext, 'flavour' | 'type'>>
Partial<Omit<IndentContext, 'flavour' | 'type'>>,
{
indentContext: IndentContext;
}
> = (cxt, next) => {
let { blockId, inlineIndex } = cxt;
const { std } = cxt;
@@ -60,7 +61,9 @@ export const canIndentParagraphCommand: Command<
});
};
export const indentParagraphCommand: Command<'indentContext'> = (ctx, next) => {
export const indentParagraphCommand: Command<{
indentContext: IndentContext;
}> = (ctx, next) => {
const { indentContext, std } = ctx;
const { store, selection, host, range } = std;
@@ -1,23 +1,11 @@
import type { BlockCommands } from '@blocksuite/block-std';
import { addParagraphCommand } from './add-paragraph.js';
import { appendParagraphCommand } from './append-paragraph.js';
import {
export { addParagraphCommand } from './add-paragraph.js';
export { appendParagraphCommand } from './append-paragraph.js';
export {
canDedentParagraphCommand,
dedentParagraphCommand,
} from './dedent-paragraph.js';
import {
export {
canIndentParagraphCommand,
indentParagraphCommand,
} from './indent-paragraph.js';
import { splitParagraphCommand } from './split-paragraph.js';
export const commands: BlockCommands = {
appendParagraph: appendParagraphCommand,
splitParagraph: splitParagraphCommand,
addParagraph: addParagraphCommand,
canIndentParagraph: canIndentParagraphCommand,
canDedentParagraph: canDedentParagraphCommand,
indentParagraph: indentParagraphCommand,
dedentParagraph: dedentParagraphCommand,
};
export { splitParagraphCommand } from './split-paragraph.js';
@@ -6,10 +6,11 @@ import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const splitParagraphCommand: Command<
never,
'paragraphConvertedId',
{
blockId?: string;
},
{
paragraphConvertedId: string;
}
> = (ctx, next) => {
const { std } = ctx;
@@ -1,16 +1,3 @@
import type { IndentContext } from '@blocksuite/affine-shared/types';
import type { addParagraphCommand } from './commands/add-paragraph.js';
import type { appendParagraphCommand } from './commands/append-paragraph.js';
import type {
canDedentParagraphCommand,
dedentParagraphCommand,
} from './commands/dedent-paragraph.js';
import type {
canIndentParagraphCommand,
indentParagraphCommand,
} from './commands/indent-paragraph.js';
import type { splitParagraphCommand } from './commands/split-paragraph.js';
import { effects as ParagraphHeadingIconEffects } from './heading-icon.js';
import { ParagraphBlockComponent } from './paragraph-block.js';
import type { ParagraphBlockService } from './paragraph-service.js';
@@ -25,19 +12,6 @@ declare global {
interface BlockServices {
'affine:paragraph': ParagraphBlockService;
}
interface Commands {
addParagraph: typeof addParagraphCommand;
appendParagraph: typeof appendParagraphCommand;
canIndentParagraph: typeof canIndentParagraphCommand;
canDedentParagraph: typeof canDedentParagraphCommand;
dedentParagraph: typeof dedentParagraphCommand;
indentParagraph: typeof indentParagraphCommand;
splitParagraph: typeof splitParagraphCommand;
}
interface CommandContext {
paragraphConvertedId?: string;
indentContext?: IndentContext;
}
}
interface HTMLElementTagNameMap {
'affine-paragraph': ParagraphBlockComponent;
@@ -1,4 +1,5 @@
export * from './adapters/index.js';
export * from './commands';
export * from './paragraph-block.js';
export * from './paragraph-service.js';
export * from './paragraph-spec.js';
@@ -12,6 +12,16 @@ import {
import { KeymapExtension, TextSelection } from '@blocksuite/block-std';
import { IS_MAC } from '@blocksuite/global/env';
import { addParagraphCommand } from './commands/add-paragraph.js';
import {
canDedentParagraphCommand,
dedentParagraphCommand,
} from './commands/dedent-paragraph.js';
import {
canIndentParagraphCommand,
indentParagraphCommand,
} from './commands/indent-paragraph.js';
import { splitParagraphCommand } from './commands/split-paragraph.js';
import { forwardDelete } from './utils/forward-delete.js';
import { mergeWithPrev } from './utils/merge-with-prev.js';
@@ -46,7 +56,11 @@ export const ParagraphKeymapExtension = KeymapExtension(
return true;
}
std.command.chain().canDedentParagraph().dedentParagraph().run();
std.command
.chain()
.pipe(canDedentParagraphCommand)
.pipe(dedentParagraphCommand)
.run();
return true;
},
'Mod-Enter': ctx => {
@@ -73,7 +87,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
return true;
}
std.command.exec('addParagraph');
std.command.chain().pipe(addParagraphCommand).run();
return true;
},
Enter: ctx => {
@@ -113,7 +127,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
raw.preventDefault();
store.captureSync();
model.text.delete(range.index - 1, 1);
std.command.exec('addParagraph');
std.command.chain().pipe(addParagraphCommand).run();
return true;
}
return true;
@@ -146,11 +160,11 @@ export const ParagraphKeymapExtension = KeymapExtension(
}
if (isEnd) {
std.command.exec('addParagraph');
std.command.chain().pipe(addParagraphCommand).run();
return true;
}
std.command.exec('splitParagraph');
std.command.chain().pipe(splitParagraphCommand).run();
return true;
},
Delete: ctx => {
@@ -189,8 +203,8 @@ export const ParagraphKeymapExtension = KeymapExtension(
Tab: ctx => {
const [success] = std.command
.chain()
.canIndentParagraph()
.indentParagraph()
.pipe(canIndentParagraphCommand)
.pipe(indentParagraphCommand)
.run();
if (!success) {
return;
@@ -201,8 +215,8 @@ export const ParagraphKeymapExtension = KeymapExtension(
'Shift-Tab': ctx => {
const [success] = std.command
.chain()
.canDedentParagraph()
.dedentParagraph()
.pipe(canDedentParagraphCommand)
.pipe(dedentParagraphCommand)
.run();
if (!success) {
return;
@@ -1,13 +1,8 @@
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 { ParagraphBlockAdapterExtensions } from './adapters/extension.js';
import { commands } from './commands/index.js';
import {
ParagraphKeymapExtension,
ParagraphTextKeymapExtension,
@@ -17,7 +12,6 @@ import { ParagraphBlockService } from './paragraph-service.js';
export const ParagraphBlockSpec: ExtensionType[] = [
FlavourExtension('affine:paragraph'),
ParagraphBlockService,
CommandExtension(commands),
BlockViewExtension('affine:paragraph', literal`affine-paragraph`),
ParagraphTextKeymapExtension,
ParagraphKeymapExtension,