Skip to content

Commit

Permalink
feat: support scale css property #891
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Apr 13, 2023
1 parent ee8a78b commit 28c4226
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
23 changes: 22 additions & 1 deletion packages/react-moveable/src/utils/getMatrixStackInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
convertCSStoMatrix, convertDimension,
createIdentityMatrix, createOriginMatrix, createScaleMatrix,
createIdentityMatrix, createOriginMatrix, createScaleMatrix, multiply,
} from "@scena/matrix";
import { getCachedStyle } from "../store/Store";
import { IS_WEBKIT, IS_SAFARI_ABOVE15, IS_FIREFOX, IS_CHROMIUM109 } from "../consts";
Expand All @@ -11,6 +11,7 @@ import {
convert3DMatrixes, getOffsetPosInfo,
getSVGMatrix, getBodyOffset, getAbsoluteMatrix,
} from "../utils";
import { createMatrix } from "css-to-mat";


export function getShadowRoot(parentElement: HTMLElement | SVGElement) {
Expand Down Expand Up @@ -58,6 +59,7 @@ export function getMatrixStackInfo(
isEnd = requestEnd;
const getStyle = getCachedStyle(el);
const position = getStyle("position");
const scale = getStyle("scale") as string;
const transform = getElementTransform(el);
const isFixed = position === "fixed";
let matrix: number[] = convertCSStoMatrix(getTransformMatrix(transform));
Expand Down Expand Up @@ -201,6 +203,25 @@ export function getMatrixStackInfo(
matrix: getAbsoluteMatrix(matrix, n, origin),
});

if (scale && scale !== "1" && scale !== "none") {
const [
scaleX,
scaleY = scaleX,
] = scale.split(" ").map(scale => parseFloat(scale)) as number[];
const scaleMatrix = createScaleMatrix([scaleX, scaleY], n);

matrixes.push({
type: "offset",
target: el,
matrix: createIdentityMatrix(n),
});

matrixes.push({
type: "target",
target: el,
matrix: getAbsoluteMatrix(scaleMatrix, n, origin),
});
}
if (hasOffset) {
const isElementTarget = el === target;
const scrollLeft = isElementTarget ? 0 : el.scrollLeft;
Expand Down
32 changes: 31 additions & 1 deletion packages/react-moveable/stories/99-Tests/Deafult.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { add } from "../utils/story";
import { findMoveable, wait } from "../utils/testing";
import { findMoveable, pan, wait } from "../utils/testing";
import { expect } from "@storybook/jest";

export default {
Expand Down Expand Up @@ -111,6 +111,36 @@ export const TestsZoomedTarget = add("Test css zoomed target", {
app: require("./ReactZoomedTargetApp").default,
path: require.resolve("./ReactZoomedTargetApp"),
});
export const TestsScaleTarget = add("Test css scale target", {
app: require("./ReactScaleTargetApp").default,
path: require.resolve("./ReactScaleTargetApp"),
play: async ({ canvasElement }) => {
await wait();
const target = canvasElement.querySelector<HTMLElement>(".target")!;
const controlBox = canvasElement.querySelector<HTMLElement>(".moveable-control-box")!;


// x1 => x2
const line1 = controlBox.querySelector<HTMLElement>(`[data-line-key="render-line-0"]`)!;
// y1 => y2
const line2 = controlBox.querySelector<HTMLElement>(`[data-line-key="render-line-1"]`)!;

// 100x 200
expect(line1.style.width).toBe("100px");
expect(line2.style.width).toBe("200px");
expect(controlBox.style.transform).toBe("translate3d(100px, 200px, 0px)");

await pan({
target,
start: [0, 0],
end: [100, 0],
duration: 100,
interval: 10,
});
expect(target.style.transform).toBe("translate(100px, 0px)");
expect(controlBox.style.transform).toBe("translate3d(200px, 200px, 0px)");
},
});

export const TestsZoomedSnap = add("Test snap for scaled target", {
app: require("./ReactZoomedSnapApp").default,
Expand Down
32 changes: 32 additions & 0 deletions packages/react-moveable/stories/99-Tests/ReactScaleTargetApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as React from "react";
import Moveable from "@/react-moveable";

export default function App(props: Record<string, any>) {
return (
<div className="root" style={{
paddingTop: "100px",
}}>
<div>
<div style={{
position: "relative",
transform: "translate3d(0px, 0px, 100px)",
}}>
<div className="target" style={{
scale: "1 2",
transform: "translate(0px, 0px)",
}}>Target</div>
</div>
<Moveable
target={".target"}
draggable={true}
rotatable={true}
resizable={true}
rootContainer={document.body}
onRender={e => {
e.target.style.cssText += e.cssText;
}}
></Moveable>
</div>
</div>
);
}

0 comments on commit 28c4226

Please sign in to comment.