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
@@ -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 }),
]);
}
@@ -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;
@@ -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);