Merge branch 'main' of github.com:ueberdosis/tiptap-next into main

This commit is contained in:
Hans Pagel
2020-11-13 15:08:40 +01:00
52 changed files with 177 additions and 179 deletions

View File

@@ -1,6 +1,9 @@
import { EditorState, Transaction } from 'prosemirror-state'
import {
SingleCommands, ChainedCommands, Editor, CommandSpec,
SingleCommands,
ChainedCommands,
Editor,
CommandSpec,
} from './Editor'
import getAllMethodNames from './utils/getAllMethodNames'
@@ -37,26 +40,25 @@ export default class CommandManager {
return this.editor
}
public runSingleCommand(name: string) {
public createCommands() {
const { commands, editor } = this
const { state, view } = editor
const command = commands[name]
if (!command) {
// TODO: prevent vue devtools to throw error
// throw new Error(`tiptap: command '${name}' not found.`)
return
}
return Object.fromEntries(Object
.entries(commands)
.map(([name, command]) => {
const method = (...args: any) => {
const { tr } = state
const props = this.buildProps(tr)
const callback = command(...args)(props)
return (...args: any) => {
const { tr } = state
const props = this.buildProps(tr)
const callback = command(...args)(props)
view.dispatch(tr)
view.dispatch(tr)
return callback
}
return callback
}
return [name, method]
})) as SingleCommands
}
public createChain(startTr?: Transaction, shouldDispatch = true) {
@@ -64,11 +66,7 @@ export default class CommandManager {
const { state, view } = editor
const callbacks: boolean[] = []
const hasStartTransaction = !!startTr
const tr = hasStartTransaction ? startTr : state.tr
if (!tr) {
return
}
const tr = startTr || state.tr
return new Proxy({}, {
get: (_, name: string, proxy) => {
@@ -101,13 +99,7 @@ export default class CommandManager {
const { commands, editor } = this
const { state } = editor
const dispatch = false
const hasStartTransaction = !!startTr
const tr = hasStartTransaction ? startTr : state.tr
if (!tr) {
return
}
const tr = startTr || state.tr
const props = this.buildProps(tr, dispatch)
const formattedCommands = Object.fromEntries(Object
.entries(commands)

View File

@@ -68,10 +68,6 @@ interface EditorOptions {
editable: boolean,
}
declare module './Editor' {
interface Editor extends SingleCommands {}
}
@magicMethods
export class Editor extends EventEmitter {
@@ -116,7 +112,7 @@ export class Editor extends EventEmitter {
this.createView()
this.injectCSS()
window.setTimeout(() => this.proxy.focus(this.options.autoFocus), 0)
window.setTimeout(() => this.commands.focus(this.options.autoFocus), 0)
}
/**
@@ -126,7 +122,14 @@ export class Editor extends EventEmitter {
*/
// eslint-disable-next-line
private __get(name: string) {
return this.commandManager.runSingleCommand(name)
// TODO: maybe remove proxy
}
/**
* An object of all registered commands.
*/
public get commands() {
return this.commandManager.createCommands()
}
/**

View File

@@ -14,21 +14,21 @@ import { createExtension } from '../Extension'
export const Keymap = createExtension({
addKeyboardShortcuts() {
const handleBackspace = () => this.editor.try(({ state, dispatch }) => [
const handleBackspace = () => this.editor.commands.try(({ state, dispatch }) => [
() => undoInputRule(state, dispatch),
() => deleteSelection(state, dispatch),
() => joinBackward(state, dispatch),
() => selectNodeBackward(state, dispatch),
])
const handleDelete = () => this.editor.try(({ state, dispatch }) => [
const handleDelete = () => this.editor.commands.try(({ state, dispatch }) => [
() => deleteSelection(state, dispatch),
() => joinForward(state, dispatch),
() => selectNodeForward(state, dispatch),
])
return {
Enter: () => this.editor.try(({ commands, state, dispatch }) => [
Enter: () => this.editor.commands.try(({ commands, state, dispatch }) => [
() => newlineInCode(state, dispatch),
() => createParagraphNear(state, dispatch),
() => liftEmptyBlock(state, dispatch),