Tiptap Placeholder 占位符

Tiptap Placeholder 占位符扩展,当编辑器内容为空时可以显示你需要提示的文字,也可以在每行显示占位符文字。

Install 安装

npm install @tiptap/extension-placeholder

额外设置

在空编辑器中仅显示1行的占位符。

.ProseMirror p.is-editor-empty:first-child::before {
  color: #adb5bd;
  content: attr(data-placeholder);
  float: left;
  height: 0;
  pointer-events: none;
}

在每一行显示占位符

.ProseMirror p.is-empty::before {
  color: #adb5bd;
  content: attr(data-placeholder);
  float: left;
  height: 0;
  pointer-events: none;
}

Settings 配置

emptyEditorClass 空编辑器的样式。

Placeholder.configure({
  emptyEditorClass: 'is-editor-empty',
})

emptyNodeClass 空节点的样式。

Placeholder.configure({
  emptyNodeClass: 'my-custom-is-empty-class',
})

placeholder 占位符内容。

Placeholder.configure({
  placeholder: ' 提示你输入点什么 ',
})

根据不同的节点显示不同的占位符

Placeholder.configure({
  placeholder: ({ node }) => {
    if (node.type.name === 'heading') {
      return '请输入标题?'
    }
    return '请输入内容?'
  },
})

showOnlyWhenEditable 当编辑器可编辑时显示占位符。

Placeholder.configure({
  showOnlyWhenEditable: false,
})

showOnlyCurrent 仅在当前选定的节点中显示占位符。

Placeholder.configure({
  showOnlyCurrent: false
})

includeChildren 也显示嵌套节点的占位符。

Placeholder.configure({
  includeChildren: true
})

源代码

placeholder 源代码

在线例子

vue 在线例子

Placeholder 例子

  • Vue 例子

  • React 例子

  • React CSS

<template>
  <editor-content :editor="editor" />
</template>
<script>
import Placeholder from '@tiptap/extension-placeholder'
import StarterKit from '@tiptap/starter-kit'
import { Editor, EditorContent } from '@tiptap/vue-3'
export default {
  components: {
    EditorContent,
  },
  data() {
    return {
      editor: null,
    }
  },
  mounted() {
    this.editor = new Editor({
      extensions: [
        StarterKit,
        Placeholder.configure({
          // Use a placeholder:
          placeholder: 'Write something …',
          // Use different placeholders depending on the node type:
          // placeholder: ({ node }) => {
          //   if (node.type.name === 'heading') {
          //     return 'What’s the title?'
          //   }


          //   return 'Can you add some further context?'
          // },
        }),
      ],
    })
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>
<style>
/* Basic editor styles */
.ProseMirror {
  > * + * {
    margin-top: 0.75em;
  }
}
/* Placeholder (at the top) */
.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}
/* Placeholder (on every new line) */
/*.ProseMirror p.is-empty::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}*/
</style>
import './styles.scss'
import Placeholder from '@tiptap/extension-placeholder'
import { EditorContent, useEditor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import React from 'react'

export default () => {
  const editor = useEditor({
    extensions: [
      StarterKit,
      Placeholder.configure({
        // Use a placeholder:
        placeholder: 'Write something …',
        // Use different placeholders depending on the node type:
        // placeholder: ({ node }) => {
        //   if (node.type.name === 'heading') {
        //     return 'What’s the title?'
        //   }
        //   return 'Can you add some further context?'
        // },
      }),
    ],
  })
  return <EditorContent editor={editor} />
}
/* Basic editor styles */
.ProseMirror {
  > * + * {
    margin-top: 0.75em;
  }
}

/* Placeholder (at the top) */
.ProseMirror p.is-editor-empty:first-child::before {
  color: #adb5bd;
  content: attr(data-placeholder);
  float: left;
  height: 0;
  pointer-events: none;
}

下载教程 Demo

本教程Demo的源码点击这里下载,Tiptap Demo,下载完成后运行npm i初始化依赖包。