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

@@ -14,6 +14,7 @@ import {
BlocksUtils,
DocModeProvider,
EditPropsStore,
getSelectedBlocksCommand,
NoteDisplayMode,
NotificationProvider,
RefNodeSlotsProvider,
@@ -216,13 +217,10 @@ const REPLACE_SELECTION = {
) => {
const currentTextSelection = currentSelections.text;
const currentBlockSelections = currentSelections.blocks;
const [_, data] = host.command
.chain()
.getSelectedBlocks({
currentTextSelection,
currentBlockSelections,
})
.run();
const [_, data] = host.command.exec(getSelectedBlocksCommand, {
currentTextSelection,
currentBlockSelections,
});
if (!data.selectedBlocks) return false;
reportResponse('result:replace');
@@ -264,14 +262,11 @@ const INSERT_BELOW = {
const currentTextSelection = currentSelections.text;
const currentBlockSelections = currentSelections.blocks;
const currentImageSelections = currentSelections.images;
const [_, data] = host.command
.chain()
.getSelectedBlocks({
currentTextSelection,
currentBlockSelections,
currentImageSelections,
})
.run();
const [_, data] = host.command.exec(getSelectedBlocksCommand, {
currentTextSelection,
currentBlockSelections,
currentImageSelections,
});
if (!data.selectedBlocks) return false;
reportResponse('result:insert');
await insertBelow(

View File

@@ -2,6 +2,7 @@ import type { Chain, InitCommandCtx } from '@blocksuite/affine/block-std';
import {
type AIItemGroupConfig,
type AISubItemConfig,
getSelectedModelsCommand,
matchFlavours,
} from '@blocksuite/affine/blocks';
@@ -94,7 +95,7 @@ const blockActionTrackerOptions: BlockSuitePresets.TrackerOptions = {
const textBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
const [_, ctx] = chain
.getSelectedModels({
.pipe(getSelectedModelsCommand, {
types: ['block', 'text'],
})
.run();
@@ -108,7 +109,7 @@ const textBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
const codeBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
const [_, ctx] = chain
.getSelectedModels({
.pipe(getSelectedModelsCommand, {
types: ['block', 'text'],
})
.run();
@@ -121,7 +122,7 @@ const codeBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
const imageBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
const [_, ctx] = chain
.getSelectedModels({
.pipe(getSelectedModelsCommand, {
types: ['block'],
})
.run();
@@ -268,7 +269,7 @@ const GenerateWithAIGroup: AIItemGroupConfig = {
handler: actionToHandler('createHeadings', AIPenIconWithAnimation),
showWhen: chain => {
const [_, ctx] = chain
.getSelectedModels({
.pipe(getSelectedModelsCommand, {
types: ['block', 'text'],
})
.run();

View File

@@ -6,7 +6,10 @@ import {
type TextSelection,
} from '@blocksuite/affine/block-std';
import type { AffineAIPanelWidget } from '@blocksuite/affine/blocks';
import { isInsideEdgelessEditor } from '@blocksuite/affine/blocks';
import {
deleteTextCommand,
isInsideEdgelessEditor,
} from '@blocksuite/affine/blocks';
import { type BlockModel, Slice } from '@blocksuite/affine/store';
import {
@@ -107,7 +110,7 @@ export const replace = async (
);
if (textSelection) {
host.std.command.exec('deleteText', { textSelection });
host.std.command.exec(deleteTextCommand, { textSelection });
const { snapshot, transformer } = await markdownToSnapshot(content, host);
await transformer.snapshotToSlice(
snapshot,

View File

@@ -3,6 +3,8 @@ import {
BlocksUtils,
DocModeProvider,
embedSyncedDocMiddleware,
getImageSelectionsCommand,
getSelectedBlocksCommand,
type ImageBlockModel,
isInsideEdgelessEditor,
MarkdownAdapter,
@@ -70,8 +72,8 @@ async function extractPageSelected(
} else if (!hasText && hasImages && images.length === 1) {
host.command
.chain()
.tryAll(chain => [chain.getImageSelections()])
.getSelectedBlocks({
.tryAll(chain => [chain.pipe(getImageSelectionsCommand)])
.pipe(getSelectedBlocksCommand, {
types: ['image'],
})
.run();

View File

@@ -5,6 +5,11 @@ import {
type CopilotTool,
EdgelessRootService,
type FrameBlockModel,
getBlockSelectionsCommand,
getImageSelectionsCommand,
getSelectedBlocksCommand,
getSelectedModelsCommand,
getTextSelectionCommand,
ImageBlockModel,
type SurfaceBlockComponent,
} from '@blocksuite/affine/blocks';
@@ -95,12 +100,9 @@ export async function selectedToPng(editor: EditorHost) {
}
export function getSelectedModels(editorHost: EditorHost) {
const chain = editorHost.std.command.chain();
const [_, ctx] = chain
.getSelectedModels({
types: ['block', 'text'],
})
.run();
const [_, ctx] = editorHost.std.command.exec(getSelectedModelsCommand, {
types: ['block', 'text'],
});
const { selectedModels } = ctx;
return selectedModels;
}
@@ -246,11 +248,11 @@ export const getSelections = (
const [_, data] = host.command
.chain()
.tryAll(chain => [
chain.getTextSelection(),
chain.getBlockSelections(),
chain.getImageSelections(),
chain.pipe(getTextSelectionCommand),
chain.pipe(getBlockSelectionsCommand),
chain.pipe(getImageSelectionsCommand),
])
.getSelectedBlocks({ types: ['text', 'block', 'image'], mode })
.pipe(getSelectedBlocksCommand, { types: ['text', 'block', 'image'], mode })
.run();
return data;
@@ -260,11 +262,11 @@ export const getSelectedImagesAsBlobs = async (host: EditorHost) => {
const [_, data] = host.command
.chain()
.tryAll(chain => [
chain.getTextSelection(),
chain.getBlockSelections(),
chain.getImageSelections(),
chain.pipe(getTextSelectionCommand),
chain.pipe(getBlockSelectionsCommand),
chain.pipe(getImageSelectionsCommand),
])
.getSelectedBlocks({
.pipe(getSelectedBlocksCommand, {
types: ['block', 'image'],
})
.run();

View File

@@ -1,5 +1,10 @@
import { FeatureFlagService } from '@affine/core/modules/feature-flag';
import { type DocMode, getLastNoteBlock } from '@blocksuite/affine/blocks';
import {
appendParagraphCommand,
type DocMode,
focusBlockEnd,
getLastNoteBlock,
} from '@blocksuite/affine/blocks';
import { Slot } from '@blocksuite/affine/global/utils';
import type {
AffineEditorContainer,
@@ -148,14 +153,15 @@ export const BlocksuiteEditorContainer = forwardRef<
lastBlock.flavour === 'affine:paragraph' &&
lastBlock.text?.length === 0
) {
std.command.exec('focusBlockEnd' as never, {
focusBlock: std.view.getBlock(lastBlock.id) as never,
const focusBlock = std.view.getBlock(lastBlock.id) ?? undefined;
std.command.exec(focusBlockEnd, {
focusBlock,
});
return;
}
}
std.command.exec('appendParagraph' as never, {});
std.command.exec(appendParagraphCommand);
}, [affineEditorContainerProxy, page, shared]);
return (

View File

@@ -53,6 +53,7 @@ import {
EdgelessRootBlockComponent,
EmbedLinkedDocBlockComponent,
GenerateDocUrlExtension,
insertLinkByQuickSearchCommand,
MobileSpecsPatches,
NativeClipboardExtension,
NoteConfigExtension,
@@ -460,34 +461,28 @@ export function patchQuickSearchService(framework: FrameworkProvider) {
(item.name === 'Linked Doc' || item.name === 'Link')
) {
item.action = async ({ rootComponent }) => {
// @ts-expect-error fixme
const { success, insertedLinkType } =
// @ts-expect-error fixme
rootComponent.std.command.exec('insertLinkByQuickSearch');
const [success, { insertedLinkType }] =
rootComponent.std.command.exec(insertLinkByQuickSearchCommand);
if (!success) return;
insertedLinkType
?.then(
(type: {
flavour?: 'affine:embed-linked-doc' | 'affine:bookmark';
}) => {
const flavour = type?.flavour;
if (!flavour) return;
?.then(type => {
const flavour = type?.flavour;
if (!flavour) return;
if (flavour === 'affine:bookmark') {
track.doc.editor.slashMenu.bookmark();
return;
}
if (flavour === 'affine:embed-linked-doc') {
track.doc.editor.slashMenu.linkDoc({
control: 'linkDoc',
});
return;
}
if (flavour === 'affine:bookmark') {
track.doc.editor.slashMenu.bookmark();
return;
}
)
if (flavour === 'affine:embed-linked-doc') {
track.doc.editor.slashMenu.linkDoc({
control: 'linkDoc',
});
return;
}
})
.catch(console.error);
};
}

View File

@@ -6,7 +6,14 @@ import { useI18n } from '@affine/i18n';
import { track } from '@affine/track';
import { type EditorHost } from '@blocksuite/affine/block-std';
import { GfxBlockElementModel } from '@blocksuite/affine/block-std/gfx';
import type { DocMode, EdgelessRootService } from '@blocksuite/affine/blocks';
import {
type DocMode,
type EdgelessRootService,
getBlockSelectionsCommand,
getImageSelectionsCommand,
getSelectedModelsCommand,
getTextSelectionCommand,
} from '@blocksuite/affine/blocks';
import { useService } from '@toeverything/infra';
import { useCallback } from 'react';
@@ -97,11 +104,11 @@ export const getSelectedNodes = (
const [success, ctx] = std.command
.chain()
.tryAll(chain => [
chain.getTextSelection(),
chain.getBlockSelections(),
chain.getImageSelections(),
chain.pipe(getTextSelectionCommand),
chain.pipe(getBlockSelectionsCommand),
chain.pipe(getImageSelectionsCommand),
])
.getSelectedModels({
.pipe(getSelectedModelsCommand, {
mode: 'highest',
})
.run();