Skip to content

Commit

Permalink
feat(getposition): support desired-place-list (by priority)
Browse files Browse the repository at this point in the history
  • Loading branch information
LennartSpitzner committed Feb 9, 2022
1 parent 8d260dc commit 982d89d
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/utils/getPosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,30 @@ export default function(e, target, node, place, desiredPlace, effect, offset) {
outsideLeft(p) || outsideRight(p) || outsideTop(p) || outsideBottom(p);
const inside = p => !outside(p);

const placesList = ['top', 'bottom', 'left', 'right'];
const insideList = [];
for (let i = 0; i < 4; i++) {
const p = placesList[i];
if (inside(p)) {
insideList.push(p);
const placeIsInside = {
top: inside('top'),
bottom: inside('bottom'),
left: inside('left'),
right: inside('right')
};

function firstInside() {
const allPlaces = desiredPlace
.split(',')
.concat(place, ['top', 'bottom', 'left', 'right']);
for (const d of allPlaces) {
if (placeIsInside[d]) return d;
}
return null;
}

const chosen = firstInside();

let isNewState = false;
let newPlace;
const shouldUpdatePlace = desiredPlace !== place;
if (inside(desiredPlace) && shouldUpdatePlace) {
isNewState = true;
newPlace = desiredPlace;
} else if (insideList.length > 0 && outside(desiredPlace) && outside(place)) {
if (chosen && chosen !== place) {
isNewState = true;
newPlace = insideList[0];
newPlace = chosen;
}

if (isNewState) {
Expand Down

0 comments on commit 982d89d

Please sign in to comment.