add try command

This commit is contained in:
Philipp Kühn
2020-11-04 15:31:42 +01:00
parent 0ba95a7ae3
commit 7a16546d6f
4 changed files with 57 additions and 28 deletions

View File

@@ -261,15 +261,20 @@ export class Editor extends EventEmitter {
*/ */
private createView() { private createView() {
this.view = new EditorView(this.options.element, { this.view = new EditorView(this.options.element, {
dispatchTransaction: this.dispatchTransaction.bind(this),
state: EditorState.create({ state: EditorState.create({
doc: this.createDocument(this.options.content), doc: this.createDocument(this.options.content),
plugins: this.extensionManager.plugins,
}), }),
dispatchTransaction: this.dispatchTransaction.bind(this),
}) })
// `editor.view` is not yet available at this time. // `editor.view` is not yet available at this time.
// Therefore we will add all node views directly afterwards. // Therefore we will add all plugins and node views directly afterwards.
const newState = this.state.reconfigure({
plugins: this.extensionManager.plugins,
})
this.view.updateState(newState)
this.view.setProps({ this.view.setProps({
nodeViews: this.extensionManager.nodeViews, nodeViews: this.extensionManager.nodeViews,
}) })

View File

@@ -1,5 +1,4 @@
import { import {
chainCommands,
newlineInCode, newlineInCode,
createParagraphNear, createParagraphNear,
liftEmptyBlock, liftEmptyBlock,
@@ -16,33 +15,31 @@ import { createExtension } from '../Extension'
export const BaseKeymap = createExtension({ export const BaseKeymap = createExtension({
addKeyboardShortcuts() { addKeyboardShortcuts() {
const enter = chainCommands( const handleBackspace = () => this.editor.try(({ state, dispatch }) => [
newlineInCode, () => undoInputRule(state, dispatch),
createParagraphNear, () => deleteSelection(state, dispatch),
liftEmptyBlock, () => joinBackward(state, dispatch),
splitBlockKeepMarks, () => selectNodeBackward(state, dispatch),
) ])
const backspace = chainCommands( const handleDelete = () => this.editor.try(({ state, dispatch }) => [
undoInputRule, () => deleteSelection(state, dispatch),
deleteSelection, () => joinForward(state, dispatch),
joinBackward, () => selectNodeForward(state, dispatch),
selectNodeBackward, ])
)
const del = chainCommands(
deleteSelection,
joinForward,
selectNodeForward,
)
return { return {
Enter: enter, Enter: () => this.editor.try(({ state, dispatch }) => [
() => newlineInCode(state, dispatch),
() => createParagraphNear(state, dispatch),
() => liftEmptyBlock(state, dispatch),
() => splitBlockKeepMarks(state, dispatch),
]),
'Mod-Enter': exitCode, 'Mod-Enter': exitCode,
Backspace: backspace, Backspace: () => handleBackspace(),
'Mod-Backspace': backspace, 'Mod-Backspace': () => handleBackspace(),
Delete: del, Delete: () => handleDelete(),
'Mod-Delete': del, 'Mod-Delete': () => handleDelete(),
// we dont need a custom `selectAll` for now // we dont need a custom `selectAll` for now
// 'Mod-a': () => this.editor.selectAll(), // 'Mod-a': () => this.editor.selectAll(),
} }

View File

@@ -24,6 +24,7 @@ export { SplitListItem } from './splitListItem'
export { ToggleBlockType } from './toggleBlockType' export { ToggleBlockType } from './toggleBlockType'
export { ToggleList } from './toggleList' export { ToggleList } from './toggleList'
export { ToggleMark } from './toggleMark' export { ToggleMark } from './toggleMark'
export { UpdateMark } from './updateMark'
export { ToggleWrap } from './toggleWrap' export { ToggleWrap } from './toggleWrap'
export { Try } from './try'
export { UpdateMark } from './updateMark'
export { WrapInList } from './wrapInList' export { WrapInList } from './wrapInList'

View File

@@ -0,0 +1,26 @@
import { Command } from '../Editor'
import { createExtension } from '../Extension'
export const Try = createExtension({
addCommands() {
return {
try: (fn: (props: Parameters<Command>[0]) => Command[]): Command => props => {
const commands = fn(props)
for (let i = 0; i < commands.length; i += 1) {
if (commands[i](props)) {
return true
}
}
return false
},
}
},
})
declare module '../Editor' {
interface AllExtensions {
Try: typeof Try,
}
}