add basic new commands

This commit is contained in:
Philipp Kühn
2018-10-28 19:26:14 +01:00
parent 11bb74babb
commit d2519c2953
5 changed files with 90 additions and 5 deletions

View File

@@ -1,7 +1,39 @@
<template> <template>
<div class="editor"> <div class="editor">
<menu-bar class="menubar" :editor="editor"> <menu-bar class="menubar" :editor="editor">
<template slot-scope="{ nodes, marks }"> <template slot-scope="{ nodes, marks, newCommands }">
<button
class="menubar__button"
@click="newCommands.history"
v-if="newCommands.history"
>
</button>
<button
class="menubar__button"
@click="newCommands.undo"
v-if="newCommands.undo"
>
</button>
<button
class="menubar__button"
@click="newCommands.redo"
v-if="newCommands.redo"
>
</button>
<button
class="menubar__button"
@click="newCommands.undoRedo"
v-if="newCommands.undoRedo"
>
undoredo
</button>
<button <button
class="menubar__button" class="menubar__button"

View File

@@ -1,5 +1,6 @@
import { Extension } from 'tiptap' import { Extension } from 'tiptap'
import { history, undo, redo } from 'prosemirror-history' import { history, undo, redo } from 'prosemirror-history'
import { setBlockType, textblockTypeInputRule, toggleBlockType } from 'tiptap-commands'
export default class History extends Extension { export default class History extends Extension {
@@ -27,4 +28,11 @@ export default class History extends Extension {
] ]
} }
commands() {
return {
undo: () => undo,
redo: () => redo,
}
}
} }

View File

@@ -12,6 +12,7 @@ export default {
marks: this.editor.menuActions.marks, marks: this.editor.menuActions.marks,
focused: this.editor.view.focused, focused: this.editor.view.focused,
focus: this.editor.focus, focus: this.editor.focus,
newCommands: this.editor.newCommands,
})) }))
} }
}, },

View File

@@ -14,7 +14,7 @@ import {
// menuBubble, // menuBubble,
// floatingMenu, // floatingMenu,
builtInKeymap, builtInKeymap,
} from '../Utils' } from '.'
import builtInNodes from '../Nodes' import builtInNodes from '../Nodes'
@@ -52,6 +52,14 @@ export default class Editor {
this.state = this.createState() this.state = this.createState()
this.view = this.createView() this.view = this.createView()
this.commands = this.createCommands() this.commands = this.createCommands()
this.newCommands = this.extensions.newCommands({
schema: this.schema,
view: this.view,
})
console.log(this.newCommands)
this.updateMenuActions() this.updateMenuActions()
this.emit('init') this.emit('init')

View File

@@ -107,4 +107,40 @@ export default class ExtensionManager {
}), {}) }), {})
} }
newCommands({ schema, view }) {
return this.extensions
.filter(extension => extension.commands)
.reduce((allCommands, { name, type, commands: provider }) => {
const commands = {}
const value = provider({
schema,
...['node', 'mark'].includes(type) ? {
type: schema[`${type}s`][name],
} : {},
})
if (Array.isArray(value)) {
commands[name] = attrs => value
.forEach(callback => callback(attrs)(view.state, view.dispatch, view))
} else if (typeof value === 'function') {
commands[name] = attrs => value(attrs)(view.state, view.dispatch, view)
} else if (typeof value === 'object') {
Object.entries(value).forEach(([commandName, commandValue]) => {
if (Array.isArray(commandValue)) {
commands[commandName] = attrs => commandValue
.forEach(callback => callback(attrs)(view.state, view.dispatch, view))
} else {
commands[commandName] = attrs => commandValue(attrs)(view.state, view.dispatch, view)
}
})
}
return {
...allCommands,
...commands,
}
}, {})
}
} }