mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +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:
@@ -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(
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user