refactoring

This commit is contained in:
Philipp Kühn 2021-01-18 13:06:56 +01:00
parent f96dff1f0f
commit 9651958ac4
4 changed files with 76 additions and 23 deletions

View File

@ -0,0 +1,22 @@
<template>
<div>
<div v-for="(item, index) in items" :key="index">
{{ item }}
</div>
</div>
</template>
<script>
export default {
props: {
items: {
type: Array,
default: () => [],
},
},
}
</script>
<style>
</style>

View File

@ -5,14 +5,13 @@
</template>
<script>
import Vue from 'vue'
import tippy, { sticky } from 'tippy.js'
import { Editor } from '@tiptap/core'
import { EditorContent } from '@tiptap/vue'
import { Editor, EditorContent, VueRenderer } from '@tiptap/vue'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Mention from '@tiptap/extension-mention'
import MentionList from './MentionList'
export default {
components: {
@ -33,47 +32,37 @@ export default {
Text,
Mention.configure({
items: query => {
console.log('items', query)
return [query]
return ['foo', 'bar']
},
renderer: () => {
let component
let popup
const Component = new (Vue.extend({
template: '<div>YAAAAY</div>',
}))().$mount()
return {
onStart(props) {
console.log('start')
component = new VueRenderer(MentionList, props)
popup = tippy('.app', {
getReferenceClientRect: () => props.virtualNode.getBoundingClientRect(),
appendTo: () => document.body,
interactive: true,
sticky: true, // make sure position of tippy is updated when content changes
sticky: true,
plugins: [sticky],
content: Component.$el,
trigger: 'mouseenter', // manual
content: component.element,
trigger: 'mouseenter',
showOnCreate: true,
theme: 'dark',
placement: 'top-start',
inertia: true,
duration: [400, 200],
})
},
onUpdate(props) {
console.log('update', props)
component.update(props)
},
onExit(props) {
console.log('exit', props)
if (popup) {
popup[0].destroy()
}
Component.$destroy()
onExit() {
popup[0].destroy()
component.destroy()
},
}
},

View File

@ -0,0 +1,41 @@
import Vue from 'vue'
import { VueConstructor } from 'vue/types/umd'
export default class VueRenderer {
vm!: Vue
constructor(component: Vue | VueConstructor, props: any) {
const Component = Vue.extend(component)
this.vm = new Component({
// parent,
propsData: props,
}).$mount()
}
get element() {
return this.vm.$el
}
update(data: { [key: string]: any } = {}) {
if (!this.vm.$props) {
return
}
// prevents `Avoid mutating a prop directly` error message
const originalSilent = Vue.config.silent
Vue.config.silent = true
Object
.entries(data)
.forEach(([key, value]) => {
this.vm.$props[key] = value
})
Vue.config.silent = originalSilent
}
destroy() {
this.vm.$destroy()
}
}

View File

@ -1,3 +1,4 @@
export * from '@tiptap/core'
export { default as VueRenderer } from './VueRenderer'
export { default as VueNodeViewRenderer } from './VueNodeViewRenderer'
export { default as EditorContent } from './components/EditorContent'