mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-13 08:06:24 +08:00
chore: merge blocksuite source code (#9213)
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import type { InlineEditor } from '../inline-editor.js';
|
||||
import type { InlineRange } from '../types.js';
|
||||
import type { BaseTextAttributes } from './base-attributes.js';
|
||||
|
||||
function handleInsertText<TextAttributes extends BaseTextAttributes>(
|
||||
inlineRange: InlineRange,
|
||||
data: string | null,
|
||||
editor: InlineEditor,
|
||||
attributes: TextAttributes
|
||||
) {
|
||||
if (!data) return;
|
||||
editor.insertText(inlineRange, data, attributes);
|
||||
editor.setInlineRange({
|
||||
index: inlineRange.index + data.length,
|
||||
length: 0,
|
||||
});
|
||||
}
|
||||
|
||||
function handleInsertReplacementText<TextAttributes extends BaseTextAttributes>(
|
||||
inlineRange: InlineRange,
|
||||
data: string | null,
|
||||
editor: InlineEditor,
|
||||
attributes: TextAttributes
|
||||
) {
|
||||
editor.getDeltasByInlineRange(inlineRange).forEach(deltaEntry => {
|
||||
attributes = { ...deltaEntry[0].attributes, ...attributes };
|
||||
});
|
||||
if (data) {
|
||||
editor.insertText(inlineRange, data, attributes);
|
||||
editor.setInlineRange({
|
||||
index: inlineRange.index + data.length,
|
||||
length: 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function handleInsertParagraph(inlineRange: InlineRange, editor: InlineEditor) {
|
||||
editor.insertLineBreak(inlineRange);
|
||||
editor.setInlineRange({
|
||||
index: inlineRange.index + 1,
|
||||
length: 0,
|
||||
});
|
||||
}
|
||||
|
||||
function handleDelete(inlineRange: InlineRange, editor: InlineEditor) {
|
||||
editor.deleteText(inlineRange);
|
||||
editor.setInlineRange({
|
||||
index: inlineRange.index,
|
||||
length: 0,
|
||||
});
|
||||
}
|
||||
|
||||
export function transformInput<TextAttributes extends BaseTextAttributes>(
|
||||
inputType: string,
|
||||
data: string | null,
|
||||
attributes: TextAttributes,
|
||||
inlineRange: InlineRange,
|
||||
editor: InlineEditor
|
||||
) {
|
||||
if (!editor.isValidInlineRange(inlineRange)) return;
|
||||
|
||||
if (inputType === 'insertText') {
|
||||
handleInsertText(inlineRange, data, editor, attributes);
|
||||
} else if (
|
||||
inputType === 'insertParagraph' ||
|
||||
inputType === 'insertLineBreak'
|
||||
) {
|
||||
handleInsertParagraph(inlineRange, editor);
|
||||
} else if (inputType.startsWith('delete')) {
|
||||
handleDelete(inlineRange, editor);
|
||||
} else if (inputType === 'insertReplacementText') {
|
||||
// Spell Checker
|
||||
handleInsertReplacementText(inlineRange, data, editor, attributes);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user