@molecule/app-spreadsheet-grid-svelte 0.0.0-stage → 0.0.1-stage
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/LICENSE +202 -0
- package/README.md +10 -2
- package/dist/SpreadsheetGrid.svelte +387 -0
- package/dist/SpreadsheetGrid.svelte.d.ts +5 -0
- package/dist/SpreadsheetGrid.svelte.d.ts.map +1 -0
- package/dist/components.d.ts +8 -0
- package/dist/components.d.ts.map +1 -0
- package/dist/components.js +7 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +69 -0
- package/dist/layout.d.ts +167 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +182 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/utilities.d.ts +74 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +121 -0
- package/dist/value.d.ts +33 -0
- package/dist/value.d.ts.map +1 -0
- package/dist/value.js +41 -0
- package/package.json +77 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @molecule/app-spreadsheet-grid-svelte
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
High-performance virtualized spreadsheet cell grid with frozen rows/columns, range selection, copy/paste as TSV, and in-cell editing — pairs with @molecule/api-formula-engine
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm install @molecule/app-spreadsheet-grid-svelte
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Part of the [Molecule](https://www.molecule.dev) package fleet. This page carries the manifest summary; the full generated documentation ships with the package's first committed version.
|
|
10
|
+
|
|
11
|
+
License: Apache-2.0
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SpreadsheetGrid: the Svelte counterpart of
|
|
3
|
+
`@molecule/app-spreadsheet-grid-react`'s `SpreadsheetGrid` — a virtualized
|
|
4
|
+
cell grid with frozen rows/columns, range selection, copy/paste as TSV and
|
|
5
|
+
in-cell editing. Same props (React names), same DOM for the same input, and
|
|
6
|
+
the same callbacks. React's `renderCell` render prop is a snippet receiving
|
|
7
|
+
the value and the A1 reference.
|
|
8
|
+
-->
|
|
9
|
+
<script lang="ts">
|
|
10
|
+
import { copyTextWithFallback } from '@molecule/app-clipboard'
|
|
11
|
+
import { useTranslation } from '@molecule/app-svelte'
|
|
12
|
+
import { getClassMap } from '@molecule/app-ui'
|
|
13
|
+
|
|
14
|
+
import { computeLayout, type GridCell, type GridEditing } from './layout.js'
|
|
15
|
+
import type { CellValue, SpreadsheetGridProps } from './types.js'
|
|
16
|
+
import {
|
|
17
|
+
cellRef,
|
|
18
|
+
formatCellValue,
|
|
19
|
+
normalizeSelection,
|
|
20
|
+
parseClipboardTsv,
|
|
21
|
+
serializeSelectionTsv,
|
|
22
|
+
} from './utilities.js'
|
|
23
|
+
import { reactInputValue } from './value.js'
|
|
24
|
+
|
|
25
|
+
let {
|
|
26
|
+
rows,
|
|
27
|
+
columns,
|
|
28
|
+
cells,
|
|
29
|
+
onCellChange,
|
|
30
|
+
selection,
|
|
31
|
+
onSelectionChange,
|
|
32
|
+
frozenRows = 0,
|
|
33
|
+
frozenCols = 0,
|
|
34
|
+
cellWidth = 96,
|
|
35
|
+
cellHeight = 28,
|
|
36
|
+
gutterWidth = 48,
|
|
37
|
+
headerHeight = 28,
|
|
38
|
+
viewportWidth = 720,
|
|
39
|
+
viewportHeight = 360,
|
|
40
|
+
dataMolId,
|
|
41
|
+
renderCell,
|
|
42
|
+
}: SpreadsheetGridProps = $props()
|
|
43
|
+
|
|
44
|
+
const cm = $derived(getClassMap())
|
|
45
|
+
const { t } = useTranslation()
|
|
46
|
+
|
|
47
|
+
let scrollLeft = $state(0)
|
|
48
|
+
let scrollTop = $state(0)
|
|
49
|
+
let editing = $state<GridEditing | null>(null)
|
|
50
|
+
/** The editor field, focused and selected once each time editing starts. */
|
|
51
|
+
let editorEl = $state<HTMLInputElement | null>(null)
|
|
52
|
+
/** Where the current drag started, while a mouse button is held. */
|
|
53
|
+
let dragAnchor: { row: number; col: number } | null = null
|
|
54
|
+
|
|
55
|
+
const layout = $derived(
|
|
56
|
+
computeLayout(
|
|
57
|
+
{
|
|
58
|
+
rows,
|
|
59
|
+
columns,
|
|
60
|
+
cells,
|
|
61
|
+
selection,
|
|
62
|
+
frozenRows,
|
|
63
|
+
frozenCols,
|
|
64
|
+
cellWidth,
|
|
65
|
+
cellHeight,
|
|
66
|
+
gutterWidth,
|
|
67
|
+
headerHeight,
|
|
68
|
+
viewportWidth,
|
|
69
|
+
viewportHeight,
|
|
70
|
+
scrollLeft,
|
|
71
|
+
scrollTop,
|
|
72
|
+
editing,
|
|
73
|
+
},
|
|
74
|
+
cm,
|
|
75
|
+
),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
// React's `useEffect(() => { focus(); select() }, [editing])`: runs after the
|
|
79
|
+
// editor is in the DOM, and again whenever the draft changes (as React's
|
|
80
|
+
// effect re-runs on each new editing object).
|
|
81
|
+
$effect(() => {
|
|
82
|
+
if (!editing) return
|
|
83
|
+
const field = editorEl
|
|
84
|
+
field?.focus()
|
|
85
|
+
field?.select()
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Opens the in-cell editor on one cell, seeding the draft from its value.
|
|
90
|
+
*
|
|
91
|
+
* @param row - 0-indexed row.
|
|
92
|
+
* @param col - 0-indexed column.
|
|
93
|
+
*/
|
|
94
|
+
function beginEdit(row: number, col: number): void {
|
|
95
|
+
editing = { row, col, draft: formatCellValue(cells.get(cellRef(row, col))) }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Commits the open edit: empty clears the cell, number-like text becomes a number. */
|
|
99
|
+
function commitEdit(): void {
|
|
100
|
+
const current = editing
|
|
101
|
+
if (!current) return
|
|
102
|
+
const ref = cellRef(current.row, current.col)
|
|
103
|
+
const trimmed = current.draft
|
|
104
|
+
let next: CellValue
|
|
105
|
+
if (trimmed === '') next = null
|
|
106
|
+
else if (/^-?\d+(\.\d+)?$/.test(trimmed)) next = Number(trimmed)
|
|
107
|
+
else next = trimmed
|
|
108
|
+
onCellChange(ref, next)
|
|
109
|
+
editing = null
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Closes the editor without committing. */
|
|
113
|
+
function cancelEdit(): void {
|
|
114
|
+
editing = null
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* React's cell `onMouseDown`: commits an open edit, then starts or extends
|
|
119
|
+
* the selection.
|
|
120
|
+
*
|
|
121
|
+
* @param row - 0-indexed row.
|
|
122
|
+
* @param col - 0-indexed column.
|
|
123
|
+
* @param shiftKey - Whether Shift was held (extend from the anchor).
|
|
124
|
+
*/
|
|
125
|
+
function handleMouseDown(row: number, col: number, shiftKey: boolean): void {
|
|
126
|
+
if (editing) commitEdit()
|
|
127
|
+
if (shiftKey) {
|
|
128
|
+
onSelectionChange({ r1: selection.r1, c1: selection.c1, r2: row, c2: col })
|
|
129
|
+
} else {
|
|
130
|
+
dragAnchor = { row, col }
|
|
131
|
+
onSelectionChange({ r1: row, c1: col, r2: row, c2: col })
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* React's cell `onMouseEnter`: extends the selection while the primary
|
|
137
|
+
* button is held.
|
|
138
|
+
*
|
|
139
|
+
* @param row - 0-indexed row.
|
|
140
|
+
* @param col - 0-indexed column.
|
|
141
|
+
* @param buttonsDown - Whether the primary button is held.
|
|
142
|
+
*/
|
|
143
|
+
function handleMouseEnter(row: number, col: number, buttonsDown: boolean): void {
|
|
144
|
+
if (!buttonsDown) return
|
|
145
|
+
const anchor = dragAnchor
|
|
146
|
+
if (!anchor) return
|
|
147
|
+
onSelectionChange({ r1: anchor.row, c1: anchor.col, r2: row, c2: col })
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Clears the drag anchor on mouse up. */
|
|
151
|
+
function handleMouseUp(): void {
|
|
152
|
+
dragAnchor = null
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* React's container `onKeyDown`: copy, and Edit on Enter / F2.
|
|
157
|
+
*
|
|
158
|
+
* @param event - The keyboard event.
|
|
159
|
+
*/
|
|
160
|
+
function handleKeyDown(event: KeyboardEvent): void {
|
|
161
|
+
const isMod = event.ctrlKey || event.metaKey
|
|
162
|
+
if (editing) return
|
|
163
|
+
if (isMod && event.key.toLowerCase() === 'c') {
|
|
164
|
+
event.preventDefault()
|
|
165
|
+
void handleCopy()
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
if (isMod && event.key.toLowerCase() === 'v') return
|
|
169
|
+
if (event.key === 'Enter' || event.key === 'F2') {
|
|
170
|
+
event.preventDefault()
|
|
171
|
+
beginEdit(selection.r1, selection.c1)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The editor field's key handling: Enter commits, Escape cancels. The grid's
|
|
177
|
+
* own key handler runs only while no editor is open (React reads the state
|
|
178
|
+
* the render before the event began, so a key that commits here never
|
|
179
|
+
* reaches it), so the event stays on the field.
|
|
180
|
+
*
|
|
181
|
+
* @param event - The keyboard event.
|
|
182
|
+
*/
|
|
183
|
+
function handleEditorKeyDown(event: KeyboardEvent): void {
|
|
184
|
+
event.stopPropagation()
|
|
185
|
+
if (event.key === 'Enter') {
|
|
186
|
+
event.preventDefault()
|
|
187
|
+
commitEdit()
|
|
188
|
+
} else if (event.key === 'Escape') {
|
|
189
|
+
event.preventDefault()
|
|
190
|
+
cancelEdit()
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Serializes the selection to TSV and copies it through the clipboard bond.
|
|
196
|
+
*
|
|
197
|
+
* @returns The TSV text.
|
|
198
|
+
*/
|
|
199
|
+
async function handleCopy(): Promise<string> {
|
|
200
|
+
const tsv = serializeSelectionTsv(cells, selection)
|
|
201
|
+
await copyTextWithFallback(tsv)
|
|
202
|
+
return tsv
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Writes pasted TSV into cells starting at the selection anchor.
|
|
207
|
+
*
|
|
208
|
+
* @param text - Clipboard text (typically TSV).
|
|
209
|
+
*/
|
|
210
|
+
function handlePaste(text: string): void {
|
|
211
|
+
const grid = parseClipboardTsv(text)
|
|
212
|
+
if (grid.length === 0) return
|
|
213
|
+
const n = normalizeSelection(selection)
|
|
214
|
+
for (let r = 0; r < grid.length; r += 1) {
|
|
215
|
+
const row = grid[r]!
|
|
216
|
+
for (let c = 0; c < row.length; c += 1) {
|
|
217
|
+
const targetRow = n.r1 + r
|
|
218
|
+
const targetCol = n.c1 + c
|
|
219
|
+
if (targetRow >= rows || targetCol >= columns) continue
|
|
220
|
+
const raw = row[c]!
|
|
221
|
+
let next: CellValue
|
|
222
|
+
if (raw === '') next = null
|
|
223
|
+
else if (/^-?\d+(\.\d+)?$/.test(raw)) next = Number(raw)
|
|
224
|
+
else next = raw
|
|
225
|
+
onCellChange(cellRef(targetRow, targetCol), next)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* React's container `onPaste`: intercepts the native paste event.
|
|
232
|
+
*
|
|
233
|
+
* @param event - The clipboard event.
|
|
234
|
+
*/
|
|
235
|
+
function handlePasteEvent(event: ClipboardEvent): void {
|
|
236
|
+
const text = event.clipboardData ? event.clipboardData.getData('text/plain') : ''
|
|
237
|
+
if (!text) return
|
|
238
|
+
event.preventDefault()
|
|
239
|
+
handlePaste(text)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The editor's body: each typed value becomes the draft.
|
|
244
|
+
*
|
|
245
|
+
* @param event - The input event.
|
|
246
|
+
*/
|
|
247
|
+
function handleDraftInput(event: Event): void {
|
|
248
|
+
const current = editing
|
|
249
|
+
if (!current) return
|
|
250
|
+
editing = { ...current, draft: (event.currentTarget as HTMLInputElement).value }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The scroller's `scroll` handler: refreshes the rendered window.
|
|
255
|
+
*
|
|
256
|
+
* @param event - The scroll event.
|
|
257
|
+
*/
|
|
258
|
+
function handleScroll(event: Event): void {
|
|
259
|
+
const element = event.currentTarget as HTMLDivElement
|
|
260
|
+
scrollLeft = element.scrollLeft
|
|
261
|
+
scrollTop = element.scrollTop
|
|
262
|
+
}
|
|
263
|
+
</script>
|
|
264
|
+
|
|
265
|
+
{#snippet cellBox(cell: GridCell)}
|
|
266
|
+
<div
|
|
267
|
+
role="gridcell"
|
|
268
|
+
data-mol-id="spreadsheet-grid-cell-{cell.ref}"
|
|
269
|
+
tabindex={cell.anchor ? 0 : -1}
|
|
270
|
+
aria-selected={cell.selected ? 'true' : undefined}
|
|
271
|
+
class={cell.classes}
|
|
272
|
+
style={cell.style}
|
|
273
|
+
onmousedown={(event) => handleMouseDown(cell.row, cell.col, event.shiftKey)}
|
|
274
|
+
onmouseenter={(event) => handleMouseEnter(cell.row, cell.col, event.buttons === 1)}
|
|
275
|
+
ondblclick={() => beginEdit(cell.row, cell.col)}
|
|
276
|
+
>
|
|
277
|
+
{#if editing && editing.row === cell.row && editing.col === cell.col}
|
|
278
|
+
<input
|
|
279
|
+
bind:this={editorEl}
|
|
280
|
+
data-mol-id="spreadsheet-grid-editor-{cell.ref}"
|
|
281
|
+
class={layout.editorClasses}
|
|
282
|
+
use:reactInputValue={{ value: editing.draft }}
|
|
283
|
+
oninput={handleDraftInput}
|
|
284
|
+
onblur={commitEdit}
|
|
285
|
+
onkeydown={handleEditorKeyDown}
|
|
286
|
+
aria-label={t(
|
|
287
|
+
'spreadsheetGrid.cellEditor',
|
|
288
|
+
{ ref: cell.ref },
|
|
289
|
+
{ defaultValue: 'Edit cell {{ref}}' },
|
|
290
|
+
)}
|
|
291
|
+
/>
|
|
292
|
+
{:else}
|
|
293
|
+
<div class={layout.contentClasses}>
|
|
294
|
+
{#if renderCell}{@render renderCell(cell.value, cell.ref)}{:else}{cell.text}{/if}
|
|
295
|
+
</div>
|
|
296
|
+
{/if}
|
|
297
|
+
</div>
|
|
298
|
+
{/snippet}
|
|
299
|
+
|
|
300
|
+
<div
|
|
301
|
+
data-mol-id={dataMolId ?? 'spreadsheet-grid'}
|
|
302
|
+
role="grid"
|
|
303
|
+
tabindex="0"
|
|
304
|
+
aria-rowcount={rows}
|
|
305
|
+
aria-colcount={columns}
|
|
306
|
+
aria-label={t('spreadsheetGrid.label', undefined, { defaultValue: 'Spreadsheet' })}
|
|
307
|
+
class={layout.rootClasses}
|
|
308
|
+
style={layout.rootStyle}
|
|
309
|
+
onkeydown={handleKeyDown}
|
|
310
|
+
onpaste={handlePasteEvent}
|
|
311
|
+
onmouseup={handleMouseUp}
|
|
312
|
+
>
|
|
313
|
+
<!-- Top-left corner (gutter × header). -->
|
|
314
|
+
<div aria-hidden="true" class={layout.cornerClasses} style={layout.cornerStyle}></div>
|
|
315
|
+
<!-- Column-letter header (frozen + scrollable). -->
|
|
316
|
+
<div role="row" aria-rowindex="1" class={layout.headerLaneClasses} style={layout.headerLaneStyle}>
|
|
317
|
+
{#each layout.frozenHeaderCells as header (header.key)}
|
|
318
|
+
<div
|
|
319
|
+
role="columnheader"
|
|
320
|
+
aria-colindex={header.index + 1}
|
|
321
|
+
class={header.classes}
|
|
322
|
+
style={header.style}
|
|
323
|
+
>
|
|
324
|
+
{header.text}
|
|
325
|
+
</div>
|
|
326
|
+
{/each}
|
|
327
|
+
<div style={layout.headerScrollerStyle}>
|
|
328
|
+
{#each layout.scrollingHeaderCells as header (header.key)}
|
|
329
|
+
<div
|
|
330
|
+
role="columnheader"
|
|
331
|
+
aria-colindex={header.index + 1}
|
|
332
|
+
class={header.classes}
|
|
333
|
+
style={header.style}
|
|
334
|
+
>
|
|
335
|
+
{header.text}
|
|
336
|
+
</div>
|
|
337
|
+
{/each}
|
|
338
|
+
</div>
|
|
339
|
+
</div>
|
|
340
|
+
<!-- Row-number gutter (frozen + scrollable). -->
|
|
341
|
+
<div aria-hidden="true" class={layout.gutterLaneClasses} style={layout.gutterLaneStyle}>
|
|
342
|
+
{#each layout.frozenGutterCells as label (label.key)}
|
|
343
|
+
<div class={label.classes} style={label.style}>{label.text}</div>
|
|
344
|
+
{/each}
|
|
345
|
+
<div style={layout.gutterScrollerStyle}>
|
|
346
|
+
{#each layout.scrollingGutterCells as label (label.key)}
|
|
347
|
+
<div class={label.classes} style={label.style}>{label.text}</div>
|
|
348
|
+
{/each}
|
|
349
|
+
</div>
|
|
350
|
+
</div>
|
|
351
|
+
<!-- Frozen-rows × scrolling-cols region. -->
|
|
352
|
+
{#if layout.showFrozenRows}
|
|
353
|
+
<div style={layout.frozenRowsRegionStyle}>
|
|
354
|
+
<div style={layout.frozenRowsInnerStyle}>
|
|
355
|
+
{#each layout.frozenRowsCells as cell (cell.ref)}{@render cellBox(cell)}{/each}
|
|
356
|
+
</div>
|
|
357
|
+
</div>
|
|
358
|
+
{/if}
|
|
359
|
+
<!-- Frozen-cols × scrolling-rows region. -->
|
|
360
|
+
{#if layout.showFrozenCols}
|
|
361
|
+
<div style={layout.frozenColsRegionStyle}>
|
|
362
|
+
<div style={layout.frozenColsInnerStyle}>
|
|
363
|
+
{#each layout.frozenColsCells as cell (cell.ref)}{@render cellBox(cell)}{/each}
|
|
364
|
+
</div>
|
|
365
|
+
</div>
|
|
366
|
+
{/if}
|
|
367
|
+
<!-- Frozen-rows × frozen-cols corner. -->
|
|
368
|
+
{#if layout.showFrozenCorner}
|
|
369
|
+
<div style={layout.frozenCornerStyle}>
|
|
370
|
+
{#each layout.frozenCornerCells as cell (cell.ref)}{@render cellBox(cell)}{/each}
|
|
371
|
+
</div>
|
|
372
|
+
{/if}
|
|
373
|
+
<!-- Main scrollable region. -->
|
|
374
|
+
<div
|
|
375
|
+
data-mol-id="spreadsheet-grid-scroll"
|
|
376
|
+
style={layout.scrollRegionStyle}
|
|
377
|
+
onscroll={handleScroll}
|
|
378
|
+
>
|
|
379
|
+
<div style={layout.scrollSpacerStyle}>
|
|
380
|
+
{#each layout.mainCells as cell (cell.ref)}{@render cellBox(cell)}{/each}
|
|
381
|
+
</div>
|
|
382
|
+
</div>
|
|
383
|
+
<!-- Hidden total-size hint for accessibility tooling. -->
|
|
384
|
+
<span class={layout.totalClasses} style={layout.totalStyle} aria-hidden="true"
|
|
385
|
+
>{layout.totalText}</span
|
|
386
|
+
>
|
|
387
|
+
</div>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SpreadsheetGridProps } from './types.js';
|
|
2
|
+
declare const SpreadsheetGrid: import("svelte").Component<SpreadsheetGridProps, {}, "">;
|
|
3
|
+
type SpreadsheetGrid = ReturnType<typeof SpreadsheetGrid>;
|
|
4
|
+
export default SpreadsheetGrid;
|
|
5
|
+
//# sourceMappingURL=SpreadsheetGrid.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SpreadsheetGrid.svelte.d.ts","sourceRoot":"","sources":["../src/SpreadsheetGrid.svelte"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAa,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAoVjE,QAAA,MAAM,eAAe,0DAAwC,CAAC;AAC9D,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC;AAC1D,eAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package's components: one Svelte 5 component per React component in
|
|
3
|
+
* `@molecule/app-spreadsheet-grid-react`, under the same React name.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export { default as SpreadsheetGrid } from './SpreadsheetGrid.svelte';
|
|
8
|
+
//# sourceMappingURL=components.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,0BAA0B,CAAA"}
|