prosemirror-inputrules
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -0,1 +1,7 @@ | ||
## 1.3.0 (2023-11-16) | ||
### New features | ||
Input rules can now be set to be non-undoable, preventing `undoInputRule` from rolling them back. | ||
## 1.2.1 (2023-05-17) | ||
@@ -2,0 +8,0 @@ |
@@ -31,4 +31,17 @@ import { EditorState, Transaction, Plugin, Command } from 'prosemirror-state'; | ||
*/ | ||
match: RegExp, handler: string | ((state: EditorState, match: RegExpMatchArray, start: number, end: number) => Transaction | null)); | ||
match: RegExp, handler: string | ((state: EditorState, match: RegExpMatchArray, start: number, end: number) => Transaction | null), options?: { | ||
/** | ||
When set to false, | ||
[`undoInputRule`](https://prosemirror.net/docs/ref/#inputrules.undoInputRule) doesn't work on | ||
this rule. | ||
*/ | ||
undoable?: boolean; | ||
}); | ||
} | ||
type PluginState = { | ||
transform: Transaction; | ||
from: number; | ||
to: number; | ||
text: string; | ||
} | null; | ||
/** | ||
@@ -41,8 +54,3 @@ Create an input rules plugin. When enabled, it will cause text | ||
rules: readonly InputRule[]; | ||
}): Plugin<{ | ||
transform: Transaction; | ||
from: number; | ||
to: number; | ||
text: string; | ||
} | null>; | ||
}): Plugin<PluginState>; | ||
/** | ||
@@ -49,0 +57,0 @@ This is a command that will undo an input rule, if applying such a |
@@ -32,6 +32,7 @@ import { Plugin } from 'prosemirror-state'; | ||
*/ | ||
match, handler) { | ||
match, handler, options = {}) { | ||
this.match = match; | ||
this.match = match; | ||
this.handler = typeof handler == "string" ? stringHandler(handler) : handler; | ||
this.undoable = options.undoable !== false; | ||
} | ||
@@ -98,7 +99,9 @@ } | ||
for (let i = 0; i < rules.length; i++) { | ||
let match = rules[i].match.exec(textBefore); | ||
let tr = match && rules[i].handler(state, match, from - (match[0].length - text.length), to); | ||
let rule = rules[i], match = rule.match.exec(textBefore); | ||
let tr = match && rule.handler(state, match, from - (match[0].length - text.length), to); | ||
if (!tr) | ||
continue; | ||
view.dispatch(tr.setMeta(plugin, { transform: tr, from, to, text })); | ||
if (rule.undoable) | ||
tr.setMeta(plugin, { transform: tr, from, to, text }); | ||
view.dispatch(tr); | ||
return true; | ||
@@ -105,0 +108,0 @@ } |
{ | ||
"name": "prosemirror-inputrules", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Automatic transforms on text input for ProseMirror", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -12,2 +12,5 @@ import {Plugin, Transaction, EditorState, TextSelection, Command} from "prosemirror-state" | ||
/// @internal | ||
undoable: boolean | ||
// :: (RegExp, union<string, (state: EditorState, match: [string], start: number, end: number) → ?Transaction>) | ||
@@ -31,6 +34,13 @@ /// Create an input rule. The rule applies when the user typed | ||
readonly match: RegExp, | ||
handler: string | ((state: EditorState, match: RegExpMatchArray, start: number, end: number) => Transaction | null) | ||
handler: string | ((state: EditorState, match: RegExpMatchArray, start: number, end: number) => Transaction | null), | ||
options: { | ||
/// When set to false, | ||
/// [`undoInputRule`](#inputrules.undoInputRule) doesn't work on | ||
/// this rule. | ||
undoable?: boolean | ||
} = {} | ||
) { | ||
this.match = match | ||
this.handler = typeof handler == "string" ? stringHandler(handler) : handler | ||
this.undoable = options.undoable !== false | ||
} | ||
@@ -58,2 +68,4 @@ } | ||
type PluginState = {transform: Transaction, from: number, to: number, text: string} | null | ||
/// Create an input rules plugin. When enabled, it will cause text | ||
@@ -63,3 +75,3 @@ /// input that matches any of the given rules to trigger the rule's | ||
export function inputRules({rules}: {rules: readonly InputRule[]}) { | ||
let plugin: Plugin<{transform: Transaction, from: number, to: number, text: string} | null> = new Plugin({ | ||
let plugin: Plugin<PluginState> = new Plugin<PluginState>({ | ||
state: { | ||
@@ -100,6 +112,7 @@ init() { return null }, | ||
for (let i = 0; i < rules.length; i++) { | ||
let match = rules[i].match.exec(textBefore) | ||
let tr = match && rules[i].handler(state, match, from - (match[0].length - text.length), to) | ||
let rule = rules[i], match = rule.match.exec(textBefore) | ||
let tr = match && rule.handler(state, match, from - (match[0].length - text.length), to) | ||
if (!tr) continue | ||
view.dispatch(tr.setMeta(plugin, {transform: tr, from, to, text})) | ||
if (rule.undoable) tr.setMeta(plugin, {transform: tr, from, to, text}) | ||
view.dispatch(tr) | ||
return true | ||
@@ -106,0 +119,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43673
679