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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 60x 20x 20x 20x 100x 40x 40x 40x 76x 76x 76x 76x 40x 40x 40x 40x 19x 19x 19x 19x 19x 19x 47x 47x 28x 28x 28x 19x 52x 52x 52x 52x 52x 52x 52x 56x 52x 52x 21x 13x 13x 13x 5x 8x 8x 16x 16x 16x 8x 8x 16x 16x 16x 4x 12x 8x 16x 16x 16x 16x 4x 12x 8x 8x 17x 12x 12x 8x 20x 17x 17x 32x 21x 12x 57x 24x 31x 29x 29x 26x 35x 32x 32x 11x 30x 39x 31x 38x 24x 1540x 1540x 1540x 2162x 2162x 2162x 2162x 2162x 2162x 2162x 3702x 3702x 3702x 2162x 27x 1540x 40x 13x 2162x 40x 40x 1567x 27x 1567x 2162x 2162x 2162x 2162x 2162x 1540x 1540x 1540x 1540x 2218x 2162x 1624x 1540x 2218x 1624x 2378x 1864x 2167x 1555x 2164x 1543x 2168x 1549x 2162x 1540x 2167x 2167x 2167x 1555x 20x 20x 1555x 20x 20x 1555x 1555x | import {Close, Visibility, VisibilityOff} from '@mui/icons-material' import { Box, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControl, FormControlLabel, FormLabel, IconButton, Stack, Switch, TextField, Tooltip, styled, useTheme, } from '@mui/material' import {type ChangeEvent, type DragEvent, type KeyboardEvent, useContext, useState, type ReactNode} from 'react' import {ManageDictionaries, type NumberObject} from './building' import {fileBaseName, newline} from './lib/utils' import {CopyDictionary} from './copyDictionary' import type {Dict} from './storage' const HiddenInput = styled('input')({ clip: 'rect(0 0 0 0)', clipPath: 'inset(50%)', height: 1, overflow: 'hidden', position: 'absolute', bottom: 0, left: 0, whiteSpace: 'nowrap', width: 1, }) const tab = /\t/ const dat = /\s\s/ const padding = /^\s+|\s+$/g const quote_padding = /^["\s]+|["\s]+$/g const dic_seps = /\s{2,}|\t+/g const regex_open = /[[({]/ const regex_close = /[\])}]/ const regex_special = /(?:\\{1,2}\w|[\[\]\(\)?^$.+])/ const senseSep = '@' function containsRegex(term: string) { if (!regex_special.test(term)) return false const open = regex_open.test(term) const close = regex_close.test(term) return (open && close) || (!open && !close) } function makeDictEntry(term: string, cats: NumberObject, sense: string, added: number, detectRegex?: boolean) { return { added, type: (detectRegex && containsRegex(term) ? 'regex' : 'fixed') as 'fixed' | 'regex', categories: cats, sense: sense || '', term, } } function parseDict(raw: string, detectRegex: boolean) { const lastTime = Date.now() const termMap: {[index: string]: Set<string>} = {} const makeId = (term: string) => { let id = term Iif (term in termMap) { id += termMap[term].size + 1 termMap[term].add(id) } else { termMap[term] = new Set() } return id } const parsed: Dict = {} EEif (raw) { try { if (raw[0] === '%') { // assumed to be dic format const lines = raw.split(newline) const categories: {[index: string]: string} = {} const n = lines.length Eif (n > 1) { let i = 1 for (i = 1; i < n; i++) { const line = lines[i].replace(padding, '') if (line === '%') break const parts = line.split(dic_seps) Eif (parts.length > 1 && parts[1]) { categories[parts[0]] = parts[1] } } for (i++; i < n; i++) { const line = lines[i].replace(padding, '') const parts = line.split(dic_seps) Eif (parts.length > 1) { const fullterm = parts.splice(0, 1)[0].toLowerCase() Eif (fullterm) { const cats: NumberObject = {} parts.forEach(index => { Eif (index in categories) cats[categories[index]] = 1 }) const [term, sense] = fullterm.split(senseSep) parsed[makeId(term)] = makeDictEntry(term, cats, sense, lastTime + n - i, detectRegex) } } } } } else if (raw[0] === '{') { // assumed to be JSON const initial = JSON.parse(raw) const keys = Object.keys(initial) if (keys.length && 'type' in initial[keys[0]] && 'string' === typeof initial[keys[0]].type) { // assumed to be a full export return initial } let n = 0 keys.forEach(cat => { Eif (cat) { const terms = initial[cat] if (Array.isArray(terms)) { n += terms.length terms.forEach((fullterm, i) => { Eif (fullterm) { const [term, sense] = fullterm.toLowerCase().split(senseSep) if (term in parsed) { parsed[term].categories[cat] = 1 } else { parsed[term] = makeDictEntry(term, {[cat]: 1}, sense, lastTime + n - i, detectRegex) } } }) } else { Object.keys(terms).forEach((fullterm, i) => { n++ Eif (fullterm) { const [term, sense] = fullterm.toLowerCase().split(senseSep) if (term in parsed) { parsed[term].categories[cat] = terms[fullterm] } else { parsed[term] = makeDictEntry(term, {[cat]: terms[term]}, sense, lastTime + n - i, detectRegex) } } }) } } }) } else { // assumed to be some sort of tabular format const lines = raw.split(newline) const sep = tab.test(lines[0]) ? '\t' : dat.test(lines[0]) ? ' ' : ',' const has_categories = lines[0].split(sep).length > 1 const categories = has_categories ? lines .splice(0, 1)[0] .split(sep) .map(cat => cat.replace(quote_padding, '')) : [] if (has_categories) categories.splice(0, 1) const has_sense = categories[0] === 'term_sense' const n = lines.length lines.forEach((l, i) => { const weights = l.split(sep).map(entry => entry.replace(quote_padding, '')) const term = weights.splice(0, 1)[0].toLowerCase() Eif (term) { let sense = '' const cats: NumberObject = {} weights.forEach((weight, index) => { if (weight && categories[index]) { const value = +weight if (!sense && ((has_sense && index === 0) || isNaN(value))) { sense = weight } else { cats[categories[index]] = +weight } } }) parsed[makeId(term)] = makeDictEntry(term, cats, sense, lastTime + n - i, detectRegex) } }) } } catch {} } return parsed } export function ImportMenu({children}: {children: ReactNode}) { const theme = useTheme() const manageDictionaries = useContext(ManageDictionaries) const [name, setName] = useState('') const [menuOpen, setMenuOpen] = useState(false) const toggleMenu = () => setMenuOpen(!menuOpen) const [detectRegex, setDetectRegex] = useState(false) const [encrypt, setEncrypt] = useState(false) const [rawContent, setRawContent] = useState('') const [password, setPassword] = useState('') const [hide, setHide] = useState(true) const clear = () => { setName('') setRawContent('') EsetPassword('') } const addDict = () => { Eif (name) { const dict = parseDict(rawContent, detectRegex) manageDictionaries({type: 'add', name, dict, password: encrypt ? password : ''}) clear() setMenuOpen(false) } } return ( <> <Button variant="outlined" onClick={toggleMenu}> {children} </Button> <Dialog open={menuOpen} onClose={toggleMenu} onDrop={(e: DragEvent) => { e.preventDefault() if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length) { const file = e.dataTransfer.files[0] const reader = new FileReader() reader.onload = () => { setName(fileBaseName(file.name)) setRawContent(reader.result as string) } reader.readAsText(file) } }} > <DialogTitle>Add Dictionary</DialogTitle> <IconButton aria-label="close import menu" onClick={toggleMenu} sx={{ position: 'absolute', right: 8, top: 12, }} className="close-button" > <Close /> </IconButton> <DialogContent sx={{p: 1}}> <Stack spacing={1}> <TextField size="small" laIbel="Name" value={name} onKeyDown={(e: KeyboardEvent<HTMLDivElement>) => { Iif ((name && e.code === 'Enter') || e.code === 'NumpadEnter') { addDict() } }} onChange={(e: ChangeEvent<HTMLInputElement>) => { setName(e.target.value) }} /> <FormControl> <FormLabel sx={{fontSize: '.8em'}} htmlFor="import_content"> Import Content </FormLabel> <textarea id="import_content" style={{ backgroundColor: theme.palette.background.default, color: theme.palette.text.primary, whiteSpace: rawContent ? 'pre' : 'normal', minWidth: '30em', minHeight: '20em', }} value={rawContent} onChange={(e: ChangeEvent<HTMLTextAreaElement>) => { setRawContent(e.target.value) }} placeholder="drag and drop a file, or enter content directly; see export menu for format examples" /> </FormControl> <Tooltip title="Attempt to convert terms to regular expressions if they contain special RegEx characters." placement="top" > <FormControlLabel label="Detect RegEx" labelPlacement="start" control={<Switch checked={detectRegex} onChange={() => setDetectRegex(!detectRegex)} />} /> </Tooltip> <Tooltip title="Encrypt dictionary and history when being stored." placement="right"> <FormControlLabel label="Encrypt" labelPlacement="start" control={<Switch checked={encrypt} onChange={() => setEncrypt(!encrypt)} />} /> </Tooltip> {encrypt ? ( <Box> <TextField fullWidth size="small" label="Password" type={hide ? 'password' : 'text'} value={password} onChange={(e: ChangeEvent<HTMLInputElement>) => setPassword(e.target.value)} /> <IconButton sx={{position: 'absolute', right: 15}} aria-label="toggle password visibility" onClick={() => setHide(!hide)} > {hide ? <VisibilityOff /> : <Visibility />} </IconButton> </Box> ) : ( <></> )} </Stack> </DialogContent> <DialogActions sx={{justifyContent: 'space-between'}}> <Stack direction="row" spacing={1}> <CopyDictionary setName={setName} setContent={setRawContent} /> <Tooltip title="select a file to import a dictionary from"> <Button variant="outlined" component="label"> File <HidEdenInput type="file" onChange={(e: ChangeEvent<HTMLInputElement>) => { Eif (e.target.files && e.target.files.length) { const file = e.target.files[0] const reader = new FileReader() reader.onload = () => { setName(fileBaseName(file.name)) setRawContent(reader.result as string) e.target.value = '' } reader.readAsText(file) } }} /> </Button> </Tooltip> <Tooltip title="clear current content"> <Button onClick={clear}>clear</Button> </Tooltip> </Stack> <Button variant="contained" disabled={(encrypt ? !password : false) || !name} onClick={addDict}> Add </Button> </DialogActions> </Dialog> </> ) } |