xlsx-svelte-viewer 0.7.0 → 1.1.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.
@@ -8,7 +8,8 @@
8
8
  <div use:themeBlock>
9
9
  <input aria-label={toolbar.labels['Name Box']} value={toolbar.selection} aria-invalid={invalidName}
10
10
  oninput={() => { invalidName = false; }} onkeydown={(e) => { if (e.key === 'Enter') { e.preventDefault(); invalidName = !selectReference(toolbar, e.currentTarget.value); } }} />
11
- <input aria-label={toolbar.labels['Formula Bar']} value={toolbar.formula} readonly={!toolbar.canEdit} aria-invalid={invalid}
11
+ <input aria-label={toolbar.labels['Formula Bar']} value={toolbar.formula} readonly={!toolbar.canEdit} aria-invalid={invalid || toolbar.editError !== null}
12
12
  oninput={(e) => { invalid = false; toolbar.onFormulaInput(e.currentTarget.value, e.currentTarget.selectionEnd ?? undefined); }}
13
13
  onkeydown={(e) => { if (e.key === 'Enter') { e.preventDefault(); invalid = !toolbar.onFormulaChange(e.currentTarget.value); } else if (e.key === 'Escape') { e.preventDefault(); toolbar.onCancelEdit(); invalid = false; } }} />
14
+ {#if toolbar.editError !== null}<span role="alert">{toolbar.editError}</span>{/if}
14
15
  </div>
@@ -1,12 +1,21 @@
1
1
  <script lang="ts">
2
2
  import { prepareBlock } from './runtime.js';
3
3
  const themeBlock = (host: HTMLElement) => prepareBlock(host, 'toolbar');
4
- import { toolbarButtons, type SpreadsheetToolbarProps } from './runtime.js';
4
+ import { toolbarModel, type SpreadsheetToolbarProps } from './runtime.js';
5
5
  let { toolbarProps }: { toolbarProps: SpreadsheetToolbarProps } = $props();
6
6
  </script>
7
7
  <div use:themeBlock role="toolbar">
8
- {#each toolbarButtons(toolbarProps) as button (button.label)}
9
- <button type="button" disabled={button.disabled} aria-pressed={button.pressed === null ? 'mixed' : button.pressed}
10
- onmousedown={(e) => e.preventDefault()} onclick={() => { void button.run(); }}>{button.label}</button>
8
+ {#each toolbarModel(toolbarProps) as item (item.id)}
9
+ {#if item.kind === 'select'}
10
+ <select aria-label={item.label} disabled={item.disabled} value={item.value}
11
+ onchange={(e) => { void item.change(e.currentTarget.value); }}>
12
+ {#each item.options as option (option.value)}
13
+ <option value={option.value} selected={option.value === item.value}>{option.label}</option>
14
+ {/each}
15
+ </select>
16
+ {:else}
17
+ <button type="button" disabled={item.disabled} aria-pressed={item.pressed === null ? 'mixed' : item.pressed}
18
+ onmousedown={(e) => e.preventDefault()} onclick={() => { void item.run(); }}>{item.label}</button>
19
+ {/if}
11
20
  {/each}
12
21
  </div>
package/dist/runtime.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as ooxml_core_xlsx from 'ooxml-core/xlsx';
2
2
  import * as ooxml_core_xlsx_ui from 'ooxml-core/xlsx/ui';
3
- import { EditorLocaleInput, EditorThemeMode, RibbonAddInTab, XlsxThemeColors, XlsxCollaborationOptions, SelectionChangeDetail, RibbonAddInCommandDetail, XlsxCollaborationState, XlsxEditorElement, SpreadsheetToolbarProps } from 'ooxml-ui/xlsx';
3
+ import * as ooxml_core_collab from 'ooxml-core/collab';
4
+ import { EditorLocaleInput, EditorThemeMode, RibbonAddInTab, XlsxThemeColors, XlsxCollaborationOptions, SelectionChangeDetail, RibbonAddInCommandDetail, XlsxCollaborationState, XlsxEditorElement, SurfaceHostActions, ValidationErrorDetail, SpreadsheetToolbarProps } from 'ooxml-ui/xlsx';
4
5
  export * from 'ooxml-ui/xlsx';
5
6
  export { SpreadsheetToolbarProps } from 'ooxml-ui/xlsx';
6
7
  import { Workbook } from '@christophervr/xlsx-core';
@@ -117,11 +118,14 @@ declare function mountEditor(host: HTMLElement, initial?: EditorOptions, surface
117
118
  declare const EDITOR_HANDLE_KEYS: readonly ["element", "load", "newWorkbook", "save", "saveBytes", "download", "markClean", "select", "getSelection", "setActiveSheet", "share", "dirty"];
118
119
  type EditorHandleKey = (typeof EDITOR_HANDLE_KEYS)[number];
119
120
  /** Framework-neutral composition API, shared by every adapter. Choose before mount. */
120
- interface SurfaceOptions extends EditorOptions {
121
+ interface SurfaceOptions extends EditorOptions, SurfaceHostActions {
121
122
  canEdit?: boolean | undefined;
123
+ /** A cell edit in progress failed validation before formatting, navigation or saving. */
124
+ onValidationError?: ((detail: ValidationErrorDetail) => void) | undefined;
122
125
  }
123
126
  declare function mountEditorSurface(host: HTMLElement, initial?: SurfaceOptions): EditorBinding & {
124
127
  update: (options: SurfaceOptions) => void;
128
+ destroy: () => void;
125
129
  getToolbarProps: () => {
126
130
  bold: boolean | null;
127
131
  italic: boolean | null;
@@ -137,12 +141,36 @@ declare function mountEditorSurface(host: HTMLElement, initial?: SurfaceOptions)
137
141
  underline: boolean;
138
142
  fontFamily: boolean;
139
143
  fontSize: boolean;
144
+ alignment: boolean;
140
145
  numberFormat: boolean;
146
+ undo: boolean;
147
+ redo: boolean;
148
+ openShareDialog: boolean;
149
+ settings: boolean;
150
+ formatCells: boolean;
151
+ findReplace: boolean;
152
+ insertFunction: boolean;
153
+ validationAlert: boolean;
141
154
  };
142
155
  labels: {
143
156
  [k: string]: string;
144
157
  };
158
+ options: {
159
+ fontFamilies: string[];
160
+ fontSizes: number[];
161
+ alignments: {
162
+ value: "center" | "left" | "right";
163
+ label: string;
164
+ }[];
165
+ numberFormats: {
166
+ value: string;
167
+ label: string;
168
+ }[];
169
+ };
145
170
  canEdit: boolean;
171
+ dirty: boolean;
172
+ collaboration: ooxml_core_collab.CollaborationState;
173
+ editError: string | null;
146
174
  selection: string;
147
175
  activeCell: string;
148
176
  activeSheet: number;
@@ -166,6 +194,11 @@ declare function mountEditorSurface(host: HTMLElement, initial?: SurfaceOptions)
166
194
  onAlignmentChange: (alignment: ooxml_core_xlsx.HorizontalAlignment) => Promise<boolean>;
167
195
  onUndo: () => Promise<boolean>;
168
196
  onRedo: () => Promise<boolean>;
197
+ onOpenShareDialog: () => Promise<boolean>;
198
+ onOpenSettings: () => Promise<boolean>;
199
+ onOpenFormatCells: () => Promise<boolean>;
200
+ onOpenFindReplace: () => Promise<boolean>;
201
+ onOpenInsertFunction: () => Promise<boolean>;
169
202
  onSelect: (ref: string) => boolean;
170
203
  onSheetChange: (index: number) => boolean;
171
204
  onFormulaInput: (text: string, caret?: number) => boolean;
@@ -182,13 +215,40 @@ interface BuildingBlockState {
182
215
  loading: boolean;
183
216
  error: Error | null;
184
217
  mode: 'view' | 'edit';
218
+ /** Unsaved changes since the last load, save or `markClean()`. */
219
+ dirty: boolean;
220
+ /** The selection as A1 text. */
221
+ selection: string;
222
+ /** Sharing state of the open workbook. */
223
+ collaboration: XlsxCollaborationState;
224
+ /** The validation message of the cell edit in progress, or null. */
225
+ editError: string | null;
185
226
  }
186
- declare function toolbarButtons(p: SpreadsheetToolbarProps): {
227
+ interface ToolbarButtonModel {
228
+ kind: 'button';
229
+ id: string;
187
230
  label: string;
231
+ /** true or false, or null for a mixed selection. */
188
232
  pressed: boolean | null;
189
233
  disabled: boolean;
190
234
  run: () => Promise<boolean>;
191
- }[];
235
+ }
236
+ interface ToolbarSelectModel {
237
+ kind: 'select';
238
+ id: string;
239
+ label: string;
240
+ /** The current value; '' when the selection is mixed. */
241
+ value: string;
242
+ disabled: boolean;
243
+ options: {
244
+ value: string;
245
+ label: string;
246
+ }[];
247
+ change: (value: string) => Promise<boolean>;
248
+ }
249
+ type ToolbarItemModel = ToolbarButtonModel | ToolbarSelectModel;
250
+ /** Every control the props support, in display order, for a framework to map onto its template. */
251
+ declare function toolbarModel(p: SpreadsheetToolbarProps): ToolbarItemModel[];
192
252
  declare function selectReference(p: SpreadsheetToolbarProps, value: string): boolean;
193
253
 
194
254
  type BuildingBlockKind = 'toolbar' | 'formula-bar' | 'tabs';
@@ -206,5 +266,5 @@ declare function createViewerBuildingBlocks(initial?: BuildingBlockOptions): {
206
266
  destroy: () => void;
207
267
  };
208
268
 
209
- export { EDITOR_EVENT_NAMES, EDITOR_HANDLE_KEYS, EDITOR_PROP_KEYS, createViewerBuildingBlocks, deferredHandle, eventOptions, mountEditor, mountEditorSurface, pickEditorProps, prepareBlock, selectReference, toolbarButtons };
269
+ export { EDITOR_EVENT_NAMES, EDITOR_HANDLE_KEYS, EDITOR_PROP_KEYS, createViewerBuildingBlocks, deferredHandle, eventOptions, mountEditor, mountEditorSurface, pickEditorProps, prepareBlock, selectReference, toolbarModel };
210
270
  export type { BuildingBlockOptions, BuildingBlockState, EditorBinding, EditorEventHandlers, EditorEventName, EditorEventOptions, EditorHandle, EditorHandleKey, EditorOptions, EditorPropKey, EditorProps, SurfaceOptions };
package/dist/runtime.js CHANGED
@@ -202,42 +202,77 @@ var M = [
202
202
  "setActiveSheet",
203
203
  "share",
204
204
  "dirty"
205
+ ], N = [
206
+ "onOpenShareDialog",
207
+ "onOpenSettings",
208
+ "onOpenFormatCells",
209
+ "onOpenFindReplace",
210
+ "onOpenInsertFunction",
211
+ "onOpenValidationAlert"
205
212
  ];
206
- function N(e, t = {}) {
207
- let n = (e) => ({
213
+ function P(e, t = {}) {
214
+ let n = t, r = (e) => ({
208
215
  ...e,
209
- readOnly: e.canEdit === !1 || e.readOnly === !0
210
- }), r = j(e, n(t), !0), i = r.update;
211
- return Object.assign(r, {
212
- update: (e) => i(n(e)),
213
- getToolbarProps: () => r.element.getToolbarProps(),
214
- subscribe: (e) => r.element.subscribeEditorState(e)
216
+ readOnly: e.canEdit !== !0 || e.readOnly === !0
217
+ }), i = j(e, r(t), !0), { element: a } = i, o = i.update, s = i.destroy, c = {}, l = (e) => {
218
+ if (N.every((t) => c[t] === e[t])) return;
219
+ let t = {};
220
+ for (let n of N) t[n] = e[n];
221
+ c = t, a.surfaceActions = t;
222
+ }, u = (e) => n.onValidationError?.(e.detail);
223
+ return a.addEventListener("validation-error", u), l(t), Object.assign(i, {
224
+ update: (e) => {
225
+ n = e, l(e), o(r(e));
226
+ },
227
+ destroy: () => {
228
+ a.removeEventListener("validation-error", u), s();
229
+ },
230
+ getToolbarProps: () => a.getToolbarProps(),
231
+ subscribe: (e) => a.subscribeEditorState(e)
215
232
  });
216
233
  }
217
234
  //#endregion
218
235
  //#region packages/bindings/src/common.ts
219
- var P = /* @__PURE__ */ h({
236
+ var F = /* @__PURE__ */ h({
220
237
  LegacyXlsError: () => e,
221
238
  csvToWorkbook: () => t,
222
239
  detectWorkbookFormat: () => n,
223
240
  loadLegacyXls: () => r,
224
241
  loadWorkbook: () => i,
225
- mountEditorSurface: () => N,
242
+ mountEditorSurface: () => P,
226
243
  parseCsv: () => a,
227
244
  saveWorkbook: () => o,
228
245
  sheetToCsv: () => s
229
246
  });
230
- _(P, v);
247
+ _(F, v);
231
248
  //#endregion
232
249
  //#region packages/bindings/src/building-blocks.ts
233
- function F(e) {
250
+ var I = Object.freeze({
251
+ active: !1,
252
+ roomId: void 0,
253
+ status: "off",
254
+ synced: !1,
255
+ people: []
256
+ });
257
+ function L(e) {
258
+ return {
259
+ dirty: e?.dirty ?? !1,
260
+ selection: e?.selection ?? "",
261
+ collaboration: e?.collaboration ?? I,
262
+ editError: e?.editError ?? null
263
+ };
264
+ }
265
+ function R(e) {
234
266
  let t = null, n, r, i = {}, a = null, o = !1, s = () => {
235
267
  let t = n?.element.loading ?? !1;
236
- t && !o && (a = null), o = t, e({
237
- toolbarProps: n?.getToolbarProps() ?? null,
268
+ t && !o && (a = null), o = t;
269
+ let r = n?.getToolbarProps() ?? null;
270
+ e({
271
+ toolbarProps: r,
238
272
  loading: n?.element.loading ?? !1,
239
273
  error: a,
240
- mode: i.canEdit === !1 || i.readOnly ? "view" : "edit"
274
+ mode: i.canEdit !== !0 || i.readOnly ? "view" : "edit",
275
+ ...L(r)
241
276
  });
242
277
  }, c = () => ({
243
278
  ...i,
@@ -250,7 +285,7 @@ function F(e) {
250
285
  return {
251
286
  handle: A(() => n),
252
287
  mount(e) {
253
- e && e === t || (l(), e && (a = null, n = N(e, c()), t = e, r = n.subscribe(s), s()));
288
+ e && e === t || (l(), e && (a = null, n = P(e, c()), t = e, r = n.subscribe(s), s()));
254
289
  },
255
290
  update(e) {
256
291
  (e.workbook !== i.workbook || e.bytes !== i.bytes || e.src !== i.src) && (a = null), i = e, n?.update(c()), s();
@@ -258,47 +293,71 @@ function F(e) {
258
293
  destroy: l
259
294
  };
260
295
  }
261
- var I = () => ({
296
+ var z = () => ({
262
297
  toolbarProps: null,
263
298
  loading: !1,
264
299
  error: null,
265
- mode: "edit"
266
- });
267
- function L(e) {
300
+ mode: "view",
301
+ ...L(null)
302
+ }), B = (e, t) => t === null ? [{
303
+ value: "",
304
+ label: ""
305
+ }, ...e] : e.some((e) => e.value === t) ? e : [{
306
+ value: t,
307
+ label: t
308
+ }, ...e];
309
+ function V(e) {
310
+ let t = (t) => e.labels[t] ?? t, n = (t) => !e.canEdit || !t, r = (e, t, n, r, i) => ({
311
+ kind: "button",
312
+ id: e,
313
+ label: t,
314
+ pressed: n,
315
+ disabled: r,
316
+ run: i
317
+ });
268
318
  return [
319
+ r("undo", t("Undo"), !1, !e.enabled.undo, e.onUndo),
320
+ r("redo", t("Redo"), !1, !e.enabled.redo, e.onRedo),
269
321
  {
270
- label: e.labels.Bold ?? "Bold",
271
- pressed: e.bold,
272
- disabled: !e.canEdit || !e.enabled.bold,
273
- run: e.onBold
274
- },
275
- {
276
- label: e.labels.Italic ?? "Italic",
277
- pressed: e.italic,
278
- disabled: !e.canEdit || !e.enabled.italic,
279
- run: e.onItalic
280
- },
281
- {
282
- label: e.labels.Underline ?? "Underline",
283
- pressed: e.underline,
284
- disabled: !e.canEdit || !e.enabled.underline,
285
- run: e.onUnderline
322
+ kind: "select",
323
+ id: "fontFamily",
324
+ label: t("Font"),
325
+ value: e.fontFamily ?? "",
326
+ disabled: n(e.enabled.fontFamily),
327
+ options: B(e.options.fontFamilies.map((e) => ({
328
+ value: e,
329
+ label: e
330
+ })), e.fontFamily),
331
+ change: (t) => e.onFontFamilyChange(t)
286
332
  },
287
333
  {
288
- label: e.labels.Undo ?? "Undo",
289
- pressed: !1,
290
- disabled: !e.canUndo,
291
- run: e.onUndo
334
+ kind: "select",
335
+ id: "fontSize",
336
+ label: t("Font Size"),
337
+ value: e.fontSize === null ? "" : String(e.fontSize),
338
+ disabled: n(e.enabled.fontSize),
339
+ options: B(e.options.fontSizes.map((e) => ({
340
+ value: String(e),
341
+ label: String(e)
342
+ })), e.fontSize === null ? null : String(e.fontSize)),
343
+ change: (t) => e.onFontSizeChange(Number(t))
292
344
  },
345
+ r("bold", t("Bold"), e.bold, n(e.enabled.bold), e.onBold),
346
+ r("italic", t("Italic"), e.italic, n(e.enabled.italic), e.onItalic),
347
+ r("underline", t("Underline"), e.underline, n(e.enabled.underline), e.onUnderline),
348
+ ...e.options.alignments.map((t) => r(`align-${t.value}`, t.label, e.alignment === null ? null : e.alignment === t.value, n(e.enabled.alignment), () => e.onAlignmentChange(t.value))),
293
349
  {
294
- label: e.labels.Redo ?? "Redo",
295
- pressed: !1,
296
- disabled: !e.canRedo,
297
- run: e.onRedo
350
+ kind: "select",
351
+ id: "numberFormat",
352
+ label: t("Number Format"),
353
+ value: e.numberFormat ?? "",
354
+ disabled: n(e.enabled.numberFormat),
355
+ options: B(e.options.numberFormats, e.numberFormat),
356
+ change: (t) => e.onNumberFormatChange(t)
298
357
  }
299
358
  ];
300
359
  }
301
- function R(e, t) {
360
+ function H(e, t) {
302
361
  try {
303
362
  return e.onSelect(t);
304
363
  } catch {
@@ -307,15 +366,15 @@ function R(e, t) {
307
366
  }
308
367
  //#endregion
309
368
  //#region packages/bindings/src/block-theme.ts
310
- function z(e, t) {
369
+ function U(e, t) {
311
370
  if (!e || typeof e != "object" || !("ownerDocument" in e)) return;
312
371
  let n = e, r = Reflect.get(u, "prepareOfficeUiBlock");
313
372
  typeof r == "function" ? r(n, t, "xlsx") : (u.installOfficeUiTheme(n.ownerDocument), n.setAttribute("data-office-block", t), n.setAttribute("data-office-product", "xlsx"));
314
373
  }
315
374
  //#endregion
316
375
  //#region packages/svelte/src/building-blocks.ts
317
- function B(e = {}) {
318
- let t = l(I()), n = F(t.set);
376
+ function W(e = {}) {
377
+ let t = l(z()), n = R(t.set);
319
378
  return n.update(e), c(n.destroy), {
320
379
  subscribe: t.subscribe,
321
380
  update: n.update,
@@ -329,7 +388,7 @@ _(/* @__PURE__ */ h({
329
388
  EDITOR_HANDLE_KEYS: () => M,
330
389
  EDITOR_PROP_KEYS: () => T,
331
390
  LegacyXlsError: () => e,
332
- createViewerBuildingBlocks: () => B,
391
+ createViewerBuildingBlocks: () => W,
333
392
  csvToWorkbook: () => t,
334
393
  deferredHandle: () => A,
335
394
  detectWorkbookFormat: () => n,
@@ -337,14 +396,14 @@ _(/* @__PURE__ */ h({
337
396
  loadLegacyXls: () => r,
338
397
  loadWorkbook: () => i,
339
398
  mountEditor: () => j,
340
- mountEditorSurface: () => N,
399
+ mountEditorSurface: () => P,
341
400
  parseCsv: () => a,
342
401
  pickEditorProps: () => O,
343
- prepareBlock: () => z,
402
+ prepareBlock: () => U,
344
403
  saveWorkbook: () => o,
345
- selectReference: () => R,
404
+ selectReference: () => H,
346
405
  sheetToCsv: () => s,
347
- toolbarButtons: () => L
348
- }), P);
406
+ toolbarModel: () => V
407
+ }), F);
349
408
  //#endregion
350
- export { E as EDITOR_EVENT_NAMES, M as EDITOR_HANDLE_KEYS, T as EDITOR_PROP_KEYS, e as LegacyXlsError, B as createViewerBuildingBlocks, t as csvToWorkbook, A as deferredHandle, n as detectWorkbookFormat, k as eventOptions, r as loadLegacyXls, i as loadWorkbook, j as mountEditor, N as mountEditorSurface, a as parseCsv, O as pickEditorProps, z as prepareBlock, o as saveWorkbook, R as selectReference, s as sheetToCsv, L as toolbarButtons };
409
+ export { E as EDITOR_EVENT_NAMES, M as EDITOR_HANDLE_KEYS, T as EDITOR_PROP_KEYS, e as LegacyXlsError, W as createViewerBuildingBlocks, t as csvToWorkbook, A as deferredHandle, n as detectWorkbookFormat, k as eventOptions, r as loadLegacyXls, i as loadWorkbook, j as mountEditor, P as mountEditorSurface, a as parseCsv, O as pickEditorProps, U as prepareBlock, o as saveWorkbook, H as selectReference, s as sheetToCsv, V as toolbarModel };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xlsx-svelte-viewer",
3
- "version": "0.7.0",
3
+ "version": "1.1.0",
4
4
  "description": "Svelte 5 component for the Excel spreadsheet editor. Self-contained: includes the editor, the formula engine and the legacy .xls reader.",
5
5
  "keywords": [
6
6
  "editor",
@@ -67,8 +67,8 @@
67
67
  },
68
68
  "dependencies": {
69
69
  "@christophervr/xlsx-core": "^0.3.0",
70
- "ooxml-core": "^1.20.0",
71
- "ooxml-ui": "^1.19.0"
70
+ "ooxml-core": "^1.23.0",
71
+ "ooxml-ui": "^1.22.0"
72
72
  },
73
73
  "peerDependencies": {
74
74
  "svelte": "^5.0.0"