@prosekit/core 0.7.8 → 0.7.10

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.
@@ -1,5 +1,5 @@
1
1
  import { AllSelection } from '@prosekit/pm/state';
2
- import { Attrs } from '@prosekit/pm/model';
2
+ import type { Attrs } from '@prosekit/pm/model';
3
3
  import { Command } from '@prosekit/pm/state';
4
4
  import type { ContentMatch } from '@prosekit/pm/model';
5
5
  import { config as default_alias_1 } from '@prosekit/dev/config-vitest';
@@ -15,16 +15,16 @@ import { Fragment } from '@prosekit/pm/model';
15
15
  import type { IsEqual } from 'type-fest';
16
16
  import { Mark } from '@prosekit/pm/model';
17
17
  import type { MarkSpec } from '@prosekit/pm/model';
18
- import { MarkType } from '@prosekit/pm/model';
18
+ import type { MarkType } from '@prosekit/pm/model';
19
19
  import type { Node as Node_2 } from '@prosekit/pm/model';
20
20
  import { NodeSelection } from '@prosekit/pm/state';
21
21
  import type { NodeSpec } from '@prosekit/pm/model';
22
- import { NodeType } from '@prosekit/pm/model';
22
+ import type { NodeType } from '@prosekit/pm/model';
23
23
  import { NodeType as NodeType_2 } from 'prosemirror-model';
24
24
  import type { NodeView } from '@prosekit/pm/view';
25
25
  import type { NodeViewConstructor } from '@prosekit/pm/view';
26
26
  import { Options } from 'tsup';
27
- import type { ParseOptions } from '@prosekit/pm/model';
27
+ import { ParseOptions } from '@prosekit/pm/model';
28
28
  import { Plugin as Plugin_2 } from '@prosekit/pm/state';
29
29
  import { ProseMirrorFragment } from '@prosekit/pm/model';
30
30
  import { ProseMirrorNode } from '@prosekit/pm/model';
@@ -167,6 +167,7 @@ declare type BaseCommandsExtension = Extension<{
167
167
  wrap: [options: WrapOptions];
168
168
  setBlockType: [options: SetBlockTypeOptions];
169
169
  setNodeAttrs: [options: SetNodeAttrsOptions];
170
+ insertDefaultBlock: [options?: InsertDefaultBlockOptions];
170
171
  selectAll: [];
171
172
  addMark: [options: AddMarkOptions];
172
173
  removeMark: [options: RemoveMarkOptions];
@@ -294,17 +295,31 @@ declare function collectNodes(content: NodeContent): ProseMirrorNode[];
294
295
  export { collectNodes }
295
296
  export { collectNodes as collectNodes_alias_1 }
296
297
 
297
- export declare function combineEventHandlers<Handler extends (...args: any[]) => boolean | void, Args extends Parameters<Handler> = Parameters<Handler>>(): readonly [(handlers: Handler[]) => void, (...args: Args) => boolean];
298
+ export declare function combineEventHandlers<Handler extends (...args: any[]) => boolean | void, Args extends Parameters<Handler> = Parameters<Handler>>(): readonly [(eventHandlers: Handler[]) => void, (...args: Args) => boolean];
298
299
 
299
300
  /**
300
301
  * A function to apply a command to the editor. It will return `true` if the command was applied, and `false` otherwise.
301
302
  *
302
- * It also has a `canApply` method to check if the command can be applied.
303
+ * It also has a `canExec` method to check if the command can be applied.
303
304
  *
304
305
  * @public
305
306
  */
306
307
  declare interface CommandAction<Args extends any[] = any[]> {
308
+ /**
309
+ * Execute the current command. Return `true` if the command was successfully
310
+ * executed, otherwise `false`.
311
+ */
307
312
  (...args: Args): boolean;
313
+ /**
314
+ * Check if the current command can be executed. Return `true` if the command
315
+ * can be executed, otherwise `false`.
316
+ */
317
+ canExec(...args: Args): boolean;
318
+ /**
319
+ * An alias for `canExec`.
320
+ *
321
+ * @deprecated Use `canExec` instead.
322
+ */
308
323
  canApply(...args: Args): boolean;
309
324
  }
310
325
  export { CommandAction }
@@ -953,6 +968,16 @@ declare class Editor<E extends Extension = any> {
953
968
  * - The string "end" (to set selection at the end)
954
969
  */
955
970
  setContent: (content: ProseMirrorNode | NodeJSON | string | HTMLElement, selection?: SelectionJSON | Selection_2 | "start" | "end") => void;
971
+ /**
972
+ * Execute the given command. Return `true` if the command was successfully
973
+ * executed, otherwise `false`.
974
+ */
975
+ exec: (command: Command) => boolean;
976
+ /**
977
+ * Check if the given command can be executed. Return `true` if the command
978
+ * can be executed, otherwise `false`.
979
+ */
980
+ canExec: (command: Command) => boolean;
956
981
  /**
957
982
  * All {@link CommandAction}s defined by the editor.
958
983
  */
@@ -986,6 +1011,7 @@ export declare class EditorInstance {
986
1011
  constructor(extension: Extension);
987
1012
  getState: () => EditorState;
988
1013
  updateState(state: EditorState): void;
1014
+ private dispatch;
989
1015
  setContent(content: NodeJSON | string | HTMLElement | ProseMirrorNode, selection?: SelectionJSON | Selection_2 | 'start' | 'end'): void;
990
1016
  private updateExtension;
991
1017
  use(extension: Extension): VoidFunction;
@@ -995,6 +1021,8 @@ export declare class EditorInstance {
995
1021
  get assertView(): EditorView;
996
1022
  definePlugins(plugins: readonly Plugin_2[]): void;
997
1023
  removePlugins(plugins: readonly Plugin_2[]): void;
1024
+ exec(command: Command): boolean;
1025
+ canExec(command: Command): boolean;
998
1026
  defineCommand<Args extends any[] = any[]>(name: string, commandCreator: CommandCreator<Args>): void;
999
1027
  removeCommand(name: string): void;
1000
1028
  }
@@ -1296,7 +1324,7 @@ export { findParentNode as findParentNode_alias_1 }
1296
1324
  */
1297
1325
  declare function findParentNodeOfType(
1298
1326
  /**
1299
- * The type of the node to remove.
1327
+ * The type of the node to find.
1300
1328
  */
1301
1329
  type: NodeType | string,
1302
1330
  /**
@@ -1462,6 +1490,31 @@ export { htmlFromNode as htmlFromNode_alias_1 }
1462
1490
 
1463
1491
  export declare function includesMark(marks: readonly Mark[], markType: MarkType, attrs?: Attrs | null): boolean;
1464
1492
 
1493
+ export declare function inputText(input: string): Promise<void>;
1494
+
1495
+ /**
1496
+ * Returns a command that inserts a default block after current selection or at
1497
+ * the given position.
1498
+ *
1499
+ * @public
1500
+ */
1501
+ declare function insertDefaultBlock(options?: InsertDefaultBlockOptions): Command;
1502
+ export { insertDefaultBlock }
1503
+ export { insertDefaultBlock as insertDefaultBlock_alias_1 }
1504
+
1505
+ /**
1506
+ * @public
1507
+ */
1508
+ declare interface InsertDefaultBlockOptions {
1509
+ /**
1510
+ * The position to insert the node at. By default it will insert after the
1511
+ * current selection.
1512
+ */
1513
+ pos?: number;
1514
+ }
1515
+ export { InsertDefaultBlockOptions }
1516
+ export { InsertDefaultBlockOptions as InsertDefaultBlockOptions_alias_1 }
1517
+
1465
1518
  /**
1466
1519
  * Returns a command that inserts the given node at the current selection or at
1467
1520
  * the given position.
@@ -2620,7 +2673,8 @@ export { Union }
2620
2673
  export { Union as Union_alias_1 }
2621
2674
 
2622
2675
  /**
2623
- * Merge multiple extensions into one.
2676
+ * Merges multiple extensions into one. You can pass multiple extensions as
2677
+ * arguments or a single array containing multiple extensions.
2624
2678
  *
2625
2679
  * @throws If no extensions are provided.
2626
2680
  *
@@ -2628,6 +2682,17 @@ export { Union as Union_alias_1 }
2628
2682
  *
2629
2683
  * ```ts
2630
2684
  * function defineFancyNodes() {
2685
+ * return union(
2686
+ * defineFancyParagraph(),
2687
+ * defineFancyHeading(),
2688
+ * )
2689
+ * }
2690
+ * ```
2691
+ *
2692
+ * @example
2693
+ *
2694
+ * ```ts
2695
+ * function defineFancyNodes() {
2631
2696
  * return union([
2632
2697
  * defineFancyParagraph(),
2633
2698
  * defineFancyHeading(),
@@ -2804,7 +2869,7 @@ declare interface WrapOptions {
2804
2869
  /**
2805
2870
  * @deprecated Use `nodeSpec` instead.
2806
2871
  */
2807
- nodeType: NodeType;
2872
+ nodeType?: NodeType;
2808
2873
  /**
2809
2874
  * Optional attributes to apply to the node.
2810
2875
  */
@@ -17,7 +17,6 @@ var DOMDocumentNotFoundError = class extends ProseKitError {
17
17
  };
18
18
 
19
19
  // src/utils/get-mark-type.ts
20
- import "@prosekit/pm/model";
21
20
  function getMarkType(schema, type) {
22
21
  if (typeof type === "string") {
23
22
  const markType = schema.marks[type];
@@ -37,7 +36,6 @@ function assert(condition, message = "Assertion failed") {
37
36
  }
38
37
 
39
38
  // src/utils/get-node-type.ts
40
- import "@prosekit/pm/model";
41
39
  function getNodeType(schema, type) {
42
40
  if (typeof type === "string") {
43
41
  const nodeType = schema.nodes[type];
@@ -59,6 +57,39 @@ var Priority = /* @__PURE__ */ ((Priority2) => {
59
57
  return Priority2;
60
58
  })(Priority || {});
61
59
 
60
+ // src/facets/facet.ts
61
+ var facetCount = 0;
62
+ var Facet = class {
63
+ /**
64
+ * @internal
65
+ */
66
+ constructor(parent, singleton, _reducer, _reduce) {
67
+ this._reducer = _reducer;
68
+ this._reduce = _reduce;
69
+ /**
70
+ * @internal
71
+ */
72
+ this.index = facetCount++;
73
+ assert((_reduce || _reducer) && !(_reduce && _reducer));
74
+ this.parent = parent;
75
+ this.singleton = singleton;
76
+ this.path = parent ? [...parent.path, this.index] : [];
77
+ }
78
+ get reducer() {
79
+ var _a, _b;
80
+ return (_b = this._reducer) != null ? _b : (_a = this._reduce) == null ? void 0 : _a.call(this);
81
+ }
82
+ };
83
+ function defineFacet(options) {
84
+ var _a;
85
+ return new Facet(
86
+ options.parent,
87
+ (_a = options.singleton) != null ? _a : false,
88
+ options.reducer,
89
+ options.reduce
90
+ );
91
+ }
92
+
62
93
  // src/utils/type-assertion.ts
63
94
  import { Fragment, Mark, ProseMirrorNode, Slice } from "@prosekit/pm/model";
64
95
  import {
@@ -95,41 +126,74 @@ function isNotNullish(value) {
95
126
  return value != null;
96
127
  }
97
128
 
98
- // src/facets/facet.ts
99
- var facetCount = 0;
100
- var Facet = class {
129
+ // src/facets/schema.ts
130
+ import { Schema } from "@prosekit/pm/model";
131
+
132
+ // src/facets/root.ts
133
+ function rootReducer(inputs) {
134
+ var _a;
135
+ let schema;
136
+ let commands;
137
+ let stateFunc;
138
+ let view;
139
+ for (const input of inputs) {
140
+ schema = input.schema || schema;
141
+ commands = input.commands || commands;
142
+ stateFunc = input.state || stateFunc;
143
+ view = input.view || view;
144
+ }
145
+ const state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema });
146
+ return { schema, state, commands, view };
147
+ }
148
+ var rootFacet = new Facet(
149
+ null,
150
+ true,
151
+ rootReducer
152
+ );
153
+
154
+ // src/facets/schema.ts
155
+ var schemaFacet = defineFacet({
156
+ reducer: (specs) => {
157
+ assert(specs.length <= 1);
158
+ const spec = specs[0];
159
+ const schema = spec ? new Schema(spec) : null;
160
+ return { schema };
161
+ },
162
+ parent: rootFacet,
163
+ singleton: true
164
+ });
165
+
166
+ // src/facets/base-extension.ts
167
+ var BaseExtension = class {
168
+ constructor() {
169
+ this.extension = [];
170
+ this.trees = [null, null, null, null, null];
171
+ }
101
172
  /**
102
173
  * @internal
103
174
  */
104
- constructor(parent, singleton, _reducer, _reduce) {
105
- this._reducer = _reducer;
106
- this._reduce = _reduce;
107
- /**
108
- * @internal
109
- */
110
- this.index = facetCount++;
111
- assert((_reduce || _reducer) && !(_reduce && _reducer));
112
- this.parent = parent;
113
- this.singleton = singleton;
114
- this.path = parent ? [...parent.path, this.index] : [];
175
+ getTree(priority) {
176
+ var _a, _b;
177
+ const pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */;
178
+ return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri));
115
179
  }
116
- get reducer() {
180
+ /**
181
+ * @internal
182
+ */
183
+ findFacetOutput(facet) {
184
+ var _a;
185
+ let node = this.getTree();
186
+ for (const index of facet.path) {
187
+ node = node == null ? void 0 : node.children.get(index);
188
+ }
189
+ return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null;
190
+ }
191
+ get schema() {
117
192
  var _a, _b;
118
- return (_b = this._reducer) != null ? _b : (_a = this._reduce) == null ? void 0 : _a.call(this);
193
+ const output = this.findFacetOutput(schemaFacet);
194
+ return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null;
119
195
  }
120
196
  };
121
- function defineFacet(options) {
122
- var _a;
123
- return new Facet(
124
- options.parent,
125
- (_a = options.singleton) != null ? _a : false,
126
- options.reducer,
127
- options.reduce
128
- );
129
- }
130
-
131
- // src/facets/base-extension.ts
132
- import "@prosekit/pm/model";
133
197
 
134
198
  // src/utils/array.ts
135
199
  function uniqPush(prev, next) {
@@ -273,75 +337,6 @@ var FacetNode = class {
273
337
  }
274
338
  };
275
339
 
276
- // src/facets/schema.ts
277
- import { Schema as Schema3 } from "@prosekit/pm/model";
278
-
279
- // src/facets/root.ts
280
- function rootReducer(inputs) {
281
- var _a;
282
- let schema;
283
- let commands;
284
- let stateFunc;
285
- let view;
286
- for (const input of inputs) {
287
- schema = input.schema || schema;
288
- commands = input.commands || commands;
289
- stateFunc = input.state || stateFunc;
290
- view = input.view || view;
291
- }
292
- const state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema });
293
- return { schema, state, commands, view };
294
- }
295
- var rootFacet = new Facet(
296
- null,
297
- true,
298
- rootReducer
299
- );
300
-
301
- // src/facets/schema.ts
302
- var schemaFacet = defineFacet({
303
- reducer: (specs) => {
304
- assert(specs.length <= 1);
305
- const spec = specs[0];
306
- const schema = spec ? new Schema3(spec) : null;
307
- return { schema };
308
- },
309
- parent: rootFacet,
310
- singleton: true
311
- });
312
-
313
- // src/facets/base-extension.ts
314
- var BaseExtension = class {
315
- constructor() {
316
- this.extension = [];
317
- this.trees = [null, null, null, null, null];
318
- }
319
- /**
320
- * @internal
321
- */
322
- getTree(priority) {
323
- var _a, _b;
324
- const pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */;
325
- return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri));
326
- }
327
- /**
328
- * @internal
329
- */
330
- findFacetOutput(facet) {
331
- var _a;
332
- let node = this.getTree();
333
- for (const index of facet.path) {
334
- node = node == null ? void 0 : node.children.get(index);
335
- }
336
- return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null;
337
- }
338
- get schema() {
339
- var _a, _b;
340
- const output = this.findFacetOutput(schemaFacet);
341
- return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null;
342
- }
343
- };
344
-
345
340
  // src/facets/facet-extension.ts
346
341
  var FacetExtensionImpl = class extends BaseExtension {
347
342
  /**
@@ -373,7 +368,10 @@ function defineFacetPayload(facet, payloads) {
373
368
  }
374
369
 
375
370
  // src/utils/parse.ts
376
- import { DOMParser, DOMSerializer } from "@prosekit/pm/model";
371
+ import {
372
+ DOMParser,
373
+ DOMSerializer
374
+ } from "@prosekit/pm/model";
377
375
  import { EditorState } from "@prosekit/pm/state";
378
376
 
379
377
  // src/utils/get-dom-api.ts
@@ -672,8 +670,9 @@ function union(...exts) {
672
670
  }
673
671
 
674
672
  // src/editor/editor.ts
675
- import "@prosekit/pm/model";
676
- import { EditorState as EditorState2 } from "@prosekit/pm/state";
673
+ import {
674
+ EditorState as EditorState2
675
+ } from "@prosekit/pm/state";
677
676
  import { EditorView } from "@prosekit/pm/view";
678
677
 
679
678
  // src/utils/deep-equals.ts
@@ -700,7 +699,6 @@ function deepEquals(a, b) {
700
699
  }
701
700
 
702
701
  // src/editor/action.ts
703
- import "@prosekit/pm/model";
704
702
  import mapValues from "just-map-values";
705
703
 
706
704
  // src/utils/attrs-match.ts
@@ -811,10 +809,10 @@ function isNodeChild(value) {
811
809
  // src/editor/editor.ts
812
810
  function setupEditorExtension(options) {
813
811
  if (options.defaultContent || options.defaultDoc || options.defaultHTML) {
814
- return union([
812
+ return union(
815
813
  options.extension,
816
814
  defineDefaultState(options)
817
- ]);
815
+ );
818
816
  }
819
817
  return options.extension;
820
818
  }
@@ -832,6 +830,13 @@ var EditorInstance = class {
832
830
  var _a;
833
831
  return ((_a = this.view) == null ? void 0 : _a.state) || this.directEditorProps.state;
834
832
  };
833
+ this.dispatch = (tr) => {
834
+ if (this.view) {
835
+ this.view.dispatch(tr);
836
+ } else {
837
+ this.directEditorProps.state = this.directEditorProps.state.apply(tr);
838
+ }
839
+ };
835
840
  this.tree = extension.getTree();
836
841
  const payload = this.tree.getRootOutput();
837
842
  const schema = payload.schema;
@@ -956,21 +961,27 @@ var EditorInstance = class {
956
961
  const newState = state.reconfigure({ plugins: newPlugins });
957
962
  view.setProps({ state: newState });
958
963
  }
964
+ exec(command) {
965
+ var _a;
966
+ const state = this.getState();
967
+ return command(state, this.dispatch, (_a = this.view) != null ? _a : void 0);
968
+ }
969
+ canExec(command) {
970
+ var _a;
971
+ const state = this.getState();
972
+ return command(state, void 0, (_a = this.view) != null ? _a : void 0);
973
+ }
959
974
  defineCommand(name, commandCreator) {
960
975
  const action = (...args) => {
961
- const view = this.view;
962
- assert(view, `Cannot call command "${name}" before the editor is mounted`);
963
976
  const command = commandCreator(...args);
964
- return command(view.state, view.dispatch.bind(view), view);
977
+ return this.exec(command);
965
978
  };
966
- action.canApply = (...args) => {
967
- const view = this.view;
968
- if (!view) {
969
- return false;
970
- }
979
+ const canExec = (...args) => {
971
980
  const command = commandCreator(...args);
972
- return command(view.state, void 0, view);
981
+ return this.canExec(command);
973
982
  };
983
+ action.canApply = canExec;
984
+ action.canExec = canExec;
974
985
  this.commands[name] = action;
975
986
  }
976
987
  removeCommand(name) {
@@ -1048,6 +1059,20 @@ var Editor = class {
1048
1059
  this.setContent = (content, selection) => {
1049
1060
  return this.instance.setContent(content, selection);
1050
1061
  };
1062
+ /**
1063
+ * Execute the given command. Return `true` if the command was successfully
1064
+ * executed, otherwise `false`.
1065
+ */
1066
+ this.exec = (command) => {
1067
+ return this.instance.exec(command);
1068
+ };
1069
+ /**
1070
+ * Check if the given command can be executed. Return `true` if the command
1071
+ * can be executed, otherwise `false`.
1072
+ */
1073
+ this.canExec = (command) => {
1074
+ return this.instance.canExec(command);
1075
+ };
1051
1076
  if (!(instance instanceof EditorInstance)) {
1052
1077
  throw new TypeError("Invalid EditorInstance");
1053
1078
  }
@@ -1112,6 +1137,9 @@ export {
1112
1137
  getNodeType,
1113
1138
  isNodeActive,
1114
1139
  Priority,
1140
+ defineFacet,
1141
+ rootFacet,
1142
+ schemaFacet,
1115
1143
  toReversed,
1116
1144
  isProseMirrorNode,
1117
1145
  isMark,
@@ -1122,9 +1150,6 @@ export {
1122
1150
  isNodeSelection,
1123
1151
  isAllSelection,
1124
1152
  isNotNullish,
1125
- defineFacet,
1126
- rootFacet,
1127
- schemaFacet,
1128
1153
  defineFacetPayload,
1129
1154
  stateFacet,
1130
1155
  isElement,
@@ -6,10 +6,13 @@ import {
6
6
  createNodeActions,
7
7
  isProseMirrorNode,
8
8
  setupEditorExtension
9
- } from "./chunk-UKHJHMFE.js";
9
+ } from "./chunk-RNBMCB5M.js";
10
10
 
11
11
  // src/test/test-editor.ts
12
- import { NodeSelection, TextSelection } from "@prosekit/pm/state";
12
+ import {
13
+ NodeSelection,
14
+ TextSelection
15
+ } from "@prosekit/pm/state";
13
16
 
14
17
  // src/test/test-builder.ts
15
18
  var createNodeForTest = (type, attrs, children) => {
@@ -4,6 +4,8 @@ export { expandMark } from './_tsup-dts-rollup';
4
4
  export { ExpandMarkOptions } from './_tsup-dts-rollup';
5
5
  export { insertNode } from './_tsup-dts-rollup';
6
6
  export { InsertNodeOptions } from './_tsup-dts-rollup';
7
+ export { insertDefaultBlock } from './_tsup-dts-rollup';
8
+ export { InsertDefaultBlockOptions } from './_tsup-dts-rollup';
7
9
  export { removeMark } from './_tsup-dts-rollup';
8
10
  export { RemoveMarkOptions } from './_tsup-dts-rollup';
9
11
  export { removeNode } from './_tsup-dts-rollup';
@@ -39,10 +39,9 @@ import {
39
39
  stateFromJSON,
40
40
  toReversed,
41
41
  union
42
- } from "./chunk-UKHJHMFE.js";
42
+ } from "./chunk-RNBMCB5M.js";
43
43
 
44
44
  // src/commands/add-mark.ts
45
- import "@prosekit/pm/model";
46
45
  function addMark(options) {
47
46
  return (state, dispatch) => {
48
47
  var _a, _b;
@@ -143,8 +142,42 @@ function insertNode(options) {
143
142
  };
144
143
  }
145
144
 
145
+ // src/commands/insert-default-block.ts
146
+ import { TextSelection as TextSelection3 } from "@prosekit/pm/state";
147
+
148
+ // src/utils/default-block-at.ts
149
+ function defaultBlockAt(match) {
150
+ for (let i = 0; i < match.edgeCount; i++) {
151
+ const { type } = match.edge(i);
152
+ if (type.isTextblock && !type.hasRequiredAttrs()) return type;
153
+ }
154
+ return null;
155
+ }
156
+
157
+ // src/commands/insert-default-block.ts
158
+ function insertDefaultBlock(options) {
159
+ return (state, dispatch) => {
160
+ const $pos = (options == null ? void 0 : options.pos) == null ? state.selection.$to : state.doc.resolve(options.pos);
161
+ const depth = $pos.parent.isTextblock ? $pos.depth - 1 : $pos.depth;
162
+ const parent = $pos.node(depth);
163
+ const index = $pos.indexAfter(depth);
164
+ const type = defaultBlockAt(parent.contentMatchAt(index));
165
+ if (!type) return false;
166
+ if (dispatch) {
167
+ const pos = $pos.posAtIndex(index, depth);
168
+ const node = type.createAndFill();
169
+ if (!node) return false;
170
+ const tr = state.tr.insert(pos, node);
171
+ const selection = TextSelection3.findFrom(tr.doc.resolve(pos), 1);
172
+ if (!selection) return false;
173
+ tr.setSelection(selection);
174
+ dispatch(tr.scrollIntoView());
175
+ }
176
+ return true;
177
+ };
178
+ }
179
+
146
180
  // src/commands/remove-mark.ts
147
- import "@prosekit/pm/model";
148
181
  function removeMark(options) {
149
182
  return (state, dispatch) => {
150
183
  var _a, _b;
@@ -191,13 +224,13 @@ function removeNode(options) {
191
224
  }
192
225
 
193
226
  // src/utils/get-custom-selection.ts
194
- import { TextSelection as TextSelection3 } from "@prosekit/pm/state";
227
+ import { TextSelection as TextSelection4 } from "@prosekit/pm/state";
195
228
  function getCustomSelection(state, from, to) {
196
229
  const pos = from != null ? from : to;
197
230
  if (pos != null) {
198
231
  const $from = state.doc.resolve(from != null ? from : pos);
199
232
  const $to = state.doc.resolve(to != null ? to : pos);
200
- return TextSelection3.between($from, $to);
233
+ return TextSelection4.between($from, $to);
201
234
  }
202
235
  return state.selection;
203
236
  }
@@ -242,7 +275,6 @@ function setBlockType(options) {
242
275
  }
243
276
 
244
277
  // src/utils/get-node-types.ts
245
- import "@prosekit/pm/model";
246
278
  function getNodeTypes(schema, types) {
247
279
  if (Array.isArray(types)) {
248
280
  return types.map((type) => getNodeType(schema, type));
@@ -284,7 +316,6 @@ function setNodeAttrs(options) {
284
316
 
285
317
  // src/commands/toggle-mark.ts
286
318
  import { toggleMark as baseToggleMark } from "@prosekit/pm/commands";
287
- import "@prosekit/pm/model";
288
319
  function toggleMark({
289
320
  type,
290
321
  attrs,
@@ -301,7 +332,6 @@ function toggleMark({
301
332
 
302
333
  // src/commands/toggle-node.ts
303
334
  import { setBlockType as setBlockType2 } from "@prosekit/pm/commands";
304
- import "@prosekit/pm/model";
305
335
  function toggleNode({ type, attrs }) {
306
336
  return (state, dispatch, view) => {
307
337
  if (isNodeActive(state, type, attrs)) {
@@ -319,7 +349,6 @@ function toggleNode({ type, attrs }) {
319
349
 
320
350
  // src/commands/unset-block-type.ts
321
351
  import { Fragment, Slice } from "@prosekit/pm/model";
322
- import "@prosekit/pm/state";
323
352
  import { ReplaceAroundStep } from "@prosekit/pm/transform";
324
353
  function unsetBlockType(options) {
325
354
  return (state, dispatch) => {
@@ -373,14 +402,13 @@ function unsetMark(options) {
373
402
  }
374
403
 
375
404
  // src/commands/wrap.ts
376
- import "@prosekit/pm/model";
377
405
  import { findWrapping } from "@prosekit/pm/transform";
378
406
  function wrap(options) {
379
407
  return (state, dispatch) => {
380
408
  const { $from, $to } = state.selection;
381
409
  const range = $from.blockRange($to);
382
410
  if (!range) return false;
383
- const nodeType = getNodeType(state.schema, options.type || options.nodeType);
411
+ const nodeType = getNodeType(state.schema, options.nodeType || options.type);
384
412
  const wrapping = findWrapping(range, nodeType, options.attrs);
385
413
  if (!wrapping) return false;
386
414
  dispatch == null ? void 0 : dispatch(state.tr.wrap(range, wrapping));
@@ -436,6 +464,7 @@ function defineBaseCommands() {
436
464
  wrap,
437
465
  setBlockType,
438
466
  setNodeAttrs,
467
+ insertDefaultBlock,
439
468
  selectAll,
440
469
  addMark,
441
470
  removeMark,
@@ -449,7 +478,6 @@ import clone from "just-clone";
449
478
  import OrderedMap2 from "orderedmap";
450
479
 
451
480
  // src/facets/schema-spec.ts
452
- import "@prosekit/pm/model";
453
481
  import OrderedMap from "orderedmap";
454
482
  var schemaSpecFacet = defineFacet({
455
483
  reducer: (specs) => {
@@ -669,10 +697,12 @@ function defineDoc() {
669
697
  }
670
698
 
671
699
  // src/extensions/events/plugin-view.ts
672
- import { PluginKey, ProseMirrorPlugin as ProseMirrorPlugin2 } from "@prosekit/pm/state";
700
+ import {
701
+ PluginKey,
702
+ ProseMirrorPlugin
703
+ } from "@prosekit/pm/state";
673
704
 
674
705
  // src/extensions/plugin.ts
675
- import "@prosekit/pm/model";
676
706
  import { Plugin } from "@prosekit/pm/state";
677
707
  function definePlugin(plugin) {
678
708
  if (plugin instanceof Plugin || Array.isArray(plugin) && plugin.every((p) => p instanceof Plugin)) {
@@ -726,7 +756,7 @@ var pluginViewFacet = defineFacet({
726
756
  let mountHandlers = [];
727
757
  let updateHandlers = [];
728
758
  let unmountHandlers = [];
729
- const plugin = new ProseMirrorPlugin2({
759
+ const plugin = new ProseMirrorPlugin({
730
760
  key: pluginKey,
731
761
  view: (view) => {
732
762
  mountHandlers.forEach((fn) => fn(view));
@@ -778,16 +808,16 @@ function defineDocChangeHandler(handler) {
778
808
  }
779
809
 
780
810
  // src/extensions/events/dom-event.ts
781
- import { PluginKey as PluginKey2, ProseMirrorPlugin as ProseMirrorPlugin3 } from "@prosekit/pm/state";
811
+ import { PluginKey as PluginKey2, ProseMirrorPlugin as ProseMirrorPlugin2 } from "@prosekit/pm/state";
782
812
 
783
813
  // src/utils/combine-event-handlers.ts
784
814
  function combineEventHandlers() {
785
- let _handlers = [];
786
- function setHandlers(handlers) {
787
- _handlers = toReversed(handlers);
815
+ let handlers = [];
816
+ function setHandlers(eventHandlers) {
817
+ handlers = toReversed(eventHandlers);
788
818
  }
789
819
  function combinedEventHandler(...args) {
790
- for (const handler of _handlers) {
820
+ for (const handler of handlers) {
791
821
  if (handler(...args)) {
792
822
  return true;
793
823
  }
@@ -828,7 +858,7 @@ var domEventFacet = defineFacet({
828
858
  setHandlers(handlers);
829
859
  }
830
860
  if (hasNewEvent) {
831
- plugin = new ProseMirrorPlugin3({
861
+ plugin = new ProseMirrorPlugin2({
832
862
  key: new PluginKey2("prosekit-dom-event-handler"),
833
863
  props: { handleDOMEvents: combinedHandlerMap }
834
864
  });
@@ -844,7 +874,7 @@ var domEventFacet = defineFacet({
844
874
  });
845
875
 
846
876
  // src/extensions/events/editor-event.ts
847
- import { PluginKey as PluginKey3, ProseMirrorPlugin as ProseMirrorPlugin4 } from "@prosekit/pm/state";
877
+ import { PluginKey as PluginKey3, ProseMirrorPlugin as ProseMirrorPlugin3 } from "@prosekit/pm/state";
848
878
  function defineKeyDownHandler(handler) {
849
879
  return defineFacetPayload(editorEventFacet, [["keyDown", handler]]);
850
880
  }
@@ -921,7 +951,7 @@ function setupEditorEventPlugin() {
921
951
  setDropHandlers((_k = map.drop) != null ? _k : []);
922
952
  setScrollToSelectionHandlers((_l = map.scrollToSelection) != null ? _l : []);
923
953
  };
924
- const plugin = new ProseMirrorPlugin4({
954
+ const plugin = new ProseMirrorPlugin3({
925
955
  key: new PluginKey3("prosekit-editor-event"),
926
956
  props: {
927
957
  handleKeyDown,
@@ -1020,11 +1050,11 @@ function defineHistory({
1020
1050
  depth = 200,
1021
1051
  newGroupDelay = 250
1022
1052
  } = {}) {
1023
- return union([
1053
+ return union(
1024
1054
  definePlugin(history({ depth, newGroupDelay })),
1025
1055
  defineKeymap(keymap),
1026
1056
  defineCommands(commands)
1027
- ]);
1057
+ );
1028
1058
  }
1029
1059
 
1030
1060
  // src/extensions/keymap-base.ts
@@ -1126,7 +1156,7 @@ function wrapParseRuleAttrs(rule, attrs) {
1126
1156
  }
1127
1157
 
1128
1158
  // src/extensions/node-view.ts
1129
- import { PluginKey as PluginKey5, ProseMirrorPlugin as ProseMirrorPlugin5 } from "@prosekit/pm/state";
1159
+ import { PluginKey as PluginKey5, ProseMirrorPlugin as ProseMirrorPlugin4 } from "@prosekit/pm/state";
1130
1160
  function defineNodeView(options) {
1131
1161
  return defineFacetPayload(nodeViewFacet, [options]);
1132
1162
  }
@@ -1139,7 +1169,7 @@ var nodeViewFacet = defineFacet({
1139
1169
  }
1140
1170
  }
1141
1171
  return () => [
1142
- new ProseMirrorPlugin5({
1172
+ new ProseMirrorPlugin4({
1143
1173
  key: new PluginKey5("prosekit-node-view"),
1144
1174
  props: { nodeViews }
1145
1175
  })
@@ -1149,7 +1179,7 @@ var nodeViewFacet = defineFacet({
1149
1179
  });
1150
1180
 
1151
1181
  // src/extensions/node-view-effect.ts
1152
- import { PluginKey as PluginKey6, ProseMirrorPlugin as ProseMirrorPlugin6 } from "@prosekit/pm/state";
1182
+ import { PluginKey as PluginKey6, ProseMirrorPlugin as ProseMirrorPlugin5 } from "@prosekit/pm/state";
1153
1183
  function defineNodeViewFactory(options) {
1154
1184
  const input = [options, null];
1155
1185
  return defineFacetPayload(nodeViewFactoryFacet, [input]);
@@ -1171,7 +1201,7 @@ var nodeViewFactoryFacet = defineFacet({
1171
1201
  nodeViews[name] = factory.factory(args);
1172
1202
  }
1173
1203
  return () => [
1174
- new ProseMirrorPlugin6({
1204
+ new ProseMirrorPlugin5({
1175
1205
  key: new PluginKey6("prosekit-node-view-effect"),
1176
1206
  props: { nodeViews }
1177
1207
  })
@@ -1219,7 +1249,7 @@ function cache(fn) {
1219
1249
  var canUseRegexLookbehind = cache(() => {
1220
1250
  try {
1221
1251
  return "ab".replace(new RegExp("(?<=a)b", "g"), "c") === "ac";
1222
- } catch (error) {
1252
+ } catch (e) {
1223
1253
  return false;
1224
1254
  }
1225
1255
  });
@@ -1229,7 +1259,6 @@ import clsxLite from "clsx/lite";
1229
1259
  var clsx = clsxLite;
1230
1260
 
1231
1261
  // src/utils/collect-children.ts
1232
- import "@prosekit/pm/model";
1233
1262
  function collectChildren(parent) {
1234
1263
  const children = [];
1235
1264
  for (let i = 0; i < parent.childCount; i++) {
@@ -1239,12 +1268,12 @@ function collectChildren(parent) {
1239
1268
  }
1240
1269
 
1241
1270
  // src/utils/collect-nodes.ts
1242
- import { ProseMirrorFragment, ProseMirrorNode as ProseMirrorNode2 } from "@prosekit/pm/model";
1271
+ import { ProseMirrorFragment, ProseMirrorNode } from "@prosekit/pm/model";
1243
1272
  function collectNodes(content) {
1244
1273
  if (Array.isArray(content)) {
1245
1274
  return content.flatMap(collectNodes);
1246
1275
  }
1247
- if (content instanceof ProseMirrorNode2) {
1276
+ if (content instanceof ProseMirrorNode) {
1248
1277
  return [content];
1249
1278
  }
1250
1279
  if (content instanceof ProseMirrorFragment) {
@@ -1267,15 +1296,6 @@ function containsInlineNode(doc, from, to) {
1267
1296
  return found;
1268
1297
  }
1269
1298
 
1270
- // src/utils/default-block-at.ts
1271
- function defaultBlockAt(match) {
1272
- for (let i = 0; i < match.edgeCount; i++) {
1273
- const { type } = match.edge(i);
1274
- if (type.isTextblock && !type.hasRequiredAttrs()) return type;
1275
- }
1276
- return null;
1277
- }
1278
-
1279
1299
  // src/utils/get-id.ts
1280
1300
  var id = 0;
1281
1301
  function getId() {
@@ -1378,6 +1398,7 @@ export {
1378
1398
  getNodeType,
1379
1399
  htmlFromJSON,
1380
1400
  htmlFromNode,
1401
+ insertDefaultBlock,
1381
1402
  insertNode,
1382
1403
  isAllSelection,
1383
1404
  isApple,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.7.8",
4
+ "version": "0.7.10",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -45,13 +45,14 @@
45
45
  "just-map-values": "^3.2.0",
46
46
  "orderedmap": "^2.1.1",
47
47
  "prosemirror-splittable": "^0.1.1",
48
- "type-fest": "^4.23.0",
48
+ "type-fest": "^4.25.0",
49
49
  "@prosekit/pm": "^0.1.8"
50
50
  },
51
51
  "devDependencies": {
52
- "tsup": "^8.2.3",
53
- "typescript": "^5.5.3",
54
- "vitest": "^2.0.4",
52
+ "@vitest/browser": "^2.0.5",
53
+ "tsup": "^8.2.4",
54
+ "typescript": "^5.5.4",
55
+ "vitest": "^2.0.5",
55
56
  "@prosekit/dev": "0.0.0"
56
57
  },
57
58
  "scripts": {