@formulaxjs/kity-runtime 0.2.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.
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import {
2
+ DEFAULT_FORMULAX_LOCALE,
3
+ FORMULAX_LOCALES,
2
4
  addEvent,
3
5
  createElement,
4
6
  createLegacyBaseComponent,
@@ -6,6 +8,10 @@ import {
6
8
  delegateEvent,
7
9
  getClassList,
8
10
  getRectBox,
11
+ kityAssetManifest,
12
+ kityFontAssets,
13
+ kityStyleAssets,
14
+ kityToolbarAssets,
9
15
  legacyBaseUtils,
10
16
  legacyBoxType,
11
17
  legacyEleType,
@@ -18,57 +24,14 @@ import {
18
24
  legacyOtherPosition,
19
25
  legacySysconf,
20
26
  legacyUiDef,
27
+ legacyUiUtils,
28
+ normalizeFormulaXLocale,
21
29
  normalizeMouseEvent,
22
30
  publish,
23
31
  setToolbarAssetUrls,
24
32
  subscribe,
25
33
  utils_default
26
- } from "./chunk-EKIJQ64F.js";
27
-
28
- // public/assets/images/toolbar/btn.png
29
- var btn_default = "./btn-5DANP6JY.png";
30
-
31
- // public/assets/images/toolbar/other.png
32
- var other_default = "./other-OMWJFGL5.png";
33
-
34
- // public/assets/styles/editor.css?url
35
- var editor_default = "./editor-JT5KLVXX.css?url";
36
-
37
- // public/resource/KF_AMS_BB.woff
38
- var KF_AMS_BB_default = "./KF_AMS_BB-5QF7FUSO.woff";
39
-
40
- // public/resource/KF_AMS_CAL.woff
41
- var KF_AMS_CAL_default = "./KF_AMS_CAL-NXRNLAZN.woff";
42
-
43
- // public/resource/KF_AMS_FRAK.woff
44
- var KF_AMS_FRAK_default = "./KF_AMS_FRAK-CO33WWN4.woff";
45
-
46
- // public/resource/KF_AMS_MAIN.woff
47
- var KF_AMS_MAIN_default = "./KF_AMS_MAIN-25QJVAWY.woff";
48
-
49
- // public/resource/KF_AMS_ROMAN.woff
50
- var KF_AMS_ROMAN_default = "./KF_AMS_ROMAN-243BR7HH.woff";
51
-
52
- // src/asset-manifest.ts
53
- var kityFontAssets = {
54
- KF_AMS_BB: KF_AMS_BB_default,
55
- KF_AMS_CAL: KF_AMS_CAL_default,
56
- KF_AMS_FRAK: KF_AMS_FRAK_default,
57
- KF_AMS_MAIN: KF_AMS_MAIN_default,
58
- KF_AMS_ROMAN: KF_AMS_ROMAN_default
59
- };
60
- var kityToolbarAssets = {
61
- btn: btn_default,
62
- other: other_default
63
- };
64
- var kityStyleAssets = {
65
- editor: editor_default
66
- };
67
- var kityAssetManifest = {
68
- fonts: kityFontAssets,
69
- toolbar: kityToolbarAssets,
70
- styles: kityStyleAssets
71
- };
34
+ } from "./chunk-LIDQRZXL.js";
72
35
 
73
36
  // src/vendor/char-position.ts
74
37
  var legacyCharPosition = {
@@ -3447,6 +3410,80 @@ function installKityRuntime(targetWindow = window) {
3447
3410
  targetWindow.kity = kity;
3448
3411
  }
3449
3412
 
3413
+ // src/perf.ts
3414
+ function getPerfHost() {
3415
+ return globalThis;
3416
+ }
3417
+ function getPerfState() {
3418
+ const host = getPerfHost();
3419
+ host.__FORMULAX_PERF_STATE__ ??= {
3420
+ reportedMeasureCount: 0,
3421
+ reportScheduled: false
3422
+ };
3423
+ return host.__FORMULAX_PERF_STATE__;
3424
+ }
3425
+ function hasPerfSupport() {
3426
+ return typeof performance !== "undefined" && typeof performance.mark === "function" && typeof performance.measure === "function" && typeof performance.getEntriesByType === "function";
3427
+ }
3428
+ function isPerfDebugEnabled() {
3429
+ return getPerfHost().__FORMULAX_PERF__ === true;
3430
+ }
3431
+ function schedulePerfReport() {
3432
+ if (!hasPerfSupport() || !isPerfDebugEnabled()) {
3433
+ return;
3434
+ }
3435
+ const state = getPerfState();
3436
+ if (state.reportScheduled) {
3437
+ return;
3438
+ }
3439
+ state.reportScheduled = true;
3440
+ queueMicrotask(() => {
3441
+ state.reportScheduled = false;
3442
+ const entries = performance.getEntriesByType("measure").filter((entry) => entry.name.startsWith("fx:")).sort((left, right) => left.startTime - right.startTime);
3443
+ const nextEntries = entries.slice(state.reportedMeasureCount);
3444
+ state.reportedMeasureCount = entries.length;
3445
+ if (!nextEntries.length) {
3446
+ return;
3447
+ }
3448
+ console.table(nextEntries.map((entry) => ({
3449
+ name: entry.name,
3450
+ duration: Number(entry.duration.toFixed(2)),
3451
+ startTime: Number(entry.startTime.toFixed(2))
3452
+ })));
3453
+ });
3454
+ }
3455
+ function markFormulaXPerf(name) {
3456
+ if (!hasPerfSupport()) {
3457
+ return null;
3458
+ }
3459
+ const markName = `${name}::${Date.now()}::${Math.random().toString(36).slice(2, 8)}`;
3460
+ performance.mark(markName);
3461
+ return markName;
3462
+ }
3463
+ function measureFormulaXPerf(name, startMark, endMark) {
3464
+ if (!hasPerfSupport() || !startMark) {
3465
+ return null;
3466
+ }
3467
+ const resolvedEndMark = endMark ?? markFormulaXPerf(`${name}:end`);
3468
+ if (!resolvedEndMark) {
3469
+ return null;
3470
+ }
3471
+ performance.measure(name, startMark, resolvedEndMark);
3472
+ schedulePerfReport();
3473
+ return resolvedEndMark;
3474
+ }
3475
+ function clearFormulaXPerfMarks(...marks) {
3476
+ if (!hasPerfSupport()) {
3477
+ return;
3478
+ }
3479
+ for (const mark of marks) {
3480
+ if (!mark) {
3481
+ continue;
3482
+ }
3483
+ performance.clearMarks(mark);
3484
+ }
3485
+ }
3486
+
3450
3487
  // src/create-editor.ts
3451
3488
  var DEFAULT_LATEX = "x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}";
3452
3489
  var DEFAULT_EDITOR_HEIGHT = "auto";
@@ -3486,13 +3523,14 @@ function resolveEditorAssets(assets) {
3486
3523
  }
3487
3524
  function ensureKityStylesheet(doc, href) {
3488
3525
  if (doc.getElementById(KITY_STYLE_ID)) {
3489
- return;
3526
+ return false;
3490
3527
  }
3491
3528
  const link = doc.createElement("link");
3492
3529
  link.id = KITY_STYLE_ID;
3493
3530
  link.rel = "stylesheet";
3494
3531
  link.href = href;
3495
3532
  doc.head.appendChild(link);
3533
+ return true;
3496
3534
  }
3497
3535
  function hydrateLegacyKf(runtimeWindow) {
3498
3536
  const requireFormula = runtimeWindow.__kityFormulaRequire__;
@@ -3540,7 +3578,7 @@ function installLegacyRuntime(runtimeWindow) {
3540
3578
  kity: runtimeWindow.kity,
3541
3579
  otherPosition: legacyOtherPosition,
3542
3580
  uiDef: legacyUiDef,
3543
- uiUtils: createLegacyUiUtils()
3581
+ uiUtils: legacyUiUtils
3544
3582
  };
3545
3583
  }
3546
3584
  async function ensureKityRuntime() {
@@ -3548,64 +3586,118 @@ async function ensureKityRuntime() {
3548
3586
  return runtimePromise;
3549
3587
  }
3550
3588
  runtimePromise = (async () => {
3589
+ const runtimeTotalStart = markFormulaXPerf("fx:kity-runtime:total");
3551
3590
  const runtimeWindow = window;
3552
3591
  runtimeWindow.kf = runtimeWindow.kf ?? {};
3553
3592
  installKityRuntime(runtimeWindow);
3554
- const { installLegacyKityFormulaRuntime } = await import("./install-ARHGHFNJ.js");
3555
- installLegacyKityFormulaRuntime(runtimeWindow);
3556
- hydrateLegacyKf(runtimeWindow);
3557
- const { installLegacyParserRuntime } = await import("./install-HKCG5NWK.js");
3558
- installLegacyParserRuntime(runtimeWindow);
3559
- installLegacyRuntime(runtimeWindow);
3560
- const { installKityEditorStart } = await import("./start-GQH6XUBI.js");
3561
- installKityEditorStart(runtimeWindow);
3593
+ try {
3594
+ const formulaImportStart = markFormulaXPerf("fx:kity-runtime:formula-import");
3595
+ const { installLegacyKityFormulaRuntime } = await import("./install-WDCVVIMM.js");
3596
+ const formulaImportEnd = markFormulaXPerf("fx:kity-runtime:formula-import:end");
3597
+ measureFormulaXPerf("fx:kity-runtime:formula-import", formulaImportStart, formulaImportEnd);
3598
+ clearFormulaXPerfMarks(formulaImportStart, formulaImportEnd);
3599
+ const formulaInstallStart = markFormulaXPerf("fx:kity-runtime:formula-install");
3600
+ installLegacyKityFormulaRuntime(runtimeWindow);
3601
+ hydrateLegacyKf(runtimeWindow);
3602
+ const formulaInstallEnd = markFormulaXPerf("fx:kity-runtime:formula-install:end");
3603
+ measureFormulaXPerf("fx:kity-runtime:formula-install", formulaInstallStart, formulaInstallEnd);
3604
+ clearFormulaXPerfMarks(formulaInstallStart, formulaInstallEnd);
3605
+ const parserImportStart = markFormulaXPerf("fx:kity-runtime:parser-import");
3606
+ const { installLegacyParserRuntime } = await import("./install-HKCG5NWK.js");
3607
+ const parserImportEnd = markFormulaXPerf("fx:kity-runtime:parser-import:end");
3608
+ measureFormulaXPerf("fx:kity-runtime:parser-import", parserImportStart, parserImportEnd);
3609
+ clearFormulaXPerfMarks(parserImportStart, parserImportEnd);
3610
+ const parserInstallStart = markFormulaXPerf("fx:kity-runtime:parser-install");
3611
+ installLegacyParserRuntime(runtimeWindow);
3612
+ const parserInstallEnd = markFormulaXPerf("fx:kity-runtime:parser-install:end");
3613
+ measureFormulaXPerf("fx:kity-runtime:parser-install", parserInstallStart, parserInstallEnd);
3614
+ clearFormulaXPerfMarks(parserInstallStart, parserInstallEnd);
3615
+ installLegacyRuntime(runtimeWindow);
3616
+ const bootImportStart = markFormulaXPerf("fx:kity-runtime:boot-import");
3617
+ const { installKityEditorStart } = await import("./start-H6H24L6P.js");
3618
+ const bootImportEnd = markFormulaXPerf("fx:kity-runtime:boot-import:end");
3619
+ measureFormulaXPerf("fx:kity-runtime:boot-import", bootImportStart, bootImportEnd);
3620
+ clearFormulaXPerfMarks(bootImportStart, bootImportEnd);
3621
+ const bootInstallStart = markFormulaXPerf("fx:kity-runtime:boot-install");
3622
+ installKityEditorStart(runtimeWindow);
3623
+ const bootInstallEnd = markFormulaXPerf("fx:kity-runtime:boot-install:end");
3624
+ measureFormulaXPerf("fx:kity-runtime:boot-install", bootInstallStart, bootInstallEnd);
3625
+ clearFormulaXPerfMarks(bootInstallStart, bootInstallEnd);
3626
+ } finally {
3627
+ const runtimeTotalEnd = markFormulaXPerf("fx:kity-runtime:total:end");
3628
+ measureFormulaXPerf("fx:kity-runtime:total", runtimeTotalStart, runtimeTotalEnd);
3629
+ clearFormulaXPerfMarks(runtimeTotalStart, runtimeTotalEnd);
3630
+ }
3562
3631
  })();
3563
3632
  return runtimePromise;
3564
3633
  }
3565
3634
  async function createKityEditor(container, options = {}) {
3635
+ const createEditorStart = markFormulaXPerf("fx:create-kity-editor:total");
3566
3636
  const fontsize = options.render?.fontsize ?? 40;
3567
3637
  const editorHeight = normalizeCssSize(options.height, DEFAULT_EDITOR_HEIGHT);
3638
+ const locale = normalizeFormulaXLocale(options.locale ?? DEFAULT_FORMULAX_LOCALE);
3568
3639
  const assets = resolveEditorAssets(options.assets);
3569
- ensureKityStylesheet(document, assets.styles.editor);
3570
- setToolbarAssetUrls(assets.toolbar);
3571
- await ensureKityRuntime();
3572
- const runtimeWindow = window;
3573
- if (!runtimeWindow.kf?.EditorFactory) {
3574
- throw new Error("Kity editor runtime did not initialize");
3575
- }
3576
- container.innerHTML = "";
3577
- const host = document.createElement("div");
3578
- host.className = "kf-editor";
3579
- host.style.width = "100%";
3580
- host.style.height = editorHeight;
3581
- container.appendChild(host);
3582
- const factory = runtimeWindow.kf.EditorFactory.create(host, {
3583
- render: {
3584
- fontsize
3585
- },
3586
- resource: {
3587
- path: "",
3588
- fonts: assets.fonts
3589
- }
3590
- });
3591
- return {
3592
- ready: factory.ready.bind(factory),
3593
- execCommand(name, value) {
3594
- factory.ready(function execWhenReady() {
3595
- this.execCommand(name, value);
3596
- });
3597
- },
3598
- focus() {
3599
- factory.ready(function focusWhenReady() {
3600
- this.execCommand("focus");
3601
- });
3602
- },
3603
- destroy() {
3604
- container.innerHTML = "";
3605
- },
3606
- host,
3607
- raw: factory
3608
- };
3640
+ try {
3641
+ const stylesheetInserted = ensureKityStylesheet(document, assets.styles.editor);
3642
+ if (stylesheetInserted) {
3643
+ const stylesheetInsertedMark = markFormulaXPerf("fx:kity-css:link-inserted");
3644
+ measureFormulaXPerf("fx:kity-css:link-inserted", createEditorStart, stylesheetInsertedMark);
3645
+ clearFormulaXPerfMarks(stylesheetInsertedMark);
3646
+ }
3647
+ setToolbarAssetUrls(assets.toolbar);
3648
+ await ensureKityRuntime();
3649
+ const runtimeReadyMark = markFormulaXPerf("fx:kity-runtime:ready-for-editor");
3650
+ measureFormulaXPerf("fx:kity-runtime:ready-for-editor", createEditorStart, runtimeReadyMark);
3651
+ clearFormulaXPerfMarks(runtimeReadyMark);
3652
+ const runtimeWindow = window;
3653
+ if (!runtimeWindow.kf?.EditorFactory) {
3654
+ throw new Error("Kity editor runtime did not initialize");
3655
+ }
3656
+ container.innerHTML = "";
3657
+ const host = document.createElement("div");
3658
+ host.className = "kf-editor";
3659
+ host.style.width = "100%";
3660
+ host.style.height = editorHeight;
3661
+ container.appendChild(host);
3662
+ const factoryCreateStart = markFormulaXPerf("fx:kity-editor-factory:create");
3663
+ const factory = runtimeWindow.kf.EditorFactory.create(host, {
3664
+ render: {
3665
+ fontsize
3666
+ },
3667
+ ui: {
3668
+ locale
3669
+ },
3670
+ resource: {
3671
+ path: "",
3672
+ fonts: assets.fonts
3673
+ }
3674
+ });
3675
+ const factoryCreateEnd = markFormulaXPerf("fx:kity-editor-factory:create:end");
3676
+ measureFormulaXPerf("fx:kity-editor-factory:create", factoryCreateStart, factoryCreateEnd);
3677
+ clearFormulaXPerfMarks(factoryCreateStart, factoryCreateEnd);
3678
+ return {
3679
+ ready: factory.ready.bind(factory),
3680
+ execCommand(name, value) {
3681
+ factory.ready(function execWhenReady() {
3682
+ this.execCommand(name, value);
3683
+ });
3684
+ },
3685
+ focus() {
3686
+ factory.ready(function focusWhenReady() {
3687
+ this.execCommand("focus");
3688
+ });
3689
+ },
3690
+ destroy() {
3691
+ container.innerHTML = "";
3692
+ },
3693
+ host,
3694
+ raw: factory
3695
+ };
3696
+ } finally {
3697
+ const createEditorEnd = markFormulaXPerf("fx:create-kity-editor:total:end");
3698
+ measureFormulaXPerf("fx:create-kity-editor:total", createEditorStart, createEditorEnd);
3699
+ clearFormulaXPerfMarks(createEditorStart, createEditorEnd);
3700
+ }
3609
3701
  }
3610
3702
  async function mountKityEditor(container, options = {}) {
3611
3703
  const editor = await createKityEditor(container, options);
@@ -3663,6 +3755,8 @@ function installLegacyKityData(target = window) {
3663
3755
  };
3664
3756
  }
3665
3757
  export {
3758
+ DEFAULT_FORMULAX_LOCALE,
3759
+ FORMULAX_LOCALES,
3666
3760
  FormulaXEditor,
3667
3761
  addEvent,
3668
3762
  createElement,
@@ -3690,7 +3784,9 @@ export {
3690
3784
  legacyKfExtDef,
3691
3785
  legacySysconf,
3692
3786
  legacyUiDef,
3787
+ legacyUiUtils,
3693
3788
  mountKityEditor,
3789
+ normalizeFormulaXLocale,
3694
3790
  normalizeMouseEvent,
3695
3791
  publish,
3696
3792
  subscribe