@sheetxl/scripting-react 0.8.0-beta.1 → 0.8.0-beta.2

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,19 +1,20 @@
1
1
  /**
2
- * @license @sheetxl/scripting-react - Scripting React - Monaco based script editor and scripting UI for SheetXL. - v0.8.0-beta.1
2
+ * @license @sheetxl/scripting-react - Scripting React - Monaco based script editor and scripting UI for SheetXL. - v0.8.0-beta.2
3
3
  *
4
4
  * (C) 2026-present SheetXL Inc. & Michael T. Ford
5
5
  * License: The license can be found at https://www.sheetxl.com/license.
6
6
  */
7
- import { Suspense as e, forwardRef as t, lazy as n, memo as r, useEffect as o, useMemo as i, useRef as s, useState as a } from "react";
8
- import { createRoot as c } from "react-dom/client";
9
- import l from "clsx";
10
- import { mergeRefs as u } from "react-merge-refs";
11
- import { CommonUtils as d, TimerUtils as p, messageKey as g } from "@sheetxl/utils";
7
+ import { Suspense as e, forwardRef as t, memo as n, useEffect as r, useMemo as o, useRef as i, useState as s } from "react";
8
+ import { createRoot as a } from "react-dom/client";
9
+ import c from "clsx";
10
+ import { mergeRefs as l } from "react-merge-refs";
11
+ import { CommonUtils as u, TimerUtils as d, defineLazyModule as p, messageKey as g } from "@sheetxl/utils";
12
12
  import { DocTags as m } from "@sheetxl/scripting";
13
- import { Command as f, useCallbackRef as y, useCommands as h, useImperativeElement as b, useNotifier as w, useTranslator as k } from "@sheetxl/utils-react";
14
- import { DefaultTaskPaneRegistry as x, useModelListener as v, useTaskPaneArea as S, useWorkbook as C } from "@sheetxl/react";
15
- import { jsx as N, jsxs as E } from "react/jsx-runtime";
16
- var L = {
13
+ import { IScript as f } from "@sheetxl/sdk";
14
+ import { Command as y, lazyComponent as h, useCallbackRef as b, useCommands as w, useImperativeElement as k, useLazyRetry as x, useNotifier as v, useTranslator as S } from "@sheetxl/utils-react";
15
+ import { jsx as C, jsxs as N } from "react/jsx-runtime";
16
+ import { DefaultTaskPaneRegistry as E, useModelListener as L, useTaskPaneArea as M, useWorkbook as D } from "@sheetxl/react";
17
+ var O = {
17
18
  defaultToken: "invalid",
18
19
  tokenPostfix: ".ts",
19
20
  keywords: [
@@ -354,7 +355,7 @@ var L = {
354
355
  { include: "common" }
355
356
  ]
356
357
  }
357
- }, M = {
358
+ }, T = {
358
359
  lightbulb: { enabled: !1 },
359
360
  tabSize: 2,
360
361
  acceptSuggestionOnCommitCharacter: !0,
@@ -417,8 +418,11 @@ var L = {
417
418
  wordWrapColumn: 80,
418
419
  wordWrapMinified: !0,
419
420
  wrappingIndent: "none"
420
- }, I = "_script-editor_1kuph_5";
421
- var D, T = n(async () => (function() {
421
+ }, A = "_script-editor_1kuph_5", R = (e) => {
422
+ const t = e?.getModules();
423
+ return t ? (t.getByName(f.DEFAULT_MODULE), f.DEFAULT_MODULE) : f.DEFAULT_MODULE;
424
+ }, I = (e) => e?.getModules()?.getByName(R(e)) ?? null;
425
+ var F, P = p("scripting-react:monaco", async () => (function() {
422
426
  if ("undefined" == typeof window) return;
423
427
  const e = window;
424
428
  if (e.__sxlDefineShim) return;
@@ -438,14 +442,31 @@ var D, T = n(async () => (function() {
438
442
  } catch {}
439
443
  }
440
444
  });
441
- }(), import("@monaco-editor/react"))), O = ["define", "require"], A = !1, R = "@0.8.0-beta.1";
442
- "@local" !== R && "@0.0.0" !== R || (R = "");
443
- var F = `https://cdn.jsdelivr.net/npm/@sheetxl/sdk${R}/types/scripting.d.ts`, P = (e) => {
445
+ }(), import("@monaco-editor/react")), { prefetch: !1 }), _ = ({ error: e }) => {
446
+ const t = x(), n = S();
447
+ return N("div", {
448
+ style: {
449
+ display: "flex",
450
+ flexDirection: "column",
451
+ alignItems: "center",
452
+ justifyContent: "center",
453
+ gap: "8px",
454
+ flex: "1 1 100%"
455
+ },
456
+ children: [C("span", { children: e instanceof Error ? e.message : String(e) }), t ? C("button", {
457
+ type: "button",
458
+ onClick: t,
459
+ children: n("scriptEditor.retry")
460
+ }) : null]
461
+ });
462
+ }, $ = h(P.load, { renderError: (e) => C(_, { error: e }) }), W = ["define", "require"], V = !1, B = "@0.8.0-beta.2";
463
+ "@local" !== B && "@0.0.0" !== B || (B = "");
464
+ var U = `https://cdn.jsdelivr.net/npm/@sheetxl/sdk${B}/types/scripting.d.ts`, j = (e) => {
444
465
  try {
445
- e.languages.setMonarchTokensProvider("typescript", L);
466
+ e.languages.setMonarchTokensProvider("typescript", O);
446
467
  } catch (e) {}
447
- }, $ = !1;
448
- function W(e, t) {
468
+ }, H = !1;
469
+ function K(e, t) {
449
470
  let n = null;
450
471
  const r = e.length;
451
472
  for (let o = 0; !n && o < r; o++) {
@@ -454,48 +475,48 @@ function W(e, t) {
454
475
  }
455
476
  return n ? n.getContext() : "formula";
456
477
  }
457
- function _(e, t, n, r, o = []) {
478
+ function q(e, t, n, r, o = []) {
458
479
  if ("function" === r.kind && "export" === r.kindModifiers) {
459
480
  const t = r.spans[0].start, i = e.getPositionAt(t);
460
481
  o.push({
461
482
  name: r.text,
462
- type: W(n, r),
483
+ type: K(n, r),
463
484
  lineNumber: i.lineNumber
464
485
  });
465
486
  }
466
- if (r.childItems) for (const i of r.childItems) _(e, t, n, i, o);
487
+ if (r.childItems) for (const i of r.childItems) q(e, t, n, i, o);
467
488
  return o;
468
489
  }
469
- function V(e, t, n = "function", r) {
490
+ function z(e, t, n = "function", r) {
470
491
  const o = e.spans, i = o.length;
471
492
  for (let s = 0; s < i; s++) {
472
493
  const i = o[s];
473
494
  if (i.start <= t && t < i.start + i.length) {
474
495
  if (e.kind === n && (!r || e.kindModifiers === r)) return e;
475
496
  for (const o of e.childItems || []) {
476
- const e = V(o, t, n, r);
497
+ const e = z(o, t, n, r);
477
498
  if (e) return e;
478
499
  }
479
500
  }
480
501
  return null;
481
502
  }
482
503
  }
483
- function B(e) {
504
+ function G(e) {
484
505
  return "comment" === e.kind;
485
506
  }
486
- var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { current: null }, J = (e, t) => {
487
- j = {
507
+ var Z = null, J = null, X = null, Y = null, Q = !1, ee = !1, te = !1, ne = { current: null }, re = (e, t) => {
508
+ J = {
488
509
  editor: e,
489
510
  monaco: t
490
- }, U && (K = U(e, t));
491
- }, X = (e) => {
492
- if (G) return;
493
- G = !0, e.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
511
+ }, X && (Y = X(e, t));
512
+ }, oe = (e) => {
513
+ if (te) return;
514
+ te = !0, e.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
494
515
  noSemanticValidation: !0,
495
516
  noSyntaxValidation: !1,
496
517
  diagnosticCodesToIgnore: [2307]
497
- }), e.languages.typescript.typescriptDefaults.setEagerModelSync(!0), e.languages.json.jsonDefaults.setDiagnosticsOptions({ validate: !0 }), P(e), ((e) => {
498
- $ || ($ = !0, e.languages.registerCompletionItemProvider("typescript", {
518
+ }), e.languages.typescript.typescriptDefaults.setEagerModelSync(!0), e.languages.json.jsonDefaults.setDiagnosticsOptions({ validate: !0 }), j(e), ((e) => {
519
+ H || (H = !0, e.languages.registerCompletionItemProvider("typescript", {
499
520
  triggerCharacters: ["@"],
500
521
  provideCompletionItems: (t, n) => {
501
522
  const r = t.getValueInRange({
@@ -573,7 +594,7 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
573
594
  (async () => {
574
595
  let t = !1;
575
596
  try {
576
- const n = await (async () => await (await fetch(F)).text())(), r = e.Uri.parse("inmemory://model/\"sheetxl-models\".d.ts");
597
+ const n = await (async () => await (await fetch(U)).text())(), r = e.Uri.parse("inmemory://model/\"sheetxl-models\".d.ts");
577
598
  let o = e.editor.getModel(r);
578
599
  o ? t = !0 : o = e.editor.createModel(n, "typescript", r), e.editor.setModelMarkers(o, "default", []);
579
600
  } catch (e) {
@@ -593,7 +614,7 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
593
614
  const g = d[d.length - 1];
594
615
  "`" !== g && "'" !== g && "\"" !== g || (c++, d = d.substring(0, d.length - 1));
595
616
  try {
596
- Z.current?.getRanges(d) && r.push({
617
+ ne.current?.getRanges(d) && r.push({
597
618
  range: {
598
619
  endColumn: l,
599
620
  endLineNumber: n,
@@ -611,7 +632,7 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
611
632
  } }), e.editor.registerLinkOpener({ open(e) {
612
633
  if (e?.path?.startsWith("/range/")) {
613
634
  try {
614
- Z.current?.getRanges(e.path.substring(7))?.select();
635
+ ne.current?.getRanges(e.path.substring(7))?.select();
615
636
  } catch (e) {
616
637
  console.warn("invalid range", e);
617
638
  }
@@ -628,6 +649,7 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
628
649
  "es2020",
629
650
  "esnext.asynciterable"
630
651
  ],
652
+ strict: !0,
631
653
  downlevelIteration: !0,
632
654
  moduleResolution: e.languages.typescript.ModuleResolutionKind.NodeJs,
633
655
  allowSyntheticDefaultImports: !0,
@@ -645,105 +667,105 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
645
667
  })();
646
668
  }
647
669
  })();
648
- }, Y = (e) => {
670
+ }, ie = (e) => {
649
671
  let t = !1;
650
672
  const n = e ? e.length : 0;
651
673
  for (let r = 0; !t && r < n; r++) 8 === e[r].severity && (t = !0);
652
- }, Q = () => {
653
- const [t, n] = a(() => H.props);
654
- return o(() => (H && (H.setProps = n), () => {
655
- H && (H.setProps = null);
656
- }), []), N("div", {
674
+ }, se = () => {
675
+ const [t, n] = s(() => Z.props);
676
+ return r(() => (Z && (Z.setProps = n), () => {
677
+ Z && (Z.setProps = null);
678
+ }), []), C("div", {
657
679
  style: {
658
680
  display: "flex",
659
681
  flex: "1 1 100%",
660
682
  width: "100%",
661
683
  height: "100%"
662
684
  },
663
- children: N(e, { children: N(T, {
685
+ children: C(e, { children: C($, {
664
686
  theme: t.theme,
665
687
  options: t.options,
666
688
  language: "typescript",
667
689
  value: t.value,
668
690
  onValidate: t.onValidate,
669
691
  beforeMount: t.beforeMount,
670
- onMount: J
692
+ onMount: re
671
693
  }) })
672
694
  });
673
- }, ee = r(t((e, t) => {
674
- const { className: n, style: r, readOnly: m = !1, darkMode: x, loadingPanel: S, autoFocus: L = !0, frame: T, scripts: R, onStatusChange: F, ...$ } = e, W = k(), G = s(null), [J, ee] = a(!1), te = C(), [ne] = h(["insertFunction"]), [re, oe] = a({
695
+ }, ae = n(t((e, t) => {
696
+ const { className: n, style: p, readOnly: m = !1, darkMode: f, loadingPanel: h, autoFocus: x = !0, frame: E, scripts: M, onStatusChange: O, ...P } = e, _ = S(), $ = i(null), [B, U] = s(!1), H = D(), [K] = w(["insertFunction"]), [te, re] = s({
675
697
  message: "Loading",
676
698
  cursor: "",
677
699
  dirty: !1
678
- }), [ie, se] = a({
700
+ }), [ae, ce] = s({
679
701
  loading: !1,
680
702
  compiling: !1,
681
703
  errors: [],
682
704
  warnings: []
683
- }), ae = y((e) => {
684
- oe((t) => {
705
+ }), le = b((e) => {
706
+ re((t) => {
685
707
  const n = {
686
708
  ...t,
687
709
  ...e
688
710
  };
689
- return d.deepEqual(n, t) ? t : (ee(n.dirty ?? !1), n);
711
+ return u.deepEqual(n, t) ? t : (U(n.dirty ?? !1), n);
690
712
  });
691
- }, [F]);
692
- o(() => {
693
- F?.(re);
694
- }, [F, re]);
695
- const ce = w(), le = y(async (e) => {
713
+ }, [O]);
714
+ r(() => {
715
+ O?.(te);
716
+ }, [O, te]);
717
+ const ue = v(), de = b(async (e) => {
696
718
  let t = null, n = null;
697
719
  try {
698
- e && (n = await ce.showBusy(W("scriptEditor.saving")), t = await R.createModule({ source: e })), R.addModule("scripts", t, { replace: !0 }), n?.();
720
+ e && (n = await ue.showBusy(_("scriptEditor.saving")), t = await M.createModule({ source: e })), M.addModule(R(M), t, { replace: !0 }), n?.();
699
721
  } catch (e) {
700
- n?.(), console.warn("Unable to compile", e), ce.showError(e);
722
+ n?.(), console.warn("Unable to compile", e), ue.showError(e);
701
723
  }
702
724
  return t;
703
- }, [R]), ue = y(async () => {
704
- if (!G.current) return;
705
- const e = G.current.editor;
725
+ }, [M]), pe = b(async () => {
726
+ if (!$.current || m) return;
727
+ const e = $.current.editor;
706
728
  let t;
707
- if (re.dirty) {
729
+ if (te.dirty) {
708
730
  const n = e.getValue();
709
- await le(n), t = R.getModules().getByName("scripts");
731
+ await de(n), t = I(M);
710
732
  const r = t?.getSource(), o = e.getValue(), i = (r?.length ?? 0) !== n.length || r !== o;
711
- ae({ dirty: i });
712
- } else t = R.getModules().getByName("scripts");
733
+ le({ dirty: i });
734
+ } else t = I(M);
713
735
  return t;
714
- }, [re, R]);
715
- o(() => {
716
- const e = T?.addListener({ onBeforeHide: async () => (await ue(), !0) });
736
+ }, [te, M]);
737
+ r(() => {
738
+ const e = E?.addListener({ onBeforeHide: async () => (await pe(), !0) });
717
739
  return () => {
718
740
  e?.();
719
741
  };
720
- }, [T]), o(() => {
721
- if (R) return R.addListeners({ onBeforeSearch: () => ue() });
722
- }, [R]), v(te, {
742
+ }, [E]), r(() => {
743
+ if (M) return M.addListeners({ onBeforeSearch: () => pe() });
744
+ }, [M]), L(H, {
723
745
  onBeforeSave: async () => {
724
- await ue();
746
+ await pe();
725
747
  },
726
748
  onLoad: () => {
727
- const e = G.current?.editor;
728
- e && e.setValue(R?.getModules().getItems()[0]?.getSource() ?? "");
749
+ const e = $.current?.editor;
750
+ e && e.setValue(I(M)?.getSource() ?? "");
729
751
  }
730
752
  });
731
- const de = y(async (e) => {
732
- if (!R) return;
753
+ const ge = b(async (e) => {
754
+ if (!M) return;
733
755
  const t = "string" == typeof e ? { functionName: e } : e ?? {};
734
756
  let n = null;
735
757
  if (t?.declaration || t?.functionName) {
736
758
  const e = t.declaration ? t.declaration.getName() : t.functionName;
737
- (await R.searchFunctions({ name: e })).length > 0 && (n = e);
759
+ (await M.searchFunctions({ name: e })).length > 0 && (n = e);
738
760
  }
739
761
  if (n) try {
740
- await R.executeMacro(n);
762
+ await M.executeMacro(n);
741
763
  } catch (e) {
742
- ce.showError(e);
764
+ ue.showError(e);
743
765
  }
744
- }, [R, ce]), [pe, ge] = a(!1), me = y(async () => {
745
- if (!G.current) return null;
746
- const e = G.current, t = e.editor;
766
+ }, [M, ue]), [me, fe] = s(!1), ye = b(async () => {
767
+ if (!$.current) return null;
768
+ const e = $.current, t = e.editor;
747
769
  if (!t) return null;
748
770
  const r = await (async (e, t, n = "export") => {
749
771
  const r = e.getModel();
@@ -753,7 +775,7 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
753
775
  if (!s) return null;
754
776
  const a = s.startLineNumber === s.endLineNumber && s.startColumn === s.endColumn ? r.getLineCount() : Math.min(s.endLineNumber, r.getLineCount());
755
777
  for (let e = s.startLineNumber; !i && e <= a; e++) {
756
- const t = V(o, r.getOffsetAt({
778
+ const t = z(o, r.getOffsetAt({
757
779
  lineNumber: e,
758
780
  column: 1
759
781
  }), "function", n);
@@ -761,25 +783,25 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
761
783
  }
762
784
  return i;
763
785
  })(t, e.monaco);
764
- return ae({ message: (r ?? "").toUpperCase() }), r ? {
786
+ return le({ message: (r ?? "").toUpperCase() }), r ? {
765
787
  source: t.getValue(),
766
788
  name: r
767
789
  } : null;
768
790
  }, []);
769
- y(p.debounce(ue, 200), []);
770
- const fe = y(async (e, t, n) => {
771
- se((e) => ({
791
+ b(d.debounce(pe, 200), []);
792
+ const he = b(async (e, t, n) => {
793
+ ce((e) => ({
772
794
  ...e,
773
795
  compiling: !0
774
796
  }));
775
797
  let r = null;
776
798
  try {
777
- if (r = await R.createModule({
799
+ if (r = await M.createModule({
778
800
  source: n?.getValue(),
779
801
  declarationsOnly: !0
780
802
  }), !r) return;
781
803
  } catch (e) {}
782
- const o = r ? r.getDiagnostics() : d.EmptyArray;
804
+ const o = r ? r.getDiagnostics() : u.EmptyArray;
783
805
  (function(e, t, n) {
784
806
  const r = (n ?? []).map((n) => {
785
807
  const r = e.getPositionAt(n.start), o = e.getPositionAt(n.start + Math.max(n.length, 1));
@@ -795,9 +817,9 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
795
817
  });
796
818
  t.editor.setModelMarkers(e, "sheetxl", r);
797
819
  })(e, t, o), async function(e, t, n, r) {
798
- const o = _(e, t, r, await (await (await t.languages.typescript.getTypeScriptWorker())(e.uri)).getNavigationTree(e.uri.toString())).map((e) => {
820
+ const o = q(e, t, r, await (await (await t.languages.typescript.getTypeScriptWorker())(e.uri)).getNavigationTree(e.uri.toString())).map((e) => {
799
821
  let n = null, r = null;
800
- return "formula" === e.type ? (n = "formula.margin-icon", r = W("scriptEditor.glyph.formula")) : "macro" === e.type ? (n = "macro.margin-icon", r = W("scriptEditor.glyph.macro")) : "autoRun" === e.type && (n = "autorun.margin-icon", r = W("scriptEditor.glyph.autoRun")), {
822
+ return "formula" === e.type ? (n = "formula.margin-icon", r = _("scriptEditor.glyph.formula")) : "macro" === e.type ? (n = "macro.margin-icon", r = _("scriptEditor.glyph.macro")) : "autoRun" === e.type && (n = "autorun.margin-icon", r = _("scriptEditor.glyph.autoRun")), {
801
823
  range: new t.Range(e.lineNumber, 1, e.lineNumber, 1),
802
824
  options: {
803
825
  isWholeLine: !1,
@@ -807,33 +829,33 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
807
829
  }
808
830
  };
809
831
  });
810
- Ce.current && Ce.current.clear(), Ce.current = n.createDecorationsCollection(o);
811
- }(e, t, n, r ? r.getFunctions().getItems() : d.EmptyArray), se((e) => ({
832
+ Ee.current && Ee.current.clear(), Ee.current = n.createDecorationsCollection(o);
833
+ }(e, t, n, r ? r.getFunctions().getItems() : u.EmptyArray), ce((e) => ({
812
834
  ...e,
813
835
  compiling: !1,
814
836
  errors: o.filter((e) => "error" === e.severity).map((e) => `${e.code}: ${e.message}`),
815
837
  warnings: o.filter((e) => "warning" === e.severity).map((e) => `${e.code}: ${e.message}`)
816
838
  }));
817
- }, [R]), ye = y(p.debounce(fe, 580), []), he = y(async (e) => {
818
- if (!G.current) return;
819
- const t = G.current.editor.getPosition(), n = { cursor: t ? t.toString() : "" };
820
- void 0 !== e && (n.message = e), ae(n);
839
+ }, [M]), be = b(d.debounce(he, 580), []), we = b(async (e) => {
840
+ if (!$.current) return;
841
+ const t = $.current.editor.getPosition(), n = { cursor: t ? t.toString() : "" };
842
+ void 0 !== e && (n.message = e), le(n);
821
843
  }, []);
822
- o(() => {}, [ie]);
823
- const be = s(null), we = y(async () => {
824
- await ue(), T?.close();
825
- }, [T]), ke = y((e) => {
826
- const t = G.current?.editor;
844
+ r(() => {}, [ae]);
845
+ const ke = i(null), xe = b(async () => {
846
+ await pe(), E?.close();
847
+ }, [E]), ve = b((e) => {
848
+ const t = $.current?.editor;
827
849
  return !!t && (t.focus(e), !0);
828
- }, [re]), xe = y(async () => {
829
- const e = G.current?.editor, t = e?.getModel(), n = e?.getSelection();
850
+ }, [te]), Se = b(async () => {
851
+ const e = $.current?.editor, t = e?.getModel(), n = e?.getSelection();
830
852
  if (!t || !n) return;
831
853
  const r = n.isEmpty() ? t.getLineContent(n.positionLineNumber) + "\n" : t.getValueInRange(n);
832
854
  try {
833
855
  await navigator.clipboard?.writeText(r);
834
856
  } catch {}
835
- }, []), ve = y(async () => {
836
- const e = G.current, t = e?.editor, n = t?.getModel(), r = t?.getSelection();
857
+ }, []), Ce = b(async () => {
858
+ const e = $.current, t = e?.editor, n = t?.getModel(), r = t?.getSelection();
837
859
  if (!n || !r) return;
838
860
  const o = r.isEmpty() ? new e.monaco.Range(r.positionLineNumber, 1, r.positionLineNumber + 1, 1) : r, i = n.getValueInRange(o);
839
861
  try {
@@ -846,8 +868,8 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
846
868
  text: "",
847
869
  forceMoveMarkers: !0
848
870
  }]), t.pushUndoStop(), t.focus();
849
- }, []), Se = y(async () => {
850
- const e = G.current?.editor;
871
+ }, []), Ne = b(async () => {
872
+ const e = $.current?.editor;
851
873
  if (!e) return;
852
874
  let t;
853
875
  try {
@@ -862,16 +884,16 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
862
884
  text: t,
863
885
  forceMoveMarkers: !0
864
886
  }]), e.pushUndoStop(), e.focus();
865
- }, []), Ce = s(null), Ne = y((e, t) => {
866
- G.current = {
887
+ }, []), Ee = i(null), Le = b((e, t) => {
888
+ $.current = {
867
889
  monaco: t,
868
890
  editor: e
869
- }, Z.current = te ?? null;
891
+ }, ne.current = H ?? null;
870
892
  const n = e.getModel(), r = [];
871
- ge(!0);
872
- const o = R?.getModules().getItems()[0]?.getSource() ?? "";
873
- return e.getValue() !== o && e.setValue(o), q ? (he(""), ye(n, t, e), P(t)) : (q = !0, he("Loading Types"), import("monaco-editor-auto-typings/custom-editor.js").then(async (n) => {
874
- he(""), n.AutoTypings.create(e, {
893
+ fe(!0);
894
+ const o = I(M)?.getSource() ?? "";
895
+ return e.getValue() !== o && e.setValue(o), Q ? (we(""), be(n, t, e), j(t)) : (Q = !0, we("Loading Types"), import("monaco-editor-auto-typings/custom-editor.js").then(async (n) => {
896
+ we(""), n.AutoTypings.create(e, {
875
897
  shareCache: !0,
876
898
  dontAdaptEditorOptions: !0,
877
899
  monaco: t,
@@ -888,8 +910,8 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
888
910
  "Not implemented yet" === e && (e = "Compile Errors"), console.warn("Error compiling types", e);
889
911
  }
890
912
  }).then((n) => {
891
- be.current = n, ye(e.getModel(), t, e), (() => {
892
- if (A) return;
913
+ ke.current = n, be(e.getModel(), t, e), (() => {
914
+ if (V) return;
893
915
  const e = (e, t) => {
894
916
  try {
895
917
  if (void 0 === t) try {
@@ -901,15 +923,17 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
901
923
  }
902
924
  else globalThis[e] = t;
903
925
  } catch (e) {}
904
- }, t = O.length;
926
+ }, t = W.length;
905
927
  for (let n = 0; n < t; n++) {
906
- const t = O[n];
907
- e(t, D[t]);
928
+ const t = W[n];
929
+ e(t, F[t]);
908
930
  }
909
- D = void 0, A = !0;
910
- })(), P(t);
931
+ F = void 0, V = !0;
932
+ })(), j(t);
911
933
  });
912
- })), z || (z = !0, function(e, t) {
934
+ }).catch((e) => {
935
+ Q = !1, we(""), console.warn("Auto typings could not be loaded:", e);
936
+ })), ee || (ee = !0, function(e, t) {
913
937
  t.languages.registerInlayHintsProvider("typescript", { provideInlayHints(e, n, r) {
914
938
  const o = [];
915
939
  for (let n = 1; n < e.getLineCount() + 1; n++) {
@@ -938,109 +962,109 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
938
962
  label: "Execute",
939
963
  keybindings: [t.KeyCode.F8],
940
964
  run: async (e) => {
941
- const t = await me();
942
- de(t);
965
+ const t = await ye();
966
+ ge(t);
943
967
  }
944
968
  })), r.push(e.addAction({
945
969
  id: "saveScript",
946
970
  label: "Save Script",
947
971
  keybindings: [t.KeyMod.CtrlCmd | t.KeyCode.KeyS],
948
972
  run: (e) => {
949
- ue();
973
+ pe();
950
974
  }
951
975
  })), r.push(e.onMouseDown(async (e) => {
952
976
  if (e.target.type === t.editor.MouseTargetType.GUTTER_GLYPH_MARGIN) {
953
977
  e.event.browserEvent.preventDefault();
954
- const r = e.target.position.lineNumber, o = await (await (await t.languages.typescript.getTypeScriptWorker())(n.uri)).getNavigationTree(n.uri.toString()), i = await ue();
978
+ const r = e.target.position.lineNumber, o = await (await (await t.languages.typescript.getTypeScriptWorker())(n.uri)).getNavigationTree(n.uri.toString()), i = await pe();
955
979
  if (!i) return;
956
- const s = i.getFunctions().getItems(), a = _(n, t, s, o).find((e) => e.lineNumber === r);
980
+ const s = i.getFunctions().getItems(), a = q(n, t, s, o).find((e) => e.lineNumber === r);
957
981
  if (a) {
958
982
  const t = e.event.browserEvent.button;
959
983
  if (0 === t) {
960
- if ("macro" === a.type || "autoRun" === a.type) de({ functionName: a.name });
984
+ if ("macro" === a.type || "autoRun" === a.type) ge({ functionName: a.name });
961
985
  else if ("formula" === a.type) {
962
986
  const e = s?.find((e) => e.getName() === a.name);
963
- e && ne?.execute(e);
987
+ e && K?.execute(e);
964
988
  }
965
989
  } else 2 === t && (console.log("Context menu for function:", a.name), e.event.browserEvent.preventDefault());
966
990
  }
967
991
  }
968
992
  })), r.push(e.onDidChangeModelContent(() => {
969
- ae({ dirty: !0 }), ye(n, t, e);
993
+ le({ dirty: !0 }), be(n, t, e);
970
994
  })), r.push(t.editor.onDidChangeMarkers((e) => {
971
995
  e.some((e) => e.toString() === n.uri.toString());
972
996
  })), r.push(n.onDidChangeContent((t) => {
973
- const n = R.getModules().getByName("scripts")?.getSource(), r = e.getValue(), o = (n?.length ?? 0) !== r.length || n !== r;
974
- ae({ dirty: o });
997
+ const n = I(M)?.getSource(), r = e.getValue(), o = (n?.length ?? 0) !== r.length || n !== r;
998
+ le({ dirty: o });
975
999
  })), r.push(e.onDidChangeCursorPosition((e) => {
976
- he();
1000
+ we();
977
1001
  })), () => {
978
1002
  for (const e of r) try {
979
1003
  e.dispose();
980
1004
  } catch {}
981
1005
  };
982
- }, [R, te]), Ee = i(() => pe ? null : S || N("div", {
1006
+ }, [M, H]), Me = o(() => me ? null : h || C("div", {
983
1007
  style: {
984
1008
  position: "absolute",
985
1009
  width: "100%",
986
1010
  height: "100%"
987
1011
  },
988
- children: W("scriptEditor.loading")
989
- }), [pe, S]), Le = i(() => ({
990
- ...M,
1012
+ children: _("scriptEditor.loading")
1013
+ }), [me, h]), De = o(() => ({
1014
+ ...T,
991
1015
  fixedOverflowWidgets: !1,
992
1016
  contextmenu: !1,
993
1017
  readOnly: m
994
- }), [m]), Me = s(null);
995
- o(() => {
1018
+ }), [m]), Oe = i(null);
1019
+ r(() => {
996
1020
  const e = {
997
- theme: x ? "extended-dark" : "extended",
998
- options: Le,
999
- value: R?.getModules()?.getByName("scripts")?.getSource() ?? "",
1000
- beforeMount: X,
1001
- onValidate: Y
1021
+ theme: f ? "extended-dark" : "extended",
1022
+ options: De,
1023
+ value: I(M)?.getSource() ?? "",
1024
+ beforeMount: oe,
1025
+ onValidate: ie
1002
1026
  }, t = ((e) => {
1003
- if (H) return H;
1004
- if (!D) {
1005
- D = {};
1006
- const e = O.length;
1027
+ if (Z) return Z;
1028
+ if (!F) {
1029
+ F = {};
1030
+ const e = W.length;
1007
1031
  for (let t = 0; t < e; t++) {
1008
- const e = O[t];
1009
- D[e] = globalThis[e];
1032
+ const e = W[t];
1033
+ F[e] = globalThis[e];
1010
1034
  }
1011
1035
  }
1012
1036
  const t = document.createElement("div");
1013
1037
  t.style.display = "none";
1014
1038
  const n = document.createElement("div");
1015
1039
  n.style.width = "100%", n.style.height = "100%", n.style.display = "flex", n.style.flex = "1 1 100%", t.appendChild(n), document.body.appendChild(t);
1016
- const r = c(n);
1017
- return H = {
1040
+ const r = a(n);
1041
+ return Z = {
1018
1042
  root: r,
1019
1043
  hostEl: n,
1020
1044
  stashEl: t,
1021
1045
  props: e,
1022
1046
  setProps: null
1023
- }, r.render(N(Q, {})), H;
1047
+ }, r.render(C(se, {})), Z;
1024
1048
  })(e);
1025
- return t.props = e, t.setProps?.(e), U = (e, t) => Ne(e, t), Me.current?.appendChild(t.hostEl), j && (K = Ne(j.editor, j.monaco)), () => {
1026
- K?.(), K = null, U = null, G.current = null, H?.stashEl.appendChild(H.hostEl);
1049
+ return t.props = e, t.setProps?.(e), X = (e, t) => Le(e, t), Oe.current?.appendChild(t.hostEl), J && (Y = Le(J.editor, J.monaco)), () => {
1050
+ Y?.(), Y = null, X = null, $.current = null, Z?.stashEl.appendChild(Z.hostEl);
1027
1051
  };
1028
- }, []), o(() => {
1029
- if (!H) return;
1052
+ }, []), r(() => {
1053
+ if (!Z) return;
1030
1054
  const e = {
1031
- ...H.props,
1032
- theme: x ? "extended-dark" : "extended",
1033
- options: Le
1055
+ ...Z.props,
1056
+ theme: f ? "extended-dark" : "extended",
1057
+ options: De
1034
1058
  };
1035
- H.props = e, H.setProps?.(e);
1036
- }, [x, Le]), o(() => {
1037
- const e = G.current, t = e?.editor;
1059
+ Z.props = e, Z.setProps?.(e);
1060
+ }, [f, De]), r(() => {
1061
+ const e = $.current, t = e?.editor;
1038
1062
  if (!t) return;
1039
- Z.current = te ?? null;
1040
- const n = R?.getModules().getItems()[0]?.getSource() ?? "";
1041
- t.getValue() !== n && (t.setValue(n), ye(t.getModel(), e.monaco, t), ae({ dirty: !1 }));
1042
- }, [R, te]);
1043
- const Ie = E("div", {
1063
+ ne.current = H ?? null;
1064
+ const n = I(M)?.getSource() ?? "";
1065
+ t.getValue() !== n && (t.setValue(n), be(t.getModel(), e.monaco, t), le({ dirty: !1 }));
1066
+ }, [M, H]);
1067
+ const Te = N("div", {
1044
1068
  style: {
1045
1069
  display: "flex",
1046
1070
  flex: "1 1 100%",
@@ -1049,26 +1073,26 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
1049
1073
  position: "relative",
1050
1074
  boxSizing: "border-box"
1051
1075
  },
1052
- children: [N("div", {
1053
- ref: Me,
1076
+ children: [C("div", {
1077
+ ref: Oe,
1054
1078
  style: {
1055
1079
  display: "flex",
1056
1080
  flex: "1 1 100%",
1057
1081
  width: "100%",
1058
1082
  position: "relative"
1059
1083
  }
1060
- }), Ee]
1061
- }), De = b(t, () => ({
1084
+ }), Me]
1085
+ }), Ae = k(t, () => ({
1062
1086
  isScriptEditorElement: () => !0,
1063
1087
  showCommandPalette: () => {
1064
- G.current?.editor && G.current.editor.trigger("keyboard", "editor.action.quickCommand", null);
1088
+ $.current?.editor && $.current.editor.trigger("keyboard", "editor.action.quickCommand", null);
1065
1089
  },
1066
- getScripts: () => R,
1090
+ getScripts: () => M,
1067
1091
  focus: (e) => {
1068
- ke(e);
1092
+ ve(e);
1069
1093
  },
1070
- close: () => we(),
1071
- insertSnippet: (e) => G.current?.editor ? (async (e, t, n) => {
1094
+ close: () => xe(),
1095
+ insertSnippet: (e) => $.current?.editor ? (async (e, t, n) => {
1072
1096
  const r = e.getModel();
1073
1097
  if (!r) return;
1074
1098
  const o = await (await (await t.languages.typescript.getTypeScriptWorker())(r.uri)).getNavigationTree(r.uri.toString()), i = e.getPosition();
@@ -1076,11 +1100,11 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
1076
1100
  s.lineNumber = 1, s.column = 1;
1077
1101
  let a = !1;
1078
1102
  for (let e = i.lineNumber; e >= 1; e--) if ("" === r.getLineContent(e).trim()) {
1079
- const t = V(o, r.getOffsetAt({
1103
+ const t = z(o, r.getOffsetAt({
1080
1104
  lineNumber: e,
1081
1105
  column: 1
1082
1106
  }), "function");
1083
- if (!t || B(t)) {
1107
+ if (!t || G(t)) {
1084
1108
  a = !0, s.lineNumber = e + 1;
1085
1109
  break;
1086
1110
  }
@@ -1104,71 +1128,78 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
1104
1128
  endLineNumber: p.startLineNumber,
1105
1129
  endColumn: p.startColumn
1106
1130
  });
1107
- })(G.current.editor, G.current.monaco, e) : Promise.resolve(),
1131
+ })($.current.editor, $.current.monaco, e) : Promise.resolve(),
1108
1132
  saveSource: async () => {
1109
- await ue();
1133
+ await pe();
1110
1134
  },
1111
1135
  runAction: (e) => {
1112
- const t = G.current?.editor;
1136
+ const t = $.current?.editor;
1113
1137
  t && (t.focus(), t.trigger("source", e, null));
1114
1138
  },
1115
- copy: () => xe(),
1116
- cut: () => ve(),
1117
- paste: () => Se()
1118
- }), [te]), [Te, Oe] = a(null), Ae = y((e) => {
1119
- null !== e && Oe(e);
1120
- }, []), Re = f.useCommandScope();
1121
- return o(() => {
1122
- if (!Re || !Te) return;
1123
- const e = Re.contribution();
1139
+ copy: () => Se(),
1140
+ cut: () => Ce(),
1141
+ paste: () => Ne()
1142
+ }), [H]), [Re, Ie] = s(null), Fe = b((e) => {
1143
+ null !== e && Ie(e);
1144
+ }, []), Pe = y.useCommandScope();
1145
+ return r(() => {
1146
+ if (!Pe || !Re) return;
1147
+ const e = Pe.contribution();
1124
1148
  return function(e, t) {
1125
1149
  const n = t.scriptEditor, r = t;
1126
1150
  e.updateCommand("scriptingHelp", { callback: () => {
1127
1151
  window?.open("https://www.sheetxl.com/docs/guides/concepts/scripting");
1128
1152
  } }), e.updateCommand("showScriptingCommands", { callback: () => {
1129
1153
  n.showCommandPalette();
1130
- } }), e.updateCommand("insertScript", { callback: (e) => n.insertSnippet(e ?? "") }), e.updateCommand("saveScripts", {
1131
- disabledReasons: r?.isDirty ? [] : [g("reason.noChangesToSave")],
1154
+ } });
1155
+ const o = r?.readOnly ? [g("reason.scriptsReadOnly")] : [];
1156
+ e.updateCommand("insertScript", {
1157
+ disabledReasons: o,
1158
+ callback: (e) => n.insertSnippet(e ?? "")
1159
+ }), e.updateCommand("saveScripts", {
1160
+ disabledReasons: [...o, ...r?.isDirty ? [] : [g("reason.noChangesToSave")]],
1132
1161
  callback: async () => {
1133
1162
  await n.saveSource();
1134
1163
  }
1135
1164
  }), e.updateCommand("copy", { callback: () => n.copy() }), e.updateCommand("cut", { callback: () => n.cut() }), e.updateCommand("paste", { callback: () => n.paste() }), e.updateCommand("selectAll", { callback: () => n.runAction("editor.action.selectAll") }), e.updateCommand("find", { callback: () => n.runAction("actions.find") }), e.updateCommand("findReplace", { callback: () => n.runAction("editor.action.startFindReplaceAction") }), e.updateCommand("goto", { callback: () => n.runAction("editor.action.gotoLine") });
1136
1165
  }(e, {
1137
- scriptEditor: Te,
1138
- isDirty: J
1166
+ scriptEditor: Re,
1167
+ isDirty: B,
1168
+ readOnly: m
1139
1169
  }), () => {
1140
1170
  e.remove();
1141
1171
  };
1142
1172
  }, [
1173
+ Pe,
1143
1174
  Re,
1144
- Te,
1145
- J
1146
- ]), o(() => (L && requestAnimationFrame(() => {
1147
- De.current?.focus();
1175
+ B,
1176
+ m
1177
+ ]), r(() => (x && requestAnimationFrame(() => {
1178
+ Ae.current?.focus();
1148
1179
  }), () => {
1149
- G.current?.editor && requestAnimationFrame(() => {
1150
- De.current?.contains(document.activeElement) && De.current?.focus();
1180
+ $.current?.editor && requestAnimationFrame(() => {
1181
+ Ae.current?.contains(document.activeElement) && Ae.current?.focus();
1151
1182
  });
1152
- }), []), N("div", {
1153
- className: l("script-editor", I, n),
1183
+ }), []), C("div", {
1184
+ className: c("script-editor", A, n),
1154
1185
  style: {
1155
1186
  display: "flex",
1156
1187
  flexDirection: "row",
1157
1188
  flex: "1 1 100%",
1158
1189
  position: "relative",
1159
- ...r
1190
+ ...p
1160
1191
  },
1161
- ref: u([De, Ae]),
1162
- ...$,
1163
- children: Ie
1192
+ ref: l([Ae, Fe]),
1193
+ ...P,
1194
+ children: Te
1164
1195
  });
1165
- })), te = null, ne = (e) => {
1166
- te = e;
1167
- }, re = () => te, oe = r((e) => {
1168
- const { scope: t, data: n } = e, { workbook: r } = n, i = w(), s = S(), a = s.getOpen(), c = y(async (e) => {
1169
- if (!r) return;
1196
+ })), ce = null, le = (e) => {
1197
+ ce = e;
1198
+ }, ue = () => ce, de = n((e) => {
1199
+ const { scope: t, data: n } = e, { workbook: o } = n, i = v(), s = M(), a = s.getOpen(), c = b(async (e) => {
1200
+ if (!o) return;
1170
1201
  let t = null;
1171
- const n = r.getScripts();
1202
+ const n = o.getScripts();
1172
1203
  if (e?.declaration || e?.functionName) {
1173
1204
  const r = e.declaration ? e.declaration.getName() : e.functionName;
1174
1205
  (await n.searchFunctions({ name: r })).length > 0 && (t = r);
@@ -1179,11 +1210,11 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
1179
1210
  i.showError(e);
1180
1211
  return;
1181
1212
  }
1182
- }, [r, i]), l = y(() => {
1183
- const e = re();
1184
- e ? r && e({ workbook: r }) : s.getOpen().includes("scriptEditor") ? s.close("scriptEditor") : s.activate("scriptEditor", {});
1185
- }, [r, s]), u = a.includes("scriptEditor");
1186
- return o(() => {
1213
+ }, [o, i]), l = b(() => {
1214
+ const e = ue();
1215
+ e ? o && e({ workbook: o }) : s.getOpen().includes("scriptEditor") ? s.close("scriptEditor") : s.activate("scriptEditor", {});
1216
+ }, [o, s]), u = a.includes("scriptEditor");
1217
+ return r(() => {
1187
1218
  if (!t) return;
1188
1219
  const e = t.contribution();
1189
1220
  return e.updateCommand("showScriptEditor", {
@@ -1197,22 +1228,24 @@ var H = null, j = null, U = null, K = null, q = !1, z = !1, G = !1, Z = { curren
1197
1228
  }, [
1198
1229
  t,
1199
1230
  u,
1200
- r,
1231
+ o,
1201
1232
  c,
1202
1233
  l
1203
- ]), v(r, { onLoad: () => {
1204
- r?.getScripts().start();
1234
+ ]), L(o, { onLoad: () => {
1235
+ o?.getScripts().start();
1205
1236
  } }), null;
1206
1237
  });
1207
- oe.displayName = "WorkbookScriptCommands";
1208
- var ie = {
1238
+ de.displayName = "WorkbookScriptCommands";
1239
+ var pe = {
1209
1240
  "reason.noChangesToSave": "No changes to save",
1241
+ "reason.scriptsReadOnly": "Scripts can't be changed while the workbook structure is protected.",
1210
1242
  "scriptEditor.glyph.autoRun": "Auto Run",
1211
1243
  "scriptEditor.glyph.formula": "Formula",
1212
1244
  "scriptEditor.glyph.macro": "Macro",
1213
1245
  "scriptEditor.loading": "Loading...",
1246
+ "scriptEditor.retry": "Retry",
1214
1247
  "scriptEditor.saving": "Saving Scripts..."
1215
- }, se = [
1248
+ }, ge = [
1216
1249
  {
1217
1250
  sourceId: "groups",
1218
1251
  catalog: {
@@ -1294,30 +1327,30 @@ var ie = {
1294
1327
  }
1295
1328
  }
1296
1329
  }
1297
- ], { DefaultCommandCatalogService: ae } = f, ce = !1;
1298
- var le = (e) => {
1330
+ ], { DefaultCommandCatalogService: me } = y, fe = !1;
1331
+ var ye = (e) => {
1299
1332
  const { model: t, entryKey: n, frame: r, ref: o, ...i } = e;
1300
- return N(ee, {
1333
+ return C(ae, {
1301
1334
  ref: o,
1302
1335
  frame: r,
1303
1336
  scripts: t?.getScripts(),
1304
1337
  ...i
1305
1338
  });
1306
- }, ue = () => {
1307
- x.registerTaskPane({
1339
+ }, he = () => {
1340
+ E.registerTaskPane({
1308
1341
  key: "scriptEditor",
1309
1342
  getTitle: () => "Script (Typescript)",
1310
1343
  getIcon: () => "ScriptRun",
1311
- component: le,
1344
+ component: ye,
1312
1345
  keepMounted: !0
1313
1346
  }, !0), function() {
1314
- if (ce) return;
1315
- ce = !0;
1316
- const e = ae;
1347
+ if (fe) return;
1348
+ fe = !0;
1349
+ const e = me;
1317
1350
  e.registerFromManifest("@sheetxl/scripting-react", import.meta.url, {
1318
- messages: ie,
1319
- catalogs: se
1351
+ messages: pe,
1352
+ catalogs: ge
1320
1353
  }), e.prefetch();
1321
- }(), f.DefaultCommandRegistry.registerDynamic("workbook", oe);
1354
+ }(), y.DefaultCommandRegistry.registerDynamic("workbook", de);
1322
1355
  };
1323
- export { ee as ScriptEditor, ue as ScriptingReactPlugin, oe as WorkbookScriptCommands, re as getScriptEditorOpener, ne as setScriptEditorOpener };
1356
+ export { ae as ScriptEditor, he as ScriptingReactPlugin, de as WorkbookScriptCommands, ue as getScriptEditorOpener, le as setScriptEditorOpener };