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

@@ -8,7 +8,11 @@ import {
mergeToCodeModel,
transformModel,
} from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import {
BlockSelection,
type Command,
TextSelection,
} from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
type UpdateBlockConfig = {
@@ -108,11 +112,11 @@ export const updateBlockType: Command<
onModelTextUpdated(host, model)
);
const selectionManager = host.selection;
const textSelection = selectionManager.find('text');
const textSelection = selectionManager.find(TextSelection);
if (!textSelection) {
return false;
}
const newTextSelection = selectionManager.create('text', {
const newTextSelection = selectionManager.create(TextSelection, {
from: {
blockId: firstNewModel.id,
index: textSelection.from.index,
@@ -143,13 +147,13 @@ export const updateBlockType: Command<
const selectionManager = host.selection;
const blockSelections = selectionManager.filter('block');
const blockSelections = selectionManager.filter(BlockSelection);
if (blockSelections.length === 0) {
return false;
}
requestAnimationFrame(() => {
const selections = updatedBlocks.map(model => {
return selectionManager.create('block', {
return selectionManager.create(BlockSelection, {
blockId: model.id,
});
});

View File

@@ -1,5 +1,5 @@
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const dedentBlocksToRoot: Command<
never,
@@ -13,7 +13,7 @@ export const dedentBlocksToRoot: Command<
const { std, stopCapture = true } = ctx;
const { doc } = std;
if (!blockIds || !blockIds.length) {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (text) {
// If the text selection is not at the beginning of the block, use default behavior
if (text.from.index !== 0) return;

View File

@@ -2,7 +2,7 @@ import {
calculateCollapsedSiblings,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const dedentBlocks: Command<
never,
@@ -75,7 +75,7 @@ export const dedentBlocks: Command<
std.command.exec('dedentBlock', { blockId: id, stopCapture: false });
});
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (textSelection) {
host.updateComplete
.then(() => {

View File

@@ -1,4 +1,4 @@
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const focusBlockEnd: Command<'focusBlock'> = (ctx, next) => {
const { focusBlock, std } = ctx;
@@ -7,7 +7,7 @@ export const focusBlockEnd: Command<'focusBlock'> = (ctx, next) => {
const { selection } = std;
selection.setGroup('note', [
selection.create('text', {
selection.create(TextSelection, {
from: {
blockId: focusBlock.blockId,
index: focusBlock.model.text.length,

View File

@@ -1,4 +1,4 @@
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const focusBlockStart: Command<'focusBlock'> = (ctx, next) => {
const { focusBlock, std } = ctx;
@@ -7,7 +7,7 @@ export const focusBlockStart: Command<'focusBlock'> = (ctx, next) => {
const { selection } = std;
selection.setGroup('note', [
selection.create('text', {
selection.create(TextSelection, {
from: { blockId: focusBlock.blockId, index: 0, length: 0 },
to: null,
}),

View File

@@ -3,7 +3,7 @@ import {
getNearestHeadingBefore,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const indentBlocks: Command<
never,
@@ -117,7 +117,7 @@ export const indentBlocks: Command<
}
}
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (textSelection) {
host.updateComplete
.then(() => {

View File

@@ -1,4 +1,4 @@
import type { Command } from '@blocksuite/block-std';
import { BlockSelection, type Command } from '@blocksuite/block-std';
export const selectBlock: Command<'focusBlock'> = (ctx, next) => {
const { focusBlock, std } = ctx;
@@ -9,7 +9,7 @@ export const selectBlock: Command<'focusBlock'> = (ctx, next) => {
const { selection } = std;
selection.setGroup('note', [
selection.create('block', { blockId: focusBlock.blockId }),
selection.create(BlockSelection, { blockId: focusBlock.blockId }),
]);
return next();

View File

@@ -1,4 +1,4 @@
import type { Command } from '@blocksuite/block-std';
import { BlockSelection, type Command } from '@blocksuite/block-std';
export const selectBlocksBetween: Command<
'focusBlock' | 'anchorBlock',
@@ -14,7 +14,7 @@ export const selectBlocksBetween: Command<
// In same block
if (anchorBlock.blockId === focusBlock.blockId) {
const blockId = focusBlock.blockId;
selection.setGroup('note', [selection.create('block', { blockId })]);
selection.setGroup('note', [selection.create(BlockSelection, { blockId })]);
return next();
}
@@ -23,11 +23,11 @@ export const selectBlocksBetween: Command<
if (selections.every(sel => sel.blockId !== focusBlock.blockId)) {
if (tail) {
selections.push(
selection.create('block', { blockId: focusBlock.blockId })
selection.create(BlockSelection, { blockId: focusBlock.blockId })
);
} else {
selections.unshift(
selection.create('block', { blockId: focusBlock.blockId })
selection.create(BlockSelection, { blockId: focusBlock.blockId })
);
}
}