@formulaxjs/kity-runtime 0.2.0 → 0.3.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
@@ -6,6 +6,10 @@ import {
6
6
  delegateEvent,
7
7
  getClassList,
8
8
  getRectBox,
9
+ kityAssetManifest,
10
+ kityFontAssets,
11
+ kityStyleAssets,
12
+ kityToolbarAssets,
9
13
  legacyBaseUtils,
10
14
  legacyBoxType,
11
15
  legacyEleType,
@@ -18,57 +22,13 @@ import {
18
22
  legacyOtherPosition,
19
23
  legacySysconf,
20
24
  legacyUiDef,
25
+ legacyUiUtils,
21
26
  normalizeMouseEvent,
22
27
  publish,
23
28
  setToolbarAssetUrls,
24
29
  subscribe,
25
30
  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
- };
31
+ } from "./chunk-AOMNUFFB.js";
72
32
 
73
33
  // src/vendor/char-position.ts
74
34
  var legacyCharPosition = {
@@ -3447,6 +3407,80 @@ function installKityRuntime(targetWindow = window) {
3447
3407
  targetWindow.kity = kity;
3448
3408
  }
3449
3409
 
3410
+ // src/perf.ts
3411
+ function getPerfHost() {
3412
+ return globalThis;
3413
+ }
3414
+ function getPerfState() {
3415
+ const host = getPerfHost();
3416
+ host.__FORMULAX_PERF_STATE__ ??= {
3417
+ reportedMeasureCount: 0,
3418
+ reportScheduled: false
3419
+ };
3420
+ return host.__FORMULAX_PERF_STATE__;
3421
+ }
3422
+ function hasPerfSupport() {
3423
+ return typeof performance !== "undefined" && typeof performance.mark === "function" && typeof performance.measure === "function" && typeof performance.getEntriesByType === "function";
3424
+ }
3425
+ function isPerfDebugEnabled() {
3426
+ return getPerfHost().__FORMULAX_PERF__ === true;
3427
+ }
3428
+ function schedulePerfReport() {
3429
+ if (!hasPerfSupport() || !isPerfDebugEnabled()) {
3430
+ return;
3431
+ }
3432
+ const state = getPerfState();
3433
+ if (state.reportScheduled) {
3434
+ return;
3435
+ }
3436
+ state.reportScheduled = true;
3437
+ queueMicrotask(() => {
3438
+ state.reportScheduled = false;
3439
+ const entries = performance.getEntriesByType("measure").filter((entry) => entry.name.startsWith("fx:")).sort((left, right) => left.startTime - right.startTime);
3440
+ const nextEntries = entries.slice(state.reportedMeasureCount);
3441
+ state.reportedMeasureCount = entries.length;
3442
+ if (!nextEntries.length) {
3443
+ return;
3444
+ }
3445
+ console.table(nextEntries.map((entry) => ({
3446
+ name: entry.name,
3447
+ duration: Number(entry.duration.toFixed(2)),
3448
+ startTime: Number(entry.startTime.toFixed(2))
3449
+ })));
3450
+ });
3451
+ }
3452
+ function markFormulaXPerf(name) {
3453
+ if (!hasPerfSupport()) {
3454
+ return null;
3455
+ }
3456
+ const markName = `${name}::${Date.now()}::${Math.random().toString(36).slice(2, 8)}`;
3457
+ performance.mark(markName);
3458
+ return markName;
3459
+ }
3460
+ function measureFormulaXPerf(name, startMark, endMark) {
3461
+ if (!hasPerfSupport() || !startMark) {
3462
+ return null;
3463
+ }
3464
+ const resolvedEndMark = endMark ?? markFormulaXPerf(`${name}:end`);
3465
+ if (!resolvedEndMark) {
3466
+ return null;
3467
+ }
3468
+ performance.measure(name, startMark, resolvedEndMark);
3469
+ schedulePerfReport();
3470
+ return resolvedEndMark;
3471
+ }
3472
+ function clearFormulaXPerfMarks(...marks) {
3473
+ if (!hasPerfSupport()) {
3474
+ return;
3475
+ }
3476
+ for (const mark of marks) {
3477
+ if (!mark) {
3478
+ continue;
3479
+ }
3480
+ performance.clearMarks(mark);
3481
+ }
3482
+ }
3483
+
3450
3484
  // src/create-editor.ts
3451
3485
  var DEFAULT_LATEX = "x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}";
3452
3486
  var DEFAULT_EDITOR_HEIGHT = "auto";
@@ -3486,13 +3520,14 @@ function resolveEditorAssets(assets) {
3486
3520
  }
3487
3521
  function ensureKityStylesheet(doc, href) {
3488
3522
  if (doc.getElementById(KITY_STYLE_ID)) {
3489
- return;
3523
+ return false;
3490
3524
  }
3491
3525
  const link = doc.createElement("link");
3492
3526
  link.id = KITY_STYLE_ID;
3493
3527
  link.rel = "stylesheet";
3494
3528
  link.href = href;
3495
3529
  doc.head.appendChild(link);
3530
+ return true;
3496
3531
  }
3497
3532
  function hydrateLegacyKf(runtimeWindow) {
3498
3533
  const requireFormula = runtimeWindow.__kityFormulaRequire__;
@@ -3540,7 +3575,7 @@ function installLegacyRuntime(runtimeWindow) {
3540
3575
  kity: runtimeWindow.kity,
3541
3576
  otherPosition: legacyOtherPosition,
3542
3577
  uiDef: legacyUiDef,
3543
- uiUtils: createLegacyUiUtils()
3578
+ uiUtils: legacyUiUtils
3544
3579
  };
3545
3580
  }
3546
3581
  async function ensureKityRuntime() {
@@ -3548,64 +3583,114 @@ async function ensureKityRuntime() {
3548
3583
  return runtimePromise;
3549
3584
  }
3550
3585
  runtimePromise = (async () => {
3586
+ const runtimeTotalStart = markFormulaXPerf("fx:kity-runtime:total");
3551
3587
  const runtimeWindow = window;
3552
3588
  runtimeWindow.kf = runtimeWindow.kf ?? {};
3553
3589
  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);
3590
+ try {
3591
+ const formulaImportStart = markFormulaXPerf("fx:kity-runtime:formula-import");
3592
+ const { installLegacyKityFormulaRuntime } = await import("./install-TIZBWEFU.js");
3593
+ const formulaImportEnd = markFormulaXPerf("fx:kity-runtime:formula-import:end");
3594
+ measureFormulaXPerf("fx:kity-runtime:formula-import", formulaImportStart, formulaImportEnd);
3595
+ clearFormulaXPerfMarks(formulaImportStart, formulaImportEnd);
3596
+ const formulaInstallStart = markFormulaXPerf("fx:kity-runtime:formula-install");
3597
+ installLegacyKityFormulaRuntime(runtimeWindow);
3598
+ hydrateLegacyKf(runtimeWindow);
3599
+ const formulaInstallEnd = markFormulaXPerf("fx:kity-runtime:formula-install:end");
3600
+ measureFormulaXPerf("fx:kity-runtime:formula-install", formulaInstallStart, formulaInstallEnd);
3601
+ clearFormulaXPerfMarks(formulaInstallStart, formulaInstallEnd);
3602
+ const parserImportStart = markFormulaXPerf("fx:kity-runtime:parser-import");
3603
+ const { installLegacyParserRuntime } = await import("./install-HKCG5NWK.js");
3604
+ const parserImportEnd = markFormulaXPerf("fx:kity-runtime:parser-import:end");
3605
+ measureFormulaXPerf("fx:kity-runtime:parser-import", parserImportStart, parserImportEnd);
3606
+ clearFormulaXPerfMarks(parserImportStart, parserImportEnd);
3607
+ const parserInstallStart = markFormulaXPerf("fx:kity-runtime:parser-install");
3608
+ installLegacyParserRuntime(runtimeWindow);
3609
+ const parserInstallEnd = markFormulaXPerf("fx:kity-runtime:parser-install:end");
3610
+ measureFormulaXPerf("fx:kity-runtime:parser-install", parserInstallStart, parserInstallEnd);
3611
+ clearFormulaXPerfMarks(parserInstallStart, parserInstallEnd);
3612
+ installLegacyRuntime(runtimeWindow);
3613
+ const bootImportStart = markFormulaXPerf("fx:kity-runtime:boot-import");
3614
+ const { installKityEditorStart } = await import("./start-LTYA5XON.js");
3615
+ const bootImportEnd = markFormulaXPerf("fx:kity-runtime:boot-import:end");
3616
+ measureFormulaXPerf("fx:kity-runtime:boot-import", bootImportStart, bootImportEnd);
3617
+ clearFormulaXPerfMarks(bootImportStart, bootImportEnd);
3618
+ const bootInstallStart = markFormulaXPerf("fx:kity-runtime:boot-install");
3619
+ installKityEditorStart(runtimeWindow);
3620
+ const bootInstallEnd = markFormulaXPerf("fx:kity-runtime:boot-install:end");
3621
+ measureFormulaXPerf("fx:kity-runtime:boot-install", bootInstallStart, bootInstallEnd);
3622
+ clearFormulaXPerfMarks(bootInstallStart, bootInstallEnd);
3623
+ } finally {
3624
+ const runtimeTotalEnd = markFormulaXPerf("fx:kity-runtime:total:end");
3625
+ measureFormulaXPerf("fx:kity-runtime:total", runtimeTotalStart, runtimeTotalEnd);
3626
+ clearFormulaXPerfMarks(runtimeTotalStart, runtimeTotalEnd);
3627
+ }
3562
3628
  })();
3563
3629
  return runtimePromise;
3564
3630
  }
3565
3631
  async function createKityEditor(container, options = {}) {
3632
+ const createEditorStart = markFormulaXPerf("fx:create-kity-editor:total");
3566
3633
  const fontsize = options.render?.fontsize ?? 40;
3567
3634
  const editorHeight = normalizeCssSize(options.height, DEFAULT_EDITOR_HEIGHT);
3568
3635
  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
- };
3636
+ try {
3637
+ const stylesheetInserted = ensureKityStylesheet(document, assets.styles.editor);
3638
+ if (stylesheetInserted) {
3639
+ const stylesheetInsertedMark = markFormulaXPerf("fx:kity-css:link-inserted");
3640
+ measureFormulaXPerf("fx:kity-css:link-inserted", createEditorStart, stylesheetInsertedMark);
3641
+ clearFormulaXPerfMarks(stylesheetInsertedMark);
3642
+ }
3643
+ setToolbarAssetUrls(assets.toolbar);
3644
+ await ensureKityRuntime();
3645
+ const runtimeReadyMark = markFormulaXPerf("fx:kity-runtime:ready-for-editor");
3646
+ measureFormulaXPerf("fx:kity-runtime:ready-for-editor", createEditorStart, runtimeReadyMark);
3647
+ clearFormulaXPerfMarks(runtimeReadyMark);
3648
+ const runtimeWindow = window;
3649
+ if (!runtimeWindow.kf?.EditorFactory) {
3650
+ throw new Error("Kity editor runtime did not initialize");
3651
+ }
3652
+ container.innerHTML = "";
3653
+ const host = document.createElement("div");
3654
+ host.className = "kf-editor";
3655
+ host.style.width = "100%";
3656
+ host.style.height = editorHeight;
3657
+ container.appendChild(host);
3658
+ const factoryCreateStart = markFormulaXPerf("fx:kity-editor-factory:create");
3659
+ const factory = runtimeWindow.kf.EditorFactory.create(host, {
3660
+ render: {
3661
+ fontsize
3662
+ },
3663
+ resource: {
3664
+ path: "",
3665
+ fonts: assets.fonts
3666
+ }
3667
+ });
3668
+ const factoryCreateEnd = markFormulaXPerf("fx:kity-editor-factory:create:end");
3669
+ measureFormulaXPerf("fx:kity-editor-factory:create", factoryCreateStart, factoryCreateEnd);
3670
+ clearFormulaXPerfMarks(factoryCreateStart, factoryCreateEnd);
3671
+ return {
3672
+ ready: factory.ready.bind(factory),
3673
+ execCommand(name, value) {
3674
+ factory.ready(function execWhenReady() {
3675
+ this.execCommand(name, value);
3676
+ });
3677
+ },
3678
+ focus() {
3679
+ factory.ready(function focusWhenReady() {
3680
+ this.execCommand("focus");
3681
+ });
3682
+ },
3683
+ destroy() {
3684
+ container.innerHTML = "";
3685
+ },
3686
+ host,
3687
+ raw: factory
3688
+ };
3689
+ } finally {
3690
+ const createEditorEnd = markFormulaXPerf("fx:create-kity-editor:total:end");
3691
+ measureFormulaXPerf("fx:create-kity-editor:total", createEditorStart, createEditorEnd);
3692
+ clearFormulaXPerfMarks(createEditorStart, createEditorEnd);
3693
+ }
3609
3694
  }
3610
3695
  async function mountKityEditor(container, options = {}) {
3611
3696
  const editor = await createKityEditor(container, options);
@@ -3690,6 +3775,7 @@ export {
3690
3775
  legacyKfExtDef,
3691
3776
  legacySysconf,
3692
3777
  legacyUiDef,
3778
+ legacyUiUtils,
3693
3779
  mountKityEditor,
3694
3780
  normalizeMouseEvent,
3695
3781
  publish,