Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add overflow.lockedPoints #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="__react-content" />

<script src="./simple.js" module></script>
</body>
</html>
8 changes: 7 additions & 1 deletion examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function align() {
overflow: {
adjustX: $id('adjustX').checked,
adjustY: $id('adjustY').checked,
lockedPoints: $id('lockedPoints').checked,
},
useCssRight: $id('useCssRight').checked,
useCssBottom: $id('useCssBottom').checked,
Expand Down Expand Up @@ -78,6 +79,11 @@ const div = (
<input type="checkbox" id="adjustY" />
</label>
&nbsp;
<label>
lockedPoints:
<input type="checkbox" id="lockedPoints" />
</label>
&nbsp;
<label>
useCssBottom:
<input type="checkbox" id="useCssBottom" />
Expand Down Expand Up @@ -131,7 +137,7 @@ const div = (
style={{
background: 'red',
width: 80,
height: 80,
height: 360,
left: 0,
top: 0,
position: 'absolute',
Expand Down
88 changes: 49 additions & 39 deletions src/align/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,55 +115,65 @@ function doAlign(el, tgtRegion, align, isTgtRegionVisible) {
if (overflow.adjustX) {
// 如果横向不能放下
if (isFailX(elFuturePos, elRegion, visibleRect)) {
// 对齐位置反下
const newPoints = flip(points, /[lr]/gi, {
l: 'r',
r: 'l',
});
// 偏移量也反下
const newOffset = flipOffset(offset, 0);
const newTargetOffset = flipOffset(targetOffset, 0);
const newElFuturePos = getElFuturePos(
elRegion,
tgtRegion,
newPoints,
newOffset,
newTargetOffset,
);
// 不锁定对齐点的话将会做对齐镜像操作
if (!overflow.lockedPoints) {
// 对齐位置反下
const newPoints = flip(points, /[lr]/gi, {
l: 'r',
r: 'l',
});
// 偏移量也反下
const newOffset = flipOffset(offset, 0);
const newTargetOffset = flipOffset(targetOffset, 0);
const newElFuturePos = getElFuturePos(
elRegion,
tgtRegion,
newPoints,
newOffset,
newTargetOffset,
);

if (!isCompleteFailX(newElFuturePos, elRegion, visibleRect)) {
if (!isCompleteFailX(newElFuturePos, elRegion, visibleRect)) {
fail = 1;
points = newPoints;
offset = newOffset;
targetOffset = newTargetOffset;
}
} else {
fail = 1;
points = newPoints;
offset = newOffset;
targetOffset = newTargetOffset;
}
}
}

if (overflow.adjustY) {
// 如果纵向不能放下
if (isFailY(elFuturePos, elRegion, visibleRect)) {
// 对齐位置反下
const newPoints = flip(points, /[tb]/gi, {
t: 'b',
b: 't',
});
// 偏移量也反下
const newOffset = flipOffset(offset, 1);
const newTargetOffset = flipOffset(targetOffset, 1);
const newElFuturePos = getElFuturePos(
elRegion,
tgtRegion,
newPoints,
newOffset,
newTargetOffset,
);
// 不锁定对齐点的话将会做对齐镜像操作
if (!overflow.lockedPoints) {
// 对齐位置反下
const newPoints = flip(points, /[tb]/gi, {
t: 'b',
b: 't',
});
// 偏移量也反下
const newOffset = flipOffset(offset, 1);
const newTargetOffset = flipOffset(targetOffset, 1);
const newElFuturePos = getElFuturePos(
elRegion,
tgtRegion,
newPoints,
newOffset,
newTargetOffset,
);

if (!isCompleteFailY(newElFuturePos, elRegion, visibleRect)) {
if (!isCompleteFailY(newElFuturePos, elRegion, visibleRect)) {
fail = 1;
points = newPoints;
offset = newOffset;
targetOffset = newTargetOffset;
}
} else {
fail = 1;
points = newPoints;
offset = newOffset;
targetOffset = newTargetOffset;
}
}
}
Expand All @@ -183,7 +193,7 @@ function doAlign(el, tgtRegion, align, isTgtRegionVisible) {
const isStillFailY = isFailY(elFuturePos, elRegion, visibleRect);
// 检查反下后的位置是否可以放下了,如果仍然放不下:
// 1. 复原修改过的定位参数
if (isStillFailX || isStillFailY) {
if ((isStillFailX || isStillFailY) && !overflow.lockedPoints) {
let newPoints = points;

// 重置对应部分的翻转逻辑
Expand Down