refactor(editor): remove selection global types (#9532)

Closes: [BS-2217](https://linear.app/affine-design/issue/BS-2217/remove-global-types-in-selection)
This commit is contained in:
Saul-Mirone
2025-01-06 03:45:10 +00:00
parent 8669936f2f
commit fc863e484c
105 changed files with 501 additions and 358 deletions

View File

@@ -114,6 +114,7 @@ export {
PlainTextAdapterFactoryExtension,
PlainTextAdapterFactoryIdentifier,
} from '@blocksuite/affine-shared/adapters';
export { HighlightSelection } from '@blocksuite/affine-shared/selection';
export * from '@blocksuite/affine-shared/services';
export { scrollbarStyle } from '@blocksuite/affine-shared/styles';
export {

View File

@@ -16,6 +16,7 @@ import {
} from '@blocksuite/affine-shared/services';
import { LassoMode } from '@blocksuite/affine-shared/types';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { SurfaceSelection, TextSelection } from '@blocksuite/block-std';
import {
GfxBlockElementModel,
type GfxToolsMap,
@@ -167,8 +168,8 @@ export class EdgelessPageKeyboardManager extends PageKeyboardManager {
const std = this.rootComponent.std;
if (
std.selection.getGroup('note').length > 0 ||
std.selection.find('text') ||
Boolean(std.selection.find('surface')?.editing)
std.selection.find(TextSelection) ||
Boolean(std.selection.find(SurfaceSelection)?.editing)
) {
return;
}

View File

@@ -22,12 +22,12 @@ import {
requestConnectedFrame,
requestThrottledConnectedFrame,
} from '@blocksuite/affine-shared/utils';
import type {
GfxBlockComponent,
import {
BlockComponent,
type GfxBlockComponent,
SurfaceSelection,
UIEventHandler,
type UIEventHandler,
} from '@blocksuite/block-std';
import { BlockComponent } from '@blocksuite/block-std';
import {
GfxControllerIdentifier,
type GfxViewportElement,
@@ -454,7 +454,7 @@ export class EdgelessRootBlockComponent extends BlockComponent<
this.handleEvent('selectionChange', () => {
const surface = this.host.selection.value.find(
(sel): sel is SurfaceSelection => sel.is('surface')
(sel): sel is SurfaceSelection => sel.is(SurfaceSelection)
);
if (!surface) return;

View File

@@ -9,11 +9,11 @@ import {
ThemeProvider,
} from '@blocksuite/affine-shared/services';
import { requestThrottledConnectedFrame } from '@blocksuite/affine-shared/utils';
import type {
GfxBlockComponent,
import {
BlockComponent,
type GfxBlockComponent,
SurfaceSelection,
} from '@blocksuite/block-std';
import { BlockComponent } from '@blocksuite/block-std';
import type { GfxViewportElement } from '@blocksuite/block-std/gfx';
import { assertExists } from '@blocksuite/global/utils';
import { css, html } from 'lit';
@@ -181,7 +181,7 @@ export class EdgelessRootPreviewBlockComponent
this.handleEvent('selectionChange', () => {
const surface = this.host.selection.value.find(
(sel): sel is SurfaceSelection => sel.is('surface')
(sel): sel is SurfaceSelection => sel.is(SurfaceSelection)
);
if (!surface) return;

View File

@@ -6,14 +6,18 @@ import {
} from '@blocksuite/affine-block-embed';
import { ParagraphBlockComponent } from '@blocksuite/affine-block-paragraph';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { BlockComponent, UIEventHandler } from '@blocksuite/block-std';
import {
type BlockComponent,
BlockSelection,
type UIEventHandler,
} from '@blocksuite/block-std';
import { IS_MAC, IS_WINDOWS } from '@blocksuite/global/env';
export class PageKeyboardManager {
private readonly _handleDelete: UIEventHandler = ctx => {
const event = ctx.get('keyboardState').raw;
const blockSelections = this._currentSelection.filter(sel =>
sel.is('block')
sel.is(BlockSelection)
);
if (blockSelections.length === 0) {
return;

View File

@@ -10,7 +10,11 @@ import {
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { PointerEventState } from '@blocksuite/block-std';
import { BlockComponent } from '@blocksuite/block-std';
import {
BlockComponent,
BlockSelection,
TextSelection,
} from '@blocksuite/block-std';
import type { BlockModel, Text } from '@blocksuite/store';
import { css, html } from 'lit';
import { query } from 'lit/decorators.js';
@@ -236,7 +240,7 @@ export class PageRootBlockComponent extends BlockComponent<
})
.flatMap(model => {
return model.children.map(child => {
return this.std.selection.create('block', {
return this.std.selection.create(BlockSelection, {
blockId: child.id,
});
});
@@ -247,7 +251,7 @@ export class PageRootBlockComponent extends BlockComponent<
ArrowUp: () => {
const selection = this.host.selection;
const sel = selection.value.find(
sel => sel.is('text') || sel.is('block')
sel => sel.is(TextSelection) || sel.is(BlockSelection)
);
if (!sel) return;
let model: BlockModel | null = null;
@@ -262,8 +266,8 @@ export class PageRootBlockComponent extends BlockComponent<
if (!model) return;
const prevNote = this.doc.getPrev(model);
if (!prevNote || prevNote.flavour !== 'affine:note') {
const isFirstText = sel.is('text') && sel.start.index === 0;
const isBlock = sel.is('block');
const isFirstText = sel.is(TextSelection) && sel.start.index === 0;
const isBlock = sel.is(BlockSelection);
if (isBlock || isFirstText) {
focusTitle(this.host);
}
@@ -361,7 +365,7 @@ export class PageRootBlockComponent extends BlockComponent<
.then(() => {
if (!newTextSelectionId) return;
this.host.selection.setGroup('note', [
this.host.selection.create('text', {
this.host.selection.create(TextSelection, {
from: {
blockId: newTextSelectionId,
index: 0,

View File

@@ -52,7 +52,12 @@ import {
ThemeProvider,
} from '@blocksuite/affine-shared/services';
import { getHostName, referenceToNode } from '@blocksuite/affine-shared/utils';
import { type BlockStdScope, WidgetComponent } from '@blocksuite/block-std';
import {
BlockSelection,
type BlockStdScope,
TextSelection,
WidgetComponent,
} from '@blocksuite/block-std';
import { type BlockModel, Text } from '@blocksuite/store';
import { autoUpdate, computePosition, flip, offset } from '@floating-ui/dom';
import { html, nothing, type TemplateResult } from 'lit';
@@ -709,13 +714,13 @@ export class EmbedCardToolbar extends WidgetComponent<
this.disposables.add(
this._selection.slots.changed.on(() => {
const hasTextSelection = this._selection.find('text');
const hasTextSelection = this._selection.find(TextSelection);
if (hasTextSelection) {
this._hide();
return;
}
const blockSelections = this._selection.filter('block');
const blockSelections = this._selection.filter(BlockSelection);
if (!blockSelections || blockSelections.length !== 1) {
this._hide();
return;

View File

@@ -8,12 +8,15 @@ import {
} from '@blocksuite/affine-components/toolbar';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type {
BaseSelection,
BlockComponent,
import {
type BaseSelection,
type BlockComponent,
BlockSelection,
CursorSelection,
TextSelection,
WidgetComponent,
} from '@blocksuite/block-std';
import { WidgetComponent } from '@blocksuite/block-std';
import { DatabaseSelection } from '@blocksuite/data-view';
import {
assertExists,
DisposableGroup,
@@ -129,11 +132,12 @@ export class AffineFormatBarWidget extends WidgetComponent {
this.disposables.add(
this._selectionManager.slots.changed.on(() => {
const update = async () => {
const textSelection = rootComponent.selection.find('text');
const blockSelections = rootComponent.selection.filter('block');
const textSelection = rootComponent.selection.find(TextSelection);
const blockSelections =
rootComponent.selection.filter(BlockSelection);
// Should not re-render format bar when only cursor selection changed in edgeless
const cursorSelection = rootComponent.selection.find('cursor');
const cursorSelection = rootComponent.selection.find(CursorSelection);
if (cursorSelection) {
if (!this._lastCursor) {
this._lastCursor = cursorSelection;
@@ -202,7 +206,7 @@ export class AffineFormatBarWidget extends WidgetComponent {
this.disposables.addFromEvent(document, 'selectionchange', () => {
if (!this.host.event.active) return;
const databaseSelection = this.host.selection.find('database');
const databaseSelection = this.host.selection.find(DatabaseSelection);
if (!databaseSelection) {
return;
}

View File

@@ -10,7 +10,11 @@ import {
} from '@blocksuite/affine-components/toolbar';
import type { ImageBlockModel } from '@blocksuite/affine-model';
import { PAGE_HEADER_HEIGHT } from '@blocksuite/affine-shared/consts';
import { WidgetComponent } from '@blocksuite/block-std';
import {
BlockSelection,
TextSelection,
WidgetComponent,
} from '@blocksuite/block-std';
import { limitShift, shift } from '@floating-ui/dom';
import { html } from 'lit';
@@ -35,7 +39,7 @@ export class AffineImageToolbarWidget extends WidgetComponent<
const imageBlock = this.block;
const selection = this.host.selection;
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (
!!textSelection &&
(!!textSelection.to || !!textSelection.from.length)
@@ -43,7 +47,7 @@ export class AffineImageToolbarWidget extends WidgetComponent<
return null;
}
const blockSelections = selection.filter('block');
const blockSelections = selection.filter(BlockSelection);
if (
blockSelections.length > 1 ||
(blockSelections.length === 1 &&

View File

@@ -3,6 +3,7 @@ import {
getBlockProps,
isInsidePageEditor,
} from '@blocksuite/affine-shared/utils';
import { BlockSelection } from '@blocksuite/block-std';
import { assertExists } from '@blocksuite/global/utils';
export function duplicate(
@@ -38,7 +39,7 @@ export function duplicate(
.then(() => {
const { selection } = editorHost;
selection.setGroup('note', [
selection.create('block', {
selection.create(BlockSelection, {
blockId: duplicateId,
}),
]);

View File

@@ -7,6 +7,7 @@ import {
import {
BLOCK_ID_ATTR,
BlockComponent,
BlockSelection,
type PointerEventState,
WidgetComponent,
} from '@blocksuite/block-std';
@@ -188,7 +189,7 @@ export class AffinePageDraggingAreaWidget extends WidgetComponent<
this._allBlocksWithRect,
userRect
).map(blockPath => {
return this.host.selection.create('block', {
return this.host.selection.create(BlockSelection, {
blockId: blockPath,
});
});

View File

@@ -7,7 +7,7 @@ import {
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { UIEventStateContext } from '@blocksuite/block-std';
import { WidgetComponent } from '@blocksuite/block-std';
import { TextSelection, WidgetComponent } from '@blocksuite/block-std';
import {
assertExists,
assertType,
@@ -122,7 +122,7 @@ export class AffineSlashMenuWidget extends WidgetComponent {
}
}
const textSelection = this.host.selection.find('text');
const textSelection = this.host.selection.find(TextSelection);
if (!textSelection) return;
const model = this.host.doc.getBlock(textSelection.blockId)?.model;
@@ -149,7 +149,7 @@ export class AffineSlashMenuWidget extends WidgetComponent {
assertType<RootBlockComponent>(rootComponent);
inlineRangeApplyCallback(() => {
const textSelection = this.host.selection.find('text');
const textSelection = this.host.selection.find(TextSelection);
if (!textSelection) return;
const model = this.host.doc.getBlock(textSelection.blockId)?.model;

View File

@@ -3,6 +3,7 @@ import type {
TextFormatConfig,
} from '@blocksuite/affine-components/rich-text';
import { isInsideBlockByFlavour } from '@blocksuite/affine-shared/utils';
import { BlockSelection } from '@blocksuite/block-std';
import { assertType } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
@@ -128,7 +129,7 @@ export function createTextFormatItem(
.chain()
.formatBlock({
blockSelections: [
std.selection.create('block', {
std.selection.create(BlockSelection, {
blockId: model.id,
}),
],

View File

@@ -19,7 +19,11 @@ import {
} from '@blocksuite/affine-components/toolbar';
import type { SurfaceRefBlockModel } from '@blocksuite/affine-model';
import { PAGE_HEADER_HEIGHT } from '@blocksuite/affine-shared/consts';
import { WidgetComponent } from '@blocksuite/block-std';
import {
BlockSelection,
TextSelection,
WidgetComponent,
} from '@blocksuite/block-std';
import { offset, shift } from '@floating-ui/dom';
import { html, nothing } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
@@ -48,7 +52,7 @@ export class AffineSurfaceRefToolbar extends WidgetComponent<
const surfaceRefBlock = this.block;
const selection = this.host.selection;
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (
!!textSelection &&
(!!textSelection.to || !!textSelection.from.length)
@@ -56,7 +60,7 @@ export class AffineSurfaceRefToolbar extends WidgetComponent<
return null;
}
const blockSelections = selection.filter('block');
const blockSelections = selection.filter(BlockSelection);
if (
blockSelections.length > 1 ||
(blockSelections.length === 1 &&