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: max rows #145

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
30 changes: 28 additions & 2 deletions src/MoveResize/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
on:pointerdown={item && item.customDragger ? null : draggable && pointerdown}
class="svlt-grid-item"
class:svlt-grid-active={active || (trans && rect)}
style="width: {active ? newSize.width : width}px; height:{active ? newSize.height : height}px;
style="width: {active ? newSize.width : width}px; height:{active ? newSize.height : height}px;
{active ? `transform: translate(${cordDiff.x}px, ${cordDiff.y}px);top:${rect.top}px;left:${rect.left}px;` : trans ? `transform: translate(${cordDiff.x}px, ${cordDiff.y}px); position:absolute; transition: width 0.2s, height 0.2s;` : `transition: transform 0.2s, opacity 0.2s; transform: translate(${left}px, ${top}px); `} ">
<slot movePointerDown={pointerdown} {resizePointerDown} />
{#if resizable && !item.customResizer}
Expand Down Expand Up @@ -100,6 +100,8 @@

export let max;
export let min;
export let maxRows;
export let rowHeight;

export let cols;

Expand All @@ -123,6 +125,7 @@
let trans = false;

let anima;
let maxY;

const inActivate = () => {
const shadowBound = shadowElement.getBoundingClientRect();
Expand Down Expand Up @@ -176,10 +179,22 @@

const getScroller = (element) => (!element ? document.documentElement : element);

function getHeightDifference() {
const {y: itemY, h: itemHeight } = item;
const itemBottomIndex = itemY + itemHeight;

return maxRows - itemBottomIndex;
}

const pointerdown = ({ clientX, clientY, target }) => {
initX = clientX;
initY = clientY;

if (maxRows) {
const diff = getHeightDifference();
maxY = initY + rowHeight * diff;
}

capturePos = { x: left, y: top };
shadow = { x: item.x, y: item.y, w: item.w, h: item.h };
newSize = { width, height };
Expand Down Expand Up @@ -232,8 +247,11 @@
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
let { clientX, clientY } = event;

const { clientX, clientY } = event;
if (maxRows && clientY >= maxY) {
clientY = maxY;
}
cordDiff = { x: clientX - initX, y: clientY - initY };

const Y_SENSOR = sensor;
Expand Down Expand Up @@ -303,6 +321,9 @@
newSize.width = initSize.width + pageX - resizeInitPos.x;
newSize.height = initSize.height + pageY - resizeInitPos.y;

const diff = getHeightDifference();
const maxHeight = diff * rowHeight + item.h * rowHeight;

// Get max col number
let maxWidth = cols - shadow.x;
maxWidth = Math.min(max.w, maxWidth) || maxWidth;
Expand All @@ -315,6 +336,11 @@
if (max.h) {
newSize.height = Math.min(newSize.height, max.h * yPerPx - gapY * 2);
}

if (newSize.height > maxHeight) {
newSize.height = maxHeight;
}

// Limit col & row
shadow.w = Math.round((newSize.width + gapX * 2) / xPerPx);
shadow.h = Math.round((newSize.height + gapY * 2) / yPerPx);
Expand Down
3 changes: 3 additions & 0 deletions src/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
{gapX}
{gapY}
{sensor}
{maxRows}
{rowHeight}
container={scroller}
nativeContainer={container}
let:resizePointerDown
Expand Down Expand Up @@ -57,6 +59,7 @@
export let fastStart = false;
export let throttleUpdate = 100;
export let throttleResize = 100;
export let maxRows;

export let scroller = undefined;
export let sensor = 20;
Expand Down