@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.mjs CHANGED
@@ -519,6 +519,16 @@ var ATOMIC_COMMANDS = {
519
519
  arabicLabel: "\u0645\u062D\u062F\u062F",
520
520
  title: "\u0645\u062D\u062F\u062F"
521
521
  },
522
+ ////////////////////// Derivatives ////////////////////////////////
523
+ ad: {
524
+ id: "ad",
525
+ category: "derivative",
526
+ englishTex: "\\mathrm{d}",
527
+ englishLabel: "d",
528
+ arabicTex: "\\ad",
529
+ arabicLabel: "\u0621",
530
+ title: "\u0639\u0644\u0627\u0645\u0629 \u0627\u0644\u0627\u0634\u062A\u0642\u0627\u0642"
531
+ },
522
532
  ////////////////////// Groups /////////////////////////////////
523
533
  N: {
524
534
  id: "N",
@@ -1198,6 +1208,7 @@ var ENGLISH_COMMAND_TITLES = {
1198
1208
  ln: "Natural logarithm",
1199
1209
  log: "Logarithm",
1200
1210
  det: "Determinant",
1211
+ ad: "Derivative (d / ad)",
1201
1212
  N: "Natural numbers",
1202
1213
  Z: "Integers",
1203
1214
  Q: "Rational numbers",
@@ -1309,6 +1320,10 @@ var EQUATION_EDITOR_MESSAGES = {
1309
1320
  insertFunction: "\u0625\u062F\u0631\u0627\u062C \u062F\u0627\u0644\u0629",
1310
1321
  insertLimitsSeries: "\u0625\u062F\u0631\u0627\u062C \u062D\u062F\u0648\u062F \u0648\u0645\u062A\u0633\u0644\u0633\u0644\u0627\u062A",
1311
1322
  insertAdditionalFunctions: "\u0625\u062F\u0631\u0627\u062C \u062F\u0648\u0627\u0644 \u0625\u0636\u0627\u0641\u064A\u0629",
1323
+ derivatives: "\u0627\u0644\u0627\u0634\u062A\u0642\u0627\u0642",
1324
+ insertDerivative: "\u0625\u062F\u0631\u0627\u062C \u0639\u0644\u0627\u0645\u0629 \u0627\u0634\u062A\u0642\u0627\u0642",
1325
+ insertFirstDerivative: "\u0625\u062F\u0631\u0627\u062C \u0627\u0634\u062A\u0642\u0627\u0642 \u0623\u0648\u0644",
1326
+ insertSecondDerivative: "\u0625\u062F\u0631\u0627\u062C \u0627\u0634\u062A\u0642\u0627\u0642 \u062B\u0627\u0646\u064A",
1312
1327
  insertNumberSets: "\u0625\u062F\u0631\u0627\u062C \u0645\u062C\u0645\u0648\u0639\u0627\u062A \u0623\u0639\u062F\u0627\u062F",
1313
1328
  operations: "\u0627\u0644\u0639\u0645\u0644\u064A\u0627\u062A",
1314
1329
  insertOperation: "\u0625\u062F\u0631\u0627\u062C \u0631\u0645\u0632 \u0639\u0645\u0644\u064A\u0629",
@@ -1412,6 +1427,10 @@ var EQUATION_EDITOR_MESSAGES = {
1412
1427
  insertFunction: "Insert function",
1413
1428
  insertLimitsSeries: "Insert limits and series",
1414
1429
  insertAdditionalFunctions: "Insert additional functions",
1430
+ derivatives: "Derivatives",
1431
+ insertDerivative: "Insert derivative marker",
1432
+ insertFirstDerivative: "Insert first derivative",
1433
+ insertSecondDerivative: "Insert second derivative",
1415
1434
  insertNumberSets: "Insert number sets",
1416
1435
  operations: "Operators",
1417
1436
  insertOperation: "Insert operator",
@@ -1578,7 +1597,7 @@ function buildFunctionAtomicCommandBody(node, side, spec) {
1578
1597
  value.appendChild(label);
1579
1598
  return value;
1580
1599
  }
1581
- value.textContent = side === "arabic" ? spec?.arabicLabel ?? node.expr : spec?.englishTex.replace("\\", "") ?? node.expr;
1600
+ value.textContent = side === "arabic" ? spec?.arabicLabel ?? node.expr : spec?.englishLabel ?? spec?.englishTex.replace(/^\\/, "") ?? node.expr;
1582
1601
  return value;
1583
1602
  }
1584
1603
  function buildSpacingAtomicCommandBody(spec, side, uiLocale) {
@@ -2482,10 +2501,13 @@ __export(editor_exports, {
2482
2501
  insertAtomicOperatorCommand: () => insertAtomicOperatorCommand,
2483
2502
  insertChar: () => insertChar,
2484
2503
  insertDelimiterPair: () => insertDelimiterPair,
2504
+ insertDivideOperator: () => insertDivideOperator,
2505
+ insertFirstDerivative: () => insertFirstDerivative,
2485
2506
  insertFraction: () => insertFraction,
2486
2507
  insertMatrixEnv: () => insertMatrixEnv,
2487
2508
  insertNumber: () => insertNumber,
2488
2509
  insertOperator: () => insertOperator,
2510
+ insertSecondDerivative: () => insertSecondDerivative,
2489
2511
  insertSqrt: () => insertSqrt,
2490
2512
  isSelectionActive: () => isSelectionActive,
2491
2513
  isSpacingCommandSpec: () => isSpacingCommandSpec,
@@ -2698,6 +2720,22 @@ function createEditorSession(englishTree, arabicTree, activeSide = "arabic") {
2698
2720
  };
2699
2721
  }
2700
2722
 
2723
+ // src/editor/divideOperator.ts
2724
+ var DIVIDE_OPERATOR_EN = "/";
2725
+ var DIVIDE_OPERATOR_AR = "\\";
2726
+ function isDivideOperatorExpr(expr) {
2727
+ return expr === DIVIDE_OPERATOR_EN || expr === DIVIDE_OPERATOR_AR;
2728
+ }
2729
+ function divideOperatorSurfaceExpr(expr, side) {
2730
+ if (!isDivideOperatorExpr(expr)) {
2731
+ return expr;
2732
+ }
2733
+ return side === "arabic" ? DIVIDE_OPERATOR_AR : DIVIDE_OPERATOR_EN;
2734
+ }
2735
+ function renderDivideOperatorLatex(side) {
2736
+ return side === "arabic" ? "\\backslash" : DIVIDE_OPERATOR_EN;
2737
+ }
2738
+
2701
2739
  // src/editor/digits.ts
2702
2740
  var WESTERN = "0123456789";
2703
2741
  var ARABIC_INDIC = "\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669";
@@ -2846,6 +2884,9 @@ function renderNodeEnglish(node, options) {
2846
2884
  if (node.kind === "char") {
2847
2885
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
2848
2886
  }
2887
+ if (node.kind === "operator" && isDivideOperatorExpr(node.expr)) {
2888
+ return `${renderDivideOperatorLatex("english")}${latinScripts(node, options)}`;
2889
+ }
2849
2890
  return `${formatDigits(node.expr, selectedDigitForm(options))}${latinScripts(node, options)}`;
2850
2891
  }
2851
2892
  function renderNodeArabic(node, options) {
@@ -2880,6 +2921,9 @@ function renderNodeArabic(node, options) {
2880
2921
  const content = renderCharContent(node, options);
2881
2922
  return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
2882
2923
  }
2924
+ if (node.kind === "operator" && isDivideOperatorExpr(node.expr)) {
2925
+ return arabicScripts(node, renderDivideOperatorLatex("arabic"), options);
2926
+ }
2883
2927
  return arabicScripts(node, formatDigits(node.expr, selectedDigitForm(options)), options);
2884
2928
  }
2885
2929
  function renderChainEnglish(chain, options) {
@@ -3406,6 +3450,128 @@ function undoRedoCanRedo(stacks) {
3406
3450
  return stacks.future.length > 0;
3407
3451
  }
3408
3452
 
3453
+ // src/editor/derivativeTemplates.ts
3454
+ var ARABIC_VAR = "\u0633";
3455
+ var ENGLISH_VAR = "x";
3456
+ function mirroredAd() {
3457
+ const english = createAtomicCommandNode("ad");
3458
+ const arabic = createAtomicCommandNode("ad");
3459
+ arabic.id = english.id;
3460
+ return { english, arabic };
3461
+ }
3462
+ function mirroredVar() {
3463
+ const english = createEditorNode("char", ENGLISH_VAR);
3464
+ const arabic = createEditorNode("char", ARABIC_VAR);
3465
+ arabic.id = english.id;
3466
+ return { english, arabic };
3467
+ }
3468
+ function withMirroredSuperscript2(english, arabic) {
3469
+ const englishSup = createEditorChain([createEditorNode("number", "2")]);
3470
+ const arabicSup = createEditorChain([createEditorNode("number", "2")]);
3471
+ arabicSup.id = englishSup.id;
3472
+ arabicSup.nodes[0].id = englishSup.nodes[0].id;
3473
+ english.superscript = englishSup;
3474
+ arabic.superscript = arabicSup;
3475
+ }
3476
+ function syncChainIds(english, arabic) {
3477
+ arabic.id = english.id;
3478
+ const limit = Math.min(english.nodes.length, arabic.nodes.length);
3479
+ for (let index = 0; index < limit; index += 1) {
3480
+ arabic.nodes[index].id = english.nodes[index].id;
3481
+ }
3482
+ }
3483
+ function syncFracIds(englishFrac, arabicFrac) {
3484
+ arabicFrac.id = englishFrac.id;
3485
+ if (englishFrac.numerator && arabicFrac.numerator) {
3486
+ syncChainIds(englishFrac.numerator, arabicFrac.numerator);
3487
+ }
3488
+ if (englishFrac.denominator && arabicFrac.denominator) {
3489
+ syncChainIds(englishFrac.denominator, arabicFrac.denominator);
3490
+ }
3491
+ }
3492
+ function buildAdVarChain() {
3493
+ const ad = mirroredAd();
3494
+ const variable = mirroredVar();
3495
+ const english = createEditorChain([ad.english, variable.english]);
3496
+ const arabic = createEditorChain([ad.arabic, variable.arabic]);
3497
+ syncChainIds(english, arabic);
3498
+ return { english, arabic };
3499
+ }
3500
+ function mirroredEmptyDelimiter() {
3501
+ const english = createDelimiterNode("", "");
3502
+ const arabic = createDelimiterNode("", "");
3503
+ arabic.id = english.id;
3504
+ if (english.innerExpr && arabic.innerExpr) {
3505
+ syncChainIds(english.innerExpr, arabic.innerExpr);
3506
+ }
3507
+ return { english, arabic };
3508
+ }
3509
+ function setMirroredDelimiterInner(english, arabic, pairs) {
3510
+ const englishInner = createEditorChain(pairs.map((pair) => pair.english));
3511
+ const arabicInner = createEditorChain(pairs.map((pair) => pair.arabic));
3512
+ syncChainIds(englishInner, arabicInner);
3513
+ english.innerExpr = englishInner;
3514
+ arabic.innerExpr = arabicInner;
3515
+ }
3516
+ function buildFirstDerivativeFrac() {
3517
+ const englishFrac = createFracNode();
3518
+ const arabicFrac = createFracNode();
3519
+ syncFracIds(englishFrac, arabicFrac);
3520
+ const numerator = buildAdVarChain();
3521
+ const denominator = buildAdVarChain();
3522
+ englishFrac.numerator = numerator.english;
3523
+ englishFrac.denominator = denominator.english;
3524
+ arabicFrac.numerator = numerator.arabic;
3525
+ arabicFrac.denominator = denominator.arabic;
3526
+ return { english: englishFrac, arabic: arabicFrac };
3527
+ }
3528
+ function buildSecondDerivativeFrac() {
3529
+ const englishFrac = createFracNode();
3530
+ const arabicFrac = createFracNode();
3531
+ syncFracIds(englishFrac, arabicFrac);
3532
+ const numAd = mirroredAd();
3533
+ const numVar = mirroredVar();
3534
+ withMirroredSuperscript2(numAd.english, numAd.arabic);
3535
+ const englishNumerator = createEditorChain([numAd.english, numVar.english]);
3536
+ const arabicNumerator = createEditorChain([numAd.arabic, numVar.arabic]);
3537
+ syncChainIds(englishNumerator, arabicNumerator);
3538
+ englishFrac.numerator = englishNumerator;
3539
+ arabicFrac.numerator = arabicNumerator;
3540
+ const denGroup = mirroredEmptyDelimiter();
3541
+ const denInnerAd = mirroredAd();
3542
+ const denInnerVar = mirroredVar();
3543
+ setMirroredDelimiterInner(denGroup.english, denGroup.arabic, [
3544
+ { english: denInnerAd.english, arabic: denInnerAd.arabic },
3545
+ { english: denInnerVar.english, arabic: denInnerVar.arabic }
3546
+ ]);
3547
+ withMirroredSuperscript2(denGroup.english, denGroup.arabic);
3548
+ englishFrac.denominator = createEditorChain([denGroup.english]);
3549
+ arabicFrac.denominator = createEditorChain([denGroup.arabic]);
3550
+ syncChainIds(englishFrac.denominator, arabicFrac.denominator);
3551
+ return { english: englishFrac, arabic: arabicFrac };
3552
+ }
3553
+ var DERIVATIVE_TEMPLATE_VAR_CARET_INDEX = 1;
3554
+ function derivativeTemplateInitialCaret(englishFrac) {
3555
+ const denominator = englishFrac.denominator;
3556
+ if (!denominator) {
3557
+ return {
3558
+ chainId: englishFrac.numerator?.id ?? "",
3559
+ index: DERIVATIVE_TEMPLATE_VAR_CARET_INDEX
3560
+ };
3561
+ }
3562
+ const loneDelimiter = denominator.nodes.length === 1 && denominator.nodes[0]?.kind === "delimiter" && denominator.nodes[0].innerExpr;
3563
+ if (loneDelimiter) {
3564
+ return {
3565
+ chainId: denominator.nodes[0].innerExpr.id,
3566
+ index: DERIVATIVE_TEMPLATE_VAR_CARET_INDEX
3567
+ };
3568
+ }
3569
+ return {
3570
+ chainId: denominator.id,
3571
+ index: DERIVATIVE_TEMPLATE_VAR_CARET_INDEX
3572
+ };
3573
+ }
3574
+
3409
3575
  // src/editor/state.ts
3410
3576
  function ensureSyncEntry(session, nodeId) {
3411
3577
  if (!session.sync[nodeId]) {
@@ -4042,10 +4208,27 @@ function insertNumber(session, expr) {
4042
4208
  inserted.forceSplitNextLeaf = false;
4043
4209
  return inserted;
4044
4210
  }
4211
+ function insertDivideOperator(session) {
4212
+ const base = replaceSelectionFirst(session);
4213
+ const next = cloneEditorSession(base);
4214
+ const englishNode = createEditorNode("operator", DIVIDE_OPERATOR_EN);
4215
+ const arabicNode = createEditorNode("operator", DIVIDE_OPERATOR_AR);
4216
+ arabicNode.id = englishNode.id;
4217
+ insertMirroredNode(next, next.caret.chainId, next.caret.index, englishNode, arabicNode);
4218
+ markExprSync(next, englishNode.id);
4219
+ next.caret.index += 1;
4220
+ next.selectedStructureNodeId = null;
4221
+ syncSelectionToCaret(next);
4222
+ appendDebug(next, "insertDivideOperator", "");
4223
+ return next;
4224
+ }
4045
4225
  function insertOperator(session, expr) {
4046
4226
  if (expr === "." && !session.splitLeafTyping && precedingIsExtendableNumber(session)) {
4047
4227
  return insertNumber(session, ".");
4048
4228
  }
4229
+ if (expr === DIVIDE_OPERATOR_EN || expr === DIVIDE_OPERATOR_AR) {
4230
+ return insertDivideOperator(session);
4231
+ }
4049
4232
  const base = replaceSelectionFirst(session);
4050
4233
  return insertLeaf(base, "operator", expr);
4051
4234
  }
@@ -4116,6 +4299,24 @@ function insertFraction(session) {
4116
4299
  appendDebug(next, "insertFraction", "");
4117
4300
  return next;
4118
4301
  }
4302
+ function insertDerivativeFrac(session, build, debugLabel) {
4303
+ const base = replaceSelectionFirst(session);
4304
+ const next = cloneEditorSession(base);
4305
+ const { english, arabic } = build();
4306
+ insertMirroredNode(next, next.caret.chainId, next.caret.index, english, arabic);
4307
+ syncSubtreeRecursive(next, english, arabic);
4308
+ next.caret = derivativeTemplateInitialCaret(english);
4309
+ next.selectedStructureNodeId = null;
4310
+ syncSelectionToCaret(next);
4311
+ appendDebug(next, debugLabel, "");
4312
+ return next;
4313
+ }
4314
+ function insertFirstDerivative(session) {
4315
+ return insertDerivativeFrac(session, buildFirstDerivativeFrac, "insertFirstDerivative");
4316
+ }
4317
+ function insertSecondDerivative(session) {
4318
+ return insertDerivativeFrac(session, buildSecondDerivativeFrac, "insertSecondDerivative");
4319
+ }
4119
4320
  function insertSqrt(session) {
4120
4321
  const base = replaceSelectionFirst(session);
4121
4322
  const next = cloneEditorSession(base);
@@ -4612,6 +4813,11 @@ function pasteText(session, text) {
4612
4813
  i += 1;
4613
4814
  continue;
4614
4815
  }
4816
+ if (ch === DIVIDE_OPERATOR_AR) {
4817
+ s = insertDivideOperator(s);
4818
+ i += 1;
4819
+ continue;
4820
+ }
4615
4821
  s = insertChar(s, ch);
4616
4822
  i += 1;
4617
4823
  }
@@ -4619,7 +4825,7 @@ function pasteText(session, text) {
4619
4825
  }
4620
4826
 
4621
4827
  // src/editor/display/base.ts
4622
- function buildBaseNodeBody(node, digitForm = "western") {
4828
+ function buildBaseNodeBody(node, digitForm = "western", side = "english") {
4623
4829
  const value = document.createElement("span");
4624
4830
  value.className = "node-value";
4625
4831
  if (node.kind === "char" && node.characterFont === "takween") {
@@ -4629,7 +4835,8 @@ function buildBaseNodeBody(node, digitForm = "western") {
4629
4835
  } else if (node.kind === "char" && node.characterFont === "diwaniOutline") {
4630
4836
  value.classList.add("node-value--diwani-outline");
4631
4837
  }
4632
- value.textContent = node.expr === "" ? "\u25A1" : formatDigits(node.expr, digitForm);
4838
+ const surfaceExpr = node.kind === "operator" ? divideOperatorSurfaceExpr(node.expr, side) : node.expr;
4839
+ value.textContent = surfaceExpr === "" ? "\u25A1" : formatDigits(surfaceExpr, digitForm);
4633
4840
  return value;
4634
4841
  }
4635
4842
 
@@ -4683,7 +4890,7 @@ function buildNodeBody(node, buildChain, side, digitForm = "western", uiLocale =
4683
4890
  if (node.kind === "atomicOperatorCommand") {
4684
4891
  return buildAtomicOperatorCommandNodeBody(node, side, uiLocale);
4685
4892
  }
4686
- return buildBaseNodeBody(node, digitForm);
4893
+ return buildBaseNodeBody(node, digitForm, side);
4687
4894
  }
4688
4895
 
4689
4896
  // src/editor/runtime.ts
@@ -5191,6 +5398,10 @@ function createEditorRuntime(options) {
5191
5398
  applyStructural((prev) => insertNumber(prev, char));
5192
5399
  return;
5193
5400
  }
5401
+ if (char === "\\") {
5402
+ applyStructural((prev) => insertDivideOperator(prev));
5403
+ return;
5404
+ }
5194
5405
  if (isOperator(char)) {
5195
5406
  applyStructural((prev) => insertOperator(prev, char));
5196
5407
  return;
@@ -5354,6 +5565,14 @@ function createEditorRuntime(options) {
5354
5565
  applyStructural((prev) => insertFraction(prev));
5355
5566
  surfaceEl.focus();
5356
5567
  },
5568
+ insertFirstDerivative: () => {
5569
+ applyStructural((prev) => insertFirstDerivative(prev));
5570
+ surfaceEl.focus();
5571
+ },
5572
+ insertSecondDerivative: () => {
5573
+ applyStructural((prev) => insertSecondDerivative(prev));
5574
+ surfaceEl.focus();
5575
+ },
5357
5576
  insertMatrixEnv: (style, rows, columns) => {
5358
5577
  applyStructural((prev) => insertMatrixEnv(prev, style, rows, columns));
5359
5578
  surfaceEl.focus();