improve node views

This commit is contained in:
Philipp Kühn
2020-11-24 10:51:57 +01:00
parent 3128c5eb9d
commit dcac8e5407
5 changed files with 60 additions and 67 deletions

View File

@@ -46,6 +46,7 @@ module.exports = {
'func-names': ['error', 'never'], 'func-names': ['error', 'never'],
'arrow-body-style': 'off', 'arrow-body-style': 'off',
'max-len': 'off', 'max-len': 'off',
'vue/one-component-per-file': 'off',
'vue/this-in-template': ['error', 'never'], 'vue/this-in-template': ['error', 'never'],
'vue/max-attributes-per-line': ['error', { 'vue/max-attributes-per-line': ['error', {
singleline: 3, singleline: 3,

View File

@@ -1,25 +1,13 @@
<template> <template>
<!-- <div style="white-space: normal;"> <node-view-wrapper>
node view <button @click="toggleChecked" contenteditable="false">
<button @click="editor.chain().focus().insertText('hey').run()">
button
</button>
<div>
attrs {{ node.attrs }}
</div>
<inner />
</div> -->
<!-- <inner /> -->
<div style="white-space: normal;">
<button @click="toggleChecked">
toggle checked toggle checked
</button> </button>
hey {{ node.attrs.checked }} <div contenteditable="false">
<inner /> checked: {{ node.attrs.checked }}
</div> </div>
<node-view-content />
<!-- <inner as="p" :class="random" foo="bar" /> --> </node-view-wrapper>
<!-- <component :is="inner" as="p" /> -->
</template> </template>
<script> <script>
@@ -35,12 +23,7 @@ export default {
required: true, required: true,
}, },
inner: { updateAttributes: {
type: [Object, Function],
required: true,
},
updateAttrs: {
type: Function, type: Function,
required: true, required: true,
}, },
@@ -48,31 +31,23 @@ export default {
data() { data() {
return { return {
random: 'foo', // random: 'foo',
} }
}, },
methods: { methods: {
toggleChecked() { toggleChecked() {
this.editor.commands.focus() // this.editor.commands.focus()
this.updateAttrs({ this.updateAttributes({
checked: !this.node.attrs.checked, checked: !this.node.attrs.checked,
}) })
}, },
}, },
mounted() { mounted() {
// console.log(this.node)
console.log('mounted')
// setInterval(() => { // setInterval(() => {
// this.random = `foo${Math.random()}` // this.random = `foo${Math.random()}`
// }, 1000) // }, 1000)
// console.log(this)
}, },
} }
</script> </script>
<style>
</style>

View File

@@ -13,8 +13,6 @@ export default Node.create({
selectable: false, selectable: false,
// atom: true,
addAttributes() { addAttributes() {
return { return {
checked: { checked: {

View File

@@ -6,9 +6,6 @@ import {
} from 'prosemirror-model' } from 'prosemirror-model'
import Vue from 'vue' import Vue from 'vue'
import { VueConstructor } from 'vue/types/umd' import { VueConstructor } from 'vue/types/umd'
// import Inner from './components/Inner.vue'
// const Inner = Vue.extend()
class VueNodeView implements NodeView { class VueNodeView implements NodeView {
@@ -31,16 +28,16 @@ class VueNodeView implements NodeView {
this.extension = props.extension this.extension = props.extension
this.node = props.node this.node = props.node
this.getPos = props.getPos this.getPos = props.getPos
this.createUniqueId()
this.mount(component) this.mount(component)
} }
mount(component: Vue | VueConstructor) { createUniqueId() {
this.id = `id_${Math.random().toString(36).replace('0.', '')}` this.id = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`
}
const { id } = this createNodeViewWrapper() {
return Vue.extend({
const Inner = Vue.extend({
// functional: true,
inheritAttrs: false, inheritAttrs: false,
props: { props: {
as: { as: {
@@ -48,38 +45,69 @@ class VueNodeView implements NodeView {
default: 'div', default: 'div',
}, },
}, },
render(createElement, context) { render(createElement) {
return createElement(
this.as, {
style: {
whiteSpace: 'normal',
},
},
this.$slots.default,
)
},
})
}
createNodeViewContent() {
const { id } = this
return Vue.extend({
inheritAttrs: false,
props: {
as: {
type: String,
default: 'div',
},
},
render(createElement) {
return createElement( return createElement(
// context.props.as, {
this.as, { this.as, {
style: { style: {
whiteSpace: 'pre-wrap', whiteSpace: 'pre-wrap',
}, },
attrs: { attrs: {
id, id,
// contenteditable: true, contenteditable: true,
}, },
}, },
) )
}, },
}) })
}
mount(component: Vue | VueConstructor) {
const NodeViewWrapper = this.createNodeViewWrapper()
const NodeViewContent = this.createNodeViewContent()
const Component = Vue const Component = Vue
.extend(component) .extend(component)
.extend({ .extend({
components: { components: {
Inner, NodeViewWrapper,
NodeViewContent,
}, },
}) })
const props = { const props = {
editor: this.editor, editor: this.editor,
inner: Inner, NodeViewWrapper,
NodeViewContent,
node: this.node, node: this.node,
updateAttrs: (attrs: {}) => this.updateAttrs(attrs), updateAttributes: (attrs: {}) => this.updateAttributes(attrs),
} }
this.vm = new Component({ this.vm = new Component({
// TODO: get parent component <editor-content>
// parent: this.parent, // parent: this.parent,
propsData: props, propsData: props,
}).$mount() }).$mount()
@@ -98,8 +126,6 @@ class VueNodeView implements NodeView {
} }
stopEvent(event: Event): boolean { stopEvent(event: Event): boolean {
// console.log(event.type)
const isDraggable = this.node.type.spec.draggable const isDraggable = this.node.type.spec.draggable
const isCopy = event.type === 'copy' const isCopy = event.type === 'copy'
const isPaste = event.type === 'paste' const isPaste = event.type === 'paste'
@@ -139,7 +165,6 @@ class VueNodeView implements NodeView {
this.node = node this.node = node
this.decorations = decorations this.decorations = decorations
this.updateComponentProps() this.updateComponentProps()
return true return true
@@ -150,24 +175,17 @@ class VueNodeView implements NodeView {
this.vm.$props.decorations = this.decorations this.vm.$props.decorations = this.decorations
} }
updateAttrs(attrs: {}) { updateAttributes(attributes: {}) {
if (!this.editor.view.editable) { if (!this.editor.view.editable) {
return return
} }
const { state } = this.editor.view const { state } = this.editor.view
// const { type } = this.node
const pos = this.getPos() const pos = this.getPos()
const newAttrs = { const transaction = state.tr.setNodeMarkup(pos, undefined, {
...this.node.attrs, ...this.node.attrs,
...attrs, ...attributes,
} })
// const transaction = this.isMark
// ? state.tr
// .removeMark(pos.from, pos.to, type)
// .addMark(pos.from, pos.to, type.create(newAttrs))
// : state.tr.setNodeMarkup(pos, null, newAttrs)
const transaction = state.tr.setNodeMarkup(pos, undefined, newAttrs)
this.editor.view.dispatch(transaction) this.editor.view.dispatch(transaction)
} }

View File

@@ -17,6 +17,7 @@ export default Vue.extend({
if (editor && editor.options.element) { if (editor && editor.options.element) {
this.$nextTick(() => { this.$nextTick(() => {
this.$el.appendChild(editor.options.element.firstChild) this.$el.appendChild(editor.options.element.firstChild)
console.log('append')
// editor.setParentComponent(this) // editor.setParentComponent(this)
}) })
} }