Skip to content

Commit

Permalink
Auto-positioning: added another test for horizontal positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimon committed Apr 19, 2021
1 parent 34c8daa commit c778a8c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/editor/setPosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand All @@ -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;

0 comments on commit c778a8c

Please sign in to comment.