55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { afterEach, expect, it } from "vitest";
|
||
import { Editor } from "@tiptap/react";
|
||
import { 正文扩展 } from "../../src/界面/编辑器/正文编辑器";
|
||
import {
|
||
转为编辑文档,
|
||
转为正文,
|
||
正文文本,
|
||
} from "../../src/界面/编辑器/文档适配";
|
||
import { 解析纯文本 } from "../../src/界面/编辑器/粘贴处理";
|
||
import type { 正文草稿 } from "../../src/功能/正文写作/数据";
|
||
|
||
const editors: Editor[] = [];
|
||
afterEach(() => editors.splice(0).forEach((e) => e.destroy()));
|
||
|
||
it("NC-editor-document-roundtrip:真实编辑器往返保留段落身份和原字符", () => {
|
||
const draft: 正文草稿 = {
|
||
format_version: 1,
|
||
paragraphs: [
|
||
{
|
||
paragraph_id: "p1",
|
||
content: [
|
||
{ type: "text", text: " “e\u0301🧑🏽” ", marks: ["strong"] },
|
||
{ type: "line_break", newline: "\r\n" },
|
||
{ type: "text", text: "第二行", marks: [] },
|
||
],
|
||
},
|
||
{ paragraph_id: "empty1", content: [] },
|
||
{ paragraph_id: "empty2", content: [] },
|
||
],
|
||
};
|
||
const e = new Editor({
|
||
extensions: 正文扩展(),
|
||
content: 转为编辑文档(draft),
|
||
});
|
||
editors.push(e);
|
||
expect(转为正文(e.getJSON())).toEqual(draft);
|
||
e.commands.setTextSelection(2);
|
||
e.commands.insertContent("甲");
|
||
expect(转为正文(e.getJSON()).paragraphs.map((p) => p.paragraph_id)).toEqual([
|
||
"p1",
|
||
"empty1",
|
||
"empty2",
|
||
]);
|
||
expect(正文文本(转为正文(e.getJSON()))).toContain("e\u0301");
|
||
});
|
||
|
||
it("NC-editor-plaintext-paste:纯文本粘贴连续空行不被默认解析器折叠", () => {
|
||
const e = new Editor({ extensions: 正文扩展() });
|
||
editors.push(e);
|
||
const text = " 首行\n\n\n末行 \r\n";
|
||
const slice = 解析纯文本(text, e.schema);
|
||
e.view.dispatch(e.state.tr.replaceSelection(slice));
|
||
expect(正文文本(转为正文(e.getJSON()))).toBe(text);
|
||
});
|