@drghaliasri/butex 4.1.0 → 4.2.0

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/index.js CHANGED
@@ -561,6 +561,16 @@ var ATOMIC_COMMANDS = {
561
561
  arabicLabel: "\u0645\u062D\u062F\u062F",
562
562
  title: "\u0645\u062D\u062F\u062F"
563
563
  },
564
+ ////////////////////// Derivatives ////////////////////////////////
565
+ ad: {
566
+ id: "ad",
567
+ category: "derivative",
568
+ englishTex: "\\mathrm{d}",
569
+ englishLabel: "d",
570
+ arabicTex: "\\ad",
571
+ arabicLabel: "\u0621",
572
+ title: "\u0639\u0644\u0627\u0645\u0629 \u0627\u0644\u0627\u0634\u062A\u0642\u0627\u0642"
573
+ },
564
574
  ////////////////////// Groups /////////////////////////////////
565
575
  N: {
566
576
  id: "N",
@@ -1240,6 +1250,7 @@ var ENGLISH_COMMAND_TITLES = {
1240
1250
  ln: "Natural logarithm",
1241
1251
  log: "Logarithm",
1242
1252
  det: "Determinant",
1253
+ ad: "Derivative (d / ad)",
1243
1254
  N: "Natural numbers",
1244
1255
  Z: "Integers",
1245
1256
  Q: "Rational numbers",
@@ -1351,6 +1362,10 @@ var EQUATION_EDITOR_MESSAGES = {
1351
1362
  insertFunction: "\u0625\u062F\u0631\u0627\u062C \u062F\u0627\u0644\u0629",
1352
1363
  insertLimitsSeries: "\u0625\u062F\u0631\u0627\u062C \u062D\u062F\u0648\u062F \u0648\u0645\u062A\u0633\u0644\u0633\u0644\u0627\u062A",
1353
1364
  insertAdditionalFunctions: "\u0625\u062F\u0631\u0627\u062C \u062F\u0648\u0627\u0644 \u0625\u0636\u0627\u0641\u064A\u0629",
1365
+ derivatives: "\u0627\u0644\u0627\u0634\u062A\u0642\u0627\u0642",
1366
+ insertDerivative: "\u0625\u062F\u0631\u0627\u062C \u0639\u0644\u0627\u0645\u0629 \u0627\u0634\u062A\u0642\u0627\u0642",
1367
+ insertFirstDerivative: "\u0625\u062F\u0631\u0627\u062C \u0627\u0634\u062A\u0642\u0627\u0642 \u0623\u0648\u0644",
1368
+ insertSecondDerivative: "\u0625\u062F\u0631\u0627\u062C \u0627\u0634\u062A\u0642\u0627\u0642 \u062B\u0627\u0646\u064A",
1354
1369
  insertNumberSets: "\u0625\u062F\u0631\u0627\u062C \u0645\u062C\u0645\u0648\u0639\u0627\u062A \u0623\u0639\u062F\u0627\u062F",
1355
1370
  operations: "\u0627\u0644\u0639\u0645\u0644\u064A\u0627\u062A",
1356
1371
  insertOperation: "\u0625\u062F\u0631\u0627\u062C \u0631\u0645\u0632 \u0639\u0645\u0644\u064A\u0629",
@@ -1454,6 +1469,10 @@ var EQUATION_EDITOR_MESSAGES = {
1454
1469
  insertFunction: "Insert function",
1455
1470
  insertLimitsSeries: "Insert limits and series",
1456
1471
  insertAdditionalFunctions: "Insert additional functions",
1472
+ derivatives: "Derivatives",
1473
+ insertDerivative: "Insert derivative marker",
1474
+ insertFirstDerivative: "Insert first derivative",
1475
+ insertSecondDerivative: "Insert second derivative",
1457
1476
  insertNumberSets: "Insert number sets",
1458
1477
  operations: "Operators",
1459
1478
  insertOperation: "Insert operator",
@@ -1620,7 +1639,7 @@ function buildFunctionAtomicCommandBody(node, side, spec) {
1620
1639
  value.appendChild(label);
1621
1640
  return value;
1622
1641
  }
1623
- value.textContent = side === "arabic" ? spec?.arabicLabel ?? node.expr : spec?.englishTex.replace("\\", "") ?? node.expr;
1642
+ value.textContent = side === "arabic" ? spec?.arabicLabel ?? node.expr : spec?.englishLabel ?? spec?.englishTex.replace(/^\\/, "") ?? node.expr;
1624
1643
  return value;
1625
1644
  }
1626
1645
  function buildSpacingAtomicCommandBody(spec, side, uiLocale) {
@@ -2524,10 +2543,13 @@ __export(editor_exports, {
2524
2543
  insertAtomicOperatorCommand: () => insertAtomicOperatorCommand,
2525
2544
  insertChar: () => insertChar,
2526
2545
  insertDelimiterPair: () => insertDelimiterPair,
2546
+ insertDivideOperator: () => insertDivideOperator,
2547
+ insertFirstDerivative: () => insertFirstDerivative,
2527
2548
  insertFraction: () => insertFraction,
2528
2549
  insertMatrixEnv: () => insertMatrixEnv,
2529
2550
  insertNumber: () => insertNumber,
2530
2551
  insertOperator: () => insertOperator,
2552
+ insertSecondDerivative: () => insertSecondDerivative,
2531
2553
  insertSqrt: () => insertSqrt,
2532
2554
  isSelectionActive: () => isSelectionActive,
2533
2555
  isSpacingCommandSpec: () => isSpacingCommandSpec,
@@ -2740,6 +2762,22 @@ function createEditorSession(englishTree, arabicTree, activeSide = "arabic") {
2740
2762
  };
2741
2763
  }
2742
2764
 
2765
+ // src/editor/divideOperator.ts
2766
+ var DIVIDE_OPERATOR_EN = "/";
2767
+ var DIVIDE_OPERATOR_AR = "\\";
2768
+ function isDivideOperatorExpr(expr) {
2769
+ return expr === DIVIDE_OPERATOR_EN || expr === DIVIDE_OPERATOR_AR;
2770
+ }
2771
+ function divideOperatorSurfaceExpr(expr, side) {
2772
+ if (!isDivideOperatorExpr(expr)) {
2773
+ return expr;
2774
+ }
2775
+ return side === "arabic" ? DIVIDE_OPERATOR_AR : DIVIDE_OPERATOR_EN;
2776
+ }
2777
+ function renderDivideOperatorLatex(side) {
2778
+ return side === "arabic" ? "\\backslash" : DIVIDE_OPERATOR_EN;
2779
+ }
2780
+
2743
2781
  // src/editor/digits.ts
2744
2782
  var WESTERN = "0123456789";
2745
2783
  var ARABIC_INDIC = "\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669";
@@ -2888,6 +2926,9 @@ function renderNodeEnglish(node, options) {
2888
2926
  if (node.kind === "char") {
2889
2927
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
2890
2928
  }
2929
+ if (node.kind === "operator" && isDivideOperatorExpr(node.expr)) {
2930
+ return `${renderDivideOperatorLatex("english")}${latinScripts(node, options)}`;
2931
+ }
2891
2932
  return `${formatDigits(node.expr, selectedDigitForm(options))}${latinScripts(node, options)}`;
2892
2933
  }
2893
2934
  function renderNodeArabic(node, options) {
@@ -2922,6 +2963,9 @@ function renderNodeArabic(node, options) {
2922
2963
  const content = renderCharContent(node, options);
2923
2964
  return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
2924
2965
  }
2966
+ if (node.kind === "operator" && isDivideOperatorExpr(node.expr)) {
2967
+ return arabicScripts(node, renderDivideOperatorLatex("arabic"), options);
2968
+ }
2925
2969
  return arabicScripts(node, formatDigits(node.expr, selectedDigitForm(options)), options);
2926
2970
  }
2927
2971
  function renderChainEnglish(chain, options) {
@@ -3448,6 +3492,128 @@ function undoRedoCanRedo(stacks) {
3448
3492
  return stacks.future.length > 0;
3449
3493
  }
3450
3494
 
3495
+ // src/editor/derivativeTemplates.ts
3496
+ var ARABIC_VAR = "\u0633";
3497
+ var ENGLISH_VAR = "x";
3498
+ function mirroredAd() {
3499
+ const english = createAtomicCommandNode("ad");
3500
+ const arabic = createAtomicCommandNode("ad");
3501
+ arabic.id = english.id;
3502
+ return { english, arabic };
3503
+ }
3504
+ function mirroredVar() {
3505
+ const english = createEditorNode("char", ENGLISH_VAR);
3506
+ const arabic = createEditorNode("char", ARABIC_VAR);
3507
+ arabic.id = english.id;
3508
+ return { english, arabic };
3509
+ }
3510
+ function withMirroredSuperscript2(english, arabic) {
3511
+ const englishSup = createEditorChain([createEditorNode("number", "2")]);
3512
+ const arabicSup = createEditorChain([createEditorNode("number", "2")]);
3513
+ arabicSup.id = englishSup.id;
3514
+ arabicSup.nodes[0].id = englishSup.nodes[0].id;
3515
+ english.superscript = englishSup;
3516
+ arabic.superscript = arabicSup;
3517
+ }
3518
+ function syncChainIds(english, arabic) {
3519
+ arabic.id = english.id;
3520
+ const limit = Math.min(english.nodes.length, arabic.nodes.length);
3521
+ for (let index = 0; index < limit; index += 1) {
3522
+ arabic.nodes[index].id = english.nodes[index].id;
3523
+ }
3524
+ }
3525
+ function syncFracIds(englishFrac, arabicFrac) {
3526
+ arabicFrac.id = englishFrac.id;
3527
+ if (englishFrac.numerator && arabicFrac.numerator) {
3528
+ syncChainIds(englishFrac.numerator, arabicFrac.numerator);
3529
+ }
3530
+ if (englishFrac.denominator && arabicFrac.denominator) {
3531
+ syncChainIds(englishFrac.denominator, arabicFrac.denominator);
3532
+ }
3533
+ }
3534
+ function buildAdVarChain() {
3535
+ const ad = mirroredAd();
3536
+ const variable = mirroredVar();
3537
+ const english = createEditorChain([ad.english, variable.english]);
3538
+ const arabic = createEditorChain([ad.arabic, variable.arabic]);
3539
+ syncChainIds(english, arabic);
3540
+ return { english, arabic };
3541
+ }
3542
+ function mirroredEmptyDelimiter() {
3543
+ const english = createDelimiterNode("", "");
3544
+ const arabic = createDelimiterNode("", "");
3545
+ arabic.id = english.id;
3546
+ if (english.innerExpr && arabic.innerExpr) {
3547
+ syncChainIds(english.innerExpr, arabic.innerExpr);
3548
+ }
3549
+ return { english, arabic };
3550
+ }
3551
+ function setMirroredDelimiterInner(english, arabic, pairs) {
3552
+ const englishInner = createEditorChain(pairs.map((pair) => pair.english));
3553
+ const arabicInner = createEditorChain(pairs.map((pair) => pair.arabic));
3554
+ syncChainIds(englishInner, arabicInner);
3555
+ english.innerExpr = englishInner;
3556
+ arabic.innerExpr = arabicInner;
3557
+ }
3558
+ function buildFirstDerivativeFrac() {
3559
+ const englishFrac = createFracNode();
3560
+ const arabicFrac = createFracNode();
3561
+ syncFracIds(englishFrac, arabicFrac);
3562
+ const numerator = buildAdVarChain();
3563
+ const denominator = buildAdVarChain();
3564
+ englishFrac.numerator = numerator.english;
3565
+ englishFrac.denominator = denominator.english;
3566
+ arabicFrac.numerator = numerator.arabic;
3567
+ arabicFrac.denominator = denominator.arabic;
3568
+ return { english: englishFrac, arabic: arabicFrac };
3569
+ }
3570
+ function buildSecondDerivativeFrac() {
3571
+ const englishFrac = createFracNode();
3572
+ const arabicFrac = createFracNode();
3573
+ syncFracIds(englishFrac, arabicFrac);
3574
+ const numAd = mirroredAd();
3575
+ const numVar = mirroredVar();
3576
+ withMirroredSuperscript2(numAd.english, numAd.arabic);
3577
+ const englishNumerator = createEditorChain([numAd.english, numVar.english]);
3578
+ const arabicNumerator = createEditorChain([numAd.arabic, numVar.arabic]);
3579
+ syncChainIds(englishNumerator, arabicNumerator);
3580
+ englishFrac.numerator = englishNumerator;
3581
+ arabicFrac.numerator = arabicNumerator;
3582
+ const denGroup = mirroredEmptyDelimiter();
3583
+ const denInnerAd = mirroredAd();
3584
+ const denInnerVar = mirroredVar();
3585
+ setMirroredDelimiterInner(denGroup.english, denGroup.arabic, [
3586
+ { english: denInnerAd.english, arabic: denInnerAd.arabic },
3587
+ { english: denInnerVar.english, arabic: denInnerVar.arabic }
3588
+ ]);
3589
+ withMirroredSuperscript2(denGroup.english, denGroup.arabic);
3590
+ englishFrac.denominator = createEditorChain([denGroup.english]);
3591
+ arabicFrac.denominator = createEditorChain([denGroup.arabic]);
3592
+ syncChainIds(englishFrac.denominator, arabicFrac.denominator);
3593
+ return { english: englishFrac, arabic: arabicFrac };
3594
+ }
3595
+ var DERIVATIVE_TEMPLATE_VAR_CARET_INDEX = 1;
3596
+ function derivativeTemplateInitialCaret(englishFrac) {
3597
+ const denominator = englishFrac.denominator;
3598
+ if (!denominator) {
3599
+ return {
3600
+ chainId: englishFrac.numerator?.id ?? "",
3601
+ index: DERIVATIVE_TEMPLATE_VAR_CARET_INDEX
3602
+ };
3603
+ }
3604
+ const loneDelimiter = denominator.nodes.length === 1 && denominator.nodes[0]?.kind === "delimiter" && denominator.nodes[0].innerExpr;
3605
+ if (loneDelimiter) {
3606
+ return {
3607
+ chainId: denominator.nodes[0].innerExpr.id,
3608
+ index: DERIVATIVE_TEMPLATE_VAR_CARET_INDEX
3609
+ };
3610
+ }
3611
+ return {
3612
+ chainId: denominator.id,
3613
+ index: DERIVATIVE_TEMPLATE_VAR_CARET_INDEX
3614
+ };
3615
+ }
3616
+
3451
3617
  // src/editor/state.ts
3452
3618
  function ensureSyncEntry(session, nodeId) {
3453
3619
  if (!session.sync[nodeId]) {
@@ -4084,10 +4250,27 @@ function insertNumber(session, expr) {
4084
4250
  inserted.forceSplitNextLeaf = false;
4085
4251
  return inserted;
4086
4252
  }
4253
+ function insertDivideOperator(session) {
4254
+ const base = replaceSelectionFirst(session);
4255
+ const next = cloneEditorSession(base);
4256
+ const englishNode = createEditorNode("operator", DIVIDE_OPERATOR_EN);
4257
+ const arabicNode = createEditorNode("operator", DIVIDE_OPERATOR_AR);
4258
+ arabicNode.id = englishNode.id;
4259
+ insertMirroredNode(next, next.caret.chainId, next.caret.index, englishNode, arabicNode);
4260
+ markExprSync(next, englishNode.id);
4261
+ next.caret.index += 1;
4262
+ next.selectedStructureNodeId = null;
4263
+ syncSelectionToCaret(next);
4264
+ appendDebug(next, "insertDivideOperator", "");
4265
+ return next;
4266
+ }
4087
4267
  function insertOperator(session, expr) {
4088
4268
  if (expr === "." && !session.splitLeafTyping && precedingIsExtendableNumber(session)) {
4089
4269
  return insertNumber(session, ".");
4090
4270
  }
4271
+ if (expr === DIVIDE_OPERATOR_EN || expr === DIVIDE_OPERATOR_AR) {
4272
+ return insertDivideOperator(session);
4273
+ }
4091
4274
  const base = replaceSelectionFirst(session);
4092
4275
  return insertLeaf(base, "operator", expr);
4093
4276
  }
@@ -4158,6 +4341,24 @@ function insertFraction(session) {
4158
4341
  appendDebug(next, "insertFraction", "");
4159
4342
  return next;
4160
4343
  }
4344
+ function insertDerivativeFrac(session, build, debugLabel) {
4345
+ const base = replaceSelectionFirst(session);
4346
+ const next = cloneEditorSession(base);
4347
+ const { english, arabic } = build();
4348
+ insertMirroredNode(next, next.caret.chainId, next.caret.index, english, arabic);
4349
+ syncSubtreeRecursive(next, english, arabic);
4350
+ next.caret = derivativeTemplateInitialCaret(english);
4351
+ next.selectedStructureNodeId = null;
4352
+ syncSelectionToCaret(next);
4353
+ appendDebug(next, debugLabel, "");
4354
+ return next;
4355
+ }
4356
+ function insertFirstDerivative(session) {
4357
+ return insertDerivativeFrac(session, buildFirstDerivativeFrac, "insertFirstDerivative");
4358
+ }
4359
+ function insertSecondDerivative(session) {
4360
+ return insertDerivativeFrac(session, buildSecondDerivativeFrac, "insertSecondDerivative");
4361
+ }
4161
4362
  function insertSqrt(session) {
4162
4363
  const base = replaceSelectionFirst(session);
4163
4364
  const next = cloneEditorSession(base);
@@ -4654,6 +4855,11 @@ function pasteText(session, text) {
4654
4855
  i += 1;
4655
4856
  continue;
4656
4857
  }
4858
+ if (ch === DIVIDE_OPERATOR_AR) {
4859
+ s = insertDivideOperator(s);
4860
+ i += 1;
4861
+ continue;
4862
+ }
4657
4863
  s = insertChar(s, ch);
4658
4864
  i += 1;
4659
4865
  }
@@ -4661,7 +4867,7 @@ function pasteText(session, text) {
4661
4867
  }
4662
4868
 
4663
4869
  // src/editor/display/base.ts
4664
- function buildBaseNodeBody(node, digitForm = "western") {
4870
+ function buildBaseNodeBody(node, digitForm = "western", side = "english") {
4665
4871
  const value = document.createElement("span");
4666
4872
  value.className = "node-value";
4667
4873
  if (node.kind === "char" && node.characterFont === "takween") {
@@ -4671,7 +4877,8 @@ function buildBaseNodeBody(node, digitForm = "western") {
4671
4877
  } else if (node.kind === "char" && node.characterFont === "diwaniOutline") {
4672
4878
  value.classList.add("node-value--diwani-outline");
4673
4879
  }
4674
- value.textContent = node.expr === "" ? "\u25A1" : formatDigits(node.expr, digitForm);
4880
+ const surfaceExpr = node.kind === "operator" ? divideOperatorSurfaceExpr(node.expr, side) : node.expr;
4881
+ value.textContent = surfaceExpr === "" ? "\u25A1" : formatDigits(surfaceExpr, digitForm);
4675
4882
  return value;
4676
4883
  }
4677
4884
 
@@ -4725,7 +4932,7 @@ function buildNodeBody(node, buildChain, side, digitForm = "western", uiLocale =
4725
4932
  if (node.kind === "atomicOperatorCommand") {
4726
4933
  return buildAtomicOperatorCommandNodeBody(node, side, uiLocale);
4727
4934
  }
4728
- return buildBaseNodeBody(node, digitForm);
4935
+ return buildBaseNodeBody(node, digitForm, side);
4729
4936
  }
4730
4937
 
4731
4938
  // src/editor/runtime.ts
@@ -5233,6 +5440,10 @@ function createEditorRuntime(options) {
5233
5440
  applyStructural((prev) => insertNumber(prev, char));
5234
5441
  return;
5235
5442
  }
5443
+ if (char === "\\") {
5444
+ applyStructural((prev) => insertDivideOperator(prev));
5445
+ return;
5446
+ }
5236
5447
  if (isOperator(char)) {
5237
5448
  applyStructural((prev) => insertOperator(prev, char));
5238
5449
  return;
@@ -5396,6 +5607,14 @@ function createEditorRuntime(options) {
5396
5607
  applyStructural((prev) => insertFraction(prev));
5397
5608
  surfaceEl.focus();
5398
5609
  },
5610
+ insertFirstDerivative: () => {
5611
+ applyStructural((prev) => insertFirstDerivative(prev));
5612
+ surfaceEl.focus();
5613
+ },
5614
+ insertSecondDerivative: () => {
5615
+ applyStructural((prev) => insertSecondDerivative(prev));
5616
+ surfaceEl.focus();
5617
+ },
5399
5618
  insertMatrixEnv: (style, rows, columns) => {
5400
5619
  applyStructural((prev) => insertMatrixEnv(prev, style, rows, columns));
5401
5620
  surfaceEl.focus();