add basic support for components

This commit is contained in:
Philipp Kühn 2020-04-24 09:32:37 +02:00
parent c2bd2b21fa
commit 3470a7be1c
7 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,29 @@
import Vue from 'vue'
export default class ComponentView {
// @ts-ignore
constructor(component, options) {
// @ts-ignore
this.component = component
// @ts-ignore
this.dom = this.createDOM()
// @ts-ignore
this.contentDOM = this.vm.$refs.content
}
createDOM() {
// @ts-ignore
const Component = Vue.extend(this.component)
// @ts-ignore
this.vm = new Component({
// parent: this.parent,
// propsData: props,
}).$mount()
// @ts-ignore
return this.vm.$el
}
}

View File

@ -170,6 +170,8 @@ export class Editor extends EventEmitter {
plugins: this.plugins,
}),
dispatchTransaction: this.dispatchTransaction.bind(this),
// @ts-ignore
nodeViews: this.extensionManager.nodeViews,
})
}

View File

@ -1,9 +1,11 @@
import collect from 'collect.js'
import { keymap } from 'prosemirror-keymap'
import { inputRules } from 'prosemirror-inputrules'
import { NodeSpec } from 'prosemirror-model'
import { Editor, CommandSpec } from './Editor'
import Extension from './Extension'
import Node from './Node'
import ComponentView from './ComponentView'
export default class ExtensionManager {
@ -85,4 +87,21 @@ export default class ExtensionManager {
.toArray()
}
get nodeViews() {
return collect(this.nodes)
.filter((schema: any) => schema.toVue)
.map((schema: any) => {
// @ts-ignore
return (node, view, getPos, decorations) => {
return new ComponentView(schema.toVue, {
node,
view,
getPos,
decorations,
})
}
})
.all()
}
}

View File

@ -1,5 +1,6 @@
import { Node } from '@tiptap/core'
import { NodeSpec } from 'prosemirror-model'
import ParagraphComponent from './paragraph.vue'
export default class Paragraph extends Node {
@ -11,6 +12,7 @@ export default class Paragraph extends Node {
group: 'block',
parseDOM: [{ tag: 'p' }],
toDOM: () => ['p', 0],
// toVue: ParagraphComponent,
}
}

View File

@ -0,0 +1,9 @@
<template>
<p ref="content"></p>
</template>
<script>
export default {
}
</script>

4
shims/vue.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}

View File

@ -25,6 +25,9 @@
"scripthost"
]
},
"files": [
"./shims/vue.d.ts"
],
"include": [
"docs/src/**/*.ts",
"docs/src/**/*.tsx",