Skip to content

Commit

Permalink
fix: fix Resizable's wrong position calculation (#101)
Browse files Browse the repository at this point in the history
* feat: add baseDirection props

* fix: fix Resizable's wrong position calculation
  • Loading branch information
daybrush authored Nov 22, 2019
1 parent dbe369d commit 7d8a228
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 74 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moveable",
"version": "0.10.8",
"version": "0.11.0-rc2",
"description": "Moveable is Draggable, Resizable, Scalable, Rotatable, Warpable, Pinchable, Groupable, Snappable.",
"main": "./dist/moveable.cjs.js",
"module": "./dist/moveable.esm.js",
Expand Down Expand Up @@ -57,7 +57,7 @@
"dependencies": {
"@egjs/component": "^2.1.2",
"preact": "^10.0.4",
"preact-moveable": "^0.12.14"
"preact-moveable": "^0.13.0"
},
"devDependencies": {
"@daybrush/builder": "^0.1.2",
Expand Down
8 changes: 4 additions & 4 deletions packages/preact-moveable/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/preact-moveable/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "preact-moveable",
"version": "0.12.14",
"version": "0.13.1",
"description": "A Preact Component that create Moveable, Draggable, Resizable, Scalable, Rotatable, Pinchable, Groupable, Snappable.",
"main": "./dist/moveable.cjs.js",
"module": "./dist/moveable.esm.js",
Expand Down Expand Up @@ -61,6 +61,6 @@
},
"dependencies": {
"preact-css-styler": "^1.0.0",
"react-moveable": "^0.13.12"
"react-moveable": "^0.14.2"
}
}
2 changes: 1 addition & 1 deletion packages/react-moveable/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/react-moveable/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-moveable",
"version": "0.13.12",
"version": "0.14.3",
"description": "A React Component that create Moveable, Draggable, Resizable, Scalable, Rotatable, Warpable, Pinchable, Groupable.",
"main": "./dist/moveable.cjs.js",
"module": "./dist/moveable.esm.js",
Expand Down
5 changes: 5 additions & 0 deletions packages/react-moveable/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,9 @@ img {
.box.box23 {
border-left-width: 0px;
border-right-width: 0px;
word-break: break-all;
height: auto;
}
.box.box23 span {
position: relative;
}
26 changes: 14 additions & 12 deletions packages/react-moveable/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ setAlias("matrix3d", ["transform", "matrix3d"]);

class App extends React.Component {
public moveable!: Moveable;
public state = {
target: null,
container: null,
targets: [],
isResizable: true,
emo: null,
} as {
public state: {
container: any,
target: any,
emo: any,
isShift: boolean,
targets: Array<HTMLElement | SVGElement>,
isResizable: boolean,
} = {
target: null,
container: null,
targets: [],
isResizable: true,
isShift: false,
emo: null,
};
private itemMap: Map<HTMLElement |SVGElement, Frame> = new Map();
private items: IObject<Frame> = {};
Expand All @@ -51,8 +53,8 @@ class App extends React.Component {
pinchable={true}
draggable={true}
rotatable={true}
// resizable={true}
scalable={true}
resizable={true}
// scalable={true}
ref={ref(this, "ab")}
// keepRatio={false}
target={this.state.targets}
Expand Down Expand Up @@ -154,7 +156,7 @@ class App extends React.Component {
target={selectedTarget}
container={document.querySelector<HTMLElement>("#con")}
ref={ref(this, "moveable")}
keepRatio={true}
// keepRatio={this.state.isShift}
origin={false}
// dragArea={true}
draggable={true}
Expand Down Expand Up @@ -352,9 +354,9 @@ class App extends React.Component {
const keycon = new KeyController(window);

keycon.keydown("shift", () => {
this.setState({ isResizable: false });
this.setState({ isResizable: false, isShift: true });
}).keyup("shift", () => {
this.setState({ isResizable: true });
this.setState({ isResizable: true, isShift: false });
});

const targets: any[] = [].slice.call(document.querySelectorAll(`[data-target="box"] span`));
Expand Down
33 changes: 26 additions & 7 deletions packages/react-moveable/src/react-moveable/DraggerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import MoveableManager from "./MoveableManager";
import { caculatePoses, getAbsoluteMatrix, getAbsolutePosesByState } from "./utils";
import { splitUnit } from "@daybrush/utils";
import { MoveableManagerState } from "./types";
import { MoveableManagerState, GroupableProps, ResizableProps } from "./types";

export function setDragStart(moveable: MoveableManager<any>, { datas }: any) {
const {
Expand Down Expand Up @@ -229,12 +229,14 @@ export function getScaleDist(
}

export function getResizeDist(
moveable: MoveableManager<any>,
moveable: MoveableManager<GroupableProps>,
width: number,
height: number,
// prevWidth: number,
// prevHeight: number,
direction: number[],
fixedPosition: number[],
transformOrigin: string[],
dragClient?: number[],
) {
const {
groupable,
Expand All @@ -245,7 +247,7 @@ export function getResizeDist(
offsetMatrix,
is3d,
width: prevWidth,
height: prevheight,
height: prevHeight,
left,
top,
} = moveable.state;
Expand All @@ -256,14 +258,31 @@ export function getResizeDist(
width,
height,
prevWidth,
prevheight,
prevHeight,
prevOrigin,
);
const groupLeft = groupable ? left : 0;
const groupTop = groupable ? top : 0;
const nextMatrix = getNextMatrix(offsetMatrix, targetMatrix, nextOrigin, n);
const startPos = dragClient ? dragClient : getStartPos(getAbsolutePosesByState(moveable.state), direction);
const dist = getDist(startPos, nextMatrix, width, height, n, direction);
const dist = getDist(fixedPosition, nextMatrix, width, height, n, direction);

return minus(dist, [groupLeft, groupTop]);
}
export function getStartDirection(
moveable: MoveableManager<ResizableProps>,
direction: number[],
) {
const {
baseDirection = [-1, -1],
} = moveable.props;
return [
direction[0] ? direction[0] : baseDirection[0] * -1,
direction[1] ? direction[0] : baseDirection[1] * -1,
];
}
export function getFixedPosition(
moveable: MoveableManager<ResizableProps>,
direction: number[],
) {
return getStartPos(getAbsolutePosesByState(moveable.state), direction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ export default class MoveableManager<T = {}, U = {}>
this.updateRect(type, true);
}
public getRect(): RectInfo {
const state = this.state;
const poses = getAbsolutePosesByState(this.state);
const [pos1, pos2, pos3, pos4] = poses;
const rect = getRect(poses);
const {
width: offsetWidth,
height: offsetHeight,
} = state;
const {
width,
height,
Expand All @@ -206,6 +211,8 @@ export default class MoveableManager<T = {}, U = {}>
pos2,
pos3,
pos4,
offsetWidth,
offsetHeight,
};
}
public checkUpdate() {
Expand Down
Loading

0 comments on commit 7d8a228

Please sign in to comment.