Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 20x 20x 10x 10x 10x 10x 10x 10x 10x 142x 142x 142x 142x 48x 48x 434x 50x 48x 172x 142x 142x 142x 4x 9x | import {IconButton, ListItemIcon, ListItemText, MenuItem, Stack, TextField} from '@mui/material'
import {useContext, type KeyboardEvent, createContext, type MouseEvent, useMemo, type RefObject} from 'react'
import {Edit, FirstPage, ChevronLeft, ChevronRight, LastPage} from '@mui/icons-material'
import type {FixedTerm, FuzzyTerm} from './term'
import {
DataGrid,
type GridColDef,
type GridCellParams,
GridColumnMenu,
useGridApiRef,
type GridColumnGroupingModel,
type GridColumnMenuProps,
QuickFilter,
QuickFilterControl,
} from '@mui/x-data-grid'
import type {DictEntry} from './storage'
import {EditorTermSetter} from './termEditor'
import type {GridApiCommunity} from '@mui/x-data-grid/internals'
import {CategoryAdder} from './categoryAdder'
export const TableAPIContext = createContext<RefObject<GridApiCommunity> | null>(null)
export type GridRow = {
[index: string]: number | string | FixedTerm | FuzzyTerm | DictEntry
dictEntry: DictEntry
id: string
term: string
sense: string
matches: number
ncats: number
} & (
| {processed: FuzzyTerm}
| {
processed: FixedTerm
sense: string
frequency: string
senses: number
related: number
}
)
export type GridCell = GridRow & {field: string; term_id?: string}
const tableAPI: {ref: RefObject<GridApiCommunity> | undefined} = {ref: undefined}
export function showTableTerm(term: string) {
Eif (tableAPI.ref && tableAPI.ref.current) {
const rowIndex = tableAPI.ref.current.state.sorting.sortedRows.indexOf(term)
tableAPI.ref.current.setPage(Math.floor(rowIndex / 100))
tableAPI.ref.current.selectRow(term, true, true)
requestAnimationFrame(() => {
Eif (tableAPI.ref && tableAPI.ref.current) {
tableAPI.ref.current.scrollToIndexes({rowIndex})
}
})
}
}
function pageActions({
count,
page,
onPageChange,
}: {
count: number
page: number
onPageChange: (e: MouseEvent<HTMLButtonElement>, newPage: number) => void
}) {
const pages = Math.ceil(count / 100) - 1
const onFirstPage = page === 0
const onLastPage = page === pages
return (
<Stack direction="row">
<IconButton aria-label="Go to first page" disabled={onFirstPage} onClick={e => onPageChange(e, 0)}>
<FirstPage />
</IconButton>
<IconButton aria-label="Go to previous page" disabled={onFirstPage} onClick={e => onPageChange(e, page - 1)}>
<ChevronLeft />
</IconButton>
<IconButton aria-label="Go to next page" disabled={onLastPage} onClick={e => onPageChange(e, page + 1)}>
<ChevronRight />
</IconButton>
<IconButton aria-label="Go to last page" disabled={onLastPage} onClick={e => onPageChange(e, pages)}>
<LastPage />
</IconButton>
</Stack>
)
}
export function Table({
rows,
columns,
isCategory,
setEditCategory,
editFromEvent,
}: {
rows: GridRow[]
columns: GridColDef[]
isCategory: (name: string) => boolean
setEditCategory: (name: string) => void
editFromEvent: (index: number, row: GridCell) => void
}) {
const setEditorTerm = useContext(EditorTermSetter)
const api = useGridApiRef()
tableAPI.ref = api as RefObject<GridApiCommunity>
const columnGroups: GridColumnGroupingModel = useMemo(() => {
const categoryGroup: {field: string}[] = []
columns.forEach(({field}) => {
if (field.substring(0, 9) === 'category_') {
categoryGroup.push({field})
}
})
return [
{
groupId: 'term',
headerName: 'Entry',
description: 'Term definition.',
headerClassName: 'column-group',
children: [{field: 'remove'}, {field: 'term'}, {field: 'sense'}],
},
{
groupId: 'descriptives',
headerName: 'Statistics',
description: 'Term descriptive statistics.',
headerClassName: 'column-group',
renderHeaderGroup: () => {
return (
<div>
<span>Statistics</span>
<CategoryAdder />
</div>
)
},
children: [{field: 'frequency'}, {field: 'matches'}, {field: 'senses'}, {field: 'related'}, {field: 'ncats'}],
},
{
groupId: 'categories',
headerName: 'Categories',
description: 'Added dictionary categories.',
headerClassName: 'column-group categories-column',
children: categoryGroup,
},
]
}, [columns])
return (
<DataGrid
apiRef={api}
className="bottom-search"
rows={rows}
columns={columns}
columnGroupingModel={columnGroups}
showCellVerticalBorder
disableDensitySelector
showToolbar
pageSizeOptions={[100]}
density="compact"
sx={{
'& .MuiDataGrid-columnHeader': {overflow: 'visible'},
'& .MuiDataGrid-columnHeaderTitleContainer': {overflow: 'visible'},
'& .column-group': {backgroundColor: '#1f1f1f', maxHeight: '2em'},
'& .MuiDataGrid-columnHeader button': {mr: 1.3},
'& .categories-column .MuiDataGrid-columnHeaderTitleContainerContent': {pl: 2.3},
}}
slots={{
toolbar: () => (
<QuickFilter>
<QuickFilterControl
render={({ref, ...controlProps}) => (
<TextField
sx={{position: 'absolute', bottom: 6, left: 5, zIndex: 1, width: 150}}
{...controlProps}
inputRef={ref}
aria-label="Search"
placeholder="Filter..."
size="small"
/>
)}
/>
</QuickFilter>
),
columnMenu: (props: GridColumnMenuProps) => {
const name = props.colDef.headerName || ''
return isCategory(name) ?
<GridColumnMenu
{...props}
slots={{
columnMenuUserItem: () => {
return (
<MenuItem
onClick={() => {
setEditCategory(name)
}}
>
<ListItemIcon>
<Edit fontSize="small" />
</ListItemIcon>
<ListItemText>Edit category</ListItemText>
</MenuItem>
)
},
}}
slotProps={{
columnMenuIserItem: {
displayOrder: 15,
},
}}
/>
: <GridColumnMenu {...props} />
},
}}
slotProps={{
pagination: {
ActionsComponent: pageActions,
},
}}
onCellKeyDown={(params: GridCellParams, e: KeyboardEvent) => {
Iif (e.key === 'Delete' || e.key === 'Backspace') {
editFromEvent(0, {...params.row, field: params.field})
}
}}
onRowClick={({row}: {row: GridRow}) => {
setEditorTerm(row.id)
}}
/>
)
}
|