question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue 3 - Can't use `setup()` in Node View

See original GitHub issue

What happens? If you create a Vue 3 component for a node view with a setup() method, it is never called.

This seems to be because when doing this to extend the component it doesn’t inherit the setup method:

https://github.com/ueberdosis/tiptap-next/blob/6d665711289f46933be6ca0063d45597972ad6e4/packages/vue-3/src/VueNodeViewRenderer.ts#L144-L151

I tried extends: (component as any) rather than unpacking and it didn’t work. (Haven’t been able to get my head around Vue 3 types on components, Component and DefineComponent don’t seem to work…)

This however sort of does:

    const extendedComponent = defineComponent({
      extends: { ...component },
      props: Object.keys(props),
      components: {
        NodeViewWrapper,
        NodeViewContent,
      },
      setup() {
        if((component as any).setup) {
          return (component as any).setup()
        } else {
          return {}
        }
      }
    })

However, a quick test with a component like this:

<template>
  <node-view-wrapper data-type="counterLine">
    <node-view-content as="p" class="counter-content"></node-view-content>
    <p class="counter-output" contenteditable="false">
      Counter: {{ counter }}
    </p>
  </node-view-wrapper>
</template>

<script>
import { defineComponent, ref, onBeforeUnmount } from 'vue';

export default defineComponent({
  name: 'CounterLine',
  
  setup() {
    const counter = ref(0);

    const interval = setInterval(() => {
      counter.value++
    }, 1000)

    onBeforeUnmount(() => {
      clearInterval(interval)
    })
    
    return { counter }
  }
  
})
</script>

Shows that we are getting a mismatch on components to node views, when I add new lines in the middle the the nodes at the bottom are the new ones with a zero counter:

https://user-images.githubusercontent.com/31130/109534874-bcc9ab80-7ab3-11eb-8b67-32994c40e1c4.mov

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
philippkuehncommented, Mar 5, 2021

The initial issue is fixed so I’m closing this for now.

But the caching problem still exists, of course. And I can’t imagine right now that we can really fix it. But we should probably open a new issue on that (there is already this one in v1: https://github.com/ueberdosis/tiptap/issues/372)

0reactions
samwilliscommented, Mar 2, 2021

Yep, sorry didn’t explain that very well.

I believe we may be able to change the <teleport :to= target to another element, moving the component. So when prosemirror creates a new node view (rather then moving it and even before removing the old one) we may be able to detect that and move the component instance to the new location in a new container element (leaving behind the old container to be removed by prosemirror).

With this we can’t do the unwrapping we currently do (so requiring attribute mapping onto the container element) but may enable retaining state within the component as it is moved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue3 setup returned value can't be found in the rest of that ...
Correct way achieving it using Composition API: Vue.createApp({ setup(props) { const c = Vue.ref(123); const increment = () => c.value++; ...
Read more >
Vue.js 3 Composition API: the SetUp Function | Geek Culture
Vue 3 introduced the setUp function. It enables us to extract repeatable parts of components into reusable pieces of code.
Read more >
Vue 3.2 - Using Composition API with Script Setup
Within the script block, we export an object with three keys: name, computed, and methods. If you are familiar with Vue, this should...
Read more >
How to use $refs in Vue 3 with Composition API and Script ...
This post is going to explain how to use Ref in Vue 3, while using the Composition API and the Script Setup. The...
Read more >
Vue 3 Script Setup Tutorial - This Changes Everything!
In this video, I'm gonna show everything you need to know to get started with the Script Setup pattern for Vue 3 &...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found