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: element jumping when resizing on zoomed frame #5103

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/canvas/view/CanvasView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface MarginPaddingOffsets {
paddingLeft?: number;
}

export type ElementPosOpts = {
avoidFrameOffset?: boolean;
avoidFrameZoom?: boolean;
noScroll?: boolean;
};
export default class CanvasView extends ModuleView<Canvas> {
events() {
return {
Expand Down Expand Up @@ -193,7 +198,7 @@ export default class CanvasView extends ModuleView<Canvas> {
* @param {HTMLElement} el
* @return { {top: number, left: number, width: number, height: number} }
*/
offset(el?: HTMLElement, opts: any = {}) {
offset(el?: HTMLElement, opts: ElementPosOpts = {}) {
const rect = getElRect(el);
const docBody = el?.ownerDocument.body;
const { noScroll } = opts;
Expand Down Expand Up @@ -243,23 +248,26 @@ export default class CanvasView extends ModuleView<Canvas> {
/**
* Returns element's rect info
* @param {HTMLElement} el
* @param {object} opts
* @return { {top: number, left: number, width: number, height: number, zoom: number, rect: any} }
* @public
*/
getElementPos(el: HTMLElement, opts: any = {}) {
getElementPos(el: HTMLElement, opts: ElementPosOpts = {}) {
const zoom = this.getZoom();
const opt = opts || {};
const frameOffset = this.getFrameOffset(el);
const canvasEl = this.el;
const canvasOffset = this.getCanvasOffset();
const elRect = this.offset(el, opts);
const frameTop = opt.avoidFrameOffset ? 0 : frameOffset.top;
const frameLeft = opt.avoidFrameOffset ? 0 : frameOffset.left;
const frameTop = opts.avoidFrameOffset ? 0 : frameOffset.top;
const frameLeft = opts.avoidFrameOffset ? 0 : frameOffset.left;

const top = elRect.top * zoom + frameTop - canvasOffset.top + canvasEl.scrollTop;
const left = elRect.left * zoom + frameLeft - canvasOffset.left + canvasEl.scrollLeft;
const height = elRect.height * zoom;
const width = elRect.width * zoom;
const elTop = opts.avoidFrameZoom ? elRect.top : elRect.top * zoom;
const elLeft = opts.avoidFrameZoom ? elRect.left : elRect.left * zoom;

const top = opts.avoidFrameOffset ? elTop : (elTop + frameTop - canvasOffset.top + canvasEl.scrollTop);
const left = opts.avoidFrameOffset ? elLeft : (elLeft + frameLeft - canvasOffset.left + canvasEl.scrollLeft);
const height = opts.avoidFrameZoom ? elRect.height : elRect.height * zoom;
const width = opts.avoidFrameZoom ? elRect.width : elRect.width * zoom;

return { top, left, height, width, zoom, rect: elRect };
}
Expand Down
7 changes: 4 additions & 3 deletions src/utils/Resizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { bindAll, isFunction, each } from 'underscore';
import { Position } from '../common';
import { on, off, normalizeFloat } from './mixins';
import { ElementPosOpts } from '../canvas/view/CanvasView';

type RectDim = {
t: number;
Expand Down Expand Up @@ -409,7 +410,7 @@ export default class Resizer {
* @param {Object} opts Custom options
* @return {Object}
*/
getElementPos(el: HTMLElement, opts = {}) {
getElementPos(el: HTMLElement, opts: ElementPosOpts = {}) {
const { posFetcher } = this;
return posFetcher ? posFetcher(el, opts) : getBoundingRect(el);
}
Expand Down Expand Up @@ -456,8 +457,8 @@ export default class Resizer {
const resizer = this;
const config = this.opts || {};
const mouseFetch = this.mousePosFetcher;
const attrName = 'data-' + config.prefix + 'handler';
const rect = this.getElementPos(el!, { target: 'el' });
const attrName = 'data-' + config.prefix + 'handler';
const rect = this.getElementPos(el!, { avoidFrameZoom: true, avoidFrameOffset: true });
const parentRect = this.getElementPos(parentEl!);
const target = e.target as HTMLElement;
this.handlerAttr = target.getAttribute(attrName)!;
Expand Down