({
address,
screen,
target,
parent,
depth,
...options
} = {})
| 224 | // |
| 225 | // note: 'screen' and 'target' should have their corners be relative to the view |
| 226 | export const replaceAddress = ({ |
| 227 | address, |
| 228 | screen, |
| 229 | target, |
| 230 | parent, |
| 231 | depth, |
| 232 | ...options |
| 233 | } = {}) => { |
| 234 | const oldScreen = getScreenFromAddress(address); |
| 235 | const targetedCorners = getMappedPositions(screen.corners, target.corners); |
| 236 | const targetedScreen = makeScreen(screen.colour, targetedCorners); |
| 237 | |
| 238 | // Pick where to place the screen |
| 239 | const pickOptions = { ...options, ignore: oldScreen, ignoreDepth: depth }; |
| 240 | let picks = targetedScreen.corners.map((corner) => |
| 241 | pickInScreen(target, corner, pickOptions) |
| 242 | ); |
| 243 | let isStillWithParent = picks.some((pick) => pick.screen === parent); |
| 244 | |
| 245 | if (!isStillWithParent) { |
| 246 | const snapPicks = targetedScreen.corners.map((corner) => |
| 247 | pickInScreen(target, corner, { ...pickOptions, snap: parent }) |
| 248 | ); |
| 249 | const snapIsStillWithParent = snapPicks.some( |
| 250 | (pick) => pick.screen === parent |
| 251 | ); |
| 252 | if (snapIsStillWithParent) { |
| 253 | const depth = Math.min(...picks.map((pick) => pick.depth)); |
| 254 | const snapDepth = Math.min(...snapPicks.map((pick) => pick.depth)); |
| 255 | if (snapDepth >= depth) { |
| 256 | picks = snapPicks; |
| 257 | isStillWithParent = snapIsStillWithParent; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Decide which pick (out of the 4) to use as the basis for the placement |
| 263 | const [head, ...tail] = picks; |
| 264 | let pickLeader = head; |
| 265 | if (isStillWithParent) { |
| 266 | pickLeader = picks.find((pick) => pick.screen === parent); |
| 267 | } else |
| 268 | for (const pick of tail) { |
| 269 | if (pick.depth > pickLeader.depth) { |
| 270 | pickLeader = pick; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Place the screen |
| 275 | const mappedCorners = getMappedPositions(screen.corners, pickLeader.corners); |
| 276 | let number = address.number; |
| 277 | if (false && isStillWithParent) { |
| 278 | oldScreen.corners = mappedCorners; |
| 279 | } else { |
| 280 | const mappedScreen = makeScreen(screen.colour, mappedCorners); |
| 281 | if (address.colour === pickLeader.screen.colour) { |
| 282 | setScreenNumber(address.colour, address.number, mappedScreen); |
| 283 | number = address.number; |
no test coverage detected