quasar-tiptap

repository·dev·Indexed 43 days ago

https://github.com/donotebase/quasar-tiptap

A modern WYSIWYG rich-text editor for Vue.js and the Quasar Framework, built on top of the Tiptap engine. Version 1.9.1 provides a QuasarTiptap component with customizable toolbars, bubble menus, and support for extensions including LaTeX math formulas via KaTeX, diagrams via Mermaid, and various embed services for video, maps, and design tools.

Tokens
8.3K
Snippets
30
Records
34
Agent score
56%

What's inside quasar-tiptap

  1. Install KaTeX via CDN

    dev

    To use KaTeX in a web project, include the KaTeX CSS and JavaScript files via a CDN. Note that KaTeX requires the HTML5 doctype to render properly. You can also include the auto-render extension to automatically detect and render math within text elements.

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <!-- KaTeX CSS -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">
    
        <!-- KaTeX JS -->
        <script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js" integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz" crossorigin="anonymous"></script>
    
        <!-- Auto-render extension: automatically renders math in text elements -->
        <script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
            onload="renderMathInElement(document.body);"></script>
      </head>
      <body>
        <!-- Content goes here -->
      </body>
    </html>
  2. Install quasar-tiptap and its dependencies

    dev

    Since quasar-tiptap is built on top of the Quasar Framework and Tiptap, you must install several packages.

    Required Dependencies

    Install tiptap and tiptap-extensions:

    yarn add tiptap
    yarn add tiptap-extensions

    Optional Dependencies

    If you want to enable Diagrams or LaTeX Math formulas, you must also install these:

    yarn add vue-codemirror
    yarn add mermaid
    yarn add katex

    Install the package

    yarn add quasar-tiptap
    yarn add tiptap
    yarn add tiptap-extensions
    yarn add quasar-tiptap
  3. Register quasar-tiptap as a global plugin

    dev

    You can register quasar-tiptap globally in your Vue application using QuasarTiptapPlugin. This allows you to configure global settings like language and spellcheck.

    import Vue from 'vue'
    import { QuasarTiptapPlugin, RecommendedExtensions } from 'quasar-tiptap'
    
    Vue.use(QuasarTiptapPlugin, {
      language: 'zh-hans',
      spellcheck: true
    })
  4. Configure quasar.conf.js for quasar-tiptap

    dev

    To ensure proper icon rendering and popup behavior, configure your quasar.conf.js to include the mdi-v5 icon set and the ClosePopup directive.

    extras: [
      'mdi-v5'
    ],
    framework: {
      directives: [
        'ClosePopup'
      ]
    }
  5. Install the quasar-tiptap plugin in Vue

    dev

    To use quasar-tiptap in your Vue application, register the QuasarTiptapPlugin using Vue.use(). You can pass an options object to configure global settings like spellcheck and language.

    Available options:

    • spellcheck (boolean): Enables or disables spellcheck. Defaults to true if not explicitly provided or set to null.
    • language (string): Sets the global language for the plugin via Vue.prototype.$o.lang.set().

    Upon installation, the plugin:

    1. Registers the quasar-tiptap component globally.
    2. Attaches the plugin instance to Vue.prototype.$quasarTiptapPlugin.
    import Vue from 'vue';
    import { QuasarTiptapPlugin } from 'quasar-tiptap';
    
    Vue.use(QuasarTiptapPlugin, {
      spellcheck: true,
      language: 'en'
    });
  6. Use the QuasarTiptap component

    dev

    You can use the QuasarTiptap component directly by importing it. The component emits an @update event containing functions to retrieve the editor content as JSON or HTML.

    <template>
      <div>
        <quasar-tiptap v-bind="options" @update="onUpdate" />
      </div>
    </template>
    
    <script>
    import { QuasarTiptap, RecommendedExtensions } from 'quasar-tiptap'
    import 'quasar-tiptap/lib/index.css'
    
    export default {
      data () {
        return {
          options: {
            content: 'content',
            editable: true,
            extensions: [
              ...RecommendedExtensions,
            ],
            toolbar: [
              'add-more',
              'separator',
              'bold',
              'italic',
              'underline',
            ]
          },
          json: '',
          html: ''
        }
      },
      components: {
        QuasarTiptap,
      },
      methods: {
        onUpdate ({ getJSON, getHTML }) {
          this.json = getJSON()
          this.html = getHTML()
        }
      },
      mounted () {
        // set language dynamically via the global instance
        this.$o.lang.set('en-us')
      }
    }
    </script>
  7. Generate math HTML with katex.renderToString()

    dev

    Use katex.renderToString(math, options) to generate an HTML string of the rendered math. This is particularly useful for server-side rendering (SSR) where you want to pre-render expressions in Node.js and send them as plain HTML to the client.

    Note: When using this method, ensure the KaTeX CSS and font files are included in the client-side environment to display the math correctly.

    // Generates an HTML string (e.g., '<span class="katex">...</span>')
    var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}", {
        throwOnError: false
    });
  8. QuasarTiptap component properties

    dev

    The quasar-tiptap component accepts the following properties via v-bind:

    PropertyTypeDefaultDescription
    contentObject or StringnullThe editor content
    editableBooleantrueWhen false, the editor is read-only
    extensionsArray[]A list of extensions (String, Nodes, Marks, or Plugins)
    toolbarArray[]A list of toolbar buttons (String or Vue components)
    tableToolbarArray[]A list of toolbar buttons specifically for tables
  9. Render math with katex.render()

    dev

    Use katex.render(math, element, options) to render a TeX expression directly into a specific DOM element.

    Options:

    • throwOnError: If set to false, invalid TeX inputs will be rendered as the raw TeX source code in red, with the error message provided as hover text.
    // Renders math into a specific DOM element
    katex.render("c = \\pm\\sqrt{a^2 + b^2}", element, {
        throwOnError: false
    });
  10. Map extensions to toolbar buttons

    dev

    When configuring the toolbar array in your component options, use the following mapping to enable specific buttons for your extensions:

    ExtensionToolbar ButtonRemarks
    'Bold''bold'
    'Italic''italic'
    'Strike''strike'
    'Underline''underline'
    'Code''code'
    'CodeBlock''code_blok'
    'BulletList''bullet_list'
    'OrderedList''ordered_list'
    'TodoList''todo_list'
    'HorizontalRule''horizontal'
    'Table''table'
    'Link''photo'(Note: Link uses 'photo' in mapping)
    'Image''photo'
    'OBlockquote''blockquote'
    'OHeading''heading'
    'OIndent''indent', 'outdent'
    'OAlignment''align-dropdown', 'align-group'
    'OLineHeight''line-height'
    'OForeColor''fore-color'
    'OBackColor''back-color'
    'OFontFamily''font-family'
    'OIframe''add-more'
    'ODiagram''add-more'
    'OKatexBlock''add-more'
    'OKatexInline''add-more'
    'OFormatClear''format_clear'
  11. Available Extensions in quasar-tiptap

    dev

    Extensions are categorized into Tiptap official extensions and Quasar-specific extensions. You can use RecommendedExtensions to include both sets.

    export const TipTapExtensions = [
      'Bold',
      'Italic',
      'Strike',
      'Underline',
      'Code',
      'CodeBlock',
      'CodeBlockHighlight',
      'BulletList',
      'OrderedList',
      'ListItem',
      'TodoList',
      'HorizontalRule',
      'Table',
      'Link',
      'Image',
    ]
    
    export const QuasarTipTapExtensions = [
      'OTitle',
      'ODoc',
      'OParagraph',
      'OBlockquote',
      'OTodoItem',
      'OHeading',
      'OAlignment',
      'OLineHeight',
      'OForeColor',
      'OBackColor',
      'OFontFamily',
      'OIframe',
      'ODiagram',
      'OKatexBlock',
      'OKatexInline',
      'OFormatClear',
    ]
    
    export const RecommendedExtensions = [
      ...TipTapExtensions,
      ...QuasarTipTapExtensions
    ]
  12. Create an indent command with createIndentCommand

    dev

    Use createIndentCommand(delta) to generate a ProseMirror command that adjusts the indentation level of selected nodes. The delta parameter is an integer representing the change in indentation (e.g., 1 to increase indent, -1 to decrease).

    Supported node types for indentation include:

    • paragraph
    • heading
    • blockquote

    Indentation is clamped between a minimum of 0 and a maximum of 7. List nodes are excluded from this command.

    import { createIndentCommand } from './src/utils/indent.js';
    
    // To increase indentation by 1 level
    const indentCommand = createIndentCommand(1);
    
    // Usage within a ProseMirror dispatch loop
    // dispatch(indentCommand(state, dispatch));