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

@@ -1,17 +1,11 @@
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 { BookmarkBlockAdapterExtensions } from './adapters/extension.js';
import { commands } from './commands/index.js';
export const BookmarkBlockSpec: ExtensionType[] = [
FlavourExtension('affine:bookmark'),
CommandExtension(commands),
BlockViewExtension('affine:bookmark', model => {
return model.parent?.flavour === 'affine:surface'
? literal`affine-edgeless-bookmark`

View File

@@ -1,9 +1,2 @@
import type { BlockCommands } from '@blocksuite/block-std';
import { insertBookmarkCommand } from './insert-bookmark.js';
import { insertLinkByQuickSearchCommand } from './insert-link-by-quick-search.js';
export const commands: BlockCommands = {
insertBookmark: insertBookmarkCommand,
insertLinkByQuickSearch: insertLinkByQuickSearchCommand,
};
export { insertBookmarkCommand } from './insert-bookmark.js';
export { insertLinkByQuickSearchCommand } from './insert-link-by-quick-search.js';

View File

@@ -5,11 +5,7 @@ import type { EmbedCardStyle } from '@blocksuite/affine-model';
import { EmbedOptionProvider } from '@blocksuite/affine-shared/services';
import type { Command } from '@blocksuite/block-std';
export const insertBookmarkCommand: Command<
never,
'insertedLinkType',
{ url: string }
> = (ctx, next) => {
export const insertBookmarkCommand: Command<{ url: string }> = (ctx, next) => {
const { url, std } = ctx;
const embedOptions = std.get(EmbedOptionProvider).getEmbedBlockOptions(url);

View File

@@ -1,10 +1,15 @@
import type { InsertedLinkType } from '@blocksuite/affine-block-embed';
import {
type InsertedLinkType,
insertEmbedLinkedDocCommand,
} from '@blocksuite/affine-block-embed';
import { QuickSearchProvider } from '@blocksuite/affine-shared/services';
import type { Command } from '@blocksuite/block-std';
import { insertBookmarkCommand } from './insert-bookmark';
export const insertLinkByQuickSearchCommand: Command<
never,
'insertedLinkType'
{},
{ insertedLinkType: Promise<InsertedLinkType> }
> = (ctx, next) => {
const { std } = ctx;
const quickSearchService = std.getOptional(QuickSearchProvider);
@@ -20,7 +25,7 @@ export const insertLinkByQuickSearchCommand: Command<
// add linked doc
if ('docId' in result) {
std.command.exec('insertEmbedLinkedDoc', {
std.command.exec(insertEmbedLinkedDocCommand, {
docId: result.docId,
params: result.params,
});
@@ -31,7 +36,7 @@ export const insertLinkByQuickSearchCommand: Command<
// add normal link;
if ('externalUrl' in result) {
std.command.exec('insertBookmark', { url: result.externalUrl });
std.command.exec(insertBookmarkCommand, { url: result.externalUrl });
return {
flavour: 'affine:bookmark',
};

View File

@@ -1,7 +1,5 @@
import { BookmarkBlockComponent } from './bookmark-block';
import { BookmarkEdgelessBlockComponent } from './bookmark-edgeless-block';
import type { insertBookmarkCommand } from './commands/insert-bookmark';
import type { insertLinkByQuickSearchCommand } from './commands/insert-link-by-quick-search';
import { BookmarkCard } from './components/bookmark-card';
import {
EmbedCardCreateModal,
@@ -24,12 +22,3 @@ export function effects() {
EmbedCardEditCaptionEditModal
);
}
declare global {
namespace BlockSuite {
interface Commands {
insertBookmark: typeof insertBookmarkCommand;
insertLinkByQuickSearch: typeof insertLinkByQuickSearchCommand;
}
}
}

View File

@@ -1,4 +1,5 @@
export * from './adapters';
export * from './bookmark-block';
export * from './bookmark-spec';
export * from './commands';
export * from './components';

View File

@@ -1,8 +1,14 @@
import { deleteTextCommand } from '@blocksuite/affine-components/rich-text';
import {
HtmlAdapter,
pasteMiddleware,
PlainTextAdapter,
} from '@blocksuite/affine-shared/adapters';
import {
getBlockIndexCommand,
getBlockSelectionsCommand,
getTextSelectionCommand,
} from '@blocksuite/affine-shared/commands';
import {
type BlockComponent,
Clipboard,
@@ -40,13 +46,13 @@ export class CodeClipboardController {
this._std.command
.chain()
.try(cmd => [
cmd.getTextSelection().inline<'currentSelectionPath'>((ctx, next) => {
cmd.pipe(getTextSelectionCommand).pipe((ctx, next) => {
const textSelection = ctx.currentTextSelection;
if (!textSelection) return;
const end = textSelection.to ?? textSelection.from;
next({ currentSelectionPath: end.blockId });
}),
cmd.getBlockSelections().inline<'currentSelectionPath'>((ctx, next) => {
cmd.pipe(getBlockSelectionsCommand).pipe((ctx, next) => {
const currentBlockSelections = ctx.currentBlockSelections;
if (!currentBlockSelections) return;
const blockSelection = currentBlockSelections.at(-1);
@@ -54,9 +60,9 @@ export class CodeClipboardController {
next({ currentSelectionPath: blockSelection.blockId });
}),
])
.getBlockIndex()
.try(cmd => [cmd.getTextSelection().deleteText()])
.inline((ctx, next) => {
.pipe(getBlockIndexCommand)
.try(cmd => [cmd.pipe(getTextSelectionCommand).pipe(deleteTextCommand)])
.pipe((ctx, next) => {
if (!ctx.parentBlock) {
return;
}

View File

@@ -1,5 +1,5 @@
import type { DatabaseBlockModel } from '@blocksuite/affine-model';
import type { BlockCommands, Command } from '@blocksuite/block-std';
import type { Command } from '@blocksuite/block-std';
import type { BlockModel, Store } from '@blocksuite/store';
import {
@@ -8,12 +8,14 @@ import {
} from './data-source';
export const insertDatabaseBlockCommand: Command<
'selectedModels',
'insertedDatabaseBlockId',
{
selectedModels?: BlockModel[];
viewType: string;
place?: 'after' | 'before';
removeEmptyLine?: boolean;
},
{
insertedDatabaseBlockId: string;
}
> = (ctx, next) => {
const { selectedModels, viewType, place, removeEmptyLine, std } = ctx;
@@ -65,7 +67,3 @@ export const initDatabaseBlock = (
doc.addBlock('affine:paragraph', {}, parent.id);
}
};
export const commands: BlockCommands = {
insertDatabaseBlock: insertDatabaseBlockCommand,
};

View File

@@ -3,6 +3,7 @@ import type {
ColumnUpdater,
DatabaseBlockModel,
} from '@blocksuite/affine-model';
import { getSelectedModelsCommand } from '@blocksuite/affine-shared/commands';
import { FeatureFlagService } from '@blocksuite/affine-shared/services';
import {
insertPositionToIndex,
@@ -517,12 +518,9 @@ export const databaseViewInitTemplate = (
datasource.viewManager.viewAdd(viewType);
};
export const convertToDatabase = (host: EditorHost, viewType: string) => {
const [_, ctx] = host.std.command
.chain()
.getSelectedModels({
types: ['block', 'text'],
})
.run();
const [_, ctx] = host.std.command.exec(getSelectedModelsCommand, {
types: ['block', 'text'],
});
const { selectedModels } = ctx;
const firstModel = selectedModels?.[0];
if (!firstModel) return;

View File

@@ -1,17 +1,11 @@
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 { DatabaseBlockAdapterExtensions } from './adapters/extension.js';
import { commands } from './commands.js';
export const DatabaseBlockSpec: ExtensionType[] = [
FlavourExtension('affine:database'),
CommandExtension(commands),
BlockViewExtension('affine:database', literal`affine-database`),
DatabaseBlockAdapterExtensions,
].flat();

View File

@@ -1,4 +1,3 @@
import type { insertDatabaseBlockCommand } from './commands';
import { CenterPeek } from './components/layout';
import { DatabaseTitle } from './components/title';
import type { DatabaseOptionsConfig } from './config';
@@ -44,20 +43,5 @@ declare global {
interface BlockConfigs {
'affine:database': Partial<DatabaseOptionsConfig>;
}
interface CommandContext {
insertedDatabaseBlockId?: string;
}
interface Commands {
/**
* insert a database block after or before the current block selection
* @param latex the LaTeX content. A input dialog will be shown if not provided
* @param removeEmptyLine remove the current block if it is empty
* @param place where to insert the LaTeX block
* @returns the id of the inserted LaTeX block
*/
insertDatabaseBlock: typeof insertDatabaseBlockCommand;
}
}
}

View File

@@ -1,8 +1,5 @@
import type * as CommandType from '@blocksuite/affine-shared/commands';
declare type _GLOBAL_ = typeof CommandType;
export * from './adapters';
export * from './commands';
export type { DatabaseOptionsConfig } from './config';
export * from './data-source';
export * from './database-block';

View File

@@ -65,12 +65,4 @@ export class DatabaseSelection extends BaseSelection {
}
}
declare global {
namespace BlockSuite {
interface Selection {
database: typeof DatabaseSelection;
}
}
}
export const DatabaseSelectionExtension = SelectionExtension(DatabaseSelection);

View File

@@ -1,7 +1 @@
import type { BlockCommands } from '@blocksuite/block-std';
import { insertEdgelessTextCommand } from './insert-edgeless-text.js';
export const commands: BlockCommands = {
insertEdgelessText: insertEdgelessTextCommand,
};
export { insertEdgelessTextCommand } from './insert-edgeless-text.js';

View File

@@ -14,11 +14,12 @@ import {
} from '../edgeless-text-block.js';
export const insertEdgelessTextCommand: Command<
never,
'textId',
{
x: number;
y: number;
},
{
textId: string;
}
> = (ctx, next) => {
const { std, x, y } = ctx;

View File

@@ -1,4 +1,5 @@
import { TextUtils } from '@blocksuite/affine-block-surface';
import { formatBlockCommand } from '@blocksuite/affine-components/rich-text';
import type { EdgelessTextBlockModel } from '@blocksuite/affine-model';
import { ThemeProvider } from '@blocksuite/affine-shared/services';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
@@ -98,21 +99,21 @@ export class EdgelessTextBlockComponent extends GfxBlockComponent<EdgelessTextBl
);
if (key === 'fontStyle') {
command.exec('formatBlock', {
command.exec(formatBlockCommand, {
blockSelections,
styles: {
italic: null,
},
});
} else if (key === 'color') {
command.exec('formatBlock', {
command.exec(formatBlockCommand, {
blockSelections,
styles: {
color: null,
},
});
} else if (key === 'fontWeight') {
command.exec('formatBlock', {
command.exec(formatBlockCommand, {
blockSelections,
styles: {
bold: null,

View File

@@ -1,10 +1,7 @@
import { BlockViewExtension, CommandExtension } from '@blocksuite/block-std';
import { BlockViewExtension } from '@blocksuite/block-std';
import type { ExtensionType } from '@blocksuite/store';
import { literal } from 'lit/static-html.js';
import { commands } from './commands/index.js';
export const EdgelessTextBlockSpec: ExtensionType[] = [
CommandExtension(commands),
BlockViewExtension('affine:edgeless-text', literal`affine-edgeless-text`),
];

View File

@@ -1,17 +1,5 @@
import type { insertEdgelessTextCommand } from './commands/insert-edgeless-text';
import { EdgelessTextBlockComponent } from './edgeless-text-block';
export function effects() {
customElements.define('affine-edgeless-text', EdgelessTextBlockComponent);
}
declare global {
namespace BlockSuite {
interface CommandContext {
textId?: string;
}
interface Commands {
insertEdgelessText: typeof insertEdgelessTextCommand;
}
}
}

View File

@@ -1,6 +1,3 @@
import type * as CommandType from '@blocksuite/affine-shared/commands';
declare type _GLOBAL_ = typeof CommandType;
export * from './commands';
export * from './edgeless-text-block.js';
export * from './edgeless-text-spec.js';

View File

@@ -12,10 +12,6 @@ import { EmbedHtmlBlockComponent } from './embed-html-block';
import { EmbedHtmlFullscreenToolbar } from './embed-html-block/components/fullscreen-toolbar';
import { EmbedEdgelessHtmlBlockComponent } from './embed-html-block/embed-edgeless-html-block';
import { EmbedLinkedDocBlockComponent } from './embed-linked-doc-block';
import type {
InsertedLinkType,
insertEmbedLinkedDocCommand,
} from './embed-linked-doc-block/commands/insert-embed-linked-doc';
import { EmbedEdgelessLinkedDocBlockComponent } from './embed-linked-doc-block/embed-edgeless-linked-doc-block';
import {
EmbedLoomBlockComponent,
@@ -119,12 +115,5 @@ declare global {
'affine:embed-loom': EmbedLoomBlockService;
'affine:embed-youtube': EmbedYoutubeBlockService;
}
interface CommandContext {
insertedLinkType?: Promise<InsertedLinkType>;
}
interface Commands {
insertEmbedLinkedDoc: typeof insertEmbedLinkedDocCommand;
}
}
}

View File

@@ -1,9 +1 @@
import type { BlockCommands } from '@blocksuite/block-std';
import { insertEmbedLinkedDocCommand } from './insert-embed-linked-doc.js';
export const commands: BlockCommands = {
insertEmbedLinkedDoc: insertEmbedLinkedDocCommand,
};
export type { InsertedLinkType } from './insert-embed-linked-doc';
export * from './insert-embed-linked-doc';

View File

@@ -7,14 +7,10 @@ export type InsertedLinkType = {
flavour?: 'affine:bookmark' | 'affine:embed-linked-doc';
} | null;
export const insertEmbedLinkedDocCommand: Command<
never,
'insertedLinkType',
{
docId: string;
params?: ReferenceParams;
}
> = (ctx, next) => {
export const insertEmbedLinkedDocCommand: Command<{
docId: string;
params?: ReferenceParams;
}> = (ctx, next) => {
const { docId, params, std } = ctx;
const flavour = 'affine:embed-linked-doc';
const targetStyle: EmbedCardStyle = 'vertical';

View File

@@ -1,4 +1,7 @@
import { EdgelessCRUDIdentifier } from '@blocksuite/affine-block-surface';
import {
EdgelessCRUDIdentifier,
reassociateConnectorsCommand,
} from '@blocksuite/affine-block-surface';
import {
EMBED_CARD_HEIGHT,
EMBED_CARD_WIDTH,
@@ -51,7 +54,7 @@ export class EmbedEdgelessLinkedDocBlockComponent extends toEdgelessEmbedBlock(
surface
);
this.std.command.exec('reassociateConnectors', {
this.std.command.exec(reassociateConnectorsCommand, {
oldId: id,
newId,
});

View File

@@ -1,12 +1,10 @@
import { BlockViewExtension, CommandExtension } from '@blocksuite/block-std';
import { BlockViewExtension } from '@blocksuite/block-std';
import type { ExtensionType } from '@blocksuite/store';
import { literal } from 'lit/static-html.js';
import { EmbedLinkedDocBlockAdapterExtensions } from './adapters/extension.js';
import { commands } from './commands/index.js';
export const EmbedLinkedDocBlockSpec: ExtensionType[] = [
CommandExtension(commands),
BlockViewExtension('affine:embed-linked-doc', model => {
return model.parent?.flavour === 'affine:surface'
? literal`affine-embed-edgeless-linked-doc-block`

View File

@@ -1,4 +1,4 @@
export * from './adapters';
export type { InsertedLinkType } from './commands';
export * from './commands';
export * from './embed-linked-doc-block';
export * from './embed-linked-doc-spec';

View File

@@ -1,4 +1,7 @@
import { EdgelessCRUDIdentifier } from '@blocksuite/affine-block-surface';
import {
EdgelessCRUDIdentifier,
reassociateConnectorsCommand,
} from '@blocksuite/affine-block-surface';
import type { AliasInfo } from '@blocksuite/affine-model';
import {
EMBED_CARD_HEIGHT,
@@ -144,7 +147,7 @@ export class EmbedEdgelessSyncedDocBlockComponent extends toEdgelessEmbedBlock(
surface
);
this.std.command.exec('reassociateConnectors', {
this.std.command.exec(reassociateConnectorsCommand, {
oldId: id,
newId,
});

View File

@@ -13,6 +13,8 @@
"author": "toeverything",
"license": "MIT",
"dependencies": {
"@blocksuite/affine-block-note": "workspace:*",
"@blocksuite/affine-block-surface": "workspace:*",
"@blocksuite/affine-components": "workspace:*",
"@blocksuite/affine-model": "workspace:*",
"@blocksuite/affine-shared": "workspace:*",

View File

@@ -1,9 +1 @@
import { getImageSelectionsCommand } from '@blocksuite/affine-shared/commands';
import type { BlockCommands } from '@blocksuite/block-std';
import { insertImagesCommand } from './insert-images.js';
export const commands: BlockCommands = {
getImageSelections: getImageSelectionsCommand,
insertImages: insertImagesCommand,
};
export { insertImagesCommand } from './insert-images.js';

View File

@@ -1,12 +1,18 @@
import { getImageFilesFromLocal } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
import { addSiblingImageBlock } from '../utils.js';
export const insertImagesCommand: Command<
'selectedModels',
'insertedImageIds',
{ removeEmptyLine?: boolean; place?: 'after' | 'before' }
{
selectedModels?: BlockModel[];
removeEmptyLine?: boolean;
place?: 'after' | 'before';
},
{
insertedImageIds: Promise<string[]>;
}
> = (ctx, next) => {
const { selectedModels, place, removeEmptyLine, std } = ctx;
if (!selectedModels) return;

View File

@@ -1,5 +1,13 @@
import { focusBlockEnd, focusBlockStart } from '@blocksuite/affine-block-note';
import {
getNextBlockCommand,
getPrevBlockCommand,
} from '@blocksuite/affine-shared/commands';
import { ImageSelection } from '@blocksuite/affine-shared/selection';
import type { UIEventStateContext } from '@blocksuite/block-std';
import type {
BlockComponent,
UIEventStateContext,
} from '@blocksuite/block-std';
import {
BlockSelection,
ShadowlessElement,
@@ -136,15 +144,14 @@ export class ImageBlockPageComponent extends WithDisposable(ShadowlessElement) {
std.command
.chain()
.getNextBlock({ path: this.block.blockId })
.inline((ctx, next) => {
.pipe(getNextBlockCommand, { path: this.block.blockId })
.pipe<{ focusBlock: BlockComponent }>((ctx, next) => {
const { nextBlock } = ctx;
if (!nextBlock) return;
return next({ focusBlock: nextBlock });
})
// @ts-expect-error FIXME(command): BS-2216
.focusBlockStart()
.pipe(focusBlockStart)
.run();
return true;
},
@@ -162,15 +169,14 @@ export class ImageBlockPageComponent extends WithDisposable(ShadowlessElement) {
std.command
.chain()
.getPrevBlock({ path: this.block.blockId })
.inline((ctx, next) => {
.pipe(getPrevBlockCommand, { path: this.block.blockId })
.pipe<{ focusBlock: BlockComponent }>((ctx, next) => {
const { prevBlock } = ctx;
if (!prevBlock) return;
return next({ focusBlock: prevBlock });
})
// @ts-expect-error FIXME(command): BS-2216
.focusBlockEnd()
.pipe(focusBlockEnd)
.run();
return true;
},

View File

@@ -1,6 +1,3 @@
import type { getImageSelectionsCommand } from '@blocksuite/affine-shared/commands';
import type { insertImagesCommand } from './commands/insert-images.js';
import { ImageBlockFallbackCard } from './components/image-block-fallback.js';
import { ImageBlockPageComponent } from './components/page-image-block.js';
import { ImageBlockComponent } from './image-block.js';
@@ -16,21 +13,6 @@ export function effects() {
declare global {
namespace BlockSuite {
interface CommandContext {
insertedImageIds?: Promise<string[]>;
}
interface Commands {
getImageSelections: typeof getImageSelectionsCommand;
/**
* open file dialog to insert images before or after the current block selection
* @param removeEmptyLine remove the current block if it is empty
* @param place where to insert the images
* @returns a promise that resolves to the inserted image ids
*/
insertImages: typeof insertImagesCommand;
}
interface BlockServices {
'affine:image': ImageBlockService;
}

View File

@@ -1,6 +1,5 @@
import {
BlockViewExtension,
CommandExtension,
FlavourExtension,
WidgetViewMapExtension,
} from '@blocksuite/block-std';
@@ -8,13 +7,11 @@ import type { ExtensionType } from '@blocksuite/store';
import { literal } from 'lit/static-html.js';
import { ImageBlockAdapterExtensions } from './adapters/extension.js';
import { commands } from './commands/index.js';
import { ImageBlockService, ImageDropOption } from './image-service.js';
export const ImageBlockSpec: ExtensionType[] = [
FlavourExtension('affine:image'),
ImageBlockService,
CommandExtension(commands),
BlockViewExtension('affine:image', model => {
const parent = model.doc.getParent(model.id);

View File

@@ -3,6 +3,7 @@ import type * as SurfaceEffects from '@blocksuite/affine-block-surface/effects';
declare type _GLOBAL_ = typeof SurfaceEffects;
export * from './adapters';
export * from './commands';
export * from './image-block';
export * from './image-edgeless-block';
export * from './image-service';

View File

@@ -1,3 +1,4 @@
import { autoResizeElementsCommand } from '@blocksuite/affine-block-surface';
import { toast } from '@blocksuite/affine-components/toast';
import type {
AttachmentBlockProps,
@@ -516,7 +517,7 @@ export async function addImages(
editing: false,
});
if (isMultipleFiles) {
std.command.exec('autoResizeElements');
std.command.exec(autoResizeElementsCommand);
}
return blockIds;
}

View File

@@ -7,6 +7,8 @@
},
"include": ["./src"],
"references": [
{ "path": "../block-note" },
{ "path": "../block-surface" },
{ "path": "../components" },
{ "path": "../model" },
{ "path": "../shared" },

View File

@@ -1,16 +1,19 @@
import type { LatexProps } from '@blocksuite/affine-model';
import type { BlockCommands, Command } from '@blocksuite/block-std';
import type { Command } from '@blocksuite/block-std';
import { assertInstanceOf } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
import { LatexBlockComponent } from './latex-block.js';
export const insertLatexBlockCommand: Command<
'selectedModels',
'insertedLatexBlockId',
{
latex?: string;
place?: 'after' | 'before';
removeEmptyLine?: boolean;
selectedModels?: BlockModel[];
},
{
insertedLatexBlockId: Promise<string>;
}
> = (ctx, next) => {
const { selectedModels, latex, place, removeEmptyLine, std } = ctx;
@@ -51,7 +54,3 @@ export const insertLatexBlockCommand: Command<
}),
});
};
export const commands: BlockCommands = {
insertLatexBlock: insertLatexBlockCommand,
};

View File

@@ -1,25 +1,5 @@
import type { insertLatexBlockCommand } from './commands';
import { LatexBlockComponent } from './latex-block';
export function effects() {
customElements.define('affine-latex', LatexBlockComponent);
}
declare global {
namespace BlockSuite {
interface CommandContext {
insertedLatexBlockId?: Promise<string>;
}
interface Commands {
/**
* insert a LaTeX block after or before the current block selection
* @param latex the LaTeX content. A input dialog will be shown if not provided
* @param place where to insert the LaTeX block
* @param removeEmptyLine remove the current block if it is empty
* @returns the id of the inserted LaTeX block
*/
insertLatexBlock: typeof insertLatexBlockCommand;
}
}
}

View File

@@ -1,13 +1,9 @@
import type * as NoteType from '@blocksuite/affine-block-note/effects';
import type * as CommandsType from '@blocksuite/affine-shared/commands';
import type * as RemarkMathType from 'remark-math';
export * from './adapters';
export * from './commands';
export * from './latex-block';
export * from './latex-spec';
// Global types
declare type _GLOBAl =
| typeof NoteType
| typeof CommandsType
| typeof RemarkMathType;
declare type _GLOBAl = typeof RemarkMathType;

View File

@@ -1,3 +1,4 @@
import { selectBlock } from '@blocksuite/affine-block-note';
import { CaptionedBlockComponent } from '@blocksuite/affine-components/caption';
import { createLitPortal } from '@blocksuite/affine-components/portal';
import type { LatexBlockModel } from '@blocksuite/affine-model';
@@ -96,7 +97,7 @@ export class LatexBlockComponent extends CaptionedBlockComponent<LatexBlockModel
}
selectBlock() {
this.host.command.exec('selectBlock', {
this.host.command.exec(selectBlock, {
focusBlock: this,
});
}

View File

@@ -1,12 +1,10 @@
import { BlockViewExtension, CommandExtension } from '@blocksuite/block-std';
import { BlockViewExtension } from '@blocksuite/block-std';
import type { ExtensionType } from '@blocksuite/store';
import { literal } from 'lit/static-html.js';
import { LatexBlockAdapterExtensions } from './adapters/extension.js';
import { commands } from './commands.js';
export const LatexBlockSpec: ExtensionType[] = [
BlockViewExtension('affine:latex', literal`affine-latex`),
CommandExtension(commands),
LatexBlockAdapterExtensions,
].flat();

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,

View File

@@ -3,12 +3,18 @@ import {
focusTextModel,
onModelTextUpdated,
} from '@blocksuite/affine-components/rich-text';
import {
getBlockSelectionsCommand,
getSelectedBlocksCommand,
getTextSelectionCommand,
} from '@blocksuite/affine-shared/commands';
import {
matchFlavours,
mergeToCodeModel,
transformModel,
} from '@blocksuite/affine-shared/utils';
import {
type BlockComponent,
BlockSelection,
type Command,
TextSelection,
@@ -21,9 +27,12 @@ type UpdateBlockConfig = {
};
export const updateBlockType: Command<
'selectedBlocks',
'updatedBlocks',
UpdateBlockConfig
UpdateBlockConfig & {
selectedBlocks?: BlockComponent[];
},
{
updatedBlocks: BlockModel[];
}
> = (ctx, next) => {
const { std, flavour, props } = ctx;
const host = std.host;
@@ -35,8 +44,11 @@ export const updateBlockType: Command<
if (selectedBlocks == null) {
const [result, ctx] = std.command
.chain()
.tryAll(chain => [chain.getTextSelection(), chain.getBlockSelections()])
.getSelectedBlocks({ types: ['text', 'block'] })
.tryAll(chain => [
chain.pipe(getTextSelectionCommand),
chain.pipe(getBlockSelectionsCommand),
])
.pipe(getSelectedBlocksCommand, { types: ['text', 'block'] })
.run();
if (result) {
selectedBlocks = ctx.selectedBlocks;
@@ -60,7 +72,10 @@ export const updateBlockType: Command<
);
}
const mergeToCode: Command<never, 'updatedBlocks'> = (_, next) => {
const mergeToCode: Command<{}, { updatedBlocks: BlockModel[] }> = (
_,
next
) => {
if (flavour !== 'affine:code') return;
const id = mergeToCodeModel(blockModels);
if (!id) return;
@@ -72,7 +87,10 @@ export const updateBlockType: Command<
}).catch(console.error);
return next({ updatedBlocks: [model] });
};
const appendDivider: Command<never, 'updatedBlocks'> = (_, next) => {
const appendDivider: Command<{}, { updatedBlocks: BlockModel[] }> = (
_,
next
) => {
if (flavour !== 'affine:divider') {
return false;
}
@@ -99,7 +117,7 @@ export const updateBlockType: Command<
return next({ updatedBlocks: [newModel] });
};
const focusText: Command<'updatedBlocks'> = (ctx, next) => {
const focusText: Command<{ updatedBlocks: BlockModel[] }> = (ctx, next) => {
const { updatedBlocks } = ctx;
if (!updatedBlocks || updatedBlocks.length === 0) {
return false;
@@ -139,7 +157,7 @@ export const updateBlockType: Command<
return next();
};
const focusBlock: Command<'updatedBlocks'> = (ctx, next) => {
const focusBlock: Command<{ updatedBlocks: BlockModel[] }> = (ctx, next) => {
const { updatedBlocks } = ctx;
if (!updatedBlocks || updatedBlocks.length === 0) {
return false;
@@ -165,15 +183,15 @@ export const updateBlockType: Command<
const [result, resultCtx] = std.command
.chain()
.inline((_, next) => {
.pipe((_, next) => {
doc.captureSync();
return next();
})
// update block type
.try<'updatedBlocks'>(chain => [
chain.inline<'updatedBlocks'>(mergeToCode),
chain.inline<'updatedBlocks'>(appendDivider),
chain.inline<'updatedBlocks'>((_, next) => {
.try<{ updatedBlocks: BlockModel[] }>(chain => [
chain.pipe(mergeToCode),
chain.pipe(appendDivider),
chain.pipe((_, next) => {
const newModels: BlockModel[] = [];
blockModels.forEach(model => {
if (
@@ -204,15 +222,15 @@ export const updateBlockType: Command<
])
// focus
.try(chain => [
chain.inline((_, next) => {
chain.pipe((_, next) => {
if (['affine:code', 'affine:divider'].includes(flavour)) {
return next();
}
return false;
}),
chain.inline(focusText),
chain.inline(focusBlock),
chain.inline((_, next) => next()),
chain.pipe(focusText),
chain.pipe(focusBlock),
chain.pipe((_, next) => next()),
])
.run();

View File

@@ -2,11 +2,11 @@ import { NoteDisplayMode } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
export const changeNoteDisplayMode: Command<
never,
never,
{ noteId: string; mode: NoteDisplayMode; stopCapture?: boolean }
> = (ctx, next) => {
export const changeNoteDisplayMode: Command<{
noteId: string;
mode: NoteDisplayMode;
stopCapture?: boolean;
}> = (ctx, next) => {
const { std, noteId, mode, stopCapture } = ctx;
const noteBlockModel = std.store.getBlock(noteId)?.model;

View File

@@ -1,14 +1,12 @@
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
export const dedentBlockToRoot: Command<
never,
never,
{
blockId?: string;
stopCapture?: boolean;
}
> = (ctx, next) => {
import { dedentBlock } from './dedent-block';
export const dedentBlockToRoot: Command<{
blockId?: string;
stopCapture?: boolean;
}> = (ctx, next) => {
let { blockId } = ctx;
const { std, stopCapture = true } = ctx;
const { store } = std;
@@ -27,7 +25,7 @@ export const dedentBlockToRoot: Command<
if (stopCapture) store.captureSync();
changed = true;
}
std.command.exec('dedentBlock', { blockId: model.id, stopCapture: true });
std.command.exec(dedentBlock, { blockId: model.id, stopCapture: true });
parent = store.getParent(model);
}

View File

@@ -20,14 +20,10 @@ import type { Command } from '@blocksuite/block-std';
* - ddd
* - eee
*/
export const dedentBlock: Command<
never,
never,
{
blockId?: string;
stopCapture?: boolean;
}
> = (ctx, next) => {
export const dedentBlock: Command<{
blockId?: string;
stopCapture?: boolean;
}> = (ctx, next) => {
let { blockId } = ctx;
const { std, stopCapture = true } = ctx;
const { store } = std;

View File

@@ -1,14 +1,12 @@
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const dedentBlocksToRoot: Command<
never,
never,
{
blockIds?: string[];
stopCapture?: boolean;
}
> = (ctx, next) => {
import { dedentBlockToRoot } from './dedent-block-to-root';
export const dedentBlocksToRoot: Command<{
blockIds?: string[];
stopCapture?: boolean;
}> = (ctx, next) => {
let { blockIds } = ctx;
const { std, stopCapture = true } = ctx;
const { store } = std;
@@ -33,7 +31,7 @@ export const dedentBlocksToRoot: Command<
const model = blockIds[i];
const parent = store.getParent(model);
if (parent && !matchFlavours(parent, ['affine:note'])) {
std.command.exec('dedentBlockToRoot', {
std.command.exec(dedentBlockToRoot, {
blockId: model,
stopCapture: false,
});

View File

@@ -4,14 +4,12 @@ import {
} from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const dedentBlocks: Command<
never,
never,
{
blockIds?: string[];
stopCapture?: boolean;
}
> = (ctx, next) => {
import { dedentBlock } from './dedent-block';
export const dedentBlocks: Command<{
blockIds?: string[];
stopCapture?: boolean;
}> = (ctx, next) => {
let { blockIds } = ctx;
const { std, stopCapture = true } = ctx;
const { store, selection, range, host } = std;
@@ -72,7 +70,7 @@ export const dedentBlocks: Command<
.slice(firstDedentIndex)
.filter(id => !collapsedIds.includes(id));
dedentIds.reverse().forEach(id => {
std.command.exec('dedentBlock', { blockId: id, stopCapture: false });
std.command.exec(dedentBlock, { blockId: id, stopCapture: false });
});
const textSelection = selection.find(TextSelection);

View File

@@ -1,6 +1,12 @@
import { type Command, TextSelection } from '@blocksuite/block-std';
import {
type BlockComponent,
type Command,
TextSelection,
} from '@blocksuite/block-std';
export const focusBlockEnd: Command<'focusBlock'> = (ctx, next) => {
export const focusBlockEnd: Command<{
focusBlock?: BlockComponent;
}> = (ctx, next) => {
const { focusBlock, std } = ctx;
if (!focusBlock || !focusBlock.model.text) return;

View File

@@ -1,6 +1,12 @@
import { type Command, TextSelection } from '@blocksuite/block-std';
import {
type BlockComponent,
type Command,
TextSelection,
} from '@blocksuite/block-std';
export const focusBlockStart: Command<'focusBlock'> = (ctx, next) => {
export const focusBlockStart: Command<{
focusBlock?: BlockComponent;
}> = (ctx, next) => {
const { focusBlock, std } = ctx;
if (!focusBlock || !focusBlock.model.text) return;

View File

@@ -21,14 +21,10 @@ import type { Command } from '@blocksuite/block-std';
* - ddd
* - eee
*/
export const indentBlock: Command<
never,
never,
{
blockId?: string;
stopCapture?: boolean;
}
> = (ctx, next) => {
export const indentBlock: Command<{
blockId?: string;
stopCapture?: boolean;
}> = (ctx, next) => {
let { blockId } = ctx;
const { std, stopCapture = true } = ctx;
const { store } = std;

View File

@@ -5,14 +5,12 @@ import {
} from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const indentBlocks: Command<
never,
never,
{
blockIds?: string[];
stopCapture?: boolean;
}
> = (ctx, next) => {
import { indentBlock } from './indent-block';
export const indentBlocks: Command<{
blockIds?: string[];
stopCapture?: boolean;
}> = (ctx, next) => {
let { blockIds } = ctx;
const { std, stopCapture = true } = ctx;
const { store, selection, range, host } = std;
@@ -95,7 +93,7 @@ export const indentBlocks: Command<
}
indentIds.forEach(id => {
std.command.exec('indentBlock', { blockId: id, stopCapture: false });
std.command.exec(indentBlock, { blockId: id, stopCapture: false });
});
{

View File

@@ -1,42 +1,12 @@
import {
getBlockIndexCommand,
getBlockSelectionsCommand,
getNextBlockCommand,
getPrevBlockCommand,
getSelectedBlocksCommand,
} from '@blocksuite/affine-shared/commands';
import type { BlockCommands } from '@blocksuite/block-std';
import { updateBlockType } from './block-type.js';
import { changeNoteDisplayMode } from './change-note-display-mode.js';
import { dedentBlock } from './dedent-block.js';
import { dedentBlockToRoot } from './dedent-block-to-root.js';
import { dedentBlocks } from './dedent-blocks.js';
import { dedentBlocksToRoot } from './dedent-blocks-to-root.js';
import { focusBlockEnd } from './focus-block-end.js';
import { focusBlockStart } from './focus-block-start.js';
import { indentBlock } from './indent-block.js';
import { indentBlocks } from './indent-blocks.js';
import { selectBlock } from './select-block.js';
import { selectBlocksBetween } from './select-blocks-between.js';
export const commands: BlockCommands = {
// block
getBlockIndex: getBlockIndexCommand,
getPrevBlock: getPrevBlockCommand,
getNextBlock: getNextBlockCommand,
getSelectedBlocks: getSelectedBlocksCommand,
getBlockSelections: getBlockSelectionsCommand,
selectBlock,
selectBlocksBetween,
focusBlockStart,
focusBlockEnd,
updateBlockType,
indentBlock,
dedentBlock,
indentBlocks,
dedentBlocks,
dedentBlockToRoot,
dedentBlocksToRoot,
changeNoteDisplayMode,
};
export { updateBlockType } from './block-type.js';
export { changeNoteDisplayMode } from './change-note-display-mode.js';
export { dedentBlock } from './dedent-block.js';
export { dedentBlockToRoot } from './dedent-block-to-root.js';
export { dedentBlocks } from './dedent-blocks.js';
export { dedentBlocksToRoot } from './dedent-blocks-to-root.js';
export { focusBlockEnd } from './focus-block-end.js';
export { focusBlockStart } from './focus-block-start.js';
export { indentBlock } from './indent-block.js';
export { indentBlocks } from './indent-blocks.js';
export { selectBlock } from './select-block.js';
export { selectBlocksBetween } from './select-blocks-between.js';

View File

@@ -1,6 +1,12 @@
import { BlockSelection, type Command } from '@blocksuite/block-std';
import {
type BlockComponent,
BlockSelection,
type Command,
} from '@blocksuite/block-std';
export const selectBlock: Command<'focusBlock'> = (ctx, next) => {
export const selectBlock: Command<{
focusBlock?: BlockComponent;
}> = (ctx, next) => {
const { focusBlock, std } = ctx;
if (!focusBlock) {
return;

View File

@@ -1,10 +1,14 @@
import { BlockSelection, type Command } from '@blocksuite/block-std';
import {
type BlockComponent,
BlockSelection,
type Command,
} from '@blocksuite/block-std';
export const selectBlocksBetween: Command<
'focusBlock' | 'anchorBlock',
never,
{ tail: boolean }
> = (ctx, next) => {
export const selectBlocksBetween: Command<{
focusBlock?: BlockComponent;
anchorBlock?: BlockComponent;
tail: boolean;
}> = (ctx, next) => {
const { focusBlock, anchorBlock, tail } = ctx;
if (!focusBlock || !anchorBlock) {
return;

View File

@@ -1,18 +1,3 @@
import type { BlockComponent } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
import type { updateBlockType } from './commands/block-type';
import type { changeNoteDisplayMode } from './commands/change-note-display-mode';
import type { dedentBlock } from './commands/dedent-block';
import type { dedentBlockToRoot } from './commands/dedent-block-to-root';
import type { dedentBlocks } from './commands/dedent-blocks';
import type { dedentBlocksToRoot } from './commands/dedent-blocks-to-root';
import type { focusBlockEnd } from './commands/focus-block-end';
import type { focusBlockStart } from './commands/focus-block-start';
import type { indentBlock } from './commands/indent-block';
import type { indentBlocks } from './commands/indent-blocks';
import type { selectBlock } from './commands/select-block';
import type { selectBlocksBetween } from './commands/select-blocks-between';
import type { NoteConfig } from './config';
import { NoteBlockComponent } from './note-block';
import {
@@ -29,25 +14,6 @@ export function effects() {
declare global {
namespace BlockSuite {
interface Commands {
selectBlock: typeof selectBlock;
selectBlocksBetween: typeof selectBlocksBetween;
focusBlockStart: typeof focusBlockStart;
focusBlockEnd: typeof focusBlockEnd;
indentBlocks: typeof indentBlocks;
dedentBlock: typeof dedentBlock;
dedentBlocksToRoot: typeof dedentBlocksToRoot;
dedentBlocks: typeof dedentBlocks;
indentBlock: typeof indentBlock;
updateBlockType: typeof updateBlockType;
dedentBlockToRoot: typeof dedentBlockToRoot;
changeNoteDisplayMode: typeof changeNoteDisplayMode;
}
interface CommandContext {
focusBlock?: BlockComponent | null;
anchorBlock?: BlockComponent | null;
updatedBlocks?: BlockModel[];
}
interface BlockServices {
'affine:note': NoteBlockService;
}

View File

@@ -1,17 +1,34 @@
import { textConversionConfigs } from '@blocksuite/affine-components/rich-text';
import { NoteBlockSchema } from '@blocksuite/affine-model';
import {
getBlockSelectionsCommand,
getNextBlockCommand,
getPrevBlockCommand,
getTextSelectionCommand,
} from '@blocksuite/affine-shared/commands';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import {
type BlockComponent,
BlockSelection,
BlockService,
type BlockStdScope,
type Chain,
TextSelection,
type UIEventHandler,
type UIEventStateContext,
} from '@blocksuite/block-std';
import type { BaseSelection, BlockModel } from '@blocksuite/store';
import {
dedentBlocks,
dedentBlocksToRoot,
focusBlockEnd,
focusBlockStart,
indentBlocks,
selectBlock,
selectBlocksBetween,
updateBlockType,
} from './commands';
import { moveBlockConfigs } from './move-block';
import { quickActionConfig } from './quick-action';
@@ -76,13 +93,13 @@ export class NoteBlockService extends BlockService {
ctx.get('defaultState').event.preventDefault();
const [result] = this._std.command
.chain()
.updateBlockType({
.pipe(updateBlockType, {
flavour: item.flavour,
props: {
type: item.type,
},
})
.inline((ctx, next) => {
.pipe((ctx, next) => {
const newModels = ctx.updatedBlocks;
if (!newModels) {
return;
@@ -145,7 +162,7 @@ export class NoteBlockService extends BlockService {
const [result] = this._std.command
.chain()
.inline((_, next) => {
.pipe((_, next) => {
this._reset();
return next();
})
@@ -154,16 +171,16 @@ export class NoteBlockService extends BlockService {
// 1. is paragraph, list, code block - follow the default behavior
// 2. is not - select the next block (use block selection instead of text selection)
cmd
.getTextSelection()
.inline<'currentSelectionPath'>((ctx, next) => {
.pipe(getTextSelectionCommand)
.pipe<{ currentSelectionPath: string }>((ctx, next) => {
const currentTextSelection = ctx.currentTextSelection;
if (!currentTextSelection) {
return;
}
return next({ currentSelectionPath: currentTextSelection.blockId });
})
.getNextBlock()
.inline((ctx, next) => {
.pipe(getNextBlockCommand)
.pipe((ctx, next) => {
const { nextBlock } = ctx;
if (!nextBlock) {
@@ -177,13 +194,9 @@ export class NoteBlockService extends BlockService {
'affine:code',
])
) {
this._std.command
.chain()
.with({
focusBlock: nextBlock,
})
.selectBlock()
.run();
this._std.command.exec(selectBlock, {
focusBlock: nextBlock,
});
}
return next({});
@@ -193,8 +206,8 @@ export class NoteBlockService extends BlockService {
// 1. is paragraph, list, code block - focus it
// 2. is not - select it using block selection
cmd
.getBlockSelections()
.inline<'currentSelectionPath'>((ctx, next) => {
.pipe(getBlockSelectionsCommand)
.pipe<{ currentSelectionPath: string }>((ctx, next) => {
const currentBlockSelections = ctx.currentBlockSelections;
const blockSelection = currentBlockSelections?.at(-1);
if (!blockSelection) {
@@ -202,8 +215,8 @@ export class NoteBlockService extends BlockService {
}
return next({ currentSelectionPath: blockSelection.blockId });
})
.getNextBlock()
.inline<'focusBlock'>((ctx, next) => {
.pipe(getNextBlockCommand)
.pipe<{ focusBlock: BlockComponent }>((ctx, next) => {
const { nextBlock } = ctx;
if (!nextBlock) {
return;
@@ -217,18 +230,15 @@ export class NoteBlockService extends BlockService {
'affine:code',
])
) {
this._std.command
.chain()
.focusBlockStart({ focusBlock: nextBlock })
.run();
this._std.command.exec(focusBlockStart, {
focusBlock: nextBlock,
});
return next();
}
this._std.command
.chain()
.with({ focusBlock: nextBlock })
.selectBlock()
.run();
this._std.command.exec(selectBlock, {
focusBlock: nextBlock,
});
return next();
}),
])
@@ -242,7 +252,7 @@ export class NoteBlockService extends BlockService {
const [result] = this._std.command
.chain()
.inline((_, next) => {
.pipe((_, next) => {
this._reset();
return next();
})
@@ -251,16 +261,16 @@ export class NoteBlockService extends BlockService {
// 1. is paragraph, list, code block - follow the default behavior
// 2. is not - select the previous block (use block selection instead of text selection)
cmd
.getTextSelection()
.inline<'currentSelectionPath'>((ctx, next) => {
.pipe(getTextSelectionCommand)
.pipe<{ currentSelectionPath: string }>((ctx, next) => {
const currentTextSelection = ctx.currentTextSelection;
if (!currentTextSelection) {
return;
}
return next({ currentSelectionPath: currentTextSelection.blockId });
})
.getPrevBlock()
.inline((ctx, next) => {
.pipe(getPrevBlockCommand)
.pipe((ctx, next) => {
const { prevBlock } = ctx;
if (!prevBlock) {
@@ -274,23 +284,19 @@ export class NoteBlockService extends BlockService {
'affine:code',
])
) {
this._std.command
.chain()
.with({
focusBlock: prevBlock,
})
.selectBlock()
.run();
this._std.command.exec(selectBlock, {
focusBlock: prevBlock,
});
}
return next({});
return next();
}),
// block selection - select the previous block
// 1. is paragraph, list, code block - focus it
// 2. is not - select it using block selection
cmd
.getBlockSelections()
.inline<'currentSelectionPath'>((ctx, next) => {
.pipe(getBlockSelectionsCommand)
.pipe<{ currentSelectionPath: string }>((ctx, next) => {
const currentBlockSelections = ctx.currentBlockSelections;
const blockSelection = currentBlockSelections?.at(-1);
if (!blockSelection) {
@@ -298,8 +304,8 @@ export class NoteBlockService extends BlockService {
}
return next({ currentSelectionPath: blockSelection.blockId });
})
.getPrevBlock()
.inline<'focusBlock'>((ctx, next) => {
.pipe(getPrevBlockCommand)
.pipe((ctx, next) => {
const { prevBlock } = ctx;
if (!prevBlock) {
return;
@@ -313,18 +319,15 @@ export class NoteBlockService extends BlockService {
])
) {
event.preventDefault();
this._std.command
.chain()
.focusBlockEnd({ focusBlock: prevBlock })
.run();
this._std.command.exec(focusBlockEnd, {
focusBlock: prevBlock,
});
return next();
}
this._std.command
.chain()
.with({ focusBlock: prevBlock })
.selectBlock()
.run();
this._std.command.exec(selectBlock, {
focusBlock: prevBlock,
});
return next();
}),
])
@@ -333,34 +336,36 @@ export class NoteBlockService extends BlockService {
return result;
};
private readonly _onBlockShiftDown = (cmd: BlockSuite.CommandChain) => {
private readonly _onBlockShiftDown = (cmd: Chain) => {
return cmd
.getBlockSelections()
.inline<'currentSelectionPath' | 'anchorBlock'>((ctx, next) => {
const blockSelections = ctx.currentBlockSelections;
if (!blockSelections) {
return;
}
.pipe(getBlockSelectionsCommand)
.pipe<{ currentSelectionPath: string; anchorBlock: BlockComponent }>(
(ctx, next) => {
const blockSelections = ctx.currentBlockSelections;
if (!blockSelections) {
return;
}
if (!this._anchorSel) {
this._anchorSel = blockSelections.at(-1) ?? null;
}
if (!this._anchorSel) {
return;
}
if (!this._anchorSel) {
this._anchorSel = blockSelections.at(-1) ?? null;
}
if (!this._anchorSel) {
return;
}
const anchorBlock = ctx.std.view.getBlock(this._anchorSel.blockId);
if (!anchorBlock) {
return;
const anchorBlock = ctx.std.view.getBlock(this._anchorSel.blockId);
if (!anchorBlock) {
return;
}
return next({
anchorBlock,
currentSelectionPath:
this._focusBlock?.blockId ?? anchorBlock?.blockId,
});
}
return next({
anchorBlock,
currentSelectionPath:
this._focusBlock?.blockId ?? anchorBlock?.blockId,
});
})
.getNextBlock({})
.inline<'focusBlock'>((ctx, next) => {
)
.pipe(getNextBlockCommand)
.pipe<{ focusBlock: BlockComponent }>((ctx, next) => {
const nextBlock = ctx.nextBlock;
if (!nextBlock) {
return;
@@ -370,35 +375,37 @@ export class NoteBlockService extends BlockService {
focusBlock: this._focusBlock,
});
})
.selectBlocksBetween({ tail: true });
.pipe(selectBlocksBetween, { tail: true });
};
private readonly _onBlockShiftUp = (cmd: BlockSuite.CommandChain) => {
private readonly _onBlockShiftUp = (cmd: Chain) => {
return cmd
.getBlockSelections()
.inline<'currentSelectionPath' | 'anchorBlock'>((ctx, next) => {
const blockSelections = ctx.currentBlockSelections;
if (!blockSelections) {
return;
.pipe(getBlockSelectionsCommand)
.pipe<{ currentSelectionPath: string; anchorBlock: BlockComponent }>(
(ctx, next) => {
const blockSelections = ctx.currentBlockSelections;
if (!blockSelections) {
return;
}
if (!this._anchorSel) {
this._anchorSel = blockSelections.at(0) ?? null;
}
if (!this._anchorSel) {
return;
}
const anchorBlock = ctx.std.view.getBlock(this._anchorSel.blockId);
if (!anchorBlock) {
return;
}
return next({
anchorBlock,
currentSelectionPath:
this._focusBlock?.blockId ?? anchorBlock?.blockId,
});
}
if (!this._anchorSel) {
this._anchorSel = blockSelections.at(0) ?? null;
}
if (!this._anchorSel) {
return;
}
const anchorBlock = ctx.std.view.getBlock(this._anchorSel.blockId);
if (!anchorBlock) {
return;
}
return next({
anchorBlock,
currentSelectionPath:
this._focusBlock?.blockId ?? anchorBlock?.blockId,
});
})
.getPrevBlock({})
.inline((ctx, next) => {
)
.pipe(getPrevBlockCommand)
.pipe((ctx, next) => {
const prevBlock = ctx.prevBlock;
if (!prevBlock) {
return;
@@ -408,15 +415,15 @@ export class NoteBlockService extends BlockService {
focusBlock: this._focusBlock,
});
})
.selectBlocksBetween({ tail: false });
.pipe(selectBlocksBetween, { tail: false });
};
private readonly _onEnter = (ctx: UIEventStateContext) => {
const event = ctx.get('defaultState').event;
const [result] = this._std.command
.chain()
.getBlockSelections()
.inline((ctx, next) => {
.pipe(getBlockSelectionsCommand)
.pipe((ctx, next) => {
const blockSelection = ctx.currentBlockSelections?.at(-1);
if (!blockSelection) {
return;
@@ -466,8 +473,8 @@ export class NoteBlockService extends BlockService {
private readonly _onEsc = () => {
const [result] = this._std.command
.chain()
.getBlockSelections()
.inline((ctx, next) => {
.pipe(getBlockSelectionsCommand)
.pipe((ctx, next) => {
const blockSelection = ctx.currentBlockSelections?.at(-1);
if (!blockSelection) {
return;
@@ -556,7 +563,7 @@ export class NoteBlockService extends BlockService {
...this._bindQuickActionHotKey(),
...this._bindTextConversionHotKey(),
Tab: ctx => {
const { success } = this.std.command.exec('indentBlocks');
const [success] = this.std.command.exec(indentBlocks);
if (!success) return;
@@ -564,7 +571,7 @@ export class NoteBlockService extends BlockService {
return true;
},
'Shift-Tab': ctx => {
const { success } = this.std.command.exec('dedentBlocks');
const [success] = this.std.command.exec(dedentBlocks);
if (!success) return;
@@ -572,7 +579,7 @@ export class NoteBlockService extends BlockService {
return true;
},
'Mod-Backspace': ctx => {
const { success } = this.std.command.exec('dedentBlocksToRoot');
const [success] = this.std.command.exec(dedentBlocksToRoot);
if (!success) return;

View File

@@ -1,8 +1,4 @@
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';
@@ -10,13 +6,11 @@ import {
DocNoteBlockAdapterExtensions,
EdgelessNoteBlockAdapterExtensions,
} from './adapters/index.js';
import { commands } from './commands/index.js';
import { NoteBlockService } from './note-service.js';
export const NoteBlockSpec: ExtensionType[] = [
FlavourExtension('affine:note'),
NoteBlockService,
CommandExtension(commands),
BlockViewExtension('affine:note', literal`affine-note`),
DocNoteBlockAdapterExtensions,
].flat();
@@ -24,7 +18,6 @@ export const NoteBlockSpec: ExtensionType[] = [
export const EdgelessNoteBlockSpec: ExtensionType[] = [
FlavourExtension('affine:note'),
NoteBlockService,
CommandExtension(commands),
BlockViewExtension('affine:note', literal`affine-edgeless-note`),
EdgelessNoteBlockAdapterExtensions,
].flat();

View File

@@ -4,6 +4,10 @@ import {
notifyDocCreated,
promptDocTitle,
} from '@blocksuite/affine-block-embed';
import {
draftSelectedModelsCommand,
getSelectedModelsCommand,
} from '@blocksuite/affine-shared/commands';
import type { BlockStdScope } from '@blocksuite/block-std';
export interface QuickActionConfig {
@@ -18,23 +22,20 @@ export const quickActionConfig: QuickActionConfig[] = [
id: 'convert-to-linked-doc',
hotkey: `Mod-Shift-l`,
showWhen: std => {
const [_, ctx] = std.command
.chain()
.getSelectedModels({
types: ['block'],
})
.run();
const [_, ctx] = std.command.exec(getSelectedModelsCommand, {
types: ['block'],
});
const { selectedModels } = ctx;
return !!selectedModels && selectedModels.length > 0;
},
action: std => {
const [_, ctx] = std.command
.chain()
.getSelectedModels({
.pipe(getSelectedModelsCommand, {
types: ['block'],
mode: 'flat',
})
.draftSelectedModels()
.pipe(draftSelectedModelsCommand)
.run();
const { selectedModels, draftedModels } = ctx;
if (!selectedModels) return;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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';

View File

@@ -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;

View File

@@ -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;

View File

@@ -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';

View File

@@ -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;

View File

@@ -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,

View File

@@ -1,15 +1,18 @@
import { getSurfaceBlock } from '@blocksuite/affine-block-surface';
import type { SurfaceRefProps } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { BlockCommands, Command } from '@blocksuite/block-std';
import type { Command } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
export const insertSurfaceRefBlockCommand: Command<
'selectedModels',
'insertedSurfaceRefBlockId',
{
reference: string;
place: 'after' | 'before';
removeEmptyLine?: boolean;
selectedModels?: BlockModel[];
},
{
insertedSurfaceRefBlockId: string;
}
> = (ctx, next) => {
const { selectedModels, reference, place, removeEmptyLine, std } = ctx;
@@ -57,7 +60,3 @@ export const insertSurfaceRefBlockCommand: Command<
insertedSurfaceRefBlockId: result[0],
});
};
export const commands: BlockCommands = {
insertSurfaceRefBlock: insertSurfaceRefBlockCommand,
};

View File

@@ -1,4 +1,3 @@
import type { insertSurfaceRefBlockCommand } from './commands.js';
import { SurfaceRefGenericBlockPortal } from './portal/generic-block.js';
import { SurfaceRefNotePortal } from './portal/note.js';
import { SurfaceRefBlockComponent } from './surface-ref-block.js';
@@ -16,22 +15,3 @@ export function effects() {
);
customElements.define('surface-ref-note-portal', SurfaceRefNotePortal);
}
declare global {
namespace BlockSuite {
interface CommandContext {
insertedSurfaceRefBlockId?: string;
}
interface Commands {
/**
* insert a SurfaceRef block after or before the current block selection
* @param reference the reference block id. The block should be group or frame
* @param place where to insert the LaTeX block
* @param removeEmptyLine remove the current block if it is empty
* @returns the id of the inserted SurfaceRef block
*/
insertSurfaceRefBlock: typeof insertSurfaceRefBlockCommand;
}
}
}

View File

@@ -1,5 +1,4 @@
import '@blocksuite/affine-shared/commands';
export * from './commands.js';
export * from './surface-ref-block.js';
export * from './surface-ref-block-edgeless.js';
export {

View File

@@ -1,17 +1,13 @@
import {
BlockViewExtension,
CommandExtension,
FlavourExtension,
WidgetViewMapExtension,
} from '@blocksuite/block-std';
import type { ExtensionType } from '@blocksuite/store';
import { literal } from 'lit/static-html.js';
import { commands } from './commands.js';
export const PageSurfaceRefBlockSpec: ExtensionType[] = [
FlavourExtension('affine:surface-ref'),
CommandExtension(commands),
BlockViewExtension('affine:surface-ref', literal`affine-surface-ref`),
WidgetViewMapExtension('affine:surface-ref', {
surfaceToolbar: literal`affine-surface-ref-toolbar`,

View File

@@ -24,10 +24,7 @@ import { updateXYWH } from '../utils/update-xywh.js';
/**
* Automatically arrange elements according to fixed row and column rules
*/
export const autoArrangeElementsCommand: Command<never, never, {}> = (
ctx,
next
) => {
export const autoArrangeElementsCommand: Command = (ctx, next) => {
const { updateBlock } = ctx.std.store;
const gfx = ctx.std.get(GfxControllerIdentifier);
@@ -42,10 +39,7 @@ export const autoArrangeElementsCommand: Command<never, never, {}> = (
/**
* Adjust the height of the selected element to a fixed value and arrange the elements
*/
export const autoResizeElementsCommand: Command<never, never, {}> = (
ctx,
next
) => {
export const autoResizeElementsCommand: Command = (ctx, next) => {
const { updateBlock } = ctx.std.store;
const gfx = ctx.std.get(GfxControllerIdentifier);

View File

@@ -1,13 +1,5 @@
import type { BlockCommands } from '@blocksuite/block-std';
import {
export {
autoArrangeElementsCommand,
autoResizeElementsCommand,
} from './auto-align.js';
import { reassociateConnectorsCommand } from './reassociate-connectors.js';
export const commands: BlockCommands = {
reassociateConnectors: reassociateConnectorsCommand,
autoArrangeElements: autoArrangeElementsCommand,
autoResizeElements: autoResizeElementsCommand,
};
export { reassociateConnectorsCommand } from './reassociate-connectors.js';

View File

@@ -6,11 +6,10 @@ import type { Command } from '@blocksuite/block-std';
* @param oldId - the old block id
* @param newId - the new block id
*/
export const reassociateConnectorsCommand: Command<
never,
never,
{ oldId: string; newId: string }
> = (ctx, next) => {
export const reassociateConnectorsCommand: Command<{
oldId: string;
newId: string;
}> = (ctx, next) => {
const { oldId, newId } = ctx;
const service = ctx.std.getService('affine:surface');
if (!oldId || !newId || !service) {

View File

@@ -1,8 +1,3 @@
import type {
autoArrangeElementsCommand,
autoResizeElementsCommand,
} from './commands/auto-align.js';
import type { reassociateConnectorsCommand } from './commands/reassociate-connectors.js';
import { SurfaceBlockComponent } from './surface-block.js';
import { SurfaceBlockVoidComponent } from './surface-block-void.js';
import type { SurfaceBlockModel } from './surface-model.js';
@@ -21,10 +16,5 @@ declare global {
interface BlockModels {
'affine:surface': SurfaceBlockModel;
}
interface Commands {
reassociateConnectors: typeof reassociateConnectorsCommand;
autoArrangeElements: typeof autoArrangeElementsCommand;
autoResizeElements: typeof autoResizeElementsCommand;
}
}
}

View File

@@ -116,3 +116,5 @@ export const MindmapUtils = {
hideNodeConnector,
containsNode,
};
export * from './commands';

View File

@@ -1,8 +1,4 @@
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';
@@ -10,7 +6,6 @@ import {
EdgelessSurfaceBlockAdapterExtensions,
SurfaceBlockAdapterExtensions,
} from './adapters/extension';
import { commands } from './commands';
import {
EdgelessCRUDExtension,
EdgelessLegacySlotExtension,
@@ -21,7 +16,6 @@ import { MindMapView } from './view/mindmap';
const CommonSurfaceBlockSpec: ExtensionType[] = [
FlavourExtension('affine:surface'),
SurfaceBlockService,
CommandExtension(commands),
MindMapView,
EdgelessCRUDExtension,
EdgelessLegacySlotExtension,

View File

@@ -1,15 +1,15 @@
import '@blocksuite/affine-shared/commands';
import { TableModelFlavour } from '@blocksuite/affine-model';
import { generateFractionalIndexingKeyBetween } from '@blocksuite/affine-shared/utils';
import type { BlockCommands, Command } from '@blocksuite/block-std';
import { nanoid, Text } from '@blocksuite/store';
import type { Command } from '@blocksuite/block-std';
import { type BlockModel, nanoid, Text } from '@blocksuite/store';
export const insertTableBlockCommand: Command<
'selectedModels',
'insertedTableBlockId',
{
place?: 'after' | 'before';
removeEmptyLine?: boolean;
selectedModels?: BlockModel[];
},
{
insertedTableBlockId: string;
}
> = (ctx, next) => {
const { selectedModels, place, removeEmptyLine, std } = ctx;
@@ -61,7 +61,3 @@ export const insertTableBlockCommand: Command<
next({ insertedTableBlockId: blockId });
};
export const tableCommands: BlockCommands = {
insertTableBlock: insertTableBlockCommand,
};

View File

@@ -1,5 +1,4 @@
import { AddButton } from './add-button';
import type { insertTableBlockCommand } from './commands';
import { SelectionLayer } from './selection-layer';
import { TableBlockComponent } from './table-block';
import { TableCell } from './table-cell';
@@ -10,15 +9,3 @@ export function effects() {
customElements.define('affine-table-add-button', AddButton);
customElements.define('affine-table-selection-layer', SelectionLayer);
}
declare global {
namespace BlockSuite {
interface CommandContext {
insertedTableBlockId?: string;
}
interface Commands {
insertTableBlock: typeof insertTableBlockCommand;
}
}
}

View File

@@ -107,12 +107,4 @@ export class TableSelection extends BaseSelection {
}
}
declare global {
namespace BlockSuite {
interface Selection {
table: typeof TableSelection;
}
}
}
export const TableSelectionExtension = SelectionExtension(TableSelection);

View File

@@ -1,18 +1,12 @@
import { TableModelFlavour } from '@blocksuite/affine-model';
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 { TableBlockAdapterExtensions } from './adapters/extension.js';
import { tableCommands } from './commands.js';
export const TableBlockSpec: ExtensionType[] = [
FlavourExtension(TableModelFlavour),
CommandExtension(tableCommands),
BlockViewExtension(TableModelFlavour, literal`affine-table`),
TableBlockAdapterExtensions,
].flat();

View File

@@ -1,4 +1,8 @@
/// <reference types="@blocksuite/affine-shared/commands" />
import {
getBlockSelectionsCommand,
getSelectedBlocksCommand,
getTextSelectionCommand,
} from '@blocksuite/affine-shared/commands';
import type {
BlockComponent,
Command,
@@ -10,15 +14,22 @@ import { isPeekable, peek } from './peekable.js';
const getSelectedPeekableBlocks = (cmd: InitCommandCtx) => {
const [result, ctx] = cmd.std.command
.chain()
.tryAll(chain => [chain.getTextSelection(), chain.getBlockSelections()])
.getSelectedBlocks({ types: ['text', 'block'] })
.tryAll(chain => [
chain.pipe(getTextSelectionCommand),
chain.pipe(getBlockSelectionsCommand),
])
.pipe(getSelectedBlocksCommand, { types: ['text', 'block'] })
.run();
return ((result ? ctx.selectedBlocks : []) || []).filter(isPeekable);
};
export const getSelectedPeekableBlocksCommand: Command<
'selectedBlocks',
'selectedPeekableBlocks'
{
selectedBlocks: BlockComponent[];
},
{
selectedPeekableBlocks: BlockComponent[];
}
> = (ctx, next) => {
const selectedPeekableBlocks = getSelectedPeekableBlocks(ctx);
if (selectedPeekableBlocks.length > 0) {
@@ -26,10 +37,9 @@ export const getSelectedPeekableBlocksCommand: Command<
}
};
export const peekSelectedBlockCommand: Command<'selectedBlocks'> = (
ctx,
next
) => {
export const peekSelectedBlockCommand: Command<{
selectedBlocks: BlockComponent[];
}> = (ctx, next) => {
const peekableBlocks = getSelectedPeekableBlocks(ctx);
// if there are multiple blocks, peek the first one
const block = peekableBlocks.at(0);
@@ -39,17 +49,3 @@ export const peekSelectedBlockCommand: Command<'selectedBlocks'> = (
next();
}
};
declare global {
namespace BlockSuite {
interface CommandContext {
selectedPeekableBlocks?: BlockComponent[];
}
interface Commands {
peekSelectedBlock: typeof peekSelectedBlockCommand;
getSelectedPeekableBlocks: typeof getSelectedPeekableBlocksCommand;
// todo: add command for peek an inline element?
}
}
}

View File

@@ -1,21 +1,3 @@
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import type { deleteTextCommand } from './format/delete-text.js';
import type { formatBlockCommand } from './format/format-block.js';
import type { formatNativeCommand } from './format/format-native.js';
import type { formatTextCommand } from './format/format-text.js';
import type { insertInlineLatex } from './format/insert-inline-latex.js';
import type {
getTextStyle,
isTextStyleActive,
toggleBold,
toggleCode,
toggleItalic,
toggleLink,
toggleStrike,
toggleTextStyleCommand,
toggleUnderline,
} from './format/text-style.js';
import {
AffineFootnoteNode,
AffineLink,
@@ -64,25 +46,4 @@ declare global {
'latex-editor-menu': LatexEditorMenu;
'link-popup': LinkPopup;
}
namespace BlockSuite {
interface CommandContext {
textStyle?: AffineTextAttributes;
}
interface Commands {
deleteText: typeof deleteTextCommand;
formatBlock: typeof formatBlockCommand;
formatNative: typeof formatNativeCommand;
formatText: typeof formatTextCommand;
toggleBold: typeof toggleBold;
toggleItalic: typeof toggleItalic;
toggleUnderline: typeof toggleUnderline;
toggleStrike: typeof toggleStrike;
toggleCode: typeof toggleCode;
toggleLink: typeof toggleLink;
toggleTextStyle: typeof toggleTextStyleCommand;
getTextStyle: typeof getTextStyle;
isTextStyleActive: typeof isTextStyleActive;
insertInlineLatex: typeof insertInlineLatex;
}
}
}

View File

@@ -9,6 +9,15 @@ import {
StrikethroughIcon,
UnderlineIcon,
} from '../../icons/index.js';
import {
isTextStyleActive,
toggleBold,
toggleCode,
toggleItalic,
toggleLink,
toggleStrike,
toggleUnderline,
} from './text-style.js';
export interface TextFormatConfig {
id: string;
@@ -28,12 +37,12 @@ export const textFormatConfigs: TextFormatConfig[] = [
activeWhen: host => {
const [result] = host.std.command
.chain()
.isTextStyleActive({ key: 'bold' })
.pipe(isTextStyleActive, { key: 'bold' })
.run();
return result;
},
action: host => {
host.std.command.chain().toggleBold().run();
host.std.command.chain().pipe(toggleBold).run();
},
},
{
@@ -44,12 +53,12 @@ export const textFormatConfigs: TextFormatConfig[] = [
activeWhen: host => {
const [result] = host.std.command
.chain()
.isTextStyleActive({ key: 'italic' })
.pipe(isTextStyleActive, { key: 'italic' })
.run();
return result;
},
action: host => {
host.std.command.chain().toggleItalic().run();
host.std.command.chain().pipe(toggleItalic).run();
},
},
{
@@ -60,12 +69,12 @@ export const textFormatConfigs: TextFormatConfig[] = [
activeWhen: host => {
const [result] = host.std.command
.chain()
.isTextStyleActive({ key: 'underline' })
.pipe(isTextStyleActive, { key: 'underline' })
.run();
return result;
},
action: host => {
host.std.command.chain().toggleUnderline().run();
host.std.command.chain().pipe(toggleUnderline).run();
},
},
{
@@ -76,12 +85,12 @@ export const textFormatConfigs: TextFormatConfig[] = [
activeWhen: host => {
const [result] = host.std.command
.chain()
.isTextStyleActive({ key: 'strike' })
.pipe(isTextStyleActive, { key: 'strike' })
.run();
return result;
},
action: host => {
host.std.command.chain().toggleStrike().run();
host.std.command.chain().pipe(toggleStrike).run();
},
},
{
@@ -92,12 +101,12 @@ export const textFormatConfigs: TextFormatConfig[] = [
activeWhen: host => {
const [result] = host.std.command
.chain()
.isTextStyleActive({ key: 'code' })
.pipe(isTextStyleActive, { key: 'code' })
.run();
return result;
},
action: host => {
host.std.command.chain().toggleCode().run();
host.std.command.chain().pipe(toggleCode).run();
},
},
{
@@ -108,12 +117,12 @@ export const textFormatConfigs: TextFormatConfig[] = [
activeWhen: host => {
const [result] = host.std.command
.chain()
.isTextStyleActive({ key: 'link' })
.pipe(isTextStyleActive, { key: 'link' })
.run();
return result;
},
action: host => {
host.std.command.chain().toggleLink().run();
host.std.command.chain().pipe(toggleLink).run();
},
},
];

View File

@@ -2,13 +2,10 @@ import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
import type { Text } from '@blocksuite/store';
export const deleteTextCommand: Command<
'currentTextSelection',
never,
{
textSelection?: TextSelection;
}
> = (ctx, next) => {
export const deleteTextCommand: Command<{
currentTextSelection?: TextSelection;
textSelection?: TextSelection;
}> = (ctx, next) => {
const textSelection = ctx.textSelection ?? ctx.currentTextSelection;
if (!textSelection) return;

View File

@@ -1,3 +1,4 @@
import { getSelectedBlocksCommand } from '@blocksuite/affine-shared/commands';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import type { BlockSelection, Command } from '@blocksuite/block-std';
import { assertExists } from '@blocksuite/global/utils';
@@ -6,15 +7,12 @@ import { INLINE_ROOT_ATTR, type InlineRootElement } from '@blocksuite/inline';
import { FORMAT_BLOCK_SUPPORT_FLAVOURS } from './consts.js';
// for block selection
export const formatBlockCommand: Command<
'currentBlockSelections',
never,
{
blockSelections?: BlockSelection[];
styles: AffineTextAttributes;
mode?: 'replace' | 'merge';
}
> = (ctx, next) => {
export const formatBlockCommand: Command<{
currentBlockSelections?: BlockSelection[];
blockSelections?: BlockSelection[];
styles: AffineTextAttributes;
mode?: 'replace' | 'merge';
}> = (ctx, next) => {
const blockSelections = ctx.blockSelections ?? ctx.currentBlockSelections;
assertExists(
blockSelections,
@@ -28,7 +26,7 @@ export const formatBlockCommand: Command<
const success = ctx.std.command
.chain()
.getSelectedBlocks({
.pipe(getSelectedBlocksCommand, {
blockSelections,
filter: el =>
FORMAT_BLOCK_SUPPORT_FLAVOURS.includes(
@@ -36,7 +34,7 @@ export const formatBlockCommand: Command<
),
types: ['block'],
})
.inline((ctx, next) => {
.pipe((ctx, next) => {
const { selectedBlocks } = ctx;
assertExists(selectedBlocks);

View File

@@ -9,15 +9,11 @@ import { INLINE_ROOT_ATTR, type InlineRootElement } from '@blocksuite/inline';
import { FORMAT_NATIVE_SUPPORT_FLAVOURS } from './consts.js';
// for native range
export const formatNativeCommand: Command<
never,
never,
{
range?: Range;
styles: AffineTextAttributes;
mode?: 'replace' | 'merge';
}
> = (ctx, next) => {
export const formatNativeCommand: Command<{
range?: Range;
styles: AffineTextAttributes;
mode?: 'replace' | 'merge';
}> = (ctx, next) => {
const { styles, mode = 'merge' } = ctx;
let range = ctx.range;

View File

@@ -1,3 +1,4 @@
import { getSelectedBlocksCommand } from '@blocksuite/affine-shared/commands';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import type { Command, TextSelection } from '@blocksuite/block-std';
import { INLINE_ROOT_ATTR, type InlineRootElement } from '@blocksuite/inline';
@@ -6,15 +7,12 @@ import { FORMAT_TEXT_SUPPORT_FLAVOURS } from './consts.js';
import { clearMarksOnDiscontinuousInput } from './utils.js';
// for text selection
export const formatTextCommand: Command<
'currentTextSelection',
never,
{
textSelection?: TextSelection;
styles: AffineTextAttributes;
mode?: 'replace' | 'merge';
}
> = (ctx, next) => {
export const formatTextCommand: Command<{
currentTextSelection?: TextSelection;
textSelection?: TextSelection;
styles: AffineTextAttributes;
mode?: 'replace' | 'merge';
}> = (ctx, next) => {
const { styles, mode = 'merge' } = ctx;
const textSelection = ctx.textSelection ?? ctx.currentTextSelection;
@@ -22,7 +20,7 @@ export const formatTextCommand: Command<
const success = ctx.std.command
.chain()
.getSelectedBlocks({
.pipe(getSelectedBlocksCommand, {
textSelection,
filter: el =>
FORMAT_TEXT_SUPPORT_FLAVOURS.includes(
@@ -30,7 +28,7 @@ export const formatTextCommand: Command<
),
types: ['text'],
})
.inline((ctx, next) => {
.pipe((ctx, next) => {
const { selectedBlocks } = ctx;
if (!selectedBlocks) return;

View File

@@ -1,19 +1,16 @@
import { getTextSelectionCommand } from '@blocksuite/affine-shared/commands';
import type { BlockCommands } from '@blocksuite/block-std';
import { deleteTextCommand } from './delete-text.js';
export type { TextFormatConfig } from './config.js';
export { textFormatConfigs } from './config.js';
import { formatBlockCommand } from './format-block.js';
export {
FORMAT_BLOCK_SUPPORT_FLAVOURS,
FORMAT_NATIVE_SUPPORT_FLAVOURS,
FORMAT_TEXT_SUPPORT_FLAVOURS,
} from './consts.js';
import { formatNativeCommand } from './format-native.js';
import { formatTextCommand } from './format-text.js';
import { insertInlineLatex } from './insert-inline-latex.js';
import {
export { deleteTextCommand } from './delete-text.js';
export { formatBlockCommand } from './format-block.js';
export { formatNativeCommand } from './format-native.js';
export { formatTextCommand } from './format-text.js';
export { insertInlineLatex } from './insert-inline-latex.js';
export {
getTextStyle,
isTextStyleActive,
toggleBold,
@@ -29,21 +26,3 @@ export {
insertContent,
isFormatSupported,
} from './utils.js';
export const textCommands: BlockCommands = {
deleteText: deleteTextCommand,
formatBlock: formatBlockCommand,
formatNative: formatNativeCommand,
formatText: formatTextCommand,
toggleBold: toggleBold,
toggleItalic: toggleItalic,
toggleUnderline: toggleUnderline,
toggleStrike: toggleStrike,
toggleCode: toggleCode,
toggleLink: toggleLink,
toggleTextStyle: toggleTextStyleCommand,
isTextStyleActive: isTextStyleActive,
getTextStyle: getTextStyle,
getTextSelection: getTextSelectionCommand,
insertInlineLatex: insertInlineLatex,
};

View File

@@ -1,12 +1,9 @@
import type { Command, TextSelection } from '@blocksuite/block-std';
export const insertInlineLatex: Command<
'currentTextSelection',
never,
{
textSelection?: TextSelection;
}
> = (ctx, next) => {
export const insertInlineLatex: Command<{
currentTextSelection?: TextSelection;
textSelection?: TextSelection;
}> = (ctx, next) => {
const textSelection = ctx.textSelection ?? ctx.currentTextSelection;
if (!textSelection || !textSelection.isCollapsed()) return;

Some files were not shown because too many files have changed in this diff Show More