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 | 4958x 4958x 4958x 4958x 4958x 4958x 4958x 4958x 380x 4958x 380x 4958x 4958x 10x 10x 10x 10x 10x 10x 10x 4958x 4958x 1312x 1312x 1854x 1854x 1312x 4958x 2602x 2602x 1932x 154x 154x 2602x 4958x 214x 10x 10x 10x 10x 214x 5x 446x 54x 208x 208x 38x 38x 38x 180x 176x | import {Close} from '@mui/icons-material' import { Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, IconButton, Stack, Switch, TextField, Typography, } from '@mui/material' import {DataGrid, type GridCellParams, type GridColDef, QuickFilter, QuickFilterControl} from '@mui/x-data-grid' import {type ChangeEvent, type KeyboardEvent, useCallback, useContext, useEffect, useMemo, useState} from 'react' import {AllCategories, BuildContext, BuildEditContext, CategoryEditContext, type NumberObject} from './building' import type {FixedTerm, FuzzyTerm} from './term' import {getProcessedTerm} from './processTerms' import {ResourceContext} from './resources' import {CategoryWeights} from './categoryWeights' import type {DictEntry} from './storage' import type {GridCell, GridRow} from './table' export function CategoryEditor({category, onClose}: {category: string; onClose: () => void}) { const [showEmptyTerms, setShowEmptyTerms] = useState(false) const categories = useContext(AllCategories) const data = useContext(ResourceContext) const dict = useContext(BuildContext) const editCategories = useContext(CategoryEditContext) const [newName, setNewName] = useState(category) const [lastViewed, setLastViewed] = useState(category) useEffect(() => { setLastViewed(category) }, [category, setLastViewed]) useEffect(() => { setNewName(lastViewed) }, [lastViewed, setNewName]) const editDictionary = useContext(BuildEditContext) const editFromEvent = useCallback( (value: string | number, row: GridCell) => { const {field, processed, dictEntry} = row Eif (field) { const cats = {...dictEntry.categories} Iif (category in cats && !value) { delete cats[category] } else Eif (value) { cats[category] = +value } editDictionary({ type: 'update', term_id: row.id as string, term: processed.term, term_type: processed.term_type, categories: cats, }) } }, [editDictionary, category] ) const ids = useMemo(() => new Map(Object.keys(dict).map((term, index) => [index, term])), [dict]) const catWeights = useMemo(() => { const weights: NumberObject = {} ids.forEach(id => { const {categories} = dict[id] if (category in categories) weights[id] = categories[category] }) return weights }, [dict, ids, category]) const rows = useMemo(() => { const out: { id: string term: string weight: string | number dictEntry: DictEntry processed: FixedTerm | FuzzyTerm }[] = [] ids.forEach(id => { if (showEmptyTerms || id in catWeights) { const entry = dict[id] out.push({ id, term: entry.term ? (entry.sense && id !== entry.term ? entry.term + '@' + entry.sense : entry.term) : id, weight: catWeights[id] || '', dictEntry: entry, processed: getProcessedTerm(id, data, dict), }) } }) return out }, [data, dict, ids, showEmptyTerms, catWeights]) if (!category || !categories.includes(category)) return <></> const cols: GridColDef[] = [ {field: 'term', headerName: 'Term'}, { field: 'weight', headerName: 'Weight', editable: true, hideable: false, valueParser: (value: string | number, row: GridRow, params) => { const parsed = +value || '' Eif (params) { editFromEvent(parsed, {...row, field: params.field}) } return parsed }, }, ] return ( <Dialog open={!!category} onClose={onClose}> <DialogTitle>Category Editor</DialogTitle> <IconButton aria-label="close category editor" onClick={onClose} sx={{ position: 'absolute', right: 8, top: 12, }} className="close-button" > <Close /> </IconButton> <DialogContent sx={{p: 1, width: '400px', maxWidth: '100%', height: '500px'}}> <Stack sx={{height: '100%'}}> <Stack direction="row"> <TextField label="Name" fullWidth value={newName} onChange={(e: ChangeEvent<HTMLInputElement>) => setNewName(e.target.value)} size="small" /> {newName !== category && ( <Button variant="contained" onClick={() => { editDictionary({type: 'rename_category', name: category, newName: newName}) }} > Rename </Button> )} </Stack> <Stack direction="column" spacing={1} sx={{mt: 2, height: '100%'}}> <Typography fontWeight="bold">Term Weights</Typography> <FormControlLabel control={ <Switch size="small" checked={showEmptyTerms} onChange={() => setShowEmptyTerms(!showEmptyTerms)} ></Switch> } label={<Typography variant="caption">Show Empty</Typography>} labelPlacement="start" /> <DataGrid className="bottom-search datagrid-vertical" rows={rows} columns={cols} disableRowSelectionOnClick disableDensitySelector disableColumnMenu showToolbar pageSizeOptions={[100]} density="compact" slots={{ Itoolbar: () => ( <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> ), }} onCellKeyDown={(params: GridCellParams, e: KeyboardEvent) => { Iif ((e.key === 'Delete' || e.key === 'Backspace') && params.cellMode === 'view') { editFromEvent(0, {...params.row, field: params.field}) } }} /> </Stack> </Stack> </DialogContent> <DialogActions> <Stack direction="row" sx={{justifyContent: 'space-between', width: '100%'}}> <Button color="error" variant="contained" onClick={() => { editDictionary({type: 'remove_category', name: category}) editCategories({type: 'remove', cat: category}) onClose() }} > Delete </Button> <CategoryWeights name={category} current={catWeights} edit={editDictionary} /> </Stack> </DialogActions> </Dialog> ) } |