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,6 +1,6 @@
import type { IndentContext } from '@blocksuite/affine-shared/types';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
import { correctNumberedListsOrderToPrev } from './utils.js';
@@ -13,7 +13,7 @@ export const canDedentListCommand: Command<
const { std } = ctx;
const { selection, doc } = std;
if (!blockId) {
const text = selection.find('text');
const text = selection.find(TextSelection);
/**
* Do nothing if the selection:
* - is not a text selection
@@ -148,7 +148,7 @@ export const dedentListCommand: Command<'indentContext'> = (ctx, next) => {
doc.moveBlocks([model], grandParent, parent, false);
correctNumberedListsOrderToPrev(doc, model);
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (textSelection) {
host.updateComplete
.then(() => {

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';
import { correctNumberedListsOrderToPrev } from './utils.js';
@@ -16,7 +16,7 @@ export const canIndentListCommand: Command<
const { std } = ctx;
const { selection, doc } = std;
if (!blockId) {
const text = selection.find('text');
const text = selection.find(TextSelection);
/**
* Do nothing if the selection:
* - is not a text selection
@@ -133,7 +133,7 @@ export const indentListCommand: Command<'indentContext', never> = (
});
}
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (textSelection) {
host.updateComplete
.then(() => {

View File

@@ -15,7 +15,11 @@ import {
import { DocModeProvider } from '@blocksuite/affine-shared/services';
import { getViewportElement } from '@blocksuite/affine-shared/utils';
import type { BaseSelection, BlockComponent } from '@blocksuite/block-std';
import { getInlineRangeProvider } from '@blocksuite/block-std';
import {
BlockSelection,
getInlineRangeProvider,
TextSelection,
} from '@blocksuite/block-std';
import type { InlineRangeProvider } from '@blocksuite/inline';
import { effect } from '@preact/signals-core';
import { html, nothing, type TemplateResult } from 'lit';
@@ -95,8 +99,10 @@ export class ListBlockComponent extends CaptionedBlockComponent<ListBlockModel>
const selection = this.host.selection;
selection.update(selList => {
return selList
.filter<BaseSelection>(sel => !sel.is('text') && !sel.is('block'))
.concat(selection.create('block', { blockId: this.blockId }));
.filter<BaseSelection>(
sel => !sel.is(TextSelection) && !sel.is(BlockSelection)
)
.concat(selection.create(BlockSelection, { blockId: this.blockId }));
});
}

View File

@@ -3,7 +3,7 @@ import {
textKeymap,
} from '@blocksuite/affine-components/rich-text';
import { ListBlockSchema } from '@blocksuite/affine-model';
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';
@@ -12,7 +12,7 @@ export const ListKeymapExtension = KeymapExtension(
std => {
return {
Enter: ctx => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return false;
ctx.get('keyboardState').raw.preventDefault();
@@ -23,7 +23,7 @@ export const ListKeymapExtension = KeymapExtension(
return true;
},
'Mod-Enter': ctx => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return false;
ctx.get('keyboardState').raw.preventDefault();
@@ -40,7 +40,7 @@ export const ListKeymapExtension = KeymapExtension(
if (selectedModels?.length !== 1) {
return false;
}
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return false;
ctx.get('keyboardState').raw.preventDefault();
@@ -61,7 +61,7 @@ export const ListKeymapExtension = KeymapExtension(
if (selectedModels?.length !== 1) {
return;
}
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return false;
ctx.get('keyboardState').raw.preventDefault();
@@ -76,7 +76,7 @@ export const ListKeymapExtension = KeymapExtension(
return true;
},
Backspace: ctx => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return false;
const isCollapsed = text.isCollapsed();
const isStart = isCollapsed && text.from.index === 0;

View File

@@ -2,7 +2,7 @@ import {
getNextContentBlock,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import { type BlockStdScope, TextSelection } from '@blocksuite/block-std';
import type { Text } from '@blocksuite/store';
// When deleting at line end of a list block,
@@ -20,7 +20,7 @@ import type { Text } from '@blocksuite/store';
- Line9
*/
export function forwardDelete(std: BlockStdScope): true | undefined {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const isCollapsed = text.isCollapsed();
const doc = std.doc;