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,5 +1,5 @@
import { focusTextModel } from '@blocksuite/affine-components/rich-text';
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
/**
* Add a paragraph next to the current block.
@@ -17,7 +17,7 @@ export const addParagraphCommand: Command<
let blockId = ctx.blockId;
if (!blockId) {
const text = selection.find('text');
const text = selection.find(TextSelection);
blockId = text?.blockId;
}
if (!blockId) return;

View File

@@ -3,7 +3,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 canDedentParagraphCommand: Command<
never,
@@ -13,7 +13,7 @@ export const canDedentParagraphCommand: Command<
let { blockId, inlineIndex } = ctx;
const { std } = ctx;
const { selection, doc } = std;
const text = selection.find('text');
const text = selection.find(TextSelection);
if (!blockId) {
/**
@@ -97,7 +97,7 @@ export const dedentParagraphCommand: Command<'indentContext'> = (ctx, next) => {
doc.moveBlocks([model], grandParent, parent, false);
}
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (textSelection) {
host.updateComplete
.then(() => {

View File

@@ -5,7 +5,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 canIndentParagraphCommand: Command<
never,
@@ -18,7 +18,7 @@ export const canIndentParagraphCommand: Command<
const { schema } = doc;
if (!blockId) {
const text = selection.find('text');
const text = selection.find(TextSelection);
/**
* Do nothing if the selection:
* - is not a text selection
@@ -140,7 +140,7 @@ export const indentParagraphCommand: Command<'indentContext'> = (ctx, next) => {
} as Partial<ListBlockModel>);
}
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (textSelection) {
host.updateComplete
.then(() => {

View File

@@ -3,7 +3,7 @@ import {
getInlineEditorByModel,
} from '@blocksuite/affine-components/rich-text';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
export const splitParagraphCommand: Command<
never,
@@ -16,7 +16,7 @@ export const splitParagraphCommand: Command<
const { doc, host, selection } = std;
let blockId = ctx.blockId;
if (!blockId) {
const text = selection.find('text');
const text = selection.find(TextSelection);
blockId = text?.blockId;
}
if (!blockId) return;

View File

@@ -16,7 +16,7 @@ import {
getViewportElement,
} from '@blocksuite/affine-shared/utils';
import type { BlockComponent } from '@blocksuite/block-std';
import { getInlineRangeProvider } from '@blocksuite/block-std';
import { getInlineRangeProvider, TextSelection } from '@blocksuite/block-std';
import type { InlineRangeProvider } from '@blocksuite/inline';
import { effect, signal } from '@preact/signals-core';
import { html, nothing, type TemplateResult } from 'lit';
@@ -119,7 +119,7 @@ export class ParagraphBlockComponent extends CaptionedBlockComponent<
this._displayPlaceholder.value = false;
return;
}
const textSelection = this.host.selection.find('text');
const textSelection = this.host.selection.find(TextSelection);
const isCollapsed = textSelection?.isCollapsed() ?? false;
if (!this.selected || !isCollapsed) {
this._displayPlaceholder.value = false;
@@ -159,7 +159,7 @@ export class ParagraphBlockComponent extends CaptionedBlockComponent<
// reset text selection when selected block is collapsed
if (this.model.type.startsWith('h') && collapsed) {
const collapsedSiblings = this.collapsedSiblings;
const textSelection = this.host.selection.find('text');
const textSelection = this.host.selection.find(TextSelection);
if (
textSelection &&

View File

@@ -9,7 +9,7 @@ import {
calculateCollapsedSiblings,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import { KeymapExtension } from '@blocksuite/block-std';
import { KeymapExtension, TextSelection } from '@blocksuite/block-std';
import { IS_MAC } from '@blocksuite/global/env';
import { forwardDelete } from './utils/forward-delete.js';
@@ -19,7 +19,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
std => {
return {
Backspace: ctx => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const isCollapsed = text.isCollapsed();
const isStart = isCollapsed && text.from.index === 0;
@@ -52,7 +52,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
},
'Mod-Enter': ctx => {
const { doc } = std;
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const model = doc.getBlock(text.from.blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) return;
@@ -79,7 +79,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
},
Enter: ctx => {
const { doc } = std;
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const model = doc.getBlock(text.from.blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) return;

View File

@@ -3,11 +3,15 @@ import {
getNextContentBlock,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import {
BlockSelection,
type BlockStdScope,
TextSelection,
} from '@blocksuite/block-std';
export function forwardDelete(std: BlockStdScope) {
const { doc, host } = std;
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const isCollapsed = text.isCollapsed();
const model = doc.getBlock(text.from.blockId)?.model;
@@ -30,7 +34,7 @@ export function forwardDelete(std: BlockStdScope) {
if (matchFlavours(nextSibling, ignoreForwardDeleteFlavourList)) {
std.selection.setGroup('note', [
std.selection.create('block', { blockId: nextSibling.id }),
std.selection.create(BlockSelection, { blockId: nextSibling.id }),
]);
return true;
}
@@ -61,7 +65,7 @@ export function forwardDelete(std: BlockStdScope) {
if (nextBlock) {
std.selection.setGroup('note', [
std.selection.create('block', { blockId: nextBlock.id }),
std.selection.create(BlockSelection, { blockId: nextBlock.id }),
]);
}
return true;

View File

@@ -11,7 +11,7 @@ import {
getPrevContentBlock,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { EditorHost } from '@blocksuite/block-std';
import { BlockSelection, type EditorHost } from '@blocksuite/block-std';
import type { BlockModel, Text } from '@blocksuite/store';
/**
@@ -72,7 +72,7 @@ export function mergeWithPrev(editorHost: EditorHost, model: BlockModel) {
...EMBED_BLOCK_FLAVOUR_LIST,
])
) {
const selection = editorHost.selection.create('block', {
const selection = editorHost.selection.create(BlockSelection, {
blockId: prevBlock.id,
});
editorHost.selection.setGroup('note', [selection]);