mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-30 08:39:53 +08:00
24448659a4
Fix #12284 Close [BS-3517](https://linear.app/affine-design/issue/BS-3517/微软新注音输入法无法使用markdown语法) This PR refactor the markdown transform during inputting, including: - Transfrom markdown syntax input in `inlineEditor.slots.inputting`, where we can detect the space character inputed by IME like Microsoft Bopomofo, but `keydown` event can't. - Remove `markdown-input.ts` which was used in `KeymapExtension` of paragraph, and refactor with `InlineMarkdownExtension` - Adjust existing `InlineMarkdownExtension` since the space is included in text. - Add two `InlineMarkdownExtension` for paragraph and list to impl Heading1-6, number, bullet, to-do list conversion. Other changes: - Improve type hint for parameter of `store.addBlock` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Added markdown shortcuts for creating code blocks and dividers in the rich text editor. - Introduced enhanced paragraph markdown support for headings and blockquotes with inline markdown patterns. - Integrated new list markdown extension supporting numbered, bulleted, and todo lists with checked states. - **Improvements** - Updated markdown formatting patterns to require trailing spaces for links, LaTeX, and inline styles, improving detection accuracy. - Markdown transformations now respond to input events instead of keydown for smoother editing experience. - Added focus management after markdown transformations to maintain seamless editing flow. - **Bug Fixes** - Removed unnecessary prevention of default behavior on space and shift-space key presses in list and paragraph editors. - **Refactor** - Enhanced event handling and typing for editor input events, improving reliability and maintainability. - Refined internal prefix text extraction logic for markdown processing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
|
|
import type { BlockStdScope } from '@blocksuite/std';
|
|
import type { InlineEditor } from '@blocksuite/std/inline';
|
|
import type { BlockModel } from '@blocksuite/store';
|
|
import { effect } from '@preact/signals-core';
|
|
|
|
import { getInlineEditorByModel } from './dom';
|
|
|
|
export function insertContent(
|
|
std: BlockStdScope,
|
|
model: BlockModel,
|
|
text: string,
|
|
attributes?: AffineTextAttributes
|
|
) {
|
|
if (!model.text) {
|
|
console.error("Can't insert text! Text not found");
|
|
return;
|
|
}
|
|
const inlineEditor = getInlineEditorByModel(std, model);
|
|
if (!inlineEditor) {
|
|
console.error("Can't insert text! Inline editor not found");
|
|
return;
|
|
}
|
|
const inlineRange = inlineEditor.getInlineRange();
|
|
const index = inlineRange ? inlineRange.index : model.text.length;
|
|
model.text.insert(text, index, attributes as Record<string, unknown>);
|
|
// Update the caret to the end of the inserted text
|
|
inlineEditor.setInlineRange({
|
|
index: index + text.length,
|
|
length: 0,
|
|
});
|
|
}
|
|
|
|
// When the user selects a range, check if it matches the previous selection.
|
|
// If it does, apply the marks from the previous selection.
|
|
// If it does not, remove the marks from the previous selection.
|
|
export function clearMarksOnDiscontinuousInput(
|
|
inlineEditor: InlineEditor
|
|
): void {
|
|
let inlineRange = inlineEditor.getInlineRange();
|
|
const dispose = effect(() => {
|
|
const r = inlineEditor.inlineRange$.value;
|
|
if (
|
|
inlineRange &&
|
|
r &&
|
|
(inlineRange.index === r.index || inlineRange.index === r.index + 1)
|
|
) {
|
|
inlineRange = r;
|
|
} else {
|
|
inlineEditor.resetMarks();
|
|
dispose();
|
|
}
|
|
});
|
|
}
|
|
|
|
export function getPrefixText(inlineEditor: InlineEditor) {
|
|
const inlineRange = inlineEditor.getInlineRange();
|
|
if (!inlineRange || inlineRange.length > 0) return '';
|
|
|
|
const nearestLineBreakIndex = inlineEditor.yTextString
|
|
.slice(0, inlineRange.index)
|
|
.lastIndexOf('\n');
|
|
const prefixText = inlineEditor.yTextString.slice(
|
|
nearestLineBreakIndex + 1,
|
|
inlineRange.index
|
|
);
|
|
return prefixText;
|
|
}
|