@sheetwrite/svelte 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -35,12 +35,25 @@ WASM initializes on client mount. Use `fallback` while loading, `onInitializatio
35
35
  ## Advanced component
36
36
 
37
37
  ```svelte
38
- <SheetwriteGrid bind:grid {workbook} {data} fill />
38
+ <SheetwriteGrid
39
+ bind:grid
40
+ {workbook}
41
+ {data}
42
+ presentation="data-grid"
43
+ {editors}
44
+ fill
45
+ />
39
46
  ```
40
47
 
41
- The bindable `grid` is published before `onReady({ grid, generation, reason })` and clears during replacement or unmount. Reset-bound inputs replace the generation; presentation props update live.
48
+ The bindable `grid` is published before
49
+ `onReady({ grid, generation, reason })` and clears during replacement or
50
+ unmount. Reset-bound inputs are `workbook`, `data`, `datasource`,
51
+ `datasourceStorage`, `presentation`, `editors`, `protectionResolver`,
52
+ `mutationPolicy`, `transactionResourceLimits`, `renderer`, `workerUrl`, and
53
+ `renderers`; `theme`, `readOnly`, `config`, `overscan`, and `minColumns` update
54
+ live.
42
55
 
43
- Grid events cover changes, viewport, selection, editing, search, and active-sheet changes.
56
+ Grid callbacks are `onGridChange`, `onViewportChange`, `onSelectionChange`, `onEditBegin`, `onEditCommit`, `onSearch`, `onCommandStateChange`, `onActiveSheetChange`, `onMutationRejected`, `onRendererFallback`, `onDatasourceError`, and `onExportError`.
44
57
 
45
58
  For vanilla/preload control, use `initSheetwrite()` and `createGrid()` from `@sheetwrite/core`. See [installation](https://sheetwrite.vercel.app/docs/start/installation/), [Svelte integration](https://sheetwrite.vercel.app/docs/frameworks/svelte/), and [collaboration](https://sheetwrite.vercel.app/docs/guides/collaboration/).
46
59
 
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@sheetwrite/svelte",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "Svelte adapter for Sheetwrite.",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/chh-ay/Sheetwrite.git",
9
+ "url": "git+https://github.com/chh-ay/sheetwrite.git",
10
10
  "directory": "packages/svelte"
11
11
  },
12
- "homepage": "https://chh-ay.github.io/Sheetwrite/",
12
+ "homepage": "https://sheetwrite.vercel.app/",
13
13
  "bugs": {
14
- "url": "https://github.com/chh-ay/Sheetwrite/issues"
14
+ "url": "https://github.com/chh-ay/sheetwrite/issues"
15
15
  },
16
16
  "keywords": [
17
17
  "spreadsheet",
@@ -44,7 +44,7 @@
44
44
  "svelte": ">=5"
45
45
  },
46
46
  "dependencies": {
47
- "@sheetwrite/core": "0.1.0"
47
+ "@sheetwrite/core": "0.3.0"
48
48
  },
49
49
  "devDependencies": {
50
50
  "svelte-check": "4.7.3",
package/src/Grid.svelte CHANGED
@@ -1,5 +1,11 @@
1
1
  <script lang="ts">
2
- import { initSheetwrite, isSheetwriteReady, type GridOptions } from "@sheetwrite/core";
2
+ import {
3
+ initSheetwrite,
4
+ isSheetwriteError,
5
+ isSheetwriteReady,
6
+ SheetwriteError,
7
+ type GridOptions,
8
+ } from "@sheetwrite/core";
3
9
  import {
4
10
  createGridController,
5
11
  getGridResetReason,
@@ -18,24 +24,35 @@ let {
18
24
  renderer = "canvas",
19
25
  workerUrl,
20
26
  theme,
27
+ presentation,
21
28
  readOnly,
22
29
  protectionResolver,
23
30
  mutationPolicy,
31
+ transactionResourceLimits,
32
+ hyperlinkActivation,
24
33
  renderers,
34
+ editors,
25
35
  overscan,
26
36
  minColumns,
27
37
  config,
28
38
  wasmSource,
29
39
  height,
30
40
  fill,
41
+ rowBridge,
31
42
  fallback,
32
43
  onGridChange,
44
+ onRowDelta,
33
45
  onSelectionChange,
34
46
  onViewportChange,
35
47
  onEditBegin,
36
48
  onEditCommit,
37
49
  onSearch,
38
50
  onActiveSheetChange,
51
+ onCommandStateChange,
52
+ onMutationRejected,
53
+ onRendererFallback,
54
+ onDatasourceError,
55
+ onExportError,
39
56
  onReady,
40
57
  onInitializationError,
41
58
  grid = $bindable(),
@@ -49,6 +66,7 @@ let generation = 0;
49
66
 
50
67
  const handlers = {
51
68
  onGridChange: (event: Parameters<NonNullable<typeof onGridChange>>[0]) => onGridChange?.(event),
69
+ onRowDelta: (event: Parameters<NonNullable<typeof onRowDelta>>[0]) => onRowDelta?.(event),
52
70
  onSelectionChange: (event: Parameters<NonNullable<typeof onSelectionChange>>[0]) =>
53
71
  onSelectionChange?.(event),
54
72
  onViewportChange: (event: Parameters<NonNullable<typeof onViewportChange>>[0]) =>
@@ -58,15 +76,43 @@ const handlers = {
58
76
  onSearch: (event: Parameters<NonNullable<typeof onSearch>>[0]) => onSearch?.(event),
59
77
  onActiveSheetChange: (event: Parameters<NonNullable<typeof onActiveSheetChange>>[0]) =>
60
78
  onActiveSheetChange?.(event),
79
+ onCommandStateChange: (event: Parameters<NonNullable<typeof onCommandStateChange>>[0]) =>
80
+ onCommandStateChange?.(event),
81
+ onMutationRejected: (event: Parameters<NonNullable<typeof onMutationRejected>>[0]) =>
82
+ onMutationRejected?.(event),
83
+ onRendererFallback: (event: Parameters<NonNullable<typeof onRendererFallback>>[0]) =>
84
+ onRendererFallback?.(event),
85
+ onDatasourceError: (event: Parameters<NonNullable<typeof onDatasourceError>>[0]) =>
86
+ onDatasourceError?.(event),
87
+ onExportError: (event: Parameters<NonNullable<typeof onExportError>>[0]) =>
88
+ onExportError?.(event),
61
89
  };
62
90
 
63
91
  const UNSET_WASM_SOURCE = Symbol("unset-wasm-source");
64
92
  let previousOptions: GridOptions | null = null;
65
93
  let lastRequestedOptions: GridOptions | null = null;
94
+ let previousRowBridge: Props["rowBridge"] | undefined;
66
95
  let previousWasmSource: Props["wasmSource"] | typeof UNSET_WASM_SOURCE = UNSET_WASM_SOURCE;
67
96
  let initializationToken = 0;
68
97
  let disposed = false;
69
98
 
99
+ function normalizeInitializationError(error: unknown): SheetwriteError {
100
+ if (error instanceof SheetwriteError) return error;
101
+ if (isSheetwriteError(error)) {
102
+ return new SheetwriteError(error.code, error.operation, error.message, {
103
+ cause: error,
104
+ context: error.context,
105
+ retryable: error.retryable,
106
+ });
107
+ }
108
+ return new SheetwriteError(
109
+ "initialization-failed",
110
+ "initialize",
111
+ error instanceof Error ? error.message : "Sheetwrite initialize failed",
112
+ { cause: error },
113
+ );
114
+ }
115
+
70
116
  function teardownGrid(): void {
71
117
  const active = untrack(() => controller);
72
118
  grid = undefined;
@@ -81,11 +127,12 @@ function publishReadyGrid(): void {
81
127
  generation === 0
82
128
  ? "initial"
83
129
  : (previousOptions && getGridResetReason(previousOptions, options)) ?? "input-reset";
84
- const active = untrack(() => createGridController(host, options, handlers));
130
+ const active = untrack(() => createGridController(host, options, handlers, rowBridge));
85
131
  controller = active;
86
132
  grid = active.grid;
87
133
  previousOptions = options;
88
134
  generation += 1;
135
+ previousRowBridge = rowBridge;
89
136
  loading = false;
90
137
  untrack(() => onReady?.({ grid: active.grid, generation, reason }));
91
138
  }
@@ -98,9 +145,14 @@ $effect(() => {
98
145
  datasourceStorage,
99
146
  renderer,
100
147
  workerUrl,
148
+ presentation,
101
149
  protectionResolver,
102
150
  mutationPolicy,
151
+ transactionResourceLimits,
152
+ hyperlinkActivation,
103
153
  renderers,
154
+ editors,
155
+ rowBridge,
104
156
  wasmSource,
105
157
  };
106
158
  const currentOptions = (): GridOptions =>
@@ -111,9 +163,13 @@ $effect(() => {
111
163
  datasourceStorage: resetInputs.datasourceStorage,
112
164
  renderer: resetInputs.renderer,
113
165
  workerUrl: resetInputs.workerUrl,
166
+ presentation: resetInputs.presentation,
114
167
  protectionResolver: resetInputs.protectionResolver,
115
168
  mutationPolicy: resetInputs.mutationPolicy,
169
+ transactionResourceLimits: resetInputs.transactionResourceLimits,
170
+ hyperlinkActivation: resetInputs.hyperlinkActivation,
116
171
  renderers: resetInputs.renderers,
172
+ editors: resetInputs.editors,
117
173
  theme,
118
174
  readOnly,
119
175
  overscan,
@@ -125,7 +181,8 @@ $effect(() => {
125
181
  const wasmChanged =
126
182
  previousWasmSource === UNSET_WASM_SOURCE || previousWasmSource !== resetInputs.wasmSource;
127
183
  const resetReason =
128
- lastRequestedOptions && getGridResetReason(lastRequestedOptions, requestedOptions);
184
+ (lastRequestedOptions && getGridResetReason(lastRequestedOptions, requestedOptions)) ??
185
+ (previousRowBridge !== rowBridge ? "input-reset" : null);
129
186
  const sourceOnlyChange = wasmChanged && resetReason === null && activeController !== undefined;
130
187
  const needsNewGeneration =
131
188
  lastRequestedOptions === null ||
@@ -136,9 +193,10 @@ $effect(() => {
136
193
  lastRequestedOptions = requestedOptions;
137
194
  const token = ++initializationToken;
138
195
  if (sourceOnlyChange) {
139
- void initSheetwrite(resetInputs.wasmSource).catch((error: unknown) => {
196
+ void initSheetwrite(resetInputs.wasmSource).then(undefined, (error: unknown) => {
140
197
  if (!disposed && token === initializationToken) {
141
- untrack(() => onInitializationError?.(error));
198
+ const initializationError = normalizeInitializationError(error);
199
+ untrack(() => onInitializationError?.(initializationError));
142
200
  }
143
201
  });
144
202
  return;
@@ -148,19 +206,26 @@ $effect(() => {
148
206
  teardownGrid();
149
207
  loading = !isSheetwriteReady();
150
208
 
151
- void (async () => {
152
- const alreadyReady = isSheetwriteReady();
153
- try {
154
- const initialization = initSheetwrite(resetInputs.wasmSource);
155
- if (alreadyReady) publishReadyGrid();
156
- await initialization;
157
- if (!alreadyReady && !disposed && isSheetwriteReady()) publishReadyGrid();
158
- } catch (error) {
209
+ const alreadyReady = isSheetwriteReady();
210
+ const initialization = initSheetwrite(resetInputs.wasmSource);
211
+ if (alreadyReady) publishReadyGrid();
212
+ void initialization.then(
213
+ () => {
214
+ if (!alreadyReady && !disposed && isSheetwriteReady()) {
215
+ try {
216
+ publishReadyGrid();
217
+ } catch (error) {
218
+ reportError(error);
219
+ }
220
+ }
221
+ },
222
+ (error: unknown) => {
159
223
  if (disposed || token !== initializationToken) return;
160
224
  loading = true;
161
- untrack(() => onInitializationError?.(error));
162
- }
163
- })();
225
+ const initializationError = normalizeInitializationError(error);
226
+ untrack(() => onInitializationError?.(initializationError));
227
+ },
228
+ );
164
229
  });
165
230
 
166
231
  $effect(() => {
@@ -1,13 +1,34 @@
1
- <script lang="ts" generics="Row extends Record<string, CellScalar>">
1
+ <script lang="ts" generics="Row extends Record<string, CellScalar>, Id extends RowBridgeId = RowBridgeId">
2
2
  import type { CellScalar } from "@sheetwrite/core";
3
- import { createSimpleGridInput } from "@sheetwrite/core/adapter";
3
+ import { createSimpleGridInput, createSimpleRowBridge } from "@sheetwrite/core/adapter";
4
+ import type { RowBridgeId } from "@sheetwrite/core/adapter";
4
5
  import GridComponent from "./Grid.svelte";
5
- import type { SheetwriteProps } from "./props.js";
6
+ import type { SheetwriteGridProps, SheetwriteProps } from "./props.js";
6
7
 
7
- type Props = SheetwriteProps<Row>;
8
+ type Props = SheetwriteProps<Row, Id>;
8
9
 
9
- let { columns, defaultRows, sheetName, grid = $bindable(), ...props }: Props = $props();
10
+ let {
11
+ columns,
12
+ defaultRows,
13
+ sheetName,
14
+ getRowId,
15
+ createRowId,
16
+ grid = $bindable(),
17
+ ...props
18
+ }: Props = $props();
10
19
  let input = $derived(createSimpleGridInput({ columns, defaultRows, sheetName }));
20
+ let rowBridge = $derived(
21
+ createSimpleRowBridge({
22
+ columns,
23
+ defaultRows,
24
+ ...(getRowId === undefined ? {} : { getRowId }),
25
+ ...(createRowId === undefined ? {} : { createRowId }),
26
+ }),
27
+ );
28
+ let resolvedProps = $derived.by(() => {
29
+ const forwarded = { ...props, ...input, rowBridge };
30
+ return forwarded as unknown as SheetwriteGridProps;
31
+ });
11
32
 
12
33
  $effect(() => {
13
34
  if (
@@ -19,4 +40,4 @@ $effect(() => {
19
40
  });
20
41
  </script>
21
42
 
22
- <GridComponent {...props} {...input} bind:grid />
43
+ <GridComponent {...resolvedProps} bind:grid />
package/src/index.ts CHANGED
@@ -1,5 +1,22 @@
1
- export type { CellScalar, Grid } from "@sheetwrite/core";
2
- export type { GridReadyEvent, SimpleColumn } from "@sheetwrite/core/adapter";
1
+ export type {
2
+ CellEditor,
3
+ CellEditorContext,
4
+ CellEditorInstance,
5
+ CellEditorNavigation,
6
+ CellScalar,
7
+ Grid,
8
+ GridCommandName,
9
+ GridCommandState,
10
+ } from "@sheetwrite/core";
11
+ export type {
12
+ GridReadyEvent,
13
+ RowBridge,
14
+ RowBridgeDelta,
15
+ RowBridgeHandler,
16
+ RowBridgeId,
17
+ RowBridgeProjection,
18
+ SimpleColumn,
19
+ } from "@sheetwrite/core/adapter";
3
20
  export type { SheetwriteGridProps } from "./Grid.svelte";
4
21
  export { default as SheetwriteGrid } from "./Grid.svelte";
5
22
  export type { SheetwriteProps } from "./Sheetwrite.svelte";
package/src/props.ts CHANGED
@@ -2,21 +2,26 @@ import type { CellScalar, Grid, GridOptions } from "@sheetwrite/core";
2
2
  import type {
3
3
  GridAdapterEventHandlers,
4
4
  GridSizeProps,
5
+ RowBridge,
6
+ RowBridgeId,
7
+ RowBridgeInsertContext,
5
8
  SheetwriteInitializationProps,
6
9
  SimpleColumn,
7
10
  } from "@sheetwrite/core/adapter";
8
11
  import type { Snippet } from "svelte";
9
12
  import type { HTMLAttributes } from "svelte/elements";
10
13
 
11
- /** Advanced framework adapter props for workbook data or datasource ownership. */
12
- export interface SheetwriteGridProps
13
- extends Omit<HTMLAttributes<HTMLDivElement>, keyof GridAdapterEventHandlers | "children">,
14
- GridAdapterEventHandlers,
14
+ /** Advanced Svelte adapter props with inferred host row identity. */
15
+ export interface SheetwriteGridProps<Id extends RowBridgeId = RowBridgeId>
16
+ extends Omit<HTMLAttributes<HTMLDivElement>, keyof GridAdapterEventHandlers<Id> | "children">,
17
+ GridAdapterEventHandlers<Id>,
15
18
  SheetwriteInitializationProps {
16
19
  /** Live workbook schema adopted by the Grid. */
17
20
  workbook: GridOptions["workbook"];
18
21
  /** Eager values for the active sheet. */
19
22
  data?: GridOptions["data"];
23
+ /** Optional canonical-to-host row projection. */
24
+ rowBridge?: RowBridge<Id>;
20
25
  /** Lazy visible-row provider. */
21
26
  datasource?: GridOptions["datasource"];
22
27
  /** Datasource storage policy. */
@@ -25,6 +30,8 @@ export interface SheetwriteGridProps
25
30
  renderer?: GridOptions["renderer"];
26
31
  /** Browser-fetchable worker module URL. */
27
32
  workerUrl?: GridOptions["workerUrl"];
33
+ /** Positional spreadsheet or semantic data-grid headers. */
34
+ presentation?: GridOptions["presentation"];
28
35
  /** Live resolved-theme overrides. */
29
36
  theme?: GridOptions["theme"];
30
37
  /** Disables mutations, not navigation. */
@@ -33,8 +40,14 @@ export interface SheetwriteGridProps
33
40
  protectionResolver?: GridOptions["protectionResolver"];
34
41
  /** Atomic or partial denial policy. */
35
42
  mutationPolicy?: GridOptions["mutationPolicy"];
43
+ /** Overrides inclusive operation-count and encoded-byte ceilings for every atomic mutation. */
44
+ transactionResourceLimits?: GridOptions["transactionResourceLimits"];
45
+ /** Controls link activation: emit an event, also navigate internally, or disable it. */
46
+ hyperlinkActivation?: GridOptions["hyperlinkActivation"];
36
47
  /** Named custom cell renderers. */
37
48
  renderers?: GridOptions["renderers"];
49
+ /** Named custom cell editors. */
50
+ editors?: GridOptions["editors"];
38
51
  /** Extra rows painted around the viewport. */
39
52
  overscan?: GridOptions["overscan"];
40
53
  /** Minimum column count with padding. */
@@ -52,9 +65,12 @@ export interface SheetwriteGridProps
52
65
  }
53
66
 
54
67
  /** Simple framework adapter props for columns and default row objects. */
55
- export type SheetwriteProps<Row extends Record<string, CellScalar>> = Omit<
56
- SheetwriteGridProps,
57
- "workbook" | "data" | "datasource" | "height" | "fill"
68
+ export type SheetwriteProps<
69
+ Row extends Record<string, CellScalar>,
70
+ Id extends RowBridgeId = RowBridgeId,
71
+ > = Omit<
72
+ SheetwriteGridProps<Id>,
73
+ "workbook" | "data" | "datasource" | "height" | "fill" | "rowBridge"
58
74
  > &
59
75
  GridSizeProps & {
60
76
  /** Ordered schema for the owned sheet. */
@@ -63,4 +79,8 @@ export type SheetwriteProps<Row extends Record<string, CellScalar>> = Omit<
63
79
  defaultRows: readonly Row[];
64
80
  /** Sheet name; defaults to `Sheet 1`. */
65
81
  sheetName?: string;
82
+ /** Opt-in stable identity for each host row. */
83
+ getRowId?: (row: Row, index: number) => Id;
84
+ /** Creates stable identities for Grid-inserted rows. */
85
+ createRowId?: (context: RowBridgeInsertContext) => Id;
66
86
  };