@sheetwrite/svelte 0.2.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 +16 -3
- package/package.json +2 -2
- package/src/Grid.svelte +78 -16
- package/src/Sheetwrite.svelte +27 -6
- package/src/index.ts +19 -2
- package/src/props.ts +25 -7
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
|
|
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
|
|
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
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheetwrite/svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Svelte adapter for Sheetwrite.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"svelte": ">=5"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@sheetwrite/core": "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 {
|
|
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,25 +24,35 @@ let {
|
|
|
18
24
|
renderer = "canvas",
|
|
19
25
|
workerUrl,
|
|
20
26
|
theme,
|
|
27
|
+
presentation,
|
|
21
28
|
readOnly,
|
|
22
29
|
protectionResolver,
|
|
23
30
|
mutationPolicy,
|
|
24
31
|
transactionResourceLimits,
|
|
32
|
+
hyperlinkActivation,
|
|
25
33
|
renderers,
|
|
34
|
+
editors,
|
|
26
35
|
overscan,
|
|
27
36
|
minColumns,
|
|
28
37
|
config,
|
|
29
38
|
wasmSource,
|
|
30
39
|
height,
|
|
31
40
|
fill,
|
|
41
|
+
rowBridge,
|
|
32
42
|
fallback,
|
|
33
43
|
onGridChange,
|
|
44
|
+
onRowDelta,
|
|
34
45
|
onSelectionChange,
|
|
35
46
|
onViewportChange,
|
|
36
47
|
onEditBegin,
|
|
37
48
|
onEditCommit,
|
|
38
49
|
onSearch,
|
|
39
50
|
onActiveSheetChange,
|
|
51
|
+
onCommandStateChange,
|
|
52
|
+
onMutationRejected,
|
|
53
|
+
onRendererFallback,
|
|
54
|
+
onDatasourceError,
|
|
55
|
+
onExportError,
|
|
40
56
|
onReady,
|
|
41
57
|
onInitializationError,
|
|
42
58
|
grid = $bindable(),
|
|
@@ -50,6 +66,7 @@ let generation = 0;
|
|
|
50
66
|
|
|
51
67
|
const handlers = {
|
|
52
68
|
onGridChange: (event: Parameters<NonNullable<typeof onGridChange>>[0]) => onGridChange?.(event),
|
|
69
|
+
onRowDelta: (event: Parameters<NonNullable<typeof onRowDelta>>[0]) => onRowDelta?.(event),
|
|
53
70
|
onSelectionChange: (event: Parameters<NonNullable<typeof onSelectionChange>>[0]) =>
|
|
54
71
|
onSelectionChange?.(event),
|
|
55
72
|
onViewportChange: (event: Parameters<NonNullable<typeof onViewportChange>>[0]) =>
|
|
@@ -59,15 +76,43 @@ const handlers = {
|
|
|
59
76
|
onSearch: (event: Parameters<NonNullable<typeof onSearch>>[0]) => onSearch?.(event),
|
|
60
77
|
onActiveSheetChange: (event: Parameters<NonNullable<typeof onActiveSheetChange>>[0]) =>
|
|
61
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),
|
|
62
89
|
};
|
|
63
90
|
|
|
64
91
|
const UNSET_WASM_SOURCE = Symbol("unset-wasm-source");
|
|
65
92
|
let previousOptions: GridOptions | null = null;
|
|
66
93
|
let lastRequestedOptions: GridOptions | null = null;
|
|
94
|
+
let previousRowBridge: Props["rowBridge"] | undefined;
|
|
67
95
|
let previousWasmSource: Props["wasmSource"] | typeof UNSET_WASM_SOURCE = UNSET_WASM_SOURCE;
|
|
68
96
|
let initializationToken = 0;
|
|
69
97
|
let disposed = false;
|
|
70
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
|
+
|
|
71
116
|
function teardownGrid(): void {
|
|
72
117
|
const active = untrack(() => controller);
|
|
73
118
|
grid = undefined;
|
|
@@ -82,11 +127,12 @@ function publishReadyGrid(): void {
|
|
|
82
127
|
generation === 0
|
|
83
128
|
? "initial"
|
|
84
129
|
: (previousOptions && getGridResetReason(previousOptions, options)) ?? "input-reset";
|
|
85
|
-
const active = untrack(() => createGridController(host, options, handlers));
|
|
130
|
+
const active = untrack(() => createGridController(host, options, handlers, rowBridge));
|
|
86
131
|
controller = active;
|
|
87
132
|
grid = active.grid;
|
|
88
133
|
previousOptions = options;
|
|
89
134
|
generation += 1;
|
|
135
|
+
previousRowBridge = rowBridge;
|
|
90
136
|
loading = false;
|
|
91
137
|
untrack(() => onReady?.({ grid: active.grid, generation, reason }));
|
|
92
138
|
}
|
|
@@ -99,10 +145,14 @@ $effect(() => {
|
|
|
99
145
|
datasourceStorage,
|
|
100
146
|
renderer,
|
|
101
147
|
workerUrl,
|
|
148
|
+
presentation,
|
|
102
149
|
protectionResolver,
|
|
103
150
|
mutationPolicy,
|
|
104
151
|
transactionResourceLimits,
|
|
152
|
+
hyperlinkActivation,
|
|
105
153
|
renderers,
|
|
154
|
+
editors,
|
|
155
|
+
rowBridge,
|
|
106
156
|
wasmSource,
|
|
107
157
|
};
|
|
108
158
|
const currentOptions = (): GridOptions =>
|
|
@@ -113,10 +163,13 @@ $effect(() => {
|
|
|
113
163
|
datasourceStorage: resetInputs.datasourceStorage,
|
|
114
164
|
renderer: resetInputs.renderer,
|
|
115
165
|
workerUrl: resetInputs.workerUrl,
|
|
166
|
+
presentation: resetInputs.presentation,
|
|
116
167
|
protectionResolver: resetInputs.protectionResolver,
|
|
117
168
|
mutationPolicy: resetInputs.mutationPolicy,
|
|
118
169
|
transactionResourceLimits: resetInputs.transactionResourceLimits,
|
|
170
|
+
hyperlinkActivation: resetInputs.hyperlinkActivation,
|
|
119
171
|
renderers: resetInputs.renderers,
|
|
172
|
+
editors: resetInputs.editors,
|
|
120
173
|
theme,
|
|
121
174
|
readOnly,
|
|
122
175
|
overscan,
|
|
@@ -128,7 +181,8 @@ $effect(() => {
|
|
|
128
181
|
const wasmChanged =
|
|
129
182
|
previousWasmSource === UNSET_WASM_SOURCE || previousWasmSource !== resetInputs.wasmSource;
|
|
130
183
|
const resetReason =
|
|
131
|
-
lastRequestedOptions && getGridResetReason(lastRequestedOptions, requestedOptions)
|
|
184
|
+
(lastRequestedOptions && getGridResetReason(lastRequestedOptions, requestedOptions)) ??
|
|
185
|
+
(previousRowBridge !== rowBridge ? "input-reset" : null);
|
|
132
186
|
const sourceOnlyChange = wasmChanged && resetReason === null && activeController !== undefined;
|
|
133
187
|
const needsNewGeneration =
|
|
134
188
|
lastRequestedOptions === null ||
|
|
@@ -139,9 +193,10 @@ $effect(() => {
|
|
|
139
193
|
lastRequestedOptions = requestedOptions;
|
|
140
194
|
const token = ++initializationToken;
|
|
141
195
|
if (sourceOnlyChange) {
|
|
142
|
-
void initSheetwrite(resetInputs.wasmSource).
|
|
196
|
+
void initSheetwrite(resetInputs.wasmSource).then(undefined, (error: unknown) => {
|
|
143
197
|
if (!disposed && token === initializationToken) {
|
|
144
|
-
|
|
198
|
+
const initializationError = normalizeInitializationError(error);
|
|
199
|
+
untrack(() => onInitializationError?.(initializationError));
|
|
145
200
|
}
|
|
146
201
|
});
|
|
147
202
|
return;
|
|
@@ -151,19 +206,26 @@ $effect(() => {
|
|
|
151
206
|
teardownGrid();
|
|
152
207
|
loading = !isSheetwriteReady();
|
|
153
208
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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) => {
|
|
162
223
|
if (disposed || token !== initializationToken) return;
|
|
163
224
|
loading = true;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
225
|
+
const initializationError = normalizeInitializationError(error);
|
|
226
|
+
untrack(() => onInitializationError?.(initializationError));
|
|
227
|
+
},
|
|
228
|
+
);
|
|
167
229
|
});
|
|
168
230
|
|
|
169
231
|
$effect(() => {
|
package/src/Sheetwrite.svelte
CHANGED
|
@@ -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 {
|
|
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 {...
|
|
43
|
+
<GridComponent {...resolvedProps} bind:grid />
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
|
|
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
|
|
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. */
|
|
@@ -35,8 +42,12 @@ export interface SheetwriteGridProps
|
|
|
35
42
|
mutationPolicy?: GridOptions["mutationPolicy"];
|
|
36
43
|
/** Overrides inclusive operation-count and encoded-byte ceilings for every atomic mutation. */
|
|
37
44
|
transactionResourceLimits?: GridOptions["transactionResourceLimits"];
|
|
45
|
+
/** Controls link activation: emit an event, also navigate internally, or disable it. */
|
|
46
|
+
hyperlinkActivation?: GridOptions["hyperlinkActivation"];
|
|
38
47
|
/** Named custom cell renderers. */
|
|
39
48
|
renderers?: GridOptions["renderers"];
|
|
49
|
+
/** Named custom cell editors. */
|
|
50
|
+
editors?: GridOptions["editors"];
|
|
40
51
|
/** Extra rows painted around the viewport. */
|
|
41
52
|
overscan?: GridOptions["overscan"];
|
|
42
53
|
/** Minimum column count with padding. */
|
|
@@ -54,9 +65,12 @@ export interface SheetwriteGridProps
|
|
|
54
65
|
}
|
|
55
66
|
|
|
56
67
|
/** Simple framework adapter props for columns and default row objects. */
|
|
57
|
-
export type SheetwriteProps<
|
|
58
|
-
|
|
59
|
-
|
|
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"
|
|
60
74
|
> &
|
|
61
75
|
GridSizeProps & {
|
|
62
76
|
/** Ordered schema for the owned sheet. */
|
|
@@ -65,4 +79,8 @@ export type SheetwriteProps<Row extends Record<string, CellScalar>> = Omit<
|
|
|
65
79
|
defaultRows: readonly Row[];
|
|
66
80
|
/** Sheet name; defaults to `Sheet 1`. */
|
|
67
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;
|
|
68
86
|
};
|