feat: Allow to use commands within InputRule and PasteRule (#2035)

* add optional state prop to commandmanager

* add commands, chain and can getter to commandmanager

* use custom CommandManager for input rules and paste rules

* export commandmanager
This commit is contained in:
Philipp Kühn
2021-10-14 11:56:40 +02:00
committed by GitHub
parent 8c32dab55c
commit 4303637a78
6 changed files with 126 additions and 40 deletions

View File

@@ -1,7 +1,15 @@
import { EditorState, Plugin } from 'prosemirror-state'
import { Editor } from './Editor'
import CommandManager from './CommandManager'
import createChainableState from './helpers/createChainableState'
import isRegExp from './utilities/isRegExp'
import { Range, ExtendedRegExpMatchArray } from './types'
import {
Range,
ExtendedRegExpMatchArray,
SingleCommands,
ChainedCommands,
CanCommands,
} from './types'
export type PasteRuleMatch = {
index: number,
@@ -22,6 +30,9 @@ export class PasteRule {
state: EditorState,
range: Range,
match: ExtendedRegExpMatchArray,
commands: SingleCommands,
chain: () => ChainedCommands,
can: () => CanCommands,
}) => void
constructor(config: {
@@ -30,6 +41,9 @@ export class PasteRule {
state: EditorState,
range: Range,
match: ExtendedRegExpMatchArray,
commands: SingleCommands,
chain: () => ChainedCommands,
can: () => CanCommands,
}) => void,
}) {
this.find = config.find
@@ -69,6 +83,7 @@ const pasteRuleMatcherHandler = (text: string, find: PasteRuleFinder): ExtendedR
}
function run(config: {
editor: Editor,
state: EditorState,
from: number,
to: number,
@@ -76,12 +91,18 @@ function run(config: {
plugin: Plugin,
}): any {
const {
editor,
state,
from,
to,
rules,
} = config
const { commands, chain, can } = new CommandManager({
editor,
state,
})
state.doc.nodesBetween(from, to, (node, pos) => {
if (!node.isTextblock || node.type.spec.code) {
return
@@ -115,6 +136,9 @@ function run(config: {
state,
range,
match,
commands,
chain,
can,
})
})
})
@@ -126,7 +150,8 @@ function run(config: {
* text that matches any of the given rules to trigger the rules
* action.
*/
export function pasteRulesPlugin(rules: PasteRule[]): Plugin {
export function pasteRulesPlugin(props: { editor: Editor, rules: PasteRule[] }): Plugin {
const { editor, rules } = props
let isProseMirrorHTML = false
const plugin = new Plugin({
@@ -165,6 +190,7 @@ export function pasteRulesPlugin(rules: PasteRule[]): Plugin {
})
run({
editor,
state: chainableState,
from: Math.max(from - 1, 0),
to: to.b,