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,4 +1,7 @@
import type { BlockComponent } from '@blocksuite/block-std';
import {
type BlockComponent,
BlockSelection as StdBlockSelection,
} from '@blocksuite/block-std';
import { SignalWatcher } from '@blocksuite/global/utils';
import { css, LitElement, type PropertyValues } from 'lit';
import { property } from 'lit/decorators.js';
@@ -55,7 +58,9 @@ export class BlockSelection extends SignalWatcher(LitElement) {
protected override updated(_changedProperties: PropertyValues): void {
super.updated(_changedProperties);
if (this.block) {
this.style.display = this.block.selected?.is('block') ? 'block' : 'none';
this.style.display = this.block.selected?.is(StdBlockSelection)
? 'block'
: 'none';
}
}

View File

@@ -3,7 +3,11 @@ import {
getCurrentNativeRange,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { BlockStdScope, EditorHost } from '@blocksuite/block-std';
import {
type BlockStdScope,
type EditorHost,
TextSelection,
} from '@blocksuite/block-std';
import type { InlineEditor, InlineRange } from '@blocksuite/inline';
import { BlockModel } from '@blocksuite/store';
@@ -81,7 +85,7 @@ export function selectTextModel(
) {
const { selection } = std;
selection.setGroup('note', [
selection.create('text', {
selection.create(TextSelection, {
from: { blockId: id, index, length },
to: null,
}),

View File

@@ -1,5 +1,5 @@
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command, TextSelection } from '@blocksuite/block-std';
import { type Command, TextSelection } from '@blocksuite/block-std';
import type { Text } from '@blocksuite/store';
export const deleteTextCommand: Command<
@@ -36,7 +36,7 @@ export const deleteTextCommand: Command<
if (!to) {
fromText.delete(from.index, from.length);
ctx.std.selection.setGroup('note', [
ctx.std.selection.create('text', {
ctx.std.selection.create(TextSelection, {
from: {
blockId: from.blockId,
index: from.index,
@@ -69,7 +69,7 @@ export const deleteTextCommand: Command<
});
ctx.std.selection.setGroup('note', [
ctx.std.selection.create('text', {
ctx.std.selection.create(TextSelection, {
from: {
blockId: from.blockId,
index: from.index,

View File

@@ -2,7 +2,12 @@ import type { ReferenceInfo } from '@blocksuite/affine-model';
import { ParseDocUrlProvider } from '@blocksuite/affine-shared/services';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import type { BlockComponent } from '@blocksuite/block-std';
import { BLOCK_ID_ATTR, ShadowlessElement } from '@blocksuite/block-std';
import {
BLOCK_ID_ATTR,
BlockSelection,
ShadowlessElement,
TextSelection,
} from '@blocksuite/block-std';
import {
type DeltaInsert,
INLINE_ROOT_ATTR,
@@ -69,12 +74,12 @@ export class AffineLink extends ShadowlessElement {
}
const selection = this.std?.selection;
const textSelection = selection?.find('text');
const textSelection = selection?.find(TextSelection);
if (!!textSelection && !textSelection.isCollapsed()) {
return null;
}
const blockSelections = selection?.filter('block');
const blockSelections = selection?.filter(BlockSelection);
if (blockSelections?.length) {
return null;
}

View File

@@ -15,6 +15,7 @@ import {
BLOCK_ID_ATTR,
type BlockComponent,
type BlockStdScope,
TextSelection,
} from '@blocksuite/block-std';
import { WithDisposable } from '@blocksuite/global/utils';
import type { InlineRange } from '@blocksuite/inline/types';
@@ -438,7 +439,7 @@ export class LinkPopup extends WithDisposable(LitElement) {
reference: null,
});
this.inlineEditor.setInlineRange(this.targetInlineRange);
const textSelection = this.host?.selection.find('text');
const textSelection = this.host?.selection.find(TextSelection);
if (!textSelection) return;
this.std?.range.syncTextSelectionToRange(textSelection);
@@ -452,7 +453,7 @@ export class LinkPopup extends WithDisposable(LitElement) {
index: this.targetInlineRange.index,
length: text.length,
});
const textSelection = this.host?.selection.find('text');
const textSelection = this.host?.selection.find(TextSelection);
if (!textSelection) return;
this.std?.range.syncTextSelectionToRange(textSelection);

View File

@@ -12,7 +12,9 @@ import {
import {
BLOCK_ID_ATTR,
type BlockComponent,
BlockSelection,
ShadowlessElement,
TextSelection,
} from '@blocksuite/block-std';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { WithDisposable } from '@blocksuite/global/utils';
@@ -108,12 +110,12 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
if (!selection) {
return null;
}
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (!!textSelection && !textSelection.isCollapsed()) {
return null;
}
const blockSelections = selection.filter('block');
const blockSelections = selection.filter(BlockSelection);
if (blockSelections.length) {
return null;
}

View File

@@ -1,4 +1,9 @@
import type { BlockStdScope, UIEventHandler } from '@blocksuite/block-std';
import {
BlockSelection,
type BlockStdScope,
TextSelection,
type UIEventHandler,
} from '@blocksuite/block-std';
import {
focusTextModel,
@@ -11,21 +16,21 @@ export const textCommonKeymap = (
): Record<string, UIEventHandler> => {
return {
ArrowUp: () => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const inline = getInlineEditorByModel(std.host, text.from.blockId);
if (!inline) return;
return !inline.isFirstLine(inline.getInlineRange());
},
ArrowDown: () => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const inline = getInlineEditorByModel(std.host, text.from.blockId);
if (!inline) return;
return !inline.isLastLine(inline.getInlineRange());
},
Escape: ctx => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
selectBlock(std, text.from.blockId);
@@ -33,7 +38,7 @@ export const textCommonKeymap = (
return true;
},
'Mod-a': ctx => {
const text = std.selection.find('text');
const text = std.selection.find(TextSelection);
if (!text) return;
const model = std.doc.getBlock(text.from.blockId)?.model;
@@ -53,7 +58,7 @@ export const textCommonKeymap = (
return true;
},
Enter: ctx => {
const blocks = std.selection.filter('block');
const blocks = std.selection.filter(BlockSelection);
const blockId = blocks.at(-1)?.blockId;
if (!blockId) return;
@@ -68,5 +73,7 @@ export const textCommonKeymap = (
};
function selectBlock(std: BlockStdScope, blockId: string) {
std.selection.setGroup('note', [std.selection.create('block', { blockId })]);
std.selection.setGroup('note', [
std.selection.create(BlockSelection, { blockId }),
]);
}

View File

@@ -3,7 +3,11 @@ import {
createDefaultDoc,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { BlockStdScope, UIEventHandler } from '@blocksuite/block-std';
import {
type BlockStdScope,
TextSelection,
type UIEventHandler,
} from '@blocksuite/block-std';
import type { InlineEditor } from '@blocksuite/inline';
import { getInlineEditorByModel } from '../dom.js';
@@ -20,7 +24,7 @@ export const bracketKeymap = (
const { doc, selection } = std;
if (doc.readonly) return;
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (!textSelection) return;
const model = doc.getBlock(textSelection.from.blockId)?.model;
if (!model) return;
@@ -46,7 +50,7 @@ export const bracketKeymap = (
const { doc, selection } = std;
if (doc.readonly) return;
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (!textSelection) return;
const model = doc.getBlock(textSelection.from.blockId)?.model;
if (!model) return;
@@ -97,7 +101,7 @@ export const bracketKeymap = (
const { doc, selection } = std;
if (doc.readonly) return;
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (!textSelection || textSelection.isCollapsed()) return;
if (!textSelection.isInSameBlock()) return;
const model = doc.getBlock(textSelection.from.blockId)?.model;

View File

@@ -1,4 +1,8 @@
import type { BlockStdScope, UIEventHandler } from '@blocksuite/block-std';
import {
type BlockStdScope,
TextSelection,
type UIEventHandler,
} from '@blocksuite/block-std';
import { textFormatConfigs } from '../format/index.js';
@@ -13,7 +17,7 @@ export const textFormatKeymap = (std: BlockStdScope) =>
const { doc, selection } = std;
if (doc.readonly) return;
const textSelection = selection.find('text');
const textSelection = selection.find(TextSelection);
if (!textSelection) return;
config.action(std.host);

View File

@@ -2,7 +2,7 @@ import {
isMarkdownPrefix,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import { type BlockStdScope, TextSelection } from '@blocksuite/block-std';
import { getInlineEditorByModel } from '../dom.js';
import { toDivider } from './divider.js';
@@ -17,7 +17,7 @@ export function markdownInput(
): string | undefined {
if (!id) {
const selection = std.selection;
const text = selection.find('text');
const text = selection.find(TextSelection);
id = text?.from.blockId;
}
if (!id) return;