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
11 changes: 10 additions & 1 deletion web/src/editor/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,16 @@ export function createExtensions(mode: EditorMode, placeholder?: string) {
? [
Placeholder.configure({
placeholder: placeholder ?? "",
showOnlyWhenEditable: true
showOnlyWhenEditable: true,
/*
* 空代码块不需要文档级占位提示:块头(语言名/语言选择器)已表明块类型。
* 实测 Placeholder 的节点装饰把 is-empty / data-placeholder 挂在 React 渲染器
* 的外层 wrapper(div.react-renderer.node-codeBlock)上,而不是 CodeBlockView
* 自己的 .code-block(NodeViewWrapper);浮动伪元素会压住块头行。
* 这里按节点类型排除 codeBlock(返回空类名 = 不挂 is-empty),普通空段落保持零变化。
*/
emptyNodeClass: ({ node }) =>
node.type.name === "codeBlock" ? "" : "is-empty"
})
]
: [])
Expand Down
65 changes: 65 additions & 0 deletions web/src/editor/placeholder.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { render, waitFor } from "@testing-library/react"
import type { Editor } from "@tiptap/core"
import { EditorContent, useEditor } from "@tiptap/react"
import { useEffect, useRef } from "react"
import { beforeAll, describe, expect, it } from "vitest"
import { createExtensions } from "@/editor/extensions"
import { installTiptapJsdomShims } from "@/test/tiptap-jsdom"

/**
* 空节点占位装饰(Placeholder 扩展)的落点回归:
* 空段落照常挂 is-empty + data-placeholder;空代码块是 NodeView 块,
* 占位装饰落在 React 渲染器外层 wrapper 会压住块头行,必须排除。
*/

function Harness({
content,
onEditor
}: {
content: object
onEditor?: (editor: Editor) => void
}) {
const editor = useEditor({
extensions: createExtensions("edit", "开始输入,或直接粘贴内容…"),
content
})
const ref = useRef(onEditor)
ref.current = onEditor
useEffect(() => {
if (editor) ref.current?.(editor)
}, [editor])
return <EditorContent editor={editor} />
}

describe("空节点占位装饰", () => {
beforeAll(() => {
installTiptapJsdomShims()
})

it("空段落照常挂占位提示", async () => {
const { container } = render(
<Harness content={{ type: "doc", content: [{ type: "paragraph" }] }} />
)
await waitFor(() => {
const empty = container.querySelector(".is-empty")
expect(empty).toHaveAttribute(
"data-placeholder",
"开始输入,或直接粘贴内容…"
)
})
})

it("空代码块不挂 is-empty(占位伪元素不落到块头行上)", async () => {
const { container } = render(
<Harness content={{ type: "doc", content: [{ type: "codeBlock" }] }} />
)
await waitFor(() => {
// React 渲染器外层 wrapper(占位装饰的真实落点)
const wrapper = container.querySelector(".node-codeBlock")
expect(wrapper).toBeTruthy()
expect(wrapper).not.toHaveClass("is-empty")
// 块头行(语言名/语言选择器)仍在
expect(container.querySelector("[data-code-header]")).toBeTruthy()
})
})
})
9 changes: 0 additions & 9 deletions web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -1734,15 +1734,6 @@ html[data-theme="sketch"].dark {
pointer-events: none;
}

/*
* 代码块是 NodeView 块:Placeholder 的节点装饰把 is-empty 挂在外层 .code-block wrapper 上,
* 上面的浮动伪元素会落在块最上方、压住块头行(语言名/语言选择器)。块头本身已表明块类型,
* 空代码块不需要文档级占位提示,这里直接抑制该伪元素(普通空段落保持零变化)。
*/
.wiki-content .code-block.is-empty::before {
display: none;
}

.wiki-content .ProseMirror-selectednode {
outline: 2px solid var(--ring);
outline-offset: 2px;
Expand Down