@37signals/lexxy 0.9.9-beta.preview2 → 0.9.9-beta.preview3

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.
Files changed (2) hide show
  1. package/dist/lexxy.esm.js +46 -13
  2. package/package.json +1 -1
package/dist/lexxy.esm.js CHANGED
@@ -2655,6 +2655,13 @@ class RewritableHistoryExtension extends LexxyExtension {
2655
2655
  }
2656
2656
 
2657
2657
  #rewriteHistory(rewrites) {
2658
+ this.#applyRewritesImmediatelyToCurrentState(rewrites);
2659
+ this.#applyRewritesToHistory(rewrites);
2660
+
2661
+ return true
2662
+ }
2663
+
2664
+ #applyRewritesImmediatelyToCurrentState(rewrites) {
2658
2665
  $getEditor().update(() => {
2659
2666
  for (const [ nodeKey, { patch, replace } ] of Object.entries(rewrites)) {
2660
2667
  const node = $getNodeByKey(nodeKey);
@@ -2664,27 +2671,27 @@ class RewritableHistoryExtension extends LexxyExtension {
2664
2671
  if (replace) node.replace(replace);
2665
2672
  }
2666
2673
  }, { discrete: true, tag: this.#getBackgroundUpdateTags() });
2674
+ }
2667
2675
 
2676
+ #applyRewritesToHistory(rewrites) {
2668
2677
  const nodeKeys = Object.keys(rewrites);
2669
2678
 
2670
2679
  for (const entry of this.#allHistoryEntries) {
2671
2680
  if (!this.#entryHasSomeKeys(entry, nodeKeys)) continue
2672
2681
 
2682
+ const editorState = entry.editorState = safeCloneEditorState(entry.editorState);
2683
+
2673
2684
  for (const [ nodeKey, { patch, replace } ] of Object.entries(rewrites)) {
2674
- const node = entry.editorState._nodeMap.get(nodeKey);
2685
+ const node = editorState._nodeMap.get(nodeKey);
2675
2686
  if (!node) continue
2676
2687
 
2677
- entry.editorState = safeCloneEditorState(entry.editorState);
2678
-
2679
2688
  if (patch) {
2680
- entry.editorState._nodeMap.set(nodeKey, $cloneNodeWithPatch(node, patch));
2689
+ this.#patchNodeInEditorState(editorState, node, patch);
2681
2690
  } else if (replace) {
2682
- entry.editorState._nodeMap.set(nodeKey, $cloneNodeAdoptingKey(replace, node));
2691
+ this.#replaceNodeInEditorState(editorState, node, replace);
2683
2692
  }
2684
2693
  }
2685
2694
  }
2686
-
2687
- return true
2688
2695
  }
2689
2696
 
2690
2697
  #entryHasSomeKeys(entry, nodeKeys) {
@@ -2696,6 +2703,14 @@ class RewritableHistoryExtension extends LexxyExtension {
2696
2703
  if (!isEditorFocused(this.editorElement.editor)) { tags.push(SKIP_DOM_SELECTION_TAG); }
2697
2704
  return tags
2698
2705
  }
2706
+
2707
+ #patchNodeInEditorState(editorState, node, patch) {
2708
+ editorState._nodeMap.set(node.__key, $cloneNodeWithPatch(node, patch));
2709
+ }
2710
+
2711
+ #replaceNodeInEditorState(editorState, node, replaceWith) {
2712
+ editorState._nodeMap.set(node.__key, $cloneNodeAdoptingKeys(replaceWith, node));
2713
+ }
2699
2714
  }
2700
2715
 
2701
2716
  function $cloneNodeWithPatch(node, patch) {
@@ -2704,12 +2719,12 @@ function $cloneNodeWithPatch(node, patch) {
2704
2719
  return clone
2705
2720
  }
2706
2721
 
2707
- function $cloneNodeAdoptingKey(source, keyNode) {
2708
- const clone = $cloneWithProperties(source);
2709
- clone.__key = keyNode.__key;
2710
- clone.__parent = keyNode.__parent;
2711
- clone.__prev = keyNode.__prev;
2712
- clone.__next = keyNode.__next;
2722
+ function $cloneNodeAdoptingKeys(node, previousNode) {
2723
+ const clone = $cloneWithProperties(node);
2724
+ clone.__key = previousNode.__key;
2725
+ clone.__parent = previousNode.__parent;
2726
+ clone.__prev = previousNode.__prev;
2727
+ clone.__next = previousNode.__next;
2713
2728
  return clone
2714
2729
  }
2715
2730
 
@@ -6497,6 +6512,15 @@ class EarlyEscapeCodeNode extends CodeNode {
6497
6512
  insertNewAfter(selection, restoreSelection) {
6498
6513
  if (!selection.isCollapsed()) return super.insertNewAfter(selection, restoreSelection)
6499
6514
 
6515
+ // Clamp element-type selection offsets that may have been invalidated
6516
+ // by the code retokenizer. The retokenizer's $updateAndRetainSelection
6517
+ // restores the element offset verbatim after re-tokenizing, but when
6518
+ // highlight splits changed the child count before retokenization, the
6519
+ // restored offset can exceed the current child count. Without clamping,
6520
+ // CodeNode.insertNewAfter passes the stale offset to splice(), which
6521
+ // throws "start + deleteCount > oldSize".
6522
+ this.#clampSelectionOffset(selection);
6523
+
6500
6524
  if (this.#isCursorAtStart(selection)) {
6501
6525
  this.insertBefore($createParagraphNode());
6502
6526
  return null
@@ -6513,6 +6537,15 @@ class EarlyEscapeCodeNode extends CodeNode {
6513
6537
  return super.insertNewAfter(selection, restoreSelection)
6514
6538
  }
6515
6539
 
6540
+ #clampSelectionOffset(selection) {
6541
+ const childrenSize = this.getChildrenSize();
6542
+ for (const point of [ selection.anchor, selection.focus ]) {
6543
+ if (point.type === "element" && point.key === this.__key && point.offset > childrenSize) {
6544
+ point.set(this.__key, childrenSize, "element");
6545
+ }
6546
+ }
6547
+ }
6548
+
6516
6549
  #isCursorAtStart(selection) {
6517
6550
  const { anchor } = selection;
6518
6551
  if (!$isAtNodeStart(anchor)) return false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@37signals/lexxy",
3
- "version": "0.9.9-beta.preview2",
3
+ "version": "0.9.9-beta.preview3",
4
4
  "description": "Lexxy - A modern rich text editor for Rails.",
5
5
  "module": "dist/lexxy.esm.js",
6
6
  "type": "module",