@sheetwrite/svelte 0.4.0 → 0.5.1

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
@@ -53,6 +53,12 @@ unmount. Reset-bound inputs are `workbook`, `data`, `datasource`,
53
53
  `renderers`; `theme`, `readOnly`, `config`, `overscan`, and `minColumns` update
54
54
  live.
55
55
 
56
+ Readiness observes the latest committed live options, including changes while
57
+ WASM initialization is pending. A throwing ready callback retires the new Grid;
58
+ consumer errors are not initialization failures. Omitted `renderer` and
59
+ `"canvas"` are equivalent. Changing an explicit `wasmSource` after readiness
60
+ preserves the Grid and retains the loader's first-source warning.
61
+
56
62
  Grid callbacks are `onGridChange`, `onViewportChange`, `onSelectionChange`, `onEditBegin`, `onEditCommit`, `onSearch`, `onCommandStateChange`, `onActiveSheetChange`, `onMutationRejected`, `onRendererFallback`, `onDatasourceError`, and `onExportError`.
57
63
 
58
64
  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/).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheetwrite/svelte",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "type": "module",
5
5
  "description": "Svelte adapter for Sheetwrite.",
6
6
  "license": "MIT",
@@ -19,6 +19,9 @@
19
19
  "svelte",
20
20
  "canvas"
21
21
  ],
22
+ "sideEffects": [
23
+ "**/*.css"
24
+ ],
22
25
  "svelte": "./src/index.ts",
23
26
  "exports": {
24
27
  ".": {
@@ -29,7 +32,8 @@
29
32
  "./styles.css": "./styles.css"
30
33
  },
31
34
  "files": [
32
- "src",
35
+ "src/**/*.ts",
36
+ "src/**/*.svelte",
33
37
  "styles.css"
34
38
  ],
35
39
  "publishConfig": {
@@ -44,7 +48,7 @@
44
48
  "svelte": ">=5"
45
49
  },
46
50
  "dependencies": {
47
- "@sheetwrite/core": "0.4.0"
51
+ "@sheetwrite/core": "0.5.1"
48
52
  },
49
53
  "devDependencies": {
50
54
  "svelte-check": "4.7.3",
package/src/Grid.svelte CHANGED
@@ -1,17 +1,12 @@
1
1
  <script lang="ts">
2
+ import { isSheetwriteReady } from "@sheetwrite/core";
2
3
  import {
3
- initSheetwrite,
4
- isSheetwriteError,
5
- isSheetwriteReady,
6
- SheetwriteError,
7
- type GridOptions,
8
- } from "@sheetwrite/core";
9
- import {
10
- createGridController,
11
- getGridResetReason,
4
+ type AdapterGridInput,
5
+ createAdapterGridController,
6
+ type GridAdapterEventHandlers,
7
+ type GridPublicationAdapter,
12
8
  gridSizeStyle,
13
9
  } from "@sheetwrite/core/adapter";
14
- import type { GridController, GridReadyReason } from "@sheetwrite/core/adapter";
15
10
  import { untrack } from "svelte";
16
11
  import type { SheetwriteGridProps as Props } from "./props.js";
17
12
 
@@ -60,11 +55,15 @@ let {
60
55
  }: Props = $props();
61
56
 
62
57
  let host: HTMLDivElement;
63
- let controller: GridController | undefined = $state();
64
58
  let loading = $state(!isSheetwriteReady());
65
- let generation = 0;
66
59
 
67
- const handlers = {
60
+ function forwardCommandStateChange(
61
+ event: Parameters<NonNullable<typeof onCommandStateChange>>[0],
62
+ ): void {
63
+ onCommandStateChange?.(event);
64
+ }
65
+
66
+ const handlers: GridAdapterEventHandlers = {
68
67
  onGridChange: (event: Parameters<NonNullable<typeof onGridChange>>[0]) => onGridChange?.(event),
69
68
  onRowDelta: (event: Parameters<NonNullable<typeof onRowDelta>>[0]) => onRowDelta?.(event),
70
69
  onSelectionChange: (event: Parameters<NonNullable<typeof onSelectionChange>>[0]) =>
@@ -76,8 +75,11 @@ const handlers = {
76
75
  onSearch: (event: Parameters<NonNullable<typeof onSearch>>[0]) => onSearch?.(event),
77
76
  onActiveSheetChange: (event: Parameters<NonNullable<typeof onActiveSheetChange>>[0]) =>
78
77
  onActiveSheetChange?.(event),
79
- onCommandStateChange: (event: Parameters<NonNullable<typeof onCommandStateChange>>[0]) =>
80
- onCommandStateChange?.(event),
78
+ // The controller only subscribes to command state while this is defined, so
79
+ // absence must be visible instead of an always-present wrapper.
80
+ get onCommandStateChange() {
81
+ return onCommandStateChange === undefined ? undefined : forwardCommandStateChange;
82
+ },
81
83
  onMutationRejected: (event: Parameters<NonNullable<typeof onMutationRejected>>[0]) =>
82
84
  onMutationRejected?.(event),
83
85
  onRendererFallback: (event: Parameters<NonNullable<typeof onRendererFallback>>[0]) =>
@@ -86,161 +88,42 @@ const handlers = {
86
88
  onDatasourceError?.(event),
87
89
  onExportError: (event: Parameters<NonNullable<typeof onExportError>>[0]) =>
88
90
  onExportError?.(event),
91
+ onReady: (event: Parameters<NonNullable<typeof onReady>>[0]) => onReady?.(event),
92
+ onInitializationError: (error: Parameters<NonNullable<typeof onInitializationError>>[0]) =>
93
+ onInitializationError?.(error),
89
94
  };
90
95
 
91
- const UNSET_WASM_SOURCE = Symbol("unset-wasm-source");
92
- let previousOptions: GridOptions | null = null;
93
- let lastRequestedOptions: GridOptions | null = null;
94
- let previousRowBridge: Props["rowBridge"] | undefined;
95
- let previousWasmSource: Props["wasmSource"] | typeof UNSET_WASM_SOURCE = UNSET_WASM_SOURCE;
96
- let initializationToken = 0;
97
- let disposed = false;
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
-
116
- function teardownGrid(): void {
117
- const active = untrack(() => controller);
118
- grid = undefined;
119
- controller = undefined;
120
- active?.destroy();
121
- }
122
-
123
- function publishReadyGrid(): void {
124
- if (disposed || untrack(() => controller) || !lastRequestedOptions) return;
125
- const options = lastRequestedOptions;
126
- const reason: GridReadyReason =
127
- generation === 0
128
- ? "initial"
129
- : (previousOptions && getGridResetReason(previousOptions, options)) ?? "input-reset";
130
- const active = untrack(() => createGridController(host, options, handlers, rowBridge));
131
- controller = active;
132
- grid = active.grid;
133
- previousOptions = options;
134
- generation += 1;
135
- previousRowBridge = rowBridge;
136
- loading = false;
137
- untrack(() => onReady?.({ grid: active.grid, generation, reason }));
138
- }
139
-
96
+ const lifetime = createAdapterGridController(handlers, {
97
+ publish(nextGrid) {
98
+ grid = nextGrid ?? undefined;
99
+ },
100
+ setInitializationState(state) {
101
+ loading = state !== "ready";
102
+ },
103
+ reportConsumerFailure(thrown) {
104
+ reportError(thrown);
105
+ },
106
+ } satisfies GridPublicationAdapter);
107
+ let attached = false;
140
108
  $effect(() => {
141
- const resetInputs = {
142
- workbook,
143
- data,
144
- datasource,
145
- datasourceStorage,
146
- renderer,
147
- workerUrl,
148
- presentation,
149
- protectionResolver,
150
- mutationPolicy,
151
- transactionResourceLimits,
152
- hyperlinkActivation,
153
- renderers,
154
- editors,
109
+ const input: AdapterGridInput = {
110
+ options: {
111
+ workbook, data, datasource, datasourceStorage, renderer, workerUrl, presentation,
112
+ protectionResolver, mutationPolicy, transactionResourceLimits, hyperlinkActivation,
113
+ renderers, editors, theme, readOnly, overscan, minColumns, config,
114
+ },
155
115
  rowBridge,
156
116
  wasmSource,
157
117
  };
158
- const currentOptions = (): GridOptions =>
159
- untrack(() => ({
160
- workbook: resetInputs.workbook,
161
- data: resetInputs.data,
162
- datasource: resetInputs.datasource,
163
- datasourceStorage: resetInputs.datasourceStorage,
164
- renderer: resetInputs.renderer,
165
- workerUrl: resetInputs.workerUrl,
166
- presentation: resetInputs.presentation,
167
- protectionResolver: resetInputs.protectionResolver,
168
- mutationPolicy: resetInputs.mutationPolicy,
169
- transactionResourceLimits: resetInputs.transactionResourceLimits,
170
- hyperlinkActivation: resetInputs.hyperlinkActivation,
171
- renderers: resetInputs.renderers,
172
- editors: resetInputs.editors,
173
- theme,
174
- readOnly,
175
- overscan,
176
- minColumns,
177
- config,
178
- }));
179
- const requestedOptions = currentOptions();
180
- const activeController = untrack(() => controller);
181
- const wasmChanged =
182
- previousWasmSource === UNSET_WASM_SOURCE || previousWasmSource !== resetInputs.wasmSource;
183
- const resetReason =
184
- (lastRequestedOptions && getGridResetReason(lastRequestedOptions, requestedOptions)) ??
185
- (previousRowBridge !== rowBridge ? "input-reset" : null);
186
- const sourceOnlyChange = wasmChanged && resetReason === null && activeController !== undefined;
187
- const needsNewGeneration =
188
- lastRequestedOptions === null ||
189
- resetReason !== null ||
190
- (wasmChanged && (!isSheetwriteReady() || activeController === undefined));
191
-
192
- previousWasmSource = resetInputs.wasmSource;
193
- lastRequestedOptions = requestedOptions;
194
- const token = ++initializationToken;
195
- if (sourceOnlyChange) {
196
- void initSheetwrite(resetInputs.wasmSource).then(undefined, (error: unknown) => {
197
- if (!disposed && token === initializationToken) {
198
- const initializationError = normalizeInitializationError(error);
199
- untrack(() => onInitializationError?.(initializationError));
200
- }
201
- });
202
- return;
203
- }
204
- if (!needsNewGeneration) return;
205
-
206
- teardownGrid();
207
- loading = !isSheetwriteReady();
208
-
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) => {
223
- if (disposed || token !== initializationToken) return;
224
- loading = true;
225
- const initializationError = normalizeInitializationError(error);
226
- untrack(() => onInitializationError?.(initializationError));
227
- },
228
- );
118
+ untrack(() => {
119
+ if (attached) lifetime.update(input);
120
+ else {
121
+ attached = true;
122
+ lifetime.mount(host, input);
123
+ }
124
+ });
229
125
  });
230
-
231
- $effect(() => {
232
- return () => {
233
- disposed = true;
234
- initializationToken += 1;
235
- teardownGrid();
236
- };
237
- });
238
-
239
- $effect(() => controller?.setReadOnly(readOnly ?? false));
240
- $effect(() => controller?.setConfig(config));
241
- $effect(() => controller?.setTheme(theme));
242
- $effect(() => controller?.setOverscan(overscan));
243
- $effect(() => controller?.setMinColumns(minColumns));
126
+ $effect(() => () => untrack(() => lifetime.unmount()));
244
127
 
245
128
  let sizing = $derived(gridSizeStyle({ height, fill }));
246
129
  </script>
package/src/index.ts CHANGED
@@ -18,7 +18,42 @@ export type {
18
18
  SimpleColumn,
19
19
  } from "@sheetwrite/core/adapter";
20
20
  export type { SheetwriteGridProps } from "./Grid.svelte";
21
+ /**
22
+ * Advanced component for an existing workbook or datasource.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { mount, unmount } from "svelte";
27
+ * import { SheetwriteGrid } from "@sheetwrite/svelte";
28
+ * import type { Workbook } from "@sheetwrite/core";
29
+ *
30
+ * function mountWorkbook(target: HTMLElement, workbook: Workbook) {
31
+ * const view = mount(SheetwriteGrid, { target, props: { workbook, height: 400 } });
32
+ * return () => unmount(view);
33
+ * }
34
+ * ```
35
+ * @see Sheetwrite
36
+ * @see /docs/frameworks/svelte/
37
+ */
21
38
  export { default as SheetwriteGrid } from "./Grid.svelte";
22
39
  export type { SheetwriteProps } from "./Sheetwrite.svelte";
23
- /** Convenience component for local object rows. Bind `grid` to access the live `Grid`. */
40
+ /**
41
+ * Convenience component for local object rows. Bind `grid` to access the live `Grid`.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import { mount, unmount } from "svelte";
46
+ * import { Sheetwrite } from "@sheetwrite/svelte";
47
+ *
48
+ * const view = mount(Sheetwrite, { target: document.body, props: {
49
+ * columns: [{ key: "amount", title: "Amount", type: "number" }],
50
+ * defaultRows: [{ amount: 12 }, { amount: 18 }],
51
+ * height: 400,
52
+ * } });
53
+ * // Call when the host view is removed.
54
+ * await unmount(view);
55
+ * ```
56
+ * @see SheetwriteGrid
57
+ * @see /docs/frameworks/svelte/
58
+ */
24
59
  export { default as Sheetwrite } from "./Sheetwrite.svelte";
package/src/props.ts CHANGED
@@ -13,7 +13,8 @@ import type { HTMLAttributes } from "svelte/elements";
13
13
 
14
14
  /** Advanced Svelte adapter props with inferred host row identity. */
15
15
  export interface SheetwriteGridProps<Id extends RowBridgeId = RowBridgeId>
16
- extends Omit<HTMLAttributes<HTMLDivElement>, keyof GridAdapterEventHandlers<Id> | "children">,
16
+ extends
17
+ Omit<HTMLAttributes<HTMLDivElement>, keyof GridAdapterEventHandlers<Id> | "children">,
17
18
  GridAdapterEventHandlers<Id>,
18
19
  SheetwriteInitializationProps {
19
20
  /** Live workbook schema adopted by the Grid. */