071faa482c
纳入 AGC Cargo workspace 的统一 rustfmt 检查与格式化入口 完成项目 TypeScript/Prettier 与 Rust 全量格式化 修复 Pingora expected executable 门禁的空白敏感误报 同步开发运维文档与 AGC skill pack 格式化忽略规则
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { act } from '@testing-library/react';
|
|
import {
|
|
$createLineBreakNode,
|
|
$createParagraphNode,
|
|
$createTextNode,
|
|
$getRoot,
|
|
getNearestEditorFromDOMNode,
|
|
} from 'lexical';
|
|
|
|
export function setPlainTextEditorValue(
|
|
contentEditable: HTMLElement,
|
|
value: string,
|
|
) {
|
|
const editor = getNearestEditorFromDOMNode(contentEditable);
|
|
if (!editor) {
|
|
throw new Error('Expected a Lexical editor root');
|
|
}
|
|
|
|
act(() => {
|
|
editor.update(
|
|
() => {
|
|
const paragraph = $createParagraphNode();
|
|
value.split('\n').forEach((line, index, lines) => {
|
|
if (line) {
|
|
paragraph.append($createTextNode(line));
|
|
}
|
|
if (index < lines.length - 1) {
|
|
paragraph.append($createLineBreakNode());
|
|
}
|
|
});
|
|
$getRoot().clear().append(paragraph);
|
|
paragraph.selectEnd();
|
|
},
|
|
{ discrete: true },
|
|
);
|
|
});
|
|
}
|
|
|
|
export function getPlainTextEditorHost(contentEditable: HTMLElement) {
|
|
const host = contentEditable.closest<HTMLElement>('.auto-grow-text-area');
|
|
if (!host) {
|
|
throw new Error('Expected an auto-grow plain-text editor host');
|
|
}
|
|
return host;
|
|
}
|