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

Fix zoomed out mode drag chip position when dragging over the scaled iframe #56889

Merged
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
65 changes: 61 additions & 4 deletions packages/components/src/draggable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@ const cloneWrapperClass = 'components-draggable__clone';
const clonePadding = 0;
const bodyClass = 'is-dragging-components-draggable';

export const getFrameScale = (
frameElement: HTMLElement
): {
x: number;
y: number;
} => {
if ( ! frameElement ) {
return { x: 1, y: 1 };
}
const rect = frameElement.getBoundingClientRect();
return {
x: rect.width / frameElement.offsetWidth,
y: rect.height / frameElement.offsetHeight,
};
};

const getScaledMousePosition = (
mousePosition: { x: number; y: number },
scale: { x: number; y: number },
frameElement: HTMLElement
): { x: number; y: number } => {
const rect = frameElement?.getBoundingClientRect();

// Only the distance traveled over the iframe is affected by scaling.
// Subtract off the position of the iframe to get that distance,
// then multiply by the scale factor to normalize the scaling.
const normalizedScaledPortion = {
x: ( mousePosition.x - rect.x ) * scale.x,
y: ( mousePosition.y - rect.y ) * scale.y,
};

// Add back on the position of the iframe to get the fully adjusted mouse
// position.
return {
x: normalizedScaledPortion.x + rect.x,
y: normalizedScaledPortion.y + rect.y,
};
};

/**
* `Draggable` is a Component that provides a way to set up a cross-browser
* (including IE) customizable drag image and the transfer data for the drag
Expand Down Expand Up @@ -190,11 +229,29 @@ export function Draggable( {
if ( cursorLeft === e.clientX && cursorTop === e.clientY ) {
return;
}
const nextX = x + e.clientX - cursorLeft;
const nextY = y + e.clientY - cursorTop;

const targetElement = e.target as HTMLElement;
let newX = e.clientX;
let newY = e.clientY;

// When hovering an iframe, take any scaling of the iframe into account.
// i.e. in Zoomed out mode.
if ( targetElement.nodeName === 'IFRAME' ) {
const scale = getFrameScale( targetElement );
const scaledMousePosition = getScaledMousePosition(
{ x: e.clientX, y: e.clientY },
scale,
targetElement
);
newX = scaledMousePosition.x;
newY = scaledMousePosition.y;
}

const nextX = x + newX - cursorLeft;
const nextY = y + newY - cursorTop;
cloneWrapper.style.transform = `translate( ${ nextX }px, ${ nextY }px )`;
cursorLeft = e.clientX;
cursorTop = e.clientY;
cursorLeft = newX;
cursorTop = newY;
x = nextX;
y = nextY;
if ( onDragOver ) {
Expand Down
Loading