@xlsx-editor.dev/vue 0.0.0-stage → 0.1.0-beta.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 ADDED
@@ -0,0 +1,3211 @@
1
+ // src/XlsxEditor.ts
2
+ import {
3
+ defineComponent as defineComponent10,
4
+ getCurrentInstance as getCurrentInstance2,
5
+ h as h3,
6
+ mergeProps,
7
+ shallowRef as shallowRef11
8
+ } from "vue";
9
+ import {
10
+ emptySnapshotOf as emptySnapshotOf3
11
+ } from "@xlsx-editor.dev/core/editor";
12
+
13
+ // src/XlsxEditorRoot.ts
14
+ import {
15
+ defineComponent,
16
+ getCurrentInstance,
17
+ h,
18
+ inject as inject3,
19
+ onBeforeUnmount,
20
+ onMounted as onMounted3,
21
+ onUnmounted,
22
+ provide as provide3,
23
+ shallowRef as shallowRef3,
24
+ watch as watch3
25
+ } from "vue";
26
+ import {
27
+ createXlsxEditor,
28
+ cycleRegion,
29
+ emptySnapshotOf as emptySnapshotOf2,
30
+ followModeProp,
31
+ relabelEditor,
32
+ reportFontError,
33
+ sameFontSource,
34
+ sameNumberLocale,
35
+ watchFontErrors,
36
+ watchGridSetup,
37
+ whenOpenSettles
38
+ } from "@xlsx-editor.dev/core/editor";
39
+
40
+ // src/context.ts
41
+ import {
42
+ getCurrentScope,
43
+ inject,
44
+ onMounted,
45
+ onScopeDispose,
46
+ provide,
47
+ shallowRef,
48
+ watch
49
+ } from "vue";
50
+ import {
51
+ createT,
52
+ deepMerge,
53
+ en
54
+ } from "@xlsx-editor.dev/i18n";
55
+ import {
56
+ emptySnapshotOf
57
+ } from "@xlsx-editor.dev/core/editor";
58
+ var xlsxEditorKey = /* @__PURE__ */ Symbol("xlsxEditor");
59
+ var xlsxTranslationKey = /* @__PURE__ */ Symbol("xlsxTranslation");
60
+ var xlsxEditorTickKey = /* @__PURE__ */ Symbol("xlsxEditorTick");
61
+ var rootModeKey = /* @__PURE__ */ Symbol("xlsxRootMode");
62
+ var modeWaitsKey = /* @__PURE__ */ Symbol("xlsxModeWaits");
63
+ function translatorFor(locale) {
64
+ const merged = deepMerge(
65
+ en,
66
+ locale
67
+ );
68
+ return createT(merged, typeof locale?._lang === "string" ? locale._lang : "en");
69
+ }
70
+ var nullEditor = shallowRef(null);
71
+ var english = shallowRef(translatorFor(void 0));
72
+ var noWait = shallowRef(false);
73
+ var editMode = () => "edit";
74
+ function provideXlsxEditor(editor) {
75
+ provide(xlsxEditorKey, editor);
76
+ const tick = shallowRef(0);
77
+ provide(xlsxEditorTickKey, tick);
78
+ let off = null;
79
+ watch(
80
+ editor,
81
+ (next) => {
82
+ off?.();
83
+ off = next ? next.subscribe(() => tick.value += 1) : null;
84
+ tick.value += 1;
85
+ },
86
+ { immediate: true }
87
+ );
88
+ onScopeDispose(() => {
89
+ off?.();
90
+ off = null;
91
+ });
92
+ }
93
+ function useXlsxEditor() {
94
+ return inject(xlsxEditorKey, nullEditor);
95
+ }
96
+ function useTranslator() {
97
+ return inject(xlsxTranslationKey, english);
98
+ }
99
+ function useTranslation() {
100
+ const base = inject(xlsxTranslationKey, english);
101
+ return { t: ((key, params) => base.value(key, params)) };
102
+ }
103
+ function hasListener(props, event, once = true) {
104
+ const name = `on${event[0].toUpperCase()}${event.slice(1)}`;
105
+ return Boolean(props?.[name] ?? (once ? props?.[`${name}Once`] : void 0));
106
+ }
107
+ function useViewModeWaits() {
108
+ return inject(modeWaitsKey, noWait);
109
+ }
110
+ var layoutPartsKey = /* @__PURE__ */ Symbol("xlsxLayoutParts");
111
+ function useLayoutPart(part) {
112
+ const parts = inject(layoutPartsKey, null);
113
+ if (!parts) return;
114
+ let counted = false;
115
+ onMounted(() => {
116
+ parts[part] += 1;
117
+ counted = true;
118
+ });
119
+ onScopeDispose(() => {
120
+ if (counted) parts[part] -= 1;
121
+ });
122
+ }
123
+ function useEditorState(selector, isEqual = Object.is, deps) {
124
+ const editorRef = useXlsxEditor();
125
+ const tick = inject(xlsxEditorTickKey, null);
126
+ const mode = inject(rootModeKey, editMode);
127
+ let seen = null;
128
+ const current = () => editorRef.value?.snapshot() ?? emptySnapshotOf(mode());
129
+ const read = () => {
130
+ seen = current();
131
+ return selector(seen);
132
+ };
133
+ const out = shallowRef(read());
134
+ const update = () => {
135
+ const next = read();
136
+ if (!isEqual(out.value, next)) out.value = next;
137
+ };
138
+ const onChange = () => {
139
+ if (current() !== seen) update();
140
+ };
141
+ if (!getCurrentScope()) return out;
142
+ if (deps) watch(deps, update);
143
+ watch(mode, onChange);
144
+ if (tick) {
145
+ watch(editorRef, update);
146
+ watch(tick, onChange, { flush: "sync" });
147
+ return out;
148
+ }
149
+ let off = null;
150
+ watch(
151
+ editorRef,
152
+ (editor) => {
153
+ off?.();
154
+ off = editor ? editor.subscribe(onChange) : null;
155
+ update();
156
+ },
157
+ { immediate: true }
158
+ );
159
+ onScopeDispose(() => {
160
+ off?.();
161
+ off = null;
162
+ });
163
+ return out;
164
+ }
165
+ var whole = (snapshot) => snapshot;
166
+ function useEditorSnapshot() {
167
+ return useEditorState(whole);
168
+ }
169
+
170
+ // src/color-mode.ts
171
+ import {
172
+ computed,
173
+ inject as inject2,
174
+ onMounted as onMounted2,
175
+ onScopeDispose as onScopeDispose2,
176
+ provide as provide2,
177
+ shallowRef as shallowRef2,
178
+ watch as watch2
179
+ } from "vue";
180
+ import {
181
+ resolveDarkChrome,
182
+ subscribeSystemDark,
183
+ systemPrefersDark
184
+ } from "@xlsx-editor.dev/core/editor";
185
+ var chromeDarkKey = /* @__PURE__ */ Symbol("xlsxChromeDark");
186
+ var scopedByAncestorKey = /* @__PURE__ */ Symbol("xlsxScopedByAncestor");
187
+ function provideScopedByAncestor() {
188
+ provide2(scopedByAncestorKey, true);
189
+ }
190
+ function useScopedByAncestor() {
191
+ return inject2(scopedByAncestorKey, false);
192
+ }
193
+ var lightChrome = computed(() => false);
194
+ function provideChromeDark(mode) {
195
+ const systemDark = shallowRef2(false);
196
+ let off = null;
197
+ const follow = (next) => {
198
+ off?.();
199
+ off = null;
200
+ if (next !== "system") return;
201
+ systemDark.value = systemPrefersDark();
202
+ off = subscribeSystemDark(() => systemDark.value = systemPrefersDark());
203
+ };
204
+ onMounted2(() => follow(mode()));
205
+ watch2(mode, follow);
206
+ onScopeDispose2(() => off?.());
207
+ provide2(
208
+ chromeDarkKey,
209
+ computed(() => resolveDarkChrome(mode(), systemDark.value))
210
+ );
211
+ }
212
+ function useChromeDark() {
213
+ return inject2(chromeDarkKey, lightChrome);
214
+ }
215
+ function useEditorColorMode() {
216
+ const dark = useChromeDark();
217
+ return { colorMode: computed(() => dark.value ? "dark" : "light") };
218
+ }
219
+ function useScopeClassName() {
220
+ const scoped = useScopedByAncestor();
221
+ const dark = useChromeDark();
222
+ return computed(() => scoped ? "" : dark.value ? "xlsx-editor dark " : "xlsx-editor ");
223
+ }
224
+
225
+ // src/is-development.ts
226
+ function developmentFrom(signals) {
227
+ const dev = signals.viteEnv?.DEV;
228
+ if (typeof dev === "boolean") return dev;
229
+ return signals.hasProcess && signals.nodeEnv !== "production";
230
+ }
231
+ function viteEnv() {
232
+ try {
233
+ return import.meta.env;
234
+ } catch {
235
+ return void 0;
236
+ }
237
+ }
238
+ function isDevelopment() {
239
+ const hasProcess = typeof process !== "undefined";
240
+ return developmentFrom({
241
+ viteEnv: viteEnv(),
242
+ nodeEnv: hasProcess ? process.env.NODE_ENV : void 0,
243
+ hasProcess
244
+ });
245
+ }
246
+
247
+ // src/XlsxEditorRoot.ts
248
+ var xlsxEditorRootProps = {
249
+ /**
250
+ * The bytes of a workbook or a template package. A new array or buffer opens the new
251
+ * workbook; a change to `undefined` closes the open workbook (`close`).
252
+ */
253
+ document: {
254
+ type: [Uint8Array, ArrayBuffer],
255
+ default: void 0
256
+ },
257
+ /**
258
+ * The faces that measure and paint text. Omitted, the packaged substitute faces load. A new
259
+ * value replaces the faces in place (`setFonts`); a list is compared by its faces.
260
+ */
261
+ fonts: { type: [Array, Function], default: void 0 },
262
+ /**
263
+ * The number locale of the shown values: separators and built-in format codes. A value that
264
+ * differs (`sameNumberLocale`) applies in place (`setNumberLocale`).
265
+ */
266
+ numberLocale: { type: Object, default: void 0 },
267
+ /** `'edit'` (default) or `'view'`. A change applies without a reload. */
268
+ mode: { type: String, default: void 0 },
269
+ /** UI strings; English when omitted. A `null` entry falls back to English. */
270
+ locale: { type: Object, default: void 0 },
271
+ /** The chrome color theme: `'light'` (default), `'dark'`, or `'system'`. */
272
+ colorMode: { type: String, default: void 0 }
273
+ };
274
+ var xlsxEditorRootEmits = {
275
+ /** Once per editor, after the first open of `document` settles. */
276
+ ready: (_editor) => true,
277
+ /**
278
+ * After every change of the editor state, with the new snapshot: a content change and a view
279
+ * change alike. The `change` event of `useEditorEvent` fires for content changes only.
280
+ */
281
+ change: (_snapshot) => true,
282
+ /** When the fonts that measure and paint text do not load. */
283
+ fontError: (_failure) => true
284
+ };
285
+ var XlsxEditorRoot = defineComponent({
286
+ name: "XlsxEditorRoot",
287
+ props: xlsxEditorRootProps,
288
+ emits: xlsxEditorRootEmits,
289
+ setup(props, { emit, expose, slots }) {
290
+ const self = getCurrentInstance();
291
+ const reportTo = () => hasListener(self?.vnode.props ?? null, "fontError") ? (f) => emit("fontError", f) : void 0;
292
+ const mode = () => props.mode ?? "edit";
293
+ const editor = shallowRef3(null);
294
+ provideXlsxEditor(editor);
295
+ provide3(rootModeKey, mode);
296
+ const modeWaits = shallowRef3(false);
297
+ provide3(modeWaitsKey, modeWaits);
298
+ const parts = { grids: 0, statusMessages: 0 };
299
+ provide3(layoutPartsKey, parts);
300
+ provideChromeDark(() => props.colorMode);
301
+ const t = shallowRef3(translatorFor(props.locale));
302
+ provide3(xlsxTranslationKey, t);
303
+ watch3(
304
+ () => props.locale ? JSON.stringify(props.locale) : "",
305
+ () => {
306
+ t.value = translatorFor(props.locale);
307
+ if (editor.value) relabelEditor(editor.value);
308
+ }
309
+ );
310
+ expose({
311
+ load: async (bytes) => editor.value ? editor.value.load(bytes) : null,
312
+ save: () => editor.value?.save() ?? null,
313
+ saveInTasks: async () => editor.value ? editor.value.saveInTasks() : null,
314
+ getEditor: () => editor.value,
315
+ focus: () => editor.value?.focus(),
316
+ snapshot: () => editor.value?.snapshot() ?? emptySnapshotOf2(mode())
317
+ });
318
+ let created = {
319
+ fonts: void 0,
320
+ numberLocale: void 0
321
+ };
322
+ let opened = false;
323
+ let modeProp = null;
324
+ const stops = [];
325
+ onMounted3(() => {
326
+ const fonts = props.fonts;
327
+ const numberLocale = props.numberLocale;
328
+ const instance = createXlsxEditor({
329
+ // Omitted, the engine loads the packaged substitute faces and keeps their failures.
330
+ ...fonts ? { fonts } : {},
331
+ mode: mode(),
332
+ ...numberLocale ? { numberLocale } : {},
333
+ // The engine's keys are keys of the shared catalog.
334
+ translate: (key, params) => t.value(key, params ? { ...params } : void 0)
335
+ });
336
+ created = { fonts, numberLocale };
337
+ stops.push(watchFontErrors(instance, (f) => reportFontError(f, reportTo())));
338
+ stops.push(instance.subscribe(() => emit("change", instance.snapshot())));
339
+ editor.value = instance;
340
+ modeProp = followModeProp(instance, (waits) => modeWaits.value = waits);
341
+ modeProp.apply(mode());
342
+ if (props.document) void instance.load(props.document);
343
+ opened = props.document !== void 0;
344
+ stops.push(whenOpenSettles(instance, () => emit("ready", instance)));
345
+ if (parts.grids > 0 && parts.statusMessages === 0 && isDevelopment())
346
+ console.warn(
347
+ "XlsxEditorRoot: the layout has no StatusMessage, so edit refusals and the stale-value notice are not shown. Add <StatusMessage /> to the layout."
348
+ );
349
+ });
350
+ onUnmounted(() => {
351
+ for (const stop of stops.splice(0)) stop();
352
+ modeProp?.dispose();
353
+ modeProp = null;
354
+ const instance = editor.value;
355
+ editor.value = null;
356
+ instance?.destroy();
357
+ });
358
+ watch3(
359
+ () => props.document,
360
+ (bytes) => {
361
+ const instance = editor.value;
362
+ if (!instance) return;
363
+ if (bytes) void instance.load(bytes);
364
+ else if (opened) instance.close();
365
+ opened = bytes !== void 0;
366
+ }
367
+ );
368
+ watch3(mode, (next) => modeProp?.apply(next));
369
+ watch3(
370
+ () => props.fonts,
371
+ (fonts) => {
372
+ const instance = editor.value;
373
+ if (!instance || sameFontSource(fonts, created.fonts)) return;
374
+ created = { ...created, fonts };
375
+ void instance.setFonts(fonts).then((failure) => {
376
+ if (failure) reportFontError(failure, reportTo());
377
+ });
378
+ }
379
+ );
380
+ watch3(
381
+ () => props.numberLocale,
382
+ (numberLocale) => {
383
+ const instance = editor.value;
384
+ if (!instance || sameNumberLocale(numberLocale, created.numberLocale)) return;
385
+ created = { ...created, numberLocale };
386
+ instance.setNumberLocale(numberLocale);
387
+ }
388
+ );
389
+ return () => slots.default?.();
390
+ }
391
+ });
392
+ var xlsxEditorViewportProps = {
393
+ /**
394
+ * Appended after the `xlsx-editor` and `xlsx-editor-viewport` classes. A host `class` also
395
+ * works.
396
+ */
397
+ className: { type: String, default: void 0 },
398
+ /**
399
+ * The name of the editor's landmark region. Default: "Workbook editor". `false` renders no
400
+ * region, for a host whose own landmarks are inside.
401
+ */
402
+ label: { type: [String, Boolean], default: void 0 },
403
+ /** Inline style, for example the editor's height. A host `style` also works. */
404
+ style: { type: [String, Object, Array], default: void 0 }
405
+ };
406
+ var XlsxEditorViewport = defineComponent({
407
+ name: "XlsxEditorViewport",
408
+ props: xlsxEditorViewportProps,
409
+ setup(props, { slots }) {
410
+ const { t } = useTranslation();
411
+ const editor = useXlsxEditor();
412
+ const dark = useChromeDark();
413
+ provideScopedByAncestor();
414
+ return () => h(
415
+ "div",
416
+ {
417
+ class: ["xlsx-editor", dark.value ? "dark" : "", "xlsx-editor-viewport", props.className].filter(Boolean).join(" "),
418
+ style: props.style,
419
+ ...props.label === false ? {} : { role: "region", "aria-label": props.label ?? t("viewport.ariaLabel") },
420
+ // F6 and Shift+F6 move the keyboard among the toolbar, the formula bar, the grid, and
421
+ // the footer.
422
+ onKeydown: (e) => void cycleRegion(e, e.currentTarget, editor.value)
423
+ },
424
+ slots.default?.()
425
+ );
426
+ }
427
+ });
428
+ var xlsxEditorContentProps = {
429
+ /** Appended after the `xlsx-canvas` class. A host `class` also works. */
430
+ className: { type: String, default: void 0 }
431
+ };
432
+ var XlsxEditorContent = defineComponent({
433
+ name: "XlsxEditorContent",
434
+ props: xlsxEditorContentProps,
435
+ setup(props) {
436
+ const editor = useXlsxEditor();
437
+ if (inject3(layoutPartsKey, null)) useLayoutPart("grids");
438
+ const host = shallowRef3(null);
439
+ let attached = null;
440
+ let stopSetup = null;
441
+ const attach = (next) => {
442
+ if (attached === next) return;
443
+ attached?.detach();
444
+ stopSetup?.();
445
+ stopSetup = null;
446
+ attached = null;
447
+ const el = host.value;
448
+ if (!next || !el) return;
449
+ next.attach(el);
450
+ attached = next;
451
+ if (isDevelopment())
452
+ stopSetup = watchGridSetup(el, "XlsxEditorContent", "@xlsx-editor.dev/vue/styles.css");
453
+ };
454
+ onMounted3(() => attach(editor.value));
455
+ watch3(editor, (next) => attach(next), { flush: "sync" });
456
+ onBeforeUnmount(() => attach(null));
457
+ return () => h("div", {
458
+ ref: host,
459
+ class: `xlsx-canvas${props.className ? ` ${props.className}` : ""}`
460
+ });
461
+ }
462
+ });
463
+
464
+ // src/chrome.tsx
465
+ import { defineComponent as defineComponent2 } from "vue";
466
+ import {
467
+ MAX_ZOOM,
468
+ MIN_ZOOM,
469
+ keepGridFocus,
470
+ sameSlice,
471
+ statusSlice,
472
+ statusView,
473
+ zoomSlice
474
+ } from "@xlsx-editor.dev/core/editor";
475
+
476
+ // src/icons.tsx
477
+ import { jsx } from "vue/jsx-runtime";
478
+ var ADD = "M440-440H200v-80h240v-240h80v240h240v80H520v240h-80v-240Z";
479
+ var REMOVE = "M200-440v-80h560v80H200Z";
480
+ function Icon(props) {
481
+ return /* @__PURE__ */ jsx("svg", { viewBox: "0 -960 960 960", "aria-hidden": "true", focusable: "false", children: /* @__PURE__ */ jsx("path", { d: props.d }) });
482
+ }
483
+ var ZoomInIcon = () => /* @__PURE__ */ jsx(Icon, { d: ADD });
484
+ var ZoomOutIcon = () => /* @__PURE__ */ jsx(Icon, { d: REMOVE });
485
+ var AddIcon = () => /* @__PURE__ */ jsx(Icon, { d: ADD });
486
+
487
+ // src/chrome.tsx
488
+ import { jsx as jsx2, jsxs } from "vue/jsx-runtime";
489
+ var ZoomControl = defineComponent2({
490
+ name: "ZoomControl",
491
+ setup() {
492
+ const { t } = useTranslation();
493
+ const editor = useXlsxEditor();
494
+ const snap = useEditorState(zoomSlice, sameSlice);
495
+ return () => {
496
+ const { status, zoom } = snap.value;
497
+ if (status !== "ready") return null;
498
+ const level = `${Math.round(zoom * 100)}%`;
499
+ const atMin = zoom <= MIN_ZOOM;
500
+ const atMax = zoom >= MAX_ZOOM;
501
+ return /* @__PURE__ */ jsxs(
502
+ "div",
503
+ {
504
+ class: "xlsx-zoom",
505
+ role: "group",
506
+ "aria-label": t("zoom.zoomLevel"),
507
+ onMousedown: keepGridFocus,
508
+ children: [
509
+ /* @__PURE__ */ jsx2(
510
+ "button",
511
+ {
512
+ type: "button",
513
+ "aria-label": t("zoom.zoomOut"),
514
+ title: t("zoom.zoomOut"),
515
+ "aria-disabled": atMin,
516
+ onClick: () => {
517
+ if (!atMin) editor.value?.zoomStep(-1);
518
+ },
519
+ children: /* @__PURE__ */ jsx2(ZoomOutIcon, {})
520
+ }
521
+ ),
522
+ /* @__PURE__ */ jsx2(
523
+ "button",
524
+ {
525
+ type: "button",
526
+ class: "xlsx-zoom-level",
527
+ "aria-label": t("zoom.resetAt", { level }),
528
+ title: t("zoom.reset"),
529
+ onClick: () => editor.value?.setZoom(1),
530
+ children: level
531
+ }
532
+ ),
533
+ /* @__PURE__ */ jsx2(
534
+ "button",
535
+ {
536
+ type: "button",
537
+ "aria-label": t("zoom.zoomIn"),
538
+ title: t("zoom.zoomIn"),
539
+ "aria-disabled": atMax,
540
+ onClick: () => {
541
+ if (!atMax) editor.value?.zoomStep(1);
542
+ },
543
+ children: /* @__PURE__ */ jsx2(ZoomInIcon, {})
544
+ }
545
+ )
546
+ ]
547
+ }
548
+ );
549
+ };
550
+ }
551
+ });
552
+ function Notice(props) {
553
+ const { on, short, text: text2, brief } = props;
554
+ return /* @__PURE__ */ jsx2(
555
+ "div",
556
+ {
557
+ class: "xlsx-notice",
558
+ role: "status",
559
+ ...on ? { title: text2 } : {},
560
+ ...on && short ? { "data-short": "" } : {},
561
+ children: on ? [
562
+ /* @__PURE__ */ jsx2("span", { class: "xlsx-notice-full", children: text2 }),
563
+ /* @__PURE__ */ jsx2("span", { class: "xlsx-notice-brief", "aria-hidden": "true", children: brief })
564
+ ] : null
565
+ }
566
+ );
567
+ }
568
+ var StatusMessage = defineComponent2({
569
+ name: "StatusMessage",
570
+ setup() {
571
+ const { t } = useTranslation();
572
+ const snap = useEditorState(statusSlice, sameSlice);
573
+ const modeWaits = useViewModeWaits();
574
+ useLayoutPart("statusMessages");
575
+ return () => {
576
+ const view = statusView(snap.value, modeWaits.value, t);
577
+ if (view.kind === "strip")
578
+ return /* @__PURE__ */ jsxs("div", { class: "xlsx-status-strip", children: [
579
+ view.refusal ? /* @__PURE__ */ jsx2("div", { class: "xlsx-refusal", children: view.refusal }) : null,
580
+ view.notices.map((n) => /* @__PURE__ */ jsx2(Notice, { on: n.on, short: n.short, text: n.text, brief: n.brief }, n.id))
581
+ ] });
582
+ return /* @__PURE__ */ jsxs("div", { class: "xlsx-message", children: [
583
+ /* @__PURE__ */ jsx2("div", { role: view.role, children: view.text }),
584
+ view.refusals.map((text2) => /* @__PURE__ */ jsx2("div", { class: "xlsx-message-refusal", children: text2 }, text2))
585
+ ] });
586
+ };
587
+ }
588
+ });
589
+
590
+ // src/FormulaBar.tsx
591
+ import {
592
+ defineComponent as defineComponent3,
593
+ onBeforeUnmount as onBeforeUnmount2,
594
+ onMounted as onMounted4,
595
+ onUpdated,
596
+ shallowRef as shallowRef4,
597
+ useId,
598
+ watch as watch4
599
+ } from "vue";
600
+ import {
601
+ MAX_GO_TO_LENGTH,
602
+ composesKey,
603
+ contentFieldText,
604
+ editMoveOf,
605
+ formulaSlice,
606
+ insertFieldText,
607
+ keepGridFocus as keepGridFocus2,
608
+ pressFieldBox,
609
+ refusalText,
610
+ refusesEditText,
611
+ refusesReference,
612
+ sameSlice as sameSlice2,
613
+ showCaretLine,
614
+ stepFieldHistory
615
+ } from "@xlsx-editor.dev/core/editor";
616
+ import { jsx as jsx3, jsxs as jsxs2 } from "vue/jsx-runtime";
617
+ var refusalOf = (s) => ({ refusal: s.refusal, numberLocale: s.numberLocale });
618
+ var FormulaBar = defineComponent3({
619
+ name: "FormulaBar",
620
+ setup() {
621
+ const { t } = useTranslation();
622
+ const editor = useXlsxEditor();
623
+ const snap = useEditorState(formulaSlice, sameSlice2);
624
+ let composing = false;
625
+ let field = null;
626
+ let caret = null;
627
+ const refusedText = shallowRef4(null);
628
+ const hintId = useId();
629
+ const reasonId = useId();
630
+ const shortenedId = useId();
631
+ let seen = null;
632
+ watch4(
633
+ () => [snap.value.refusal, snap.value.editing, snap.value.text],
634
+ ([refusal, editing, text2]) => {
635
+ if (!editing) refusedText.value = null;
636
+ else if (refusal !== seen)
637
+ refusedText.value = refusal && refusesEditText(refusal.code) ? text2 : null;
638
+ seen = refusal;
639
+ },
640
+ { immediate: true }
641
+ );
642
+ const afterRender = () => {
643
+ const at = caret;
644
+ caret = null;
645
+ if (at !== null) field?.setSelectionRange(at, at);
646
+ showCaretLine(field);
647
+ };
648
+ onMounted4(afterRender);
649
+ onUpdated(afterRender);
650
+ const onSelectionChange = () => showCaretLine(field);
651
+ let doc = null;
652
+ const endOpenComposition = () => {
653
+ if (!composing) return;
654
+ composing = false;
655
+ const s = editor.value?.snapshot();
656
+ if (s?.editing) editor.value?.setEditText(s.editText ?? "", { composing: false });
657
+ };
658
+ const bindField = (el) => {
659
+ const next = el;
660
+ if (next === field) return;
661
+ field = next;
662
+ if (!next) endOpenComposition();
663
+ else if (doc !== next.ownerDocument) {
664
+ doc?.removeEventListener("selectionchange", onSelectionChange);
665
+ doc = next.ownerDocument;
666
+ doc.addEventListener("selectionchange", onSelectionChange);
667
+ }
668
+ };
669
+ onBeforeUnmount2(() => {
670
+ doc?.removeEventListener("selectionchange", onSelectionChange);
671
+ doc = null;
672
+ endOpenComposition();
673
+ });
674
+ const type = (value, composes) => editor.value?.setEditText(value, { composing: composes }).ok === true;
675
+ const nameBoxFocus = () => field?.focus();
676
+ return () => {
677
+ const s = snap.value;
678
+ if (s.status !== "ready") return null;
679
+ const { editing, refusal, text: text2 } = s;
680
+ const shown = contentFieldText(text2);
681
+ const shortened = shown.length < text2.length;
682
+ const readOnly = s.view || shortened;
683
+ const shortenedNote = `${t("formulaBar.shortened")}${s.view ? "" : ` ${t("formulaBar.shortenedEdit")}`}`;
684
+ const endComposition = (value) => {
685
+ if (!composing) return;
686
+ composing = false;
687
+ const open = editor.value?.snapshot();
688
+ if (open?.editing) type(shortened ? open.editText ?? "" : value, false);
689
+ };
690
+ const invalid = editing && refusal !== null && refusedText.value === text2;
691
+ const onKeydown = (e) => {
692
+ const ed = editor.value;
693
+ if (!ed || composesKey(e, composing)) return;
694
+ if (stepFieldHistory(e, ed, editing)) return;
695
+ const el = e.currentTarget;
696
+ const move = editMoveOf(e);
697
+ if (move) {
698
+ e.preventDefault();
699
+ ed.commitEdit(move);
700
+ } else if (e.key === "Enter" && e.altKey && !readOnly) {
701
+ e.preventDefault();
702
+ if (insertFieldText(el, "\n")) return;
703
+ const { value, selectionStart, selectionEnd } = el;
704
+ const next = `${value.slice(0, selectionStart)}
705
+ ${value.slice(selectionEnd)}`;
706
+ if (type(next, false)) caret = selectionStart + 1;
707
+ } else if (e.key === "Escape") {
708
+ e.preventDefault();
709
+ e.stopPropagation();
710
+ if (ed.snapshot().editing) ed.cancelEdit();
711
+ else ed.focus();
712
+ }
713
+ };
714
+ const lines = shortened ? 1 : text2.split("\n").length;
715
+ const reason = invalid && refusal ? refusalText(refusal, t, s.numberLocale) : "";
716
+ const showLine = (e) => showCaretLine(e.currentTarget);
717
+ return /* @__PURE__ */ jsxs2("div", { class: "xlsx-formula-bar", onMousedown: keepGridFocus2, children: [
718
+ /* @__PURE__ */ jsx3(NameBox, { reference: s.ref ?? "", focusContent: nameBoxFocus }),
719
+ /* @__PURE__ */ jsxs2(
720
+ "div",
721
+ {
722
+ class: "xlsx-cell-content-box",
723
+ "data-lines": lines > 1 ? "" : void 0,
724
+ onMousedown: (e) => pressFieldBox(field, e),
725
+ children: [
726
+ /* @__PURE__ */ jsx3(
727
+ "textarea",
728
+ {
729
+ ref: bindField,
730
+ class: "xlsx-cell-content",
731
+ name: "xlsx-cell-content",
732
+ rows: 1,
733
+ wrap: "off",
734
+ spellcheck: false,
735
+ autocomplete: "off",
736
+ autocapitalize: "off",
737
+ "aria-label": t("formulaBar.content"),
738
+ "aria-describedby": shortened ? shortenedId : s.view ? void 0 : invalid ? `${reasonId} ${hintId}` : hintId,
739
+ readonly: readOnly,
740
+ "aria-invalid": invalid || void 0,
741
+ value: shown,
742
+ onInput: (e) => {
743
+ const el = e.currentTarget;
744
+ const composes = composing || e.isComposing === true;
745
+ if (shortened || !type(el.value, composes)) {
746
+ const open = editor.value?.snapshot();
747
+ const now = contentFieldText(open?.editing ? open.editText ?? "" : text2);
748
+ if (el.value !== now) el.value = now;
749
+ }
750
+ },
751
+ onCompositionstart: () => {
752
+ composing = true;
753
+ },
754
+ onCompositionend: (e) => endComposition(e.currentTarget.value),
755
+ onBlur: (e) => endComposition(e.currentTarget.value),
756
+ onKeydown,
757
+ onKeyup: showLine,
758
+ onMouseup: showLine,
759
+ onFocus: showLine,
760
+ onSelect: showLine
761
+ }
762
+ ),
763
+ shortened ? /* @__PURE__ */ jsx3(
764
+ "span",
765
+ {
766
+ id: shortenedId,
767
+ class: "xlsx-cell-content-lines xlsx-cell-content-note",
768
+ title: shortenedNote,
769
+ children: shortenedNote
770
+ }
771
+ ) : lines > 1 ? /* @__PURE__ */ jsx3("span", { class: "xlsx-cell-content-lines", "aria-hidden": "true", children: t("formulaBar.lines", { count: lines }) }) : null
772
+ ]
773
+ }
774
+ ),
775
+ /* @__PURE__ */ jsx3("span", { id: hintId, hidden: true, children: t("formulaBar.contentHint") }),
776
+ /* @__PURE__ */ jsx3("span", { id: reasonId, hidden: true, children: reason })
777
+ ] });
778
+ };
779
+ }
780
+ });
781
+ var NameBox = defineComponent3({
782
+ name: "XlsxNameBox",
783
+ props: {
784
+ reference: { type: String, required: true },
785
+ /** Give the keyboard to the content field. */
786
+ focusContent: { type: Function, required: true }
787
+ },
788
+ setup(props) {
789
+ const { t } = useTranslation();
790
+ const editor = useXlsxEditor();
791
+ const shown = useEditorState(refusalOf, sameSlice2);
792
+ const draft = shallowRef4(null);
793
+ const refused = shallowRef4(false);
794
+ let composing = false;
795
+ const hintId = useId();
796
+ const reasonId = useId();
797
+ let raised = false;
798
+ const reset = () => {
799
+ draft.value = null;
800
+ refused.value = false;
801
+ raised = false;
802
+ };
803
+ const drop = (ed) => {
804
+ if (raised && refusesReference(ed.snapshot().refusal?.code ?? "loading")) ed.dismissRefusal();
805
+ reset();
806
+ };
807
+ const go = (ed, text2) => {
808
+ if (text2.trim() === "") {
809
+ drop(ed);
810
+ ed.focus();
811
+ return;
812
+ }
813
+ const r = ed.selectReference(text2);
814
+ if (r.ok) {
815
+ reset();
816
+ ed.focus();
817
+ } else if (refusesReference(r.refusal.code)) {
818
+ draft.value = text2;
819
+ refused.value = true;
820
+ raised = true;
821
+ } else {
822
+ reset();
823
+ props.focusContent();
824
+ }
825
+ };
826
+ return () => {
827
+ const { refusal, numberLocale } = shown.value;
828
+ const reason = refused.value && refusal ? refusalText(refusal, t, numberLocale) : "";
829
+ return [
830
+ /* @__PURE__ */ jsx3(
831
+ "input",
832
+ {
833
+ class: "xlsx-name-box",
834
+ name: "xlsx-name-box",
835
+ type: "text",
836
+ spellcheck: false,
837
+ autocomplete: "off",
838
+ autocapitalize: "off",
839
+ maxlength: MAX_GO_TO_LENGTH,
840
+ "aria-label": t("formulaBar.nameBox"),
841
+ "aria-describedby": refused.value ? `${reasonId} ${hintId}` : hintId,
842
+ "aria-invalid": refused.value || void 0,
843
+ value: draft.value ?? props.reference,
844
+ onFocus: (e) => e.currentTarget.select(),
845
+ onInput: (e) => {
846
+ draft.value = e.currentTarget.value;
847
+ refused.value = false;
848
+ },
849
+ onBlur: () => {
850
+ if (editor.value) drop(editor.value);
851
+ else reset();
852
+ },
853
+ onCompositionstart: () => {
854
+ composing = true;
855
+ },
856
+ onCompositionend: () => {
857
+ composing = false;
858
+ },
859
+ onKeydown: (e) => {
860
+ const ed = editor.value;
861
+ if (!ed || composesKey(e, composing)) return;
862
+ if (stepFieldHistory(e, ed, draft.value !== null || ed.snapshot().editing)) return;
863
+ if (e.key === "Enter") {
864
+ e.preventDefault();
865
+ go(ed, e.currentTarget.value);
866
+ } else if (e.key === "Escape") {
867
+ e.preventDefault();
868
+ e.stopPropagation();
869
+ drop(ed);
870
+ ed.focus();
871
+ }
872
+ }
873
+ }
874
+ ),
875
+ /* @__PURE__ */ jsx3("span", { id: hintId, hidden: true, children: t("formulaBar.nameBoxHint") }),
876
+ /* @__PURE__ */ jsx3("span", { id: reasonId, hidden: true, children: reason })
877
+ ];
878
+ };
879
+ }
880
+ });
881
+
882
+ // src/SheetTabs.tsx
883
+ import {
884
+ computed as computed2,
885
+ defineComponent as defineComponent4,
886
+ onBeforeUnmount as onBeforeUnmount3,
887
+ onMounted as onMounted5,
888
+ shallowRef as shallowRef5,
889
+ useId as useId2,
890
+ watch as watch5
891
+ } from "vue";
892
+ import {
893
+ RENAME_COMMIT_EVENT,
894
+ disabledReasonText,
895
+ focusAfterRelease,
896
+ historyShortcutOf,
897
+ keepGridFocus as keepGridFocus3,
898
+ listenSheetMenuDismiss,
899
+ moveInSheetMenu,
900
+ openSheetMenu,
901
+ refusalText as refusalText2,
902
+ sameSlice as sameSlice3,
903
+ scrollIntoNearestScroller,
904
+ sheetCommandState,
905
+ sheetListKey,
906
+ sheetMenuLeft,
907
+ sheetRenameBox,
908
+ tabKey,
909
+ tabsSlice
910
+ } from "@xlsx-editor.dev/core/editor";
911
+ import { jsx as jsx4, jsxs as jsxs3 } from "vue/jsx-runtime";
912
+ var SheetTabs = defineComponent4({
913
+ name: "SheetTabs",
914
+ setup() {
915
+ const { t } = useTranslation();
916
+ const editor = useXlsxEditor();
917
+ const snap = useEditorState(tabsSlice, sameSlice3);
918
+ const bar = shallowRef5(null);
919
+ const list = shallowRef5(null);
920
+ const menuRef = shallowRef5(null);
921
+ const addRef = shallowRef5(null);
922
+ const menu = shallowRef5(null);
923
+ const renaming = shallowRef5(null);
924
+ const focusSheet = shallowRef5(null);
925
+ const reasonId = useId2();
926
+ const viewReasonId = useId2();
927
+ const renameReasonId = useId2();
928
+ const tabReasonId = useId2();
929
+ const renameField = { current: null };
930
+ let refusedPress = false;
931
+ let listFocused = false;
932
+ const renameRefused = shallowRef5(false);
933
+ let renameRefusal = null;
934
+ const stateOf = (command, sheet = -1) => sheetCommandState(snap.value, command, sheet);
935
+ watch5(
936
+ focusSheet,
937
+ (sheet) => {
938
+ if (sheet === null) return;
939
+ focusSheet.value = null;
940
+ list.value?.querySelector(`[data-sheet-index="${sheet}"]`)?.focus();
941
+ },
942
+ { flush: "post" }
943
+ );
944
+ const listKey = computed2(() => sheetListKey(snap.value.sheets));
945
+ watch5(
946
+ [() => snap.value.activeSheet, listKey],
947
+ ([active]) => {
948
+ const tab = list.value?.querySelector(`[data-sheet-index="${active}"]`);
949
+ if (tab) scrollIntoNearestScroller(tab, list.value);
950
+ },
951
+ { flush: "post", immediate: true }
952
+ );
953
+ watch5(
954
+ [listKey, editor],
955
+ () => {
956
+ renameField.current?.drop();
957
+ const field = bar.value?.ownerDocument.activeElement;
958
+ if (field && bar.value?.contains(field) && field.classList.contains("xlsx-sheet-rename"))
959
+ editor.value?.focus();
960
+ renaming.value = null;
961
+ },
962
+ { flush: "post" }
963
+ );
964
+ watch5(
965
+ menu,
966
+ (open, _old, onCleanup) => {
967
+ const el = bar.value;
968
+ if (!open || !el) return;
969
+ onCleanup(
970
+ listenSheetMenuDismiss(
971
+ el,
972
+ (node) => menuRef.value?.contains(node) ?? false,
973
+ () => menu.value = null
974
+ )
975
+ );
976
+ },
977
+ { flush: "post" }
978
+ );
979
+ watch5(
980
+ [menu, editor],
981
+ ([open, ed], _old, onCleanup) => {
982
+ const el = menuRef.value;
983
+ if (!open || !el) return;
984
+ onCleanup(openSheetMenu(el, bar.value, open.left, open, ed));
985
+ },
986
+ { flush: "post" }
987
+ );
988
+ const choose = (index) => editor.value?.setActiveSheet(index) ?? false;
989
+ const openMenu = (sheet, tab, clientX) => {
990
+ const had = tab.ownerDocument.activeElement;
991
+ const inList = !!had && !!list.value?.contains(had);
992
+ const back = clientX === void 0 || inList ? tab : had;
993
+ if (!choose(sheet)) return;
994
+ menu.value = {
995
+ sheet,
996
+ left: sheetMenuLeft(bar.value, tab, clientX),
997
+ back,
998
+ keyboard: clientX === void 0
999
+ };
1000
+ };
1001
+ const renameBox = (name) => sheetRenameBox(
1002
+ { bar: bar.value, list: list.value, add: addRef.value },
1003
+ snap.value.sheets,
1004
+ name
1005
+ );
1006
+ const commitRename = (press) => {
1007
+ const open = renameField.current;
1008
+ if (!renaming.value || !open) return true;
1009
+ const written = open.commit();
1010
+ if (!press || !written) editor.value?.focus();
1011
+ else focusAfterRelease(press, () => editor.value?.focus());
1012
+ return written;
1013
+ };
1014
+ const startRename = (sheet) => {
1015
+ if (!stateOf("rename", sheet).enabled) return;
1016
+ const ed = editor.value;
1017
+ if (ed && !ed.commitEdit().ok) return;
1018
+ const shown = ed?.snapshot().refusal;
1019
+ if (shown && shown === renameRefusal) ed?.dismissRefusal();
1020
+ renameRefusal = null;
1021
+ const name = snap.value.sheets[sheet]?.name;
1022
+ renameRefused.value = false;
1023
+ if (name !== void 0) renaming.value = renameBox(name);
1024
+ };
1025
+ const stepHistory = (history) => {
1026
+ const ed = editor.value;
1027
+ if (history === "undo") ed?.undo();
1028
+ else ed?.redo();
1029
+ const shown = ed?.snapshot().activeSheet;
1030
+ if (shown !== void 0 && shown >= 0) focusSheet.value = shown;
1031
+ };
1032
+ const onTabKeydown = (e, sheet, tabs) => {
1033
+ const history = historyShortcutOf(e);
1034
+ if (history) {
1035
+ e.preventDefault();
1036
+ stepHistory(history);
1037
+ return;
1038
+ }
1039
+ if (e.key === "F10" && e.shiftKey || e.key === "ContextMenu") {
1040
+ e.preventDefault();
1041
+ openMenu(sheet, e.currentTarget);
1042
+ return;
1043
+ }
1044
+ if (e.key === "F2") {
1045
+ e.preventDefault();
1046
+ startRename(sheet);
1047
+ return;
1048
+ }
1049
+ const sheets = tabs.filter((s) => s.worksheet);
1050
+ const position = sheets.findIndex((s) => s.index === sheet);
1051
+ const step = e.key === "ArrowRight" ? 1 : e.key === "ArrowLeft" ? -1 : 0;
1052
+ const next = e.key === "Home" ? 0 : e.key === "End" ? sheets.length - 1 : step ? position + step : -1;
1053
+ if (position < 0 || next < 0 || next >= sheets.length) return;
1054
+ e.preventDefault();
1055
+ const target = sheets[next];
1056
+ if (!choose(target.index)) return;
1057
+ list.value?.querySelector(`[data-sheet-index="${target.index}"]`)?.focus();
1058
+ };
1059
+ const onMenuKeydown = (e) => {
1060
+ const history = historyShortcutOf(e);
1061
+ if (history) {
1062
+ e.preventDefault();
1063
+ e.stopPropagation();
1064
+ menu.value = null;
1065
+ stepHistory(history);
1066
+ return;
1067
+ }
1068
+ if (e.key === "Escape" || e.key === "Tab") {
1069
+ e.preventDefault();
1070
+ e.stopPropagation();
1071
+ menu.value = null;
1072
+ return;
1073
+ }
1074
+ if (menuRef.value && moveInSheetMenu(menuRef.value, e.key)) {
1075
+ e.preventDefault();
1076
+ e.stopPropagation();
1077
+ }
1078
+ };
1079
+ const item = (command, label, state, run) => {
1080
+ const disabled = !state.enabled;
1081
+ const reason = disabled && state.disabledReason ? disabledReasonText(state.disabledReason, t) : null;
1082
+ const id = `${reasonId}-${command}`;
1083
+ return [
1084
+ /* @__PURE__ */ jsx4(
1085
+ "button",
1086
+ {
1087
+ type: "button",
1088
+ role: "menuitem",
1089
+ class: "xlsx-sheet-menu-item",
1090
+ tabindex: -1,
1091
+ "aria-disabled": disabled || void 0,
1092
+ ...reason ? { title: t("toolbar.disabledTitle", { label, reason }), "aria-describedby": id } : {},
1093
+ onMousedown: keepGridFocus3,
1094
+ onClick: () => {
1095
+ if (!disabled) run();
1096
+ },
1097
+ children: label
1098
+ },
1099
+ command
1100
+ ),
1101
+ reason ? /* @__PURE__ */ jsx4("span", { id, hidden: true, children: reason }, id) : null
1102
+ ];
1103
+ };
1104
+ return () => {
1105
+ const s = snap.value;
1106
+ const tabs = s.sheets.filter((sheet2) => sheet2.visible);
1107
+ if (s.status !== "ready" || tabs.length === 0) return null;
1108
+ const addState = stateOf("add");
1109
+ const sheetLock = addState.disabledReason ? disabledReasonText(addState.disabledReason, t) : null;
1110
+ const blocked = !addState.enabled;
1111
+ const open = menu.value;
1112
+ const sheet = open?.sheet ?? -1;
1113
+ const renameState = stateOf("rename", sheet);
1114
+ const deleteState = stateOf("delete", sheet);
1115
+ const leftState = stateOf("moveLeft", sheet);
1116
+ const rightState = stateOf("moveRight", sheet);
1117
+ const keyboardMenu = open?.keyboard ?? false;
1118
+ const moveTo = (to) => {
1119
+ menu.value = null;
1120
+ const r = editor.value?.moveSheet(sheet, to);
1121
+ if (keyboardMenu) focusSheet.value = r?.ok ? to : sheet;
1122
+ };
1123
+ const rename = renaming.value;
1124
+ const renameReason = rename && renameRefused.value && s.refusal ? refusalText2(s.refusal, t, s.numberLocale) : null;
1125
+ const viewReason = sheetLock ?? "";
1126
+ const notWorksheet = disabledReasonText({ key: "disabledReason.notWorksheet" }, t);
1127
+ const tabReasons = /* @__PURE__ */ new Map();
1128
+ const tabReason = (tab) => {
1129
+ const state = stateOf("rename", tab.index);
1130
+ const text2 = !tab.worksheet ? notWorksheet : !state.enabled && state.disabledReason ? disabledReasonText(state.disabledReason, t) : null;
1131
+ if (text2 === null) return null;
1132
+ let id = tabReasons.get(text2);
1133
+ if (id === void 0) tabReasons.set(text2, id = `${tabReasonId}-${tabReasons.size}`);
1134
+ return { text: text2, id };
1135
+ };
1136
+ const reasonOfTab = tabs.map(tabReason);
1137
+ const addLabel = t("sheetTabs.add");
1138
+ return /* @__PURE__ */ jsxs3("div", { class: "xlsx-sheet-bar", ref: bar, onMousedown: keepGridFocus3, children: [
1139
+ /* @__PURE__ */ jsx4(
1140
+ "div",
1141
+ {
1142
+ class: "xlsx-sheet-tabs",
1143
+ role: "tablist",
1144
+ "aria-label": t("sheetTabs.ariaLabel"),
1145
+ ref: list,
1146
+ onScroll: () => {
1147
+ if (renaming.value) renaming.value = renameBox(renaming.value.name);
1148
+ },
1149
+ children: tabs.map((tab, i) => /* @__PURE__ */ jsx4(
1150
+ "button",
1151
+ {
1152
+ type: "button",
1153
+ role: "tab",
1154
+ class: `xlsx-sheet-tab${rename?.name === tab.name ? " xlsx-sheet-tab-renaming" : ""}`,
1155
+ "data-sheet-index": tab.index,
1156
+ "aria-selected": tab.index === s.activeSheet,
1157
+ ...tab.worksheet ? { "aria-haspopup": "menu", "aria-expanded": open?.sheet === tab.index } : { "aria-disabled": true },
1158
+ tabindex: tab.worksheet && tab.index === s.activeSheet ? 0 : -1,
1159
+ "aria-describedby": reasonOfTab[i]?.id,
1160
+ title: reasonOfTab[i] ? t("toolbar.disabledTitle", { label: tab.name, reason: reasonOfTab[i].text }) : tab.name,
1161
+ onMousedown: (e) => {
1162
+ keepGridFocus3(e);
1163
+ const doc = e.currentTarget.ownerDocument;
1164
+ listFocused = list.value?.contains(doc.activeElement) ?? false;
1165
+ if (e.detail <= 1) refusedPress = !commitRename(doc);
1166
+ },
1167
+ onClick: (e) => {
1168
+ if (refusedPress || !tab.worksheet) return;
1169
+ choose(tab.index);
1170
+ if (listFocused) e.currentTarget.focus();
1171
+ else if (!renaming.value) editor.value?.focus();
1172
+ },
1173
+ onDblclick: () => {
1174
+ if (!refusedPress && tab.worksheet) startRename(tab.index);
1175
+ },
1176
+ onContextmenu: (e) => {
1177
+ e.preventDefault();
1178
+ if (refusedPress || !tab.worksheet) return;
1179
+ openMenu(tab.index, e.currentTarget, e.clientX);
1180
+ },
1181
+ onKeydown: (e) => {
1182
+ if (tab.worksheet) onTabKeydown(e, tab.index, tabs);
1183
+ },
1184
+ children: tab.name
1185
+ },
1186
+ tabKey(tabs, i)
1187
+ ))
1188
+ }
1189
+ ),
1190
+ rename ? /* @__PURE__ */ jsx4(
1191
+ RenameField,
1192
+ {
1193
+ name: rename.name,
1194
+ label: t("sheetTabs.renameInput"),
1195
+ box: rename,
1196
+ reasonId: renameReason ? renameReasonId : void 0,
1197
+ handle: renameField,
1198
+ focusGrid: () => editor.value?.focus(),
1199
+ onDone: (name, keyboard) => {
1200
+ const ed = editor.value;
1201
+ const target = ed?.snapshot().sheets.find((x) => x.name === rename.name);
1202
+ let written = true;
1203
+ if (name !== null && target) {
1204
+ const r = ed?.renameSheet(target.index, name);
1205
+ written = r?.ok ?? false;
1206
+ renameRefused.value = !written;
1207
+ renameRefusal = written ? null : ed?.snapshot().refusal ?? null;
1208
+ if (!written && keyboard) return false;
1209
+ }
1210
+ renaming.value = null;
1211
+ if (keyboard && target) focusSheet.value = target.index;
1212
+ return written;
1213
+ },
1214
+ onCancel: () => {
1215
+ if (renameRefused.value) editor.value?.dismissRefusal();
1216
+ renameRefused.value = false;
1217
+ }
1218
+ },
1219
+ rename.name
1220
+ ) : null,
1221
+ blocked ? /* @__PURE__ */ jsx4("span", { id: viewReasonId, hidden: true, children: viewReason }) : null,
1222
+ [...tabReasons].map(([text2, id]) => /* @__PURE__ */ jsx4("span", { id, hidden: true, children: text2 }, id)),
1223
+ renameReason ? /* @__PURE__ */ jsx4("span", { id: renameReasonId, hidden: true, children: renameReason }) : null,
1224
+ /* @__PURE__ */ jsx4(
1225
+ "button",
1226
+ {
1227
+ ref: addRef,
1228
+ type: "button",
1229
+ class: "xlsx-sheet-add",
1230
+ "aria-label": addLabel,
1231
+ title: blocked ? t("toolbar.disabledTitle", { label: addLabel, reason: viewReason }) : addLabel,
1232
+ "aria-disabled": blocked || void 0,
1233
+ "aria-describedby": blocked ? viewReasonId : void 0,
1234
+ onMousedown: keepGridFocus3,
1235
+ onKeydown: (e) => {
1236
+ const history = historyShortcutOf(e);
1237
+ if (!history) return;
1238
+ e.preventDefault();
1239
+ if (history === "undo") editor.value?.undo();
1240
+ else editor.value?.redo();
1241
+ },
1242
+ onClick: (e) => {
1243
+ if (blocked) return;
1244
+ const doc = e.currentTarget.ownerDocument;
1245
+ const fromList = list.value?.contains(doc.activeElement) ?? false;
1246
+ const ed = editor.value;
1247
+ if (commitRename() && ed?.addSheet().ok && fromList)
1248
+ focusSheet.value = ed.snapshot().activeSheet;
1249
+ },
1250
+ children: /* @__PURE__ */ jsx4(AddIcon, {})
1251
+ }
1252
+ ),
1253
+ open ? /* @__PURE__ */ jsxs3(
1254
+ "div",
1255
+ {
1256
+ ref: menuRef,
1257
+ class: "xlsx-sheet-menu",
1258
+ role: "menu",
1259
+ "aria-label": t("sheetTabs.menu"),
1260
+ style: { left: `${open.left}px` },
1261
+ onMousedown: keepGridFocus3,
1262
+ onContextmenu: (e) => e.preventDefault(),
1263
+ onKeydown: onMenuKeydown,
1264
+ children: [
1265
+ item("rename", t("sheetTabs.rename"), renameState, () => {
1266
+ menu.value = null;
1267
+ startRename(sheet);
1268
+ }),
1269
+ item("delete", t("sheetTabs.delete"), deleteState, () => {
1270
+ menu.value = null;
1271
+ const ed = editor.value;
1272
+ ed?.deleteSheet(sheet);
1273
+ const shown = ed?.snapshot().activeSheet;
1274
+ if (keyboardMenu && shown !== void 0 && shown >= 0) focusSheet.value = shown;
1275
+ }),
1276
+ item("moveLeft", t("sheetTabs.moveLeft"), leftState, () => moveTo(leftState.to)),
1277
+ item(
1278
+ "moveRight",
1279
+ t("sheetTabs.moveRight"),
1280
+ rightState,
1281
+ () => moveTo(rightState.to)
1282
+ )
1283
+ ]
1284
+ }
1285
+ ) : null
1286
+ ] });
1287
+ };
1288
+ }
1289
+ });
1290
+ var RenameField = defineComponent4({
1291
+ name: "XlsxSheetRename",
1292
+ props: {
1293
+ name: { type: String, required: true },
1294
+ label: { type: String, required: true },
1295
+ box: {
1296
+ type: Object,
1297
+ required: true
1298
+ },
1299
+ /** The id of the reason of a refused name, while the field stays open after it. */
1300
+ reasonId: { type: String, default: void 0 },
1301
+ handle: { type: Object, required: true },
1302
+ focusGrid: { type: Function, required: true },
1303
+ onDone: {
1304
+ type: Function,
1305
+ required: true
1306
+ },
1307
+ onCancel: { type: Function, required: true }
1308
+ },
1309
+ setup(props) {
1310
+ const value = shallowRef5(props.name);
1311
+ const field = shallowRef5(null);
1312
+ let ended = false;
1313
+ const finish = (write, keyboard) => {
1314
+ if (ended) return true;
1315
+ ended = true;
1316
+ const next = value.value.trim();
1317
+ const written = props.onDone(
1318
+ write && next !== "" && next !== props.name ? next : null,
1319
+ keyboard
1320
+ );
1321
+ if (!written && keyboard) {
1322
+ ended = false;
1323
+ field.value?.focus();
1324
+ }
1325
+ return written;
1326
+ };
1327
+ const onCommit = (e) => {
1328
+ e.detail.written = finish(true, false);
1329
+ props.focusGrid();
1330
+ };
1331
+ onMounted5(() => {
1332
+ field.value?.focus();
1333
+ field.value?.select();
1334
+ props.handle.current = {
1335
+ commit: () => finish(true, false),
1336
+ drop: () => {
1337
+ ended = true;
1338
+ }
1339
+ };
1340
+ field.value?.addEventListener(RENAME_COMMIT_EVENT, onCommit);
1341
+ });
1342
+ onBeforeUnmount3(() => {
1343
+ props.handle.current = null;
1344
+ field.value?.removeEventListener(RENAME_COMMIT_EVENT, onCommit);
1345
+ });
1346
+ return () => {
1347
+ const { box } = props;
1348
+ return /* @__PURE__ */ jsx4(
1349
+ "input",
1350
+ {
1351
+ ref: field,
1352
+ class: "xlsx-sheet-rename",
1353
+ "aria-label": props.label,
1354
+ "aria-invalid": props.reasonId ? true : void 0,
1355
+ "aria-describedby": props.reasonId,
1356
+ value: value.value,
1357
+ style: {
1358
+ left: `${box.left}px`,
1359
+ minWidth: `${box.width}px`,
1360
+ ...box.maxWidth === void 0 ? {} : { maxWidth: `${box.maxWidth}px` }
1361
+ },
1362
+ size: Math.max(4, Math.min(value.value.length + 1, 31)),
1363
+ onInput: (e) => value.value = e.target.value,
1364
+ onBlur: () => finish(true, false),
1365
+ onKeydown: (e) => {
1366
+ e.stopPropagation();
1367
+ if (e.isComposing || e.keyCode === 229) return;
1368
+ if (e.key === "Enter") {
1369
+ e.preventDefault();
1370
+ finish(true, true);
1371
+ } else if (e.key === "Escape") {
1372
+ e.preventDefault();
1373
+ finish(false, true);
1374
+ props.onCancel();
1375
+ }
1376
+ }
1377
+ }
1378
+ );
1379
+ };
1380
+ }
1381
+ });
1382
+
1383
+ // src/toolbar/Toolbar.tsx
1384
+ import {
1385
+ Fragment as Fragment3,
1386
+ computed as computed7,
1387
+ defineComponent as defineComponent9,
1388
+ h as h2,
1389
+ provide as provide5,
1390
+ shallowRef as shallowRef10,
1391
+ watch as watch9
1392
+ } from "vue";
1393
+ import {
1394
+ TOOLBAR_FIXED_ATTRIBUTE,
1395
+ arrangeToolbar,
1396
+ chromeSlotId as chromeSlotId2,
1397
+ guardToolbarMousedown as guardToolbarMousedown2,
1398
+ listenSaveKey,
1399
+ measureToolbar,
1400
+ moveBarFocus,
1401
+ observeToolbar,
1402
+ runToolbarCommand as runToolbarCommand2,
1403
+ toolbarGroupHeading as toolbarGroupHeading2
1404
+ } from "@xlsx-editor.dev/core/editor";
1405
+
1406
+ // src/toolbar/slot.tsx
1407
+ import { computed as computed4 } from "vue";
1408
+ import {
1409
+ chromeControlForSlot,
1410
+ slotWords
1411
+ } from "@xlsx-editor.dev/core/editor";
1412
+
1413
+ // src/useEditorCommand.ts
1414
+ import {
1415
+ computed as computed3,
1416
+ inject as inject4,
1417
+ shallowRef as shallowRef6,
1418
+ toValue
1419
+ } from "vue";
1420
+ import {
1421
+ editorCommandSlice,
1422
+ runToolbarCommand,
1423
+ sameEditorCommandSlice
1424
+ } from "@xlsx-editor.dev/core/editor";
1425
+ var toolbarHostKey = /* @__PURE__ */ Symbol("xlsxToolbarHost");
1426
+ function translateKey(t, key, params) {
1427
+ return t(key, params ? { ...params } : void 0);
1428
+ }
1429
+ var noHost = shallowRef6({});
1430
+ function useEditorCommand(slot) {
1431
+ const editor = useXlsxEditor();
1432
+ const host = inject4(toolbarHostKey, noHost);
1433
+ const t = useTranslator();
1434
+ const slice = useEditorState(
1435
+ (snapshot) => editorCommandSlice(snapshot, toValue(slot), host.value, t.value),
1436
+ sameEditorCommandSlice,
1437
+ // The slot, the toolbar's host, and the root's translator change the slice without an
1438
+ // editor change.
1439
+ () => [toValue(slot), host.value, t.value]
1440
+ );
1441
+ return {
1442
+ execute: (value) => editor.value ? runToolbarCommand(editor.value, toValue(slot), value, host.value).ok : false,
1443
+ isActive: computed3(() => slice.value.active),
1444
+ isEnabled: computed3(() => slice.value.enabled),
1445
+ disabledReason: computed3(() => slice.value.disabledReason),
1446
+ hint: computed3(() => slice.value.hint),
1447
+ value: computed3(() => slice.value.value)
1448
+ };
1449
+ }
1450
+
1451
+ // src/toolbar/slot.tsx
1452
+ import { jsx as jsx5 } from "vue/jsx-runtime";
1453
+ function useToolbarSlot(slot) {
1454
+ const editor = useXlsxEditor();
1455
+ const { t } = useTranslation();
1456
+ const command = useEditorCommand(slot);
1457
+ const control2 = chromeControlForSlot(slot);
1458
+ return {
1459
+ ...command,
1460
+ words: computed4(
1461
+ () => slotWords(
1462
+ control2.labelKey,
1463
+ { disabledReason: command.disabledReason.value, hint: command.hint.value },
1464
+ t
1465
+ )
1466
+ ),
1467
+ slot,
1468
+ control: control2,
1469
+ focusGrid: () => editor.value?.focus()
1470
+ };
1471
+ }
1472
+ function ChromeIcon(props) {
1473
+ if (!props.paths) return null;
1474
+ return /* @__PURE__ */ jsx5("svg", { class: props.className, viewBox: "0 -960 960 960", "aria-hidden": "true", focusable: "false", children: props.paths.map((d) => /* @__PURE__ */ jsx5("path", { d }, d)) });
1475
+ }
1476
+
1477
+ // src/toolbar/parts.tsx
1478
+ import {
1479
+ computed as computed6,
1480
+ defineComponent as defineComponent7,
1481
+ shallowRef as shallowRef9,
1482
+ useId as useId4,
1483
+ watch as watch8
1484
+ } from "vue";
1485
+ import {
1486
+ COLOR_CHOICES,
1487
+ FONT_FAMILY_CHOICES,
1488
+ FONT_SIZE_CHOICES,
1489
+ NO_FILL,
1490
+ focusPopupItem,
1491
+ guardToolbarMousedown,
1492
+ isHexColor,
1493
+ movePopupFocus,
1494
+ numberFormatListView,
1495
+ parseFontSize,
1496
+ placePopup,
1497
+ popupTriggerAction
1498
+ } from "@xlsx-editor.dev/core/editor";
1499
+
1500
+ // src/toolbar/useDropdownClose.ts
1501
+ import { watch as watch6 } from "vue";
1502
+ import {
1503
+ hasOpenNestedPopup,
1504
+ listenForOutsidePress,
1505
+ listenForPopupEscape,
1506
+ listenForPopupFocusLeave
1507
+ } from "@xlsx-editor.dev/core/editor";
1508
+ function useDropdownClose(open, rootRef, hidden = () => false, nested = false) {
1509
+ watch6(
1510
+ [open, hidden],
1511
+ ([isOpen, isHidden]) => {
1512
+ if (isOpen && isHidden) open.value = false;
1513
+ },
1514
+ { immediate: true }
1515
+ );
1516
+ watch6(
1517
+ [open, hidden, rootRef],
1518
+ ([isOpen, isHidden, root], _old, onCleanup) => {
1519
+ if (!isOpen || isHidden || !root) return;
1520
+ const close = () => {
1521
+ open.value = false;
1522
+ };
1523
+ const contains = (node) => root.contains(node);
1524
+ const stopEscape = listenForPopupEscape({
1525
+ popup: root,
1526
+ contains,
1527
+ chromeRoot: () => root.closest(".xlsx-toolbar"),
1528
+ ...nested ? { skip: () => hasOpenNestedPopup(root.querySelector("[data-popup]")) } : {},
1529
+ close: (fromInside) => {
1530
+ close();
1531
+ if (fromInside) root.querySelector("[aria-haspopup]")?.focus();
1532
+ }
1533
+ });
1534
+ const stopFocus = listenForPopupFocusLeave({ popup: root, contains, close });
1535
+ const stopPress = listenForOutsidePress({ popup: root, contains, close });
1536
+ onCleanup(() => {
1537
+ stopPress();
1538
+ stopEscape();
1539
+ stopFocus();
1540
+ });
1541
+ },
1542
+ { immediate: true, flush: "post" }
1543
+ );
1544
+ }
1545
+
1546
+ // src/toolbar/NumberFormatDialog.tsx
1547
+ import {
1548
+ computed as computed5,
1549
+ defineComponent as defineComponent6,
1550
+ inject as inject5,
1551
+ onMounted as onMounted7,
1552
+ provide as provide4,
1553
+ shallowRef as shallowRef8,
1554
+ useId as useId3,
1555
+ watch as watch7
1556
+ } from "vue";
1557
+ import {
1558
+ numberFormatDialogApplyStep,
1559
+ numberFormatDialogCurrent,
1560
+ numberFormatDialogStart,
1561
+ previewNumberFormat,
1562
+ refusalText as refusalText3,
1563
+ workbookNumberFormatCodes
1564
+ } from "@xlsx-editor.dev/core/editor";
1565
+
1566
+ // src/dialog-frame.tsx
1567
+ import { defineComponent as defineComponent5, onBeforeUnmount as onBeforeUnmount4, onMounted as onMounted6, shallowRef as shallowRef7 } from "vue";
1568
+ import { isDialogBackdropPoint, trapTabWithin } from "@xlsx-editor.dev/core/editor";
1569
+ import { jsx as jsx6 } from "vue/jsx-runtime";
1570
+ var DialogFrame = defineComponent5({
1571
+ name: "XlsxDialogFrame",
1572
+ props: {
1573
+ /** The `data-xlsx-dialog` kind: the styling hook of the dialog. */
1574
+ kind: { type: String, required: true },
1575
+ /** The accessible name. */
1576
+ label: { type: String, required: true },
1577
+ /** Close the dialog: Escape, or a press outside the panel. */
1578
+ onClose: { type: Function, required: true },
1579
+ /**
1580
+ * Where focus goes when the dialog closes. Return true when it took focus; otherwise the
1581
+ * frame focuses the opener, when it is still in the page.
1582
+ */
1583
+ onRestoreFocus: { type: Function, default: void 0 },
1584
+ /** The dialog's own keys, after Escape and Tab. */
1585
+ onKeyDown: { type: Function, default: void 0 }
1586
+ },
1587
+ setup(props, { slots }) {
1588
+ const panelRef = shallowRef7(null);
1589
+ let pressedOutside = false;
1590
+ const ownScope = shallowRef7(false);
1591
+ const dark = useChromeDark();
1592
+ let opener = null;
1593
+ onMounted6(() => {
1594
+ const panel = panelRef.value;
1595
+ if (!panel) return;
1596
+ ownScope.value = !panel.parentElement?.closest(".xlsx-editor");
1597
+ const active = panel.ownerDocument.activeElement;
1598
+ opener = active === panel.ownerDocument.body ? null : active;
1599
+ if (typeof panel.showModal === "function") panel.showModal();
1600
+ else panel.setAttribute("open", "");
1601
+ panel.querySelector(
1602
+ 'input:not([disabled]),select:not([disabled]),button:not([disabled]),[tabindex="0"]'
1603
+ )?.focus({ preventScroll: true });
1604
+ });
1605
+ onBeforeUnmount4(() => {
1606
+ const panel = panelRef.value;
1607
+ if (!panel) return;
1608
+ const active = panel.ownerDocument.activeElement;
1609
+ if (active && panel.contains(active)) active.blur?.();
1610
+ if (typeof panel.close === "function" && panel.open) panel.close();
1611
+ else panel.removeAttribute("open");
1612
+ if (props.onRestoreFocus?.()) return;
1613
+ if (opener?.isConnected) opener.focus({ preventScroll: true });
1614
+ });
1615
+ return () => /* @__PURE__ */ jsx6(
1616
+ "dialog",
1617
+ {
1618
+ ref: panelRef,
1619
+ "aria-modal": "true",
1620
+ "aria-label": props.label,
1621
+ "data-xlsx-dialog": props.kind,
1622
+ class: ownScope.value ? `xlsx-dialog xlsx-editor${dark.value ? " dark" : ""}` : "xlsx-dialog",
1623
+ onCancel: (event) => {
1624
+ event.preventDefault();
1625
+ props.onClose();
1626
+ },
1627
+ onMousedown: (event) => {
1628
+ pressedOutside = isDialogBackdropPoint(event.currentTarget, event);
1629
+ event.stopPropagation();
1630
+ },
1631
+ onClick: (event) => {
1632
+ const close = pressedOutside && isDialogBackdropPoint(event.currentTarget, event);
1633
+ pressedOutside = false;
1634
+ if (close) props.onClose();
1635
+ event.stopPropagation();
1636
+ },
1637
+ onKeydown: (event) => {
1638
+ const composing = event.isComposing || event.keyCode === 229;
1639
+ if (event.key === "Escape" && !composing) {
1640
+ event.preventDefault();
1641
+ props.onClose();
1642
+ } else if (trapTabWithin(event.currentTarget, event))
1643
+ event.preventDefault();
1644
+ else if (!composing && !event.defaultPrevented) props.onKeyDown?.(event);
1645
+ event.stopPropagation();
1646
+ },
1647
+ children: slots.default?.()
1648
+ }
1649
+ );
1650
+ }
1651
+ });
1652
+
1653
+ // src/toolbar/NumberFormatDialog.tsx
1654
+ import { Fragment, jsx as jsx7, jsxs as jsxs4 } from "vue/jsx-runtime";
1655
+ var openerKey = /* @__PURE__ */ Symbol("xlsxNumberFormatOpener");
1656
+ var panelCloseKey = /* @__PURE__ */ Symbol("xlsxPanelClose");
1657
+ function useNumberFormatDialogOpener() {
1658
+ const opener = inject5(openerKey, null);
1659
+ const closePanel = inject5(panelCloseKey, null);
1660
+ if (!opener || !closePanel) return opener;
1661
+ return {
1662
+ open(returnTo) {
1663
+ const opened = opener.open(returnTo);
1664
+ if (opened && !returnTo) closePanel();
1665
+ return opened;
1666
+ }
1667
+ };
1668
+ }
1669
+ var NumberFormatDialogHost = defineComponent6({
1670
+ name: "XlsxNumberFormatDialogHost",
1671
+ setup(_props, { slots }) {
1672
+ const editor = useXlsxEditor();
1673
+ const open = shallowRef8(null);
1674
+ provide4(openerKey, {
1675
+ open: (returnTo) => {
1676
+ const start = editor.value ? numberFormatDialogStart(editor.value) : null;
1677
+ if (start) open.value = { returnTo, start };
1678
+ return start !== null;
1679
+ }
1680
+ });
1681
+ const close = () => {
1682
+ open.value = null;
1683
+ };
1684
+ return () => [
1685
+ slots.default?.(),
1686
+ open.value ? /* @__PURE__ */ jsx7(
1687
+ NumberFormatDialog,
1688
+ {
1689
+ returnTo: open.value.returnTo,
1690
+ start: open.value.start,
1691
+ onClose: close
1692
+ }
1693
+ ) : null
1694
+ ];
1695
+ }
1696
+ });
1697
+ var HEX6 = /^[0-9A-Fa-f]{6}$/;
1698
+ var NumberFormatDialog = defineComponent6({
1699
+ name: "XlsxNumberFormatDialog",
1700
+ props: {
1701
+ returnTo: { type: Object, default: null },
1702
+ start: { type: Object, required: true },
1703
+ onClose: { type: Function, required: true }
1704
+ },
1705
+ setup(props) {
1706
+ const editor = useXlsxEditor();
1707
+ const { t } = useTranslation();
1708
+ const s = useToolbarSlot("number.custom");
1709
+ const code = shallowRef8(props.start.initial);
1710
+ const error = shallowRef8(null);
1711
+ const errorCount = shallowRef8(0);
1712
+ let applied = false;
1713
+ const attempted = shallowRef8(false);
1714
+ const field = shallowRef8(null);
1715
+ const list = shallowRef8(null);
1716
+ const id = useId3();
1717
+ const ids = {
1718
+ field: `${id}-code`,
1719
+ hint: `${id}-hint`,
1720
+ preview: `${id}-preview`,
1721
+ error: `${id}-error`,
1722
+ list: `${id}-list`,
1723
+ sample: `${id}-sample`,
1724
+ blank: `${id}-blank`,
1725
+ previewLabel: `${id}-preview-label`
1726
+ };
1727
+ onMounted7(() => field.value?.select());
1728
+ const snapshot = useEditorSnapshot();
1729
+ const codes = computed5(() => {
1730
+ void snapshot.value;
1731
+ return editor.value ? workbookNumberFormatCodes(editor.value) : [];
1732
+ });
1733
+ const current = useEditorState(
1734
+ () => !!editor.value && numberFormatDialogCurrent(editor.value, props.start)
1735
+ );
1736
+ watch7(
1737
+ current,
1738
+ (now) => {
1739
+ if (!now) props.onClose();
1740
+ },
1741
+ { immediate: true, flush: "post" }
1742
+ );
1743
+ const preview = computed5(() => {
1744
+ void snapshot.value;
1745
+ return editor.value ? previewNumberFormat(editor.value, code.value) : null;
1746
+ });
1747
+ const problem = computed5(() => {
1748
+ const p = preview.value;
1749
+ return p && !p.ok ? translateKey(t, p.problem.key, p.problem.params) : null;
1750
+ });
1751
+ watch7(problem, (p) => {
1752
+ if (p === null) attempted.value = false;
1753
+ });
1754
+ watch7(
1755
+ [code, codes, list],
1756
+ ([now, listed, el]) => {
1757
+ if (!el) return;
1758
+ if (listed.includes(now)) el.value = now;
1759
+ else el.selectedIndex = -1;
1760
+ },
1761
+ { flush: "post", immediate: true }
1762
+ );
1763
+ const apply = () => {
1764
+ const ed = editor.value;
1765
+ if (!ed) return;
1766
+ const step = numberFormatDialogApplyStep(ed, props.start, code.value);
1767
+ if (step.kind === "closed" || step.kind === "unchanged") {
1768
+ if (step.kind === "unchanged") applied = true;
1769
+ props.onClose();
1770
+ return;
1771
+ }
1772
+ if (step.kind === "problem") {
1773
+ attempted.value = true;
1774
+ error.value = translateKey(t, step.problem.key, step.problem.params);
1775
+ errorCount.value += 1;
1776
+ field.value?.focus();
1777
+ return;
1778
+ }
1779
+ if (s.execute(code.value)) {
1780
+ applied = true;
1781
+ props.onClose();
1782
+ return;
1783
+ }
1784
+ const after = ed.snapshot();
1785
+ error.value = after.refusal ? refusalText3(after.refusal, t, after.numberLocale) : t("dialogs.numberFormat.notApplied");
1786
+ errorCount.value += 1;
1787
+ };
1788
+ const pickCode = (value) => {
1789
+ code.value = value;
1790
+ error.value = null;
1791
+ };
1792
+ return () => {
1793
+ const p = preview.value;
1794
+ const listed = codes.value;
1795
+ const invalid = problem.value !== null && (attempted.value || code.value !== "");
1796
+ const sample = !!p?.ok && p.sample;
1797
+ const blank = !!p?.ok && p.text === "";
1798
+ const shownColor = p?.ok && p.color && HEX6.test(p.color) ? p.color : null;
1799
+ return /* @__PURE__ */ jsx7(
1800
+ DialogFrame,
1801
+ {
1802
+ kind: "numberFormat",
1803
+ label: t("dialogs.numberFormat.title"),
1804
+ onClose: props.onClose,
1805
+ onRestoreFocus: () => {
1806
+ if (applied && editor.value) {
1807
+ editor.value.focus();
1808
+ return true;
1809
+ }
1810
+ const back = props.returnTo;
1811
+ if (!back?.isConnected) return false;
1812
+ back.focus({ preventScroll: true });
1813
+ return true;
1814
+ },
1815
+ onKeyDown: (event) => {
1816
+ const tag = event.target.tagName;
1817
+ if (event.key !== "Enter" || tag !== "INPUT" && tag !== "SELECT") return;
1818
+ event.preventDefault();
1819
+ apply();
1820
+ },
1821
+ children: {
1822
+ default: () => /* @__PURE__ */ jsxs4(Fragment, { children: [
1823
+ /* @__PURE__ */ jsx7("div", { class: "xlsx-dialog__header", children: /* @__PURE__ */ jsx7("h2", { class: "xlsx-dialog__title", children: t("dialogs.numberFormat.title") }) }),
1824
+ /* @__PURE__ */ jsxs4("div", { class: "xlsx-dialog__body", children: [
1825
+ /* @__PURE__ */ jsx7("label", { class: "xlsx-dialog__label", for: ids.field, children: t("dialogs.numberFormat.code") }),
1826
+ /* @__PURE__ */ jsx7(
1827
+ "input",
1828
+ {
1829
+ ref: field,
1830
+ id: ids.field,
1831
+ name: "xlsx-number-format-code",
1832
+ type: "text",
1833
+ dir: "ltr",
1834
+ autocomplete: "off",
1835
+ spellcheck: false,
1836
+ class: "xlsx-dialog__input",
1837
+ value: code.value,
1838
+ "aria-invalid": invalid,
1839
+ "aria-describedby": [
1840
+ ids.hint,
1841
+ ...error.value ? [ids.error] : [ids.previewLabel, ids.preview],
1842
+ sample ? ids.sample : "",
1843
+ blank && !error.value ? ids.blank : ""
1844
+ ].filter(Boolean).join(" "),
1845
+ onInput: (e) => pickCode(e.currentTarget.value)
1846
+ }
1847
+ ),
1848
+ /* @__PURE__ */ jsx7("p", { id: ids.error, role: "alert", class: "xlsx-dialog__error", children: error.value }, errorCount.value),
1849
+ /* @__PURE__ */ jsx7("p", { id: ids.hint, class: "xlsx-dialog__hint", children: t(
1850
+ listed.length > 0 ? "dialogs.numberFormat.codeHint" : "dialogs.numberFormat.codeHintNoList"
1851
+ ) }),
1852
+ /* @__PURE__ */ jsxs4("div", { class: "xlsx-dialog__preview", hidden: error.value !== null, children: [
1853
+ /* @__PURE__ */ jsx7("span", { id: ids.previewLabel, class: "xlsx-dialog__preview-label", children: t("dialogs.numberFormat.preview") }),
1854
+ /* @__PURE__ */ jsx7(
1855
+ "output",
1856
+ {
1857
+ id: ids.preview,
1858
+ for: ids.field,
1859
+ "aria-labelledby": ids.previewLabel,
1860
+ "aria-live": "off",
1861
+ class: "xlsx-dialog__preview-value",
1862
+ "data-problem": problem.value ? "" : void 0,
1863
+ style: shownColor ? { color: `#${shownColor}` } : void 0,
1864
+ children: p?.ok ? p.stale ? t("grid.staleResult", { text: p.text }) : p.text : problem.value ?? ""
1865
+ }
1866
+ )
1867
+ ] }),
1868
+ blank && error.value === null ? /* @__PURE__ */ jsx7("p", { id: ids.blank, class: "xlsx-dialog__hint", children: t("dialogs.numberFormat.previewBlank") }) : null,
1869
+ sample ? /* @__PURE__ */ jsx7("p", { id: ids.sample, class: "xlsx-dialog__hint", children: t("dialogs.numberFormat.previewSample") }) : null,
1870
+ listed.length > 0 ? /* @__PURE__ */ jsx7("label", { class: "xlsx-dialog__label", for: ids.list, children: t("dialogs.numberFormat.existing") }) : /* @__PURE__ */ jsx7("span", { class: "xlsx-dialog__label", children: t("dialogs.numberFormat.existing") }),
1871
+ listed.length > 0 ? /* @__PURE__ */ jsx7(
1872
+ "select",
1873
+ {
1874
+ id: ids.list,
1875
+ name: "xlsx-number-format-existing",
1876
+ size: Math.min(6, Math.max(2, listed.length)),
1877
+ dir: "ltr",
1878
+ class: "xlsx-dialog__list",
1879
+ ref: list,
1880
+ onChange: (e) => pickCode(e.currentTarget.value),
1881
+ onKeydown: (e) => {
1882
+ const el = e.currentTarget;
1883
+ const down = e.key === "ArrowDown";
1884
+ if (el.selectedIndex !== -1 || !down && e.key !== "ArrowUp") return;
1885
+ e.preventDefault();
1886
+ const option = el.options[down ? 0 : el.options.length - 1];
1887
+ if (!option) return;
1888
+ pickCode(option.value);
1889
+ },
1890
+ onDblclick: (e) => {
1891
+ if (e.target.tagName === "OPTION") apply();
1892
+ },
1893
+ children: listed.map((c) => /* @__PURE__ */ jsx7("option", { value: c, title: c, children: c }, c))
1894
+ }
1895
+ ) : /* @__PURE__ */ jsx7("p", { class: "xlsx-dialog__hint", children: t("dialogs.numberFormat.noExisting") })
1896
+ ] }),
1897
+ /* @__PURE__ */ jsxs4("div", { class: "xlsx-dialog__footer", children: [
1898
+ /* @__PURE__ */ jsx7("button", { type: "button", class: "xlsx-dialog__button", onClick: props.onClose, children: t("common.cancel") }),
1899
+ /* @__PURE__ */ jsx7(
1900
+ "button",
1901
+ {
1902
+ type: "button",
1903
+ class: "xlsx-dialog__button xlsx-dialog__button--primary",
1904
+ "data-slot": "number.custom",
1905
+ onClick: apply,
1906
+ children: t("common.apply")
1907
+ }
1908
+ )
1909
+ ] })
1910
+ ] })
1911
+ }
1912
+ }
1913
+ );
1914
+ };
1915
+ }
1916
+ });
1917
+
1918
+ // src/toolbar/icons.ts
1919
+ import { CHROME_ICON_PATHS } from "@xlsx-editor.dev/core/editor";
1920
+ var ICON_MORE = CHROME_ICON_PATHS.more_horiz;
1921
+ var ICON_FIELD_CARET = CHROME_ICON_PATHS.expand_more;
1922
+ var ICON_MENU_CARET = CHROME_ICON_PATHS.arrow_drop_down;
1923
+ var ICON_CHECK = CHROME_ICON_PATHS.check;
1924
+
1925
+ // src/toolbar/parts.tsx
1926
+ import { jsx as jsx8, jsxs as jsxs5 } from "vue/jsx-runtime";
1927
+ var layoutProp = { type: String, default: "bar" };
1928
+ function slotAttributes(s, reasonId) {
1929
+ const words = s.words.value;
1930
+ return {
1931
+ "data-slot": s.slot,
1932
+ "aria-disabled": s.isEnabled.value ? void 0 : "true",
1933
+ ...words.description ? { "aria-describedby": reasonId } : {},
1934
+ title: words.title,
1935
+ onMousedown: guardToolbarMousedown,
1936
+ ...s.isActive.value ? { "data-active": "" } : {},
1937
+ ...s.isEnabled.value ? {} : { "data-disabled": "" }
1938
+ };
1939
+ }
1940
+ function reasonText(id, reason) {
1941
+ return reason ? /* @__PURE__ */ jsx8("span", { id, hidden: true, children: reason }) : null;
1942
+ }
1943
+ var ToolbarButton = defineComponent7({
1944
+ name: "XlsxToolbarButton",
1945
+ props: {
1946
+ slot: { type: String, required: true },
1947
+ layout: layoutProp
1948
+ },
1949
+ setup(props) {
1950
+ const s = useToolbarSlot(props.slot);
1951
+ const reasonId = useId4();
1952
+ return () => {
1953
+ const words = s.words.value;
1954
+ return /* @__PURE__ */ jsxs5(
1955
+ "button",
1956
+ {
1957
+ type: "button",
1958
+ class: props.layout === "row" ? "xlsx-toolbar-row" : "xlsx-toolbar-button",
1959
+ "aria-label": words.label,
1960
+ ...s.control.toggle ? { "aria-pressed": s.isActive.value } : {},
1961
+ ...slotAttributes(s, reasonId),
1962
+ onKeydown: (e) => {
1963
+ if (e.key === "Enter" && e.repeat) e.preventDefault();
1964
+ },
1965
+ onClick: () => {
1966
+ if (s.isEnabled.value) s.execute();
1967
+ },
1968
+ children: [
1969
+ /* @__PURE__ */ jsx8(ChromeIcon, { paths: s.control.paths }),
1970
+ props.layout === "row" ? /* @__PURE__ */ jsx8("span", { children: words.label }) : null,
1971
+ reasonText(reasonId, words.description)
1972
+ ]
1973
+ }
1974
+ );
1975
+ };
1976
+ }
1977
+ });
1978
+ function usePopup(selector, options = {}) {
1979
+ const { inline = false, closed = () => false, nested = false } = options;
1980
+ const open = shallowRef9(false);
1981
+ const root = shallowRef9(null);
1982
+ let focusOnOpen = false;
1983
+ const id = useId4();
1984
+ useDropdownClose(open, root, closed, nested);
1985
+ const focusIn = () => focusPopupItem(root.value, selector);
1986
+ watch8(
1987
+ open,
1988
+ (isOpen) => {
1989
+ if (!isOpen || !root.value) return;
1990
+ placePopup(root.value, inline, selector);
1991
+ if (!focusOnOpen) return;
1992
+ focusOnOpen = false;
1993
+ focusIn();
1994
+ },
1995
+ { flush: "post" }
1996
+ );
1997
+ return {
1998
+ open,
1999
+ setOpen: (next) => {
2000
+ open.value = next;
2001
+ },
2002
+ root,
2003
+ id,
2004
+ openWithKeyboard: () => {
2005
+ focusOnOpen = true;
2006
+ open.value = true;
2007
+ },
2008
+ focusIn
2009
+ };
2010
+ }
2011
+ function popupTriggerKeys(popup, enabled) {
2012
+ return {
2013
+ onKeydown: (e) => {
2014
+ const action = popupTriggerAction(e.key, popup.open.value);
2015
+ if (!action) return;
2016
+ e.preventDefault();
2017
+ if (!enabled) return;
2018
+ if (action === "open") popup.openWithKeyboard();
2019
+ else if (action === "focusIn") popup.focusIn();
2020
+ else popup.setOpen(false);
2021
+ },
2022
+ // Space activates a button on key up in some browsers: the key down already acted.
2023
+ onKeyup: (e) => {
2024
+ if (e.key === " ") e.preventDefault();
2025
+ }
2026
+ };
2027
+ }
2028
+ function trigger(t, options, children) {
2029
+ const { s, popup, kind, className, caret, name, reasonId, label } = options;
2030
+ const attributes = slotAttributes(s, reasonId);
2031
+ const after = s.words.value.description;
2032
+ const title = label === void 0 ? attributes.title : after ? t("toolbar.hintTitle", { label, hint: after }) : label;
2033
+ return /* @__PURE__ */ jsxs5(
2034
+ "button",
2035
+ {
2036
+ type: "button",
2037
+ class: className,
2038
+ "aria-haspopup": kind,
2039
+ "aria-expanded": popup.open.value,
2040
+ "aria-controls": popup.open.value ? popup.id : void 0,
2041
+ "aria-label": name,
2042
+ ...attributes,
2043
+ title,
2044
+ onClick: () => {
2045
+ if (s.isEnabled.value) popup.setOpen(!popup.open.value);
2046
+ },
2047
+ ...popupTriggerKeys(popup, s.isEnabled.value),
2048
+ children: [
2049
+ children,
2050
+ /* @__PURE__ */ jsx8(
2051
+ ChromeIcon,
2052
+ {
2053
+ paths: caret === "field" ? ICON_FIELD_CARET : ICON_MENU_CARET,
2054
+ className: "xlsx-toolbar-caret"
2055
+ }
2056
+ ),
2057
+ reasonText(reasonId, after)
2058
+ ]
2059
+ }
2060
+ );
2061
+ }
2062
+ function popupAttributes(layout, className) {
2063
+ return {
2064
+ class: `xlsx-toolbar-popup ${className}`,
2065
+ "data-popup": "",
2066
+ ...layout === "row" ? { "data-inline": "" } : {}
2067
+ };
2068
+ }
2069
+ var ToolbarList = defineComponent7({
2070
+ name: "XlsxToolbarList",
2071
+ props: {
2072
+ slot: { type: String, required: true },
2073
+ options: {
2074
+ type: Array,
2075
+ required: true
2076
+ },
2077
+ text: { type: String, required: true },
2078
+ /** What the trigger's accessible name and title say after the slot label, when not `text`. */
2079
+ name: { type: String, default: void 0 },
2080
+ /** The current entry (`aria-current`), which the keyboard focuses on open when none is picked. */
2081
+ active: { type: String, default: void 0 },
2082
+ layout: { type: String, required: true },
2083
+ width: { type: String, required: true },
2084
+ entry: { type: Object, default: void 0 },
2085
+ /** A last item after the list, such as the number format dialog's opener. */
2086
+ more: { type: Function, default: void 0 }
2087
+ },
2088
+ setup(props) {
2089
+ const { t } = useTranslation();
2090
+ const s = useToolbarSlot(props.slot);
2091
+ const items = `${props.entry ? "input, " : ""}[role="option"]${props.more ? ", [data-list-more]" : ""}`;
2092
+ const popup = usePopup(items, {
2093
+ inline: props.layout === "row",
2094
+ closed: () => !s.isEnabled.value
2095
+ });
2096
+ const invalid = shallowRef9(false);
2097
+ const reasonId = useId4();
2098
+ const pick = (value) => {
2099
+ popup.setOpen(false);
2100
+ s.execute(value);
2101
+ s.focusGrid();
2102
+ };
2103
+ return () => {
2104
+ const { layout, entry, text: text2, name } = props;
2105
+ const label = s.words.value.label;
2106
+ return /* @__PURE__ */ jsxs5(
2107
+ "div",
2108
+ {
2109
+ ref: popup.root,
2110
+ class: `xlsx-toolbar-dropdown${layout === "row" ? " xlsx-toolbar-labelled" : ""}`,
2111
+ children: [
2112
+ layout === "row" ? /* @__PURE__ */ jsx8("span", { class: "xlsx-toolbar-row-label", children: label }) : null,
2113
+ trigger(
2114
+ t,
2115
+ {
2116
+ s,
2117
+ popup,
2118
+ kind: "listbox",
2119
+ className: `xlsx-toolbar-select xlsx-toolbar-select-${props.width}`,
2120
+ caret: "field",
2121
+ name: `${label}: ${name ?? text2}`,
2122
+ reasonId,
2123
+ ...name !== void 0 ? { label: `${label}: ${name}` } : {}
2124
+ },
2125
+ /* @__PURE__ */ jsx8("span", { class: "xlsx-toolbar-select-text", children: text2 })
2126
+ ),
2127
+ popup.open.value ? /* @__PURE__ */ jsxs5(
2128
+ "div",
2129
+ {
2130
+ ...popupAttributes(layout, "xlsx-toolbar-list-popup"),
2131
+ onKeydown: (e) => movePopupFocus(e, items),
2132
+ children: [
2133
+ entry ? /* @__PURE__ */ jsx8(
2134
+ "input",
2135
+ {
2136
+ type: "text",
2137
+ inputmode: "decimal",
2138
+ autocomplete: "off",
2139
+ class: "xlsx-toolbar-entry",
2140
+ "aria-label": entry.label,
2141
+ "aria-invalid": invalid.value,
2142
+ ...invalid.value ? { "aria-describedby": `${popup.id}-entry-error` } : {},
2143
+ placeholder: text2,
2144
+ onInput: () => invalid.value = false,
2145
+ onKeydown: (e) => {
2146
+ if (e.key !== "Enter") return;
2147
+ e.preventDefault();
2148
+ const value = entry.parse(e.currentTarget.value);
2149
+ if (value === null) invalid.value = true;
2150
+ else pick(value);
2151
+ }
2152
+ }
2153
+ ) : null,
2154
+ entry && invalid.value ? /* @__PURE__ */ jsx8("div", { id: `${popup.id}-entry-error`, role: "alert", class: "xlsx-toolbar-entry-error", children: entry.invalid }) : null,
2155
+ /* @__PURE__ */ jsx8("ul", { id: popup.id, role: "listbox", "aria-label": label, class: "xlsx-toolbar-list", children: props.options.map((o) => /* @__PURE__ */ jsxs5(
2156
+ "li",
2157
+ {
2158
+ role: "option",
2159
+ tabindex: -1,
2160
+ "aria-selected": o.value === s.value.value,
2161
+ ...o.value === props.active ? { "aria-current": "true" } : {},
2162
+ onMousedown: guardToolbarMousedown,
2163
+ onClick: () => pick(o.value),
2164
+ onKeydown: (e) => {
2165
+ if (e.key !== "Enter" && e.key !== " ") return;
2166
+ e.preventDefault();
2167
+ pick(o.value);
2168
+ },
2169
+ children: [
2170
+ o.label,
2171
+ o.value === s.value.value ? /* @__PURE__ */ jsx8(ChromeIcon, { paths: ICON_CHECK, className: "xlsx-toolbar-check" }) : null
2172
+ ]
2173
+ },
2174
+ o.value
2175
+ )) }),
2176
+ props.more?.(() => popup.setOpen(false))
2177
+ ]
2178
+ }
2179
+ ) : null
2180
+ ]
2181
+ }
2182
+ );
2183
+ };
2184
+ }
2185
+ });
2186
+ var FontFamilyList = defineComponent7({
2187
+ name: "XlsxFontFamilyList",
2188
+ props: { layout: layoutProp },
2189
+ setup(props) {
2190
+ const s = useToolbarSlot("font.family");
2191
+ return () => {
2192
+ const current = s.value.value ?? "";
2193
+ const names = current && !FONT_FAMILY_CHOICES.includes(current) ? [current] : [];
2194
+ const options = [...names, ...FONT_FAMILY_CHOICES].map((n) => ({ value: n, label: n }));
2195
+ return /* @__PURE__ */ jsx8(
2196
+ ToolbarList,
2197
+ {
2198
+ slot: "font.family",
2199
+ options,
2200
+ text: current,
2201
+ layout: props.layout,
2202
+ width: "wide"
2203
+ }
2204
+ );
2205
+ };
2206
+ }
2207
+ });
2208
+ var FontSizeList = defineComponent7({
2209
+ name: "XlsxFontSizeList",
2210
+ props: { layout: layoutProp },
2211
+ setup(props) {
2212
+ const { t } = useTranslation();
2213
+ const s = useToolbarSlot("font.size");
2214
+ return () => {
2215
+ const current = Number(s.value.value);
2216
+ const sizes = [...FONT_SIZE_CHOICES];
2217
+ if (Number.isFinite(current) && current > 0 && !sizes.includes(current)) sizes.push(current);
2218
+ sizes.sort((a, b) => a - b);
2219
+ const options = sizes.map((n) => ({ value: String(n), label: String(n) }));
2220
+ return /* @__PURE__ */ jsx8(
2221
+ ToolbarList,
2222
+ {
2223
+ slot: "font.size",
2224
+ options,
2225
+ text: s.value.value ?? "",
2226
+ layout: props.layout,
2227
+ width: "narrow",
2228
+ entry: {
2229
+ label: t("fontSize.typeSize"),
2230
+ invalid: t("fontSize.invalid"),
2231
+ parse: parseFontSize
2232
+ }
2233
+ }
2234
+ );
2235
+ };
2236
+ }
2237
+ });
2238
+ var numberLocaleOf = (snapshot) => snapshot.numberLocale;
2239
+ var NumberFormatList = defineComponent7({
2240
+ name: "XlsxNumberFormatList",
2241
+ props: {
2242
+ layout: layoutProp,
2243
+ /** Show the number format dialog's opener (`number.custom`) after the list. */
2244
+ more: { type: Boolean, default: false }
2245
+ },
2246
+ setup(props) {
2247
+ const { t } = useTranslation();
2248
+ const s = useToolbarSlot("number.format");
2249
+ const locale = useEditorState(numberLocaleOf);
2250
+ const view = computed6(
2251
+ () => numberFormatListView(
2252
+ s.value.value ?? void 0,
2253
+ (key, params) => translateKey(t, key, params),
2254
+ locale.value
2255
+ )
2256
+ );
2257
+ const moreItem = (close) => /* @__PURE__ */ jsx8(MoreFormatsItem, { close });
2258
+ return () => {
2259
+ const v = view.value;
2260
+ return /* @__PURE__ */ jsx8(
2261
+ ToolbarList,
2262
+ {
2263
+ slot: "number.format",
2264
+ options: v.entries,
2265
+ text: v.text,
2266
+ ...v.name ? { name: v.name } : {},
2267
+ ...v.current !== void 0 ? { active: v.current } : {},
2268
+ layout: props.layout,
2269
+ width: "wide",
2270
+ ...props.more ? { more: moreItem } : {}
2271
+ }
2272
+ );
2273
+ };
2274
+ }
2275
+ });
2276
+ var MoreFormatsItem = defineComponent7({
2277
+ name: "XlsxMoreFormatsItem",
2278
+ props: { close: { type: Function, required: true } },
2279
+ setup(props) {
2280
+ const s = useToolbarSlot("number.custom");
2281
+ const dialog = useNumberFormatDialogOpener();
2282
+ const reasonId = useId4();
2283
+ return () => {
2284
+ if (!dialog) return null;
2285
+ const words = s.words.value;
2286
+ return /* @__PURE__ */ jsxs5(
2287
+ "button",
2288
+ {
2289
+ type: "button",
2290
+ class: "xlsx-toolbar-list-more",
2291
+ tabindex: -1,
2292
+ "data-list-more": "",
2293
+ "aria-haspopup": "dialog",
2294
+ ...slotAttributes(s, reasonId),
2295
+ onClick: (e) => {
2296
+ if (!s.isEnabled.value) return;
2297
+ const item = e.currentTarget;
2298
+ const root = item.closest(".xlsx-toolbar-dropdown");
2299
+ const fromKeyboard = item.ownerDocument.activeElement === item;
2300
+ const back = root?.querySelector('[aria-haspopup="listbox"]') ?? null;
2301
+ props.close();
2302
+ if (!dialog.open(fromKeyboard ? back : null) && fromKeyboard) s.focusGrid();
2303
+ },
2304
+ children: [
2305
+ words.label,
2306
+ reasonText(reasonId, words.description)
2307
+ ]
2308
+ }
2309
+ );
2310
+ };
2311
+ }
2312
+ });
2313
+ var NumberFormatDialogButton = defineComponent7({
2314
+ name: "XlsxNumberFormatDialogButton",
2315
+ props: { layout: layoutProp },
2316
+ setup(props) {
2317
+ const s = useToolbarSlot("number.custom");
2318
+ const dialog = useNumberFormatDialogOpener();
2319
+ const reasonId = useId4();
2320
+ return () => {
2321
+ if (!dialog) return null;
2322
+ const words = s.words.value;
2323
+ return /* @__PURE__ */ jsxs5(
2324
+ "button",
2325
+ {
2326
+ type: "button",
2327
+ class: props.layout === "row" ? "xlsx-toolbar-row" : "xlsx-toolbar-button",
2328
+ "aria-label": words.label,
2329
+ "aria-haspopup": "dialog",
2330
+ ...slotAttributes(s, reasonId),
2331
+ onClick: (e) => {
2332
+ if (!s.isEnabled.value) return;
2333
+ const self = e.currentTarget;
2334
+ dialog.open(self.ownerDocument.activeElement === self ? self : null);
2335
+ },
2336
+ children: [
2337
+ /* @__PURE__ */ jsx8(ChromeIcon, { paths: s.control.paths }),
2338
+ props.layout === "row" ? /* @__PURE__ */ jsx8("span", { children: words.label }) : null,
2339
+ reasonText(reasonId, words.description)
2340
+ ]
2341
+ }
2342
+ );
2343
+ };
2344
+ }
2345
+ });
2346
+ var ColorPalette = defineComponent7({
2347
+ name: "XlsxColorPalette",
2348
+ props: {
2349
+ slot: { type: String, required: true },
2350
+ layout: layoutProp
2351
+ },
2352
+ setup(props) {
2353
+ const { t } = useTranslation();
2354
+ const s = useToolbarSlot(props.slot);
2355
+ const popup = usePopup("[data-color]", {
2356
+ inline: props.layout === "row",
2357
+ closed: () => !s.isEnabled.value
2358
+ });
2359
+ const reasonId = useId4();
2360
+ const pick = (value) => {
2361
+ popup.setOpen(false);
2362
+ s.execute(value);
2363
+ s.focusGrid();
2364
+ };
2365
+ return () => {
2366
+ const { layout } = props;
2367
+ const label = s.words.value.label;
2368
+ const current = s.value.value ?? void 0;
2369
+ const colors = [
2370
+ ...props.slot === "fill.color" ? [{ value: NO_FILL, label: t("colorPicker.noFill") }] : [],
2371
+ ...COLOR_CHOICES.map((c) => ({ value: c.value, label: translateKey(t, c.labelKey) }))
2372
+ ];
2373
+ return /* @__PURE__ */ jsxs5(
2374
+ "div",
2375
+ {
2376
+ ref: popup.root,
2377
+ class: `xlsx-toolbar-dropdown${layout === "row" ? " xlsx-toolbar-labelled" : ""}`,
2378
+ children: [
2379
+ layout === "row" ? /* @__PURE__ */ jsx8("span", { class: "xlsx-toolbar-row-label", children: label }) : null,
2380
+ trigger(
2381
+ t,
2382
+ {
2383
+ s,
2384
+ popup,
2385
+ kind: "listbox",
2386
+ className: "xlsx-toolbar-color",
2387
+ caret: "menu",
2388
+ name: label,
2389
+ reasonId
2390
+ },
2391
+ /* @__PURE__ */ jsxs5("span", { class: "xlsx-toolbar-color-icon", children: [
2392
+ /* @__PURE__ */ jsx8(ChromeIcon, { paths: s.control.paths }),
2393
+ /* @__PURE__ */ jsx8(
2394
+ "span",
2395
+ {
2396
+ class: "xlsx-toolbar-color-bar",
2397
+ "data-none": isHexColor(current) ? void 0 : "",
2398
+ style: isHexColor(current) ? { backgroundColor: `#${current}` } : void 0
2399
+ }
2400
+ )
2401
+ ] })
2402
+ ),
2403
+ popup.open.value ? /* @__PURE__ */ jsx8(
2404
+ "div",
2405
+ {
2406
+ id: popup.id,
2407
+ role: "listbox",
2408
+ "aria-label": label,
2409
+ "aria-orientation": "horizontal",
2410
+ ...popupAttributes(layout, "xlsx-toolbar-palette"),
2411
+ onKeydown: (e) => movePopupFocus(e, "[data-color]", true),
2412
+ children: colors.map((c) => /* @__PURE__ */ jsx8(
2413
+ "button",
2414
+ {
2415
+ type: "button",
2416
+ role: "option",
2417
+ tabindex: -1,
2418
+ "data-color": c.value,
2419
+ "aria-label": c.label,
2420
+ title: c.label,
2421
+ "aria-selected": c.value === current,
2422
+ class: c.value === NO_FILL ? "xlsx-toolbar-no-fill" : "xlsx-toolbar-swatch",
2423
+ style: c.value === NO_FILL ? void 0 : { backgroundColor: `#${c.value}` },
2424
+ onMousedown: guardToolbarMousedown,
2425
+ onClick: () => pick(c.value),
2426
+ children: c.value === NO_FILL ? c.label : null
2427
+ },
2428
+ c.value
2429
+ ))
2430
+ }
2431
+ ) : null
2432
+ ]
2433
+ }
2434
+ );
2435
+ };
2436
+ }
2437
+ });
2438
+ var MenuItem = defineComponent7({
2439
+ name: "XlsxToolbarMenuItem",
2440
+ props: {
2441
+ slot: { type: String, required: true },
2442
+ close: { type: Function, required: true }
2443
+ },
2444
+ setup(props) {
2445
+ const s = useToolbarSlot(props.slot);
2446
+ const reasonId = useId4();
2447
+ return () => {
2448
+ const words = s.words.value;
2449
+ const reason = s.disabledReason.value;
2450
+ return /* @__PURE__ */ jsxs5(
2451
+ "button",
2452
+ {
2453
+ type: "button",
2454
+ role: "menuitem",
2455
+ class: "xlsx-toolbar-row",
2456
+ tabindex: -1,
2457
+ "data-slot": s.slot,
2458
+ "aria-disabled": !s.isEnabled.value,
2459
+ ...reason ? { "aria-describedby": reasonId } : {},
2460
+ title: words.title,
2461
+ onMousedown: guardToolbarMousedown,
2462
+ onClick: () => {
2463
+ if (!s.isEnabled.value) return;
2464
+ props.close();
2465
+ s.execute();
2466
+ s.focusGrid();
2467
+ },
2468
+ children: [
2469
+ /* @__PURE__ */ jsx8(ChromeIcon, { paths: s.control.paths }),
2470
+ /* @__PURE__ */ jsx8("span", { children: words.label }),
2471
+ reason ? /* @__PURE__ */ jsx8("span", { id: reasonId, class: "xlsx-toolbar-reason", children: reason }) : null
2472
+ ]
2473
+ }
2474
+ );
2475
+ };
2476
+ }
2477
+ });
2478
+ var GroupMenu = defineComponent7({
2479
+ name: "XlsxGroupMenu",
2480
+ props: { group: { type: Object, required: true } },
2481
+ setup(props) {
2482
+ const { t } = useTranslation();
2483
+ const popup = usePopup('[role="menuitem"]');
2484
+ const close = () => popup.setOpen(false);
2485
+ return () => {
2486
+ const { group } = props;
2487
+ const name = translateKey(t, group.labelKey);
2488
+ return /* @__PURE__ */ jsxs5("div", { ref: popup.root, class: "xlsx-toolbar-dropdown", children: [
2489
+ /* @__PURE__ */ jsxs5(
2490
+ "button",
2491
+ {
2492
+ type: "button",
2493
+ class: "xlsx-toolbar-button xlsx-toolbar-menu-button",
2494
+ "data-group-menu": group.id,
2495
+ "aria-haspopup": "menu",
2496
+ "aria-expanded": popup.open.value,
2497
+ "aria-controls": popup.open.value ? popup.id : void 0,
2498
+ "aria-label": name,
2499
+ title: name,
2500
+ onMousedown: guardToolbarMousedown,
2501
+ onClick: () => popup.setOpen(!popup.open.value),
2502
+ ...popupTriggerKeys(popup, true),
2503
+ children: [
2504
+ /* @__PURE__ */ jsx8(ChromeIcon, { paths: group.menu?.paths ?? null }),
2505
+ /* @__PURE__ */ jsx8(ChromeIcon, { paths: ICON_MENU_CARET, className: "xlsx-toolbar-caret" })
2506
+ ]
2507
+ }
2508
+ ),
2509
+ popup.open.value ? /* @__PURE__ */ jsx8(
2510
+ "div",
2511
+ {
2512
+ id: popup.id,
2513
+ role: "menu",
2514
+ "aria-label": name,
2515
+ class: "xlsx-toolbar-popup xlsx-toolbar-menu",
2516
+ "data-popup": "",
2517
+ onKeydown: (e) => movePopupFocus(e, '[role="menuitem"]'),
2518
+ children: group.controls.map((c) => /* @__PURE__ */ jsx8(MenuItem, { slot: `${group.id}.${c.id}`, close }, c.id))
2519
+ }
2520
+ ) : null
2521
+ ] });
2522
+ };
2523
+ }
2524
+ });
2525
+
2526
+ // src/toolbar/host-parts.tsx
2527
+ import {
2528
+ Comment,
2529
+ Fragment as Fragment2,
2530
+ camelize,
2531
+ cloneVNode,
2532
+ createTextVNode,
2533
+ defineComponent as defineComponent8,
2534
+ inject as inject6,
2535
+ isVNode
2536
+ } from "vue";
2537
+ import {
2538
+ CHROME_GROUPS,
2539
+ chromeSlotId,
2540
+ toolbarGroupHeading
2541
+ } from "@xlsx-editor.dev/core/editor";
2542
+ import { jsx as jsx9 } from "vue/jsx-runtime";
2543
+ var partLayoutKey = /* @__PURE__ */ Symbol("xlsx-toolbar-part-layout");
2544
+ function control(group, c, layout, replaced) {
2545
+ const slot = chromeSlotId(group, c);
2546
+ if (slot === "font.family") return /* @__PURE__ */ jsx9(FontFamilyList, { layout }, c.id);
2547
+ if (slot === "font.size") return /* @__PURE__ */ jsx9(FontSizeList, { layout }, c.id);
2548
+ const listed = (id) => group.controls.some((x) => x.id === id) && !replaced?.has(`${group.id}.${id}`);
2549
+ if (slot === "number.format")
2550
+ return /* @__PURE__ */ jsx9(NumberFormatList, { layout, more: listed("custom") }, c.id);
2551
+ if (c.shape === "dialog" && slot === "number.custom")
2552
+ return listed("format") ? null : /* @__PURE__ */ jsx9(NumberFormatDialogButton, { layout }, c.id);
2553
+ if (slot === "font.color" || slot === "fill.color")
2554
+ return /* @__PURE__ */ jsx9(ColorPalette, { slot, layout }, c.id);
2555
+ return /* @__PURE__ */ jsx9(ToolbarButton, { slot, layout }, c.id);
2556
+ }
2557
+ function groupOfSlot(slot) {
2558
+ for (const g of CHROME_GROUPS)
2559
+ for (const c of g.controls)
2560
+ if (chromeSlotId(g, c) === slot) return { group: { ...g, controls: [c] }, control: c };
2561
+ return null;
2562
+ }
2563
+ var keyPart = (child, index) => isVNode(child) && child.key != null ? `k${String(child.key)}` : `#${index}`;
2564
+ function visibleNodes(content) {
2565
+ const out = [];
2566
+ const walk = (node, key) => {
2567
+ if (Array.isArray(node)) node.forEach((child, i) => walk(child, `${key}/${keyPart(child, i)}`));
2568
+ else if (typeof node === "string" || typeof node === "number") {
2569
+ if (node !== "") out.push(cloneVNode(createTextVNode(String(node)), { key }));
2570
+ } else if (isVNode(node)) {
2571
+ if (node.type === Fragment2) walk(node.children, key);
2572
+ else if (node.type !== Comment) out.push(cloneVNode(node, { key }));
2573
+ }
2574
+ };
2575
+ walk(content, "");
2576
+ return out;
2577
+ }
2578
+ function markPart(part, marker) {
2579
+ part[marker] = true;
2580
+ }
2581
+ var xlsxEditorToolbarSlotProps = {
2582
+ /** The built-in slot that the part names, such as `'font.bold'`. */
2583
+ slotId: { type: String, required: true },
2584
+ /**
2585
+ * Removes the slot where it stands, as the toolbar's `hidden` list does. A group whose slots
2586
+ * are all hidden is left out.
2587
+ */
2588
+ hidden: { type: Boolean, default: void 0 }
2589
+ };
2590
+ var XlsxEditorToolbarSlot = defineComponent8({
2591
+ name: "XlsxEditorToolbarSlot",
2592
+ props: xlsxEditorToolbarSlotProps,
2593
+ setup(props, { slots }) {
2594
+ const layout = inject6(partLayoutKey, "bar");
2595
+ return () => {
2596
+ if (props.hidden) return null;
2597
+ const content = visibleNodes(slots.default?.());
2598
+ if (content.length > 0) return content;
2599
+ const found = groupOfSlot(props.slotId);
2600
+ if (!found) warnToolbarProblems([unknownSlotProblem(props.slotId, "XlsxEditorToolbar.Slot")]);
2601
+ return found ? control(found.group, found.control, layout) : null;
2602
+ };
2603
+ }
2604
+ });
2605
+ markPart(XlsxEditorToolbarSlot, "xlsxToolbarSlot");
2606
+ var xlsxEditorToolbarGroupProps = {
2607
+ /**
2608
+ * The group id. A built-in id, such as `'font'` or `'file'`, adds the default slot to that
2609
+ * group, after its own controls. Any other id makes a host group, which the bar measures and
2610
+ * moves into the "More" panel as it moves its own groups.
2611
+ */
2612
+ id: { type: String, required: true },
2613
+ /**
2614
+ * The heading of a host group in the "More" panel, and its accessible name. It wins over
2615
+ * `labelKey`. Without either, the heading is the `id`. A built-in group keeps its own label.
2616
+ */
2617
+ label: { type: String, default: void 0 },
2618
+ /** A catalog key for the heading of a host group, translated with the toolbar's locale. */
2619
+ labelKey: { type: String, default: void 0 },
2620
+ /**
2621
+ * The group that a host group follows in the bar. Without it, a host group follows every
2622
+ * built-in group, in order of appearance. A built-in group keeps its place.
2623
+ */
2624
+ after: { type: String, default: void 0 },
2625
+ /** Removes the group, built-in or host, from the bar and from the "More" panel. */
2626
+ hidden: { type: Boolean, default: void 0 },
2627
+ /** Appended after the `xlsx-toolbar-group` class. A host `class` also works. */
2628
+ className: { type: String, default: void 0 }
2629
+ };
2630
+ var XlsxEditorToolbarGroup = defineComponent8({
2631
+ name: "XlsxEditorToolbarGroup",
2632
+ props: xlsxEditorToolbarGroupProps,
2633
+ setup(props, { slots }) {
2634
+ const { t } = useTranslation();
2635
+ return () => {
2636
+ const content = props.hidden ? [] : visibleNodes(slots.default?.());
2637
+ return content.length === 0 ? null : /* @__PURE__ */ jsx9(
2638
+ "div",
2639
+ {
2640
+ role: "group",
2641
+ "aria-label": toolbarGroupHeading(props, (key) => translateKey(t, key)),
2642
+ class: ["xlsx-toolbar-group", props.className],
2643
+ "data-host-group": props.id,
2644
+ children: content
2645
+ }
2646
+ );
2647
+ };
2648
+ }
2649
+ });
2650
+ markPart(XlsxEditorToolbarGroup, "xlsxToolbarGroup");
2651
+ function vnodeFlag(value) {
2652
+ return value === true || value === "";
2653
+ }
2654
+ var isPart = (node, marker) => typeof node.type === "object" && node.type !== null && node.type[marker] === true;
2655
+ function partProps(node) {
2656
+ const props = /* @__PURE__ */ Object.create(null);
2657
+ for (const [key, value] of Object.entries(node.props ?? {})) props[camelize(key)] = value;
2658
+ const children = node.children;
2659
+ const slot = children !== null && typeof children === "object" && !Array.isArray(children) ? children.default : void 0;
2660
+ const content = typeof slot === "function" ? visibleNodes(slot()) : Array.isArray(children) || typeof children === "string" ? visibleNodes(children) : [];
2661
+ return { props, content };
2662
+ }
2663
+ var text = (value) => typeof value === "string" ? value : void 0;
2664
+ function readToolbarChildren(children) {
2665
+ const slots = /* @__PURE__ */ new Map();
2666
+ const groups = /* @__PURE__ */ new Map();
2667
+ const slotRequests = [];
2668
+ const groupRequests = [];
2669
+ const appended = [];
2670
+ for (const node of visibleNodes(children)) {
2671
+ if (isPart(node, "xlsxToolbarGroup")) {
2672
+ const { props, content } = partProps(node);
2673
+ const id = String(props.id);
2674
+ const className = [text(props.class), text(props.className)].filter(Boolean).join(" ");
2675
+ groups.set(id, { className: className || void 0, content });
2676
+ groupRequests.push({
2677
+ id,
2678
+ after: text(props.after),
2679
+ hidden: vnodeFlag(props.hidden),
2680
+ label: text(props.label),
2681
+ labelKey: text(props.labelKey),
2682
+ content: content.length > 0
2683
+ });
2684
+ } else if (isPart(node, "xlsxToolbarSlot")) {
2685
+ const { props, content } = partProps(node);
2686
+ const slotId = String(props.slotId);
2687
+ slots.set(slotId, content);
2688
+ slotRequests.push({ slotId, hidden: vnodeFlag(props.hidden), content: content.length > 0 });
2689
+ } else appended.push(node);
2690
+ }
2691
+ return { slots, groups, appended, slotRequests, groupRequests };
2692
+ }
2693
+ function unknownSlotProblem(slotId, part) {
2694
+ return `"${slotId}" is not a toolbar slot id, so its ${part} renders nothing. Use a ChromeSlotId, such as "font.bold".`;
2695
+ }
2696
+ var printed = /* @__PURE__ */ new Set();
2697
+ function warnToolbarProblems(problems) {
2698
+ if (problems.length === 0 || !isDevelopment()) return;
2699
+ for (const problem of problems) {
2700
+ if (printed.has(problem)) continue;
2701
+ printed.add(problem);
2702
+ console.warn(`[xlsx-editor] ${problem}`);
2703
+ }
2704
+ }
2705
+
2706
+ // src/toolbar/Toolbar.tsx
2707
+ import { jsx as jsx10, jsxs as jsxs6 } from "vue/jsx-runtime";
2708
+ var PART_NAMES = { slot: "XlsxEditorToolbar.Slot", group: "XlsxEditorToolbar.Group" };
2709
+ var xlsxEditorToolbarProps = {
2710
+ /**
2711
+ * Receives the workbook as bytes of the opened package's kind (`snapshot().workbookKind`: a
2712
+ * workbook or a template) when the save control is pressed, or when the user presses Control
2713
+ * or Command and S in the editor, also with the save control hidden. Without it, the save
2714
+ * control is disabled with the engine's reason, and the browser keeps the shortcut. A refused
2715
+ * save calls nothing: the snapshot holds the refusal with `command: 'save'`. The bytes arrive
2716
+ * after the press, because a save runs in short tasks. `saved.stillOpen` is false when
2717
+ * another open, `close()`, or `destroy()` ended the workbook during the save, and
2718
+ * `saved.revision` is the revision of the bytes. A `@save` listener sets it too.
2719
+ */
2720
+ onSave: {
2721
+ type: Function,
2722
+ default: void 0
2723
+ },
2724
+ /**
2725
+ * Called once when a press of the save control, or of Control or Command and S in the editor,
2726
+ * starts a delivery to `onSave`. It gets the promise of the result, so you can show that the
2727
+ * save runs. A press that joins the running save of an earlier press and a refused save do
2728
+ * not call it. A press that joins a save from `saveInTasks` calls it, because that save has no
2729
+ * delivery yet. The promise resolves to `null` when the save is refused later. A
2730
+ * `@save-start` listener sets it too.
2731
+ */
2732
+ onSaveStart: {
2733
+ type: Function,
2734
+ default: void 0
2735
+ },
2736
+ /**
2737
+ * Whether your page opens another workbook now, while the editor still shows the old one: for
2738
+ * example during a download or a file read before `load`. While it is true, the save control
2739
+ * is disabled with the reason that a workbook is loading, and Control or Command and S in the
2740
+ * editor starts no save work. After such a press, and only then, the snapshot holds the
2741
+ * refusal `loading`, with `command: 'save'`. When it changes to false, the toolbar forgets
2742
+ * that refusal and keeps any other refusal. The default is `false`.
2743
+ */
2744
+ opening: { type: Boolean, default: void 0 },
2745
+ /**
2746
+ * `false` wraps the controls onto more rows instead of moving the groups that do not fit
2747
+ * into the "More" panel. It has no effect with `:preset="false"`, which always wraps.
2748
+ */
2749
+ overflow: { type: Boolean, default: true },
2750
+ /**
2751
+ * `false` renders only the default slot, in its order, with no measure and no "More" panel.
2752
+ * An `XlsxEditorToolbar.Slot` there renders the built-in control of its slot, or its default
2753
+ * slot. The default is `true`: the groups of `CHROME_GROUPS`, changed by the default slot,
2754
+ * where `XlsxEditorToolbar.Slot` replaces or hides one slot where it stands and
2755
+ * `XlsxEditorToolbar.Group` adds a host group or changes a built-in group. Other content
2756
+ * shows in one block after the groups, which never moves into the "More" panel.
2757
+ */
2758
+ preset: { type: Boolean, default: true },
2759
+ /** Appended after the `xlsx-toolbar` class. A host `class` also works. */
2760
+ className: { type: String, default: void 0 },
2761
+ /**
2762
+ * Slot ids to leave out, such as `['file.save']`. A group whose slots are all hidden is left
2763
+ * out with its separator. The order of the list does not matter.
2764
+ */
2765
+ hidden: { type: Array, default: void 0 }
2766
+ };
2767
+ function groupHeading(t, group) {
2768
+ const translate = (key) => translateKey(t, key);
2769
+ return group.chrome ? translate(group.chrome.labelKey) : toolbarGroupHeading2(group, translate);
2770
+ }
2771
+ function groupContent(group, parts, replaced, layout) {
2772
+ const chrome = group.chrome;
2773
+ const controls = chrome?.controls ?? [];
2774
+ const own = !!chrome?.menu && layout === "bar";
2775
+ const menu = chrome && own ? controls.filter((c) => !replaced.has(chromeSlotId2(chrome, c))) : [];
2776
+ const keyed = (key, nodes) => nodes ? h2(Fragment3, { key }, Array.isArray(nodes) ? [...nodes] : [nodes]) : null;
2777
+ return [
2778
+ chrome && menu.length > 0 ? /* @__PURE__ */ jsx10(GroupMenu, { group: { ...chrome, controls: menu } }, "menu") : null,
2779
+ ...controls.map((c) => {
2780
+ const slot = chromeSlotId2(chrome, c);
2781
+ if (replaced.has(slot)) return keyed(`slot:${c.id}`, parts.slots.get(slot));
2782
+ return own ? null : keyed(`control:${c.id}`, control(chrome, c, layout, replaced));
2783
+ }),
2784
+ keyed("content", parts.groups.get(group.id)?.content)
2785
+ ];
2786
+ }
2787
+ function barGroup(t, group, parts, replaced) {
2788
+ return /* @__PURE__ */ jsx10(
2789
+ "div",
2790
+ {
2791
+ class: ["xlsx-toolbar-group", parts.groups.get(group.id)?.className],
2792
+ role: "group",
2793
+ "aria-label": groupHeading(t, group),
2794
+ "data-group": group.id,
2795
+ children: groupContent(group, parts, replaced, "bar")
2796
+ },
2797
+ `group:${group.id}`
2798
+ );
2799
+ }
2800
+ var isLoading = (s) => s.status === "loading";
2801
+ var MorePanel = defineComponent9({
2802
+ name: "XlsxToolbarMore",
2803
+ props: {
2804
+ groups: { type: Array, required: true },
2805
+ parts: { type: Object, required: true },
2806
+ replaced: { type: Object, required: true }
2807
+ },
2808
+ setup(props) {
2809
+ const { t } = useTranslation();
2810
+ provide5(partLayoutKey, "row");
2811
+ const popup = usePopup("button, input", { nested: true });
2812
+ const loading = useEditorState(isLoading);
2813
+ watch9(loading, (now) => {
2814
+ if (now) popup.setOpen(false);
2815
+ });
2816
+ provide5(panelCloseKey, () => popup.setOpen(false));
2817
+ return () => {
2818
+ const text2 = t("formattingBar.more");
2819
+ return /* @__PURE__ */ jsxs6("div", { ref: popup.root, class: "xlsx-toolbar-more", children: [
2820
+ /* @__PURE__ */ jsx10(
2821
+ "button",
2822
+ {
2823
+ type: "button",
2824
+ class: "xlsx-toolbar-button",
2825
+ "aria-haspopup": "dialog",
2826
+ "aria-expanded": popup.open.value,
2827
+ "aria-controls": popup.open.value ? popup.id : void 0,
2828
+ "aria-label": text2,
2829
+ title: text2,
2830
+ onMousedown: guardToolbarMousedown2,
2831
+ onClick: () => popup.setOpen(!popup.open.value),
2832
+ ...popupTriggerKeys(popup, true),
2833
+ children: /* @__PURE__ */ jsx10(ChromeIcon, { paths: ICON_MORE })
2834
+ }
2835
+ ),
2836
+ popup.open.value ? /* @__PURE__ */ jsx10(
2837
+ "div",
2838
+ {
2839
+ id: popup.id,
2840
+ role: "dialog",
2841
+ "aria-label": text2,
2842
+ class: "xlsx-toolbar-panel",
2843
+ "data-popup": "",
2844
+ children: props.groups.map((g) => /* @__PURE__ */ jsxs6(
2845
+ "div",
2846
+ {
2847
+ role: "group",
2848
+ "aria-label": groupHeading(t, g),
2849
+ class: "xlsx-toolbar-section",
2850
+ children: [
2851
+ /* @__PURE__ */ jsx10("span", { class: "xlsx-toolbar-heading", "aria-hidden": "true", children: groupHeading(t, g) }),
2852
+ groupContent(g, props.parts, props.replaced, "row")
2853
+ ]
2854
+ },
2855
+ `group:${g.id}`
2856
+ ))
2857
+ }
2858
+ ) : null
2859
+ ] });
2860
+ };
2861
+ }
2862
+ });
2863
+ var XlsxEditorToolbarRoot = defineComponent9({
2864
+ name: "XlsxEditorToolbar",
2865
+ inheritAttrs: false,
2866
+ props: xlsxEditorToolbarProps,
2867
+ setup(props, { attrs, slots }) {
2868
+ const { t } = useTranslation();
2869
+ const translator = useTranslator();
2870
+ const editor = useXlsxEditor();
2871
+ const host = computed7(() => {
2872
+ const opening = props.opening;
2873
+ return {
2874
+ ...props.onSave ? { save: props.onSave } : {},
2875
+ ...props.onSaveStart ? { saveStart: props.onSaveStart } : {},
2876
+ ...opening === void 0 ? {} : { opening: () => props.opening ?? false }
2877
+ };
2878
+ });
2879
+ provide5(toolbarHostKey, host);
2880
+ const arranged = shallowRef10(arrangeToolbar({ hidden: props.hidden }));
2881
+ const groups = computed7(() => arranged.value.groups);
2882
+ const hasFixed = shallowRef10(false);
2883
+ const bar = shallowRef10(null);
2884
+ let wasOpening = false;
2885
+ watch9(
2886
+ [() => props.opening, editor],
2887
+ ([opening, ed]) => {
2888
+ const ended = wasOpening && !opening;
2889
+ wasOpening = !!opening;
2890
+ if (!ended || !ed) return;
2891
+ const refusal = ed.snapshot().refusal;
2892
+ if (refusal?.code === "loading" && refusal.command === "save") ed.dismissRefusal();
2893
+ },
2894
+ { immediate: true }
2895
+ );
2896
+ watch9(
2897
+ [bar, editor, () => !!props.onSave, host],
2898
+ ([el, ed, saves, h4], _old, onCleanup) => {
2899
+ if (!el || !ed || !saves) return;
2900
+ onCleanup(listenSaveKey(el, () => runToolbarCommand2(ed, "file.save", void 0, h4)));
2901
+ },
2902
+ { immediate: true, flush: "post" }
2903
+ );
2904
+ const widths = /* @__PURE__ */ new Map();
2905
+ const shown = shallowRef10(groups.value.length);
2906
+ let measuredFor = null;
2907
+ watch9(
2908
+ [() => props.overflow && props.preset, groups, shown, translator, bar, hasFixed],
2909
+ ([measuring, all, , tr, el], _old, onCleanup) => {
2910
+ if (measuredFor?.t !== tr || measuredFor.groups !== all) widths.clear();
2911
+ measuredFor = { t: tr, groups: all };
2912
+ if (!el || !measuring) {
2913
+ shown.value = all.length;
2914
+ return;
2915
+ }
2916
+ const measure = () => {
2917
+ const next = measureToolbar(el, all, widths);
2918
+ if (shown.value !== next) shown.value = next;
2919
+ };
2920
+ measure();
2921
+ onCleanup(observeToolbar(el, measure));
2922
+ },
2923
+ { immediate: true, flush: "post" }
2924
+ );
2925
+ return () => {
2926
+ const children = slots.default?.();
2927
+ const measuring = props.overflow && props.preset;
2928
+ let body = children;
2929
+ if (!props.preset) hasFixed.value = false;
2930
+ else {
2931
+ const parts = readToolbarChildren(children);
2932
+ const next = arrangeToolbar({
2933
+ hidden: props.hidden,
2934
+ slots: parts.slotRequests,
2935
+ groups: parts.groupRequests,
2936
+ names: PART_NAMES
2937
+ });
2938
+ warnToolbarProblems(next.problems);
2939
+ if (next.key !== arranged.value.key) arranged.value = next;
2940
+ const all = next.groups;
2941
+ const replaced = next.replaced;
2942
+ const rest = all.slice(shown.value);
2943
+ const items = all.slice(0, shown.value).map((g) => barGroup(t, g, parts, replaced));
2944
+ if (rest.length > 0)
2945
+ items.push(/* @__PURE__ */ jsx10(MorePanel, { groups: rest, parts, replaced }, "more"));
2946
+ const fixed = [
2947
+ ...parts.appended,
2948
+ ...next.outside.map(
2949
+ (slot) => h2(Fragment3, { key: `slot:${slot}` }, [...parts.slots.get(slot) ?? []])
2950
+ )
2951
+ ];
2952
+ hasFixed.value = fixed.length > 0;
2953
+ if (fixed.length > 0)
2954
+ items.push(
2955
+ /* @__PURE__ */ jsx10("div", { class: "xlsx-toolbar-group", ...{ [TOOLBAR_FIXED_ATTRIBUTE]: "" }, children: fixed }, "fixed")
2956
+ );
2957
+ body = items;
2958
+ }
2959
+ return /* @__PURE__ */ jsx10(NumberFormatDialogHost, { children: {
2960
+ default: () => /* @__PURE__ */ jsx10(
2961
+ "div",
2962
+ {
2963
+ ...attrs,
2964
+ ref: bar,
2965
+ role: "toolbar",
2966
+ "aria-label": t("formattingBar.label"),
2967
+ class: ["xlsx-toolbar", props.className, attrs.class],
2968
+ ...measuring ? { "data-overflow": "" } : {},
2969
+ onMousedown: guardToolbarMousedown2,
2970
+ onKeydown: moveBarFocus,
2971
+ children: body
2972
+ }
2973
+ )
2974
+ } });
2975
+ };
2976
+ }
2977
+ });
2978
+ var XlsxEditorToolbar = Object.assign(XlsxEditorToolbarRoot, {
2979
+ Slot: XlsxEditorToolbarSlot,
2980
+ Group: XlsxEditorToolbarGroup
2981
+ });
2982
+
2983
+ // src/XlsxEditor.ts
2984
+ var xlsxEditorProps = {
2985
+ /** The bytes of a workbook or a template package, as on `XlsxEditorRoot`. */
2986
+ document: {
2987
+ type: [Uint8Array, ArrayBuffer],
2988
+ default: void 0
2989
+ },
2990
+ /** The faces that measure and paint text, as on `XlsxEditorRoot`. */
2991
+ fonts: { type: [Array, Function], default: void 0 },
2992
+ /** The number locale of the shown values, as on `XlsxEditorRoot`. */
2993
+ numberLocale: { type: Object, default: void 0 },
2994
+ /** `'edit'` (default) or `'view'`, as on `XlsxEditorRoot`. */
2995
+ mode: { type: String, default: void 0 },
2996
+ /** UI strings, as on `XlsxEditorRoot`. English when omitted. */
2997
+ locale: { type: Object, default: void 0 },
2998
+ /** The chrome color theme, as on `XlsxEditorRoot`. */
2999
+ colorMode: { type: String, default: void 0 },
3000
+ /**
3001
+ * Whether your page opens another workbook now, while the editor still shows the old one. While
3002
+ * it is true, the save control is disabled and Control or Command and S starts no save. See the
3003
+ * `opening` prop of `XlsxEditorToolbar`.
3004
+ */
3005
+ opening: { type: Boolean, default: void 0 }
3006
+ };
3007
+ var statusOf = (snapshot) => snapshot.status;
3008
+ var Body = defineComponent10({
3009
+ name: "XlsxEditorBody",
3010
+ props: {
3011
+ onSave: {
3012
+ type: Function,
3013
+ default: void 0
3014
+ },
3015
+ onSaveStart: {
3016
+ type: Function,
3017
+ default: void 0
3018
+ },
3019
+ opening: { type: Boolean, default: void 0 }
3020
+ },
3021
+ setup(props) {
3022
+ const status = useEditorState(statusOf);
3023
+ return () => [
3024
+ h3(XlsxEditorToolbar, {
3025
+ ...props.onSave ? { onSave: props.onSave } : {},
3026
+ ...props.onSaveStart ? { onSaveStart: props.onSaveStart } : {},
3027
+ ...props.opening === void 0 ? {} : { opening: props.opening }
3028
+ }),
3029
+ h3(FormulaBar),
3030
+ h3(StatusMessage),
3031
+ h3(XlsxEditorContent, status.value === "ready" ? {} : { className: "xlsx-canvas-idle" }),
3032
+ h3("div", { class: "xlsx-footer" }, [h3(SheetTabs), h3(ZoomControl)])
3033
+ ];
3034
+ }
3035
+ });
3036
+ var XlsxEditor = defineComponent10({
3037
+ name: "XlsxEditor",
3038
+ // The root renders no element, so the host's attributes (`class`, `style`, `id`, `data-*`)
3039
+ // go onto the viewport, as React's `className` and `style` do.
3040
+ inheritAttrs: false,
3041
+ props: xlsxEditorProps,
3042
+ emits: {
3043
+ ready: (_editor) => true,
3044
+ change: (_snapshot) => true,
3045
+ fontError: (_failure) => true,
3046
+ save: (_bytes, _saved) => true,
3047
+ saveStart: (_saving) => true
3048
+ },
3049
+ setup(props, { attrs, emit, expose }) {
3050
+ const self = getCurrentInstance2();
3051
+ const root = shallowRef11(null);
3052
+ expose({
3053
+ load: async (bytes) => root.value ? root.value.load(bytes) : null,
3054
+ save: () => root.value?.save() ?? null,
3055
+ saveInTasks: async () => root.value ? root.value.saveInTasks() : null,
3056
+ getEditor: () => root.value?.getEditor() ?? null,
3057
+ focus: () => root.value?.focus(),
3058
+ snapshot: () => root.value?.snapshot() ?? emptySnapshotOf3(props.mode ?? "edit")
3059
+ });
3060
+ const onSave = (bytes, saved) => emit("save", bytes, saved);
3061
+ const onSaveStart = (saving) => emit("saveStart", saving);
3062
+ return () => {
3063
+ const listeners = self?.vnode.props ?? null;
3064
+ return h3(
3065
+ XlsxEditorRoot,
3066
+ {
3067
+ ref: root,
3068
+ ...props.document ? { document: props.document } : {},
3069
+ ...props.fonts ? { fonts: props.fonts } : {},
3070
+ ...props.numberLocale ? { numberLocale: props.numberLocale } : {},
3071
+ ...props.mode ? { mode: props.mode } : {},
3072
+ ...props.locale ? { locale: props.locale } : {},
3073
+ ...props.colorMode ? { colorMode: props.colorMode } : {},
3074
+ // Forwarded only with a listener, so the root still warns when nobody listens.
3075
+ ...hasListener(listeners, "fontError") ? { onFontError: (failure) => emit("fontError", failure) } : {},
3076
+ onReady: (editor) => emit("ready", editor),
3077
+ onChange: (snapshot) => emit("change", snapshot)
3078
+ },
3079
+ {
3080
+ default: () => h3(
3081
+ XlsxEditorViewport,
3082
+ mergeProps(attrs),
3083
+ () => h3(Body, {
3084
+ // A `.once` save listener does not enable Save: after it runs, a press would save
3085
+ // for no listener. `XlsxEditorToolbar` takes `save` as a prop, which has no
3086
+ // `.once` either.
3087
+ ...hasListener(listeners, "save", false) ? { onSave } : {},
3088
+ ...hasListener(listeners, "saveStart") ? { onSaveStart } : {},
3089
+ ...props.opening === void 0 ? {} : { opening: props.opening }
3090
+ })
3091
+ )
3092
+ }
3093
+ );
3094
+ };
3095
+ }
3096
+ });
3097
+
3098
+ // src/index.ts
3099
+ import {
3100
+ CLIPBOARD_TYPE,
3101
+ composesKey as composesKey2,
3102
+ contentFieldText as contentFieldText2,
3103
+ CUSTOM_NUMBER_FORMAT_PREFIX,
3104
+ editMoveOf as editMoveOf2,
3105
+ fieldHistoryKey,
3106
+ MAX_COLUMN_WIDTH,
3107
+ MAX_CONTENT_FIELD_CHARS,
3108
+ MAX_GO_TO_LENGTH as MAX_GO_TO_LENGTH2,
3109
+ MAX_LISTED_NUMBER_FORMATS,
3110
+ MAX_RESIZE_NEW_ROWS,
3111
+ MAX_ROW_HEIGHT,
3112
+ mergeCommandState,
3113
+ NUMBER_FORMAT_CODE_PROBLEM_KEYS,
3114
+ NUMBER_FORMAT_SAMPLE,
3115
+ numberFormatChoiceCode,
3116
+ numberFormatChoices,
3117
+ numberFormatCodeProblem,
3118
+ numberFormatDialogApplyStep as numberFormatDialogApplyStep2,
3119
+ numberFormatDialogCurrent as numberFormatDialogCurrent2,
3120
+ numberFormatDialogStart as numberFormatDialogStart2,
3121
+ numberFormatListView as numberFormatListView2,
3122
+ numberFormatValue,
3123
+ previewNumberFormat as previewNumberFormat2,
3124
+ refusalText as refusalText4,
3125
+ refusesEditText as refusesEditText2,
3126
+ refusesReference as refusesReference2,
3127
+ sheetCommandState as sheetCommandState2,
3128
+ trapTabWithin as trapTabWithin2,
3129
+ workbookNumberFormatCodes as workbookNumberFormatCodes2
3130
+ } from "@xlsx-editor.dev/core/editor";
3131
+
3132
+ // src/customization-hooks.ts
3133
+ import { getCurrentScope as getCurrentScope2, watch as watch10 } from "vue";
3134
+ function useEditorEvent(event, handler) {
3135
+ const editor = useXlsxEditor();
3136
+ if (!getCurrentScope2()) return;
3137
+ watch10(
3138
+ editor,
3139
+ (instance, _prev, onCleanup) => {
3140
+ if (instance) onCleanup(instance.on(event, handler));
3141
+ },
3142
+ { immediate: true }
3143
+ );
3144
+ }
3145
+
3146
+ // src/index.ts
3147
+ var XlsxEditor2 = Object.assign(XlsxEditor, {
3148
+ Root: XlsxEditorRoot,
3149
+ Viewport: XlsxEditorViewport,
3150
+ Content: XlsxEditorContent,
3151
+ Toolbar: XlsxEditorToolbar,
3152
+ FormulaBar,
3153
+ SheetTabs,
3154
+ ZoomControl,
3155
+ StatusMessage
3156
+ });
3157
+ export {
3158
+ CLIPBOARD_TYPE,
3159
+ CUSTOM_NUMBER_FORMAT_PREFIX,
3160
+ FormulaBar,
3161
+ MAX_COLUMN_WIDTH,
3162
+ MAX_CONTENT_FIELD_CHARS,
3163
+ MAX_GO_TO_LENGTH2 as MAX_GO_TO_LENGTH,
3164
+ MAX_LISTED_NUMBER_FORMATS,
3165
+ MAX_RESIZE_NEW_ROWS,
3166
+ MAX_ROW_HEIGHT,
3167
+ NUMBER_FORMAT_CODE_PROBLEM_KEYS,
3168
+ NUMBER_FORMAT_SAMPLE,
3169
+ SheetTabs,
3170
+ StatusMessage,
3171
+ XlsxEditor2 as XlsxEditor,
3172
+ XlsxEditorContent,
3173
+ XlsxEditorRoot,
3174
+ XlsxEditorToolbar,
3175
+ XlsxEditorToolbarGroup,
3176
+ XlsxEditorToolbarSlot,
3177
+ XlsxEditorViewport,
3178
+ ZoomControl,
3179
+ composesKey2 as composesKey,
3180
+ contentFieldText2 as contentFieldText,
3181
+ editMoveOf2 as editMoveOf,
3182
+ fieldHistoryKey,
3183
+ mergeCommandState,
3184
+ numberFormatChoiceCode,
3185
+ numberFormatChoices,
3186
+ numberFormatCodeProblem,
3187
+ numberFormatDialogApplyStep2 as numberFormatDialogApplyStep,
3188
+ numberFormatDialogCurrent2 as numberFormatDialogCurrent,
3189
+ numberFormatDialogStart2 as numberFormatDialogStart,
3190
+ numberFormatListView2 as numberFormatListView,
3191
+ numberFormatValue,
3192
+ previewNumberFormat2 as previewNumberFormat,
3193
+ refusalText4 as refusalText,
3194
+ refusesEditText2 as refusesEditText,
3195
+ refusesReference2 as refusesReference,
3196
+ sheetCommandState2 as sheetCommandState,
3197
+ translatorFor,
3198
+ trapTabWithin2 as trapTabWithin,
3199
+ useEditorColorMode,
3200
+ useEditorCommand,
3201
+ useEditorEvent,
3202
+ useEditorSnapshot,
3203
+ useEditorState,
3204
+ useScopeClassName,
3205
+ useTranslation,
3206
+ useViewModeWaits,
3207
+ useXlsxEditor,
3208
+ workbookNumberFormatCodes2 as workbookNumberFormatCodes,
3209
+ xlsxEditorKey,
3210
+ xlsxTranslationKey
3211
+ };