@formulaxjs/kity-runtime 0.3.0 → 0.4.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.
@@ -1,4 +1,5 @@
1
1
  import {
2
+ DEFAULT_FORMULAX_LOCALE,
2
3
  createLegacyBaseComponent,
3
4
  createLegacyUiUtils,
4
5
  legacyBaseUtils,
@@ -11,8 +12,9 @@ import {
11
12
  legacyOtherPosition,
12
13
  legacySysconf,
13
14
  legacyUiDef,
15
+ normalizeFormulaXLocale,
14
16
  resolveToolbarAssetPath
15
- } from "./chunk-AOMNUFFB.js";
17
+ } from "./chunk-LIDQRZXL.js";
16
18
 
17
19
  // src/vendor/runtime-interop.ts
18
20
  function getLegacyRuntime() {
@@ -1813,6 +1815,40 @@ function resolveUnicode(key) {
1813
1815
  var UI_ELE_TYPE = legacyEleType;
1814
1816
  var BOX_TYPE2 = legacyBoxType;
1815
1817
  var OTHER_POSITION = legacyOtherPosition;
1818
+ var zhCnToEnUsText = /* @__PURE__ */ new Map([
1819
+ ["\u9884\u8BBE", "Presets"],
1820
+ ["\u9884\u8BBE\u516C\u5F0F", "Preset formulas"],
1821
+ ["\u4E8C\u6B21\u516C\u5F0F", "Quadratic formula"],
1822
+ ["\u4E8C\u9879\u5F0F\u5B9A\u7406", "Binomial theorem"],
1823
+ ["\u52FE\u80A1\u5B9A\u7406", "Pythagorean theorem"],
1824
+ ["\u57FA\u7840\u6570\u5B66", "Basic math"],
1825
+ ["\u5E0C\u814A\u5B57\u6BCD", "Greek letters"],
1826
+ ["\u6C42\u53CD\u5173\u7CFB\u8FD0\u7B97\u7B26", "Negated operators"],
1827
+ ["\u5B57\u6BCD\u7C7B\u7B26\u53F7", "Letter-like symbols"],
1828
+ ["\u7BAD\u5934", "Arrows"],
1829
+ ["\u624B\u5199\u4F53", "Script"],
1830
+ ["\u5206\u6570", "Fraction"],
1831
+ ["\u5E38\u7528\u5206\u6570", "Common fractions"],
1832
+ ["\u4E0A\u4E0B\u6807", "Scripts"],
1833
+ ["\u4E0A\u6807\u548C\u4E0B\u6807", "Superscripts and subscripts"],
1834
+ ["\u5E38\u7528\u7684\u4E0A\u6807\u548C\u4E0B\u6807", "Common superscripts and subscripts"],
1835
+ ["\u6839\u5F0F", "Radicals"],
1836
+ ["\u5E38\u7528\u6839\u5F0F", "Common radicals"],
1837
+ ["\u79EF\u5206", "Integrals"],
1838
+ ["\u5927\u578B\u8FD0\u7B97\u7B26", "Large operators"],
1839
+ ["\u6C42\u548C", "Summations"],
1840
+ ["\u62EC\u53F7", "Brackets"],
1841
+ ["\u65B9\u62EC\u53F7", "Brackets"],
1842
+ ["\u51FD\u6570", "Functions"],
1843
+ ["\u4E09\u89D2\u51FD\u6570", "Trigonometric functions"],
1844
+ ["\u5E38\u7528\u51FD\u6570", "Common functions"],
1845
+ ["\u5C0F\u5199", "Lowercase"],
1846
+ ["\u5927\u5199", "Uppercase"],
1847
+ ["\u53D8\u4F53", "Variants"],
1848
+ ["\u82B1\u4F53", "Fraktur"],
1849
+ ["\u53CC\u7EBF", "Double-struck"],
1850
+ ["\u7F57\u9A6C", "Roman"]
1851
+ ]);
1816
1852
  function each(list, callback) {
1817
1853
  if (Array.isArray(list)) {
1818
1854
  list.forEach((item, index) => callback(item, index));
@@ -1820,7 +1856,36 @@ function each(list, callback) {
1820
1856
  }
1821
1857
  Object.keys(list).forEach((key) => callback(list[key], key));
1822
1858
  }
1823
- var config = [{
1859
+ function translateToolbarText(value, locale) {
1860
+ if (locale === "zh_CN") {
1861
+ return value;
1862
+ }
1863
+ const normalizedValue = value.replace(/<br\s*\/?>/gi, "").trim();
1864
+ const translatedValue = zhCnToEnUsText.get(normalizedValue);
1865
+ if (!translatedValue) {
1866
+ return value;
1867
+ }
1868
+ const lineBreakMatch = value.match(/<br\s*\/?>/i);
1869
+ return lineBreakMatch ? `${translatedValue}${lineBreakMatch[0]}` : translatedValue;
1870
+ }
1871
+ function localizeToolbarConfig(value, locale) {
1872
+ if (Array.isArray(value)) {
1873
+ return value.map((item) => localizeToolbarConfig(item, locale));
1874
+ }
1875
+ if (!value || typeof value !== "object") {
1876
+ return value;
1877
+ }
1878
+ const result = {};
1879
+ for (const [key, currentValue] of Object.entries(value)) {
1880
+ if ((key === "label" || key === "title") && typeof currentValue === "string") {
1881
+ result[key] = translateToolbarText(currentValue, locale);
1882
+ continue;
1883
+ }
1884
+ result[key] = localizeToolbarConfig(currentValue, locale);
1885
+ }
1886
+ return result;
1887
+ }
1888
+ var baseToolbarConfig = [{
1824
1889
  type: UI_ELE_TYPE.DRAPDOWN_BOX,
1825
1890
  options: {
1826
1891
  button: {
@@ -2226,7 +2291,7 @@ var config = [{
2226
2291
  }];
2227
2292
  (function() {
2228
2293
  let tmp = [], otherImageSrc = resolveToolbarAssetPath("other.png"), currentConf = [];
2229
- each(config, function(conf) {
2294
+ each(baseToolbarConfig, function(conf) {
2230
2295
  if (conf.type === UI_ELE_TYPE.DELIMITER) {
2231
2296
  return;
2232
2297
  }
@@ -2298,7 +2363,7 @@ var config = [{
2298
2363
  "aleph",
2299
2364
  "beth",
2300
2365
  "blacksquare"
2301
- ], configList = config[2].options.box.group[0].items;
2366
+ ], configList = baseToolbarConfig[2].options.box.group[0].items;
2302
2367
  configList.push({
2303
2368
  title: "\u57FA\u7840\u6570\u5B66",
2304
2369
  content: getUnicodeContents(list)
@@ -2314,7 +2379,7 @@ var config = [{
2314
2379
  }, {
2315
2380
  title: "\u53D8\u4F53",
2316
2381
  values: ["digamma", "varepsilon", "varkappa", "varphi", "varpi", "varrho", "varsigma", "vartheta"]
2317
- }], greekConfigList = config[2].options.box.group[1].items;
2382
+ }], greekConfigList = baseToolbarConfig[2].options.box.group[1].items;
2318
2383
  greekConfigList.push({
2319
2384
  title: greekList[0].title,
2320
2385
  content: getUnicodeContents(greekList[0].values)
@@ -2363,7 +2428,7 @@ var config = [{
2363
2428
  "nVDash",
2364
2429
  "nexists"
2365
2430
  ]
2366
- }], greekConfigList = config[2].options.box.group[2].items;
2431
+ }], greekConfigList = baseToolbarConfig[2].options.box.group[2].items;
2367
2432
  greekConfigList.push({
2368
2433
  title: greekList[0].title,
2369
2434
  content: getUnicodeContents(greekList[0].values)
@@ -2389,7 +2454,7 @@ var config = [{
2389
2454
  "Game",
2390
2455
  "Im",
2391
2456
  "Re"
2392
- ], configList = config[2].options.box.group[3].items;
2457
+ ], configList = baseToolbarConfig[2].options.box.group[3].items;
2393
2458
  configList.push({
2394
2459
  title: "\u5B57\u6BCD\u7C7B\u7B26\u53F7",
2395
2460
  content: getUnicodeContents(list)
@@ -2457,7 +2522,7 @@ var config = [{
2457
2522
  "twoheadleftarrow",
2458
2523
  "twoheadrightarrow",
2459
2524
  "rightsquigarrow"
2460
- ], configList = config[2].options.box.group[4].items;
2525
+ ], configList = baseToolbarConfig[2].options.box.group[4].items;
2461
2526
  configList.push({
2462
2527
  title: "\u7BAD\u5934",
2463
2528
  content: getUnicodeContents(list)
@@ -2636,7 +2701,7 @@ var config = [{
2636
2701
  "y",
2637
2702
  "z"
2638
2703
  ]
2639
- }], configList = config[2].options.box.group[5].items;
2704
+ }], configList = baseToolbarConfig[2].options.box.group[5].items;
2640
2705
  each(list[0].values, function(item, index) {
2641
2706
  list[0].values[index] = "mathcal{" + item + "}";
2642
2707
  });
@@ -2685,7 +2750,13 @@ function getUnicodeContents(keySet) {
2685
2750
  });
2686
2751
  return result;
2687
2752
  }
2688
- var toolbar_config_default = config;
2753
+ function createToolbarConfig(locale = DEFAULT_FORMULAX_LOCALE) {
2754
+ return localizeToolbarConfig(
2755
+ baseToolbarConfig,
2756
+ normalizeFormulaXLocale(locale)
2757
+ );
2758
+ }
2759
+ var toolbar_config_default = createToolbarConfig;
2689
2760
 
2690
2761
  // src/legacy/ui.ts
2691
2762
  var $$8 = createLegacyUiUtils();
@@ -2718,7 +2789,8 @@ var UIComponent = kity10.createClass("UIComponent", {
2718
2789
  this.initScrollEvent();
2719
2790
  },
2720
2791
  initComponents() {
2721
- this.components.toolbar = new toolbar_default(this, this.kfEditor, toolbar_config_default);
2792
+ const toolbarConfig = toolbar_config_default(normalizeFormulaXLocale(this.options.locale));
2793
+ this.components.toolbar = new toolbar_default(this, this.kfEditor, toolbarConfig);
2722
2794
  this.components.scrollbar = new scrollbar_default(this, this.kfEditor);
2723
2795
  },
2724
2796
  updateContainerSize(container, toolbar, editArea) {
@@ -3938,6 +4010,21 @@ var syntax_move_default = MoveComponent;
3938
4010
  // src/legacy/syntax.ts
3939
4011
  var CURSOR_CHAR2 = legacySysconf.cursorCharacter;
3940
4012
  var kity18 = getLegacyKity();
4013
+ function clampOffset(offset, maxOffset) {
4014
+ return Math.max(0, Math.min(offset, maxOffset));
4015
+ }
4016
+ function normalizeCursorRecord(objTree, cursor) {
4017
+ const fallbackGroupId = objTree.mapping.root.strGroup.attr.id;
4018
+ const targetGroupId = cursor.groupId && objTree.mapping[cursor.groupId] ? cursor.groupId : fallbackGroupId;
4019
+ const operandCount = objTree.mapping[targetGroupId].strGroup.operand.length;
4020
+ const startOffset = clampOffset(cursor.startOffset, operandCount);
4021
+ const endOffset = clampOffset(cursor.endOffset, operandCount);
4022
+ return {
4023
+ groupId: targetGroupId,
4024
+ startOffset: Math.min(startOffset, endOffset),
4025
+ endOffset: Math.max(startOffset, endOffset)
4026
+ };
4027
+ }
3941
4028
  var SyntaxComponent = kity18.createClass("SyntaxComponenet", {
3942
4029
  constructor(kfEditor) {
3943
4030
  this.kfEditor = kfEditor;
@@ -3987,10 +4074,14 @@ var SyntaxComponent = kity18.createClass("SyntaxComponenet", {
3987
4074
  },
3988
4075
  updateObjTree(objTree) {
3989
4076
  const selectInfo = objTree.select;
4077
+ this.objTree = objTree;
3990
4078
  if (selectInfo?.groupId) {
3991
4079
  this.updateCursor(selectInfo.groupId, selectInfo.startOffset, selectInfo.endOffset);
4080
+ return;
4081
+ }
4082
+ if (this.record.cursor.groupId !== null) {
4083
+ this.record.cursor = normalizeCursorRecord(objTree, this.record.cursor);
3992
4084
  }
3993
- this.objTree = objTree;
3994
4085
  },
3995
4086
  hasCursorInfo() {
3996
4087
  return this.record.cursor.groupId !== null;
@@ -4036,7 +4127,8 @@ var SyntaxComponent = kity18.createClass("SyntaxComponenet", {
4036
4127
  return this.objTree;
4037
4128
  },
4038
4129
  getGroupObject(id) {
4039
- return this.objTree.mapping[id].objGroup || null;
4130
+ const groupInfo = this.objTree.mapping[id];
4131
+ return groupInfo ? groupInfo.objGroup : null;
4040
4132
  },
4041
4133
  getCursorRecord() {
4042
4134
  return kity18.Utils.extend({}, this.record.cursor);
@@ -4112,7 +4204,7 @@ var SyntaxComponent = kity18.createClass("SyntaxComponenet", {
4112
4204
  return this.hasRootplaceholder();
4113
4205
  },
4114
4206
  serialization() {
4115
- const cursor = this.record.cursor;
4207
+ const cursor = normalizeCursorRecord(this.objTree, this.record.cursor);
4116
4208
  const objGroup = this.objTree.mapping[cursor.groupId];
4117
4209
  const curStrGroup = objGroup.strGroup;
4118
4210
  let strStartIndex = Math.min(cursor.endOffset, cursor.startOffset);
@@ -4148,11 +4240,12 @@ var SyntaxComponent = kity18.createClass("SyntaxComponenet", {
4148
4240
  endOffset = startOffset;
4149
4241
  startOffset = tmp;
4150
4242
  }
4151
- this.record.cursor = {
4243
+ const nextCursor = {
4152
4244
  groupId,
4153
4245
  startOffset,
4154
4246
  endOffset
4155
4247
  };
4248
+ this.record.cursor = this.objTree ? normalizeCursorRecord(this.objTree, nextCursor) : nextCursor;
4156
4249
  },
4157
4250
  leftMove() {
4158
4251
  this.components.move.leftMove();
@@ -4197,7 +4290,15 @@ var KEY_CODE = {
4197
4290
  DELETE: 8,
4198
4291
  INPUT: 229
4199
4292
  };
4293
+ var CURSOR_CHAR3 = legacySysconf.cursorCharacter;
4200
4294
  var kity19 = getLegacyKity();
4295
+ function insertCursorMarkers(value, selectionStart, selectionEnd) {
4296
+ const normalizedStart = Math.max(0, Math.min(selectionStart ?? value.length, value.length));
4297
+ const normalizedEnd = Math.max(0, Math.min(selectionEnd ?? normalizedStart, value.length));
4298
+ const rangeStart = Math.min(normalizedStart, normalizedEnd);
4299
+ const rangeEnd = Math.max(normalizedStart, normalizedEnd);
4300
+ return value.slice(0, rangeStart) + CURSOR_CHAR3 + value.slice(rangeStart, rangeEnd) + CURSOR_CHAR3 + value.slice(rangeEnd);
4301
+ }
4201
4302
  var InputComponent = kity19.createClass("InputComponent", {
4202
4303
  constructor(parentComponent, kfEditor) {
4203
4304
  this.parentComponent = parentComponent;
@@ -4369,7 +4470,12 @@ var InputComponent = kity19.createClass("InputComponent", {
4369
4470
  return `${e.shiftKey ? "s+" : ""}${e.keyCode}`;
4370
4471
  },
4371
4472
  processingInput() {
4372
- this.restruct(this.inputBox.value);
4473
+ const latexWithCursor = insertCursorMarkers(
4474
+ this.inputBox.value,
4475
+ this.inputBox.selectionStart,
4476
+ this.inputBox.selectionEnd
4477
+ );
4478
+ this.restruct(latexWithCursor);
4373
4479
  this.kfEditor.requestService("ui.update.canvas.view");
4374
4480
  },
4375
4481
  restruct(latexStr) {
@@ -4856,4 +4962,4 @@ export {
4856
4962
  start_default as default,
4857
4963
  installKityEditorStart
4858
4964
  };
4859
- //# sourceMappingURL=start-LTYA5XON.js.map
4965
+ //# sourceMappingURL=start-H6H24L6P.js.map