tiptap/docs/installation/alpine.md
Justis R 1331dd8890
Update alpine.md with better info about proxy limitations (#4678)
The issue described in #1515 is now mentioned in the Alpine documentation so that readers understand why the example does not store the editor as component data and why they should not do so either.
2023-12-14 15:29:10 +01:00

4.8 KiB
Raw Blame History

title tableOfContents
Alpine WYSIWYG true

Alpine.js

Introduction

The following guide describes how to integrate Tiptap with version 3 of Alpine.js.

For the sake of this guide well use Vite to quickly set up a project, but you can use whatever youre used to. Vite is just really fast and we love it.

Requirements

1. Create a project (optional)

If you already have an existing Alpine.js project, thats fine too. Just skip this step and proceed with the next step.

For the sake of this guide, lets start with a fresh Vite project called my-tiptap-project. Vite sets up everything we need, just select the Vanilla JavaScript template.

npm init vite@latest my-tiptap-project -- --template vanilla
cd my-tiptap-project
npm install
npm run dev

2. Install the dependencies

Okay, enough of the boring boilerplate work. Lets finally install Tiptap! For the following example youll need alpinejs, the @tiptap/core package, the @tiptap/pm package and the @tiptap/starter-kit, which includes the most common extensions to get started quickly.

npm install alpinejs @tiptap/core @tiptap/pm @tiptap/starter-kit

If you followed step 1, you can now start your project with npm run dev, and open http://localhost:5173 in your favorite browser. This might be different, if youre working with an existing project.

3. Initialize the editor

To actually start using Tiptap, youll need to write a little bit of JavaScript. Lets put the following example code in a file called main.js.

This is the fastest way to get Tiptap up and running with Alpine.js. It will give you a very basic version of Tiptap. No worries, you will be able to add more functionality soon.

import Alpine from 'alpinejs'
import { Editor } from '@tiptap/core'
import StarterKit from '@tiptap/starter-kit'

document.addEventListener('alpine:init', () => {
  Alpine.data('editor', (content) => {
    let editor // Alpine's reactive engine automatically wraps component properties in proxy objects. Attempting to use a proxied editor instance to apply a transaction will cause a "Range Error: Applying a mismatched transaction", so be sure to unwrap it using Alpine.raw(), or simply avoid storing your editor as a component property, as shown in this example.

    return {
      updatedAt: Date.now(), // force Alpine to rerender on selection change
      init() {
        const _this = this

        editor = new Editor({
          element: this.$refs.element,
          extensions: [
            StarterKit
          ],
          content: content,
          onCreate({ editor }) {
            _this.updatedAt = Date.now()
          },
          onUpdate({ editor }) {
            _this.updatedAt = Date.now()
          },
          onSelectionUpdate({ editor }) {
            _this.updatedAt = Date.now()
          }
        })
      },
      isLoaded() {
        return editor
      },
      isActive(type, opts = {}) {
        return editor.isActive(type, opts)
      },
      toggleHeading(opts) {
        editor.chain().toggleHeading(opts).focus().run()
      },
      toggleBold() {
        editor.chain().toggleBold().focus().run()
      },
      toggleItalic() {
        editor.chain().toggleItalic().focus().run()
      },
    }
  })
})

window.Alpine = Alpine
Alpine.start()

4. Add it to your app

Now, lets replace the content of the index.html with the following example code to use the editor in our app.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
</head>
<body>
  <div x-data="editor('<p>Hello world! :-)</p>')">

    <template x-if="isLoaded()">
      <div class="menu">
        <button
          @click="toggleHeading({ level: 1 })"
          :class="{ 'is-active': isActive('heading', { level: 1 }, updatedAt) }"
        >
          H1
        </button>
        <button
          @click="toggleBold()"
          :class="{ 'is-active' : isActive('bold', updatedAt) }"
        >
          Bold
        </button>
        <button
          @click="toggleItalic()"
          :class="{ 'is-active' : isActive('italic', updatedAt) }"
        >
          Italic
        </button>
      </div>
    </template>

    <div x-ref="element"></div>
  </div>

  <script type="module" src="/main.js"></script>

  <style>
    body { margin: 2rem; font-family: sans-serif; }
    button.is-active { background: black; color: white; }
    .tiptap { padding: 0.5rem 1rem; margin: 1rem 0; border: 1px solid #ccc; }
  </style>
</body>
</html>

You should now see Tiptap in your browser. Time to give yourself a pat on the back! :)