xlsx-svelte-viewer 0.7.0 → 1.0.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,35 @@ 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;
141
153
  };
142
154
  labels: {
143
155
  [k: string]: string;
144
156
  };
157
+ options: {
158
+ fontFamilies: string[];
159
+ fontSizes: number[];
160
+ alignments: {
161
+ value: "center" | "left" | "right";
162
+ label: string;
163
+ }[];
164
+ numberFormats: {
165
+ value: string;
166
+ label: string;
167
+ }[];
168
+ };
145
169
  canEdit: boolean;
170
+ dirty: boolean;
171
+ collaboration: ooxml_core_collab.CollaborationState;
172
+ editError: string | null;
146
173
  selection: string;
147
174
  activeCell: string;
148
175
  activeSheet: number;
@@ -166,6 +193,11 @@ declare function mountEditorSurface(host: HTMLElement, initial?: SurfaceOptions)
166
193
  onAlignmentChange: (alignment: ooxml_core_xlsx.HorizontalAlignment) => Promise<boolean>;
167
194
  onUndo: () => Promise<boolean>;
168
195
  onRedo: () => Promise<boolean>;
196
+ onOpenShareDialog: () => Promise<boolean>;
197
+ onOpenSettings: () => Promise<boolean>;
198
+ onOpenFormatCells: () => Promise<boolean>;
199
+ onOpenFindReplace: () => Promise<boolean>;
200
+ onOpenInsertFunction: () => Promise<boolean>;
169
201
  onSelect: (ref: string) => boolean;
170
202
  onSheetChange: (index: number) => boolean;
171
203
  onFormulaInput: (text: string, caret?: number) => boolean;
@@ -182,13 +214,40 @@ interface BuildingBlockState {
182
214
  loading: boolean;
183
215
  error: Error | null;
184
216
  mode: 'view' | 'edit';
217
+ /** Unsaved changes since the last load, save or `markClean()`. */
218
+ dirty: boolean;
219
+ /** The selection as A1 text. */
220
+ selection: string;
221
+ /** Sharing state of the open workbook. */
222
+ collaboration: XlsxCollaborationState;
223
+ /** The validation message of the cell edit in progress, or null. */
224
+ editError: string | null;
185
225
  }
186
- declare function toolbarButtons(p: SpreadsheetToolbarProps): {
226
+ interface ToolbarButtonModel {
227
+ kind: 'button';
228
+ id: string;
187
229
  label: string;
230
+ /** true or false, or null for a mixed selection. */
188
231
  pressed: boolean | null;
189
232
  disabled: boolean;
190
233
  run: () => Promise<boolean>;
191
- }[];
234
+ }
235
+ interface ToolbarSelectModel {
236
+ kind: 'select';
237
+ id: string;
238
+ label: string;
239
+ /** The current value; '' when the selection is mixed. */
240
+ value: string;
241
+ disabled: boolean;
242
+ options: {
243
+ value: string;
244
+ label: string;
245
+ }[];
246
+ change: (value: string) => Promise<boolean>;
247
+ }
248
+ type ToolbarItemModel = ToolbarButtonModel | ToolbarSelectModel;
249
+ /** Every control the props support, in display order, for a framework to map onto its template. */
250
+ declare function toolbarModel(p: SpreadsheetToolbarProps): ToolbarItemModel[];
192
251
  declare function selectReference(p: SpreadsheetToolbarProps, value: string): boolean;
193
252
 
194
253
  type BuildingBlockKind = 'toolbar' | 'formula-bar' | 'tabs';
@@ -206,5 +265,5 @@ declare function createViewerBuildingBlocks(initial?: BuildingBlockOptions): {
206
265
  destroy: () => void;
207
266
  };
208
267
 
209
- export { EDITOR_EVENT_NAMES, EDITOR_HANDLE_KEYS, EDITOR_PROP_KEYS, createViewerBuildingBlocks, deferredHandle, eventOptions, mountEditor, mountEditorSurface, pickEditorProps, prepareBlock, selectReference, toolbarButtons };
268
+ export { EDITOR_EVENT_NAMES, EDITOR_HANDLE_KEYS, EDITOR_PROP_KEYS, createViewerBuildingBlocks, deferredHandle, eventOptions, mountEditor, mountEditorSurface, pickEditorProps, prepareBlock, selectReference, toolbarModel };
210
269
  export type { BuildingBlockOptions, BuildingBlockState, EditorBinding, EditorEventHandlers, EditorEventName, EditorEventOptions, EditorHandle, EditorHandleKey, EditorOptions, EditorPropKey, EditorProps, SurfaceOptions };
package/dist/runtime.js CHANGED
@@ -202,42 +202,76 @@ var M = [
202
202
  "setActiveSheet",
203
203
  "share",
204
204
  "dirty"
205
+ ], N = [
206
+ "onOpenShareDialog",
207
+ "onOpenSettings",
208
+ "onOpenFormatCells",
209
+ "onOpenFindReplace",
210
+ "onOpenInsertFunction"
205
211
  ];
206
- function N(e, t = {}) {
207
- let n = (e) => ({
212
+ function P(e, t = {}) {
213
+ let n = t, r = (e) => ({
208
214
  ...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)
215
+ readOnly: e.canEdit !== !0 || e.readOnly === !0
216
+ }), i = j(e, r(t), !0), { element: a } = i, o = i.update, s = i.destroy, c = {}, l = (e) => {
217
+ if (N.every((t) => c[t] === e[t])) return;
218
+ let t = {};
219
+ for (let n of N) t[n] = e[n];
220
+ c = t, a.surfaceActions = t;
221
+ }, u = (e) => n.onValidationError?.(e.detail);
222
+ return a.addEventListener("validation-error", u), l(t), Object.assign(i, {
223
+ update: (e) => {
224
+ n = e, l(e), o(r(e));
225
+ },
226
+ destroy: () => {
227
+ a.removeEventListener("validation-error", u), s();
228
+ },
229
+ getToolbarProps: () => a.getToolbarProps(),
230
+ subscribe: (e) => a.subscribeEditorState(e)
215
231
  });
216
232
  }
217
233
  //#endregion
218
234
  //#region packages/bindings/src/common.ts
219
- var P = /* @__PURE__ */ h({
235
+ var F = /* @__PURE__ */ h({
220
236
  LegacyXlsError: () => e,
221
237
  csvToWorkbook: () => t,
222
238
  detectWorkbookFormat: () => n,
223
239
  loadLegacyXls: () => r,
224
240
  loadWorkbook: () => i,
225
- mountEditorSurface: () => N,
241
+ mountEditorSurface: () => P,
226
242
  parseCsv: () => a,
227
243
  saveWorkbook: () => o,
228
244
  sheetToCsv: () => s
229
245
  });
230
- _(P, v);
246
+ _(F, v);
231
247
  //#endregion
232
248
  //#region packages/bindings/src/building-blocks.ts
233
- function F(e) {
249
+ var I = Object.freeze({
250
+ active: !1,
251
+ roomId: void 0,
252
+ status: "off",
253
+ synced: !1,
254
+ people: []
255
+ });
256
+ function L(e) {
257
+ return {
258
+ dirty: e?.dirty ?? !1,
259
+ selection: e?.selection ?? "",
260
+ collaboration: e?.collaboration ?? I,
261
+ editError: e?.editError ?? null
262
+ };
263
+ }
264
+ function R(e) {
234
265
  let t = null, n, r, i = {}, a = null, o = !1, s = () => {
235
266
  let t = n?.element.loading ?? !1;
236
- t && !o && (a = null), o = t, e({
237
- toolbarProps: n?.getToolbarProps() ?? null,
267
+ t && !o && (a = null), o = t;
268
+ let r = n?.getToolbarProps() ?? null;
269
+ e({
270
+ toolbarProps: r,
238
271
  loading: n?.element.loading ?? !1,
239
272
  error: a,
240
- mode: i.canEdit === !1 || i.readOnly ? "view" : "edit"
273
+ mode: i.canEdit !== !0 || i.readOnly ? "view" : "edit",
274
+ ...L(r)
241
275
  });
242
276
  }, c = () => ({
243
277
  ...i,
@@ -250,7 +284,7 @@ function F(e) {
250
284
  return {
251
285
  handle: A(() => n),
252
286
  mount(e) {
253
- e && e === t || (l(), e && (a = null, n = N(e, c()), t = e, r = n.subscribe(s), s()));
287
+ e && e === t || (l(), e && (a = null, n = P(e, c()), t = e, r = n.subscribe(s), s()));
254
288
  },
255
289
  update(e) {
256
290
  (e.workbook !== i.workbook || e.bytes !== i.bytes || e.src !== i.src) && (a = null), i = e, n?.update(c()), s();
@@ -258,47 +292,71 @@ function F(e) {
258
292
  destroy: l
259
293
  };
260
294
  }
261
- var I = () => ({
295
+ var z = () => ({
262
296
  toolbarProps: null,
263
297
  loading: !1,
264
298
  error: null,
265
- mode: "edit"
266
- });
267
- function L(e) {
299
+ mode: "view",
300
+ ...L(null)
301
+ }), B = (e, t) => t === null ? [{
302
+ value: "",
303
+ label: ""
304
+ }, ...e] : e.some((e) => e.value === t) ? e : [{
305
+ value: t,
306
+ label: t
307
+ }, ...e];
308
+ function V(e) {
309
+ let t = (t) => e.labels[t] ?? t, n = (t) => !e.canEdit || !t, r = (e, t, n, r, i) => ({
310
+ kind: "button",
311
+ id: e,
312
+ label: t,
313
+ pressed: n,
314
+ disabled: r,
315
+ run: i
316
+ });
268
317
  return [
318
+ r("undo", t("Undo"), !1, !e.enabled.undo, e.onUndo),
319
+ r("redo", t("Redo"), !1, !e.enabled.redo, e.onRedo),
269
320
  {
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
321
+ kind: "select",
322
+ id: "fontFamily",
323
+ label: t("Font"),
324
+ value: e.fontFamily ?? "",
325
+ disabled: n(e.enabled.fontFamily),
326
+ options: B(e.options.fontFamilies.map((e) => ({
327
+ value: e,
328
+ label: e
329
+ })), e.fontFamily),
330
+ change: (t) => e.onFontFamilyChange(t)
286
331
  },
287
332
  {
288
- label: e.labels.Undo ?? "Undo",
289
- pressed: !1,
290
- disabled: !e.canUndo,
291
- run: e.onUndo
333
+ kind: "select",
334
+ id: "fontSize",
335
+ label: t("Font Size"),
336
+ value: e.fontSize === null ? "" : String(e.fontSize),
337
+ disabled: n(e.enabled.fontSize),
338
+ options: B(e.options.fontSizes.map((e) => ({
339
+ value: String(e),
340
+ label: String(e)
341
+ })), e.fontSize === null ? null : String(e.fontSize)),
342
+ change: (t) => e.onFontSizeChange(Number(t))
292
343
  },
344
+ r("bold", t("Bold"), e.bold, n(e.enabled.bold), e.onBold),
345
+ r("italic", t("Italic"), e.italic, n(e.enabled.italic), e.onItalic),
346
+ r("underline", t("Underline"), e.underline, n(e.enabled.underline), e.onUnderline),
347
+ ...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
348
  {
294
- label: e.labels.Redo ?? "Redo",
295
- pressed: !1,
296
- disabled: !e.canRedo,
297
- run: e.onRedo
349
+ kind: "select",
350
+ id: "numberFormat",
351
+ label: t("Number Format"),
352
+ value: e.numberFormat ?? "",
353
+ disabled: n(e.enabled.numberFormat),
354
+ options: B(e.options.numberFormats, e.numberFormat),
355
+ change: (t) => e.onNumberFormatChange(t)
298
356
  }
299
357
  ];
300
358
  }
301
- function R(e, t) {
359
+ function H(e, t) {
302
360
  try {
303
361
  return e.onSelect(t);
304
362
  } catch {
@@ -307,15 +365,15 @@ function R(e, t) {
307
365
  }
308
366
  //#endregion
309
367
  //#region packages/bindings/src/block-theme.ts
310
- function z(e, t) {
368
+ function U(e, t) {
311
369
  if (!e || typeof e != "object" || !("ownerDocument" in e)) return;
312
370
  let n = e, r = Reflect.get(u, "prepareOfficeUiBlock");
313
371
  typeof r == "function" ? r(n, t, "xlsx") : (u.installOfficeUiTheme(n.ownerDocument), n.setAttribute("data-office-block", t), n.setAttribute("data-office-product", "xlsx"));
314
372
  }
315
373
  //#endregion
316
374
  //#region packages/svelte/src/building-blocks.ts
317
- function B(e = {}) {
318
- let t = l(I()), n = F(t.set);
375
+ function W(e = {}) {
376
+ let t = l(z()), n = R(t.set);
319
377
  return n.update(e), c(n.destroy), {
320
378
  subscribe: t.subscribe,
321
379
  update: n.update,
@@ -329,7 +387,7 @@ _(/* @__PURE__ */ h({
329
387
  EDITOR_HANDLE_KEYS: () => M,
330
388
  EDITOR_PROP_KEYS: () => T,
331
389
  LegacyXlsError: () => e,
332
- createViewerBuildingBlocks: () => B,
390
+ createViewerBuildingBlocks: () => W,
333
391
  csvToWorkbook: () => t,
334
392
  deferredHandle: () => A,
335
393
  detectWorkbookFormat: () => n,
@@ -337,14 +395,14 @@ _(/* @__PURE__ */ h({
337
395
  loadLegacyXls: () => r,
338
396
  loadWorkbook: () => i,
339
397
  mountEditor: () => j,
340
- mountEditorSurface: () => N,
398
+ mountEditorSurface: () => P,
341
399
  parseCsv: () => a,
342
400
  pickEditorProps: () => O,
343
- prepareBlock: () => z,
401
+ prepareBlock: () => U,
344
402
  saveWorkbook: () => o,
345
- selectReference: () => R,
403
+ selectReference: () => H,
346
404
  sheetToCsv: () => s,
347
- toolbarButtons: () => L
348
- }), P);
405
+ toolbarModel: () => V
406
+ }), F);
349
407
  //#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 };
408
+ 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.0.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.22.0",
71
+ "ooxml-ui": "^1.21.0"
72
72
  },
73
73
  "peerDependencies": {
74
74
  "svelte": "^5.0.0"