@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.
@@ -167,7 +167,7 @@ var CommandNode = class extends BaseAstNode {
167
167
  arabicLatex() {
168
168
  const sup = this.superscript !== null ? this.superscript.arabicLatex() : "";
169
169
  const sub = this.subscript !== null ? this.subscript.arabicLatex() : "";
170
- const arabicName = this.name === "\\sqrt" ? "\\arabsqrt" : this.name;
170
+ const arabicName = this.name === "\\sqrt" || this.name === "\\arabsqrt" ? "\\arsqrt" : this.name;
171
171
  const content = renderCommandCore({
172
172
  name: arabicName,
173
173
  optionalArgs: this.optionalArgs,
@@ -2166,7 +2166,7 @@ function renderNodeArabic(node, options) {
2166
2166
  return arabicScripts(node, content, options);
2167
2167
  }
2168
2168
  if (node.kind === "sqrt" && node.radicand) {
2169
- const content = `\\arabsqrt${optionalRootIndexArabic(node, options)}{${renderChainArabic(node.radicand, options)}}`;
2169
+ const content = `\\arsqrt${optionalRootIndexArabic(node, options)}{${renderChainArabic(node.radicand, options)}}`;
2170
2170
  return arabicScripts(node, content, options);
2171
2171
  }
2172
2172
  if (node.kind === "overset" && node.baseExpr) {
@@ -5667,7 +5667,8 @@ ${BUTEX_FONT_FACE_CSS}
5667
5667
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
5668
5668
  }
5669
5669
 
5670
- .surface:focus {
5670
+ .surface:focus,
5671
+ .surface.surface--typing-focus {
5671
5672
  border-color: var(--butex-focus-border, rgba(13, 148, 136, 0.45));
5672
5673
  box-shadow: 0 0 0 3px var(--butex-focus-shadow, rgba(13, 148, 136, 0.12));
5673
5674
  }
@@ -5869,7 +5870,7 @@ function injectBuTeXEditorStyles(doc) {
5869
5870
 
5870
5871
  // src/editor/runtime.ts
5871
5872
  function createEditorRuntime(options) {
5872
- const { surfaceEl } = options;
5873
+ const { surfaceEl, inputEl } = options;
5873
5874
  const buttonElements = options.buttonElements ?? {};
5874
5875
  const outsidePointerIgnoreSelectors = options.outsidePointerIgnoreSelectors ?? [];
5875
5876
  let session = options.initialSession ?? createStarterSession();
@@ -5883,6 +5884,23 @@ function createEditorRuntime(options) {
5883
5884
  let dragSelecting = false;
5884
5885
  let dragChainId = null;
5885
5886
  let dragChanged = false;
5887
+ let composingInput = false;
5888
+ let pendingCompositionText = null;
5889
+ function resetInputElement() {
5890
+ if (!inputEl) {
5891
+ return;
5892
+ }
5893
+ inputEl.value = "";
5894
+ }
5895
+ function focusInput() {
5896
+ if (!inputEl) {
5897
+ surfaceEl.focus();
5898
+ return;
5899
+ }
5900
+ inputEl.focus({ preventScroll: true });
5901
+ const end = inputEl.value.length;
5902
+ inputEl.setSelectionRange(end, end);
5903
+ }
5886
5904
  function notifySessionChange() {
5887
5905
  options.onSessionChange?.(session);
5888
5906
  }
@@ -6147,7 +6165,7 @@ function createEditorRuntime(options) {
6147
6165
  event.preventDefault();
6148
6166
  event.stopPropagation();
6149
6167
  startDragSelection(chainId, index);
6150
- surfaceEl.focus();
6168
+ focusInput();
6151
6169
  });
6152
6170
  slot.addEventListener("click", (event) => {
6153
6171
  if (dragChanged) {
@@ -6157,7 +6175,7 @@ function createEditorRuntime(options) {
6157
6175
  return;
6158
6176
  }
6159
6177
  applyTransient((prev) => setCaret(prev, chainId, index));
6160
- surfaceEl.focus();
6178
+ focusInput();
6161
6179
  });
6162
6180
  if (session.caret.chainId === chainId && session.caret.index === index) {
6163
6181
  const caret = document.createElement("span");
@@ -6189,7 +6207,7 @@ function createEditorRuntime(options) {
6189
6207
  event.preventDefault();
6190
6208
  event.stopPropagation();
6191
6209
  startDragSelection(chainId, indexInChain);
6192
- surfaceEl.focus();
6210
+ focusInput();
6193
6211
  });
6194
6212
  nodeEl.addEventListener("click", (event) => {
6195
6213
  if (dragChanged) {
@@ -6200,7 +6218,7 @@ function createEditorRuntime(options) {
6200
6218
  }
6201
6219
  event.stopPropagation();
6202
6220
  applyTransient((prev) => selectNode(prev, node.id));
6203
- surfaceEl.focus();
6221
+ focusInput();
6204
6222
  });
6205
6223
  const body = document.createElement("span");
6206
6224
  body.className = "node-body";
@@ -6235,7 +6253,7 @@ function createEditorRuntime(options) {
6235
6253
  const activeTree = session[activeTreeKey(session.activeSide)];
6236
6254
  surfaceEl.appendChild(buildChain(activeTree));
6237
6255
  if (initialKeyboardFocusPending) {
6238
- requestAnimationFrame(() => surfaceEl.focus());
6256
+ requestAnimationFrame(focusInput);
6239
6257
  initialKeyboardFocusPending = false;
6240
6258
  }
6241
6259
  notifySessionChange();
@@ -6273,7 +6291,7 @@ function createEditorRuntime(options) {
6273
6291
  }
6274
6292
  session = restored;
6275
6293
  render();
6276
- surfaceEl.focus();
6294
+ focusInput();
6277
6295
  }
6278
6296
  function performRedo() {
6279
6297
  const restored = restoreRedo(undoRedo, session);
@@ -6282,9 +6300,12 @@ function createEditorRuntime(options) {
6282
6300
  }
6283
6301
  session = restored;
6284
6302
  render();
6285
- surfaceEl.focus();
6303
+ focusInput();
6286
6304
  }
6287
6305
  function onKeyDown(event) {
6306
+ if (event.isComposing || event.key === "Process" || event.keyCode === 229) {
6307
+ return;
6308
+ }
6288
6309
  const shortcutMod = event.ctrlKey || event.metaKey;
6289
6310
  const keyLower = typeof event.key === "string" ? event.key.toLowerCase() : "";
6290
6311
  if (shortcutMod && !event.altKey && keyLower === "z") {
@@ -6383,6 +6404,70 @@ function createEditorRuntime(options) {
6383
6404
  applyStructural((prev) => insertChar(prev, char));
6384
6405
  }
6385
6406
  }
6407
+ function onBeforeInput(event) {
6408
+ if (composingInput || event.isComposing) {
6409
+ return;
6410
+ }
6411
+ if (event.inputType === "deleteContentBackward") {
6412
+ event.preventDefault();
6413
+ applyStructural((prev) => deleteBackward(prev));
6414
+ resetInputElement();
6415
+ return;
6416
+ }
6417
+ if (event.inputType === "deleteContentForward") {
6418
+ event.preventDefault();
6419
+ applyStructural((prev) => deleteForward(prev));
6420
+ resetInputElement();
6421
+ }
6422
+ }
6423
+ function onInput(event) {
6424
+ if (!inputEl || composingInput) {
6425
+ return;
6426
+ }
6427
+ const inputEvent = event;
6428
+ const text = inputEvent.data ?? inputEl.value;
6429
+ resetInputElement();
6430
+ if (pendingCompositionText !== null && (inputEvent.inputType === "insertFromComposition" || text === pendingCompositionText)) {
6431
+ pendingCompositionText = null;
6432
+ return;
6433
+ }
6434
+ pendingCompositionText = null;
6435
+ if (inputEvent.inputType === "deleteContentBackward") {
6436
+ applyStructural((prev) => deleteBackward(prev));
6437
+ return;
6438
+ }
6439
+ if (inputEvent.inputType === "deleteContentForward") {
6440
+ applyStructural((prev) => deleteForward(prev));
6441
+ return;
6442
+ }
6443
+ if (text) {
6444
+ applyStructural((prev) => pasteText(prev, text));
6445
+ }
6446
+ }
6447
+ function onCompositionStart() {
6448
+ composingInput = true;
6449
+ }
6450
+ function onCompositionEnd(event) {
6451
+ if (!inputEl) {
6452
+ return;
6453
+ }
6454
+ composingInput = false;
6455
+ const text = event.data || inputEl.value;
6456
+ resetInputElement();
6457
+ if (text) {
6458
+ applyStructural((prev) => pasteText(prev, text));
6459
+ }
6460
+ pendingCompositionText = text || null;
6461
+ window.setTimeout(() => {
6462
+ pendingCompositionText = null;
6463
+ }, 0);
6464
+ }
6465
+ function onInputFocus() {
6466
+ surfaceEl.classList.add("surface--typing-focus");
6467
+ }
6468
+ function onInputBlur() {
6469
+ surfaceEl.classList.remove("surface--typing-focus");
6470
+ }
6386
6471
  function isShortcutOnlyEvent(event) {
6387
6472
  if (!(event.ctrlKey || event.metaKey) || event.altKey) {
6388
6473
  return false;
@@ -6432,7 +6517,7 @@ function createEditorRuntime(options) {
6432
6517
  }
6433
6518
  event.preventDefault();
6434
6519
  startDragSelection(resolved.chainId, resolved.slotIndex);
6435
- surfaceEl.focus();
6520
+ focusInput();
6436
6521
  }
6437
6522
  function onSurfaceClick(event) {
6438
6523
  if (dragChanged) {
@@ -6449,7 +6534,7 @@ function createEditorRuntime(options) {
6449
6534
  } else {
6450
6535
  clearSelectionAndNodeHighlights();
6451
6536
  }
6452
- surfaceEl.focus();
6537
+ focusInput();
6453
6538
  }
6454
6539
  function onDocumentPointerDown(event) {
6455
6540
  const target = event.target;
@@ -6465,6 +6550,13 @@ function createEditorRuntime(options) {
6465
6550
  clearSelectionAndNodeHighlights();
6466
6551
  }
6467
6552
  surfaceEl.addEventListener("keydown", onKeyDown);
6553
+ inputEl?.addEventListener("keydown", onKeyDown);
6554
+ inputEl?.addEventListener("beforeinput", onBeforeInput);
6555
+ inputEl?.addEventListener("input", onInput);
6556
+ inputEl?.addEventListener("compositionstart", onCompositionStart);
6557
+ inputEl?.addEventListener("compositionend", onCompositionEnd);
6558
+ inputEl?.addEventListener("focus", onInputFocus);
6559
+ inputEl?.addEventListener("blur", onInputBlur);
6468
6560
  document.addEventListener("keydown", onDocumentKeyDown);
6469
6561
  document.addEventListener("mouseup", endDragSelection);
6470
6562
  document.addEventListener("mouseleave", endDragSelection);
@@ -6476,6 +6568,13 @@ function createEditorRuntime(options) {
6476
6568
  render,
6477
6569
  destroy: () => {
6478
6570
  surfaceEl.removeEventListener("keydown", onKeyDown);
6571
+ inputEl?.removeEventListener("keydown", onKeyDown);
6572
+ inputEl?.removeEventListener("beforeinput", onBeforeInput);
6573
+ inputEl?.removeEventListener("input", onInput);
6574
+ inputEl?.removeEventListener("compositionstart", onCompositionStart);
6575
+ inputEl?.removeEventListener("compositionend", onCompositionEnd);
6576
+ inputEl?.removeEventListener("focus", onInputFocus);
6577
+ inputEl?.removeEventListener("blur", onInputBlur);
6479
6578
  document.removeEventListener("keydown", onDocumentKeyDown);
6480
6579
  document.removeEventListener("mouseup", endDragSelection);
6481
6580
  document.removeEventListener("mouseleave", endDragSelection);
@@ -6496,6 +6595,7 @@ function createEditorRuntime(options) {
6496
6595
  uiLocale = locale;
6497
6596
  render();
6498
6597
  },
6598
+ focusInput,
6499
6599
  performUndo,
6500
6600
  performRedo,
6501
6601
  performCopy,
@@ -6503,27 +6603,27 @@ function createEditorRuntime(options) {
6503
6603
  performPaste,
6504
6604
  toggleSide: () => {
6505
6605
  applyTransient((prev) => switchSide(prev));
6506
- surfaceEl.focus();
6606
+ focusInput();
6507
6607
  },
6508
6608
  toggleSplitLeafTyping: () => {
6509
6609
  applyStructural((prev) => setSplitLeafTyping(prev, !prev.splitLeafTyping));
6510
- surfaceEl.focus();
6610
+ focusInput();
6511
6611
  },
6512
6612
  toggleArabicReverseNumberTyping: () => {
6513
6613
  applyStructural((prev) => setArabicReverseNumberTyping(prev, !prev.arabicReverseNumberTyping));
6514
- surfaceEl.focus();
6614
+ focusInput();
6515
6615
  },
6516
6616
  toggleTakweenCharacterFont: () => {
6517
6617
  applyTransient((prev) => toggleTakweenCharacterFont(prev));
6518
- surfaceEl.focus();
6618
+ focusInput();
6519
6619
  },
6520
6620
  setCurrentCharacterFont: (fontId) => {
6521
6621
  applyTransient((prev) => setCurrentCharacterFont(prev, fontId));
6522
- surfaceEl.focus();
6622
+ focusInput();
6523
6623
  },
6524
6624
  setCurrentDigitForm: (digitForm) => {
6525
6625
  applyStructural((prev) => setCurrentDigitForm(prev, digitForm));
6526
- surfaceEl.focus();
6626
+ focusInput();
6527
6627
  },
6528
6628
  insertDelimiterByKind: (kind) => {
6529
6629
  if (kind === "paren") {
@@ -6533,79 +6633,79 @@ function createEditorRuntime(options) {
6533
6633
  } else {
6534
6634
  applyStructural((prev) => insertDelimiterPair(prev, "\\left\\{", "\\right\\}"));
6535
6635
  }
6536
- surfaceEl.focus();
6636
+ focusInput();
6537
6637
  },
6538
6638
  insertFraction: () => {
6539
6639
  applyStructural((prev) => insertFraction(prev));
6540
- surfaceEl.focus();
6640
+ focusInput();
6541
6641
  },
6542
6642
  insertFirstDerivative: () => {
6543
6643
  applyStructural((prev) => insertFirstDerivative(prev));
6544
- surfaceEl.focus();
6644
+ focusInput();
6545
6645
  },
6546
6646
  insertSecondDerivative: () => {
6547
6647
  applyStructural((prev) => insertSecondDerivative(prev));
6548
- surfaceEl.focus();
6648
+ focusInput();
6549
6649
  },
6550
6650
  insertMatrixEnv: (style, rows, columns) => {
6551
6651
  applyStructural((prev) => insertMatrixEnv(prev, style, rows, columns));
6552
- surfaceEl.focus();
6652
+ focusInput();
6553
6653
  },
6554
6654
  insertArrayEnv: (rows, columns) => {
6555
6655
  applyStructural((prev) => insertArrayEnv(prev, rows, columns));
6556
- surfaceEl.focus();
6656
+ focusInput();
6557
6657
  },
6558
6658
  insertAlignedEnv: (rows, columns) => {
6559
6659
  applyStructural((prev) => insertAlignedEnv(prev, rows, columns));
6560
- surfaceEl.focus();
6660
+ focusInput();
6561
6661
  },
6562
6662
  insertSqrt: () => {
6563
6663
  applyStructural((prev) => insertSqrt(prev));
6564
- surfaceEl.focus();
6664
+ focusInput();
6565
6665
  },
6566
6666
  insertOverset: () => {
6567
6667
  applyStructural((prev) => insertOverset(prev));
6568
- surfaceEl.focus();
6668
+ focusInput();
6569
6669
  },
6570
6670
  insertUnderset: () => {
6571
6671
  applyStructural((prev) => insertUnderset(prev));
6572
- surfaceEl.focus();
6672
+ focusInput();
6573
6673
  },
6574
6674
  insertLimitsSeriesTemplate: (commandId) => {
6575
6675
  applyStructural((prev) => insertLimitsSeriesTemplate(prev, commandId));
6576
- surfaceEl.focus();
6676
+ focusInput();
6577
6677
  },
6578
6678
  insertAccentPair: () => {
6579
6679
  applyStructural((prev) => insertAccentPair(prev));
6580
- surfaceEl.focus();
6680
+ focusInput();
6581
6681
  },
6582
6682
  insertAccent: (accentId) => {
6583
6683
  applyStructural((prev) => insertAccent(prev, accentId));
6584
- surfaceEl.focus();
6684
+ focusInput();
6585
6685
  },
6586
6686
  insertAtomicCommand: (commandId) => {
6587
6687
  applyStructural((prev) => insertAtomicCommand(prev, commandId));
6588
- surfaceEl.focus();
6688
+ focusInput();
6589
6689
  },
6590
6690
  insertAtomicOperatorCommand: (commandId) => {
6591
6691
  applyStructural((prev) => insertAtomicOperatorCommand(prev, commandId));
6592
- surfaceEl.focus();
6692
+ focusInput();
6593
6693
  },
6594
6694
  addSup: () => {
6595
6695
  applyStructural((prev) => addSup(prev));
6596
- surfaceEl.focus();
6696
+ focusInput();
6597
6697
  },
6598
6698
  addSub: () => {
6599
6699
  applyStructural((prev) => addSub(prev));
6600
- surfaceEl.focus();
6700
+ focusInput();
6601
6701
  },
6602
6702
  removeSup: () => {
6603
6703
  applyStructural((prev) => removeSup(prev));
6604
- surfaceEl.focus();
6704
+ focusInput();
6605
6705
  },
6606
6706
  removeSub: () => {
6607
6707
  applyStructural((prev) => removeSub(prev));
6608
- surfaceEl.focus();
6708
+ focusInput();
6609
6709
  },
6610
6710
  setMatrixEnvStyle: (style) => {
6611
6711
  applyStructural((prev) => setMatrixEnvStyle(prev, style));
@@ -6627,7 +6727,7 @@ function createEditorRuntime(options) {
6627
6727
  },
6628
6728
  deleteStructure: () => {
6629
6729
  applyStructural((prev) => deleteStructure(prev));
6630
- surfaceEl.focus();
6730
+ focusInput();
6631
6731
  }
6632
6732
  };
6633
6733
  render();
@@ -6889,7 +6989,7 @@ function commandToEditorNode(node) {
6889
6989
  }
6890
6990
  return { editable: true, node: frac };
6891
6991
  }
6892
- if ((node.name === "\\sqrt" || node.name === "\\arabsqrt") && node.mandatoryArgs.length >= 1) {
6992
+ if ((node.name === "\\sqrt" || node.name === "\\arabsqrt" || node.name === "\\arsqrt") && node.mandatoryArgs.length >= 1) {
6893
6993
  const sqrt = createSqrtNode();
6894
6994
  const radicand = chainToEditorChain(node.mandatoryArgs[0]);
6895
6995
  if (!radicand.editable) {
@@ -8177,6 +8277,22 @@ var WIDGET_CHROME_CSS = `
8177
8277
  padding: 10px 12px;
8178
8278
  }
8179
8279
 
8280
+ .butex-widget .butex-editor-input-bridge {
8281
+ border: 0;
8282
+ font-size: 16px;
8283
+ height: 1px;
8284
+ margin: 0;
8285
+ max-height: 1px;
8286
+ min-height: 1px;
8287
+ opacity: 0;
8288
+ overflow: hidden;
8289
+ padding: 0;
8290
+ pointer-events: none;
8291
+ position: absolute;
8292
+ resize: none;
8293
+ width: 1px;
8294
+ }
8295
+
8180
8296
  .butex-widget .toolbar {
8181
8297
  display: flex;
8182
8298
  flex-wrap: wrap;
@@ -9183,6 +9299,7 @@ var ButexEditor = forwardRef(
9183
9299
  const messages = equationEditorMessages(uiLocale);
9184
9300
  const wrapperRef = useRef(null);
9185
9301
  const surfaceRef = useRef(null);
9302
+ const inputRef = useRef(null);
9186
9303
  const mathOutputRef = useRef(null);
9187
9304
  const renderErrorRef = useRef(null);
9188
9305
  const latexLinesRef = useRef(null);
@@ -9294,6 +9411,7 @@ ${arabic || currentMessages.empty}`;
9294
9411
  }
9295
9412
  const runtime = createEditorRuntime({
9296
9413
  surfaceEl,
9414
+ inputEl: inputRef.current,
9297
9415
  initialSession,
9298
9416
  defaultSide,
9299
9417
  uiLocale,
@@ -9335,7 +9453,7 @@ ${arabic || currentMessages.empty}`;
9335
9453
  } else {
9336
9454
  runtime.render();
9337
9455
  }
9338
- surfaceEl.focus();
9456
+ runtime.focusInput();
9339
9457
  return () => {
9340
9458
  runtime.destroy();
9341
9459
  runtimeRef.current = null;
@@ -9349,7 +9467,7 @@ ${arabic || currentMessages.empty}`;
9349
9467
  }
9350
9468
  }, [uiLocale]);
9351
9469
  function focusSurface() {
9352
- surfaceRef.current?.focus();
9470
+ runtimeRef.current?.focusInput();
9353
9471
  }
9354
9472
  function insertSelectedEnvironment(rows, columns) {
9355
9473
  if (envPaletteMode === "array") {
@@ -10427,6 +10545,20 @@ ${arabic || currentMessages.empty}`;
10427
10545
  "aria-label": messages.equationEditor
10428
10546
  }
10429
10547
  ),
10548
+ /* @__PURE__ */ jsx(
10549
+ "textarea",
10550
+ {
10551
+ ref: inputRef,
10552
+ className: "butex-editor-input-bridge",
10553
+ "aria-label": messages.equationEditor,
10554
+ autoCapitalize: "none",
10555
+ autoComplete: "off",
10556
+ autoCorrect: "off",
10557
+ inputMode: "text",
10558
+ spellCheck: false,
10559
+ tabIndex: -1
10560
+ }
10561
+ ),
10430
10562
  /* @__PURE__ */ jsx("div", { ref: renderErrorRef, className: "error", hidden: true })
10431
10563
  ] }),
10432
10564
  /* @__PURE__ */ jsx("section", { className: "panel panel-preview", "aria-label": messages.renderPreview, children: /* @__PURE__ */ jsx("div", { ref: mathOutputRef, className: "preview-box" }) }),