mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 14:13:21 +08:00
move componentrenderer
This commit is contained in:
parent
3cca79b960
commit
30d53d33a5
@ -4,7 +4,7 @@
|
||||
|
||||
<script>
|
||||
import { Editor } from '@tiptap/core'
|
||||
import { EditorContent } from '@tiptap/vue'
|
||||
import { EditorContent, Renderer } from '@tiptap/vue'
|
||||
import extensions from '@tiptap/starter-kit'
|
||||
|
||||
export default {
|
||||
@ -22,6 +22,7 @@ export default {
|
||||
this.editor = new Editor({
|
||||
content: '<p>foo</p>',
|
||||
extensions: extensions(),
|
||||
renderer: Renderer,
|
||||
})
|
||||
},
|
||||
|
||||
|
@ -2,6 +2,7 @@ import { Editor, CommandSpec } from './src/Editor'
|
||||
|
||||
export default Editor
|
||||
export { Editor, CommandSpec }
|
||||
export { default as ComponentRenderer } from './src/ComponentRenderer'
|
||||
export { default as Extension } from './src/Extension'
|
||||
export { default as Node } from './src/Node'
|
||||
export { default as Mark } from './src/Mark'
|
||||
|
3
packages/core/src/ComponentRenderer.ts
Normal file
3
packages/core/src/ComponentRenderer.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export default abstract class ComponentRenderer {
|
||||
|
||||
}
|
@ -34,6 +34,7 @@ interface EditorOptions {
|
||||
content: EditorContent
|
||||
extensions: (Extension | Node | Mark)[]
|
||||
injectCSS: Boolean,
|
||||
renderer: any,
|
||||
}
|
||||
|
||||
@magicMethods
|
||||
@ -50,6 +51,7 @@ export class Editor extends EventEmitter {
|
||||
content: '',
|
||||
injectCSS: true,
|
||||
extensions: [],
|
||||
renderer: null,
|
||||
}
|
||||
|
||||
private lastCommand = Promise.resolve()
|
||||
|
@ -1,11 +1,12 @@
|
||||
import collect from 'collect.js'
|
||||
import { keymap } from 'prosemirror-keymap'
|
||||
import { inputRules } from 'prosemirror-inputrules'
|
||||
import { NodeSpec } from 'prosemirror-model'
|
||||
import { EditorView, Decoration } from 'prosemirror-view'
|
||||
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
||||
import { Editor, CommandSpec } from './Editor'
|
||||
import Extension from './Extension'
|
||||
import Node from './Node'
|
||||
import ComponentView from './ComponentView'
|
||||
import capitalize from './utils/capitalize'
|
||||
|
||||
export default class ExtensionManager {
|
||||
|
||||
@ -88,12 +89,24 @@ export default class ExtensionManager {
|
||||
}
|
||||
|
||||
get nodeViews() {
|
||||
const { renderer: Renderer } = this.editor.options
|
||||
|
||||
if (!Renderer || !Renderer.type) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const prop = `to${capitalize(Renderer.type)}`
|
||||
|
||||
return collect(this.nodes)
|
||||
.filter((schema: any) => schema.toVue)
|
||||
.filter((schema: any) => schema[prop])
|
||||
.map((schema: any) => {
|
||||
// @ts-ignore
|
||||
return (node, view, getPos, decorations) => {
|
||||
return new ComponentView(schema.toVue, {
|
||||
return (
|
||||
node: ProsemirrorNode,
|
||||
view: EditorView,
|
||||
getPos: (() => number) | boolean,
|
||||
decorations: Decoration[],
|
||||
) => {
|
||||
return new Renderer(schema[prop], {
|
||||
node,
|
||||
view,
|
||||
getPos,
|
||||
|
3
packages/core/src/utils/capitalize.ts
Normal file
3
packages/core/src/utils/capitalize.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export default function capitalize(value: string = ''): string {
|
||||
return value.charAt(0).toUpperCase() + value.slice(1)
|
||||
}
|
@ -12,7 +12,7 @@ export default class Paragraph extends Node {
|
||||
group: 'block',
|
||||
parseDOM: [{ tag: 'p' }],
|
||||
toDOM: () => ['p', 0],
|
||||
// toVue: ParagraphComponent,
|
||||
toVue: ParagraphComponent,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<p ref="content"></p>
|
||||
<p data-component="yeah" ref="content"></p>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
|
@ -1 +1,2 @@
|
||||
export { default as EditorContent } from './src/components/EditorContent'
|
||||
export { default as EditorContent } from './src/components/EditorContent'
|
||||
export { default as Renderer } from './src/Renderer'
|
||||
|
@ -12,6 +12,7 @@
|
||||
"dist"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"@tiptap/core": "2.x",
|
||||
"vue": "2.x"
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,20 @@
|
||||
import Vue from 'vue'
|
||||
import { ComponentRenderer } from '@tiptap/core'
|
||||
|
||||
export default class ComponentView {
|
||||
// @ts-ignore
|
||||
constructor(component, options) {
|
||||
export default class Renderer extends ComponentRenderer {
|
||||
|
||||
static type: string = 'vue'
|
||||
|
||||
vm!: Vue
|
||||
|
||||
constructor(component: Vue, options: any) {
|
||||
super()
|
||||
this.mount(component)
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
mount(component) {
|
||||
// @ts-ignore
|
||||
mount(component: Vue) {
|
||||
const Component = Vue.extend(component)
|
||||
|
||||
// @ts-ignore
|
||||
this.vm = new Component({
|
||||
// parent: this.parent,
|
||||
// propsData: props,
|
||||
@ -19,12 +22,10 @@ export default class ComponentView {
|
||||
}
|
||||
|
||||
get dom() {
|
||||
// @ts-ignore
|
||||
return this.vm.$el
|
||||
}
|
||||
|
||||
get contentDOM() {
|
||||
// @ts-ignore
|
||||
return this.vm.$refs.content
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user