Skip to content

Commit

Permalink
fix: fix line break #1001
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Oct 4, 2023
1 parent 33366b5 commit 5f68143
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 62 deletions.
70 changes: 56 additions & 14 deletions packages/react-moveable/src/MoveableManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ export default class MoveableManager<T = {}>
public state: MoveableManagerState = {
container: null,
gestos: {},
renderLines: [
[[0, 0], [0, 0]],
[[0, 0], [0, 0]],
[[0, 0], [0, 0]],
[[0, 0], [0, 0]],
],
renderPoses: [[0, 0], [0, 0], [0, 0], [0, 0]],
disableNativeEvent: false,
posDelta: [0, 0],
Expand Down Expand Up @@ -760,22 +766,29 @@ export default class MoveableManager<T = {}>
top: stateTop,
isPersisted,
} = state;
const zoom = props.zoom || 1;

if (!padding) {
if (!padding && zoom <= 1) {
state.renderPoses = [
pos1,
pos2,
pos3,
pos4,
];
state.renderLines = [
[pos1, pos2],
[pos2, pos4],
[pos4, pos3],
[pos3, pos1],
];
return;
}
const {
left,
top,
bottom,
right,
} = getPaddingBox(padding);
} = getPaddingBox(padding || {});
const n = is3d ? 4 : 3;

// const clipPathInfo = getClipPath(
Expand Down Expand Up @@ -808,12 +821,46 @@ export default class MoveableManager<T = {}>
createOriginMatrix(transformOrigin, n),
);

const renderPos1 = calculatePadding(nextMatrix, pos1, [-left, -top], n);
const renderPos2 = calculatePadding(nextMatrix, pos2, [right, -top], n);
const renderPos3 = calculatePadding(nextMatrix, pos3, [-left, bottom], n);
const renderPos4 = calculatePadding(nextMatrix, pos4, [right, bottom], n);

state.renderPoses = [
calculatePadding(nextMatrix, pos1, [-left, -top], n),
calculatePadding(nextMatrix, pos2, [right, -top], n),
calculatePadding(nextMatrix, pos3, [-left, bottom], n),
calculatePadding(nextMatrix, pos4, [right, bottom], n),
renderPos1,
renderPos2,
renderPos3,
renderPos4,
];
state.renderLines = [
[renderPos1, renderPos2],
[renderPos2, renderPos4],
[renderPos4, renderPos3],
[renderPos3, renderPos1],
];

if (zoom) {
const zoomOffset = zoom / 2;

state.renderLines = [
[
calculatePadding(nextMatrix, pos1, [-left - zoomOffset, -top], n),
calculatePadding(nextMatrix, pos2, [right + zoomOffset, -top], n),
],
[
calculatePadding(nextMatrix, pos2, [right, -top - zoomOffset], n),
calculatePadding(nextMatrix, pos4, [right, bottom + zoomOffset], n),
],
[
calculatePadding(nextMatrix, pos4, [right + zoomOffset, bottom], n),
calculatePadding(nextMatrix, pos3, [-left - zoomOffset, bottom], n),
],
[
calculatePadding(nextMatrix, pos3, [-left, bottom + zoomOffset], n),
calculatePadding(nextMatrix, pos1, [-left, -top - zoomOffset], n),
],
];
}
}
public checkUpdate() {
this._isPropTargetChanged = false;
Expand Down Expand Up @@ -1043,18 +1090,13 @@ export default class MoveableManager<T = {}>
if (hideDefaultLines || (parentMoveable && hideChildMoveableDefaultLines)) {
return [];
}
const renderPoses = this.getState().renderPoses;
const state = this.getState();
const Renderer = {
createElement,
};

return [
[0, 1],
[1, 3],
[3, 2],
[2, 0],
].map(([from, to], i) => {
return renderLine(Renderer, "", renderPoses[from], renderPoses[to], zoom!, `render-line-${i}`);
return state.renderLines.map((line, i) => {
return renderLine(Renderer, "", line[0], line[1], zoom!, `render-line-${i}`);
});
}
private _onPreventClick = (e: any) => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-moveable/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export type MoveableManagerState<T = {}> = {
container: SVGElement | HTMLElement | null | undefined;
disableNativeEvent: boolean;
gestos: Record<string, Gesto | CustomGesto | null>;
renderLines: number[][][];
renderPoses: number[][];
posDelta: number[];
style: Partial<Writable<CSSStyleDeclaration>>;
Expand Down
4 changes: 2 additions & 2 deletions packages/react-moveable/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1215,8 +1215,8 @@ export function calculatePadding(
return pos;
}

const xAdded = calculatePosition(matrix, [normalized(added[0]), 0], n);
const yAdded = calculatePosition(matrix, [0, normalized(added[1])], n);
const xAdded = calculatePosition(matrix, [normalized(added[0] || 1), 0], n);
const yAdded = calculatePosition(matrix, [0, normalized(added[1] || 1)], n);
const nextAdded = calculatePosition(matrix, [
added[0] / getDistSize(xAdded),
added[1] / getDistSize(yAdded),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,16 @@ export const OptionsRootContainer = add("rootContainer (css transformed containe
});

export const OptionsRootContainerZoom = add("rootContainer (css zoomed container)", {
app: require("./ReactRootContainerApp").default,
path: require.resolve("./ReactRootContainerApp"),
});

export const OptionsZoom = add("zoom (line, control)", {
app: require("./ReactZoomApp").default,
path: require.resolve("./ReactZoomApp"),
});


export const OptionsAccuratePosition = add("useAccuratePosition (Render in a more accurate position)", {
app: require("./ReactUseAccuratePositionApp").default,
path: require.resolve("./ReactUseAccuratePositionApp"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as React from "react";
import Moveable from "@/react-moveable";

export default function App(props: Record<string, any>) {
return (
<div className="margin no-relative">
<div style={{
position: "absolute",
left: "84px",
top: "159px",
zIndex: 1,
}}>92 x 167 (34, 84)</div>
<div className="root" style={{
// position: "relative",
padding: "10px",
}}>
<div style={{
zoom: 0.5,
position: "relative",
}}>
<div style={{
position: "relative",
marginTop: "100px",
width: "500px",
height: "500px",
}}>
<div className="target" style={{
transform: "translate(0px, 0px)",
}}>Target</div>
<Moveable
target={".target"}
draggable={true}
rotatable={true}
resizable={true}
rootContainer={document.body}
onRender={e => {
e.target.style.cssText += e.cssText;
}}
onRenderEnd={e => {
console.log("RCR", e.moveable.state.rootContainerClientRect);
console.log("ORM", e.moveable.state.originalRootMatrix);
console.log("MCR", e.moveable.state.moveableClientRect);
}}
></Moveable>
</div>
</div>
</div>
</div>
);
}
62 changes: 16 additions & 46 deletions packages/react-moveable/stories/4-Options/ReactZoomApp.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
import * as React from "react";
import Moveable from "@/react-moveable";

export default function App(props: Record<string, any>) {
return (
<div className="margin no-relative">
<div style={{
position: "absolute",
left: "84px",
top: "159px",
zIndex: 1,
}}>92 x 167 (34, 84)</div>
<div className="root" style={{
// position: "relative",
padding: "10px",
}}>
<div style={{
zoom: 0.5,
position: "relative",
}}>
<div style={{
position: "relative",
marginTop: "100px",
width: "500px",
height: "500px",
}}>
<div className="target" style={{
transform: "translate(0px, 0px)",
}}>Target</div>
<Moveable
target={".target"}
draggable={true}
rotatable={true}
resizable={true}
rootContainer={document.body}
onRender={e => {
e.target.style.cssText += e.cssText;
}}
onRenderEnd={e => {
console.log("RCR", e.moveable.state.rootContainerClientRect);
console.log("ORM", e.moveable.state.originalRootMatrix);
console.log("MCR", e.moveable.state.moveableClientRect);
}}
></Moveable>
</div>
</div>
</div>
</div>
);
export default function App() {
return <div className="container">
<div className="target" style={{
width: "300px",
height: "300px",
}}></div>
<Moveable
target={".target"}
draggable={true}
rotatable={true}
zoom={5}
onRender={e => {
e.target.style.cssText += e.cssText;
}}
/>
</div>;
}

0 comments on commit 5f68143

Please sign in to comment.