@kerebron/editor 0.4.28 → 0.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/esm/CoreEditor.js +1 -0
  2. package/esm/CoreEditor.js.map +1 -0
  3. package/esm/DummyEditorView.js +1 -0
  4. package/esm/DummyEditorView.js.map +1 -0
  5. package/esm/Extension.js +1 -0
  6. package/esm/Extension.js.map +1 -0
  7. package/esm/ExtensionManager.js +1 -0
  8. package/esm/ExtensionManager.js.map +1 -0
  9. package/esm/Mark.js +1 -0
  10. package/esm/Mark.js.map +1 -0
  11. package/esm/Node.js +1 -0
  12. package/esm/Node.js.map +1 -0
  13. package/esm/commands/CommandManager.js +1 -0
  14. package/esm/commands/CommandManager.js.map +1 -0
  15. package/esm/commands/baseCommandFactories.js +1 -0
  16. package/esm/commands/baseCommandFactories.js.map +1 -0
  17. package/esm/commands/createChainableState.js +1 -0
  18. package/esm/commands/createChainableState.js.map +1 -0
  19. package/esm/commands/keyCommandFactories.js +1 -0
  20. package/esm/commands/keyCommandFactories.js.map +1 -0
  21. package/esm/commands/mod.js +1 -0
  22. package/esm/commands/mod.js.map +1 -0
  23. package/esm/commands/replaceCommandFactories.js +1 -0
  24. package/esm/commands/replaceCommandFactories.js.map +1 -0
  25. package/esm/commands/types.js +1 -0
  26. package/esm/commands/types.js.map +1 -0
  27. package/esm/mod.js +1 -0
  28. package/esm/mod.js.map +1 -0
  29. package/esm/nodeToTreeString.js +1 -0
  30. package/esm/nodeToTreeString.js.map +1 -0
  31. package/esm/plugins/TrackSelecionPlugin.js +1 -0
  32. package/esm/plugins/TrackSelecionPlugin.js.map +1 -0
  33. package/esm/plugins/input-rules/InputRulesPlugin.js +1 -0
  34. package/esm/plugins/input-rules/InputRulesPlugin.js.map +1 -0
  35. package/esm/plugins/input-rules/mod.js +1 -0
  36. package/esm/plugins/input-rules/mod.js.map +1 -0
  37. package/esm/plugins/input-rules/rulebuilders.js +1 -0
  38. package/esm/plugins/input-rules/rulebuilders.js.map +1 -0
  39. package/esm/plugins/keymap/keymap.js +1 -0
  40. package/esm/plugins/keymap/keymap.js.map +1 -0
  41. package/esm/plugins/keymap/mod.js +1 -0
  42. package/esm/plugins/keymap/mod.js.map +1 -0
  43. package/esm/plugins/keymap/w3c-keyname.js +1 -0
  44. package/esm/plugins/keymap/w3c-keyname.js.map +1 -0
  45. package/esm/search/mod.js +1 -0
  46. package/esm/search/mod.js.map +1 -0
  47. package/esm/search/query.js +1 -0
  48. package/esm/search/query.js.map +1 -0
  49. package/esm/search/search.js +1 -0
  50. package/esm/search/search.js.map +1 -0
  51. package/esm/types.js +1 -0
  52. package/esm/types.js.map +1 -0
  53. package/esm/ui.js +1 -0
  54. package/esm/ui.js.map +1 -0
  55. package/esm/utilities/SmartOutput.js +1 -0
  56. package/esm/utilities/SmartOutput.js.map +1 -0
  57. package/esm/utilities/createNodeFromContent.js +1 -0
  58. package/esm/utilities/createNodeFromContent.js.map +1 -0
  59. package/esm/utilities/getHtmlAttributes.js +1 -0
  60. package/esm/utilities/getHtmlAttributes.js.map +1 -0
  61. package/esm/utilities/getShadowRoot.js +1 -0
  62. package/esm/utilities/getShadowRoot.js.map +1 -0
  63. package/esm/utilities/mod.js +1 -0
  64. package/esm/utilities/mod.js.map +1 -0
  65. package/esm/utilities/toRawTextResult.js +1 -0
  66. package/esm/utilities/toRawTextResult.js.map +1 -0
  67. package/package.json +5 -1
  68. package/src/CoreEditor.ts +277 -0
  69. package/src/DummyEditorView.ts +403 -0
  70. package/src/Extension.ts +63 -0
  71. package/src/ExtensionManager.ts +328 -0
  72. package/src/Mark.ts +47 -0
  73. package/src/Node.ts +66 -0
  74. package/src/commands/CommandManager.ts +145 -0
  75. package/src/commands/baseCommandFactories.ts +1103 -0
  76. package/src/commands/createChainableState.ts +36 -0
  77. package/src/commands/keyCommandFactories.ts +26 -0
  78. package/src/commands/mod.ts +104 -0
  79. package/src/commands/replaceCommandFactories.ts +129 -0
  80. package/src/commands/types.ts +30 -0
  81. package/src/mod.ts +8 -0
  82. package/src/nodeToTreeString.ts +100 -0
  83. package/src/plugins/TrackSelecionPlugin.ts +27 -0
  84. package/src/plugins/input-rules/InputRulesPlugin.ts +242 -0
  85. package/src/plugins/input-rules/mod.ts +2 -0
  86. package/src/plugins/input-rules/rulebuilders.ts +88 -0
  87. package/src/plugins/keymap/keymap.ts +117 -0
  88. package/src/plugins/keymap/mod.ts +1 -0
  89. package/src/plugins/keymap/w3c-keyname.ts +123 -0
  90. package/src/search/mod.ts +2 -0
  91. package/src/search/query.ts +412 -0
  92. package/src/search/search.ts +284 -0
  93. package/src/types.ts +71 -0
  94. package/src/ui.ts +35 -0
  95. package/src/utilities/SmartOutput.ts +284 -0
  96. package/src/utilities/createNodeFromContent.ts +66 -0
  97. package/src/utilities/getHtmlAttributes.ts +68 -0
  98. package/src/utilities/getShadowRoot.ts +18 -0
  99. package/src/utilities/mod.ts +5 -0
  100. package/src/utilities/toRawTextResult.ts +27 -0
  101. package/assets/base.css +0 -114
  102. package/assets/content.css +0 -35
  103. package/assets/gapcursor.css +0 -25
  104. package/assets/index.css +0 -2
  105. package/assets/main.css +0 -8
  106. package/assets/mobile.css +0 -33
  107. package/assets/prosemirror.css +0 -20
  108. package/assets/search.css +0 -6
  109. package/assets/vars.css +0 -110
@@ -0,0 +1,36 @@
1
+ import { EditorState, Transaction } from 'prosemirror-state';
2
+
3
+ export function createChainableState(
4
+ transaction: Transaction,
5
+ state: EditorState,
6
+ ): EditorState {
7
+ let { selection } = transaction;
8
+ let { doc } = transaction;
9
+ let { storedMarks } = transaction;
10
+
11
+ return {
12
+ ...state,
13
+ apply: state.apply.bind(state),
14
+ applyTransaction: state.applyTransaction.bind(state),
15
+ plugins: state.plugins,
16
+ schema: state.schema,
17
+ reconfigure: state.reconfigure.bind(state),
18
+ toJSON: state.toJSON.bind(state),
19
+ get storedMarks() {
20
+ return storedMarks;
21
+ },
22
+ get selection() {
23
+ return selection;
24
+ },
25
+ get doc() {
26
+ return doc;
27
+ },
28
+ get tr() {
29
+ selection = transaction.selection;
30
+ doc = transaction.doc;
31
+ storedMarks = transaction.storedMarks;
32
+
33
+ return transaction;
34
+ },
35
+ };
36
+ }
@@ -0,0 +1,26 @@
1
+ import { CommandFactory, firstCommand } from './mod.js';
2
+ import { baseCommandFactories } from './baseCommandFactories.js';
3
+
4
+ const backspace = firstCommand(
5
+ baseCommandFactories.undoInputRule(),
6
+ baseCommandFactories.deleteSelection(),
7
+ baseCommandFactories.joinBackward(),
8
+ baseCommandFactories.selectNodeBackward(),
9
+ );
10
+ const del = firstCommand(
11
+ baseCommandFactories.deleteSelection(),
12
+ baseCommandFactories.joinForward(),
13
+ baseCommandFactories.selectNodeForward(),
14
+ );
15
+ const enter = firstCommand(
16
+ baseCommandFactories.newlineInCode(),
17
+ baseCommandFactories.createParagraphNear(),
18
+ baseCommandFactories.liftEmptyBlock(),
19
+ baseCommandFactories.splitBlock(),
20
+ );
21
+
22
+ export const keyCommandFactories: Record<string, CommandFactory> = {
23
+ backspace: () => backspace,
24
+ del: () => del,
25
+ enter: () => enter,
26
+ };
@@ -0,0 +1,104 @@
1
+ import type { Transaction } from 'prosemirror-state';
2
+ import type { Node } from 'prosemirror-model';
3
+ import type { Command } from './types.js';
4
+ import { canJoin } from 'prosemirror-transform';
5
+
6
+ export * from './types.js';
7
+
8
+ /// Combine a number of command functions into a single function (which
9
+ /// calls them one by one until one returns true).
10
+ export function firstCommand(...commands: readonly Command[]): Command {
11
+ const cmd: Command = function (state, dispatch, view) {
12
+ for (let i = 0; i < commands.length; i++) {
13
+ if (commands[i](state, dispatch, view)) {
14
+ const cmd: Command = commands[i];
15
+ if (cmd.displayName !== 'first') {
16
+ console.debug(
17
+ 'firstCommand: ',
18
+ commands[i].displayName || commands[i],
19
+ );
20
+ }
21
+ return true;
22
+ }
23
+ }
24
+ return false;
25
+ };
26
+ cmd.displayName = 'first';
27
+
28
+ return cmd;
29
+ }
30
+
31
+ export function alternativeCommands(...commands: readonly Command[]): Command {
32
+ return function (state, dispatch, view) {
33
+ for (let i = 0; i < commands.length; i++) {
34
+ if (commands[i](state, dispatch, view)) return true;
35
+ }
36
+ return false;
37
+ };
38
+ }
39
+
40
+ function wrapDispatchForJoin(
41
+ dispatch: (tr: Transaction) => void,
42
+ isJoinable: (a: Node, b: Node) => boolean,
43
+ ) {
44
+ return (tr: Transaction) => {
45
+ if (!tr.isGeneric) return dispatch(tr);
46
+
47
+ let ranges: number[] = [];
48
+ for (let i = 0; i < tr.mapping.maps.length; i++) {
49
+ let map = tr.mapping.maps[i];
50
+ for (let j = 0; j < ranges.length; j++) {
51
+ ranges[j] = map.map(ranges[j]);
52
+ }
53
+ map.forEach((_s, _e, from, to) => ranges.push(from, to));
54
+ }
55
+
56
+ // Figure out which joinable points exist inside those ranges,
57
+ // by checking all node boundaries in their parent nodes.
58
+ let joinable = [];
59
+ for (let i = 0; i < ranges.length; i += 2) {
60
+ let from = ranges[i], to = ranges[i + 1];
61
+ let $from = tr.doc.resolve(from),
62
+ depth = $from.sharedDepth(to),
63
+ parent = $from.node(depth);
64
+ for (
65
+ let index = $from.indexAfter(depth), pos = $from.after(depth + 1);
66
+ pos <= to;
67
+ ++index
68
+ ) {
69
+ let after = parent.maybeChild(index);
70
+ if (!after) break;
71
+ if (index && joinable.indexOf(pos) == -1) {
72
+ let before = parent.child(index - 1);
73
+ if (before.type == after.type && isJoinable(before, after)) {
74
+ joinable.push(pos);
75
+ }
76
+ }
77
+ pos += after.nodeSize;
78
+ }
79
+ }
80
+ // Join the joinable points
81
+ joinable.sort((a, b) => a - b);
82
+ for (let i = joinable.length - 1; i >= 0; i--) {
83
+ if (canJoin(tr.doc, joinable[i])) tr.join(joinable[i]);
84
+ }
85
+ dispatch(tr);
86
+ };
87
+ }
88
+
89
+ /// Wrap a command so that, when it produces a transform that causes
90
+ /// two joinable nodes to end up next to each other, those are joined.
91
+ /// Nodes are considered joinable when they are of the same type and
92
+ /// when the `isJoinable` predicate returns true for them or, if an
93
+ /// array of strings was passed, if their node type name is in that
94
+ /// array.
95
+ export function autoJoin(
96
+ command: Command,
97
+ isJoinable: ((before: Node, after: Node) => boolean) | readonly string[],
98
+ ): Command {
99
+ let canJoin = Array.isArray(isJoinable)
100
+ ? (node: Node) => isJoinable.indexOf(node.type.name) > -1
101
+ : isJoinable as (a: Node, b: Node) => boolean;
102
+ return (state, dispatch, view) =>
103
+ command(state, dispatch && wrapDispatchForJoin(dispatch, canJoin), view);
104
+ }
@@ -0,0 +1,129 @@
1
+ import { Fragment, Node, Slice } from 'prosemirror-model';
2
+
3
+ import type { Command, CommandFactory } from './mod.js';
4
+ import type { TextRange } from '../mod.js';
5
+ import { EditorState } from 'prosemirror-state';
6
+
7
+ const replaceRangeText = (range: TextRange, text: string): Command => {
8
+ return (state, dispatch) => {
9
+ const tr = state.tr;
10
+
11
+ const from = tr.mapping.map(range.from);
12
+ const to = tr.mapping.map(range.to);
13
+
14
+ const frag = Fragment.from(state.schema.text(text));
15
+ const insert = new Slice(frag, 0, 0);
16
+
17
+ tr.replace(from, to, insert);
18
+
19
+ if (dispatch) {
20
+ dispatch(tr);
21
+ }
22
+
23
+ return true;
24
+ };
25
+ };
26
+
27
+ const replaceRangeSlice = (range: TextRange, insert: Slice): Command => {
28
+ return (state, dispatch) => {
29
+ const tr = state.tr;
30
+
31
+ const from = tr.mapping.map(range.from);
32
+ const to = tr.mapping.map(range.to);
33
+
34
+ tr.replace(from, to, insert);
35
+
36
+ if (dispatch) {
37
+ dispatch(tr);
38
+ }
39
+
40
+ return true;
41
+ };
42
+ };
43
+
44
+ function getTopMostBlockStartForOffset(state: EditorState, offset: number) {
45
+ const { doc } = state;
46
+ const $pos = doc.resolve(offset);
47
+
48
+ const blockPos = $pos.before($pos.depth);
49
+
50
+ if ($pos.depth === 1) return blockPos;
51
+ return $pos.start(1);
52
+ }
53
+
54
+ function getTopMostBlockEndForOffset(state: EditorState, offset: number) {
55
+ const { doc } = state;
56
+
57
+ const $pos = doc.resolve(offset);
58
+ return $pos.end(1);
59
+ }
60
+
61
+ function getAdjacentTopBlock(state: EditorState, offset: number) {
62
+ const { doc } = state;
63
+
64
+ if (offset < 0 || offset > doc.content.size) {
65
+ throw new RangeError('Offset is outside the document');
66
+ }
67
+
68
+ const $pos = doc.resolve(offset);
69
+ const atBlockStart = $pos.pos === $pos.start($pos.depth);
70
+
71
+ if (atBlockStart) {
72
+ return getTopMostBlockStartForOffset(state, offset);
73
+ } else {
74
+ return getTopMostBlockEndForOffset(state, offset);
75
+ }
76
+ }
77
+
78
+ const insertBlockBefore = (pos: number, insert: Node): Command => {
79
+ return (state, dispatch) => {
80
+ const tr = state.tr;
81
+
82
+ const blockPos = getTopMostBlockStartForOffset(state, tr.mapping.map(pos));
83
+ tr.insert(blockPos, insert);
84
+
85
+ if (dispatch) {
86
+ dispatch(tr);
87
+ }
88
+
89
+ return true;
90
+ };
91
+ };
92
+
93
+ const insertBlockAfter = (pos: number, insert: Node): Command => {
94
+ return (state, dispatch) => {
95
+ const tr = state.tr;
96
+
97
+ const blockPos = getTopMostBlockEndForOffset(state, tr.mapping.map(pos));
98
+ tr.insert(blockPos, insert);
99
+
100
+ if (dispatch) {
101
+ dispatch(tr);
102
+ }
103
+
104
+ return true;
105
+ };
106
+ };
107
+
108
+ const insertBlockSmart = (pos: number, insert: Node): Command => {
109
+ return (state, dispatch) => {
110
+ const tr = state.tr;
111
+
112
+ const blockPos = getAdjacentTopBlock(state, tr.mapping.map(pos));
113
+ tr.insert(blockPos, insert);
114
+
115
+ if (dispatch) {
116
+ dispatch(tr);
117
+ }
118
+
119
+ return true;
120
+ };
121
+ };
122
+
123
+ export const replaceCommandFactories: Record<string, CommandFactory> = {
124
+ replaceRangeText,
125
+ replaceRangeSlice,
126
+ insertBlockBefore,
127
+ insertBlockAfter,
128
+ insertBlockSmart,
129
+ };
@@ -0,0 +1,30 @@
1
+ import type { Command as PmCommand } from 'prosemirror-state';
2
+
3
+ interface Command extends PmCommand {
4
+ displayName?: string;
5
+ description?: string;
6
+ }
7
+
8
+ export type { Command };
9
+
10
+ export type CommandFactory = (...args: any[]) => Command;
11
+
12
+ export interface Commands {
13
+ [name: string]: Command;
14
+ }
15
+
16
+ export interface CommandFactories {
17
+ [name: string]: CommandFactory;
18
+ }
19
+
20
+ export type CommandShortcuts = {
21
+ [name: string]: string;
22
+ };
23
+
24
+ export type ChainedCommands =
25
+ & {
26
+ [Item in keyof Commands]: (...args: unknown[]) => ChainedCommands;
27
+ }
28
+ & {
29
+ run: () => boolean;
30
+ };
package/src/mod.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './CoreEditor.js';
2
+ export * from './Extension.js';
3
+ export * from './Mark.js';
4
+ export * from './Node.js';
5
+ export * from './nodeToTreeString.js';
6
+ export * from './types.js';
7
+ export * from './utilities/SmartOutput.js';
8
+ export * from './ui.js';
@@ -0,0 +1,100 @@
1
+ import type { Node } from 'prosemirror-model';
2
+ import { SmartOutput } from './utilities/SmartOutput.js';
3
+
4
+ function trimText(str: string, maxLen = 20): string {
5
+ str = str.replaceAll('\n', '\\n');
6
+
7
+ if (str.length <= maxLen) {
8
+ return str;
9
+ }
10
+
11
+ return str.slice(0, maxLen) + '...';
12
+ }
13
+
14
+ export interface NodeAndPos {
15
+ node: Node;
16
+ pos: number;
17
+ }
18
+
19
+ export function nodeToTreeStringOutput(
20
+ output: SmartOutput<NodeAndPos>,
21
+ node: Node | Node[] | readonly Node[],
22
+ level = 0,
23
+ currentPos = 0,
24
+ ) {
25
+ let delim = '';
26
+ for (let i = 0; i < level; i++) {
27
+ delim += ' ';
28
+ }
29
+
30
+ if (Array.isArray(node)) {
31
+ for (const child of node) {
32
+ output.log(delim, { node: child, pos: currentPos });
33
+ nodeToTreeStringOutput(output, child, level + 1, currentPos);
34
+ // .replace(/\s+$/gm, '') +
35
+ // '\n'
36
+ }
37
+ return;
38
+ }
39
+
40
+ // https://prosemirror.net/docs/guide/#doc.indexing
41
+
42
+ let line = '';
43
+ // if (node.type) {
44
+ if ('type' in node) {
45
+ line += ` - [${node.type.name}] `;
46
+ // } else {
47
+ // line += ` - `;
48
+ // }
49
+ line += `pos: ${currentPos}, `;
50
+ line += `nodeSize: ${node.nodeSize}, `; // isLeaf ? 1 : 2 + this.content.size
51
+ line += `epos: ${currentPos + node.nodeSize}, `; // isLeaf ? 1 : 2 + this.content.size
52
+ if (node.content) {
53
+ line += `fragment.size: ${node.content.size}, `;
54
+ }
55
+
56
+ output.log((delim + line) + '\n', { node, pos: currentPos });
57
+
58
+ let marksLine = '';
59
+ if (node.marks) {
60
+ for (const mark of node.marks) {
61
+ marksLine += `(${mark.type.name}), `;
62
+ }
63
+ }
64
+
65
+ if (marksLine) {
66
+ output.log((delim + ' ' + marksLine) + '\n', {
67
+ node,
68
+ pos: currentPos,
69
+ });
70
+ }
71
+
72
+ if (node.text) {
73
+ output.log((delim + ' "' + trimText(node.text) + '"') + '\n', {
74
+ node,
75
+ pos: currentPos,
76
+ });
77
+ }
78
+ }
79
+
80
+ node.forEach((child, offset) => {
81
+ // output +=
82
+ nodeToTreeStringOutput(output, child, level + 1, currentPos + offset + 1);
83
+ // .replace(
84
+ // /\s+$/gm,
85
+ // '',
86
+ // ) + '\n'; // + (node.isLeaf ? 1 : 2)
87
+ });
88
+
89
+ return output;
90
+ }
91
+
92
+ export function nodeToTreeString(node: Node | Node[] | readonly Node[]) {
93
+ const output = new SmartOutput<NodeAndPos>();
94
+ nodeToTreeStringOutput(output, node);
95
+ return output.toString();
96
+ }
97
+
98
+ export function debugNode(node: Node | Node[]) {
99
+ console.debug(nodeToTreeString(node));
100
+ }
@@ -0,0 +1,27 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import type { CoreEditor } from '../mod.js';
3
+
4
+ export class TrackSelecionPlugin extends Plugin {
5
+ constructor(editor: CoreEditor) {
6
+ super({
7
+ view(editorView) {
8
+ return {
9
+ update(view, prevState) {
10
+ const state = view.state;
11
+ const prevSelection = prevState.selection;
12
+ const selection = state.selection;
13
+
14
+ if (!prevSelection.eq(selection)) {
15
+ const event = new CustomEvent('selection', {
16
+ detail: {
17
+ selection,
18
+ },
19
+ });
20
+ editor.dispatchEvent(event);
21
+ }
22
+ },
23
+ };
24
+ },
25
+ });
26
+ }
27
+ }
@@ -0,0 +1,242 @@
1
+ import { Fragment, Node as ProseMirrorNode } from 'prosemirror-model';
2
+ import {
3
+ EditorState,
4
+ Plugin,
5
+ TextSelection,
6
+ Transaction,
7
+ } from 'prosemirror-state';
8
+ import { EditorView } from 'prosemirror-view';
9
+
10
+ import type { Command, CommandFactory } from '../../commands/types.js';
11
+
12
+ const MAX_MATCH = 500;
13
+
14
+ type PluginState = {
15
+ transform: Transaction;
16
+ from: number;
17
+ to: number;
18
+ text: string;
19
+ } | null;
20
+
21
+ /// Input rules are regular expressions describing a piece of text
22
+ /// that, when typed, causes something to happen. This might be
23
+ /// changing two dashes into an emdash, wrapping a paragraph starting
24
+ /// with `"> "` into a blockquote, or something entirely different.
25
+ export class InputRule {
26
+ /// @internal
27
+ handler: (
28
+ state: EditorState,
29
+ match: RegExpMatchArray,
30
+ start: number,
31
+ end: number,
32
+ ) => Transaction | null;
33
+
34
+ /// @internal
35
+ undoable: boolean;
36
+ inCode: boolean | 'only';
37
+
38
+ // :: (RegExp, union<string, (state: EditorState, match: [string], start: number, end: number) → ?Transaction>)
39
+ /// Create an input rule. The rule applies when the user typed
40
+ /// something and the text directly in front of the cursor matches
41
+ /// `match`, which should end with `$`.
42
+ ///
43
+ /// The `handler` can be a string, in which case the matched text, or
44
+ /// the first matched group in the regexp, is replaced by that
45
+ /// string.
46
+ ///
47
+ /// Or a it can be a function, which will be called with the match
48
+ /// array produced by
49
+ /// [`RegExp.exec`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec),
50
+ /// as well as the start and end of the matched range, and which can
51
+ /// return a [transaction](#state.Transaction) that describes the
52
+ /// rule's effect, or null to indicate the input was not handled.
53
+ constructor(
54
+ /// @internal
55
+ readonly match: RegExp,
56
+ handler:
57
+ | string
58
+ | ((
59
+ state: EditorState,
60
+ match: RegExpMatchArray,
61
+ start: number,
62
+ end: number,
63
+ ) => Transaction | null),
64
+ options: {
65
+ /// When set to false,
66
+ /// [`undoInputRule`](#inputrules.undoInputRule) doesn't work on
67
+ /// this rule.
68
+ undoable?: boolean;
69
+ /// By default, input rules will not apply inside nodes marked
70
+ /// as [code](#model.NodeSpec.code). Set this to true to change
71
+ /// that, or to `"only"` to _only_ match in such nodes.
72
+ inCode?: boolean | 'only';
73
+ } = {},
74
+ ) {
75
+ this.match = match;
76
+ this.handler = typeof handler == 'string'
77
+ ? stringHandler(handler)
78
+ : handler;
79
+ this.undoable = options.undoable !== false;
80
+ this.inCode = options.inCode || false;
81
+ }
82
+ }
83
+
84
+ function stringHandler(string: string) {
85
+ return function (
86
+ state: EditorState,
87
+ match: RegExpMatchArray,
88
+ start: number,
89
+ end: number,
90
+ ) {
91
+ let insert = string;
92
+ if (match[1]) {
93
+ const offset = match[0].lastIndexOf(match[1]);
94
+ insert += match[0].slice(offset + match[1].length);
95
+ start += offset;
96
+ const cutOff = start - end;
97
+ if (cutOff > 0) {
98
+ insert = match[0].slice(offset - cutOff, offset) + insert;
99
+ start = end;
100
+ }
101
+ }
102
+ return state.tr.insertText(insert, start, end);
103
+ };
104
+ }
105
+
106
+ /// Create an input rules plugin. When enabled, it will cause text
107
+ /// input that matches any of the given rules to trigger the rule's
108
+ /// action.
109
+ export class InputRulesPlugin extends Plugin<PluginState> {
110
+ constructor(rules: readonly InputRule[]) {
111
+ super({
112
+ state: {
113
+ init() {
114
+ return null;
115
+ },
116
+ apply(this: InputRulesPlugin, tr, prev) {
117
+ const stored = tr.getMeta(this);
118
+ if (stored) {
119
+ return stored;
120
+ }
121
+ return tr.selectionSet || tr.docChanged ? null : prev;
122
+ },
123
+ },
124
+
125
+ props: {
126
+ handleTextInput(view, from, to, text) {
127
+ const cmd = runInputRules(from, to, text, rules);
128
+ const dispatch = (tr: Transaction) => {
129
+ view.dispatch(tr);
130
+ };
131
+ return cmd(view.state, dispatch, view);
132
+ // return run(view, from, to, text, rules, this);
133
+ },
134
+ handleDOMEvents: {
135
+ compositionend: (view) => {
136
+ setTimeout(() => {
137
+ const { $cursor } = view.state.selection as TextSelection;
138
+ if ($cursor) {
139
+ const cmd = runInputRules($cursor.pos, $cursor.pos, '', rules);
140
+ const dispatch = (tr: Transaction) => {
141
+ view.dispatch(tr);
142
+ };
143
+ return cmd(view.state, dispatch, view);
144
+ // run(view, $cursor.pos, $cursor.pos, '', rules, this);
145
+ }
146
+ });
147
+ },
148
+ },
149
+ },
150
+
151
+ isInputRules: true,
152
+ });
153
+ }
154
+ }
155
+
156
+ export const runInputRules: CommandFactory = (
157
+ from: number,
158
+ to: number,
159
+ text: string,
160
+ rules: readonly InputRule[],
161
+ ) => {
162
+ const cmd: Command = (
163
+ state: EditorState,
164
+ dispatch?: (tr: Transaction) => void,
165
+ view?: EditorView,
166
+ ) => {
167
+ const plugins = state.plugins;
168
+ const plugin: InputRulesPlugin | undefined = plugins.find((plugin) =>
169
+ (plugin.spec as any).isInputRules
170
+ );
171
+ if (!plugin) {
172
+ return false;
173
+ }
174
+
175
+ if (view?.composing) return false;
176
+ const $from = state.doc.resolve(from);
177
+ const textBefore = $from.parent.textBetween(
178
+ Math.max(0, $from.parentOffset - MAX_MATCH),
179
+ $from.parentOffset,
180
+ null,
181
+ '\ufffc',
182
+ ) + text;
183
+ for (let i = 0; i < rules.length; i++) {
184
+ const rule = rules[i];
185
+ if ($from.parent.type.spec.code) {
186
+ if (!rule.inCode) continue;
187
+ } else if (rule.inCode === 'only') {
188
+ continue;
189
+ }
190
+ const match = rule.match.exec(textBefore);
191
+ const tr = match &&
192
+ rule.handler(state, match, from - (match[0].length - text.length), to);
193
+ if (!tr) continue;
194
+ if (rule.undoable) {
195
+ tr.setMeta(plugin, { transform: tr, from, to, text });
196
+ }
197
+ view?.dispatch(tr);
198
+ return true;
199
+ }
200
+ return false;
201
+ };
202
+ cmd.displayName = 'runInputRules';
203
+
204
+ return cmd;
205
+ };
206
+
207
+ /// This is a command that will undo an input rule, if applying such a
208
+ /// rule was the last thing that the user did.
209
+ export const undoInputRuleCommand: Command = (state, dispatch) => {
210
+ const plugins = state.plugins;
211
+ const plugin: InputRulesPlugin | undefined = plugins.find((plugin) =>
212
+ (plugin.spec as any).isInputRules
213
+ );
214
+ if (!plugin) {
215
+ return false;
216
+ }
217
+
218
+ const undoable: PluginState | undefined = plugin.getState(state);
219
+ if (!undoable) {
220
+ return false;
221
+ }
222
+
223
+ if (dispatch) {
224
+ const tr = state.tr;
225
+ const toUndo = undoable.transform;
226
+ for (let j = toUndo.steps.length - 1; j >= 0; j--) {
227
+ tr.step(toUndo.steps[j].invert(toUndo.docs[j]));
228
+ }
229
+ if (undoable.text) {
230
+ const marks = tr.doc.resolve(undoable.from).marks();
231
+ tr.replaceWith(
232
+ undoable.from,
233
+ undoable.to,
234
+ state.schema.text(undoable.text, marks),
235
+ );
236
+ } else {
237
+ tr.delete(undoable.from, undoable.to);
238
+ }
239
+ dispatch(tr);
240
+ }
241
+ return true;
242
+ };