refactor(editor): align rich text util apis (#11039)

This commit is contained in:
Saul-Mirone
2025-03-20 10:40:11 +00:00
parent 77e659d0b0
commit 66ea3038af
18 changed files with 57 additions and 73 deletions

View File

@@ -4,29 +4,24 @@ import {
getCurrentNativeRange,
matchModels,
} from '@blocksuite/affine-shared/utils';
import {
type BlockStdScope,
type EditorHost,
TextSelection,
} from '@blocksuite/block-std';
import { type BlockStdScope, TextSelection } from '@blocksuite/block-std';
import type { InlineEditor, InlineRange } from '@blocksuite/block-std/inline';
import { BlockModel } from '@blocksuite/store';
import type { AffineInlineEditor } from './inline/index.js';
import type { RichText } from './rich-text.js';
/**
* In most cases, you not need RichText, you can use {@link getInlineEditorByModel} instead.
*/
export function getRichTextByModel(editorHost: EditorHost, id: string) {
const blockComponent = editorHost.view.getBlock(id);
export function getRichTextByModel(std: BlockStdScope, id: string) {
const blockComponent = std.view.getBlock(id);
const richText = blockComponent?.querySelector<RichText>('rich-text');
if (!richText) return null;
return richText;
}
export async function asyncGetRichText(editorHost: EditorHost, id: string) {
const blockComponent = await asyncGetBlockComponent(editorHost, id);
export async function asyncGetRichText(std: BlockStdScope, id: string) {
const blockComponent = await asyncGetBlockComponent(std, id);
if (!blockComponent) return null;
await blockComponent.updateComplete;
const richText = blockComponent?.querySelector<RichText>('rich-text');
@@ -35,29 +30,27 @@ export async function asyncGetRichText(editorHost: EditorHost, id: string) {
}
export function getInlineEditorByModel(
editorHost: EditorHost,
std: BlockStdScope,
model: BlockModel | string
) {
const blockModel =
typeof model === 'string'
? editorHost.std.store.getBlock(model)?.model
: model;
typeof model === 'string' ? std.store.getBlock(model)?.model : model;
if (!blockModel || matchModels(blockModel, [DatabaseBlockModel])) {
// Not support database model since it's may be have multiple inline editor instances.
// Support to enter the editing state through the Enter key in the database.
return null;
}
const richText = getRichTextByModel(editorHost, blockModel.id);
const richText = getRichTextByModel(std, blockModel.id);
if (!richText) return null;
return richText.inlineEditor;
}
export async function asyncSetInlineRange(
editorHost: EditorHost,
std: BlockStdScope,
model: BlockModel,
inlineRange: InlineRange
) {
const richText = await asyncGetRichText(editorHost, model.id);
const richText = await asyncGetRichText(std, model.id);
if (!richText) {
return;
}
@@ -94,11 +87,11 @@ export function selectTextModel(
}
export async function onModelTextUpdated(
editorHost: EditorHost,
std: BlockStdScope,
model: BlockModel,
callback?: (text: RichText) => void
) {
const richText = await asyncGetRichText(editorHost, model.id);
const richText = await asyncGetRichText(std, model.id);
if (!richText) {
console.error('RichText is not ready yet.');
return;
@@ -121,8 +114,8 @@ export async function onModelTextUpdated(
* Remove specified text from the current range.
*/
export function cleanSpecifiedTail(
editorHost: EditorHost,
inlineEditorOrModel: AffineInlineEditor | BlockModel,
std: BlockStdScope,
inlineEditorOrModel: InlineEditor | BlockModel,
str: string
) {
if (!str) {
@@ -131,7 +124,7 @@ export function cleanSpecifiedTail(
}
const inlineEditor =
inlineEditorOrModel instanceof BlockModel
? getInlineEditorByModel(editorHost, inlineEditorOrModel)
? getInlineEditorByModel(std, inlineEditorOrModel)
: inlineEditorOrModel;
if (!inlineEditor) {
return;

View File

@@ -7,8 +7,8 @@ import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import {
BLOCK_ID_ATTR,
type BlockComponent,
type BlockStdScope,
type Chain,
type EditorHost,
type InitCommandCtx,
} from '@blocksuite/block-std';
import {
@@ -233,7 +233,7 @@ export function clearMarksOnDiscontinuousInput(
}
export function insertContent(
editorHost: EditorHost,
std: BlockStdScope,
model: BlockModel,
text: string,
attributes?: AffineTextAttributes
@@ -242,7 +242,7 @@ export function insertContent(
console.error("Can't insert text! Text not found");
return;
}
const inlineEditor = getInlineEditorByModel(editorHost, model);
const inlineEditor = getInlineEditorByModel(std, model);
if (!inlineEditor) {
console.error("Can't insert text! Inline editor not found");
return;

View File

@@ -18,14 +18,14 @@ export const textCommonKeymap = (
ArrowUp: () => {
const text = std.selection.find(TextSelection);
if (!text) return;
const inline = getInlineEditorByModel(std.host, text.from.blockId);
const inline = getInlineEditorByModel(std, text.from.blockId);
if (!inline) return;
return !inline.isFirstLine(inline.getInlineRange());
},
ArrowDown: () => {
const text = std.selection.find(TextSelection);
if (!text) return;
const inline = getInlineEditorByModel(std.host, text.from.blockId);
const inline = getInlineEditorByModel(std, text.from.blockId);
if (!inline) return;
return !inline.isLastLine(inline.getInlineRange());
},

View File

@@ -28,7 +28,7 @@ export const bracketKeymap = (
if (!model) return;
if (!matchModels(model, [CodeBlockModel])) return;
const inlineEditor = getInlineEditorByModel(
std.host,
std,
textSelection.from.blockId
);
if (!inlineEditor) return;
@@ -61,7 +61,7 @@ export const bracketKeymap = (
ctx.get('keyboardState').raw.preventDefault();
const inlineEditor = getInlineEditorByModel(
std.host,
std,
textSelection.from.blockId
);
if (!inlineEditor) return;
@@ -107,7 +107,7 @@ export const bracketKeymap = (
ctx.get('keyboardState').raw.preventDefault();
const inlineEditor = getInlineEditorByModel(
std.host,
std,
textSelection.from.blockId
);
if (!inlineEditor) return;

View File

@@ -29,7 +29,7 @@ export function markdownInput(
if (!id) return;
const model = std.store.getBlock(id)?.model;
if (!model) return;
const inline = getInlineEditorByModel(std.host, model);
const inline = getInlineEditorByModel(std, model);
if (!inline) return;
const range = inline.getInlineRange();
if (!range) return;