Gryphz
Gryphz

Reputation: 33

Lifting Tiptap editor instance to Pinia store

I have a very basic tiptap editor setup in VueJS, something like this:

// TipTap.vue

<template>
  <div>
    <editor-content :editor="editor" spellcheck="false" />
  </div>
</template>
<script setup>
// Imports
const editor = ref(null)

onMounted(() => {
  editor.value = new Editor({
    editable: true,
    extensions: [
      StarterKit
    ],
    content: props.modelValue,
  })
})
</script>

And it works fine, but when I want to centralize my state in a Pinia store so that I'd be able to check editors state or call its' methods from different components, like so:

// editorStore.js

export const useEditorStore = defineStore('editor', () => {
  const editorRef = ref(null)
  const textEditorContent = ref('')

  const initializeEditor = () => {
    editorRef.value = new Editor({
      editable: true,
      extensions: [
        StarterKit
      ],
      content: textEditorContent.value,
    })
  }
})

<template>
  <div>
    <editor-content :editor="editor" spellcheck="false" />
  </div>
</template>
<script setup>

const editorStore = useEditorStore()
const editor = editorStore.editorRef
onMounted(() => {
  editorStore.initializeEditor()
})

It either breaks the reactivity

Or creates too much recursion

I feel like I am missing crucial logic when it comes to reactivity/state management here. What could be the problem?

Upvotes: 0

Views: 27

Answers (0)

Related Questions