index.d.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { EditorState, Transaction, Plugin, Command } from 'prosemirror-state';
  2. import { NodeType, Attrs, Node } from 'prosemirror-model';
  3. /**
  4. Input rules are regular expressions describing a piece of text
  5. that, when typed, causes something to happen. This might be
  6. changing two dashes into an emdash, wrapping a paragraph starting
  7. with `"> "` into a blockquote, or something entirely different.
  8. */
  9. declare class InputRule {
  10. inCode: boolean | "only";
  11. /**
  12. Create an input rule. The rule applies when the user typed
  13. something and the text directly in front of the cursor matches
  14. `match`, which should end with `$`.
  15. The `handler` can be a string, in which case the matched text, or
  16. the first matched group in the regexp, is replaced by that
  17. string.
  18. Or a it can be a function, which will be called with the match
  19. array produced by
  20. [`RegExp.exec`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec),
  21. as well as the start and end of the matched range, and which can
  22. return a [transaction](https://prosemirror.net/docs/ref/#state.Transaction) that describes the
  23. rule's effect, or null to indicate the input was not handled.
  24. */
  25. constructor(
  26. /**
  27. @internal
  28. */
  29. match: RegExp, handler: string | ((state: EditorState, match: RegExpMatchArray, start: number, end: number) => Transaction | null), options?: {
  30. /**
  31. When set to false,
  32. [`undoInputRule`](https://prosemirror.net/docs/ref/#inputrules.undoInputRule) doesn't work on
  33. this rule.
  34. */
  35. undoable?: boolean;
  36. /**
  37. By default, input rules will not apply inside nodes marked
  38. as [code](https://prosemirror.net/docs/ref/#model.NodeSpec.code). Set this to true to change
  39. that, or to `"only"` to _only_ match in such nodes.
  40. */
  41. inCode?: boolean | "only";
  42. });
  43. }
  44. type PluginState = {
  45. transform: Transaction;
  46. from: number;
  47. to: number;
  48. text: string;
  49. } | null;
  50. /**
  51. Create an input rules plugin. When enabled, it will cause text
  52. input that matches any of the given rules to trigger the rule's
  53. action.
  54. */
  55. declare function inputRules({ rules }: {
  56. rules: readonly InputRule[];
  57. }): Plugin<PluginState>;
  58. /**
  59. This is a command that will undo an input rule, if applying such a
  60. rule was the last thing that the user did.
  61. */
  62. declare const undoInputRule: Command;
  63. /**
  64. Converts double dashes to an emdash.
  65. */
  66. declare const emDash: InputRule;
  67. /**
  68. Converts three dots to an ellipsis character.
  69. */
  70. declare const ellipsis: InputRule;
  71. /**
  72. “Smart” opening double quotes.
  73. */
  74. declare const openDoubleQuote: InputRule;
  75. /**
  76. “Smart” closing double quotes.
  77. */
  78. declare const closeDoubleQuote: InputRule;
  79. /**
  80. “Smart” opening single quotes.
  81. */
  82. declare const openSingleQuote: InputRule;
  83. /**
  84. “Smart” closing single quotes.
  85. */
  86. declare const closeSingleQuote: InputRule;
  87. /**
  88. Smart-quote related input rules.
  89. */
  90. declare const smartQuotes: readonly InputRule[];
  91. /**
  92. Build an input rule for automatically wrapping a textblock when a
  93. given string is typed. The `regexp` argument is
  94. directly passed through to the `InputRule` constructor. You'll
  95. probably want the regexp to start with `^`, so that the pattern can
  96. only occur at the start of a textblock.
  97. `nodeType` is the type of node to wrap in. If it needs attributes,
  98. you can either pass them directly, or pass a function that will
  99. compute them from the regular expression match.
  100. By default, if there's a node with the same type above the newly
  101. wrapped node, the rule will try to [join](https://prosemirror.net/docs/ref/#transform.Transform.join) those
  102. two nodes. You can pass a join predicate, which takes a regular
  103. expression match and the node before the wrapped node, and can
  104. return a boolean to indicate whether a join should happen.
  105. */
  106. declare function wrappingInputRule(regexp: RegExp, nodeType: NodeType, getAttrs?: Attrs | null | ((matches: RegExpMatchArray) => Attrs | null), joinPredicate?: (match: RegExpMatchArray, node: Node) => boolean): InputRule;
  107. /**
  108. Build an input rule that changes the type of a textblock when the
  109. matched text is typed into it. You'll usually want to start your
  110. regexp with `^` to that it is only matched at the start of a
  111. textblock. The optional `getAttrs` parameter can be used to compute
  112. the new node's attributes, and works the same as in the
  113. `wrappingInputRule` function.
  114. */
  115. declare function textblockTypeInputRule(regexp: RegExp, nodeType: NodeType, getAttrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null)): InputRule;
  116. export { InputRule, closeDoubleQuote, closeSingleQuote, ellipsis, emDash, inputRules, openDoubleQuote, openSingleQuote, smartQuotes, textblockTypeInputRule, undoInputRule, wrappingInputRule };