element-tiptap

repository·next·Indexed 23 days ago

https://github.com/leecason/element-tiptap

A modern WYSIWYG rich-text editor for Vue 3 that combines Tiptap 2 with Element Plus UI components. It features a highly extensible system using an extensions array to define editor capabilities and toolbar buttons, supporting both HTML and JSON output formats. The library includes built-in i18n support for multiple languages and provides a set of configurable props for dimensions, read-only mode, and spellcheck.

Tokens
10.1K
Snippets
30
Records
71
Agent score
80%

What's inside element-tiptap

  1. Register Element Tiptap globally in Vue 3

    next

    To use the el-tiptap component globally, install the plugin and its required styles in your main entry file.

    Note: You must also have element-plus installed as it is a peer dependency.

    import { createApp } from 'vue';
    import App from './App.vue';
    import ElementPlus from 'element-plus';
    import ElementTiptapPlugin from 'element-tiptap';
    // import element-tiptap 样式
    import 'element-tiptap/lib/style.css';
    
    // 安装 ElementUI 插件
    app.use(ElementPlus);
    // 安装 element-tiptap 插件
    app.use(ElementTiptapPlugin);
    // 现在你已经在全局注册了 `el-tiptap` 组件。
    
    app.mount('#app');
  2. Import Element Tiptap locally in Vue 3

    next

    If you prefer not to register the component globally, you can import ElementTiptap directly within your component's <script setup> block.

    <template>
      <el-tiptap ...><el-tiptap>
    </template>
    
    <script setup>
    import { ElementTiptap } from 'element-tiptap';
    </script>
  3. Install element-tiptap

    next

    You can install element-tiptap using yarn or npm.

    Global Registration

    To register the el-tiptap component globally in your Vue 3 application, install the package and use the ElementTiptapPlugin in your main entry file. You must also import the library's styles.

    Partial Import

    Alternatively, you can import the ElementTiptap component directly for local use in your components.

    yarn add element-tiptap
    # or
    npm install --save element-tiptap
  4. Register ElementTiptapPlugin globally

    next

    To use the el-tiptap component globally, register the plugin in your Vue app instance and ensure you import the required CSS.

    import { createApp } from 'vue';
    import App from './App.vue';
    import ElementPlus from 'element-plus';
    import ElementTiptapPlugin from 'element-tiptap';
    import 'element-tiptap/lib/style.css';
    
    const app = createApp(App);
    
    app.use(ElementPlus);
    app.use(ElementTiptapPlugin);
    
    app.mount('#app');
    import { createApp } from 'vue';
    import App from './App.vue';
    import ElementPlus from 'element-plus';
    import ElementTiptapPlugin from 'element-tiptap';
    // import ElementTiptap's styles
    import 'element-tiptap/lib/style.css';
    
    const app = createApp(App);
    
    // use ElementPlus's plugin
    app.use(ElementPlus);
    // use this package's plugin
    app.use(ElementTiptapPlugin);
    // Now you register `'el-tiptap'` component globally.
    
    app.mount('#app');
  5. Define command buttons in Tiptap extensions

    next

    The Menubar component relies on a specific pattern within extension options to render toolbar buttons. For an extension to appear in the Menubar, its options object must include a button property which is a function.

    This function is called by the Menubar component and receives an object containing:

    • editor: The Tiptap Editor instance.
    • t: The translation function (injected from the parent context).
    • extension: The extension instance itself.

    The button function must return either a single component specification object or an array of component specification objects. These objects are then used with v-bind to pass props and events to the rendered component.

  6. Customize bubble menu buttons via extensions

    next

    You can define custom command buttons for the MenuBubble by configuring the bubble option in your Tiptap extensions.

    An extension's bubble option should be a function that returns a component specification (or an array of specifications). This function receives an object containing the editor, translation function t, and the extension itself.

    Example of the expected return structure for the button function:

    // Inside an extension configuration
    options: {
      bubble: ({ editor, t, extension }) => ({
        component: MyCustomButtonComponent,
        componentProps: {
          // props passed to MyCustomButtonComponent
        },
        componentEvents: {
          // events for MyCustomButtonComponent
        }
      })
    }
  7. Basic Usage of el-tiptap

    next

    The el-tiptap component uses v-model:content for two-way data binding. You must provide an array of extensions to define the editor's capabilities and the toolbar buttons. Extensions are added to the menubar and bubble menu in the order they are declared.

    Some extensions can be configured using .configure(). For example, Bold.configure({ bubble: true }) enables the command button in the bubble menu.

    <template>
      <el-tiptap v-model:content="content" :extensions="extensions" />
    </template>
    
    <script setup>
    import { ref } from 'vue';
    import {
      // necessary extensions
      Doc,
      Text,
      Paragraph,
      Heading,
      Bold,
      Underline,
      Italic,
      Strike,
      BulletList,
      OrderedList,
    } from 'element-tiptap';
    
    // editor extensions
    // they will be added to menubar and bubble menu by the order you declare.
    const extensions = [
      Doc,
      Text,
      Paragraph,
      Heading.configure({ level: 5 }),
      Bold.configure({ bubble: true }), // render command-button in bubble menu.
      Underline.configure({ bubble: true, menubar: false }), // render command-button in bubble menu but not in menubar.
      Italic,
      Strike,
      BulletList,
      OrderedList,
    ];
    
    // editor's content
    const content = ref(`
      <h1>Heading</h1>
      <p>This Editor is awesome!</p>
    `);
    </script>
  8. Basic usage of Element Tiptap

    next

    To use the editor, provide a v-model:content for data binding and an extensions array to define the available tools. Extensions are added to the menubar and bubble menu in the order they are declared.

    You can use .configure() on extensions to customize their behavior, such as enabling the bubble menu (bubble: true) or disabling them in the menubar (menubar: false).

    <template>
      <el-tiptap v-model:content="content" :extensions="extensions" />
    </template>
    
    <script setup>
    import { ref } from 'vue';
    import {
      // 需要的 extensions
      Doc,
      Text,
      Paragraph,
      Heading,
      Bold,
      Underline,
      Italic,
      Strike,
      BulletList,
      OrderedList,
    } from 'element-tiptap';
    
    // 编辑器的 extensions
    // 它们将会按照你声明的顺序被添加到菜单栏和气泡菜单中
    const extensions = [
      Doc,
      Text,
      Paragraph,
      Heading.configure({ level: 5 }),
      Bold.configure({ bubble: true }), // 在气泡菜单中渲染菜单按钮
      Underline.configure({ bubble: true, menubar: false }), // 在气泡菜单而不在菜单栏中渲染菜单按钮
      Italic,
      Strike,
      BulletList,
      OrderedList,
    ];
    
    // 编辑器的内容
    const content = ref(`
      <h1>Heading</h1>
      <p>This Editor is awesome!</p>
    `);
    </script>
  9. Configure editor i18n locale

    next

    To change the editor's language, import a locale object from element-tiptap/lib/locales/ and pass it to the locale prop.

    Supported languages include: en (default), zh, pl, ru, de, ko, es, zh_tw, fr, pt_br, nl, he.

    <template>
      <el-tiptap :locale="en"></el-tiptap>
    </template>
    
    <script setup>
    import { ElementTiptap } from 'element-tiptap';
    import en from 'element-tiptap/lib/locales/en';
    </script>