Tiptap中BubbleMenu讲解

Tiptap 的 BubbleMenu 是一个浮动的菜单,通常用于在编辑器中选中文本时提供格式化选项。这个组件会在用户选择文本时显示,并可以定制为提供不同的编辑功能,如加粗、斜体、链接添加等。

1、安装

首先,确保你已经安装了 Tiptap。如果还没有安装,可以通过以下命令安装:

npm install @tiptap/core @tiptap/extension-bubble-menu

2、使用 BubbleMenu

下面是如何在 React 项目中使用 BubbleMenu 的示例:

1、导入所需模块:

import { useEditor, EditorContent } from '@tiptap/react';
import StarterKit from '@tiptap/starter-kit';
import { BubbleMenu } from '@tiptap/react';

2、设置编辑器: 创建一个编辑器实例,通常在一个 React 组件中完成。

const MyEditor = () => {
  const editor = useEditor({
    extensions: [
      StarterKit,
    ],
    content: '<p>Hello World!</p>',
  });

  return editor;
};

3、添加 BubbleMenu: 在编辑器组件中加入 BubbleMenu,并定义需要显示的按钮或功能。

const MyEditor = () => {
  const editor = useEditor({
    extensions: [
      StarterKit,
    ],
    content: '<p>Hello World!</p>',
  });

  if (!editor) {
    return null;
  }

  return (
    <>
      <EditorContent editor={editor} />
      <BubbleMenu editor={editor} tippyOptions={{ placement: 'top' }}>
        <button onClick={() => editor.chain().focus().toggleBold().run()} className={editor.isActive('bold') ? 'is-active' : ''}>
          Bold
        </button>
        <button onClick={() => editor.chain().focus().toggleItalic().run()} className={editor.isActive('italic') ? 'is-active' : ''}>
          Italic
        </button>
        <button onClick={() => editor.chain().focus().toggleUnderline().run()} className={editor.isActive('underline') ? 'is-active' : ''}>
          Underline
        </button>
      </BubbleMenu>
    </>
  );
};

注意事项

条件渲染:确保编辑器实例 (editor) 已经创建后再渲染 BubbleMenu。

按钮激活状态:使用 editor.isActive(‘bold’) 等方法来判断当前状态,从而改变按钮的激活状态。

tippyOptions:BubbleMenu 使用 tippy.js 为底层的浮动提示库,可以传递 tippyOptions 来自定义菜单的位置和行为。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
虽然tiptap是基于Vue.js的,但也可以在React使用。在React嵌套组件的方法与Vue.js类似,使用NodeView和MarkView来实现。 NodeView和MarkView是针对Vue.js的特定组件,但是您可以使用类似的方法将它们转换为React组件。这需要一些额外的工作,但是理论上是可行的。 以下是在React使用tiptap嵌套组件的示例: ```javascript import { NodeView } from 'tiptap' import React from 'react' import ReactDOM from 'react-dom' class CustomLink extends React.Component { render() { const { url, children } = this.props return ( <a href={url} style={{ color: 'red' }}> {children} </a> ) } } class CustomLinkNodeView extends NodeView { constructor(node, view, getPos) { super(node, view, getPos) this.dom = document.createElement('a') } update(node) { if (node.type.name !== this.node.type.name) { return false } this.dom.setAttribute('href', node.attrs.url) ReactDOM.render( <CustomLink url={node.attrs.url}> {node.textContent} </CustomLink>, this.dom ) return true } } export default CustomLinkNodeView ``` 在上面的示例,我们定义了一个名为CustomLink的React组件,并使用NodeView将其映射到一个文本节点。CustomLink组件接受一个url属性,并将节点文本作为其子元素渲染。在CustomLinkNodeView类,我们使用ReactDOM.render方法将CustomLink组件渲染到this.dom元素。最后,我们将CustomLinkNodeView导出为默认值。 请注意,在React使用tiptap嵌套组件需要进行一些额外的工作,因为tiptap是针对Vue.js的。但是,如果您熟悉React并且不想使用Vue.js,这是一种可行的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值