Appearance
占位提示 Placeholder
占位提示扩展。fork 自 @tiptap/extension-placeholder(保持其全部行为),占位回调额外提供 parent / ancestors,且基于触发时正在扫描的文档解析,初始化与增量更新均正确。
用法
ts
import { Placeholder } from '@qingpai/core'
const editor = new Editor({
extensions: [
Placeholder.configure({
placeholder: ({ node }) => {
if (node.type.name === 'questionStem') {
return '请输入题干'
}
return ''
},
}),
],
})配置
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
emptyEditorClass | string | 'is-editor-empty' | 空编辑器根节点的 class |
emptyNodeClass | ((props: PlaceholderProps) => string) | string | 'is-empty' | 空节点的 class,可为函数 |
dataAttribute | string | 'placeholder' | 占位文案写入的 data-attribute 名(会自动转为 kebab-case) |
placeholder | ((props: PlaceholderProps) => string) | string | 'Write something …' | 占位文案,可为函数 |
showOnlyWhenEditable | boolean | true | 是否仅在编辑器可编辑时展示 |
showOnlyCurrent | boolean | true | 是否仅展示当前节点(光标所在)的占位 |
includeChildren | boolean | false | 是否同时扫描子节点 |
PlaceholderProps 相比官方扩展额外提供祖先链信息:
ts
interface PlaceholderProps {
editor: Editor
node: PMNode
pos: number
hasAnchor: boolean
parent: PMNode
/** 祖先链,外层在前:`[doc, ..., parent]` */
ancestors: PMNode[]
}与官方扩展的区别
TipTap 官方 Placeholder 的回调不提供 parent 且读取的 editor.state 已过期(扫描中的文档与编辑器状态不一致)。本扩展基于正在扫描的文档解析,两个缺陷均已修复。
Source code
packages/core/src/extensions/placeholder/