From c778a8c282ca3b8425acc31e553f87596e4b36e1 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Mon, 19 Apr 2021 19:41:38 +0200 Subject: [PATCH] Auto-positioning: added another test for horizontal positioning --- src/editor/setPosition.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/editor/setPosition.js b/src/editor/setPosition.js index 4a29cf1..44ed127 100644 --- a/src/editor/setPosition.js +++ b/src/editor/setPosition.js @@ -10,19 +10,22 @@ const setPosition = (wrapperEl, editorEl, selectedEl) => { // Set visible editorEl.style.opacity = 1; - // Default orientation + // Default orientation (upwards arrow, at bottom-left of shape) const { left, top, right, height, bottom } = selectedEl.getBoundingClientRect(); editorEl.style.top = `${top + height - containerBounds.top}px`; editorEl.style.left = `${left - containerBounds.left}px`; const defaultOrientation = editorEl.children[1].getBoundingClientRect(); + // Test 1: does right edge extend beyond the width of the page? + // If so, flip horizontally if (defaultOrientation.right > window.innerWidth) { - // Default bounds clipped - flip horizontally editorEl.classList.add('align-right'); editorEl.style.left = `${right - defaultOrientation.width - containerBounds.left}px`; } + // Test 2: does the bottom edge extend beyond the height of the page? + // If so, flip vertically if (defaultOrientation.bottom > window.innerHeight) { // Flip vertically const annotationTop = top + pageYOffset; // Annotation bottom relative to parents @@ -33,8 +36,11 @@ const setPosition = (wrapperEl, editorEl, selectedEl) => { editorEl.style.bottom = `${containerHeight - annotationTop}px`; } - // Check if vertical flip helped, push down if not + // Get bounding box in current orientation const currentOrientation = editorEl.children[1].getBoundingClientRect(); + + // Test 3: does the top (still?) extend beyond top of the page? + // If so, push down if (currentOrientation.top < 0) { editorEl.style.top = `${-containerBounds.top}px`; editorEl.style.bottom = 'auto'; @@ -46,6 +52,12 @@ const setPosition = (wrapperEl, editorEl, selectedEl) => { editorEl.classList.remove('align-bottom'); } + // Test 4: does the left edge extend beyond the start of the page? + // If so, push inward + if (currentOrientation.left < 0) { + editorEl.style.left = `${-containerBounds.left}px`; + } + } export default setPosition;