|
| 1 | +// @flow |
| 2 | +import { Trans } from '@lingui/macro'; |
| 3 | +import * as React from 'react'; |
| 4 | +import Tooltip from '@material-ui/core/Tooltip'; |
| 5 | +import LinkIcon from '../../UI/CustomSvgIcons/Link'; |
| 6 | +import UnlinkIcon from '../../UI/CustomSvgIcons/Unlink'; |
| 7 | +import { |
| 8 | + propertiesToGridSelection, |
| 9 | + getEndpointCells, |
| 10 | + handleCellClick, |
| 11 | + gridSelectionToProperties, |
| 12 | +} from './AnchorGridMapping'; |
| 13 | +import styles from './AnchorGrid.module.css'; |
| 14 | + |
| 15 | +type Props = {| |
| 16 | + getPropertyValue: (propertyName: string) => string, |
| 17 | + onUpdateProperty: (propertyName: string, value: string) => void, |
| 18 | +|}; |
| 19 | + |
| 20 | +const gridPositions: Array<{| col: 0 | 1 | 2, row: 0 | 1 | 2 |}> = [ |
| 21 | + { col: 0, row: 0 }, |
| 22 | + { col: 1, row: 0 }, |
| 23 | + { col: 2, row: 0 }, |
| 24 | + { col: 0, row: 1 }, |
| 25 | + { col: 1, row: 1 }, |
| 26 | + { col: 2, row: 1 }, |
| 27 | + { col: 0, row: 2 }, |
| 28 | + { col: 1, row: 2 }, |
| 29 | + { col: 2, row: 2 }, |
| 30 | +]; |
| 31 | + |
| 32 | +const AnchorGrid = ({ |
| 33 | + getPropertyValue, |
| 34 | + onUpdateProperty, |
| 35 | +}: Props): React.Node => { |
| 36 | + // Read the current anchor properties and convert to grid state |
| 37 | + const leftEdgeAnchor = getPropertyValue('leftEdgeAnchor'); |
| 38 | + const rightEdgeAnchor = getPropertyValue('rightEdgeAnchor'); |
| 39 | + const topEdgeAnchor = getPropertyValue('topEdgeAnchor'); |
| 40 | + const bottomEdgeAnchor = getPropertyValue('bottomEdgeAnchor'); |
| 41 | + |
| 42 | + const gridSelection = propertiesToGridSelection( |
| 43 | + leftEdgeAnchor, |
| 44 | + rightEdgeAnchor, |
| 45 | + topEdgeAnchor, |
| 46 | + bottomEdgeAnchor |
| 47 | + ); |
| 48 | + |
| 49 | + const endpointCells = getEndpointCells(gridSelection.rect); |
| 50 | + |
| 51 | + // Store the last non-proportional rect so we can restore it when |
| 52 | + // the user toggles proportional mode off. |
| 53 | + const lastNonProportionalRect = React.useRef(gridSelection.rect); |
| 54 | + if (!gridSelection.proportional && gridSelection.rect) { |
| 55 | + lastNonProportionalRect.current = gridSelection.rect; |
| 56 | + } |
| 57 | + |
| 58 | + const applyGridRect = React.useCallback( |
| 59 | + (rect, proportional) => { |
| 60 | + const properties = gridSelectionToProperties(rect, proportional); |
| 61 | + onUpdateProperty('leftEdgeAnchor', properties.leftEdgeAnchor); |
| 62 | + onUpdateProperty('rightEdgeAnchor', properties.rightEdgeAnchor); |
| 63 | + onUpdateProperty('topEdgeAnchor', properties.topEdgeAnchor); |
| 64 | + onUpdateProperty('bottomEdgeAnchor', properties.bottomEdgeAnchor); |
| 65 | + }, |
| 66 | + [onUpdateProperty] |
| 67 | + ); |
| 68 | + |
| 69 | + const onCellClick = React.useCallback( |
| 70 | + (col: 0 | 1 | 2, row: 0 | 1 | 2) => { |
| 71 | + // If proportional is on, turn it off and select this cell |
| 72 | + if (gridSelection.proportional) { |
| 73 | + const newRect = { minCol: col, maxCol: col, minRow: row, maxRow: row }; |
| 74 | + applyGridRect(newRect, false); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const newRect = handleCellClick(gridSelection.rect, col, row); |
| 79 | + applyGridRect(newRect, false); |
| 80 | + }, |
| 81 | + [gridSelection, applyGridRect] |
| 82 | + ); |
| 83 | + |
| 84 | + const onToggleProportional = React.useCallback( |
| 85 | + () => { |
| 86 | + if (gridSelection.proportional) { |
| 87 | + // Turn off proportional → restore last non-proportional rect |
| 88 | + applyGridRect(lastNonProportionalRect.current, false); |
| 89 | + } else { |
| 90 | + // Turn on proportional |
| 91 | + applyGridRect(gridSelection.rect, true); |
| 92 | + } |
| 93 | + }, |
| 94 | + [gridSelection, applyGridRect] |
| 95 | + ); |
| 96 | + |
| 97 | + const rect = gridSelection.rect; |
| 98 | + const hasRange = |
| 99 | + rect && (rect.minCol !== rect.maxCol || rect.minRow !== rect.maxRow); |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className={styles.wrapper}> |
| 103 | + <div className={styles.grid}> |
| 104 | + {rect && hasRange && ( |
| 105 | + <div |
| 106 | + className={styles.selectionOverlay} |
| 107 | + style={{ |
| 108 | + gridColumn: `${rect.minCol + 1} / ${rect.maxCol + 2}`, |
| 109 | + gridRow: `${rect.minRow + 1} / ${rect.maxRow + 2}`, |
| 110 | + }} |
| 111 | + /> |
| 112 | + )} |
| 113 | + {gridPositions.map(({ col, row }) => { |
| 114 | + const key = `${col},${row}`; |
| 115 | + const isEndpoint = endpointCells.has(key); |
| 116 | + const isInRange = |
| 117 | + hasRange && |
| 118 | + rect && |
| 119 | + col >= rect.minCol && |
| 120 | + col <= rect.maxCol && |
| 121 | + row >= rect.minRow && |
| 122 | + row <= rect.maxRow; |
| 123 | + return ( |
| 124 | + <button |
| 125 | + key={key} |
| 126 | + className={`${styles.cell}${ |
| 127 | + isEndpoint && !hasRange ? ` ${styles.endpoint}` : '' |
| 128 | + }${isInRange ? ` ${styles.inRange}` : ''}`} |
| 129 | + style={{ |
| 130 | + gridColumn: col + 1, |
| 131 | + gridRow: row + 1, |
| 132 | + }} |
| 133 | + onClick={() => onCellClick(col, row)} |
| 134 | + > |
| 135 | + {isEndpoint && <div className={styles.innerSquare} />} |
| 136 | + </button> |
| 137 | + ); |
| 138 | + })} |
| 139 | + </div> |
| 140 | + <div className={styles.linkButton}> |
| 141 | + <Tooltip |
| 142 | + title={ |
| 143 | + gridSelection.proportional ? ( |
| 144 | + <Trans>Disable proportional resize</Trans> |
| 145 | + ) : ( |
| 146 | + <Trans>Enable proportional resize</Trans> |
| 147 | + ) |
| 148 | + } |
| 149 | + > |
| 150 | + <button |
| 151 | + className={`${styles.linkToggle}${ |
| 152 | + gridSelection.proportional ? ` ${styles.active}` : '' |
| 153 | + }`} |
| 154 | + onClick={onToggleProportional} |
| 155 | + > |
| 156 | + {gridSelection.proportional ? ( |
| 157 | + <LinkIcon className={styles.linkIcon} /> |
| 158 | + ) : ( |
| 159 | + <UnlinkIcon className={styles.linkIcon} /> |
| 160 | + )} |
| 161 | + </button> |
| 162 | + </Tooltip> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + ); |
| 166 | +}; |
| 167 | + |
| 168 | +export default AnchorGrid; |
0 commit comments