Browse Source

Update getTargetToElementFixed

pull/5649/head
Artur Arseniev 3 years ago
parent
commit
0979d2a779
  1. 57
      src/canvas/index.ts
  2. 12
      src/canvas/view/CanvasView.ts
  3. 7
      src/common/index.ts

57
src/canvas/index.ts

@ -402,10 +402,6 @@ export default class CanvasModule extends Module<CanvasConfig> {
const scroll = top ? scrollTop : scrollLeft; const scroll = top ? scrollTop : scrollLeft;
const offset = top ? offsetTop : offsetLeft; const offset = top ? offsetTop : offsetLeft;
// if (!top) {
// console.log('LEFT', { posLeft: pos[side], scroll, offset }, el);
// }
return pos[side] - (scroll - offset) * zoom; return pos[side] - (scroll - offset) * zoom;
}; };
@ -417,46 +413,51 @@ export default class CanvasModule extends Module<CanvasConfig> {
/** /**
* *
* @param {HTMLElement} el The element on which I'd attach the toolbar * @param {HTMLElement} el The component element in the canvas
* @param {HTMLElement} elToMove The target in this case could be the toolbar * @param {HTMLElement} targetEl The target element to position (eg. toolbar)
* @param {Object} opts * @param {Object} opts
* @private * @private
*/ */
getTargetToElementFixed(el: HTMLElement, elToMove: HTMLElement, opts: any = {}) { getTargetToElementFixed(el: HTMLElement, targetEl: HTMLElement, opts: any = {}) {
const pos = opts.pos || this.getElementPos(el, { noScroll: true }); const elRect = opts.pos || this.getElementPos(el, { noScroll: true });
const cvOff = opts.canvasOff || this.canvasRectOffset(el, pos); const canvasOffset = opts.canvasOff || this.canvasRectOffset(el, elRect);
const toolbarH = elToMove.offsetHeight || 0; const targetHeight = targetEl.offsetHeight || 0;
const toolbarW = elToMove.offsetWidth || 0; const targetWidth = targetEl.offsetWidth || 0;
const elRight = pos.left + pos.width; const elRight = elRect.left + elRect.width;
const cv = this.getCanvasView(); const canvasView = this.getCanvasView();
const frCvOff = cv.getPosition(); const canvasRect = canvasView.getPosition();
const frameOffset = cv.getFrameOffset(el); const frameOffset = canvasView.getFrameOffset(el);
const { event } = opts; const { event } = opts;
let top = -toolbarH; let top = -targetHeight;
let left = !isUndefined(opts.left) ? opts.left : pos.width - toolbarW; let left = !isUndefined(opts.left) ? opts.left : elRect.width - targetWidth;
left = pos.left < -left ? -pos.left : left; left = elRect.left < -left ? -elRect.left : left;
const frCvWidth = frCvOff?.width ?? 0; left = elRight > canvasRect.width ? left - (elRight - canvasRect.width) : left;
left = elRight > frCvWidth ? left - (elRight - frCvWidth) : left;
// Scroll with the window if the top edge is reached and the // Check when the target top edge reaches the top of the viewable canvas
// element is bigger than the canvas if (canvasOffset.top < targetHeight) {
const fullHeight = pos.height + toolbarH; const fullHeight = elRect.height + targetHeight;
const elIsShort = fullHeight < frameOffset.height; const elIsShort = fullHeight < frameOffset.height;
if (cvOff.top < toolbarH) { // Scroll with the window if the top edge is reached and the
// element is bigger than the canvas
if (elIsShort) { if (elIsShort) {
top = top + fullHeight; top = top + fullHeight;
} else { } else {
top = -cvOff.top < pos.height ? -cvOff.top : pos.height; top = -canvasOffset.top < elRect.height ? -canvasOffset.top : elRect.height;
} }
} }
const result = { const result = {
top, top,
left, left,
canvasOffsetTop: cvOff.top, canvasOffsetTop: canvasOffset.top,
canvasOffsetLeft: cvOff.left, canvasOffsetLeft: canvasOffset.left,
elRect,
canvasOffset,
canvasRect,
targetWidth,
targetHeight,
}; };
// In this way I can catch data and also change the position strategy // In this way I can catch data and also change the position strategy

12
src/canvas/view/CanvasView.ts

@ -7,6 +7,7 @@ import Canvas from '../model/Canvas';
import FrameView from './FrameView'; import FrameView from './FrameView';
import ComponentView from '../../dom_components/view/ComponentView'; import ComponentView from '../../dom_components/view/ComponentView';
import Component from '../../dom_components/model/Component'; import Component from '../../dom_components/model/Component';
import { ElementRect } from '../../common';
export interface MarginPaddingOffsets { export interface MarginPaddingOffsets {
marginTop?: number; marginTop?: number;
@ -304,9 +305,16 @@ export default class CanvasView extends ModuleView<Canvas> {
* @return { {top: number, left: number, width: number, height: number} } obj Position object * @return { {top: number, left: number, width: number, height: number} } obj Position object
* @public * @public
*/ */
getPosition(opts: any = {}) { getPosition(opts: any = {}): ElementRect {
const doc = this.frame?.el.contentDocument; const doc = this.frame?.el.contentDocument;
if (!doc) return; if (!doc) {
return {
top: 0,
left: 0,
width: 0,
height: 0,
};
}
const bEl = doc.body; const bEl = doc.body;
const zoom = this.getZoom(); const zoom = this.getZoom();
const fo = this.getFrameOffset(); const fo = this.getFrameOffset();

7
src/common/index.ts

@ -22,6 +22,13 @@ export type Position = {
y: number; y: number;
}; };
export type ElementRect = {
top: number;
left: number;
width: number;
height: number;
};
export type CombinedModelConstructorOptions< export type CombinedModelConstructorOptions<
E, E,
M extends Model<any, any, E> = Model M extends Model<any, any, E> = Model

Loading…
Cancel
Save