Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 209 additions & 0 deletions web/src/editor/code-block-toggle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import { Editor } from "@tiptap/core"
import { afterEach, beforeAll, describe, expect, it } from "vitest"
import { createExtensions } from "@/editor/extensions"
import { installTiptapJsdomShims } from "@/test/tiptap-jsdom"
import { docJson } from "@/test/tiptap-json"

/**
* 代码块合并/拆回(缺陷一):
* 跨块选区点代码块应合并为单个 codeBlock(各块纯文本 \n 连接、行内标记剥除),
* 再点一次按行拆回段落;单块选区保持原生开关行为。
*/

const editors: Editor[] = []

function cleanupEditors() {
while (editors.length) {
const ed = editors.pop()
const el = ed?.options.element
if (el instanceof Element) el.remove()
ed?.destroy()
}
}

function makeEditor(content: string): Editor {
const host = document.createElement("div")
document.body.appendChild(host)
const editor = new Editor({
element: host,
extensions: createExtensions("edit"),
content
})
editors.push(editor)
return editor
}

function codeBlockNode(editor: Editor) {
return docJson(editor).content?.find((node) => node.type === "codeBlock")
}

function paragraphTexts(editor: Editor): string[] {
return (docJson(editor).content ?? [])
.filter((node) => node.type === "paragraph" && node.content?.length)
.map((node) =>
(node.content ?? [])
.map((child) => ("text" in child ? child.text : ""))
.join("")
)
}

describe("代码块合并与拆回", () => {
beforeAll(() => {
installTiptapJsdomShims()
})

afterEach(() => {
cleanupEditors()
})

it("选中 3 个段落点代码块:恰好 1 个 codeBlock、三行文本、行内标记剥除;再点一次拆回 3 段", () => {
const editor = makeEditor(
"<p>第一行</p><p><strong>第二行</strong></p><p>第三行</p>"
)
editor.commands.selectAll()
editor.chain().focus().toggleCodeBlock().run()

const doc = docJson(editor)
const codeBlocks = (doc.content ?? []).filter(
(node) => node.type === "codeBlock"
)
expect(codeBlocks).toHaveLength(1)

const code = codeBlockNode(editor)
expect(code?.attrs).toMatchObject({ language: null, collapsed: null })
expect(code?.content).toHaveLength(1)
expect(code?.content?.[0]).toEqual({
type: "text",
text: "第一行\n第二行\n第三行"
})

// 再点一次拆回段落(忽略 TrailingNode 追加的空段落)
editor.chain().focus().toggleCodeBlock().run()
expect(codeBlockNode(editor)).toBeUndefined()
expect(paragraphTexts(editor)).toEqual(["第一行", "第二行", "第三行"])
})

it("含标题/列表项的混合选区:合并为 1 个代码块,每块一行", () => {
const editor = makeEditor(
"<h2>标题</h2><ul><li>项目一</li><li>项目二</li></ul>"
)
// 选中标题 + 整个列表(不要 selectAll:它会把 TrailingNode 追加的空段落也选进来)
editor.commands.setTextSelection({
from: 0,
to: editor.state.doc.content.size
})
editor.chain().focus().toggleCodeBlock().run()

const code = codeBlockNode(editor)
expect(code).toBeTruthy()
expect(code?.attrs).toMatchObject({ language: null, collapsed: null })
expect(code?.content?.[0]).toEqual({
type: "text",
text: "标题\n项目一\n项目二"
})
expect(
(docJson(editor).content ?? []).filter(
(node) => node.type === "codeBlock"
)
).toHaveLength(1)

editor.chain().focus().toggleCodeBlock().run()
expect(paragraphTexts(editor)).toEqual(["标题", "项目一", "项目二"])
})

it("单段落选区保持原生开关:点一次成单行代码块,再点一次回段落", () => {
const editor = makeEditor("<p>单段</p>")
editor.commands.setTextSelection({ from: 1, to: 3 })
editor.chain().focus().toggleCodeBlock().run()

const code = codeBlockNode(editor)
expect(code?.content?.[0]).toEqual({ type: "text", text: "单段" })

editor.chain().focus().toggleCodeBlock().run()
expect(paragraphTexts(editor)).toEqual(["单段"])
})

it("引用块内两段选区:合并为 1 个代码块(嵌在引用块内),每段一行", () => {
const editor = makeEditor("<blockquote><p>甲</p><p>乙</p></blockquote>")
editor.commands.setTextSelection({
from: 0,
to: editor.state.doc.content.size
})
editor.chain().focus().toggleCodeBlock().run()

const doc = docJson(editor)
const blockquote = doc.content?.find((node) => node.type === "blockquote")
const code = blockquote?.content?.find((node) => node.type === "codeBlock")
expect(code?.content?.[0]).toEqual({ type: "text", text: "甲\n乙" })
// 整篇只有这一个代码块
expect(
(doc.content ?? []).filter((node) => node.type === "codeBlock")
).toHaveLength(0)
})

it("列表两项选区:合并为 1 个代码块,每项一行", () => {
const editor = makeEditor("<ul><li>一</li><li>二</li></ul>")
editor.commands.setTextSelection({
from: 0,
to: editor.state.doc.content.size
})
editor.chain().focus().toggleCodeBlock().run()

const code = codeBlockNode(editor)
expect(code?.content?.[0]).toEqual({ type: "text", text: "一\n二" })
expect(
(docJson(editor).content ?? []).filter(
(node) => node.type === "codeBlock"
)
).toHaveLength(1)
})

it("列表部分项选区:只合并选中项,未选中项留在列表里", () => {
const editor = makeEditor("<ul><li>一</li><li>二</li><li>三</li></ul>")
// 选中 "二" 与 "三"(文本位置 8..14)
editor.commands.setTextSelection({ from: 8, to: 14 })
editor.chain().focus().toggleCodeBlock().run()

const doc = docJson(editor)
const list = doc.content?.find((node) => node.type === "bulletList")
const code = doc.content?.find((node) => node.type === "codeBlock")
expect(list?.content?.[0]?.content?.[0]?.content?.[0]).toEqual({
type: "text",
text: "一"
})
expect(code?.content?.[0]).toEqual({ type: "text", text: "二\n三" })
expect(
(doc.content ?? []).filter((node) => node.type === "codeBlock")
).toHaveLength(1)
})

it("全选标题+列表:合并文本不包含尾随空行", () => {
const editor = makeEditor(
"<h2>标题</h2><ul><li>项目一</li><li>项目二</li></ul>"
)
editor.commands.selectAll()
editor.chain().focus().toggleCodeBlock().run()

const code = codeBlockNode(editor)
expect(code?.content?.[0]).toEqual({
type: "text",
text: "标题\n项目一\n项目二"
})
})

it("全选单个标题(含文末空占位段落):恰好 1 个代码块,不生成空代码块", () => {
const editor = makeEditor("<h2>标题</h2>")
editor.commands.selectAll()
editor.chain().focus().toggleCodeBlock().run()

const doc = docJson(editor)
const codeBlocks = (doc.content ?? []).filter(
(node) => node.type === "codeBlock"
)
expect(codeBlocks).toHaveLength(1)
expect(codeBlocks[0]?.content?.[0]).toEqual({
type: "text",
text: "标题"
})
})
})
Loading