refactoring

This commit is contained in:
Philipp Kühn 2021-01-18 13:06:56 +01:00 committed by Hans Pagel
parent 18d3cbdef8
commit 3cb4b0f71f
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> </template>
<script> <script>
import Vue from 'vue'
import tippy, { sticky } from 'tippy.js' import tippy, { sticky } from 'tippy.js'
import { Editor } from '@tiptap/core' import { Editor, EditorContent, VueRenderer } from '@tiptap/vue'
import { EditorContent } from '@tiptap/vue'
import Document from '@tiptap/extension-document' import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph' import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text' import Text from '@tiptap/extension-text'
import Mention from '@tiptap/extension-mention' import Mention from '@tiptap/extension-mention'
import MentionList from './MentionList'
export default { export default {
components: { components: {
@ -33,47 +32,37 @@ export default {
Text, Text,
Mention.configure({ Mention.configure({
items: query => { items: query => {
console.log('items', query) return ['foo', 'bar']
return [query]
}, },
renderer: () => { renderer: () => {
let component
let popup let popup
const Component = new (Vue.extend({
template: '<div>YAAAAY</div>',
}))().$mount()
return { return {
onStart(props) { onStart(props) {
console.log('start') component = new VueRenderer(MentionList, props)
popup = tippy('.app', { popup = tippy('.app', {
getReferenceClientRect: () => props.virtualNode.getBoundingClientRect(), getReferenceClientRect: () => props.virtualNode.getBoundingClientRect(),
appendTo: () => document.body, appendTo: () => document.body,
interactive: true, interactive: true,
sticky: true, // make sure position of tippy is updated when content changes sticky: true,
plugins: [sticky], plugins: [sticky],
content: Component.$el, content: component.element,
trigger: 'mouseenter', // manual trigger: 'mouseenter',
showOnCreate: true, showOnCreate: true,
theme: 'dark', theme: 'dark',
placement: 'top-start', placement: 'top-start',
inertia: true, inertia: true,
duration: [400, 200], duration: [400, 200],
}) })
}, },
onUpdate(props) { onUpdate(props) {
console.log('update', props) component.update(props)
}, },
onExit(props) { onExit() {
console.log('exit', props) popup[0].destroy()
component.destroy()
if (popup) {
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 * from '@tiptap/core'
export { default as VueRenderer } from './VueRenderer'
export { default as VueNodeViewRenderer } from './VueNodeViewRenderer' export { default as VueNodeViewRenderer } from './VueNodeViewRenderer'
export { default as EditorContent } from './components/EditorContent' export { default as EditorContent } from './components/EditorContent'