Skip to content

Commit

Permalink
fix: fix multiple snaps for slight difference #121
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Dec 23, 2019
1 parent 2ba7276 commit ef58132
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getRect,
filterAbles,
equals,
resetClientRect,
} from "./utils";
import styler from "react-css-styler";
import Dragger from "@daybrush/drag";
Expand Down Expand Up @@ -60,8 +61,8 @@ export default class MoveableManager<T = {}, U = {}>
pos2: [0, 0],
pos3: [0, 0],
pos4: [0, 0],
clientRect: { left: 0, top: 0, bottom: 0, right: 0, width: 0, height: 0 },
containerRect: { left: 0, top: 0, bottom: 0, right: 0, width: 0, height: 0 },
clientRect: resetClientRect(),
containerRect: resetClientRect(),
rotation: 0,
} as any;
public targetAbles: Array<Able<T>> = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../types";
import {
prefix, caculatePoses, getRect,
getAbsolutePosesByState, getAbsolutePoses, selectValue, throttle
getAbsolutePosesByState, getAbsolutePoses, selectValue, throttle, roundSign
} from "../utils";
import { directionCondition } from "../groupUtils";
import { isUndefined, IObject } from "@daybrush/utils";
Expand Down Expand Up @@ -50,8 +50,8 @@ function snapStart(moveable: MoveableManager<SnappableProps, SnappableState>) {
const poses = getAbsolutePosesByState(state);
const targetLeft = Math.min(...poses.map(pos => pos[0]));
const targetTop = Math.min(...poses.map(pos => pos[1]));
const distLeft = targetLeft - (clientLeft - containerLeft);
const distTop = targetTop - (clientTop - containerTop);
const distLeft = roundSign(targetLeft - (clientLeft - containerLeft));
const distTop = roundSign(targetTop - (clientTop - containerTop));
const guidelines: Guideline[] = [];

elementGuidelines!.forEach(el => {
Expand All @@ -61,7 +61,6 @@ function snapStart(moveable: MoveableManager<SnappableProps, SnappableState>) {
const elementBottom = elementTop + height;
const elementLeft = left - containerLeft;
const elementRight = elementLeft + width;

guidelines.push({ type: "vertical", element: el, pos: [
throttle(elementLeft + distLeft, 0.1),
elementTop,
Expand Down
12 changes: 9 additions & 3 deletions packages/react-moveable/src/react-moveable/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@moveable/matrix";

import MoveableManager from "./MoveableManager";
import { MoveableManagerState, Able } from "./types";
import { MoveableManagerState, Able, MoveableClientRect } from "./types";

export function round(num: number) {
return Math.round(num);
Expand Down Expand Up @@ -603,8 +603,8 @@ export function getTargetInfo(
let is3d = false;
let targetTransform = "";
let beforeOrigin = [0, 0];
let clientRect = { left: 0, right: 0, top: 0, bottom: 0, width: 0, height: 0 };
let containerRect = { left: 0, right: 0, top: 0, bottom: 0, width: 0, height: 0 };
let clientRect = resetClientRect();
let containerRect = resetClientRect();
let rotation = 0;

const prevMatrix = state ? state.beforeMatrix : undefined;
Expand Down Expand Up @@ -687,6 +687,9 @@ export function getTargetInfo(
clientRect,
};
}
export function resetClientRect(): MoveableClientRect {
return { left: 0, right: 0, top: 0, width: 0, height: 0, bottom: 0 };
}
export function getClientRect(el: HTMLElement | SVGElement) {
const { left, width, top, bottom, right, height } = el.getBoundingClientRect();

Expand Down Expand Up @@ -742,6 +745,9 @@ export function getAbsolutePosesByState({
}) {
return getAbsolutePoses([pos1, pos2, pos3, pos4], [left, top]);
}
export function roundSign(num: number) {
return Math.round(num % 1 === -0.5 ? num - 1 : num);
}
export function throttle(num: number, unit: number) {
if (!unit) {
return num;
Expand Down

0 comments on commit ef58132

Please sign in to comment.