| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ChunkSelectState, LineRegion, SelectLine} from './stackEdit/chunkSelectState'; |
| b69ab31 | | | 9 | import type {RangeInfo} from './stackEdit/ui/TextEditable'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {Set as ImSet} from 'immutable'; |
| b69ab31 | | | 12 | import {Checkbox} from 'isl-components/Checkbox'; |
| b69ab31 | | | 13 | import {RadioGroup} from 'isl-components/Radio'; |
| b69ab31 | | | 14 | import {useRef, useState} from 'react'; |
| b69ab31 | | | 15 | import {notEmpty} from 'shared/utils'; |
| b69ab31 | | | 16 | import {t} from './i18n'; |
| b69ab31 | | | 17 | import {TextEditable} from './stackEdit/ui/TextEditable'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | import './PartialFileSelection.css'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | type Props = { |
| b69ab31 | | | 22 | chunkSelection: ChunkSelectState; |
| b69ab31 | | | 23 | setChunkSelection: (state: ChunkSelectState) => void; |
| b69ab31 | | | 24 | }; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | export type PartialFileEditMode = 'unified' | 'side-by-side' | 'free-edit'; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | export function PartialFileSelection(props: Props) { |
| b69ab31 | | | 29 | const [editMode, setEditMode] = useState<PartialFileEditMode>('unified'); |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | return ( |
| b69ab31 | | | 32 | <div> |
| b69ab31 | | | 33 | <RadioGroup |
| b69ab31 | | | 34 | choices={[ |
| b69ab31 | | | 35 | {title: t('Unified'), value: 'unified'}, |
| b69ab31 | | | 36 | {title: t('Side-by-side'), value: 'side-by-side'}, |
| b69ab31 | | | 37 | {title: t('Freeform edit'), value: 'free-edit'}, |
| b69ab31 | | | 38 | ]} |
| b69ab31 | | | 39 | current={editMode} |
| b69ab31 | | | 40 | onChange={setEditMode} |
| b69ab31 | | | 41 | /> |
| b69ab31 | | | 42 | <PartialFileSelectionWithMode {...props} mode={editMode} /> |
| b69ab31 | | | 43 | </div> |
| b69ab31 | | | 44 | ); |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | export function PartialFileSelectionWithMode(props: Props & {mode: PartialFileEditMode}) { |
| b69ab31 | | | 48 | if (props.mode === 'unified') { |
| b69ab31 | | | 49 | return <PartialFileSelectionWithCheckbox {...props} unified={true} />; |
| b69ab31 | | | 50 | } else if (props.mode === 'side-by-side') { |
| b69ab31 | | | 51 | return <PartialFileSelectionWithCheckbox {...props} unified={false} />; |
| b69ab31 | | | 52 | } else { |
| b69ab31 | | | 53 | return <PartialFileSelectionWithFreeEdit {...props} />; |
| b69ab31 | | | 54 | } |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | /** Show chunks with selection checkboxes. Supports unified and side-by-side modes. */ |
| b69ab31 | | | 58 | function PartialFileSelectionWithCheckbox(props: Props & {unified?: boolean}) { |
| b69ab31 | | | 59 | const unified = props.unified ?? true; |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | // State for dragging on line numbers for range selection. |
| b69ab31 | | | 62 | const lastLine = useRef<SelectLine | null>(null); |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | // Toggle selection of a line or a region. |
| b69ab31 | | | 65 | const toggleLineOrRegion = (line: SelectLine, region: LineRegion | null) => { |
| b69ab31 | | | 66 | const selected = !line.selected; |
| b69ab31 | | | 67 | const lineSelects: Array<[number, boolean]> = []; |
| b69ab31 | | | 68 | if (region) { |
| b69ab31 | | | 69 | region.lines.forEach(line => { |
| b69ab31 | | | 70 | lineSelects.push([line.rawIndex, selected]); |
| b69ab31 | | | 71 | }); |
| b69ab31 | | | 72 | } else { |
| b69ab31 | | | 73 | lineSelects.push([line.rawIndex, selected]); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | const newSelection = props.chunkSelection.setSelectedLines(lineSelects); |
| b69ab31 | | | 76 | lastLine.current = line; |
| b69ab31 | | | 77 | props.setChunkSelection(newSelection); |
| b69ab31 | | | 78 | }; |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | const handlePointerDown = ( |
| b69ab31 | | | 81 | line: SelectLine, |
| b69ab31 | | | 82 | region: LineRegion | null, |
| b69ab31 | | | 83 | e: React.PointerEvent, |
| b69ab31 | | | 84 | ) => { |
| b69ab31 | | | 85 | if (e.isPrimary && line.selected !== null) { |
| b69ab31 | | | 86 | toggleLineOrRegion(line, region); |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | }; |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | // Toggle selection of a single line. |
| b69ab31 | | | 91 | const handlePointerEnter = (line: SelectLine, e: React.PointerEvent<HTMLDivElement>) => { |
| b69ab31 | | | 92 | if (e.buttons === 1 && line.selected !== null && lastLine.current?.rawIndex !== line.rawIndex) { |
| b69ab31 | | | 93 | const newSelection = props.chunkSelection.setSelectedLines([[line.rawIndex, !line.selected]]); |
| b69ab31 | | | 94 | lastLine.current = line; |
| b69ab31 | | | 95 | props.setChunkSelection(newSelection); |
| b69ab31 | | | 96 | } |
| b69ab31 | | | 97 | }; |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | const lineCheckbox: JSX.Element[] = []; |
| b69ab31 | | | 100 | const lineANumber: JSX.Element[] = []; |
| b69ab31 | | | 101 | const lineBNumber: JSX.Element[] = []; |
| b69ab31 | | | 102 | const lineAContent: JSX.Element[] = []; // side by side left, or unified |
| b69ab31 | | | 103 | const lineBContent: JSX.Element[] = unified ? lineAContent : []; // side by side right |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | const lineRegions = props.chunkSelection.getLineRegions(); |
| b69ab31 | | | 106 | lineRegions.forEach((region, regionIndex) => { |
| b69ab31 | | | 107 | const key = region.lines[0].rawIndex; |
| b69ab31 | | | 108 | if (region.collapsed) { |
| b69ab31 | | | 109 | // Skip "~~~~" for the first and last collapsed region. |
| b69ab31 | | | 110 | if (regionIndex > 0 && regionIndex + 1 < lineRegions.length) { |
| b69ab31 | | | 111 | lineAContent.push(<td key={'line-a' + key} className="line line-context" />); |
| b69ab31 | | | 112 | if (!unified) { |
| b69ab31 | | | 113 | lineBContent.push(<td key={'line-b' + key} className="line line-context" />); |
| b69ab31 | | | 114 | } |
| b69ab31 | | | 115 | lineCheckbox.push(<td key="c" />); |
| b69ab31 | | | 116 | lineANumber.push(<td key="anum" />); |
| b69ab31 | | | 117 | lineBNumber.push(<td key="bnum" />); |
| b69ab31 | | | 118 | } |
| b69ab31 | | | 119 | return; |
| b69ab31 | | | 120 | } |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | let hasPushedCheckbox = false; |
| b69ab31 | | | 123 | if (!region.same) { |
| b69ab31 | | | 124 | const selectableCount = region.lines.reduce( |
| b69ab31 | | | 125 | (acc, line) => acc + (line.selected != null ? 1 : 0), |
| b69ab31 | | | 126 | 0, |
| b69ab31 | | | 127 | ); |
| b69ab31 | | | 128 | if (selectableCount > 0) { |
| b69ab31 | | | 129 | const selectedCount = region.lines.reduce((acc, line) => acc + (line.selected ? 1 : 0), 0); |
| b69ab31 | | | 130 | const indeterminate = selectedCount > 0 && selectedCount < selectableCount; |
| b69ab31 | | | 131 | const checked = selectedCount === selectableCount; |
| b69ab31 | | | 132 | lineCheckbox.push( |
| b69ab31 | | | 133 | <td className="checkbox-anchor" key={`${key}c`}> |
| b69ab31 | | | 134 | <div className="checkbox-container"> |
| b69ab31 | | | 135 | <Checkbox |
| b69ab31 | | | 136 | checked={checked} |
| b69ab31 | | | 137 | indeterminate={indeterminate} |
| b69ab31 | | | 138 | onChange={() => { |
| b69ab31 | | | 139 | toggleLineOrRegion(region.lines[0], region); |
| b69ab31 | | | 140 | }} |
| b69ab31 | | | 141 | /> |
| b69ab31 | | | 142 | </div> |
| b69ab31 | | | 143 | </td>, |
| b69ab31 | | | 144 | ); |
| b69ab31 | | | 145 | } |
| b69ab31 | | | 146 | hasPushedCheckbox = true; |
| b69ab31 | | | 147 | } |
| b69ab31 | | | 148 | |
| b69ab31 | | | 149 | let regionALineCount = 0; |
| b69ab31 | | | 150 | let regionBLineCount = 0; |
| b69ab31 | | | 151 | region.lines.forEach(line => { |
| b69ab31 | | | 152 | const lineClasses = ['line']; |
| b69ab31 | | | 153 | const isAdd = line.sign.includes('+'); |
| b69ab31 | | | 154 | if (isAdd) { |
| b69ab31 | | | 155 | lineClasses.push('line-add'); |
| b69ab31 | | | 156 | } else if (line.sign.includes('-')) { |
| b69ab31 | | | 157 | lineClasses.push('line-del'); |
| b69ab31 | | | 158 | } |
| b69ab31 | | | 159 | |
| b69ab31 | | | 160 | const lineNumberClasses = ['line-number']; |
| b69ab31 | | | 161 | if (line.selected != null) { |
| b69ab31 | | | 162 | lineNumberClasses.push('selectable'); |
| b69ab31 | | | 163 | } |
| b69ab31 | | | 164 | if (line.selected) { |
| b69ab31 | | | 165 | lineNumberClasses.push('selected'); |
| b69ab31 | | | 166 | } |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | const hasA = unified || line.aLine != null; |
| b69ab31 | | | 169 | const hasB = |
| b69ab31 | | | 170 | unified || |
| b69ab31 | | | 171 | line.bLine != null || |
| b69ab31 | | | 172 | isAdd; /* isAdd is for "line.bits == 0b010", added by manual editing */ |
| b69ab31 | | | 173 | const key = line.rawIndex; |
| b69ab31 | | | 174 | const handlerProps = { |
| b69ab31 | | | 175 | onPointerDown: handlePointerDown.bind(null, line, null), |
| b69ab31 | | | 176 | onPointerEnter: handlePointerEnter.bind(null, line), |
| b69ab31 | | | 177 | }; |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | if (hasA) { |
| b69ab31 | | | 180 | lineANumber.push( |
| b69ab31 | | | 181 | <td |
| b69ab31 | | | 182 | key={'line-a-num-' + key} |
| b69ab31 | | | 183 | className={'column-a-number ' + lineNumberClasses.join(' ')} |
| b69ab31 | | | 184 | {...handlerProps}> |
| b69ab31 | | | 185 | {line.aLine} |
| b69ab31 | | | 186 | {'\n'} |
| b69ab31 | | | 187 | </td>, |
| b69ab31 | | | 188 | ); |
| b69ab31 | | | 189 | lineAContent.push( |
| b69ab31 | | | 190 | <td key={'line-a-' + key} className={lineClasses.join(' ')}> |
| b69ab31 | | | 191 | {line.data} |
| b69ab31 | | | 192 | </td>, |
| b69ab31 | | | 193 | ); |
| b69ab31 | | | 194 | regionALineCount += 1; |
| b69ab31 | | | 195 | } |
| b69ab31 | | | 196 | if (hasB) { |
| b69ab31 | | | 197 | lineBNumber.push( |
| b69ab31 | | | 198 | <td |
| b69ab31 | | | 199 | key={'line-b-num-' + key} |
| b69ab31 | | | 200 | className={'column-b-number ' + lineNumberClasses.join(' ')} |
| b69ab31 | | | 201 | {...handlerProps}> |
| b69ab31 | | | 202 | {line.bLine} |
| b69ab31 | | | 203 | {'\n'} |
| b69ab31 | | | 204 | </td>, |
| b69ab31 | | | 205 | ); |
| b69ab31 | | | 206 | if (!unified) { |
| b69ab31 | | | 207 | lineBContent.push( |
| b69ab31 | | | 208 | <td key={'line-b-' + key} className={lineClasses.join(' ')}> |
| b69ab31 | | | 209 | {line.data} |
| b69ab31 | | | 210 | </td>, |
| b69ab31 | | | 211 | ); |
| b69ab31 | | | 212 | regionBLineCount += 1; |
| b69ab31 | | | 213 | } |
| b69ab31 | | | 214 | } |
| b69ab31 | | | 215 | }); |
| b69ab31 | | | 216 | |
| b69ab31 | | | 217 | if (!unified) { |
| b69ab31 | | | 218 | let columns: JSX.Element[][] = []; |
| b69ab31 | | | 219 | let count = 0; |
| b69ab31 | | | 220 | if (regionALineCount < regionBLineCount) { |
| b69ab31 | | | 221 | columns = [lineANumber, lineAContent]; |
| b69ab31 | | | 222 | count = regionBLineCount - regionALineCount; |
| b69ab31 | | | 223 | } else if (regionALineCount > regionBLineCount) { |
| b69ab31 | | | 224 | columns = [lineBNumber, lineBContent]; |
| b69ab31 | | | 225 | count = regionALineCount - regionBLineCount; |
| b69ab31 | | | 226 | } |
| b69ab31 | | | 227 | for (let i = 0; i < count; i++) { |
| b69ab31 | | | 228 | columns.forEach(column => column.push(<td key={`${key}-pad-${i}`}>{'\n'}</td>)); |
| b69ab31 | | | 229 | } |
| b69ab31 | | | 230 | } |
| b69ab31 | | | 231 | |
| b69ab31 | | | 232 | for (let i = hasPushedCheckbox ? 1 : 0; i < Math.max(regionALineCount, regionBLineCount); i++) { |
| b69ab31 | | | 233 | lineCheckbox.push(<td key={`${key}-pad-${i}`}>{'\n'}</td>); |
| b69ab31 | | | 234 | } |
| b69ab31 | | | 235 | }); |
| b69ab31 | | | 236 | |
| b69ab31 | | | 237 | return ( |
| b69ab31 | | | 238 | <> |
| b69ab31 | | | 239 | <table className="partial-file-selection checkboxes"> |
| b69ab31 | | | 240 | <colgroup> |
| b69ab31 | | | 241 | <col width={'3em'} /> |
| b69ab31 | | | 242 | <col width={'3em'} /> |
| b69ab31 | | | 243 | <col width={'40px'} /> |
| b69ab31 | | | 244 | <col width={'100%'} /> |
| b69ab31 | | | 245 | </colgroup> |
| b69ab31 | | | 246 | <tbody> |
| b69ab31 | | | 247 | {lineAContent.map((line, i) => { |
| b69ab31 | | | 248 | return ( |
| b69ab31 | | | 249 | <tr key={i} className="column-unified"> |
| b69ab31 | | | 250 | {lineCheckbox[i]} |
| b69ab31 | | | 251 | {lineANumber[i]} |
| b69ab31 | | | 252 | {lineBNumber[i]} |
| b69ab31 | | | 253 | {line} |
| b69ab31 | | | 254 | </tr> |
| b69ab31 | | | 255 | ); |
| b69ab31 | | | 256 | })} |
| b69ab31 | | | 257 | </tbody> |
| b69ab31 | | | 258 | </table> |
| b69ab31 | | | 259 | </> |
| b69ab31 | | | 260 | ); |
| b69ab31 | | | 261 | } |
| b69ab31 | | | 262 | |
| b69ab31 | | | 263 | /** Show 3 editors side-by-side: `|A|M|B|`. `M` allows editing. No checkboxes. */ |
| b69ab31 | | | 264 | function PartialFileSelectionWithFreeEdit(props: Props) { |
| b69ab31 | | | 265 | // States for context line expansion. |
| b69ab31 | | | 266 | const [expandedALines, setExpandedALines] = useState<ImSet<number>>(ImSet); |
| b69ab31 | | | 267 | const [currentCaretLine, setCurrentSelLine] = useState<number>(-1); |
| b69ab31 | | | 268 | |
| b69ab31 | | | 269 | const lineRegions = props.chunkSelection.getLineRegions({ |
| b69ab31 | | | 270 | expandedALines, |
| b69ab31 | | | 271 | expandedSelLine: currentCaretLine, |
| b69ab31 | | | 272 | }); |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | // Needed by TextEditable. Ranges of text on the right side. |
| b69ab31 | | | 275 | const rangeInfos: RangeInfo[] = []; |
| b69ab31 | | | 276 | let start = 0; |
| b69ab31 | | | 277 | |
| b69ab31 | | | 278 | // Render the rows. |
| b69ab31 | | | 279 | // We draw 3 editors: A (working parent), M (selected), B (working copy). |
| b69ab31 | | | 280 | // A and B are read-only. M is editable. The user selects content from |
| b69ab31 | | | 281 | // either A or B to change content of M. |
| b69ab31 | | | 282 | const lineAContent: JSX.Element[] = []; |
| b69ab31 | | | 283 | const lineBContent: JSX.Element[] = []; |
| b69ab31 | | | 284 | const lineMContent: JSX.Element[] = []; |
| b69ab31 | | | 285 | const lineANumber: JSX.Element[] = []; |
| b69ab31 | | | 286 | const lineBNumber: JSX.Element[] = []; |
| b69ab31 | | | 287 | const lineMNumber: JSX.Element[] = []; |
| b69ab31 | | | 288 | |
| b69ab31 | | | 289 | const insertContextLines = (lines: Readonly<SelectLine[]>) => { |
| b69ab31 | | | 290 | const handleExpand = () => { |
| b69ab31 | | | 291 | // Only the "unchanged" lines need expansion. |
| b69ab31 | | | 292 | // We use line numbers on the "a" side, which remains "stable" regardless of editing. |
| b69ab31 | | | 293 | const newLines = lines.map(l => l.aLine).filter(notEmpty); |
| b69ab31 | | | 294 | const newSet = expandedALines.union(newLines); |
| b69ab31 | | | 295 | setExpandedALines(newSet); |
| b69ab31 | | | 296 | }; |
| b69ab31 | | | 297 | const key = lines[0].rawIndex; |
| b69ab31 | | | 298 | const contextLineContent = ( |
| b69ab31 | | | 299 | <div |
| b69ab31 | | | 300 | key={key} |
| b69ab31 | | | 301 | className="line line-context" |
| b69ab31 | | | 302 | title={t('Click to expand lines.')} |
| b69ab31 | | | 303 | onClick={handleExpand} |
| b69ab31 | | | 304 | /> |
| b69ab31 | | | 305 | ); |
| b69ab31 | | | 306 | lineAContent.push(contextLineContent); |
| b69ab31 | | | 307 | lineBContent.push(contextLineContent); |
| b69ab31 | | | 308 | lineMContent.push(contextLineContent); |
| b69ab31 | | | 309 | const contextLineNumber = <div key={key} className="line-number line-context" />; |
| b69ab31 | | | 310 | lineANumber.push(contextLineNumber); |
| b69ab31 | | | 311 | lineBNumber.push(contextLineNumber); |
| b69ab31 | | | 312 | lineMNumber.push(contextLineNumber); |
| b69ab31 | | | 313 | }; |
| b69ab31 | | | 314 | |
| b69ab31 | | | 315 | lineRegions.forEach(region => { |
| b69ab31 | | | 316 | if (region.collapsed) { |
| b69ab31 | | | 317 | // Draw "~~~" between chunks. |
| b69ab31 | | | 318 | insertContextLines(region.lines); |
| b69ab31 | | | 319 | } |
| b69ab31 | | | 320 | |
| b69ab31 | | | 321 | const regionClass = region.same ? 'region-same' : 'region-diff'; |
| b69ab31 | | | 322 | |
| b69ab31 | | | 323 | let regionALineCount = 0; |
| b69ab31 | | | 324 | let regionBLineCount = 0; |
| b69ab31 | | | 325 | let regionMLineCount = 0; |
| b69ab31 | | | 326 | region.lines.forEach(line => { |
| b69ab31 | | | 327 | let dataRangeId = undefined; |
| b69ab31 | | | 328 | // Provide `RangeInfo` for editing, if the line exists in the selection version. |
| b69ab31 | | | 329 | // This is also needed for "~~~" context lines. |
| b69ab31 | | | 330 | if (line.selLine !== null) { |
| b69ab31 | | | 331 | const end = start + line.data.length; |
| b69ab31 | | | 332 | dataRangeId = rangeInfos.length; |
| b69ab31 | | | 333 | rangeInfos.push({start, end}); |
| b69ab31 | | | 334 | start = end; |
| b69ab31 | | | 335 | } |
| b69ab31 | | | 336 | |
| b69ab31 | | | 337 | if (region.collapsed) { |
| b69ab31 | | | 338 | return; |
| b69ab31 | | | 339 | } |
| b69ab31 | | | 340 | |
| b69ab31 | | | 341 | // Draw the actual line and line numbers. |
| b69ab31 | | | 342 | let lineAClass = 'line line-a'; |
| b69ab31 | | | 343 | let lineBClass = 'line line-b'; |
| b69ab31 | | | 344 | let lineMClass = 'line line-m'; |
| b69ab31 | | | 345 | |
| b69ab31 | | | 346 | // Find the "unique" lines (different with other versions). They will be highlighted. |
| b69ab31 | | | 347 | switch (line.bits) { |
| b69ab31 | | | 348 | case 0b100: |
| b69ab31 | | | 349 | lineAClass += ' line-unique'; |
| b69ab31 | | | 350 | break; |
| b69ab31 | | | 351 | case 0b010: |
| b69ab31 | | | 352 | lineMClass += ' line-unique'; |
| b69ab31 | | | 353 | break; |
| b69ab31 | | | 354 | case 0b001: |
| b69ab31 | | | 355 | lineBClass += ' line-unique'; |
| b69ab31 | | | 356 | break; |
| b69ab31 | | | 357 | } |
| b69ab31 | | | 358 | |
| b69ab31 | | | 359 | const key = line.rawIndex; |
| b69ab31 | | | 360 | if (line.aLine !== null) { |
| b69ab31 | | | 361 | lineAContent.push( |
| b69ab31 | | | 362 | <div key={key} className={`${lineAClass} ${regionClass}`}> |
| b69ab31 | | | 363 | {line.data} |
| b69ab31 | | | 364 | </div>, |
| b69ab31 | | | 365 | ); |
| b69ab31 | | | 366 | lineANumber.push( |
| b69ab31 | | | 367 | <div key={key} className={`line-number line-a ${regionClass}`}> |
| b69ab31 | | | 368 | {line.aLine} |
| b69ab31 | | | 369 | </div>, |
| b69ab31 | | | 370 | ); |
| b69ab31 | | | 371 | regionALineCount += 1; |
| b69ab31 | | | 372 | } |
| b69ab31 | | | 373 | if (line.bLine !== null) { |
| b69ab31 | | | 374 | lineBContent.push( |
| b69ab31 | | | 375 | <div key={key} className={`${lineBClass} ${regionClass}`}> |
| b69ab31 | | | 376 | {line.data} |
| b69ab31 | | | 377 | </div>, |
| b69ab31 | | | 378 | ); |
| b69ab31 | | | 379 | lineBNumber.push( |
| b69ab31 | | | 380 | <div key={key} className={`line-number line-b ${regionClass}`}> |
| b69ab31 | | | 381 | {line.bLine} |
| b69ab31 | | | 382 | </div>, |
| b69ab31 | | | 383 | ); |
| b69ab31 | | | 384 | regionBLineCount += 1; |
| b69ab31 | | | 385 | } |
| b69ab31 | | | 386 | if (line.selLine !== null) { |
| b69ab31 | | | 387 | lineMContent.push( |
| b69ab31 | | | 388 | <div key={key} className={`${lineMClass} ${regionClass}`} data-range-id={dataRangeId}> |
| b69ab31 | | | 389 | {line.data} |
| b69ab31 | | | 390 | </div>, |
| b69ab31 | | | 391 | ); |
| b69ab31 | | | 392 | lineMNumber.push( |
| b69ab31 | | | 393 | <div key={key} className={`line-number line-m ${regionClass}`}> |
| b69ab31 | | | 394 | {line.selLine} |
| b69ab31 | | | 395 | </div>, |
| b69ab31 | | | 396 | ); |
| b69ab31 | | | 397 | regionMLineCount += 1; |
| b69ab31 | | | 398 | } |
| b69ab31 | | | 399 | }); |
| b69ab31 | | | 400 | |
| b69ab31 | | | 401 | // Add padding lines to align the "bottom" of the region. |
| b69ab31 | | | 402 | const regionPadLineCount = Math.max(regionALineCount, regionBLineCount, regionMLineCount); |
| b69ab31 | | | 403 | const key = region.lines[0].rawIndex; |
| b69ab31 | | | 404 | ( |
| b69ab31 | | | 405 | [ |
| b69ab31 | | | 406 | [lineAContent, lineANumber, regionALineCount], |
| b69ab31 | | | 407 | [lineBContent, lineBNumber, regionBLineCount], |
| b69ab31 | | | 408 | [lineMContent, lineMNumber, regionMLineCount], |
| b69ab31 | | | 409 | ] as [JSX.Element[], JSX.Element[], number][] |
| b69ab31 | | | 410 | ).forEach(([lineContent, lineNumber, lineCount]) => { |
| b69ab31 | | | 411 | for (let i = 0; i < regionPadLineCount - lineCount; i++) { |
| b69ab31 | | | 412 | lineContent.push( |
| b69ab31 | | | 413 | <div key={`${key}-pad-${i}`} className="line"> |
| b69ab31 | | | 414 | {'\n'} |
| b69ab31 | | | 415 | </div>, |
| b69ab31 | | | 416 | ); |
| b69ab31 | | | 417 | lineNumber.push( |
| b69ab31 | | | 418 | <div key={`${key}-pad-${i}`} className="line-number"> |
| b69ab31 | | | 419 | {'\n'} |
| b69ab31 | | | 420 | </div>, |
| b69ab31 | | | 421 | ); |
| b69ab31 | | | 422 | } |
| b69ab31 | | | 423 | }); |
| b69ab31 | | | 424 | }); |
| b69ab31 | | | 425 | |
| b69ab31 | | | 426 | const textValue = props.chunkSelection.getSelectedText(); |
| b69ab31 | | | 427 | const handleTextChange = (text: string) => { |
| b69ab31 | | | 428 | const newChunkSelect = props.chunkSelection.setSelectedText(text); |
| b69ab31 | | | 429 | props.setChunkSelection(newChunkSelect); |
| b69ab31 | | | 430 | }; |
| b69ab31 | | | 431 | const handleSelChange = (start: number, end: number) => { |
| b69ab31 | | | 432 | // Expand the line of the cursor. But do not expand on range selection (ex. Ctrl+A). |
| b69ab31 | | | 433 | if (start === end) { |
| b69ab31 | | | 434 | let selLine = countLines(textValue.substring(0, start)); |
| b69ab31 | | | 435 | if (start == textValue.length && textValue.endsWith('\n')) { |
| b69ab31 | | | 436 | selLine -= 1; |
| b69ab31 | | | 437 | } |
| b69ab31 | | | 438 | setCurrentSelLine(selLine); |
| b69ab31 | | | 439 | } |
| b69ab31 | | | 440 | }; |
| b69ab31 | | | 441 | |
| b69ab31 | | | 442 | return ( |
| b69ab31 | | | 443 | <div className="partial-file-selection-width-min-content"> |
| b69ab31 | | | 444 | <div className="partial-file-selection-scroll-y"> |
| b69ab31 | | | 445 | <div className="partial-file-selection free-form"> |
| b69ab31 | | | 446 | <pre className="column-a-number readonly">{lineANumber}</pre> |
| b69ab31 | | | 447 | <div className="partial-file-selection-scroll-x readonly"> |
| b69ab31 | | | 448 | <pre className="column-a">{lineAContent}</pre> |
| b69ab31 | | | 449 | </div> |
| b69ab31 | | | 450 | <pre className="column-m-number">{lineMNumber}</pre> |
| b69ab31 | | | 451 | <div className="partial-file-selection-scroll-x"> |
| b69ab31 | | | 452 | <TextEditable |
| b69ab31 | | | 453 | value={textValue} |
| b69ab31 | | | 454 | rangeInfos={rangeInfos} |
| b69ab31 | | | 455 | onTextChange={handleTextChange} |
| b69ab31 | | | 456 | onSelectChange={handleSelChange}> |
| b69ab31 | | | 457 | <pre className="column-m">{lineMContent}</pre> |
| b69ab31 | | | 458 | </TextEditable> |
| b69ab31 | | | 459 | </div> |
| b69ab31 | | | 460 | <pre className="column-b-number readonly">{lineBNumber}</pre> |
| b69ab31 | | | 461 | <div className="partial-file-selection-scroll-x readonly"> |
| b69ab31 | | | 462 | <pre className="column-b">{lineBContent}</pre> |
| b69ab31 | | | 463 | </div> |
| b69ab31 | | | 464 | </div> |
| b69ab31 | | | 465 | </div> |
| b69ab31 | | | 466 | </div> |
| b69ab31 | | | 467 | ); |
| b69ab31 | | | 468 | } |
| b69ab31 | | | 469 | |
| b69ab31 | | | 470 | function countLines(text: string): number { |
| b69ab31 | | | 471 | let result = 1; |
| b69ab31 | | | 472 | for (const ch of text) { |
| b69ab31 | | | 473 | if (ch === '\n') { |
| b69ab31 | | | 474 | result++; |
| b69ab31 | | | 475 | } |
| b69ab31 | | | 476 | } |
| b69ab31 | | | 477 | return result; |
| b69ab31 | | | 478 | } |