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

@@ -1,7 +1,8 @@
import type {
BlockStdScope,
EditorHost,
TextRangePoint,
import {
type BlockStdScope,
type EditorHost,
type TextRangePoint,
TextSelection,
} from '@blocksuite/block-std';
import type {
BlockSnapshot,
@@ -38,7 +39,7 @@ const sliceText = (slots: JobSlots, std: EditorHost['std']) => {
const snapshot = payload.snapshot;
const model = payload.model;
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (text && text.from.blockId === model.id) {
handlePoint(text.from, snapshot, model);
return;

View File

@@ -7,9 +7,10 @@ import {
import {
BLOCK_ID_ATTR,
type BlockComponent,
BlockSelection,
type EditorHost,
type TextRangePoint,
type TextSelection,
TextSelection,
} from '@blocksuite/block-std';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { assertExists } from '@blocksuite/global/utils';
@@ -25,6 +26,7 @@ import {
import * as Y from 'yjs';
import { REFERENCE_NODE } from '../../consts';
import { ImageSelection } from '../../selection';
import {
ParseDocUrlProvider,
type ParseDocUrlService,
@@ -290,19 +292,19 @@ class PasteTr {
}
if (!cursorModel.text) {
if (matchFlavours(cursorModel, ['affine:image'])) {
const selection = this.std.selection.create('image', {
const selection = this.std.selection.create(ImageSelection, {
blockId: target.blockId,
});
this.std.selection.setGroup('note', [selection]);
return;
}
const selection = this.std.selection.create('block', {
const selection = this.std.selection.create(BlockSelection, {
blockId: target.blockId,
});
this.std.selection.setGroup('note', [selection]);
return;
}
const selection = this.std.selection.create('text', {
const selection = this.std.selection.create(TextSelection, {
from: {
blockId: target.blockId,
index: cursorModel.text ? this.lastIndex : 0,
@@ -511,7 +513,7 @@ export const pasteMiddleware = (std: EditorHost['std']): JobMiddleware => {
const { snapshot } = payload;
flatNote(snapshot);
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) {
return;
}

View File

@@ -16,7 +16,7 @@ export const getSelectedBlocksCommand: Command<
blockSelections?: BlockSelection[];
imageSelections?: ImageSelection[];
filter?: (el: BlockComponent) => boolean;
types?: Extract<BlockSuite.SelectionType, 'block' | 'text' | 'image'>[];
types?: Array<'image' | 'text' | 'block'>;
roles?: RoleType[];
mode?: 'all' | 'flat' | 'highest';
}

View File

@@ -1,4 +1,4 @@
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const clearAndSelectFirstModelCommand: Command<'selectedModels'> = (
ctx,
@@ -17,7 +17,7 @@ export const clearAndSelectFirstModelCommand: Command<'selectedModels'> = (
const firstModel = models[0];
if (firstModel.text) {
firstModel.text.clear();
const selection = ctx.std.selection.create('text', {
const selection = ctx.std.selection.create(TextSelection, {
from: {
blockId: firstModel.id,
index: 0,

View File

@@ -32,7 +32,7 @@ export const getSelectedModelsCommand: Command<
never,
'selectedModels',
{
types?: Extract<BlockSuite.SelectionType, 'block' | 'text' | 'image'>[];
types?: Array<'image' | 'text' | 'block'>;
mode?: 'all' | 'flat' | 'highest';
}
> = (ctx, next) => {

View File

@@ -1,10 +1,10 @@
import type { BlockSelection, Command } from '@blocksuite/block-std';
import { BlockSelection, type Command } from '@blocksuite/block-std';
export const getBlockSelectionsCommand: Command<
never,
'currentBlockSelections'
> = (ctx, next) => {
const currentBlockSelections = ctx.std.selection.filter('block');
const currentBlockSelections = ctx.std.selection.filter(BlockSelection);
if (currentBlockSelections.length === 0) return;
next({ currentBlockSelections });

View File

@@ -1,12 +1,12 @@
import type { Command } from '@blocksuite/block-std';
import type { ImageSelection } from '../../selection/index.js';
import { ImageSelection } from '../../selection/index.js';
export const getImageSelectionsCommand: Command<
never,
'currentImageSelections'
> = (ctx, next) => {
const currentImageSelections = ctx.std.selection.filter('image');
const currentImageSelections = ctx.std.selection.filter(ImageSelection);
if (currentImageSelections.length === 0) return;
next({ currentImageSelections });

View File

@@ -1,10 +1,10 @@
import type { Command, TextSelection } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const getTextSelectionCommand: Command<never, 'currentTextSelection'> = (
ctx,
next
) => {
const currentTextSelection = ctx.std.selection.find('text');
const currentTextSelection = ctx.std.selection.find(TextSelection);
if (!currentTextSelection) return;
next({ currentTextSelection });