pass extension to component view

This commit is contained in:
Philipp Kühn
2018-11-09 21:55:54 +01:00
parent b16ae7d36d
commit 181cebc8f2
4 changed files with 36 additions and 38 deletions

View File

@@ -2,6 +2,7 @@ import Vue from 'vue'
export default class ComponentView {
constructor(component, {
extension,
parent,
node,
view,
@@ -9,6 +10,7 @@ export default class ComponentView {
decorations,
editable,
}) {
this.extension = extension
this.parent = parent
this.component = component
this.node = node
@@ -63,11 +65,14 @@ export default class ComponentView {
return true
}
stopEvent(event) {
// TODO: find a way to pass full extensions to ComponentView
// so we could check for schema.draggable
// for now we're allowing all drag events for node views
return !/drag/.test(event.type)
stopEvent() {
const draggable = !!this.extension.schema.draggable
if (draggable) {
return false
}
return true
}
update(node, decorations) {

View File

@@ -42,7 +42,6 @@ export default class Editor {
this.extensions = this.createExtensions()
this.nodes = this.createNodes()
this.marks = this.createMarks()
this.views = this.createViews()
this.schema = this.createSchema()
this.plugins = this.createPlugins()
this.keymaps = this.createKeymaps()
@@ -93,10 +92,6 @@ export default class Editor {
return this.extensions.marks
}
createViews() {
return this.extensions.views
}
createSchema() {
return new Schema({
nodes: this.nodes,
@@ -146,7 +141,10 @@ export default class Editor {
state: this.state,
dispatchTransaction: this.dispatchTransaction.bind(this),
nodeViews: initNodeViews({
nodes: this.views,
extensions: [
...builtInNodes,
...this.options.extensions,
],
editable: this.options.editable,
}),
})

View File

@@ -33,16 +33,6 @@ export default class ExtensionManager {
]), [])
}
get views() {
return this.extensions
.filter(extension => ['node', 'mark'].includes(extension.type))
.filter(extension => extension.view)
.reduce((views, { name, view }) => ({
...views,
[name]: view,
}), {})
}
keymaps({ schema }) {
const extensionKeymaps = this.extensions
.filter(extension => ['extension'].includes(extension.type))

View File

@@ -1,22 +1,27 @@
import ComponentView from './ComponentView'
export default function initNodeViews({ parent, nodes, editable }) {
const nodeViews = {}
export default function initNodeViews({ parent, extensions, editable }) {
return extensions
.filter(extension => ['node', 'mark'].includes(extension.type))
.filter(extension => extension.view)
.reduce((nodeViews, extension) => {
const nodeView = (node, view, getPos, decorations) => {
const component = extension.view
Object.keys(nodes).forEach(nodeName => {
nodeViews[nodeName] = (node, view, getPos, decorations) => {
const component = nodes[nodeName]
return new ComponentView(component, {
extension,
parent,
node,
view,
getPos,
decorations,
editable,
})
}
return new ComponentView(component, {
parent,
node,
view,
getPos,
decorations,
editable,
})
}
})
return nodeViews
return {
...nodeViews,
[extension.name]: nodeView,
}
}, {})
}