import { makeStyles, Fade, Tooltip } from '@material-ui/core'; import React from 'react'; import { DiffHighlightType, DiffStringOutput, Highlights } from '../../selectors/diff-stringifier'; interface Props { stringifierOutput: DiffStringOutput, flashObj?: T, flashRef?: (n: any) => void, onClick?: (path: (string | number)[]) => void isClickable?: (path: (string | number)[]) => boolean } const useStyles = makeStyles({ pre: { wordWrap: 'break-word', whiteSpace: 'pre-wrap', wordBreak: 'break-all', overflow: 'hidden', maxWidth: '100%', margin: 0, }, }); const diffTypeToStyle: Record = { [DiffHighlightType.Insert]: {backgroundColor: '#0f0'}, [DiffHighlightType.Remove]: {backgroundColor: '#f88'}, [DiffHighlightType.Update]: {border: '2px solid #00f'}, }; const flashStyle = { backgroundColor: 'yellow', boxShadow: '0 0 0.5em 0.5em yellow', transition: 'background-color 2s ease-in-out, box-shadow 2s ease-in-out', }; const clickableStyle = { color: '#0066CC', cursor: 'pointer', textDecoration: 'underline', textDecorationSkip: 'spaces', }; export default function DiffSection( {stringifierOutput, flashObj, flashRef, onClick, isClickable}: Props, ): JSX.Element { const classes = useStyles(); return <>{stringifierOutput.map((chunk, i) => { if(Array.isArray(chunk)) { const hasFlashObj = chunk.some(e => !Array.isArray(e) && highlightsIncludeObj(e.highlights, flashObj)); return
; } else { const {str, highlights} = chunk; return Object.entries(highlights).length ? onClick && onClick(chunk.path)} style={{ ...makeHighlightStyles(Object.keys(highlights) as DiffHighlightType[]), ...(isClickable && isClickable(chunk.path)) ? clickableStyle : {}, }}> {str} : onClick && onClick(chunk.path)}>{str}; } })}; } function highlightsIncludeObj(highlights: Highlights, op?: T){ if(op === undefined) return false; return Object.values(highlights).flat().some(o => o === op); } function makeHighlightDescriptions(highlights: Highlights): string { return Object.keys(highlights).join(', '); } function makeHighlightStyles(types: DiffHighlightType[]) { return Object.assign({}, ...types.map(h => diffTypeToStyle[h])); }