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,7 @@
import { WorkspaceImpl } from '@affine/core/modules/workspace/impls/workspace';
import type {
EditorHost,
TextRangePoint,
import {
type EditorHost,
type TextRangePoint,
TextSelection,
} from '@blocksuite/affine/block-std';
import {
@@ -68,7 +68,7 @@ function processSnapshot(
*/
function processTextInSnapshot(snapshot: SliceSnapshot, host: EditorHost) {
const { content } = snapshot;
const text = host.selection.find('text');
const text = host.selection.find(TextSelection);
if (!content.length || !text) return;
content.forEach(snapshot => processSnapshot(snapshot, text, host));

View File

@@ -1,7 +1,7 @@
import { ChatHistoryOrder } from '@affine/graphql';
import type {
import {
BlockSelection,
EditorHost,
type EditorHost,
TextSelection,
} from '@blocksuite/affine/block-std';
import type {
@@ -198,8 +198,8 @@ const REPLACE_SELECTION = {
icon: ReplaceIcon,
title: 'Replace selection',
showWhen: (host: EditorHost) => {
const textSelection = host.selection.find('text');
const blockSelections = host.selection.filter('block');
const textSelection = host.selection.find(TextSelection);
const blockSelections = host.selection.filter(BlockSelection);
if (
(!textSelection || textSelection.from.length === 0) &&
blockSelections?.length === 0

View File

@@ -1,4 +1,8 @@
import type { EditorHost } from '@blocksuite/affine/block-std';
import {
BlockSelection,
type EditorHost,
TextSelection,
} from '@blocksuite/affine/block-std';
import {
type AffineAIPanelWidgetConfig,
type AIItemGroupConfig,
@@ -92,8 +96,8 @@ export class AskAIToolbarButton extends WithDisposable(LitElement) {
this._panelRoot.style.visibility = text ? 'hidden' : 'visible';
};
const textSelection = this.host.selection.find('text');
const blockSelections = this.host.selection.filter('block');
const textSelection = this.host.selection.find(TextSelection);
const blockSelections = this.host.selection.filter(BlockSelection);
let lastBlockId: string | undefined;
if (textSelection) {
lastBlockId = textSelection.to?.blockId ?? textSelection.blockId;

View File

@@ -1,4 +1,4 @@
import type { EditorHost } from '@blocksuite/affine/block-std';
import { type EditorHost, TextSelection } from '@blocksuite/affine/block-std';
import type {
AffineAIPanelWidget,
AffineAIPanelWidgetConfig,
@@ -209,7 +209,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
export function handleInlineAskAIAction(host: EditorHost) {
const panel = getAIPanelWidget(host);
const selection = host.selection.find('text');
const selection = host.selection.find(TextSelection);
const lastBlockPath = selection
? (selection.to?.blockId ?? selection.blockId)
: null;

View File

@@ -1,4 +1,4 @@
import type { EditorHost } from '@blocksuite/affine/block-std';
import { type EditorHost, TextSelection } from '@blocksuite/affine/block-std';
import {
GfxBlockElementModel,
type GfxModel,
@@ -214,7 +214,7 @@ async function insertMarkdownAbove(host: EditorHost) {
}
function getSelection(host: EditorHost) {
const textSelection = host.selection.find('text');
const textSelection = host.selection.find(TextSelection);
const mode = textSelection ? 'flat' : 'highest';
const { selectedBlocks } = getSelections(host, mode);
if (!selectedBlocks) return;

View File

@@ -9,6 +9,8 @@ const buttonOptions: AskAIButtonOptions = {
panelWidth: 240,
};
import { BlockSelection } from '@blocksuite/affine/block-std';
import type { AskAIButtonOptions } from '../../_common/components/ask-ai-button';
import { buildAICodeItemGroups } from '../../_common/config';
@@ -22,7 +24,9 @@ export function setupCodeToolbarAIEntry(codeToolbar: AffineCodeToolbarWidget) {
action: () => {
const { selection } = host;
selection.setGroup('note', [
selection.create('block', { blockId: blockComponent.blockId }),
selection.create(BlockSelection, {
blockId: blockComponent.blockId,
}),
]);
},
render: item =>

View File

@@ -1,6 +1,9 @@
import '../../_common/components/ask-ai-button';
import type { AffineImageToolbarWidget } from '@blocksuite/affine/blocks';
import {
type AffineImageToolbarWidget,
ImageSelection,
} from '@blocksuite/affine/blocks';
import { html } from 'lit';
import type { AskAIButtonOptions } from '../../_common/components/ask-ai-button';
@@ -26,7 +29,9 @@ export function setupImageToolbarAIEntry(
action: () => {
const { selection } = host;
selection.setGroup('note', [
selection.create('image', { blockId: blockComponent.blockId }),
selection.create(ImageSelection, {
blockId: blockComponent.blockId,
}),
]);
},
render: item =>

View File

@@ -1,3 +1,4 @@
import { TextSelection } from '@blocksuite/affine/block-std';
import type { AffineAIPanelWidget } from '@blocksuite/affine/blocks';
import { handleInlineAskAIAction } from '../../actions/doc-handler';
@@ -12,7 +13,7 @@ export function setupSpaceAIEntry(panel: AffineAIPanelWidget) {
keyboardState.raw.key === ' ' &&
!keyboardState.raw.isComposing
) {
const selection = host.selection.find('text');
const selection = host.selection.find(TextSelection);
if (selection && selection.isCollapsed() && selection.from.index === 0) {
const block = host.view.getBlock(selection.blockId);
if (!block?.model?.text || block.model.text?.length > 0) return;

View File

@@ -1,7 +1,9 @@
import type {
BlockComponent,
EditorHost,
TextSelection,
import {
type BlockComponent,
BlockSelection,
type EditorHost,
SurfaceSelection,
type TextSelection,
} from '@blocksuite/affine/block-std';
import type { AffineAIPanelWidget } from '@blocksuite/affine/blocks';
import { isInsideEdgelessEditor } from '@blocksuite/affine/blocks';
@@ -32,12 +34,12 @@ const setBlockSelection = (
) => {
const selections = models
.map(model => model.id)
.map(blockId => host.selection.create('block', { blockId }));
.map(blockId => host.selection.create(BlockSelection, { blockId }));
if (isInsideEdgelessEditor(host)) {
const surfaceElementId = getNoteId(parent);
const surfaceSelection = host.selection.create(
'surface',
SurfaceSelection,
selections[0].blockId,
[surfaceElementId],
true

View File

@@ -1,4 +1,4 @@
import type { EditorHost } from '@blocksuite/affine/block-std';
import { type EditorHost, TextSelection } from '@blocksuite/affine/block-std';
import {
BlocksUtils,
type CopilotTool,
@@ -174,7 +174,7 @@ export async function selectAboveBlocks(editorHost: EditorHost, num = 10) {
const { selection } = editorHost;
selection.set([
selection.create('text', {
selection.create(TextSelection, {
from: {
blockId: startBlock.id,
index: 0,
@@ -183,7 +183,7 @@ export async function selectAboveBlocks(editorHost: EditorHost, num = 10) {
to: {
blockId: lastLeafModel.id,
index: 0,
length: selection.find('text')?.from.index ?? 0,
length: selection.find(TextSelection)?.from.index ?? 0,
},
}),
]);

View File

@@ -5,6 +5,7 @@ import { I18nService } from '@affine/core/modules/i18n';
import { UrlService } from '@affine/core/modules/url';
import { WorkspaceService } from '@affine/core/modules/workspace';
import { useI18n } from '@affine/i18n';
import { TextSelection } from '@blocksuite/affine/block-std';
import type { AffineEditorContainer } from '@blocksuite/affine/presets';
import { useService, useServiceOptional } from '@toeverything/infra';
import { useStore } from 'jotai';
@@ -29,7 +30,7 @@ import { useActiveBlocksuiteEditor } from './use-block-suite-editor';
import { useNavigateHelper } from './use-navigate-helper';
function hasLinkPopover(editor: AffineEditorContainer | null) {
const textSelection = editor?.host?.std.selection.find('text');
const textSelection = editor?.host?.std.selection.find(TextSelection);
if (editor && textSelection && textSelection.from.length > 0) {
const formatBar = editor.host?.querySelector('affine-format-bar-widget');
if (formatBar) {

View File

@@ -2,6 +2,7 @@ import type { DefaultOpenProperty } from '@affine/core/components/doc-properties
import {
type DocMode,
EdgelessRootService,
HighlightSelection,
type ReferenceParams,
} from '@blocksuite/affine/blocks';
import type {
@@ -118,7 +119,6 @@ export class Editor extends Entity {
const stablePrimaryMode = this.doc.getPrimaryMode();
// eslint-disable-next-line rxjs/finnish
const viewParams$ = view
.queryString$<ReferenceParams & { refreshKey?: string }>(
paramsParseOptions
@@ -235,7 +235,7 @@ export class Editor extends Entity {
if (mode === this.mode$.value) {
selection?.setGroup('scene', [
selection?.create('highlight', {
selection?.create(HighlightSelection, {
mode,
[key]: [id],
}),
@@ -290,7 +290,7 @@ export class Editor extends Entity {
const { id, key, mode } = anchor;
selection.setGroup('scene', [
selection.create('highlight', {
selection.create(HighlightSelection, {
mode,
[key]: [id],
}),