@prosekit/extensions 0.1.6 → 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';
@@ -49,8 +48,6 @@ export declare function createAutocompletePlugin({ getRules, }: {
49
48
  getRules: () => AutocompleteRule[];
50
49
  }): Plugin_2;
51
50
 
52
- export declare function createPredictionPlugin(options: SuggestionOptions): Plugin_2;
53
-
54
51
  export declare const default_alias: Options | Options[] | ((overrideOptions: Options) => Options | Options[] | Promise<Options | Options[]>);
55
52
 
56
53
  export declare const default_alias_1: {
@@ -63,10 +60,6 @@ export declare function defaultCanMatch({ state }: {
63
60
  state: EditorState;
64
61
  }): boolean;
65
62
 
66
- export declare function defaultIsValid({ state }: {
67
- state: EditorState;
68
- }): boolean;
69
-
70
63
  export declare function defineAutocomplete(rule: AutocompleteRule): Extension;
71
64
 
72
65
  /**
@@ -121,18 +114,7 @@ toggleCode: [];
121
114
  *
122
115
  * @public
123
116
  */
124
- export declare function defineCodeBlock(options?: {
125
- /**
126
- * @deprecated Use `defineCodeBlockHighlight` function instead.
127
- */
128
- hljs?: HLJSApi;
129
- /**
130
- * A parser for the `prosemirror-highlight` package to use for syntax highlighting.
131
- *
132
- * @deprecated Use the standalone `defineCodeBlockHighlight` function instead.
133
- */
134
- parser?: HighlightParser;
135
- }): Extension< {
117
+ export declare function defineCodeBlock(): Extension< {
136
118
  NODES: "codeBlock";
137
119
  COMMAND_ARGS: {
138
120
  setCodeBlockLanguage: [language: string];
@@ -166,13 +148,6 @@ declare function defineCodeBlockHighlight({ parser, }: {
166
148
  export { defineCodeBlockHighlight }
167
149
  export { defineCodeBlockHighlight as defineCodeBlockHighlight_alias_1 }
168
150
 
169
- /**
170
- * @deprecated
171
- */
172
- export declare function defineCodeBlockHighlightDeprecated(options: {
173
- hljs?: HLJSApi;
174
- }): Extension<ExtensionTyping<string, string, CommandArgs>>;
175
-
176
151
  /**
177
152
  * Adds input rules for `codeBlock` nodes.
178
153
  *
@@ -403,11 +378,6 @@ export declare function defineStrikeSpec(): Extension< {
403
378
  MARKS: "strike";
404
379
  }>;
405
380
 
406
- /**
407
- * @deprecated Use `defineAutocomplete` instead.
408
- */
409
- export declare function defineSuggestion(options: SuggestionOptions): Extension<ExtensionTyping<string, string, CommandArgs>>;
410
-
411
381
  /**
412
382
  * @public
413
383
  */
@@ -469,18 +439,6 @@ declare type MatchHandler = (options: {
469
439
  export { MatchHandler }
470
440
  export { MatchHandler as MatchHandler_alias_1 }
471
441
 
472
- /**
473
- * @returns Return a Transaction object if you want to append a transaction to current state (using )
474
- */
475
- declare type MatchHandler_2 = (options: {
476
- rule: PredictionRule;
477
- match: RegExpMatchArray;
478
- matchAfter: RegExpMatchArray | null;
479
- state: EditorState;
480
- dismiss: VoidFunction;
481
- deleteMatch: VoidFunction;
482
- }) => void;
483
-
484
442
  export declare interface MentionAttrs {
485
443
  id: string;
486
444
  kind: string;
@@ -517,31 +475,6 @@ export declare interface PredictionPluginState {
517
475
  } | null;
518
476
  }
519
477
 
520
- declare interface PredictionRule {
521
- match: RegExp;
522
- matchAfter?: RegExp;
523
- }
524
- export { PredictionRule }
525
- export { PredictionRule as PredictionRule_alias_1 }
526
-
527
478
  export declare function setTrMeta(tr: Transaction, meta: PredictionPluginState): Transaction;
528
479
 
529
- declare interface SuggestionOptions {
530
- rules: PredictionRule[];
531
- onMatch: MatchHandler_2;
532
- onDeactivate: VoidFunction;
533
- /**
534
- * You can pass this function if you want to skip the matching in some cases.
535
- * By default, the plugin will only run the matching if the current selection
536
- * is empty, and the selection is not inside a
537
- * [code](https://prosemirror.net/docs/ref/#model.NodeSpec.code) node nor
538
- * inside a mark with the name as `code`.
539
- */
540
- isValid?: (options: {
541
- state: EditorState;
542
- }) => boolean;
543
- }
544
- export { SuggestionOptions }
545
- export { SuggestionOptions as SuggestionOptions_alias_1 }
546
-
547
480
  export { }
@@ -30,47 +30,6 @@ function defineCodeBlockHighlight({
30
30
  );
31
31
  }
32
32
 
33
- // src/code-block/code-block-highlight-deprecated.ts
34
- import { definePlugin as definePlugin2 } from "@prosekit/core";
35
- import { PluginKey, ProseMirrorPlugin } from "@prosekit/pm/state";
36
- import { DecorationSet } from "@prosekit/pm/view";
37
- import { getHighlightDecorations } from "prosemirror-highlightjs";
38
- function defineCodeBlockHighlightDeprecated(options) {
39
- const hljs = options.hljs;
40
- const plugin = new ProseMirrorPlugin({
41
- key,
42
- state: {
43
- init(_config, state) {
44
- const decorations = hljs ? getHighlightDecorations(
45
- state.doc,
46
- hljs,
47
- blockTypes,
48
- languageExtractor
49
- ) : [];
50
- return DecorationSet.create(state.doc, decorations);
51
- },
52
- apply(tr, set) {
53
- if (!tr.docChanged) {
54
- return set.map(tr.mapping, tr.doc);
55
- }
56
- const decorations = hljs ? getHighlightDecorations(tr.doc, hljs, blockTypes, languageExtractor) : [];
57
- return DecorationSet.create(tr.doc, decorations);
58
- }
59
- },
60
- props: {
61
- decorations(state) {
62
- return key.getState(state);
63
- }
64
- }
65
- });
66
- return definePlugin2(plugin);
67
- }
68
- var key = new PluginKey("prosekit-code-block-highlight");
69
- var blockTypes = ["codeBlock"];
70
- function languageExtractor(node) {
71
- return node.attrs.language || "javascript";
72
- }
73
-
74
33
  // src/code-block/code-block-input-rule.ts
75
34
  import { defineInputRule, getNodeType } from "@prosekit/core";
76
35
  import { textblockTypeInputRule } from "@prosekit/pm/inputrules";
@@ -117,21 +76,12 @@ function defineCodeBlockSpec() {
117
76
  }
118
77
 
119
78
  // src/code-block/index.ts
120
- function defineCodeBlock(options) {
121
- const extensions = [
79
+ function defineCodeBlock() {
80
+ return union([
122
81
  defineCodeBlockSpec(),
123
82
  defineCodeBlockInputRule(),
124
83
  defineCodeBlockCommands()
125
- ];
126
- const parser = options == null ? void 0 : options.parser;
127
- if (parser) {
128
- extensions.push(defineCodeBlockHighlight({ parser }));
129
- }
130
- const hljs = options == null ? void 0 : options.hljs;
131
- if (hljs) {
132
- extensions.push(defineCodeBlockHighlightDeprecated({ hljs }));
133
- }
134
- return union(extensions);
84
+ ]);
135
85
  }
136
86
  export {
137
87
  defineCodeBlock,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/extensions",
3
3
  "type": "module",
4
- "version": "0.1.6",
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.1.8",
119
+ "@prosekit/core": "^0.2.0",
125
120
  "@prosekit/pm": "^0.1.1",
126
- "highlight.js": "^11.9.0",
127
121
  "prosemirror-flat-list": "^0.4.5",
128
- "prosemirror-highlight": "^0.3.2",
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.4"
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,3 +0,0 @@
1
- export { defineSuggestion } from './_tsup-dts-rollup';
2
- export { PredictionRule } from './_tsup-dts-rollup';
3
- export { SuggestionOptions } from './_tsup-dts-rollup';
@@ -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
- };