@portabletext/editor 1.48.14 → 1.48.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.
@@ -2394,7 +2394,16 @@ function compileType(rawType) {
2394
2394
  types: [rawType]
2395
2395
  }).get(rawType.name);
2396
2396
  }
2397
- const debug$b = debugWithName("operationToPatches");
2397
+ function createEditorPriority(config) {
2398
+ return {
2399
+ id: defaultKeyGenerator(),
2400
+ name: config?.name,
2401
+ reference: config?.reference
2402
+ };
2403
+ }
2404
+ const corePriority = createEditorPriority({
2405
+ name: "core"
2406
+ }), debug$b = debugWithName("operationToPatches");
2398
2407
  function createOperationToPatches(editorActor) {
2399
2408
  const textBlockName = editorActor.getSnapshot().context.schema.block.name;
2400
2409
  function insertTextPatch(editor, operation, beforeValue) {
@@ -5710,19 +5719,29 @@ function createInternalEditor(editorActor) {
5710
5719
  editorActorSnapshot: editorActor.getSnapshot(),
5711
5720
  slateEditorInstance: slateEditor.instance
5712
5721
  }),
5713
- registerBehavior: (config) => (editorActor.send({
5714
- type: "add behavior",
5715
- behavior: config.behavior
5716
- }), () => {
5717
- editorActor.send({
5718
- type: "remove behavior",
5719
- behavior: config.behavior
5720
- });
5721
- }),
5722
+ registerBehavior: (behaviorConfig) => {
5723
+ const priority = createEditorPriority({
5724
+ name: "custom",
5725
+ reference: {
5726
+ priority: corePriority,
5727
+ importance: "higher"
5728
+ }
5729
+ }), behaviorConfigWithPriority = {
5730
+ ...behaviorConfig,
5731
+ priority
5732
+ };
5733
+ return editorActor.send({
5734
+ type: "add behavior",
5735
+ behaviorConfig: behaviorConfigWithPriority
5736
+ }), () => {
5737
+ editorActor.send({
5738
+ type: "remove behavior",
5739
+ behaviorConfig: behaviorConfigWithPriority
5740
+ });
5741
+ };
5742
+ },
5722
5743
  send: (event) => {
5723
5744
  switch (event.type) {
5724
- case "add behavior":
5725
- case "remove behavior":
5726
5745
  case "update key generator":
5727
5746
  case "update readOnly":
5728
5747
  case "patches":
@@ -6466,7 +6485,10 @@ const arrowDownOnLonelyBlockObject = behaviors_index.defineBehavior({
6466
6485
  clearListOnEnter,
6467
6486
  indentListOnTab,
6468
6487
  unindentListOnShiftTab
6469
- }, coreBehaviors = [coreAnnotationBehaviors.addAnnotationOnCollapsedSelection, coreDecoratorBehaviors.strongShortcut, coreDecoratorBehaviors.emShortcut, coreDecoratorBehaviors.underlineShortcut, coreDecoratorBehaviors.codeShortcut, ...coreDndBehaviors, coreBlockObjectBehaviors.clickingAboveLonelyBlockObject, coreBlockObjectBehaviors.clickingBelowLonelyBlockObject, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace, coreListBehaviors.clearListOnEnter, coreListBehaviors.indentListOnTab, coreListBehaviors.unindentListOnShiftTab, coreInsertBreakBehaviors.breakingAtTheEndOfTextBlock, coreInsertBreakBehaviors.breakingAtTheStartOfTextBlock, coreInsertBreakBehaviors.breakingEntireDocument, coreInsertBreakBehaviors.breakingEntireBlocks], abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
6488
+ }, coreBehaviorsConfig = [coreAnnotationBehaviors.addAnnotationOnCollapsedSelection, coreDecoratorBehaviors.strongShortcut, coreDecoratorBehaviors.emShortcut, coreDecoratorBehaviors.underlineShortcut, coreDecoratorBehaviors.codeShortcut, ...coreDndBehaviors, coreBlockObjectBehaviors.clickingAboveLonelyBlockObject, coreBlockObjectBehaviors.clickingBelowLonelyBlockObject, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace, coreListBehaviors.clearListOnEnter, coreListBehaviors.indentListOnTab, coreListBehaviors.unindentListOnShiftTab, coreInsertBreakBehaviors.breakingAtTheEndOfTextBlock, coreInsertBreakBehaviors.breakingAtTheStartOfTextBlock, coreInsertBreakBehaviors.breakingEntireDocument, coreInsertBreakBehaviors.breakingEntireBlocks].map((behavior) => ({
6489
+ behavior,
6490
+ priority: corePriority
6491
+ })), abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
6470
6492
  on: "annotation.toggle",
6471
6493
  guard: ({
6472
6494
  snapshot,
@@ -7542,6 +7564,49 @@ function performEvent({
7542
7564
  });
7543
7565
  }), editor.onChange()) : nativeEventPrevented && nativeEvent?.preventDefault();
7544
7566
  }
7567
+ function sortByPriority(items) {
7568
+ if (items.length === 0)
7569
+ return [];
7570
+ const itemsWithPriority = items.filter((item) => item.priority !== void 0), itemsWithoutPriority = items.filter((item) => item.priority === void 0);
7571
+ if (itemsWithPriority.length === 0)
7572
+ return items;
7573
+ const itemsByPriorityId = new Map(itemsWithPriority.map((item) => [item.priority.id, item])), graph = /* @__PURE__ */ new Map(), inDegree = /* @__PURE__ */ new Map();
7574
+ function ensureNode(id) {
7575
+ graph.has(id) || (graph.set(id, /* @__PURE__ */ new Set()), inDegree.set(id, 0));
7576
+ }
7577
+ for (const item of itemsWithPriority) {
7578
+ const id = item.priority.id;
7579
+ ensureNode(id);
7580
+ }
7581
+ function addEdge(fromId, toId) {
7582
+ !graph.has(fromId) || !graph.has(toId) || (graph.get(fromId)?.add(toId), inDegree.set(toId, (inDegree.get(toId) ?? 0) + 1));
7583
+ }
7584
+ for (const item of itemsWithPriority) {
7585
+ const id = item.priority.id, visited = /* @__PURE__ */ new Set();
7586
+ let ref = item.priority.reference;
7587
+ for (; ref; ) {
7588
+ const refId = ref.priority.id;
7589
+ if (ensureNode(refId), visited.has(refId))
7590
+ throw new Error("Circular dependency detected in priorities");
7591
+ visited.add(refId), ref.importance === "higher" ? addEdge(id, refId) : addEdge(refId, id), ref = ref.priority.reference;
7592
+ }
7593
+ }
7594
+ const queue = [];
7595
+ for (const [id, degree] of inDegree)
7596
+ degree === 0 && queue.push(id);
7597
+ const result = [];
7598
+ for (; queue.length > 0; ) {
7599
+ const currentId = queue.shift(), currentItem = itemsByPriorityId.get(currentId);
7600
+ currentItem && result.push(currentItem);
7601
+ for (const neighborId of graph.get(currentId) ?? []) {
7602
+ const newDegree = (inDegree.get(neighborId) ?? 0) - 1;
7603
+ inDegree.set(neighborId, newDegree), newDegree === 0 && queue.push(neighborId);
7604
+ }
7605
+ }
7606
+ for (const item of itemsWithPriority)
7607
+ result.includes(item) || result.push(item);
7608
+ return [...result, ...itemsWithoutPriority];
7609
+ }
7545
7610
  function createEditorSnapshot({
7546
7611
  converters,
7547
7612
  editor,
@@ -7588,13 +7653,13 @@ const editorMachine = xstate.setup({
7588
7653
  behaviors: ({
7589
7654
  context,
7590
7655
  event
7591
- }) => (xstate.assertEvent(event, "add behavior"), /* @__PURE__ */ new Set([...context.behaviors, event.behavior]))
7656
+ }) => (xstate.assertEvent(event, "add behavior"), /* @__PURE__ */ new Set([...context.behaviors, event.behaviorConfig]))
7592
7657
  }),
7593
7658
  "remove behavior from context": xstate.assign({
7594
7659
  behaviors: ({
7595
7660
  context,
7596
7661
  event
7597
- }) => (xstate.assertEvent(event, "remove behavior"), context.behaviors.delete(event.behavior), /* @__PURE__ */ new Set([...context.behaviors]))
7662
+ }) => (xstate.assertEvent(event, "remove behavior"), context.behaviors.delete(event.behaviorConfig), /* @__PURE__ */ new Set([...context.behaviors]))
7598
7663
  }),
7599
7664
  "assign schema": xstate.assign({
7600
7665
  schema: ({
@@ -7688,10 +7753,11 @@ const editorMachine = xstate.setup({
7688
7753
  }) => {
7689
7754
  xstate.assertEvent(event, ["behavior event"]);
7690
7755
  try {
7756
+ const behaviors = sortByPriority([...context.behaviors.values(), ...coreBehaviorsConfig]).map((config) => config.behavior);
7691
7757
  performEvent({
7692
7758
  mode: "raise",
7693
- behaviors: [...context.behaviors.values(), ...coreBehaviors],
7694
- remainingEventBehaviors: [...context.behaviors.values(), ...coreBehaviors],
7759
+ behaviors,
7760
+ remainingEventBehaviors: behaviors,
7695
7761
  event: event.behaviorEvent,
7696
7762
  editor: event.editor,
7697
7763
  keyGenerator: context.keyGenerator,
@@ -8642,6 +8708,8 @@ exports.EditorActorContext = EditorActorContext;
8642
8708
  exports.EditorProvider = EditorProvider;
8643
8709
  exports.KEY_TO_VALUE_ELEMENT = KEY_TO_VALUE_ELEMENT;
8644
8710
  exports.PortableTextEditor = PortableTextEditor;
8711
+ exports.corePriority = corePriority;
8712
+ exports.createEditorPriority = createEditorPriority;
8645
8713
  exports.debugWithName = debugWithName;
8646
8714
  exports.defaultKeyGenerator = defaultKeyGenerator;
8647
8715
  exports.defineSchema = defineSchema;