@prosekit/extensions 0.1.5 → 0.2.0
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.
@@ -3,7 +3,6 @@ import { DedentListOptions } from 'prosemirror-flat-list';
|
|
3
3
|
import { EditorState } from '@prosekit/pm/state';
|
4
4
|
import { Extension } from '@prosekit/core';
|
5
5
|
import { ExtensionTyping } from '@prosekit/core';
|
6
|
-
import type { HLJSApi } from 'highlight.js';
|
7
6
|
import { IndentListOptions } from 'prosemirror-flat-list';
|
8
7
|
import { ListAttributes } from 'prosemirror-flat-list';
|
9
8
|
import { NodeRange } from 'prosemirror-model';
|
@@ -34,6 +33,11 @@ declare class AutocompleteRule {
|
|
34
33
|
export { AutocompleteRule }
|
35
34
|
export { AutocompleteRule as AutocompleteRule_alias_1 }
|
36
35
|
|
36
|
+
/**
|
37
|
+
* The attributes for the `codeBlock` node.
|
38
|
+
*
|
39
|
+
* @public
|
40
|
+
*/
|
37
41
|
declare interface CodeBlockAttrs {
|
38
42
|
language?: string;
|
39
43
|
}
|
@@ -44,8 +48,6 @@ export declare function createAutocompletePlugin({ getRules, }: {
|
|
44
48
|
getRules: () => AutocompleteRule[];
|
45
49
|
}): Plugin_2;
|
46
50
|
|
47
|
-
export declare function createPredictionPlugin(options: SuggestionOptions): Plugin_2;
|
48
|
-
|
49
51
|
export declare const default_alias: Options | Options[] | ((overrideOptions: Options) => Options | Options[] | Promise<Options | Options[]>);
|
50
52
|
|
51
53
|
export declare const default_alias_1: {
|
@@ -58,10 +60,6 @@ export declare function defaultCanMatch({ state }: {
|
|
58
60
|
state: EditorState;
|
59
61
|
}): boolean;
|
60
62
|
|
61
|
-
export declare function defaultIsValid({ state }: {
|
62
|
-
state: EditorState;
|
63
|
-
}): boolean;
|
64
|
-
|
65
63
|
export declare function defineAutocomplete(rule: AutocompleteRule): Extension;
|
66
64
|
|
67
65
|
/**
|
@@ -108,46 +106,67 @@ toggleCode: [];
|
|
108
106
|
}>;
|
109
107
|
|
110
108
|
/**
|
109
|
+
* Adds `codeBlock` nodes to the editor. This includes the following extensions:
|
110
|
+
*
|
111
|
+
* - {@link defineCodeBlockSpec}
|
112
|
+
* - {@link defineCodeBlockInputRule}
|
113
|
+
* - {@link defineCodeBlockCommands}.
|
114
|
+
*
|
111
115
|
* @public
|
112
116
|
*/
|
113
|
-
export declare function defineCodeBlock(
|
114
|
-
/**
|
115
|
-
* @deprecated Use `highlight` instead.
|
116
|
-
*/
|
117
|
-
hljs?: HLJSApi;
|
118
|
-
/**
|
119
|
-
* A parser for the `prosemirror-highlight` package to use for syntax highlighting.
|
120
|
-
*/
|
121
|
-
parser?: HighlightParser;
|
122
|
-
}): Extension< {
|
117
|
+
export declare function defineCodeBlock(): Extension< {
|
123
118
|
NODES: "codeBlock";
|
124
119
|
COMMAND_ARGS: {
|
125
120
|
setCodeBlockLanguage: [language: string];
|
126
121
|
};
|
127
122
|
}>;
|
128
123
|
|
129
|
-
|
124
|
+
/**
|
125
|
+
* Adds commands for working with `codeBlock` nodes.
|
126
|
+
*
|
127
|
+
* @public
|
128
|
+
*/
|
129
|
+
declare function defineCodeBlockCommands(): Extension< {
|
130
130
|
COMMAND_ARGS: {
|
131
131
|
setCodeBlockLanguage: [language: string];
|
132
132
|
};
|
133
133
|
}>;
|
134
|
-
|
135
|
-
export
|
136
|
-
parser: Parser;
|
137
|
-
}): Extension<ExtensionTyping<string, string, CommandArgs>>;
|
134
|
+
export { defineCodeBlockCommands }
|
135
|
+
export { defineCodeBlockCommands as defineCodeBlockCommands_alias_1 }
|
138
136
|
|
139
137
|
/**
|
140
|
-
*
|
138
|
+
* Adds syntax highlighting to code blocks. This function requires a `Parser`
|
139
|
+
* instance from the `prosemirror-highlight` package. See the
|
140
|
+
* [documentation](https://github.com/ocavue/prosemirror-highlight) for more
|
141
|
+
* information.
|
142
|
+
*
|
143
|
+
* @public
|
141
144
|
*/
|
142
|
-
|
143
|
-
|
144
|
-
}): Extension
|
145
|
+
declare function defineCodeBlockHighlight({ parser, }: {
|
146
|
+
parser: HighlightParser;
|
147
|
+
}): Extension;
|
148
|
+
export { defineCodeBlockHighlight }
|
149
|
+
export { defineCodeBlockHighlight as defineCodeBlockHighlight_alias_1 }
|
145
150
|
|
146
|
-
|
151
|
+
/**
|
152
|
+
* Adds input rules for `codeBlock` nodes.
|
153
|
+
*
|
154
|
+
* @public
|
155
|
+
*/
|
156
|
+
declare function defineCodeBlockInputRule(): Extension<ExtensionTyping<string, string, CommandArgs>>;
|
157
|
+
export { defineCodeBlockInputRule }
|
158
|
+
export { defineCodeBlockInputRule as defineCodeBlockInputRule_alias_1 }
|
147
159
|
|
148
|
-
|
160
|
+
/**
|
161
|
+
* Defines the `codeBlock` node spec.
|
162
|
+
*
|
163
|
+
* @public
|
164
|
+
*/
|
165
|
+
declare function defineCodeBlockSpec(): Extension< {
|
149
166
|
NODES: "codeBlock";
|
150
167
|
}>;
|
168
|
+
export { defineCodeBlockSpec }
|
169
|
+
export { defineCodeBlockSpec as defineCodeBlockSpec_alias_1 }
|
151
170
|
|
152
171
|
export declare function defineCodeCommands(): Extension< {
|
153
172
|
COMMAND_ARGS: {
|
@@ -359,11 +378,6 @@ export declare function defineStrikeSpec(): Extension< {
|
|
359
378
|
MARKS: "strike";
|
360
379
|
}>;
|
361
380
|
|
362
|
-
/**
|
363
|
-
* @deprecated Use `defineAutocomplete` instead.
|
364
|
-
*/
|
365
|
-
export declare function defineSuggestion(options: SuggestionOptions): Extension<ExtensionTyping<string, string, CommandArgs>>;
|
366
|
-
|
367
381
|
/**
|
368
382
|
* @public
|
369
383
|
*/
|
@@ -399,7 +413,9 @@ export declare interface HeadingAttrs {
|
|
399
413
|
*
|
400
414
|
* An alias for the `Parser` type from the `prosemirror-highlight` package.
|
401
415
|
*/
|
402
|
-
|
416
|
+
declare type HighlightParser = Parser;
|
417
|
+
export { HighlightParser }
|
418
|
+
export { HighlightParser as HighlightParser_alias_1 }
|
403
419
|
|
404
420
|
export declare interface ImageAttrs {
|
405
421
|
src?: string | null;
|
@@ -423,18 +439,6 @@ declare type MatchHandler = (options: {
|
|
423
439
|
export { MatchHandler }
|
424
440
|
export { MatchHandler as MatchHandler_alias_1 }
|
425
441
|
|
426
|
-
/**
|
427
|
-
* @returns Return a Transaction object if you want to append a transaction to current state (using )
|
428
|
-
*/
|
429
|
-
declare type MatchHandler_2 = (options: {
|
430
|
-
rule: PredictionRule;
|
431
|
-
match: RegExpMatchArray;
|
432
|
-
matchAfter: RegExpMatchArray | null;
|
433
|
-
state: EditorState;
|
434
|
-
dismiss: VoidFunction;
|
435
|
-
deleteMatch: VoidFunction;
|
436
|
-
}) => void;
|
437
|
-
|
438
442
|
export declare interface MentionAttrs {
|
439
443
|
id: string;
|
440
444
|
kind: string;
|
@@ -471,31 +475,6 @@ export declare interface PredictionPluginState {
|
|
471
475
|
} | null;
|
472
476
|
}
|
473
477
|
|
474
|
-
declare interface PredictionRule {
|
475
|
-
match: RegExp;
|
476
|
-
matchAfter?: RegExp;
|
477
|
-
}
|
478
|
-
export { PredictionRule }
|
479
|
-
export { PredictionRule as PredictionRule_alias_1 }
|
480
|
-
|
481
478
|
export declare function setTrMeta(tr: Transaction, meta: PredictionPluginState): Transaction;
|
482
479
|
|
483
|
-
declare interface SuggestionOptions {
|
484
|
-
rules: PredictionRule[];
|
485
|
-
onMatch: MatchHandler_2;
|
486
|
-
onDeactivate: VoidFunction;
|
487
|
-
/**
|
488
|
-
* You can pass this function if you want to skip the matching in some cases.
|
489
|
-
* By default, the plugin will only run the matching if the current selection
|
490
|
-
* is empty, and the selection is not inside a
|
491
|
-
* [code](https://prosemirror.net/docs/ref/#model.NodeSpec.code) node nor
|
492
|
-
* inside a mark with the name as `code`.
|
493
|
-
*/
|
494
|
-
isValid?: (options: {
|
495
|
-
state: EditorState;
|
496
|
-
}) => boolean;
|
497
|
-
}
|
498
|
-
export { SuggestionOptions }
|
499
|
-
export { SuggestionOptions as SuggestionOptions_alias_1 }
|
500
|
-
|
501
480
|
export { }
|
@@ -1,6 +1,7 @@
|
|
1
|
-
export { defineCodeBlockSpec } from './_tsup-dts-rollup';
|
2
|
-
export { defineCodeBlockInputRule } from './_tsup-dts-rollup';
|
3
|
-
export { defineCodeBlockCommands } from './_tsup-dts-rollup';
|
4
1
|
export { defineCodeBlock } from './_tsup-dts-rollup';
|
2
|
+
export { defineCodeBlockCommands_alias_1 as defineCodeBlockCommands } from './_tsup-dts-rollup';
|
3
|
+
export { defineCodeBlockHighlight_alias_1 as defineCodeBlockHighlight } from './_tsup-dts-rollup';
|
4
|
+
export { defineCodeBlockInputRule_alias_1 as defineCodeBlockInputRule } from './_tsup-dts-rollup';
|
5
|
+
export { defineCodeBlockSpec_alias_1 as defineCodeBlockSpec } from './_tsup-dts-rollup';
|
5
6
|
export { CodeBlockAttrs_alias_1 as CodeBlockAttrs } from './_tsup-dts-rollup';
|
6
|
-
export { HighlightParser } from './_tsup-dts-rollup';
|
7
|
+
export { HighlightParser_alias_1 as HighlightParser } from './_tsup-dts-rollup';
|
@@ -1,64 +1,50 @@
|
|
1
1
|
// src/code-block/index.ts
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
2
|
+
import { union } from "@prosekit/core";
|
3
|
+
|
4
|
+
// src/code-block/code-block-commands.ts
|
5
|
+
import { defineCommands } from "@prosekit/core";
|
6
|
+
function defineCodeBlockCommands() {
|
7
|
+
return defineCommands({
|
8
|
+
setCodeBlockLanguage: (language) => (state, dispatch) => {
|
9
|
+
const pos = state.selection.$from.before();
|
10
|
+
const codeBlock = state.doc.nodeAt(pos);
|
11
|
+
if (!codeBlock || codeBlock.type.name !== "codeBlock") {
|
12
|
+
return false;
|
13
|
+
}
|
14
|
+
const { tr } = state;
|
15
|
+
tr.setNodeMarkup(pos, void 0, { language });
|
16
|
+
dispatch == null ? void 0 : dispatch(tr);
|
17
|
+
return true;
|
18
|
+
}
|
19
|
+
});
|
20
|
+
}
|
10
21
|
|
11
22
|
// src/code-block/code-block-highlight.ts
|
12
23
|
import { definePlugin } from "@prosekit/core";
|
13
24
|
import { createHighlightPlugin } from "prosemirror-highlight";
|
14
|
-
function defineCodeBlockHighlight({
|
25
|
+
function defineCodeBlockHighlight({
|
26
|
+
parser
|
27
|
+
}) {
|
15
28
|
return definePlugin(
|
16
29
|
createHighlightPlugin({ parser, nodeTypes: ["codeBlock"] })
|
17
30
|
);
|
18
31
|
}
|
19
32
|
|
20
|
-
// src/code-block/code-block-
|
21
|
-
import {
|
22
|
-
import {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
init(_config, state) {
|
31
|
-
const decorations = hljs ? getHighlightDecorations(
|
32
|
-
state.doc,
|
33
|
-
hljs,
|
34
|
-
blockTypes,
|
35
|
-
languageExtractor
|
36
|
-
) : [];
|
37
|
-
return DecorationSet.create(state.doc, decorations);
|
38
|
-
},
|
39
|
-
apply(tr, set) {
|
40
|
-
if (!tr.docChanged) {
|
41
|
-
return set.map(tr.mapping, tr.doc);
|
42
|
-
}
|
43
|
-
const decorations = hljs ? getHighlightDecorations(tr.doc, hljs, blockTypes, languageExtractor) : [];
|
44
|
-
return DecorationSet.create(tr.doc, decorations);
|
45
|
-
}
|
46
|
-
},
|
47
|
-
props: {
|
48
|
-
decorations(state) {
|
49
|
-
return key.getState(state);
|
50
|
-
}
|
51
|
-
}
|
33
|
+
// src/code-block/code-block-input-rule.ts
|
34
|
+
import { defineInputRule, getNodeType } from "@prosekit/core";
|
35
|
+
import { textblockTypeInputRule } from "@prosekit/pm/inputrules";
|
36
|
+
function defineCodeBlockInputRule() {
|
37
|
+
return defineInputRule(({ schema }) => {
|
38
|
+
const nodeType = getNodeType(schema, "codeBlock");
|
39
|
+
const getAttrs = (match) => {
|
40
|
+
return { language: match[1] || "" };
|
41
|
+
};
|
42
|
+
return textblockTypeInputRule(/^```(\S*)\s$/, nodeType, getAttrs);
|
52
43
|
});
|
53
|
-
return definePlugin2(plugin);
|
54
|
-
}
|
55
|
-
var key = new PluginKey("prosekit-code-block-highlight");
|
56
|
-
var blockTypes = ["codeBlock"];
|
57
|
-
function languageExtractor(node) {
|
58
|
-
return node.attrs.language || "javascript";
|
59
44
|
}
|
60
45
|
|
61
|
-
// src/code-block/
|
46
|
+
// src/code-block/code-block-spec.ts
|
47
|
+
import { defineNodeSpec } from "@prosekit/core";
|
62
48
|
function defineCodeBlockSpec() {
|
63
49
|
return defineNodeSpec({
|
64
50
|
name: "codeBlock",
|
@@ -88,49 +74,19 @@ function defineCodeBlockSpec() {
|
|
88
74
|
}
|
89
75
|
});
|
90
76
|
}
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
return { language: match[1] || "" };
|
96
|
-
};
|
97
|
-
return textblockTypeInputRule(/^```(\S*)\s$/, nodeType, getAttrs);
|
98
|
-
});
|
99
|
-
}
|
100
|
-
function defineCodeBlockCommands() {
|
101
|
-
return defineCommands({
|
102
|
-
setCodeBlockLanguage: (language) => (state, dispatch) => {
|
103
|
-
const pos = state.selection.$from.before();
|
104
|
-
const codeBlock = state.doc.nodeAt(pos);
|
105
|
-
if (!codeBlock || codeBlock.type.name !== "codeBlock") {
|
106
|
-
return false;
|
107
|
-
}
|
108
|
-
const { tr } = state;
|
109
|
-
tr.setNodeMarkup(pos, void 0, { language });
|
110
|
-
dispatch == null ? void 0 : dispatch(tr);
|
111
|
-
return true;
|
112
|
-
}
|
113
|
-
});
|
114
|
-
}
|
115
|
-
function defineCodeBlock(options) {
|
116
|
-
const extensions = [
|
77
|
+
|
78
|
+
// src/code-block/index.ts
|
79
|
+
function defineCodeBlock() {
|
80
|
+
return union([
|
117
81
|
defineCodeBlockSpec(),
|
118
82
|
defineCodeBlockInputRule(),
|
119
83
|
defineCodeBlockCommands()
|
120
|
-
];
|
121
|
-
const parser = options == null ? void 0 : options.parser;
|
122
|
-
if (parser) {
|
123
|
-
extensions.push(defineCodeBlockHighlight({ parser }));
|
124
|
-
}
|
125
|
-
const hljs = options == null ? void 0 : options.hljs;
|
126
|
-
if (hljs) {
|
127
|
-
extensions.push(defineCodeBlockHighlightDeprecated({ hljs }));
|
128
|
-
}
|
129
|
-
return union(extensions);
|
84
|
+
]);
|
130
85
|
}
|
131
86
|
export {
|
132
87
|
defineCodeBlock,
|
133
88
|
defineCodeBlockCommands,
|
89
|
+
defineCodeBlockHighlight,
|
134
90
|
defineCodeBlockInputRule,
|
135
91
|
defineCodeBlockSpec
|
136
92
|
};
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@prosekit/extensions",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.2.0",
|
5
5
|
"private": false,
|
6
6
|
"author": {
|
7
7
|
"name": "ocavue",
|
@@ -106,11 +106,6 @@
|
|
106
106
|
"import": "./dist/prosekit-extensions-strike.js",
|
107
107
|
"default": "./dist/prosekit-extensions-strike.js"
|
108
108
|
},
|
109
|
-
"./suggestion": {
|
110
|
-
"types": "./dist/prosekit-extensions-suggestion.d.ts",
|
111
|
-
"import": "./dist/prosekit-extensions-suggestion.js",
|
112
|
-
"default": "./dist/prosekit-extensions-suggestion.js"
|
113
|
-
},
|
114
109
|
"./underline": {
|
115
110
|
"types": "./dist/prosekit-extensions-underline.d.ts",
|
116
111
|
"import": "./dist/prosekit-extensions-underline.js",
|
@@ -121,18 +116,16 @@
|
|
121
116
|
"dist"
|
122
117
|
],
|
123
118
|
"dependencies": {
|
124
|
-
"@prosekit/core": "^0.
|
125
|
-
"@prosekit/pm": "^0.1.
|
126
|
-
"highlight.js": "^11.9.0",
|
119
|
+
"@prosekit/core": "^0.2.0",
|
120
|
+
"@prosekit/pm": "^0.1.1",
|
127
121
|
"prosemirror-flat-list": "^0.4.5",
|
128
|
-
"prosemirror-highlight": "^0.3.
|
129
|
-
"prosemirror-highlightjs": "^0.9.1"
|
122
|
+
"prosemirror-highlight": "^0.3.3"
|
130
123
|
},
|
131
124
|
"devDependencies": {
|
132
125
|
"@prosekit/dev": "*",
|
133
126
|
"tsup": "^8.0.1",
|
134
127
|
"typescript": "^5.3.3",
|
135
|
-
"vitest": "^1.0
|
128
|
+
"vitest": "^1.1.0"
|
136
129
|
},
|
137
130
|
"scripts": {
|
138
131
|
"build:tsup": "tsup",
|
@@ -186,9 +179,6 @@
|
|
186
179
|
"strike": [
|
187
180
|
"./dist/prosekit-extensions-strike.d.ts"
|
188
181
|
],
|
189
|
-
"suggestion": [
|
190
|
-
"./dist/prosekit-extensions-suggestion.d.ts"
|
191
|
-
],
|
192
182
|
"underline": [
|
193
183
|
"./dist/prosekit-extensions-underline.d.ts"
|
194
184
|
]
|
@@ -1,161 +0,0 @@
|
|
1
|
-
// src/suggestion/index.ts
|
2
|
-
import { definePlugin } from "@prosekit/core";
|
3
|
-
|
4
|
-
// src/suggestion/plugin.ts
|
5
|
-
import { ProseKitError } from "@prosekit/core";
|
6
|
-
import { Plugin, PluginKey } from "@prosekit/pm/state";
|
7
|
-
import { Decoration, DecorationSet } from "@prosekit/pm/view";
|
8
|
-
|
9
|
-
// src/suggestion/is-valid.ts
|
10
|
-
import "@prosekit/pm/model";
|
11
|
-
import "@prosekit/pm/state";
|
12
|
-
function defaultIsValid({ state }) {
|
13
|
-
return state.selection.empty && !isInsideCode(state.selection.$from);
|
14
|
-
}
|
15
|
-
function isInsideCode($pos) {
|
16
|
-
for (let d = $pos.depth; d > 0; d--) {
|
17
|
-
if ($pos.node(d).type.spec.code) {
|
18
|
-
return true;
|
19
|
-
}
|
20
|
-
}
|
21
|
-
return $pos.marks().some((mark) => mark.type.name === "code");
|
22
|
-
}
|
23
|
-
|
24
|
-
// src/suggestion/plugin.ts
|
25
|
-
var pluginKey = new PluginKey("prosemirror-prediction");
|
26
|
-
function getPluginState(state) {
|
27
|
-
return pluginKey.getState(state);
|
28
|
-
}
|
29
|
-
function getTrMeta(tr) {
|
30
|
-
return tr.getMeta(pluginKey);
|
31
|
-
}
|
32
|
-
function setTrMeta(tr, meta) {
|
33
|
-
return tr.setMeta(pluginKey, meta);
|
34
|
-
}
|
35
|
-
function createPredictionPlugin(options) {
|
36
|
-
if (options.rules.length === 0) {
|
37
|
-
throw new ProseKitError(
|
38
|
-
"You can't create a prediction plugin without rules"
|
39
|
-
);
|
40
|
-
}
|
41
|
-
const { onMatch, onDeactivate, isValid = defaultIsValid } = options;
|
42
|
-
return new Plugin({
|
43
|
-
key: pluginKey,
|
44
|
-
state: {
|
45
|
-
init: () => {
|
46
|
-
return { active: false, ignore: null, matching: null };
|
47
|
-
},
|
48
|
-
apply: (tr, prevValue, oldState, newState) => {
|
49
|
-
var _a;
|
50
|
-
const meta = getTrMeta(tr);
|
51
|
-
if (!tr.docChanged && oldState.selection.eq(newState.selection) && !meta) {
|
52
|
-
return prevValue;
|
53
|
-
}
|
54
|
-
if (meta) {
|
55
|
-
return meta;
|
56
|
-
}
|
57
|
-
if (!isValid({ state: newState })) {
|
58
|
-
return { active: false, ignore: null, matching: null };
|
59
|
-
}
|
60
|
-
const nextValue = calcPluginState(newState, options.rules);
|
61
|
-
if (nextValue.active && prevValue.ignore != null && ((_a = nextValue.matching) == null ? void 0 : _a.from) === prevValue.ignore) {
|
62
|
-
return prevValue;
|
63
|
-
}
|
64
|
-
return nextValue;
|
65
|
-
}
|
66
|
-
},
|
67
|
-
view: () => ({
|
68
|
-
update: (view, prevState) => {
|
69
|
-
const prevPluginState = getPluginState(prevState);
|
70
|
-
const currPluginState = getPluginState(view.state);
|
71
|
-
if ((currPluginState == null ? void 0 : currPluginState.active) && currPluginState.matching && currPluginState.matching.from !== currPluginState.ignore) {
|
72
|
-
const { from, to } = currPluginState.matching;
|
73
|
-
const dismiss = () => {
|
74
|
-
view.dispatch(
|
75
|
-
setTrMeta(view.state.tr, {
|
76
|
-
active: false,
|
77
|
-
ignore: from,
|
78
|
-
matching: null
|
79
|
-
})
|
80
|
-
);
|
81
|
-
};
|
82
|
-
const textContent = view.state.doc.textBetween(from, to, "\uFFFC");
|
83
|
-
const deleteMatch = () => {
|
84
|
-
if (view.state.doc.textBetween(from, to, "\uFFFC") === textContent) {
|
85
|
-
view.dispatch(view.state.tr.delete(from, to));
|
86
|
-
}
|
87
|
-
};
|
88
|
-
onMatch({
|
89
|
-
rule: currPluginState.matching.rule,
|
90
|
-
match: currPluginState.matching.match,
|
91
|
-
matchAfter: currPluginState.matching.matchAfter,
|
92
|
-
state: view.state,
|
93
|
-
dismiss,
|
94
|
-
deleteMatch
|
95
|
-
});
|
96
|
-
} else if (prevPluginState == null ? void 0 : prevPluginState.active) {
|
97
|
-
onDeactivate();
|
98
|
-
}
|
99
|
-
}
|
100
|
-
}),
|
101
|
-
props: {
|
102
|
-
decorations: (state) => {
|
103
|
-
const pluginState = getPluginState(state);
|
104
|
-
if ((pluginState == null ? void 0 : pluginState.active) && pluginState.matching) {
|
105
|
-
const { from, to } = pluginState.matching;
|
106
|
-
const deco = Decoration.inline(from, to, {
|
107
|
-
class: "prosemirror-prediction-match"
|
108
|
-
});
|
109
|
-
return DecorationSet.create(state.doc, [deco]);
|
110
|
-
}
|
111
|
-
return null;
|
112
|
-
}
|
113
|
-
}
|
114
|
-
});
|
115
|
-
}
|
116
|
-
function calcPluginState(state, rules) {
|
117
|
-
const { $anchor } = state.selection;
|
118
|
-
const matchAfter = rules.some((rule) => rule.matchAfter);
|
119
|
-
const parentOffset = $anchor.parentOffset;
|
120
|
-
const textBefore = $anchor.parent.textBetween(
|
121
|
-
Math.max(0, parentOffset - MAX_MATCH),
|
122
|
-
parentOffset,
|
123
|
-
null,
|
124
|
-
"\uFFFC"
|
125
|
-
);
|
126
|
-
const textAfter = matchAfter ? $anchor.parent.textBetween(
|
127
|
-
parentOffset,
|
128
|
-
Math.min(parentOffset + MAX_MATCH, $anchor.parent.content.size),
|
129
|
-
null
|
130
|
-
) : "";
|
131
|
-
for (const rule of rules) {
|
132
|
-
const match = textBefore.match(rule.match);
|
133
|
-
const matchAfter2 = rule.matchAfter ? textAfter.match(rule.matchAfter) : null;
|
134
|
-
if ((match == null ? void 0 : match.index) != null) {
|
135
|
-
const from = $anchor.pos - textBefore.length + match.index;
|
136
|
-
const to = $anchor.pos + (matchAfter2 ? matchAfter2[0].length : 0);
|
137
|
-
return {
|
138
|
-
active: true,
|
139
|
-
ignore: null,
|
140
|
-
matching: {
|
141
|
-
rule,
|
142
|
-
from,
|
143
|
-
to,
|
144
|
-
match,
|
145
|
-
matchAfter: matchAfter2
|
146
|
-
}
|
147
|
-
};
|
148
|
-
}
|
149
|
-
}
|
150
|
-
return { active: false };
|
151
|
-
}
|
152
|
-
var MAX_MATCH = 200;
|
153
|
-
|
154
|
-
// src/suggestion/index.ts
|
155
|
-
function defineSuggestion(options) {
|
156
|
-
const plugin = createPredictionPlugin(options);
|
157
|
-
return definePlugin(plugin);
|
158
|
-
}
|
159
|
-
export {
|
160
|
-
defineSuggestion
|
161
|
-
};
|