@drghaliasri/butex 5.5.0 → 5.5.2

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.
package/dist/react.mjs CHANGED
@@ -1420,7 +1420,7 @@ function renderNodeArabic(node, options) {
1420
1420
  return arabicScripts(node, content, options);
1421
1421
  }
1422
1422
  if (node.kind === "sqrt" && node.radicand) {
1423
- const content = `\\arabsqrt${optionalRootIndexArabic(node, options)}{${renderChainArabic(node.radicand, options)}}`;
1423
+ const content = `\\arsqrt${optionalRootIndexArabic(node, options)}{${renderChainArabic(node.radicand, options)}}`;
1424
1424
  return arabicScripts(node, content, options);
1425
1425
  }
1426
1426
  if (node.kind === "overset" && node.baseExpr) {
@@ -4921,7 +4921,8 @@ ${BUTEX_FONT_FACE_CSS}
4921
4921
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
4922
4922
  }
4923
4923
 
4924
- .surface:focus {
4924
+ .surface:focus,
4925
+ .surface.surface--typing-focus {
4925
4926
  border-color: var(--butex-focus-border, rgba(13, 148, 136, 0.45));
4926
4927
  box-shadow: 0 0 0 3px var(--butex-focus-shadow, rgba(13, 148, 136, 0.12));
4927
4928
  }
@@ -5123,7 +5124,7 @@ function injectBuTeXEditorStyles(doc) {
5123
5124
 
5124
5125
  // src/editor/runtime.ts
5125
5126
  function createEditorRuntime(options) {
5126
- const { surfaceEl } = options;
5127
+ const { surfaceEl, inputEl } = options;
5127
5128
  const buttonElements = options.buttonElements ?? {};
5128
5129
  const outsidePointerIgnoreSelectors = options.outsidePointerIgnoreSelectors ?? [];
5129
5130
  let session = options.initialSession ?? createStarterSession();
@@ -5137,6 +5138,23 @@ function createEditorRuntime(options) {
5137
5138
  let dragSelecting = false;
5138
5139
  let dragChainId = null;
5139
5140
  let dragChanged = false;
5141
+ let composingInput = false;
5142
+ let pendingCompositionText = null;
5143
+ function resetInputElement() {
5144
+ if (!inputEl) {
5145
+ return;
5146
+ }
5147
+ inputEl.value = "";
5148
+ }
5149
+ function focusInput() {
5150
+ if (!inputEl) {
5151
+ surfaceEl.focus();
5152
+ return;
5153
+ }
5154
+ inputEl.focus({ preventScroll: true });
5155
+ const end = inputEl.value.length;
5156
+ inputEl.setSelectionRange(end, end);
5157
+ }
5140
5158
  function notifySessionChange() {
5141
5159
  options.onSessionChange?.(session);
5142
5160
  }
@@ -5401,7 +5419,7 @@ function createEditorRuntime(options) {
5401
5419
  event.preventDefault();
5402
5420
  event.stopPropagation();
5403
5421
  startDragSelection(chainId, index);
5404
- surfaceEl.focus();
5422
+ focusInput();
5405
5423
  });
5406
5424
  slot.addEventListener("click", (event) => {
5407
5425
  if (dragChanged) {
@@ -5411,7 +5429,7 @@ function createEditorRuntime(options) {
5411
5429
  return;
5412
5430
  }
5413
5431
  applyTransient((prev) => setCaret(prev, chainId, index));
5414
- surfaceEl.focus();
5432
+ focusInput();
5415
5433
  });
5416
5434
  if (session.caret.chainId === chainId && session.caret.index === index) {
5417
5435
  const caret = document.createElement("span");
@@ -5443,7 +5461,7 @@ function createEditorRuntime(options) {
5443
5461
  event.preventDefault();
5444
5462
  event.stopPropagation();
5445
5463
  startDragSelection(chainId, indexInChain);
5446
- surfaceEl.focus();
5464
+ focusInput();
5447
5465
  });
5448
5466
  nodeEl.addEventListener("click", (event) => {
5449
5467
  if (dragChanged) {
@@ -5454,7 +5472,7 @@ function createEditorRuntime(options) {
5454
5472
  }
5455
5473
  event.stopPropagation();
5456
5474
  applyTransient((prev) => selectNode(prev, node.id));
5457
- surfaceEl.focus();
5475
+ focusInput();
5458
5476
  });
5459
5477
  const body = document.createElement("span");
5460
5478
  body.className = "node-body";
@@ -5489,7 +5507,7 @@ function createEditorRuntime(options) {
5489
5507
  const activeTree = session[activeTreeKey(session.activeSide)];
5490
5508
  surfaceEl.appendChild(buildChain(activeTree));
5491
5509
  if (initialKeyboardFocusPending) {
5492
- requestAnimationFrame(() => surfaceEl.focus());
5510
+ requestAnimationFrame(focusInput);
5493
5511
  initialKeyboardFocusPending = false;
5494
5512
  }
5495
5513
  notifySessionChange();
@@ -5527,7 +5545,7 @@ function createEditorRuntime(options) {
5527
5545
  }
5528
5546
  session = restored;
5529
5547
  render();
5530
- surfaceEl.focus();
5548
+ focusInput();
5531
5549
  }
5532
5550
  function performRedo() {
5533
5551
  const restored = restoreRedo(undoRedo, session);
@@ -5536,9 +5554,12 @@ function createEditorRuntime(options) {
5536
5554
  }
5537
5555
  session = restored;
5538
5556
  render();
5539
- surfaceEl.focus();
5557
+ focusInput();
5540
5558
  }
5541
5559
  function onKeyDown(event) {
5560
+ if (event.isComposing || event.key === "Process" || event.keyCode === 229) {
5561
+ return;
5562
+ }
5542
5563
  const shortcutMod = event.ctrlKey || event.metaKey;
5543
5564
  const keyLower = typeof event.key === "string" ? event.key.toLowerCase() : "";
5544
5565
  if (shortcutMod && !event.altKey && keyLower === "z") {
@@ -5637,6 +5658,70 @@ function createEditorRuntime(options) {
5637
5658
  applyStructural((prev) => insertChar(prev, char));
5638
5659
  }
5639
5660
  }
5661
+ function onBeforeInput(event) {
5662
+ if (composingInput || event.isComposing) {
5663
+ return;
5664
+ }
5665
+ if (event.inputType === "deleteContentBackward") {
5666
+ event.preventDefault();
5667
+ applyStructural((prev) => deleteBackward(prev));
5668
+ resetInputElement();
5669
+ return;
5670
+ }
5671
+ if (event.inputType === "deleteContentForward") {
5672
+ event.preventDefault();
5673
+ applyStructural((prev) => deleteForward(prev));
5674
+ resetInputElement();
5675
+ }
5676
+ }
5677
+ function onInput(event) {
5678
+ if (!inputEl || composingInput) {
5679
+ return;
5680
+ }
5681
+ const inputEvent = event;
5682
+ const text = inputEvent.data ?? inputEl.value;
5683
+ resetInputElement();
5684
+ if (pendingCompositionText !== null && (inputEvent.inputType === "insertFromComposition" || text === pendingCompositionText)) {
5685
+ pendingCompositionText = null;
5686
+ return;
5687
+ }
5688
+ pendingCompositionText = null;
5689
+ if (inputEvent.inputType === "deleteContentBackward") {
5690
+ applyStructural((prev) => deleteBackward(prev));
5691
+ return;
5692
+ }
5693
+ if (inputEvent.inputType === "deleteContentForward") {
5694
+ applyStructural((prev) => deleteForward(prev));
5695
+ return;
5696
+ }
5697
+ if (text) {
5698
+ applyStructural((prev) => pasteText(prev, text));
5699
+ }
5700
+ }
5701
+ function onCompositionStart() {
5702
+ composingInput = true;
5703
+ }
5704
+ function onCompositionEnd(event) {
5705
+ if (!inputEl) {
5706
+ return;
5707
+ }
5708
+ composingInput = false;
5709
+ const text = event.data || inputEl.value;
5710
+ resetInputElement();
5711
+ if (text) {
5712
+ applyStructural((prev) => pasteText(prev, text));
5713
+ }
5714
+ pendingCompositionText = text || null;
5715
+ window.setTimeout(() => {
5716
+ pendingCompositionText = null;
5717
+ }, 0);
5718
+ }
5719
+ function onInputFocus() {
5720
+ surfaceEl.classList.add("surface--typing-focus");
5721
+ }
5722
+ function onInputBlur() {
5723
+ surfaceEl.classList.remove("surface--typing-focus");
5724
+ }
5640
5725
  function isShortcutOnlyEvent(event) {
5641
5726
  if (!(event.ctrlKey || event.metaKey) || event.altKey) {
5642
5727
  return false;
@@ -5686,7 +5771,7 @@ function createEditorRuntime(options) {
5686
5771
  }
5687
5772
  event.preventDefault();
5688
5773
  startDragSelection(resolved.chainId, resolved.slotIndex);
5689
- surfaceEl.focus();
5774
+ focusInput();
5690
5775
  }
5691
5776
  function onSurfaceClick(event) {
5692
5777
  if (dragChanged) {
@@ -5703,7 +5788,7 @@ function createEditorRuntime(options) {
5703
5788
  } else {
5704
5789
  clearSelectionAndNodeHighlights();
5705
5790
  }
5706
- surfaceEl.focus();
5791
+ focusInput();
5707
5792
  }
5708
5793
  function onDocumentPointerDown(event) {
5709
5794
  const target = event.target;
@@ -5719,6 +5804,13 @@ function createEditorRuntime(options) {
5719
5804
  clearSelectionAndNodeHighlights();
5720
5805
  }
5721
5806
  surfaceEl.addEventListener("keydown", onKeyDown);
5807
+ inputEl?.addEventListener("keydown", onKeyDown);
5808
+ inputEl?.addEventListener("beforeinput", onBeforeInput);
5809
+ inputEl?.addEventListener("input", onInput);
5810
+ inputEl?.addEventListener("compositionstart", onCompositionStart);
5811
+ inputEl?.addEventListener("compositionend", onCompositionEnd);
5812
+ inputEl?.addEventListener("focus", onInputFocus);
5813
+ inputEl?.addEventListener("blur", onInputBlur);
5722
5814
  document.addEventListener("keydown", onDocumentKeyDown);
5723
5815
  document.addEventListener("mouseup", endDragSelection);
5724
5816
  document.addEventListener("mouseleave", endDragSelection);
@@ -5730,6 +5822,13 @@ function createEditorRuntime(options) {
5730
5822
  render,
5731
5823
  destroy: () => {
5732
5824
  surfaceEl.removeEventListener("keydown", onKeyDown);
5825
+ inputEl?.removeEventListener("keydown", onKeyDown);
5826
+ inputEl?.removeEventListener("beforeinput", onBeforeInput);
5827
+ inputEl?.removeEventListener("input", onInput);
5828
+ inputEl?.removeEventListener("compositionstart", onCompositionStart);
5829
+ inputEl?.removeEventListener("compositionend", onCompositionEnd);
5830
+ inputEl?.removeEventListener("focus", onInputFocus);
5831
+ inputEl?.removeEventListener("blur", onInputBlur);
5733
5832
  document.removeEventListener("keydown", onDocumentKeyDown);
5734
5833
  document.removeEventListener("mouseup", endDragSelection);
5735
5834
  document.removeEventListener("mouseleave", endDragSelection);
@@ -5750,6 +5849,7 @@ function createEditorRuntime(options) {
5750
5849
  uiLocale = locale;
5751
5850
  render();
5752
5851
  },
5852
+ focusInput,
5753
5853
  performUndo,
5754
5854
  performRedo,
5755
5855
  performCopy,
@@ -5757,27 +5857,27 @@ function createEditorRuntime(options) {
5757
5857
  performPaste,
5758
5858
  toggleSide: () => {
5759
5859
  applyTransient((prev) => switchSide(prev));
5760
- surfaceEl.focus();
5860
+ focusInput();
5761
5861
  },
5762
5862
  toggleSplitLeafTyping: () => {
5763
5863
  applyStructural((prev) => setSplitLeafTyping(prev, !prev.splitLeafTyping));
5764
- surfaceEl.focus();
5864
+ focusInput();
5765
5865
  },
5766
5866
  toggleArabicReverseNumberTyping: () => {
5767
5867
  applyStructural((prev) => setArabicReverseNumberTyping(prev, !prev.arabicReverseNumberTyping));
5768
- surfaceEl.focus();
5868
+ focusInput();
5769
5869
  },
5770
5870
  toggleTakweenCharacterFont: () => {
5771
5871
  applyTransient((prev) => toggleTakweenCharacterFont(prev));
5772
- surfaceEl.focus();
5872
+ focusInput();
5773
5873
  },
5774
5874
  setCurrentCharacterFont: (fontId) => {
5775
5875
  applyTransient((prev) => setCurrentCharacterFont(prev, fontId));
5776
- surfaceEl.focus();
5876
+ focusInput();
5777
5877
  },
5778
5878
  setCurrentDigitForm: (digitForm) => {
5779
5879
  applyStructural((prev) => setCurrentDigitForm(prev, digitForm));
5780
- surfaceEl.focus();
5880
+ focusInput();
5781
5881
  },
5782
5882
  insertDelimiterByKind: (kind) => {
5783
5883
  if (kind === "paren") {
@@ -5787,79 +5887,79 @@ function createEditorRuntime(options) {
5787
5887
  } else {
5788
5888
  applyStructural((prev) => insertDelimiterPair(prev, "\\left\\{", "\\right\\}"));
5789
5889
  }
5790
- surfaceEl.focus();
5890
+ focusInput();
5791
5891
  },
5792
5892
  insertFraction: () => {
5793
5893
  applyStructural((prev) => insertFraction(prev));
5794
- surfaceEl.focus();
5894
+ focusInput();
5795
5895
  },
5796
5896
  insertFirstDerivative: () => {
5797
5897
  applyStructural((prev) => insertFirstDerivative(prev));
5798
- surfaceEl.focus();
5898
+ focusInput();
5799
5899
  },
5800
5900
  insertSecondDerivative: () => {
5801
5901
  applyStructural((prev) => insertSecondDerivative(prev));
5802
- surfaceEl.focus();
5902
+ focusInput();
5803
5903
  },
5804
5904
  insertMatrixEnv: (style, rows, columns) => {
5805
5905
  applyStructural((prev) => insertMatrixEnv(prev, style, rows, columns));
5806
- surfaceEl.focus();
5906
+ focusInput();
5807
5907
  },
5808
5908
  insertArrayEnv: (rows, columns) => {
5809
5909
  applyStructural((prev) => insertArrayEnv(prev, rows, columns));
5810
- surfaceEl.focus();
5910
+ focusInput();
5811
5911
  },
5812
5912
  insertAlignedEnv: (rows, columns) => {
5813
5913
  applyStructural((prev) => insertAlignedEnv(prev, rows, columns));
5814
- surfaceEl.focus();
5914
+ focusInput();
5815
5915
  },
5816
5916
  insertSqrt: () => {
5817
5917
  applyStructural((prev) => insertSqrt(prev));
5818
- surfaceEl.focus();
5918
+ focusInput();
5819
5919
  },
5820
5920
  insertOverset: () => {
5821
5921
  applyStructural((prev) => insertOverset(prev));
5822
- surfaceEl.focus();
5922
+ focusInput();
5823
5923
  },
5824
5924
  insertUnderset: () => {
5825
5925
  applyStructural((prev) => insertUnderset(prev));
5826
- surfaceEl.focus();
5926
+ focusInput();
5827
5927
  },
5828
5928
  insertLimitsSeriesTemplate: (commandId) => {
5829
5929
  applyStructural((prev) => insertLimitsSeriesTemplate(prev, commandId));
5830
- surfaceEl.focus();
5930
+ focusInput();
5831
5931
  },
5832
5932
  insertAccentPair: () => {
5833
5933
  applyStructural((prev) => insertAccentPair(prev));
5834
- surfaceEl.focus();
5934
+ focusInput();
5835
5935
  },
5836
5936
  insertAccent: (accentId) => {
5837
5937
  applyStructural((prev) => insertAccent(prev, accentId));
5838
- surfaceEl.focus();
5938
+ focusInput();
5839
5939
  },
5840
5940
  insertAtomicCommand: (commandId) => {
5841
5941
  applyStructural((prev) => insertAtomicCommand(prev, commandId));
5842
- surfaceEl.focus();
5942
+ focusInput();
5843
5943
  },
5844
5944
  insertAtomicOperatorCommand: (commandId) => {
5845
5945
  applyStructural((prev) => insertAtomicOperatorCommand(prev, commandId));
5846
- surfaceEl.focus();
5946
+ focusInput();
5847
5947
  },
5848
5948
  addSup: () => {
5849
5949
  applyStructural((prev) => addSup(prev));
5850
- surfaceEl.focus();
5950
+ focusInput();
5851
5951
  },
5852
5952
  addSub: () => {
5853
5953
  applyStructural((prev) => addSub(prev));
5854
- surfaceEl.focus();
5954
+ focusInput();
5855
5955
  },
5856
5956
  removeSup: () => {
5857
5957
  applyStructural((prev) => removeSup(prev));
5858
- surfaceEl.focus();
5958
+ focusInput();
5859
5959
  },
5860
5960
  removeSub: () => {
5861
5961
  applyStructural((prev) => removeSub(prev));
5862
- surfaceEl.focus();
5962
+ focusInput();
5863
5963
  },
5864
5964
  setMatrixEnvStyle: (style) => {
5865
5965
  applyStructural((prev) => setMatrixEnvStyle(prev, style));
@@ -5881,7 +5981,7 @@ function createEditorRuntime(options) {
5881
5981
  },
5882
5982
  deleteStructure: () => {
5883
5983
  applyStructural((prev) => deleteStructure(prev));
5884
- surfaceEl.focus();
5984
+ focusInput();
5885
5985
  }
5886
5986
  };
5887
5987
  render();
@@ -6348,6 +6448,22 @@ var WIDGET_CHROME_CSS = `
6348
6448
  padding: 10px 12px;
6349
6449
  }
6350
6450
 
6451
+ .butex-widget .butex-editor-input-bridge {
6452
+ border: 0;
6453
+ font-size: 16px;
6454
+ height: 1px;
6455
+ margin: 0;
6456
+ max-height: 1px;
6457
+ min-height: 1px;
6458
+ opacity: 0;
6459
+ overflow: hidden;
6460
+ padding: 0;
6461
+ pointer-events: none;
6462
+ position: absolute;
6463
+ resize: none;
6464
+ width: 1px;
6465
+ }
6466
+
6351
6467
  .butex-widget .toolbar {
6352
6468
  display: flex;
6353
6469
  flex-wrap: wrap;
@@ -7354,6 +7470,7 @@ var ButexEditor = forwardRef(
7354
7470
  const messages = equationEditorMessages(uiLocale);
7355
7471
  const wrapperRef = useRef(null);
7356
7472
  const surfaceRef = useRef(null);
7473
+ const inputRef = useRef(null);
7357
7474
  const mathOutputRef = useRef(null);
7358
7475
  const renderErrorRef = useRef(null);
7359
7476
  const latexLinesRef = useRef(null);
@@ -7465,6 +7582,7 @@ ${arabic || currentMessages.empty}`;
7465
7582
  }
7466
7583
  const runtime = createEditorRuntime({
7467
7584
  surfaceEl,
7585
+ inputEl: inputRef.current,
7468
7586
  initialSession,
7469
7587
  defaultSide,
7470
7588
  uiLocale,
@@ -7506,7 +7624,7 @@ ${arabic || currentMessages.empty}`;
7506
7624
  } else {
7507
7625
  runtime.render();
7508
7626
  }
7509
- surfaceEl.focus();
7627
+ runtime.focusInput();
7510
7628
  return () => {
7511
7629
  runtime.destroy();
7512
7630
  runtimeRef.current = null;
@@ -7520,7 +7638,7 @@ ${arabic || currentMessages.empty}`;
7520
7638
  }
7521
7639
  }, [uiLocale]);
7522
7640
  function focusSurface() {
7523
- surfaceRef.current?.focus();
7641
+ runtimeRef.current?.focusInput();
7524
7642
  }
7525
7643
  function insertSelectedEnvironment(rows, columns) {
7526
7644
  if (envPaletteMode === "array") {
@@ -8598,6 +8716,20 @@ ${arabic || currentMessages.empty}`;
8598
8716
  "aria-label": messages.equationEditor
8599
8717
  }
8600
8718
  ),
8719
+ /* @__PURE__ */ jsx(
8720
+ "textarea",
8721
+ {
8722
+ ref: inputRef,
8723
+ className: "butex-editor-input-bridge",
8724
+ "aria-label": messages.equationEditor,
8725
+ autoCapitalize: "none",
8726
+ autoComplete: "off",
8727
+ autoCorrect: "off",
8728
+ inputMode: "text",
8729
+ spellCheck: false,
8730
+ tabIndex: -1
8731
+ }
8732
+ ),
8601
8733
  /* @__PURE__ */ jsx("div", { ref: renderErrorRef, className: "error", hidden: true })
8602
8734
  ] }),
8603
8735
  /* @__PURE__ */ jsx("section", { className: "panel panel-preview", "aria-label": messages.renderPreview, children: /* @__PURE__ */ jsx("div", { ref: mathOutputRef, className: "preview-box" }) }),