Skip to content

Commit 982d89d

Browse files
feat(getposition): support desired-place-list (by priority)
1 parent 8d260dc commit 982d89d

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/utils/getPosition.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,30 @@ export default function(e, target, node, place, desiredPlace, effect, offset) {
7676
outsideLeft(p) || outsideRight(p) || outsideTop(p) || outsideBottom(p);
7777
const inside = p => !outside(p);
7878

79-
const placesList = ['top', 'bottom', 'left', 'right'];
80-
const insideList = [];
81-
for (let i = 0; i < 4; i++) {
82-
const p = placesList[i];
83-
if (inside(p)) {
84-
insideList.push(p);
79+
const placeIsInside = {
80+
top: inside('top'),
81+
bottom: inside('bottom'),
82+
left: inside('left'),
83+
right: inside('right')
84+
};
85+
86+
function firstInside() {
87+
const allPlaces = desiredPlace
88+
.split(',')
89+
.concat(place, ['top', 'bottom', 'left', 'right']);
90+
for (const d of allPlaces) {
91+
if (placeIsInside[d]) return d;
8592
}
93+
return null;
8694
}
8795

96+
const chosen = firstInside();
97+
8898
let isNewState = false;
8999
let newPlace;
90-
const shouldUpdatePlace = desiredPlace !== place;
91-
if (inside(desiredPlace) && shouldUpdatePlace) {
92-
isNewState = true;
93-
newPlace = desiredPlace;
94-
} else if (insideList.length > 0 && outside(desiredPlace) && outside(place)) {
100+
if (chosen && chosen !== place) {
95101
isNewState = true;
96-
newPlace = insideList[0];
102+
newPlace = chosen;
97103
}
98104

99105
if (isNewState) {

0 commit comments

Comments
 (0)