@prosekit/core 0.0.14 → 0.0.16

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,7 @@ import { Attrs as Attrs_2 } from 'prosemirror-model';
3
3
  import { Command } from '@prosekit/pm/state';
4
4
  import type { ConditionalExcept } from 'type-fest';
5
5
  import type { DirectEditorProps } from '@prosekit/pm/view';
6
- import type { EditorState } from '@prosekit/pm/state';
6
+ import { EditorState } from '@prosekit/pm/state';
7
7
  import type { EditorStateConfig } from '@prosekit/pm/state';
8
8
  import { EditorView } from '@prosekit/pm/view';
9
9
  import type { EmptyObject } from 'type-fest';
@@ -23,6 +23,7 @@ import { ProseMirrorFragment } from '@prosekit/pm/model';
23
23
  import { ProseMirrorNode } from '@prosekit/pm/model';
24
24
  import { Schema } from '@prosekit/pm/model';
25
25
  import type { SchemaSpec } from '@prosekit/pm/model';
26
+ import { Selection as Selection_2 } from 'prosemirror-state';
26
27
  import type { Simplify } from 'type-fest';
27
28
  import { Transaction } from '@prosekit/pm/state';
28
29
  import type { UnionToIntersection } from 'type-fest';
@@ -129,7 +130,12 @@ declare function defineBaseCommands(): Extension<{
129
130
  nodeType: NodeType_2;
130
131
  attrs?: Attrs_2 | null | undefined;
131
132
  }];
132
- setBlockType: [nodeType: NodeType_2, attrs?: Attrs_2 | null | undefined];
133
+ setBlockType: [options: {
134
+ type: string | NodeType_2;
135
+ attrs?: Attrs_2 | null | undefined;
136
+ from?: number | undefined;
137
+ to?: number | undefined;
138
+ }];
133
139
  selectAll: [];
134
140
  };
135
141
  }>;
@@ -221,13 +227,26 @@ declare function defineNodeViewEffect(options: NodeViewEffectOptions): Extension
221
227
  export { defineNodeViewEffect }
222
228
  export { defineNodeViewEffect as defineNodeViewEffect_alias_1 }
223
229
 
224
- /** @public */
230
+ /**
231
+ * @public
232
+ *
233
+ * Defines a paragraph node spec as the highest priority, because it should be the default block node for most cases.
234
+ */
225
235
  declare function defineParagraph(): Extension< {
226
236
  NODES: "paragraph";
227
237
  }>;
228
238
  export { defineParagraph }
229
239
  export { defineParagraph as defineParagraph_alias_1 }
230
240
 
241
+ /**
242
+ * @public
243
+ *
244
+ * Defines a paragraph node spec.
245
+ */
246
+ export declare function defineParagraphSpec(): Extension< {
247
+ NODES: "paragraph";
248
+ }>;
249
+
231
250
  /**
232
251
  * Adds a ProseMirror plugin to the editor.
233
252
  *
@@ -380,8 +399,10 @@ declare class Facet<Input, Output> {
380
399
  readonly converter: () => FacetConverter<Input, Output>;
381
400
  /** @internal */
382
401
  readonly next: Facet<Output, any> | null;
402
+ /** @internal */
403
+ readonly singleton: boolean;
383
404
  private constructor();
384
- static define<Input, Output>({ converter: converter, convert: convert, next, }: FacetOptions<Input, Output>): Facet<Input, Output>;
405
+ static define<Input, Output>({ converter: converter, convert: convert, next, singleton, }: FacetOptions<Input, Output>): Facet<Input, Output>;
385
406
  /** @internal */
386
407
  static defineRootFacet<Input>(options: Omit<FacetOptions<Input, Input>, 'next'>): Facet<Input, Input>;
387
408
  extension(payloads: Input[]): FacetExtension<Input, Output>;
@@ -410,10 +431,13 @@ declare interface FacetOptions<Input, Output> {
410
431
  convert?: (payloads: Input[]) => Output;
411
432
  converter?: () => FacetConverter<Input, Output>;
412
433
  next: Facet<Output, any>;
434
+ singleton?: boolean;
413
435
  }
414
436
  export { FacetOptions }
415
437
  export { FacetOptions as FacetOptions_alias_1 }
416
438
 
439
+ export declare function getCustomSelection(state: EditorState, from?: number | null, to?: number | null): Selection_2;
440
+
417
441
  export declare function getFacetCount(): number;
418
442
 
419
443
  /** @internal */
@@ -596,12 +620,18 @@ declare interface SelectionJson {
596
620
  export { SelectionJson }
597
621
  export { SelectionJson as SelectionJson_alias_1 }
598
622
 
599
- export declare function setBlockType({ nodeType, attrs, from, to, }: {
600
- nodeType: NodeType;
623
+ /**
624
+ * Returns a command that tries to set the selected textblocks to the given node
625
+ * type with the given attributes.
626
+ */
627
+ declare function setBlockType(options: {
628
+ type: NodeType | string;
601
629
  attrs?: Attrs | null;
602
630
  from?: number;
603
631
  to?: number;
604
632
  }): Command;
633
+ export { setBlockType }
634
+ export { setBlockType as setBlockType_alias_1 }
605
635
 
606
636
  /**
607
637
  * @internal
@@ -1,13 +1,11 @@
1
1
  export { addMark } from './_tsup-dts-rollup';
2
- export { toggleMark } from './_tsup-dts-rollup';
3
2
  export { insertNode } from './_tsup-dts-rollup';
3
+ export { setBlockType } from './_tsup-dts-rollup';
4
+ export { toggleMark } from './_tsup-dts-rollup';
4
5
  export { toggleNode } from './_tsup-dts-rollup';
5
6
  export { Editor } from './_tsup-dts-rollup';
6
7
  export { createEditor } from './_tsup-dts-rollup';
7
8
  export { EditorOptions } from './_tsup-dts-rollup';
8
- export { Facet } from './_tsup-dts-rollup';
9
- export { FacetExtension } from './_tsup-dts-rollup';
10
- export { FacetOptions } from './_tsup-dts-rollup';
11
9
  export { union } from './_tsup-dts-rollup';
12
10
  export { withPriority } from './_tsup-dts-rollup';
13
11
  export { ProseKitError_alias_1 as ProseKitError } from './_tsup-dts-rollup';
@@ -35,6 +33,9 @@ export { definePlugin } from './_tsup-dts-rollup';
35
33
  export { pluginFacet } from './_tsup-dts-rollup';
36
34
  export { PluginPayload } from './_tsup-dts-rollup';
37
35
  export { defineText } from './_tsup-dts-rollup';
36
+ export { Facet } from './_tsup-dts-rollup';
37
+ export { FacetExtension } from './_tsup-dts-rollup';
38
+ export { FacetOptions } from './_tsup-dts-rollup';
38
39
  export { CommandArgs } from './_tsup-dts-rollup';
39
40
  export { Extension } from './_tsup-dts-rollup';
40
41
  export { ExtractCommandAppliers } from './_tsup-dts-rollup';
@@ -36,23 +36,6 @@ function addMark(options) {
36
36
  };
37
37
  }
38
38
 
39
- // src/commands/toggle-mark.ts
40
- import { toggleMark as baseToggleMark } from "@prosekit/pm/commands";
41
- import "@prosekit/pm/model";
42
- import "@prosekit/pm/state";
43
- function toggleMark({
44
- type,
45
- attrs
46
- }) {
47
- return (state, dispatch, view) => {
48
- return baseToggleMark(getMarkType(state.schema, type), attrs)(
49
- state,
50
- dispatch,
51
- view
52
- );
53
- };
54
- }
55
-
56
39
  // src/commands/insert-node.ts
57
40
  import { insertPoint } from "@prosekit/pm/transform";
58
41
 
@@ -92,8 +75,82 @@ function insertNode(options) {
92
75
  };
93
76
  }
94
77
 
78
+ // src/commands/set-block-type.ts
79
+ import "@prosekit/pm/state";
80
+
81
+ // src/utils/get-custom-selection.ts
82
+ import { TextSelection } from "@prosekit/pm/state";
83
+ function getCustomSelection(state, from, to) {
84
+ const pos = from != null ? from : to;
85
+ if (pos != null) {
86
+ const $from = state.doc.resolve(from != null ? from : pos);
87
+ const $to = state.doc.resolve(to != null ? to : pos);
88
+ return TextSelection.between($from, $to);
89
+ }
90
+ return state.selection;
91
+ }
92
+
93
+ // src/commands/set-block-type.ts
94
+ function setBlockType(options) {
95
+ return (state, dispatch) => {
96
+ const nodeType = getNodeType(state.schema, options.type);
97
+ const selection = getCustomSelection(state, options.from, options.to);
98
+ const attrs = options.attrs;
99
+ let applicable = false;
100
+ for (let i = 0; i < selection.ranges.length && !applicable; i++) {
101
+ const {
102
+ $from: { pos: from },
103
+ $to: { pos: to }
104
+ } = selection.ranges[i];
105
+ state.doc.nodesBetween(from, to, (node, pos) => {
106
+ if (applicable)
107
+ return false;
108
+ if (!node.isTextblock || node.hasMarkup(nodeType, attrs))
109
+ return;
110
+ if (node.type == nodeType) {
111
+ applicable = true;
112
+ } else {
113
+ const $pos = state.doc.resolve(pos), index = $pos.index();
114
+ applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType);
115
+ }
116
+ });
117
+ }
118
+ if (!applicable)
119
+ return false;
120
+ if (dispatch) {
121
+ const tr = state.tr;
122
+ for (const range of selection.ranges) {
123
+ const {
124
+ $from: { pos: from },
125
+ $to: { pos: to }
126
+ } = range;
127
+ tr.setBlockType(from, to, nodeType, attrs);
128
+ }
129
+ dispatch(tr.scrollIntoView());
130
+ }
131
+ return true;
132
+ };
133
+ }
134
+
135
+ // src/commands/toggle-mark.ts
136
+ import { toggleMark as baseToggleMark } from "@prosekit/pm/commands";
137
+ import "@prosekit/pm/model";
138
+ import "@prosekit/pm/state";
139
+ function toggleMark({
140
+ type,
141
+ attrs
142
+ }) {
143
+ return (state, dispatch, view) => {
144
+ return baseToggleMark(getMarkType(state.schema, type), attrs)(
145
+ state,
146
+ dispatch,
147
+ view
148
+ );
149
+ };
150
+ }
151
+
95
152
  // src/commands/toggle-node.ts
96
- import { setBlockType } from "@prosekit/pm/commands";
153
+ import { setBlockType as setBlockType2 } from "@prosekit/pm/commands";
97
154
  import "@prosekit/pm/model";
98
155
  import "@prosekit/pm/state";
99
156
 
@@ -151,10 +208,10 @@ function toggleNode({
151
208
  if (!defaultType) {
152
209
  return false;
153
210
  }
154
- return setBlockType(defaultType)(state, dispatch, view);
211
+ return setBlockType2(defaultType)(state, dispatch, view);
155
212
  } else {
156
213
  const nodeType = getNodeType(state.schema, type);
157
- return setBlockType(nodeType, attrs)(state, dispatch, view);
214
+ return setBlockType2(nodeType, attrs)(state, dispatch, view);
158
215
  }
159
216
  };
160
217
  }
@@ -194,16 +251,18 @@ function getFacetCount() {
194
251
  return facetCount;
195
252
  }
196
253
  var Facet = class _Facet {
197
- constructor(converter, next) {
254
+ constructor(converter, next, singleton) {
198
255
  /** @internal */
199
256
  this.index = facetCount++;
200
257
  this.converter = converter;
201
258
  this.next = next;
259
+ this.singleton = singleton;
202
260
  }
203
261
  static define({
204
262
  converter,
205
263
  convert,
206
- next
264
+ next,
265
+ singleton
207
266
  }) {
208
267
  const converterFunction = converter ? converter : convert ? () => ({
209
268
  create: convert,
@@ -212,7 +271,7 @@ var Facet = class _Facet {
212
271
  if (!converterFunction) {
213
272
  throw new ProseKitError("Facet must have either 'convert' or 'converter'");
214
273
  }
215
- return new _Facet(converterFunction, next);
274
+ return new _Facet(converterFunction, next, singleton != null ? singleton : false);
216
275
  }
217
276
  /** @internal */
218
277
  static defineRootFacet(options) {
@@ -509,7 +568,7 @@ function updateExtension(prevInputs, prevConverters, extension, mode) {
509
568
  }
510
569
  const inputTuple = modifyInputTuple(prevInputs[index], inputs[index]);
511
570
  prevInputs[index] = inputTuple;
512
- if (facet.next) {
571
+ if (facet.next && !facet.singleton) {
513
572
  let hasOutput = false;
514
573
  const outputTuple = [[], [], [], [], []];
515
574
  for (let pri = 0; pri < 5; pri++) {
@@ -544,35 +603,43 @@ function updateExtension(prevInputs, prevConverters, extension, mode) {
544
603
  continue;
545
604
  } else {
546
605
  const inputArray = flattenInputTuple(inputTuple);
547
- const converterTuple = prevConverters[index] || (prevConverters[index] = [
606
+ prevConverters[index] || (prevConverters[index] = [
548
607
  void 0,
549
608
  void 0,
550
609
  void 0,
551
610
  void 0,
552
611
  void 0
553
612
  ]);
554
- const prevConverter = converterTuple[2 /* default */];
613
+ const prevConverter = prevConverters[index][2 /* default */];
555
614
  const converter = prevConverter || facet.converter();
556
615
  prevConverters[index][2 /* default */] = converter;
557
616
  const output = prevConverter ? converter.update(inputArray) : converter.create(inputArray);
558
617
  if (!output) {
559
618
  continue;
560
619
  }
561
- switch (facet) {
562
- case schemaFacet:
563
- schemaInput = output;
564
- break;
565
- case stateFacet:
566
- stateInput = output;
567
- break;
568
- case viewFacet:
569
- viewInput = output;
570
- break;
571
- case commandFacet:
572
- commandInput = output;
573
- break;
574
- default:
575
- throw new ProseKitError("Invalid root facet");
620
+ if (facet.next) {
621
+ const outputTuple = [[], [], [output], [], []];
622
+ inputs[facet.next.index] = modifyInputTuple(
623
+ inputs[facet.next.index],
624
+ outputTuple
625
+ );
626
+ } else {
627
+ switch (facet) {
628
+ case schemaFacet:
629
+ schemaInput = output;
630
+ break;
631
+ case stateFacet:
632
+ stateInput = output;
633
+ break;
634
+ case viewFacet:
635
+ viewInput = output;
636
+ break;
637
+ case commandFacet:
638
+ commandInput = output;
639
+ break;
640
+ default:
641
+ throw new ProseKitError("Invalid root facet");
642
+ }
576
643
  }
577
644
  }
578
645
  }
@@ -808,9 +875,6 @@ function withPriority(extension, priority) {
808
875
  return { extension, priority };
809
876
  }
810
877
 
811
- // src/extensions/command.ts
812
- import { setBlockType as setBlockType2 } from "@prosekit/pm/commands";
813
-
814
878
  // src/commands/insert-text.ts
815
879
  function insertText({
816
880
  text,
@@ -884,7 +948,7 @@ function defineBaseCommands() {
884
948
  insertText,
885
949
  insertNode: insertNode2,
886
950
  wrap,
887
- setBlockType: setBlockType2,
951
+ setBlockType,
888
952
  selectAll
889
953
  });
890
954
  }
@@ -915,7 +979,8 @@ var nodeSpecFacet = Facet.define({
915
979
  function defineDoc() {
916
980
  return defineNodeSpec({
917
981
  name: "doc",
918
- content: "block+"
982
+ content: "block+",
983
+ topNode: true
919
984
  });
920
985
  }
921
986
 
@@ -983,7 +1048,8 @@ var eventFacet = Facet.define({
983
1048
  }
984
1049
  };
985
1050
  },
986
- next: pluginFacet
1051
+ next: pluginFacet,
1052
+ singleton: true
987
1053
  });
988
1054
  var pluginKey = new PluginKey("prosekit-event-handler");
989
1055
 
@@ -1027,7 +1093,8 @@ var keymapFacet = Facet.define({
1027
1093
  }
1028
1094
  };
1029
1095
  },
1030
- next: pluginFacet
1096
+ next: pluginFacet,
1097
+ singleton: true
1031
1098
  });
1032
1099
  function mergeKeymaps(keymaps) {
1033
1100
  const bindings = {};
@@ -1154,7 +1221,7 @@ var nodeViewEffectFacet = Facet.define({
1154
1221
  });
1155
1222
 
1156
1223
  // src/extensions/paragraph.ts
1157
- function defineParagraph() {
1224
+ function defineParagraphSpec() {
1158
1225
  return defineNodeSpec({
1159
1226
  name: "paragraph",
1160
1227
  content: "inline*",
@@ -1165,6 +1232,9 @@ function defineParagraph() {
1165
1232
  }
1166
1233
  });
1167
1234
  }
1235
+ function defineParagraph() {
1236
+ return withPriority(defineParagraphSpec(), 0 /* highest */);
1237
+ }
1168
1238
 
1169
1239
  // src/extensions/text.ts
1170
1240
  function defineText() {
@@ -1201,6 +1271,7 @@ export {
1201
1271
  getNodeType,
1202
1272
  insertNode,
1203
1273
  pluginFacet,
1274
+ setBlockType,
1204
1275
  toggleMark,
1205
1276
  toggleNode,
1206
1277
  union,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/core",
3
3
  "type": "module",
4
- "version": "0.0.14",
4
+ "version": "0.0.16",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -40,7 +40,7 @@
40
40
  "dependencies": {
41
41
  "@prosekit/pm": "^0.0.6",
42
42
  "orderedmap": "^2.1.1",
43
- "type-fest": "^4.5.0"
43
+ "type-fest": "^4.6.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@prosekit/dev": "*",
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  export { addMark } from './commands/add-mark'
2
- export { toggleMark } from './commands/toggle-mark'
3
2
  export { insertNode } from './commands/insert-node'
3
+ export { setBlockType } from './commands/set-block-type'
4
+ export { toggleMark } from './commands/toggle-mark'
4
5
  export { toggleNode } from './commands/toggle-node'
5
6
  export { Editor, createEditor, type EditorOptions } from './editor/editor'
6
- export { Facet, FacetExtension, type FacetOptions } from './facets/facet'
7
7
  export { union } from './editor/type-utils'
8
8
  export { withPriority } from './editor/with-priority'
9
9
  export { ProseKitError } from './error'
@@ -35,6 +35,7 @@ export {
35
35
  type PluginPayload,
36
36
  } from './extensions/plugin'
37
37
  export { defineText } from './extensions/text'
38
+ export { Facet, FacetExtension, type FacetOptions } from './facets/facet'
38
39
  export { type CommandArgs as CommandArgs } from './types/command'
39
40
  export {
40
41
  type Extension,