| 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 {useState} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import './copyFromSelectedColumn.css'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | function copyFromSelectedColumn(e: React.ClipboardEvent<HTMLTableElement>) { |
| b69ab31 | | | 13 | const clipboard = e.clipboardData; |
| b69ab31 | | | 14 | const text = getSelectedColumnText(); |
| b69ab31 | | | 15 | if (!text) { |
| b69ab31 | | | 16 | return; |
| b69ab31 | | | 17 | } |
| b69ab31 | | | 18 | e.preventDefault(); |
| b69ab31 | | | 19 | clipboard.setData('text/plain', text); |
| b69ab31 | | | 20 | } |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | function getSelectedColumnText(numColumns = 4): string | undefined { |
| b69ab31 | | | 23 | const sel = window.getSelection(); |
| b69ab31 | | | 24 | if (sel == null) { |
| b69ab31 | | | 25 | return; |
| b69ab31 | | | 26 | } |
| b69ab31 | | | 27 | const range = sel.getRangeAt(0); |
| b69ab31 | | | 28 | const doc = range.cloneContents(); |
| b69ab31 | | | 29 | const nodes = doc.querySelectorAll('tr'); |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | if (nodes.length === 0) { |
| b69ab31 | | | 32 | return doc.textContent ?? ''; |
| b69ab31 | | | 33 | } |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | let text = ''; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | // We know how many columns are in the table, so each tr should contain ${numColumns} tds. |
| b69ab31 | | | 38 | // But the selection may not be aligned at the start or end: |
| b69ab31 | | | 39 | // start drag here |
| b69ab31 | | | 40 | // v |
| b69ab31 | | | 41 | // CC DDD |
| b69ab31 | | | 42 | // AAA BBB CCC DDD |
| b69ab31 | | | 43 | // AAA BBB CCC DDD |
| b69ab31 | | | 44 | // AAA B |
| b69ab31 | | | 45 | // ^ |
| b69ab31 | | | 46 | // end drag here |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | // We can compute what side we started dragging from by comparing |
| b69ab31 | | | 49 | // the first row's length to the expected length of 4: |
| b69ab31 | | | 50 | const idx = numColumns - nodes[0].children.length; |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | nodes.forEach((tr, i) => { |
| b69ab31 | | | 53 | // The first row will be missing columns before the dragged point, |
| b69ab31 | | | 54 | // so idx will always be 0 for that column. |
| b69ab31 | | | 55 | // The last row also has the wrong number of columns, but idx is still correct |
| b69ab31 | | | 56 | // as long as it's within bounds. |
| b69ab31 | | | 57 | const newIdx = i === 0 ? 0 : idx; |
| b69ab31 | | | 58 | if (newIdx >= 0) { |
| b69ab31 | | | 59 | const td = tr.cells[newIdx]; |
| b69ab31 | | | 60 | if (td) { |
| b69ab31 | | | 61 | text += (i > 0 ? '\n' : '') + td.textContent; |
| b69ab31 | | | 62 | } |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | }); |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | return text; |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | /** |
| b69ab31 | | | 70 | * When using a <table> to render columns of text, add support |
| b69ab31 | | | 71 | * for selecting ( & copying) within any one column. |
| b69ab31 | | | 72 | * Returns props to be forwarded to the table. |
| b69ab31 | | | 73 | * |
| b69ab31 | | | 74 | * Note: you need to add data-column={3} prop (replace 3 with appropriate column index) to your <td>s to get the |
| b69ab31 | | | 75 | * selection to only appear in one column. |
| b69ab31 | | | 76 | */ |
| b69ab31 | | | 77 | export function useTableColumnSelection(): React.TableHTMLAttributes<HTMLTableElement> { |
| b69ab31 | | | 78 | const [selectedColumn, setSelectedColumn] = useState(0); |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | return { |
| b69ab31 | | | 81 | onCopy: copyFromSelectedColumn, |
| b69ab31 | | | 82 | className: `single-column-selection-table selected-column-${selectedColumn}`, |
| b69ab31 | | | 83 | onMouseDown: e => { |
| b69ab31 | | | 84 | const td = findParent('TD', e.target as HTMLElement) as HTMLTableCellElement; |
| b69ab31 | | | 85 | if (td) { |
| b69ab31 | | | 86 | const col = td.cellIndex; |
| b69ab31 | | | 87 | setSelectedColumn(col); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | }, |
| b69ab31 | | | 90 | }; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | function findParent(type: string, start: HTMLElement): HTMLElement { |
| b69ab31 | | | 94 | let el = start; |
| b69ab31 | | | 95 | while (el.tagName !== type) { |
| b69ab31 | | | 96 | if (el.parentElement == null) { |
| b69ab31 | | | 97 | return el; |
| b69ab31 | | | 98 | } |
| b69ab31 | | | 99 | el = el.parentElement; |
| b69ab31 | | | 100 | } |
| b69ab31 | | | 101 | return el; |
| b69ab31 | | | 102 | } |