@prosekit/core 0.0.0-next-20240605080819 → 0.0.0-next-20240620223217

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';
@@ -271,6 +272,10 @@ declare function defineBaseCommands(): Extension<{
271
272
  type: string;
272
273
  attrs?: Attrs_2 | undefined;
273
274
  }];
275
+ removeNode: [options: {
276
+ type: string | NodeType_2;
277
+ pos?: number | undefined;
278
+ }];
274
279
  wrap: [{
275
280
  nodeType: NodeType_2;
276
281
  attrs?: Attrs_2 | null | undefined;
@@ -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 = {
@@ -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";
@@ -370,22 +394,30 @@ function unionFacetNode(a, b) {
370
394
  return assert(a.facet === b.facet), new FacetNode(
371
395
  a.facet,
372
396
  zip5(a.inputs, b.inputs, unionInput),
373
- unionChildren(a.children, b.children)
397
+ unionChildren(a.children, b.children),
398
+ a.reducers
374
399
  );
375
400
  }
376
401
  function subtractFacetNode(a, b) {
377
402
  return assert(a.facet === b.facet), new FacetNode(
378
403
  a.facet,
379
404
  zip5(a.inputs, b.inputs, subtractInput),
380
- subtractChildren(a.children, b.children)
405
+ subtractChildren(a.children, b.children),
406
+ a.reducers
381
407
  );
382
408
  }
383
409
  var FacetNode = class {
384
- constructor(facet, inputs = [null, null, null, null, null], children = /* @__PURE__ */ new Map()) {
410
+ constructor(facet, inputs = [null, null, null, null, null], children = /* @__PURE__ */ new Map(), reducers = [
411
+ null,
412
+ null,
413
+ null,
414
+ null,
415
+ null
416
+ ]) {
385
417
  this.facet = facet;
386
418
  this.inputs = inputs;
387
419
  this.children = children;
388
- this.reducers = [null, null, null, null, null];
420
+ this.reducers = reducers;
389
421
  this.output = null;
390
422
  }
391
423
  calcOutput() {
@@ -797,17 +829,23 @@ var UnionExtensionImpl = class extends BaseExtension {
797
829
  createTree(priority) {
798
830
  var _a;
799
831
  let pri = (_a = this.priority) != null ? _a : priority, extensions = [...this.extension];
800
- return extensions.sort((a, b) => {
832
+ extensions.sort((a, b) => {
801
833
  var _a2, _b;
802
834
  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));
835
+ });
836
+ let children = extensions.map((ext) => ext.getTree(pri));
837
+ assert(children.length > 0);
838
+ let node = children[0];
839
+ for (let i = 1; i < children.length; i++)
840
+ node = unionFacetNode(node, children[i]);
841
+ return node;
804
842
  }
805
843
  };
806
844
 
807
845
  // src/editor/union.ts
808
846
  function union(extension) {
809
847
  let array = Array.isArray(extension) ? extension : [extension];
810
- return new UnionExtensionImpl(
848
+ return assert(array.length > 0, "At least one extension is required"), new UnionExtensionImpl(
811
849
  array
812
850
  );
813
851
  }
@@ -1062,6 +1100,7 @@ function defineBaseCommands() {
1062
1100
  return defineCommands({
1063
1101
  insertText,
1064
1102
  insertNode,
1103
+ removeNode,
1065
1104
  wrap,
1066
1105
  setBlockType,
1067
1106
  setNodeAttrs,
@@ -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.0.0-next-20240605080819",
4
+ "version": "0.0.0-next-20240620223217",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -38,8 +38,8 @@
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",
42
- "@prosekit/pm": "^0.0.0-next-20240605080819"
41
+ "type-fest": "^4.20.1",
42
+ "@prosekit/pm": "^0.0.0-next-20240620223217"
43
43
  },
44
44
  "devDependencies": {
45
45
  "tsup": "^8.1.0",