@prosekit/core 0.0.14 → 0.0.15

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
  }>;
@@ -380,8 +386,10 @@ declare class Facet<Input, Output> {
380
386
  readonly converter: () => FacetConverter<Input, Output>;
381
387
  /** @internal */
382
388
  readonly next: Facet<Output, any> | null;
389
+ /** @internal */
390
+ readonly singleton: boolean;
383
391
  private constructor();
384
- static define<Input, Output>({ converter: converter, convert: convert, next, }: FacetOptions<Input, Output>): Facet<Input, Output>;
392
+ static define<Input, Output>({ converter: converter, convert: convert, next, singleton, }: FacetOptions<Input, Output>): Facet<Input, Output>;
385
393
  /** @internal */
386
394
  static defineRootFacet<Input>(options: Omit<FacetOptions<Input, Input>, 'next'>): Facet<Input, Input>;
387
395
  extension(payloads: Input[]): FacetExtension<Input, Output>;
@@ -410,10 +418,13 @@ declare interface FacetOptions<Input, Output> {
410
418
  convert?: (payloads: Input[]) => Output;
411
419
  converter?: () => FacetConverter<Input, Output>;
412
420
  next: Facet<Output, any>;
421
+ singleton?: boolean;
413
422
  }
414
423
  export { FacetOptions }
415
424
  export { FacetOptions as FacetOptions_alias_1 }
416
425
 
426
+ export declare function getCustomSelection(state: EditorState, from?: number | null, to?: number | null): Selection_2;
427
+
417
428
  export declare function getFacetCount(): number;
418
429
 
419
430
  /** @internal */
@@ -596,12 +607,18 @@ declare interface SelectionJson {
596
607
  export { SelectionJson }
597
608
  export { SelectionJson as SelectionJson_alias_1 }
598
609
 
599
- export declare function setBlockType({ nodeType, attrs, from, to, }: {
600
- nodeType: NodeType;
610
+ /**
611
+ * Returns a command that tries to set the selected textblocks to the given node
612
+ * type with the given attributes.
613
+ */
614
+ declare function setBlockType(options: {
615
+ type: NodeType | string;
601
616
  attrs?: Attrs | null;
602
617
  from?: number;
603
618
  to?: number;
604
619
  }): Command;
620
+ export { setBlockType }
621
+ export { setBlockType as setBlockType_alias_1 }
605
622
 
606
623
  /**
607
624
  * @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
  }
@@ -983,7 +1047,8 @@ var eventFacet = Facet.define({
983
1047
  }
984
1048
  };
985
1049
  },
986
- next: pluginFacet
1050
+ next: pluginFacet,
1051
+ singleton: true
987
1052
  });
988
1053
  var pluginKey = new PluginKey("prosekit-event-handler");
989
1054
 
@@ -1027,7 +1092,8 @@ var keymapFacet = Facet.define({
1027
1092
  }
1028
1093
  };
1029
1094
  },
1030
- next: pluginFacet
1095
+ next: pluginFacet,
1096
+ singleton: true
1031
1097
  });
1032
1098
  function mergeKeymaps(keymaps) {
1033
1099
  const bindings = {};
@@ -1201,6 +1267,7 @@ export {
1201
1267
  getNodeType,
1202
1268
  insertNode,
1203
1269
  pluginFacet,
1270
+ setBlockType,
1204
1271
  toggleMark,
1205
1272
  toggleNode,
1206
1273
  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.15",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
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,