@prosekit/core 0.0.0-next-20240505104851 → 0.0.0-next-20240531032056

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.
@@ -107,6 +107,13 @@ function expandMarkAfter($pos, predicate) {
107
107
  import "@prosekit/pm/state";
108
108
  import { insertPoint } from "@prosekit/pm/transform";
109
109
 
110
+ // src/utils/assert.ts
111
+ function assert(condition, message = "Assertion failed") {
112
+ if (!condition) {
113
+ throw new ProseKitError(message);
114
+ }
115
+ }
116
+
110
117
  // src/utils/get-node-type.ts
111
118
  import "@prosekit/pm/model";
112
119
  function getNodeType(schema, type) {
@@ -133,10 +140,8 @@ function setSelectionAround(tr, pos) {
133
140
  function insertNode(options) {
134
141
  return (state, dispatch) => {
135
142
  var _a;
136
- const node = options.node ? options.node : options.type ? getNodeType(state.schema, options.type).createChecked(options.attrs) : null;
137
- if (!node) {
138
- throw new ProseKitError("You must provide either a node or a type");
139
- }
143
+ const node = options.node ? options.node : options.type ? getNodeType(state.schema, options.type).createAndFill(options.attrs) : null;
144
+ assert(node, "You must provide either a node or a type");
140
145
  const insertPos = insertPoint(
141
146
  state.doc,
142
147
  (_a = options.pos) != null ? _a : state.selection.to,
@@ -228,20 +233,38 @@ function setBlockType(options) {
228
233
  };
229
234
  }
230
235
 
236
+ // src/utils/get-node-types.ts
237
+ import "@prosekit/pm/model";
238
+ function getNodeTypes(schema, types) {
239
+ if (Array.isArray(types)) {
240
+ return types.map((type) => getNodeType(schema, type));
241
+ }
242
+ return [getNodeType(schema, types)];
243
+ }
244
+
231
245
  // src/commands/set-node-attrs.ts
232
246
  function setNodeAttrs(options) {
233
247
  return (state, dispatch) => {
234
- var _a;
235
- const nodeType = getNodeType(state.schema, options.type);
236
- const pos = (_a = options.pos) != null ? _a : state.selection.$from.before();
237
- const node = state.doc.nodeAt(pos);
238
- if (!node || node.type !== nodeType) {
248
+ var _a, _b;
249
+ const nodeTypes = getNodeTypes(state.schema, options.type);
250
+ const from = (_a = options.pos) != null ? _a : state.selection.from;
251
+ const to = (_b = options.pos) != null ? _b : state.selection.to;
252
+ const positions = [];
253
+ state.doc.nodesBetween(from, to, (node, pos) => {
254
+ if (nodeTypes.includes(node.type)) {
255
+ positions.push(pos);
256
+ }
257
+ if (!dispatch && positions.length > 0) {
258
+ return false;
259
+ }
260
+ });
261
+ if (positions.length === 0) {
239
262
  return false;
240
263
  }
241
264
  if (dispatch) {
242
265
  const { tr } = state;
243
- for (const [key, value] of Object.entries(options.attrs)) {
244
- if (value !== void 0) {
266
+ for (const pos of positions) {
267
+ for (const [key, value] of Object.entries(options.attrs)) {
245
268
  tr.setNodeAttribute(pos, key, value);
246
269
  }
247
270
  }
@@ -383,14 +406,27 @@ function toggleNode({
383
406
  }
384
407
 
385
408
  // src/editor/editor.ts
386
- import { Schema as Schema6 } from "@prosekit/pm/model";
409
+ import "@prosekit/pm/model";
387
410
  import { EditorState as EditorState2 } from "@prosekit/pm/state";
388
411
  import { EditorView } from "@prosekit/pm/view";
389
412
 
390
413
  // src/extensions/default-state.ts
391
414
  import { Selection } from "@prosekit/pm/state";
392
415
 
393
- // src/utils/uniq-array.ts
416
+ // src/types/priority.ts
417
+ var Priority = /* @__PURE__ */ ((Priority2) => {
418
+ Priority2[Priority2["lowest"] = 0] = "lowest";
419
+ Priority2[Priority2["low"] = 1] = "low";
420
+ Priority2[Priority2["default"] = 2] = "default";
421
+ Priority2[Priority2["high"] = 3] = "high";
422
+ Priority2[Priority2["highest"] = 4] = "highest";
423
+ return Priority2;
424
+ })(Priority || {});
425
+
426
+ // src/facets/base-extension.ts
427
+ import "@prosekit/pm/model";
428
+
429
+ // src/utils/array.ts
394
430
  function uniqPush(prev, next) {
395
431
  const result = [...prev];
396
432
  for (const item of next) {
@@ -400,84 +436,270 @@ function uniqPush(prev, next) {
400
436
  }
401
437
  return result;
402
438
  }
403
- function uniqRemove(prev, next) {
404
- const result = [...prev];
405
- for (const item of next) {
406
- const index = result.indexOf(item);
407
- if (index !== -1) {
408
- result.splice(index, 1);
409
- }
410
- }
411
- return result;
439
+ function arraySubstract(a, b) {
440
+ return a.filter((x) => !b.includes(x));
441
+ }
442
+ function toReversed(arr) {
443
+ var _a, _b;
444
+ return (_b = (_a = arr.toReversed) == null ? void 0 : _a.call(arr)) != null ? _b : [...arr].reverse();
412
445
  }
413
446
 
414
- // src/facets/base-extension.ts
415
- import "@prosekit/pm/model";
416
- var BaseExtension = class {
417
- constructor() {
418
- this.extension = [];
447
+ // src/facets/facet-node.ts
448
+ function zip5(a, b, mapper) {
449
+ return [
450
+ mapper(a[0], b[0]),
451
+ mapper(a[1], b[1]),
452
+ mapper(a[2], b[2]),
453
+ mapper(a[3], b[3]),
454
+ mapper(a[4], b[4])
455
+ ];
456
+ }
457
+ function unionInput(a, b) {
458
+ if (!a && !b)
459
+ return null;
460
+ return uniqPush(a != null ? a : [], b != null ? b : []);
461
+ }
462
+ function subtractInput(a, b) {
463
+ if (!a)
464
+ return null;
465
+ if (!b)
466
+ return [...a];
467
+ return arraySubstract(a, b);
468
+ }
469
+ function unionChildren(a, b) {
470
+ const merged = new Map(a);
471
+ for (const [key, valueB] of b.entries()) {
472
+ const valueA = a.get(key);
473
+ merged.set(key, valueA ? unionFacetNode(valueA, valueB) : valueB);
474
+ }
475
+ return merged;
476
+ }
477
+ function subtractChildren(a, b) {
478
+ const merged = new Map(a);
479
+ for (const [key, valueB] of b.entries()) {
480
+ const valueA = a.get(key);
481
+ if (valueA) {
482
+ merged.set(key, subtractFacetNode(valueA, valueB));
483
+ }
484
+ }
485
+ return merged;
486
+ }
487
+ function unionFacetNode(a, b) {
488
+ assert(a.facet === b.facet);
489
+ return new FacetNode(
490
+ a.facet,
491
+ zip5(a.inputs, b.inputs, unionInput),
492
+ unionChildren(a.children, b.children)
493
+ );
494
+ }
495
+ function subtractFacetNode(a, b) {
496
+ assert(a.facet === b.facet);
497
+ return new FacetNode(
498
+ a.facet,
499
+ zip5(a.inputs, b.inputs, subtractInput),
500
+ subtractChildren(a.children, b.children)
501
+ );
502
+ }
503
+ var FacetNode = class {
504
+ constructor(facet, inputs = [null, null, null, null, null], children = /* @__PURE__ */ new Map()) {
505
+ this.facet = facet;
506
+ this.inputs = inputs;
507
+ this.children = children;
508
+ this.reducers = [null, null, null, null, null];
509
+ this.output = null;
510
+ }
511
+ calcOutput() {
512
+ var _a, _b, _c;
513
+ const inputs = [null, null, null, null, null];
514
+ const output = [null, null, null, null, null];
515
+ for (let pri = 0; pri < 5; pri++) {
516
+ const input = this.inputs[pri];
517
+ if (input) {
518
+ inputs[pri] = [...input];
519
+ }
520
+ }
521
+ for (const child of this.children.values()) {
522
+ const childOutput = child.getOutput();
523
+ for (let pri = 0; pri < 5; pri++) {
524
+ if (childOutput[pri]) {
525
+ const input = inputs[pri] || (inputs[pri] = []);
526
+ input.push(childOutput[pri]);
527
+ }
528
+ }
529
+ }
530
+ if (this.facet.singleton) {
531
+ const reducer = (_a = this.reducers)[_b = 2 /* default */] || (_a[_b] = this.facet.reducer);
532
+ const input = inputs.filter(Boolean).flat();
533
+ output[2 /* default */] = reducer(input);
534
+ } else {
535
+ for (let pri = 0; pri < 5; pri++) {
536
+ const input = inputs[pri];
537
+ if (input) {
538
+ const reducer = (_c = this.reducers)[pri] || (_c[pri] = this.facet.reducer);
539
+ output[pri] = reducer(input);
540
+ }
541
+ }
542
+ }
543
+ return output;
544
+ }
545
+ getOutput() {
546
+ if (!this.output) {
547
+ this.output = this.calcOutput();
548
+ }
549
+ return this.output;
550
+ }
551
+ getSingletonOutput() {
552
+ assert(this.facet.singleton);
553
+ return this.getOutput()[2 /* default */];
554
+ }
555
+ getRootOutput() {
556
+ assert(this.isRoot());
557
+ const output = this.getSingletonOutput();
558
+ assert(output);
559
+ return output;
560
+ }
561
+ isRoot() {
562
+ return !this.facet.parent;
419
563
  }
420
564
  };
421
565
 
566
+ // src/facets/schema.ts
567
+ import { Schema as Schema4 } from "@prosekit/pm/model";
568
+
422
569
  // src/facets/facet.ts
423
570
  var facetCount = 0;
424
- function getFacetCount() {
425
- return facetCount;
426
- }
427
- var Facet = class _Facet {
428
- constructor(converter, next, singleton) {
571
+ var Facet = class {
572
+ /**
573
+ * @internal
574
+ */
575
+ constructor(parent, singleton, _reducer, _reduce) {
576
+ this._reducer = _reducer;
577
+ this._reduce = _reduce;
429
578
  /**
430
579
  * @internal
431
580
  */
432
581
  this.index = facetCount++;
433
- /**
434
- * @internal
435
- */
436
- this.isSchema = false;
437
- this.converter = converter;
438
- this.next = next;
582
+ assert((_reduce || _reducer) && !(_reduce && _reducer));
583
+ this.parent = parent;
439
584
  this.singleton = singleton;
585
+ this.path = parent ? [...parent.path, this.index] : [];
440
586
  }
441
- static define({
442
- converter,
443
- convert,
444
- next,
445
- singleton
446
- }) {
447
- const converterFunction = converter ? converter : convert ? () => ({
448
- create: convert,
449
- update: convert
450
- }) : null;
451
- if (!converterFunction) {
452
- throw new ProseKitError("Facet must have either 'convert' or 'converter'");
453
- }
454
- return new _Facet(converterFunction, next, singleton != null ? singleton : false);
587
+ get reducer() {
588
+ var _a, _b;
589
+ return (_b = this._reducer) != null ? _b : (_a = this._reduce) == null ? void 0 : _a.call(this);
590
+ }
591
+ };
592
+ function defineFacet(options) {
593
+ var _a;
594
+ return new Facet(
595
+ options.parent,
596
+ (_a = options.singleton) != null ? _a : false,
597
+ options.reducer,
598
+ options.reduce
599
+ );
600
+ }
601
+
602
+ // src/facets/root.ts
603
+ function rootReducer(inputs) {
604
+ var _a;
605
+ let schema;
606
+ let commands;
607
+ let stateFunc;
608
+ let view;
609
+ for (const input of inputs) {
610
+ schema = input.schema || schema;
611
+ commands = input.commands || commands;
612
+ stateFunc = input.state || stateFunc;
613
+ view = input.view || view;
614
+ }
615
+ const state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema });
616
+ return { schema, state, commands, view };
617
+ }
618
+ var rootFacet = new Facet(
619
+ null,
620
+ true,
621
+ rootReducer
622
+ );
623
+
624
+ // src/facets/schema.ts
625
+ var schemaFacet = defineFacet({
626
+ reducer: (specs) => {
627
+ assert(specs.length <= 1);
628
+ const spec = specs[0];
629
+ const schema = spec ? new Schema4(spec) : null;
630
+ return { schema };
631
+ },
632
+ parent: rootFacet,
633
+ singleton: true
634
+ });
635
+
636
+ // src/facets/base-extension.ts
637
+ var BaseExtension = class {
638
+ constructor() {
639
+ this.extension = [];
640
+ this.trees = [null, null, null, null, null];
641
+ }
642
+ /**
643
+ * @internal
644
+ */
645
+ getTree(priority) {
646
+ var _a, _b;
647
+ const pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */;
648
+ return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri));
455
649
  }
456
650
  /**
457
651
  * @internal
458
652
  */
459
- static defineRootFacet(options) {
460
- return _Facet.define(options);
653
+ findFacetOutput(facet) {
654
+ var _a;
655
+ let node = this.getTree();
656
+ for (const index of facet.path) {
657
+ node = node == null ? void 0 : node.children.get(index);
658
+ }
659
+ return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null;
461
660
  }
462
- extension(payloads) {
463
- return new FacetExtensionImpl(this, payloads);
661
+ get schema() {
662
+ var _a, _b;
663
+ const output = this.findFacetOutput(schemaFacet);
664
+ return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null;
464
665
  }
465
666
  };
667
+
668
+ // src/facets/facet-extension.ts
466
669
  var FacetExtensionImpl = class extends BaseExtension {
670
+ /**
671
+ * @internal
672
+ */
467
673
  constructor(facet, payloads) {
468
- var _a;
469
674
  super();
470
675
  this.facet = facet;
471
676
  this.payloads = payloads;
472
- this.schema = null;
473
- this.hasSchema = !!(facet.isSchema || ((_a = facet.next) == null ? void 0 : _a.isSchema));
677
+ }
678
+ /**
679
+ * @internal
680
+ */
681
+ createTree(priority) {
682
+ var _a;
683
+ const pri = (_a = this.priority) != null ? _a : priority;
684
+ const inputs = [null, null, null, null, null];
685
+ inputs[pri] = [...this.payloads];
686
+ let node = new FacetNode(this.facet, inputs);
687
+ while (node.facet.parent) {
688
+ const children = /* @__PURE__ */ new Map([[node.facet.index, node]]);
689
+ node = new FacetNode(node.facet.parent, void 0, children);
690
+ }
691
+ return node;
474
692
  }
475
693
  };
694
+ function defineFacetPayload(facet, payloads) {
695
+ return new FacetExtensionImpl(facet, payloads);
696
+ }
476
697
 
477
698
  // src/facets/state.ts
478
- var stateFacet = Facet.defineRootFacet({
479
- convert: (callbacks) => {
480
- return (ctx) => {
699
+ var stateFacet = defineFacet({
700
+ reduce: () => {
701
+ let callbacks = [];
702
+ const state = (ctx) => {
481
703
  var _a, _b, _c, _d, _e, _f;
482
704
  const configs = callbacks.map((cb) => cb(ctx));
483
705
  const config = {
@@ -492,17 +714,22 @@ var stateFacet = Facet.defineRootFacet({
492
714
  config.storedMarks = [...config.storedMarks, ...(_d = c.storedMarks) != null ? _d : []];
493
715
  config.plugins = uniqPush((_e = config.plugins) != null ? _e : [], (_f = c.plugins) != null ? _f : []);
494
716
  }
495
- if (!config.doc && !config.schema) {
496
- throw new ProseKitError(
497
- "Can't create state without a schema nor a document"
498
- );
499
- }
717
+ assert(
718
+ config.doc || config.schema,
719
+ "Can't create state without a schema nor a document"
720
+ );
500
721
  if (config.doc) {
501
722
  config.schema = void 0;
502
723
  }
503
724
  return config;
504
725
  };
505
- }
726
+ return function reducer(inputs) {
727
+ callbacks = inputs;
728
+ return { state };
729
+ };
730
+ },
731
+ singleton: true,
732
+ parent: rootFacet
506
733
  });
507
734
 
508
735
  // src/utils/parse.ts
@@ -618,7 +845,7 @@ function defineDefaultState({
618
845
  "Only one of defaultHTML and defaultDoc can be provided"
619
846
  );
620
847
  }
621
- return stateFacet.extension([
848
+ return defineFacetPayload(stateFacet, [
622
849
  ({ schema }) => {
623
850
  const config = {};
624
851
  if (defaultHTML) {
@@ -639,220 +866,27 @@ function defineDefaultState({
639
866
  ]);
640
867
  }
641
868
 
642
- // src/types/priority.ts
643
- var Priority = /* @__PURE__ */ ((Priority2) => {
644
- Priority2[Priority2["lowest"] = 4] = "lowest";
645
- Priority2[Priority2["low"] = 3] = "low";
646
- Priority2[Priority2["default"] = 2] = "default";
647
- Priority2[Priority2["high"] = 1] = "high";
648
- Priority2[Priority2["highest"] = 0] = "highest";
649
- return Priority2;
650
- })(Priority || {});
651
-
652
- // src/facets/command.ts
653
- var commandFacet = Facet.defineRootFacet({
654
- convert: (inputs) => {
655
- return Object.assign({}, ...inputs);
656
- }
657
- });
658
-
659
- // src/facets/schema.ts
869
+ // src/utils/deep-equals.ts
660
870
  import OrderedMap from "orderedmap";
661
- var schemaFacet = Facet.defineRootFacet({
662
- convert: (specs) => {
663
- var _a;
664
- let nodes = OrderedMap.from({});
665
- let marks = OrderedMap.from({});
666
- let topNode = void 0;
667
- for (const spec of specs) {
668
- nodes = nodes.append(spec.nodes);
669
- marks = marks.append((_a = spec.marks) != null ? _a : {});
670
- topNode = topNode != null ? topNode : spec.topNode;
671
- }
672
- return { nodes, marks, topNode };
871
+ function deepEquals(a, b) {
872
+ if (a === b) {
873
+ return true;
673
874
  }
674
- });
675
- schemaFacet.isSchema = true;
676
-
677
- // src/facets/view.ts
678
- var viewFacet = Facet.defineRootFacet({
679
- convert: (props) => {
680
- return Object.assign({}, ...props);
875
+ if (!a || !b) {
876
+ return false;
681
877
  }
682
- });
683
-
684
- // src/facets/flatten.ts
685
- function flattenInputTuple(inputTuple) {
686
- return [
687
- ...inputTuple[0],
688
- ...inputTuple[1],
689
- ...inputTuple[2],
690
- ...inputTuple[3],
691
- ...inputTuple[4]
692
- ];
693
- }
694
- function mergeInputTuple(tupleA, tupleB) {
695
- if (!tupleA)
696
- return tupleB;
697
- if (!tupleB)
698
- return tupleA;
699
- const [a0, a1, a2, a3, a4] = tupleA;
700
- const [b0, b1, b2, b3, b4] = tupleB;
701
- return [
702
- uniqPush(a0, b0),
703
- uniqPush(a1, b1),
704
- uniqPush(a2, b2),
705
- uniqPush(a3, b3),
706
- uniqPush(a4, b4)
707
- ];
708
- }
709
- function removeInputTuple(tupleA, tupleB) {
710
- if (!tupleA)
711
- return [[], [], [], [], []];
712
- if (!tupleB)
713
- return tupleA;
714
- const [a0, a1, a2, a3, a4] = tupleA;
715
- const [b0, b1, b2, b3, b4] = tupleB;
716
- return [
717
- uniqRemove(a0, b0),
718
- uniqRemove(a1, b1),
719
- uniqRemove(a2, b2),
720
- uniqRemove(a3, b3),
721
- uniqRemove(a4, b4)
722
- ];
723
- }
724
- function extractFacets(root) {
725
- var _a;
726
- const extensions = [root];
727
- const priorities = [2 /* default */];
728
- const facets = [];
729
- const payloads = [];
730
- while (extensions.length > 0) {
731
- const ext = extensions.pop();
732
- const pri = priorities.pop();
733
- if (ext instanceof FacetExtensionImpl) {
734
- const facet = ext.facet;
735
- if (!facets[facet.index]) {
736
- facets[facet.index] = facet;
737
- payloads[facet.index] = [[], [], [], [], []];
738
- }
739
- const facetPayloads = ext.payloads;
740
- payloads[facet.index][pri].push(...facetPayloads);
741
- } else if (ext.extension) {
742
- const p = (_a = ext.priority) != null ? _a : pri;
743
- if (Array.isArray(ext.extension)) {
744
- for (const e of ext.extension) {
745
- extensions.push(e);
746
- priorities.push(p);
747
- }
748
- } else {
749
- extensions.push(ext.extension);
750
- priorities.push(p);
751
- }
752
- } else {
753
- throw new ProseKitError("Invalid extension");
754
- }
878
+ if (Array.isArray(a) && Array.isArray(b)) {
879
+ return a.length === b.length && a.every((x, i) => deepEquals(x, b[i]));
755
880
  }
756
- return [facets, payloads];
757
- }
758
- function updateExtension(prevInputs, prevConverters, extension, mode) {
759
- var _a;
760
- const modifyInputTuple = mode === "add" ? mergeInputTuple : removeInputTuple;
761
- const [facets, inputs] = extractFacets(extension);
762
- let schemaInput = null;
763
- let stateInput = null;
764
- let viewInput = null;
765
- let commandInput = null;
766
- for (let index = getFacetCount(); index >= 0; index--) {
767
- const facet = facets[index];
768
- if (!facet) {
769
- continue;
770
- }
771
- const nextFacet = facet.next;
772
- if (nextFacet) {
773
- facets[_a = nextFacet.index] || (facets[_a] = nextFacet);
774
- }
775
- if (!inputs[facet.index]) {
776
- continue;
777
- }
778
- const inputTuple = modifyInputTuple(prevInputs[index], inputs[index]);
779
- prevInputs[index] = inputTuple;
780
- if (facet.next && !facet.singleton) {
781
- let hasOutput = false;
782
- const outputTuple = [[], [], [], [], []];
783
- for (let pri = 0; pri < 5; pri++) {
784
- const inputArray = inputTuple[pri];
785
- if (inputArray.length === 0) {
786
- continue;
787
- }
788
- const converterTuple = prevConverters[index] || (prevConverters[index] = [
789
- void 0,
790
- void 0,
791
- void 0,
792
- void 0,
793
- void 0
794
- ]);
795
- const prevConverter = converterTuple[pri];
796
- const converter = prevConverter || facet.converter();
797
- prevConverters[index][pri] = converter;
798
- const output = prevConverter ? converter.update(inputArray) : converter.create(inputArray);
799
- if (!output) {
800
- continue;
801
- }
802
- hasOutput = true;
803
- outputTuple[pri].push(output);
804
- }
805
- if (!hasOutput) {
806
- continue;
807
- }
808
- inputs[facet.next.index] = modifyInputTuple(
809
- inputs[facet.next.index],
810
- outputTuple
811
- );
812
- continue;
813
- } else {
814
- const inputArray = flattenInputTuple(inputTuple);
815
- prevConverters[index] || (prevConverters[index] = [
816
- void 0,
817
- void 0,
818
- void 0,
819
- void 0,
820
- void 0
821
- ]);
822
- const prevConverter = prevConverters[index][2 /* default */];
823
- const converter = prevConverter || facet.converter();
824
- prevConverters[index][2 /* default */] = converter;
825
- const output = prevConverter ? converter.update(inputArray) : converter.create(inputArray);
826
- if (!output) {
827
- continue;
828
- }
829
- if (facet.next) {
830
- const outputTuple = [[], [], [output], [], []];
831
- inputs[facet.next.index] = modifyInputTuple(
832
- inputs[facet.next.index],
833
- outputTuple
834
- );
835
- } else {
836
- switch (facet) {
837
- case schemaFacet:
838
- schemaInput = output;
839
- break;
840
- case stateFacet:
841
- stateInput = output;
842
- break;
843
- case viewFacet:
844
- viewInput = output;
845
- break;
846
- case commandFacet:
847
- commandInput = output;
848
- break;
849
- default:
850
- throw new ProseKitError("Invalid root facet");
851
- }
852
- }
853
- }
881
+ if (a instanceof OrderedMap && b instanceof OrderedMap) {
882
+ return a.size === b.size && deepEquals(a.toObject(), b.toObject());
883
+ }
884
+ if (typeof a === "object" && typeof b === "object") {
885
+ const aKeys = Object.keys(a);
886
+ const bKeys = Object.keys(b);
887
+ return aKeys.length === bKeys.length && aKeys.every((key) => deepEquals(a[key], b[key]));
854
888
  }
855
- return { schemaInput, stateInput, viewInput, commandInput };
889
+ return false;
856
890
  }
857
891
 
858
892
  // src/editor/builder.ts
@@ -970,43 +1004,27 @@ function isNodeChild(value) {
970
1004
  }
971
1005
 
972
1006
  // src/facets/union-extension.ts
973
- import { Schema as Schema5 } from "@prosekit/pm/model";
974
1007
  var UnionExtensionImpl = class extends BaseExtension {
1008
+ /**
1009
+ * @internal
1010
+ */
975
1011
  constructor(extension = []) {
976
1012
  super();
977
1013
  this.extension = extension;
978
- this._schema = void 0;
979
- this.hasSchemaCount = 0;
980
- for (const e of extension) {
981
- if (e instanceof BaseExtension) {
982
- this.hasSchemaCount += e.hasSchema ? 1 : 0;
983
- } else {
984
- throw new ProseKitError("Invalid extension");
985
- }
986
- }
987
- }
988
- get hasSchema() {
989
- return this.hasSchemaCount > 0;
990
1014
  }
991
- get schema() {
1015
+ /**
1016
+ * @internal
1017
+ */
1018
+ createTree(priority) {
992
1019
  var _a;
993
- if (this._schema !== void 0) {
994
- return this._schema;
995
- }
996
- if (this.hasSchemaCount === 0) {
997
- this._schema = null;
998
- return this._schema;
999
- }
1000
- if (this.hasSchemaCount === 1) {
1001
- const schema = (_a = this.extension.find((e) => e.hasSchema)) == null ? void 0 : _a.schema;
1002
- if (schema) {
1003
- this._schema = schema;
1004
- return this._schema;
1005
- }
1006
- }
1007
- const { schemaInput } = updateExtension([], [], this, "add");
1008
- this._schema = schemaInput ? new Schema5(schemaInput) : null;
1009
- return this._schema;
1020
+ const pri = (_a = this.priority) != null ? _a : priority;
1021
+ const extensions = [...this.extension];
1022
+ extensions.sort((a, b) => {
1023
+ var _a2, _b;
1024
+ return ((_a2 = a.priority) != null ? _a2 : pri) - ((_b = b.priority) != null ? _b : pri);
1025
+ });
1026
+ const children = extensions.map((ext) => ext.getTree(pri));
1027
+ return children.reduce(unionFacetNode, new FacetNode(rootFacet));
1010
1028
  }
1011
1029
  };
1012
1030
 
@@ -1038,24 +1056,21 @@ var EditorInstance = class {
1038
1056
  constructor(extension) {
1039
1057
  this.view = null;
1040
1058
  this.commandAppliers = {};
1041
- this.payloads = [];
1042
- this.converters = [];
1043
1059
  this.mount = this.mount.bind(this);
1044
1060
  this.unmount = this.unmount.bind(this);
1045
- const { schemaInput, stateInput, viewInput, commandInput } = updateExtension(this.payloads, this.converters, extension, "add");
1046
- if (!schemaInput) {
1047
- throw new ProseKitError("Schema must be defined");
1048
- }
1049
- const schema = new Schema6(schemaInput);
1050
- const stateConfig = stateInput ? stateInput({ schema }) : { schema };
1061
+ this.tree = extension.getTree();
1062
+ const payload = this.tree.getRootOutput();
1063
+ const schema = payload.schema;
1064
+ const stateConfig = payload.state;
1065
+ assert(schema && stateConfig, "Schema must be defined");
1051
1066
  const state = EditorState2.create(stateConfig);
1052
1067
  this.cachedState = state;
1053
- if (commandInput) {
1054
- for (const [name, commandCreator] of Object.entries(commandInput)) {
1068
+ if (payload.commands) {
1069
+ for (const [name, commandCreator] of Object.entries(payload.commands)) {
1055
1070
  this.defineCommand(name, commandCreator);
1056
1071
  }
1057
1072
  }
1058
- this.directEditorProps = { state, ...viewInput };
1073
+ this.directEditorProps = { state, ...payload.view };
1059
1074
  this.schema = this.directEditorProps.state.schema;
1060
1075
  const getState = () => this.getState();
1061
1076
  this.nodeBuilders = Object.fromEntries(
@@ -1077,29 +1092,34 @@ var EditorInstance = class {
1077
1092
  }
1078
1093
  return this.cachedState;
1079
1094
  }
1080
- updateExtension(extension, mode) {
1081
- var _a;
1082
- const { schemaInput, stateInput, viewInput, commandInput } = updateExtension(this.payloads, this.converters, extension, mode);
1083
- if (schemaInput) {
1095
+ updateExtension(extension, add) {
1096
+ var _a, _b, _c, _d;
1097
+ const view = this.view;
1098
+ if (!view || view.isDestroyed) {
1099
+ return;
1100
+ }
1101
+ const tree = extension.getTree();
1102
+ const payload = tree.getRootOutput();
1103
+ if (payload == null ? void 0 : payload.schema) {
1084
1104
  throw new ProseKitError("Schema cannot be changed");
1085
1105
  }
1086
- if (viewInput) {
1106
+ if (payload == null ? void 0 : payload.view) {
1087
1107
  throw new ProseKitError("View cannot be changed");
1088
1108
  }
1089
- const plugins = (_a = stateInput == null ? void 0 : stateInput({ schema: this.schema })) == null ? void 0 : _a.plugins;
1090
- if (plugins && plugins.length > 0) {
1091
- if (!this.view) {
1092
- throw new ProseKitError(
1093
- "Unexpected inner state: EditorInstance.view is not defined"
1094
- );
1095
- }
1096
- const state = this.view.state.reconfigure({ plugins });
1097
- this.view.updateState(state);
1098
- }
1099
- if (commandInput) {
1100
- const names = Object.keys(commandInput);
1109
+ const oldPayload = this.tree.getRootOutput();
1110
+ const oldPlugins = [...(_b = (_a = view.state) == null ? void 0 : _a.plugins) != null ? _b : []];
1111
+ this.tree = add ? unionFacetNode(this.tree, tree) : subtractFacetNode(this.tree, tree);
1112
+ const newPayload = this.tree.getRootOutput();
1113
+ const newPlugins = [...(_d = (_c = newPayload == null ? void 0 : newPayload.state) == null ? void 0 : _c.plugins) != null ? _d : []];
1114
+ if (!deepEquals(oldPlugins, newPlugins)) {
1115
+ const state = view.state.reconfigure({ plugins: newPlugins });
1116
+ view.updateState(state);
1117
+ }
1118
+ if ((newPayload == null ? void 0 : newPayload.commands) && !deepEquals(oldPayload == null ? void 0 : oldPayload.commands, newPayload == null ? void 0 : newPayload.commands)) {
1119
+ const commands = newPayload.commands;
1120
+ const names = Object.keys(commands);
1101
1121
  for (const name of names) {
1102
- this.defineCommand(name, commandInput[name]);
1122
+ this.defineCommand(name, commands[name]);
1103
1123
  }
1104
1124
  }
1105
1125
  }
@@ -1119,6 +1139,9 @@ var EditorInstance = class {
1119
1139
  this.view.destroy();
1120
1140
  this.view = null;
1121
1141
  }
1142
+ get mounted() {
1143
+ return !!this.view && !this.view.isDestroyed;
1144
+ }
1122
1145
  get assertView() {
1123
1146
  if (!this.view) {
1124
1147
  throw new ProseKitError("Editor is not mounted");
@@ -1185,7 +1208,7 @@ var Editor = class _Editor {
1185
1208
  * Whether the editor is mounted.
1186
1209
  */
1187
1210
  get mounted() {
1188
- return !!this.instance.view;
1211
+ return this.instance.mounted;
1189
1212
  }
1190
1213
  /**
1191
1214
  * The editor view.
@@ -1253,8 +1276,8 @@ var Editor = class _Editor {
1253
1276
  lazyRemove == null ? void 0 : lazyRemove();
1254
1277
  };
1255
1278
  }
1256
- this.instance.updateExtension(extension, "add");
1257
- return () => this.instance.updateExtension(extension, "remove");
1279
+ this.instance.updateExtension(extension, true);
1280
+ return () => this.instance.updateExtension(extension, false);
1258
1281
  }
1259
1282
  get state() {
1260
1283
  return this.instance.getState();
@@ -1318,9 +1341,19 @@ function wrap({
1318
1341
  };
1319
1342
  }
1320
1343
 
1344
+ // src/facets/command.ts
1345
+ var commandFacet = defineFacet({
1346
+ reducer: (inputs) => {
1347
+ const commands = Object.assign({}, ...inputs);
1348
+ return { commands };
1349
+ },
1350
+ parent: rootFacet,
1351
+ singleton: true
1352
+ });
1353
+
1321
1354
  // src/extensions/command.ts
1322
1355
  function defineCommands(commands) {
1323
- return commandFacet.extension([commands]);
1356
+ return defineFacetPayload(commandFacet, [commands]);
1324
1357
  }
1325
1358
  function defineBaseCommands() {
1326
1359
  return defineCommands({
@@ -1335,6 +1368,29 @@ function defineBaseCommands() {
1335
1368
  });
1336
1369
  }
1337
1370
 
1371
+ // src/extensions/node-spec.ts
1372
+ import OrderedMap3 from "orderedmap";
1373
+
1374
+ // src/facets/schema-spec.ts
1375
+ import "@prosekit/pm/model";
1376
+ import OrderedMap2 from "orderedmap";
1377
+ var schemaSpecFacet = defineFacet({
1378
+ reducer: (specs) => {
1379
+ var _a;
1380
+ let nodes = OrderedMap2.from({});
1381
+ let marks = OrderedMap2.from({});
1382
+ let topNode = void 0;
1383
+ for (const spec of specs) {
1384
+ nodes = nodes.append(spec.nodes);
1385
+ marks = marks.append((_a = spec.marks) != null ? _a : {});
1386
+ topNode = topNode != null ? topNode : spec.topNode;
1387
+ }
1388
+ return { nodes, marks, topNode };
1389
+ },
1390
+ parent: schemaFacet,
1391
+ singleton: true
1392
+ });
1393
+
1338
1394
  // src/utils/is-element.ts
1339
1395
  var hasElement = typeof Element !== "undefined";
1340
1396
  function isElement(value) {
@@ -1349,44 +1405,42 @@ function isNotNull(value) {
1349
1405
  // src/extensions/node-spec.ts
1350
1406
  function defineNodeSpec(options) {
1351
1407
  const payload = [options, void 0];
1352
- return nodeSpecFacet.extension([payload]);
1408
+ return defineFacetPayload(nodeSpecFacet, [payload]);
1353
1409
  }
1354
1410
  function defineNodeAttr(options) {
1355
1411
  const payload = [void 0, options];
1356
- return nodeSpecFacet.extension([payload]);
1412
+ return defineFacetPayload(nodeSpecFacet, [payload]);
1357
1413
  }
1358
- var nodeSpecFacet = Facet.define({
1359
- convert: (payloads) => {
1360
- const nodes = {};
1414
+ var nodeSpecFacet = defineFacet({
1415
+ reducer: (payloads) => {
1416
+ let nodes = OrderedMap3.from({});
1361
1417
  let topNodeName = void 0;
1362
1418
  const specPayloads = payloads.map((input) => input[0]).filter(isNotNull);
1363
1419
  const attrPayloads = payloads.map((input) => input[1]).filter(isNotNull);
1364
1420
  for (const { name, topNode, ...spec } of specPayloads) {
1365
- if (nodes[name]) {
1366
- throw new ProseKitError(`Node type ${name} has already been defined`);
1367
- }
1421
+ assert(!nodes.get(name), `Node type ${name} can only be defined once`);
1368
1422
  if (topNode) {
1369
1423
  topNodeName = name;
1370
1424
  }
1371
- nodes[name] = spec;
1425
+ nodes = nodes.addToStart(name, spec);
1372
1426
  }
1373
1427
  for (const {
1374
1428
  type,
1375
1429
  attr,
1376
1430
  default: defaultValue,
1431
+ splittable,
1377
1432
  toDOM,
1378
1433
  parseDOM
1379
1434
  } of attrPayloads) {
1380
- const spec = nodes[type];
1381
- if (!spec) {
1382
- throw new ProseKitError(
1383
- `Node type ${type} must be defined before defining attributes`
1384
- );
1385
- }
1435
+ const spec = nodes.get(type);
1436
+ assert(spec, `Node type ${type} must be defined`);
1386
1437
  if (!spec.attrs) {
1387
1438
  spec.attrs = {};
1388
1439
  }
1389
- spec.attrs[attr] = { default: defaultValue };
1440
+ spec.attrs[attr] = {
1441
+ default: defaultValue,
1442
+ splittable
1443
+ };
1390
1444
  if (toDOM && spec.toDOM) {
1391
1445
  const existingToDom = spec.toDOM;
1392
1446
  spec.toDOM = (node) => {
@@ -1404,14 +1458,22 @@ var nodeSpecFacet = Facet.define({
1404
1458
  }
1405
1459
  if (Array.isArray(dom)) {
1406
1460
  if (typeof dom[1] === "object") {
1407
- return [dom[0], { ...dom[1], [key]: value }, ...dom.slice(2)];
1461
+ return [
1462
+ dom[0],
1463
+ setObjectAttribute(
1464
+ dom[1],
1465
+ key,
1466
+ value
1467
+ ),
1468
+ ...dom.slice(2)
1469
+ ];
1408
1470
  } else {
1409
1471
  return [dom[0], { [key]: value }, ...dom.slice(1)];
1410
1472
  }
1411
1473
  } else if (isElement(dom)) {
1412
- dom.setAttribute(key, value);
1474
+ setElementAttribute(dom, key, value);
1413
1475
  } else if (typeof dom === "object" && "dom" in dom && isElement(dom.dom)) {
1414
- dom.dom.setAttribute(key, value);
1476
+ setElementAttribute(dom.dom, key, value);
1415
1477
  }
1416
1478
  return dom;
1417
1479
  };
@@ -1437,9 +1499,21 @@ var nodeSpecFacet = Facet.define({
1437
1499
  }
1438
1500
  return { nodes, topNode: topNodeName };
1439
1501
  },
1440
- next: schemaFacet,
1502
+ parent: schemaSpecFacet,
1441
1503
  singleton: true
1442
1504
  });
1505
+ function setObjectAttribute(obj, key, value) {
1506
+ if (key === "style") {
1507
+ value = `${value}${obj.style || ""}`;
1508
+ }
1509
+ return { ...obj, [key]: value };
1510
+ }
1511
+ function setElementAttribute(element, key, value) {
1512
+ if (key === "style") {
1513
+ value = `${value}${element.getAttribute("style") || ""}`;
1514
+ }
1515
+ element.setAttribute(key, value);
1516
+ }
1443
1517
 
1444
1518
  // src/extensions/doc.ts
1445
1519
  function defineDoc() {
@@ -1451,60 +1525,61 @@ function defineDoc() {
1451
1525
  }
1452
1526
 
1453
1527
  // src/extensions/events/plugin-view.ts
1454
- import { PluginKey, ProseMirrorPlugin } from "@prosekit/pm/state";
1528
+ import { PluginKey, ProseMirrorPlugin as ProseMirrorPlugin2 } from "@prosekit/pm/state";
1455
1529
 
1456
1530
  // src/extensions/plugin.ts
1457
1531
  import "@prosekit/pm/model";
1458
1532
  import { Plugin as Plugin2 } from "@prosekit/pm/state";
1459
1533
  function definePlugin(plugin) {
1460
1534
  if (plugin instanceof Plugin2) {
1461
- return pluginFacet.extension([() => [plugin]]);
1535
+ return defineFacetPayload(pluginFacet, [() => [plugin]]);
1462
1536
  }
1463
1537
  if (Array.isArray(plugin) && plugin.every((p) => p instanceof Plugin2)) {
1464
- return pluginFacet.extension([() => plugin]);
1538
+ return defineFacetPayload(pluginFacet, [() => plugin]);
1465
1539
  }
1466
1540
  if (typeof plugin === "function") {
1467
- return pluginFacet.extension([plugin]);
1541
+ return defineFacetPayload(pluginFacet, [plugin]);
1468
1542
  }
1469
1543
  throw new TypeError("Invalid plugin");
1470
1544
  }
1471
- var pluginFacet = Facet.define({
1472
- converter: () => {
1473
- let inputs = [];
1474
- const output = ({ schema }) => {
1475
- const plugins = inputs.flatMap((func) => func({ schema }));
1476
- return { plugins };
1477
- };
1478
- return {
1479
- create: (payloads) => {
1480
- inputs = payloads;
1481
- return output;
1482
- },
1483
- update: (payloads) => {
1484
- inputs = payloads;
1485
- return output;
1545
+ var pluginFacet = defineFacet({
1546
+ reducer: (payloads) => {
1547
+ return ({ schema }) => {
1548
+ const plugins = [];
1549
+ for (const payload of payloads) {
1550
+ if (payload instanceof Plugin2) {
1551
+ plugins.push(payload);
1552
+ } else if (Array.isArray(payload) && payload.every((p) => p instanceof Plugin2)) {
1553
+ plugins.push(...payload);
1554
+ } else if (typeof payload === "function") {
1555
+ plugins.push(...[payload({ schema })].flat());
1556
+ } else {
1557
+ throw new ProseKitError("Invalid plugin");
1558
+ }
1486
1559
  }
1560
+ plugins.reverse();
1561
+ return { plugins };
1487
1562
  };
1488
1563
  },
1489
- next: stateFacet
1564
+ parent: stateFacet
1490
1565
  });
1491
1566
 
1492
1567
  // src/extensions/events/plugin-view.ts
1493
1568
  function defineMountHandler(handler) {
1494
- return pluginViewFacet.extension([["mount", handler]]);
1569
+ return defineFacetPayload(pluginViewFacet, [["mount", handler]]);
1495
1570
  }
1496
1571
  function defineUpdateHandler(handler) {
1497
- return pluginViewFacet.extension([["update", handler]]);
1572
+ return defineFacetPayload(pluginViewFacet, [["update", handler]]);
1498
1573
  }
1499
1574
  function defineUnmountHandler(handler) {
1500
- return pluginViewFacet.extension([["unmount", handler]]);
1575
+ return defineFacetPayload(pluginViewFacet, [["unmount", handler]]);
1501
1576
  }
1502
- var pluginViewFacet = Facet.define({
1503
- converter: () => {
1577
+ var pluginViewFacet = defineFacet({
1578
+ reduce: () => {
1504
1579
  let mountHandlers = [];
1505
1580
  let updateHandlers = [];
1506
1581
  let unmountHandlers = [];
1507
- const plugin = new ProseMirrorPlugin({
1582
+ const plugin = new ProseMirrorPlugin2({
1508
1583
  key: pluginKey,
1509
1584
  view: (view) => {
1510
1585
  mountHandlers.forEach((fn) => fn(view));
@@ -1518,7 +1593,6 @@ var pluginViewFacet = Facet.define({
1518
1593
  };
1519
1594
  }
1520
1595
  });
1521
- const pluginFunc = () => [plugin];
1522
1596
  const register = (input) => {
1523
1597
  mountHandlers = [];
1524
1598
  updateHandlers = [];
@@ -1537,18 +1611,12 @@ var pluginViewFacet = Facet.define({
1537
1611
  }
1538
1612
  }
1539
1613
  };
1540
- return {
1541
- create: (input) => {
1542
- register(input);
1543
- return pluginFunc;
1544
- },
1545
- update: (input) => {
1546
- register(input);
1547
- return null;
1548
- }
1614
+ return function reducer(input) {
1615
+ register(input);
1616
+ return plugin;
1549
1617
  };
1550
1618
  },
1551
- next: pluginFacet,
1619
+ parent: pluginFacet,
1552
1620
  singleton: true
1553
1621
  });
1554
1622
  var pluginKey = new PluginKey("prosekit-plugin-view-handler");
@@ -1563,13 +1631,13 @@ function defineDocChangeHandler(handler) {
1563
1631
  }
1564
1632
 
1565
1633
  // src/extensions/events/dom-event.ts
1566
- import { PluginKey as PluginKey2, ProseMirrorPlugin as ProseMirrorPlugin2 } from "@prosekit/pm/state";
1634
+ import { PluginKey as PluginKey2, ProseMirrorPlugin as ProseMirrorPlugin3 } from "@prosekit/pm/state";
1567
1635
 
1568
1636
  // src/utils/combine-event-handlers.ts
1569
1637
  function combineEventHandlers() {
1570
1638
  let _handlers = [];
1571
1639
  function setHandlers(handlers) {
1572
- _handlers = handlers;
1640
+ _handlers = toReversed(handlers);
1573
1641
  }
1574
1642
  function combinedEventHandler(...args) {
1575
1643
  for (const handler of _handlers) {
@@ -1598,106 +1666,97 @@ function groupEntries(entries) {
1598
1666
 
1599
1667
  // src/extensions/events/dom-event.ts
1600
1668
  function defineDOMEventHandler(event, handler) {
1601
- return domEventFacet.extension([
1669
+ return defineFacetPayload(domEventFacet, [
1602
1670
  [event, handler]
1603
1671
  ]);
1604
1672
  }
1605
- var domEventFacet = Facet.define({
1606
- converter: () => {
1673
+ var domEventFacet = defineFacet({
1674
+ reduce: () => {
1607
1675
  const setHandlersMap = {};
1608
1676
  const combinedHandlerMap = {};
1677
+ let plugin = null;
1609
1678
  const update = (payloads) => {
1679
+ var _a;
1610
1680
  let hasNewEvent = false;
1611
1681
  for (const [event] of payloads) {
1612
1682
  if (!setHandlersMap[event]) {
1613
1683
  hasNewEvent = true;
1614
1684
  const [setHandlers, combinedHandler] = combineEventHandlers();
1615
1685
  setHandlersMap[event] = setHandlers;
1616
- combinedHandlerMap[event] = combinedHandler;
1686
+ const e = (view, eventObject) => {
1687
+ return combinedHandler(view, eventObject);
1688
+ };
1689
+ combinedHandlerMap[event] = e;
1617
1690
  }
1618
1691
  }
1619
1692
  const map = groupEntries(payloads);
1620
- for (const [event, handlers] of Object.entries(map)) {
1621
- const setHandlers = setHandlersMap[event];
1622
- setHandlers(handlers != null ? handlers : []);
1693
+ for (const [event, setHandlers] of Object.entries(setHandlersMap)) {
1694
+ const handlers = (_a = map[event]) != null ? _a : [];
1695
+ setHandlers(handlers);
1623
1696
  }
1624
1697
  if (hasNewEvent) {
1625
- return new ProseMirrorPlugin2({
1698
+ plugin = new ProseMirrorPlugin3({
1626
1699
  key: new PluginKey2("prosekit-dom-event-handler"),
1627
1700
  props: { handleDOMEvents: combinedHandlerMap }
1628
1701
  });
1629
- } else {
1630
- return null;
1631
1702
  }
1632
1703
  };
1633
- return {
1634
- create: (payloads) => {
1635
- const plugin = update(payloads);
1636
- return plugin ? () => plugin : () => [];
1637
- },
1638
- update: (payloads) => {
1639
- const plugin = update(payloads);
1640
- return plugin ? () => plugin : null;
1641
- }
1704
+ return function reducer(inputs) {
1705
+ update(inputs);
1706
+ return plugin != null ? plugin : [];
1642
1707
  };
1643
1708
  },
1644
- next: pluginFacet,
1709
+ parent: pluginFacet,
1645
1710
  singleton: true
1646
1711
  });
1647
1712
 
1648
1713
  // src/extensions/events/editor-event.ts
1649
- import { PluginKey as PluginKey3, ProseMirrorPlugin as ProseMirrorPlugin3 } from "@prosekit/pm/state";
1714
+ import { PluginKey as PluginKey3, ProseMirrorPlugin as ProseMirrorPlugin4 } from "@prosekit/pm/state";
1650
1715
  function defineKeyDownHandler(handler) {
1651
- return editorEventFacet.extension([["keyDown", handler]]);
1716
+ return defineFacetPayload(editorEventFacet, [["keyDown", handler]]);
1652
1717
  }
1653
1718
  function defineKeyPressHandler(handler) {
1654
- return editorEventFacet.extension([["keyPress", handler]]);
1719
+ return defineFacetPayload(editorEventFacet, [["keyPress", handler]]);
1655
1720
  }
1656
1721
  function defineTextInputHandler(handler) {
1657
- return editorEventFacet.extension([["textInput", handler]]);
1722
+ return defineFacetPayload(editorEventFacet, [["textInput", handler]]);
1658
1723
  }
1659
1724
  function defineClickOnHandler(handler) {
1660
- return editorEventFacet.extension([["clickOn", handler]]);
1725
+ return defineFacetPayload(editorEventFacet, [["clickOn", handler]]);
1661
1726
  }
1662
1727
  function defineClickHandler(handler) {
1663
- return editorEventFacet.extension([["click", handler]]);
1728
+ return defineFacetPayload(editorEventFacet, [["click", handler]]);
1664
1729
  }
1665
1730
  function defineDoubleClickOnHandler(handler) {
1666
- return editorEventFacet.extension([["doubleClickOn", handler]]);
1731
+ return defineFacetPayload(editorEventFacet, [["doubleClickOn", handler]]);
1667
1732
  }
1668
1733
  function defineDoubleClickHandler(handler) {
1669
- return editorEventFacet.extension([["doubleClick", handler]]);
1734
+ return defineFacetPayload(editorEventFacet, [["doubleClick", handler]]);
1670
1735
  }
1671
1736
  function defineTripleClickOnHandler(handler) {
1672
- return editorEventFacet.extension([["tripleClickOn", handler]]);
1737
+ return defineFacetPayload(editorEventFacet, [["tripleClickOn", handler]]);
1673
1738
  }
1674
1739
  function defineTripleClickHandler(handler) {
1675
- return editorEventFacet.extension([["tripleClick", handler]]);
1740
+ return defineFacetPayload(editorEventFacet, [["tripleClick", handler]]);
1676
1741
  }
1677
1742
  function definePasteHandler(handler) {
1678
- return editorEventFacet.extension([["paste", handler]]);
1743
+ return defineFacetPayload(editorEventFacet, [["paste", handler]]);
1679
1744
  }
1680
1745
  function defineDropHandler(handler) {
1681
- return editorEventFacet.extension([["drop", handler]]);
1746
+ return defineFacetPayload(editorEventFacet, [["drop", handler]]);
1682
1747
  }
1683
1748
  function defineScrollToSelectionHandler(handler) {
1684
- return editorEventFacet.extension([["scrollToSelection", handler]]);
1749
+ return defineFacetPayload(editorEventFacet, [["scrollToSelection", handler]]);
1685
1750
  }
1686
- var editorEventFacet = Facet.define({
1687
- converter: () => {
1751
+ var editorEventFacet = defineFacet({
1752
+ reduce: () => {
1688
1753
  const [update, plugin] = setupEditorEventPlugin();
1689
- return {
1690
- create: (entries) => {
1691
- update(entries);
1692
- return () => plugin;
1693
- },
1694
- update: (entries) => {
1695
- update(entries);
1696
- return null;
1697
- }
1754
+ return (entries) => {
1755
+ update(entries);
1756
+ return plugin;
1698
1757
  };
1699
1758
  },
1700
- next: pluginFacet,
1759
+ parent: pluginFacet,
1701
1760
  singleton: true
1702
1761
  });
1703
1762
  function setupEditorEventPlugin() {
@@ -1729,8 +1788,8 @@ function setupEditorEventPlugin() {
1729
1788
  setDropHandlers((_k = map.drop) != null ? _k : []);
1730
1789
  setScrollToSelectionHandlers((_l = map.scrollToSelection) != null ? _l : []);
1731
1790
  };
1732
- const plugin = new ProseMirrorPlugin3({
1733
- key: new PluginKey3("prosekit-editor-handler"),
1791
+ const plugin = new ProseMirrorPlugin4({
1792
+ key: new PluginKey3("prosekit-editor-event"),
1734
1793
  props: {
1735
1794
  handleKeyDown,
1736
1795
  handleKeyPress,
@@ -1753,7 +1812,7 @@ function setupEditorEventPlugin() {
1753
1812
  function defineFocusChangeHandler(handler) {
1754
1813
  const handleFocus = () => handler(true);
1755
1814
  const handleBlur = () => handler(false);
1756
- return domEventFacet.extension([
1815
+ return defineFacetPayload(domEventFacet, [
1757
1816
  ["focus", handleFocus],
1758
1817
  ["blur", handleBlur]
1759
1818
  ]);
@@ -1766,19 +1825,35 @@ import { history, redo, undo } from "@prosekit/pm/history";
1766
1825
  var isApple = typeof navigator !== "undefined" ? /Mac|iP(hone|[ao]d)/.test(navigator.platform) : false;
1767
1826
 
1768
1827
  // src/extensions/keymap.ts
1769
- import { baseKeymap, chainCommands } from "@prosekit/pm/commands";
1828
+ import {
1829
+ baseKeymap,
1830
+ chainCommands,
1831
+ createParagraphNear,
1832
+ liftEmptyBlock,
1833
+ newlineInCode
1834
+ } from "@prosekit/pm/commands";
1770
1835
  import { keydownHandler } from "@prosekit/pm/keymap";
1771
1836
  import { Plugin as Plugin3, PluginKey as PluginKey4 } from "@prosekit/pm/state";
1837
+ import { splitSplittableBlock } from "prosemirror-splittable";
1838
+ var customBaseKeymap = {
1839
+ ...baseKeymap,
1840
+ Enter: chainCommands(
1841
+ newlineInCode,
1842
+ createParagraphNear,
1843
+ liftEmptyBlock,
1844
+ splitSplittableBlock
1845
+ )
1846
+ };
1772
1847
  function defineKeymap(keymap) {
1773
- return keymapFacet.extension([keymap]);
1848
+ return defineFacetPayload(keymapFacet, [keymap]);
1774
1849
  }
1775
1850
  function defineBaseKeymap(options) {
1776
1851
  var _a;
1777
- const priority = (_a = options == null ? void 0 : options.priority) != null ? _a : 3 /* low */;
1778
- return withPriority(defineKeymap(baseKeymap), priority);
1852
+ const priority = (_a = options == null ? void 0 : options.priority) != null ? _a : 1 /* low */;
1853
+ return withPriority(defineKeymap(customBaseKeymap), priority);
1779
1854
  }
1780
- var keymapFacet = Facet.define({
1781
- converter: () => {
1855
+ var keymapFacet = defineFacet({
1856
+ reduce: () => {
1782
1857
  let handler = null;
1783
1858
  const handlerWrapper = (view, event) => {
1784
1859
  if (handler)
@@ -1789,19 +1864,17 @@ var keymapFacet = Facet.define({
1789
1864
  key: keymapPluginKey,
1790
1865
  props: { handleKeyDown: handlerWrapper }
1791
1866
  });
1792
- const pluginFunc = () => [plugin];
1793
- return {
1794
- create: (keymaps) => {
1795
- handler = keydownHandler(mergeKeymaps(keymaps));
1796
- return pluginFunc;
1797
- },
1798
- update: (keymaps) => {
1799
- handler = keydownHandler(mergeKeymaps(keymaps));
1800
- return null;
1801
- }
1867
+ return (keymaps) => {
1868
+ handler = keydownHandler(
1869
+ mergeKeymaps(
1870
+ // The keymap at the end have a higher priority.
1871
+ toReversed(keymaps)
1872
+ )
1873
+ );
1874
+ return plugin;
1802
1875
  };
1803
1876
  },
1804
- next: pluginFacet,
1877
+ parent: pluginFacet,
1805
1878
  singleton: true
1806
1879
  });
1807
1880
  function mergeKeymaps(keymaps) {
@@ -1843,14 +1916,14 @@ function defineHistory() {
1843
1916
  // src/extensions/mark-spec.ts
1844
1917
  function defineMarkSpec(options) {
1845
1918
  const payload = [options, void 0];
1846
- return markSpecFacet.extension([payload]);
1919
+ return defineFacetPayload(markSpecFacet, [payload]);
1847
1920
  }
1848
1921
  function defineMarkAttr(options) {
1849
1922
  const payload = [void 0, options];
1850
- return markSpecFacet.extension([payload]);
1923
+ return defineFacetPayload(markSpecFacet, [payload]);
1851
1924
  }
1852
- var markSpecFacet = Facet.define({
1853
- convert: (payloads) => {
1925
+ var markSpecFacet = defineFacet({
1926
+ reducer: (payloads) => {
1854
1927
  const marks = {};
1855
1928
  const specPayloads = payloads.map((input) => input[0]).filter(isNotNull);
1856
1929
  const attrPayloads = payloads.map((input) => input[1]).filter(isNotNull);
@@ -1927,37 +2000,42 @@ var markSpecFacet = Facet.define({
1927
2000
  }
1928
2001
  return { marks, nodes: {} };
1929
2002
  },
1930
- next: schemaFacet,
2003
+ parent: schemaSpecFacet,
1931
2004
  singleton: true
1932
2005
  });
1933
2006
 
1934
2007
  // src/extensions/node-view.ts
1935
- import { ProseMirrorPlugin as ProseMirrorPlugin4 } from "@prosekit/pm/state";
2008
+ import { PluginKey as PluginKey5, ProseMirrorPlugin as ProseMirrorPlugin5 } from "@prosekit/pm/state";
1936
2009
  import "@prosekit/pm/view";
1937
2010
  function defineNodeView(options) {
1938
- return nodeViewFacet.extension([options]);
2011
+ return defineFacetPayload(nodeViewFacet, [options]);
1939
2012
  }
1940
- var nodeViewFacet = Facet.define({
1941
- convert: (inputs) => {
2013
+ var nodeViewFacet = defineFacet({
2014
+ reducer: (inputs) => {
1942
2015
  const nodeViews = {};
1943
2016
  for (const input of inputs) {
1944
2017
  if (!nodeViews[input.name]) {
1945
2018
  nodeViews[input.name] = input.constructor;
1946
2019
  }
1947
2020
  }
1948
- return () => [new ProseMirrorPlugin4({ props: { nodeViews } })];
2021
+ return () => [
2022
+ new ProseMirrorPlugin5({
2023
+ key: new PluginKey5("prosekit-node-view"),
2024
+ props: { nodeViews }
2025
+ })
2026
+ ];
1949
2027
  },
1950
- next: pluginFacet
2028
+ parent: pluginFacet
1951
2029
  });
1952
2030
 
1953
2031
  // src/extensions/node-view-effect.ts
1954
- import { ProseMirrorPlugin as ProseMirrorPlugin5 } from "@prosekit/pm/state";
2032
+ import { PluginKey as PluginKey6, ProseMirrorPlugin as ProseMirrorPlugin6 } from "@prosekit/pm/state";
1955
2033
  import "@prosekit/pm/view";
1956
2034
  function defineNodeViewFactory(options) {
1957
- return nodeViewFactoryFacet.extension([options]);
2035
+ return defineFacetPayload(nodeViewFactoryFacet, [options]);
1958
2036
  }
1959
- var nodeViewFactoryFacet = Facet.define({
1960
- convert: (inputs) => {
2037
+ var nodeViewFactoryFacet = defineFacet({
2038
+ reducer: (inputs) => {
1961
2039
  const nodeViews = {};
1962
2040
  const options = {};
1963
2041
  const factories = {};
@@ -1979,9 +2057,14 @@ var nodeViewFactoryFacet = Facet.define({
1979
2057
  nodeViews[name] = factory(args);
1980
2058
  }
1981
2059
  }
1982
- return () => [new ProseMirrorPlugin5({ props: { nodeViews } })];
2060
+ return () => [
2061
+ new ProseMirrorPlugin6({
2062
+ key: new PluginKey6("prosekit-node-view-effect"),
2063
+ props: { nodeViews }
2064
+ })
2065
+ ];
1983
2066
  },
1984
- next: pluginFacet
2067
+ parent: pluginFacet
1985
2068
  });
1986
2069
 
1987
2070
  // src/extensions/paragraph.ts
@@ -1997,7 +2080,7 @@ function defineParagraphSpec() {
1997
2080
  });
1998
2081
  }
1999
2082
  function defineParagraph() {
2000
- return withPriority(defineParagraphSpec(), 0 /* highest */);
2083
+ return withPriority(defineParagraphSpec(), 4 /* highest */);
2001
2084
  }
2002
2085
 
2003
2086
  // src/extensions/text.ts
@@ -2077,12 +2160,12 @@ function withSkipCodeBlock(command) {
2077
2160
  export {
2078
2161
  Editor,
2079
2162
  EditorNotFoundError,
2080
- Facet,
2081
2163
  OBJECT_REPLACEMENT_CHARACTER,
2082
2164
  Priority,
2083
2165
  ProseKitError,
2084
2166
  getId as _getId,
2085
2167
  addMark,
2168
+ assert,
2086
2169
  canUseRegexLookbehind,
2087
2170
  clsx,
2088
2171
  createEditor,
@@ -2099,6 +2182,8 @@ export {
2099
2182
  defineDoubleClickHandler,
2100
2183
  defineDoubleClickOnHandler,
2101
2184
  defineDropHandler,
2185
+ defineFacet,
2186
+ defineFacetPayload,
2102
2187
  defineFocusChangeHandler,
2103
2188
  defineHistory,
2104
2189
  defineKeyDownHandler,