@prosekit/core 0.5.5 → 0.6.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.
@@ -27,6 +27,7 @@ import { Options } from 'tsup';
27
27
  import { Plugin as Plugin_2 } from '@prosekit/pm/state';
28
28
  import { ProseMirrorFragment } from '@prosekit/pm/model';
29
29
  import { ProseMirrorNode } from '@prosekit/pm/model';
30
+ import type { ResolvedPos } from '@prosekit/pm/model';
30
31
  import { Schema } from '@prosekit/pm/model';
31
32
  import { SchemaSpec } from '@prosekit/pm/model';
32
33
  import { Selection as Selection_2 } from '@prosekit/pm/state';
@@ -257,47 +258,51 @@ declare function defineBaseCommands(): Extension<{
257
258
  Commands: {
258
259
  insertText: [{
259
260
  text: string;
260
- from?: number | undefined;
261
- to?: number | undefined;
261
+ from?: number;
262
+ to?: number;
262
263
  }];
263
264
  insertNode: [options: {
264
265
  node: Node_2;
265
- pos?: number | undefined;
266
+ pos?: number;
266
267
  type?: undefined;
267
268
  attrs?: undefined;
268
269
  } | {
269
270
  node?: undefined;
270
- pos?: number | undefined;
271
+ pos?: number;
271
272
  type: string;
272
- attrs?: Attrs_2 | undefined;
273
+ attrs?: Attrs_2;
274
+ }];
275
+ removeNode: [options: {
276
+ type: string | NodeType_2;
277
+ pos?: number;
273
278
  }];
274
279
  wrap: [{
275
280
  nodeType: NodeType_2;
276
- attrs?: Attrs_2 | null | undefined;
281
+ attrs?: Attrs_2 | null;
277
282
  }];
278
283
  setBlockType: [options: {
279
- type: string | NodeType_2;
280
- attrs?: Attrs_2 | null | undefined;
281
- from?: number | undefined;
282
- to?: number | undefined;
284
+ type: NodeType_2 | string;
285
+ attrs?: Attrs_2 | null;
286
+ from?: number;
287
+ to?: number;
283
288
  }];
284
289
  setNodeAttrs: [options: {
285
290
  type: string | NodeType_2 | string[] | NodeType_2[];
286
291
  attrs: Attrs_2;
287
- pos?: number | undefined;
292
+ pos?: number;
288
293
  }];
289
294
  selectAll: [];
290
295
  addMark: [options: {
291
296
  type: string | MarkType_2;
292
- attrs?: Attrs_2 | null | undefined;
293
- from?: number | undefined;
294
- to?: number | undefined;
297
+ attrs?: Attrs_2 | null;
298
+ from?: number;
299
+ to?: number;
295
300
  }];
296
301
  removeMark: [options: {
297
302
  type: string | MarkType_2;
298
- attrs?: Attrs_2 | null | undefined;
299
- from?: number | undefined;
300
- to?: number | undefined;
303
+ attrs?: Attrs_2 | null;
304
+ from?: number;
305
+ to?: number;
301
306
  }];
302
307
  };
303
308
  Nodes: never;
@@ -987,9 +992,9 @@ export declare class FacetNode<I = any, O = any> {
987
992
  readonly facet: Facet<I, O>;
988
993
  readonly inputs: Tuple5<I[] | null>;
989
994
  readonly children: Map<number, FacetNode>;
990
- reducers: Tuple5<FacetReducer<I, O> | null>;
995
+ readonly reducers: Tuple5<FacetReducer<I, O> | null>;
991
996
  output: Tuple5<O | null> | null;
992
- constructor(facet: Facet<I, O>, inputs?: Tuple5<I[] | null>, children?: Map<number, FacetNode>);
997
+ constructor(facet: Facet<I, O>, inputs?: Tuple5<I[] | null>, children?: Map<number, FacetNode>, reducers?: Tuple5<FacetReducer<I, O> | null>);
993
998
  private calcOutput;
994
999
  getOutput(): Tuple5<O | null>;
995
1000
  getSingletonOutput(): O | null;
@@ -1010,6 +1015,11 @@ export declare function findBrowserWindow(options?: {
1010
1015
  document?: Document;
1011
1016
  }): (Window & typeof globalThis) | null | undefined;
1012
1017
 
1018
+ export declare function findParentNode(nodeType: NodeType, $pos: ResolvedPos): {
1019
+ from: number | null;
1020
+ to: number | null;
1021
+ };
1022
+
1013
1023
  /**
1014
1024
  * A function that is called when the editor gains or loses focus.
1015
1025
  *
@@ -1558,6 +1568,24 @@ declare function removeMark(options: {
1558
1568
  export { removeMark }
1559
1569
  export { removeMark as removeMark_alias_1 }
1560
1570
 
1571
+ /**
1572
+ * Returns a command to remove the nearest ancestor node of a specific type from the current position.
1573
+ *
1574
+ * @public
1575
+ */
1576
+ declare function removeNode(options: {
1577
+ /**
1578
+ * The type of the node to remove.
1579
+ */
1580
+ type: string | NodeType;
1581
+ /**
1582
+ * The document position to start searching node. By default it will be the anchor position of current selection.
1583
+ */
1584
+ pos?: number;
1585
+ }): Command;
1586
+ export { removeNode }
1587
+ export { removeNode as removeNode_alias_1 }
1588
+
1561
1589
  export declare const rootFacet: Facet<RootPayload, RootOutput>;
1562
1590
 
1563
1591
  export declare type RootOutput = {
@@ -1652,7 +1680,7 @@ export declare function setSelectionAround(tr: Transaction, pos: number): void;
1652
1680
  export declare type Setter<T> = (value: T) => void;
1653
1681
 
1654
1682
  /**
1655
- * @intneral
1683
+ * @internal
1656
1684
  */
1657
1685
  declare type SimplifyUnion<T> = Simplify<UnionToIntersection<T>>;
1658
1686
  export { SimplifyUnion }
@@ -1691,6 +1719,14 @@ export declare type StatePayload = (ctx: {
1691
1719
  schema: Schema;
1692
1720
  }) => EditorStateConfig;
1693
1721
 
1722
+ /**
1723
+ * Takes two facet nodes and returns a new facet node containing inputs and
1724
+ * children from the first node but not the second.
1725
+ *
1726
+ * The reducers of the first facet node will be reused.
1727
+ *
1728
+ * @internal
1729
+ */
1694
1730
  export declare function subtractFacetNode<I, O>(a: FacetNode<I, O>, b: FacetNode<I, O>): FacetNode<I, O>;
1695
1731
 
1696
1732
  declare type TextInputHandler = (view: EditorView, from: number, to: number, text: string) => boolean | void;
@@ -1748,6 +1784,8 @@ export declare type Tuple5<T> = [T, T, T, T, T];
1748
1784
  /**
1749
1785
  * Merge multiple extensions into one.
1750
1786
  *
1787
+ * @throws If no extensions are provided.
1788
+ *
1751
1789
  * @public
1752
1790
  */
1753
1791
  declare function union<E extends Extension | Extension[]>(extension: E): UnionExtension<E>;
@@ -1777,6 +1815,14 @@ export declare class UnionExtensionImpl<T extends ExtensionTyping = ExtensionTyp
1777
1815
  createTree(priority: Priority): FacetNode;
1778
1816
  }
1779
1817
 
1818
+ /**
1819
+ * Takes two facet nodes and returns a new facet node containing inputs and
1820
+ * children from both nodes.
1821
+ *
1822
+ * The reducers of the first facet node will be reused.
1823
+ *
1824
+ * @internal
1825
+ */
1780
1826
  export declare function unionFacetNode<I, O>(a: FacetNode<I, O>, b: FacetNode<I, O>): FacetNode<I, O>;
1781
1827
 
1782
1828
  export declare function uniqPush<T>(prev: readonly T[], next: readonly T[]): T[];
@@ -1,6 +1,7 @@
1
1
  export { addMark } from './_tsup-dts-rollup';
2
2
  export { expandMark } from './_tsup-dts-rollup';
3
3
  export { insertNode } from './_tsup-dts-rollup';
4
+ export { removeNode } from './_tsup-dts-rollup';
4
5
  export { removeMark } from './_tsup-dts-rollup';
5
6
  export { setBlockType } from './_tsup-dts-rollup';
6
7
  export { setNodeAttrs } from './_tsup-dts-rollup';
@@ -117,6 +117,30 @@ function insertNode(options) {
117
117
  };
118
118
  }
119
119
 
120
+ // src/utils/find-parent-node.ts
121
+ function findParentNode(nodeType, $pos) {
122
+ for (let depth = $pos.depth; depth > 0; depth -= 1)
123
+ if ($pos.node(depth).type === nodeType) {
124
+ let from = $pos.before(depth), to = $pos.after(depth);
125
+ return {
126
+ from,
127
+ to
128
+ };
129
+ }
130
+ return {
131
+ from: null,
132
+ to: null
133
+ };
134
+ }
135
+
136
+ // src/commands/remove-node.ts
137
+ function removeNode(options) {
138
+ return (state, dispatch) => {
139
+ let nodeType = getNodeType(state.schema, options.type), $pos = typeof options.pos == "number" ? state.doc.resolve(options.pos) : state.selection.$anchor, { from, to } = findParentNode(nodeType, $pos);
140
+ return from == null || to == null || from > to ? !1 : (dispatch == null || dispatch(state.tr.delete(from, to)), !0);
141
+ };
142
+ }
143
+
120
144
  // src/commands/remove-mark.ts
121
145
  import "@prosekit/pm/model";
122
146
  import "@prosekit/pm/state";
@@ -334,6 +358,11 @@ function toReversed(arr) {
334
358
  return (_b = (_a = arr.toReversed) == null ? void 0 : _a.call(arr)) != null ? _b : [...arr].reverse();
335
359
  }
336
360
 
361
+ // src/utils/is-not-null.ts
362
+ function isNotNull(value) {
363
+ return value != null;
364
+ }
365
+
337
366
  // src/facets/facet-node.ts
338
367
  function zip5(a, b, mapper) {
339
368
  return [
@@ -370,22 +399,30 @@ function unionFacetNode(a, b) {
370
399
  return assert(a.facet === b.facet), new FacetNode(
371
400
  a.facet,
372
401
  zip5(a.inputs, b.inputs, unionInput),
373
- unionChildren(a.children, b.children)
402
+ unionChildren(a.children, b.children),
403
+ a.reducers
374
404
  );
375
405
  }
376
406
  function subtractFacetNode(a, b) {
377
407
  return assert(a.facet === b.facet), new FacetNode(
378
408
  a.facet,
379
409
  zip5(a.inputs, b.inputs, subtractInput),
380
- subtractChildren(a.children, b.children)
410
+ subtractChildren(a.children, b.children),
411
+ a.reducers
381
412
  );
382
413
  }
383
414
  var FacetNode = class {
384
- constructor(facet, inputs = [null, null, null, null, null], children = /* @__PURE__ */ new Map()) {
415
+ constructor(facet, inputs = [null, null, null, null, null], children = /* @__PURE__ */ new Map(), reducers = [
416
+ null,
417
+ null,
418
+ null,
419
+ null,
420
+ null
421
+ ]) {
385
422
  this.facet = facet;
386
423
  this.inputs = inputs;
387
424
  this.children = children;
388
- this.reducers = [null, null, null, null, null];
425
+ this.reducers = reducers;
389
426
  this.output = null;
390
427
  }
391
428
  calcOutput() {
@@ -401,7 +438,7 @@ var FacetNode = class {
401
438
  childOutput[pri] && (inputs[pri] || (inputs[pri] = [])).push(childOutput[pri]);
402
439
  }
403
440
  if (this.facet.singleton) {
404
- let reducer = (_a = this.reducers)[_b = 2 /* default */] || (_a[_b] = this.facet.reducer), input = inputs.filter(Boolean).flat();
441
+ let reducer = (_a = this.reducers)[_b = 2 /* default */] || (_a[_b] = this.facet.reducer), input = inputs.filter(isNotNull).flat();
405
442
  output[2 /* default */] = reducer(input);
406
443
  } else
407
444
  for (let pri = 0; pri < 5; pri++) {
@@ -797,17 +834,23 @@ var UnionExtensionImpl = class extends BaseExtension {
797
834
  createTree(priority) {
798
835
  var _a;
799
836
  let pri = (_a = this.priority) != null ? _a : priority, extensions = [...this.extension];
800
- return extensions.sort((a, b) => {
837
+ extensions.sort((a, b) => {
801
838
  var _a2, _b;
802
839
  return ((_a2 = a.priority) != null ? _a2 : pri) - ((_b = b.priority) != null ? _b : pri);
803
- }), extensions.map((ext) => ext.getTree(pri)).reduce(unionFacetNode, new FacetNode(rootFacet));
840
+ });
841
+ let children = extensions.map((ext) => ext.getTree(pri));
842
+ assert(children.length > 0);
843
+ let node = children[0];
844
+ for (let i = 1; i < children.length; i++)
845
+ node = unionFacetNode(node, children[i]);
846
+ return node;
804
847
  }
805
848
  };
806
849
 
807
850
  // src/editor/union.ts
808
851
  function union(extension) {
809
852
  let array = Array.isArray(extension) ? extension : [extension];
810
- return new UnionExtensionImpl(
853
+ return assert(array.length > 0, "At least one extension is required"), new UnionExtensionImpl(
811
854
  array
812
855
  );
813
856
  }
@@ -1062,6 +1105,7 @@ function defineBaseCommands() {
1062
1105
  return defineCommands({
1063
1106
  insertText,
1064
1107
  insertNode,
1108
+ removeNode,
1065
1109
  wrap,
1066
1110
  setBlockType,
1067
1111
  setNodeAttrs,
@@ -1095,11 +1139,6 @@ function isElement(value) {
1095
1139
  return hasElement && value instanceof Element;
1096
1140
  }
1097
1141
 
1098
- // src/utils/is-not-null.ts
1099
- function isNotNull(value) {
1100
- return value != null;
1101
- }
1102
-
1103
1142
  // src/extensions/node-spec.ts
1104
1143
  function defineNodeSpec(options) {
1105
1144
  return defineFacetPayload(nodeSpecFacet, [[options, void 0]]);
@@ -1758,6 +1797,7 @@ export {
1758
1797
  nodeFromJSON,
1759
1798
  pluginFacet,
1760
1799
  removeMark,
1800
+ removeNode,
1761
1801
  setBlockType,
1762
1802
  setNodeAttrs,
1763
1803
  stateFromJSON,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.5.5",
4
+ "version": "0.6.0",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -38,12 +38,12 @@
38
38
  "clsx": "^2.1.1",
39
39
  "orderedmap": "^2.1.1",
40
40
  "prosemirror-splittable": "^0.1.1",
41
- "type-fest": "^4.19.0",
41
+ "type-fest": "^4.20.1",
42
42
  "@prosekit/pm": "^0.1.5"
43
43
  },
44
44
  "devDependencies": {
45
45
  "tsup": "^8.1.0",
46
- "typescript": "^5.4.5",
46
+ "typescript": "^5.5.2",
47
47
  "vitest": "^1.6.0",
48
48
  "@prosekit/dev": "0.0.0"
49
49
  },