1
0
Fork 0
mirror of synced 2024-09-17 01:38:40 +12:00

Add ability to focus first cell via tab

This commit is contained in:
Andrew Kingston 2023-03-14 10:03:47 +00:00
parent 9a024d96e7
commit 4754be109a

View file

@ -1,24 +1,23 @@
<script> <script>
import { getContext, onMount } from "svelte" import { getContext, onMount } from "svelte"
import { get } from "svelte/store"
const { const {
rows, rows,
selectedCellId, selectedCellId,
columns, visibleColumns,
selectedCellRow, selectedCellRow,
stickyColumn, stickyColumn,
selectedCellAPI, selectedCellAPI,
} = getContext("sheet") } = getContext("sheet")
const handleKeyDown = e => { const handleKeyDown = e => {
if (!get(selectedCellId)) { if (!$selectedCellId) {
if (e.key === "Tab") { if (e.key === "Tab") {
selectFirstCell() selectFirstCell()
} }
return return
} }
const api = get(selectedCellAPI) const api = $selectedCellAPI
// Always intercept certain key presses // Always intercept certain key presses
if (e.key === "Escape") { if (e.key === "Escape") {
@ -60,20 +59,28 @@
} }
const selectFirstCell = () => { const selectFirstCell = () => {
console.log("select first") const firstRow = $rows[0]
if (!firstRow) {
return
}
const firstColumn = $stickyColumn || $visibleColumns[0]
if (!firstColumn) {
return
}
selectedCellId.set(`${firstRow._id}-${firstColumn.name}`)
} }
const changeSelectedColumn = delta => { const changeSelectedColumn = delta => {
if (!$selectedCellId) { if (!$selectedCellId) {
return return
} }
const cols = $columns const cols = $visibleColumns
const split = $selectedCellId.split("-") const split = $selectedCellId.split("-")
const columnName = split[1] const columnName = split[1]
let newColumnName let newColumnName
if (columnName === $stickyColumn?.name) { if (columnName === $stickyColumn?.name) {
const index = delta - 1 const index = delta - 1
newColumnName = $columns[index]?.name newColumnName = cols[index]?.name
} else { } else {
const index = cols.findIndex(col => col.name === columnName) + delta const index = cols.findIndex(col => col.name === columnName) + delta
if (index === -1) { if (index === -1) {