mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-28 15:49:23 +08:00
add try command
This commit is contained in:
parent
0ba95a7ae3
commit
7a16546d6f
@ -261,15 +261,20 @@ export class Editor extends EventEmitter {
|
||||
*/
|
||||
private createView() {
|
||||
this.view = new EditorView(this.options.element, {
|
||||
dispatchTransaction: this.dispatchTransaction.bind(this),
|
||||
state: EditorState.create({
|
||||
doc: this.createDocument(this.options.content),
|
||||
plugins: this.extensionManager.plugins,
|
||||
}),
|
||||
dispatchTransaction: this.dispatchTransaction.bind(this),
|
||||
})
|
||||
|
||||
// `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({
|
||||
nodeViews: this.extensionManager.nodeViews,
|
||||
})
|
||||
|
@ -1,5 +1,4 @@
|
||||
import {
|
||||
chainCommands,
|
||||
newlineInCode,
|
||||
createParagraphNear,
|
||||
liftEmptyBlock,
|
||||
@ -16,33 +15,31 @@ import { createExtension } from '../Extension'
|
||||
|
||||
export const BaseKeymap = createExtension({
|
||||
addKeyboardShortcuts() {
|
||||
const enter = chainCommands(
|
||||
newlineInCode,
|
||||
createParagraphNear,
|
||||
liftEmptyBlock,
|
||||
splitBlockKeepMarks,
|
||||
)
|
||||
const handleBackspace = () => this.editor.try(({ state, dispatch }) => [
|
||||
() => undoInputRule(state, dispatch),
|
||||
() => deleteSelection(state, dispatch),
|
||||
() => joinBackward(state, dispatch),
|
||||
() => selectNodeBackward(state, dispatch),
|
||||
])
|
||||
|
||||
const backspace = chainCommands(
|
||||
undoInputRule,
|
||||
deleteSelection,
|
||||
joinBackward,
|
||||
selectNodeBackward,
|
||||
)
|
||||
|
||||
const del = chainCommands(
|
||||
deleteSelection,
|
||||
joinForward,
|
||||
selectNodeForward,
|
||||
)
|
||||
const handleDelete = () => this.editor.try(({ state, dispatch }) => [
|
||||
() => deleteSelection(state, dispatch),
|
||||
() => joinForward(state, dispatch),
|
||||
() => selectNodeForward(state, dispatch),
|
||||
])
|
||||
|
||||
return {
|
||||
Enter: enter,
|
||||
Enter: () => this.editor.try(({ state, dispatch }) => [
|
||||
() => newlineInCode(state, dispatch),
|
||||
() => createParagraphNear(state, dispatch),
|
||||
() => liftEmptyBlock(state, dispatch),
|
||||
() => splitBlockKeepMarks(state, dispatch),
|
||||
]),
|
||||
'Mod-Enter': exitCode,
|
||||
Backspace: backspace,
|
||||
'Mod-Backspace': backspace,
|
||||
Delete: del,
|
||||
'Mod-Delete': del,
|
||||
Backspace: () => handleBackspace(),
|
||||
'Mod-Backspace': () => handleBackspace(),
|
||||
Delete: () => handleDelete(),
|
||||
'Mod-Delete': () => handleDelete(),
|
||||
// we don’t need a custom `selectAll` for now
|
||||
// 'Mod-a': () => this.editor.selectAll(),
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ export { SplitListItem } from './splitListItem'
|
||||
export { ToggleBlockType } from './toggleBlockType'
|
||||
export { ToggleList } from './toggleList'
|
||||
export { ToggleMark } from './toggleMark'
|
||||
export { UpdateMark } from './updateMark'
|
||||
export { ToggleWrap } from './toggleWrap'
|
||||
export { Try } from './try'
|
||||
export { UpdateMark } from './updateMark'
|
||||
export { WrapInList } from './wrapInList'
|
||||
|
26
packages/core/src/extensions/try.ts
Normal file
26
packages/core/src/extensions/try.ts
Normal 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,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user