Browse Source

Add convertPxToUnit to options

enhance-resize
Artur Arseniev 7 months ago
parent
commit
febd1d2070
  1. 80
      packages/core/src/commands/view/Resize.ts

80
packages/core/src/commands/view/Resize.ts

@ -59,12 +59,17 @@ export interface ComponentResizeEventUpdateProps extends ComponentResizeEventPro
pointer: Position; pointer: Position;
style: StyleProps; style: StyleProps;
updateStyle: (styles?: StyleProps) => void; updateStyle: (styles?: StyleProps) => void;
convertPxToUnit: (props: ConvertPxToUnitProps) => string;
} }
export interface ConvertPxToUnitProps { export interface ConvertPxToUnitProps {
el: HTMLElement; el: HTMLElement;
valuePx: number; valuePx: number;
unit: string; unit?: string;
/**
* @default 3
*/
roundDecimals?: number;
/** /**
* DPI (Dots Per Inch) value to use for conversion. * DPI (Dots Per Inch) value to use for conversion.
* @default 96 * @default 96
@ -195,8 +200,8 @@ export default {
options.afterEnd?.(); options.afterEnd?.();
}, },
updateTarget(el, rect, options) { updateTarget: (_el, rect, options) => {
updateTarget(el, rect, options); updateTarget(_el, rect, options);
if (!modelToStyle) { if (!modelToStyle) {
return; return;
} }
@ -211,11 +216,23 @@ export default {
if (!onlyHeight) { if (!onlyHeight) {
const bodyw = Canvas.getBody()?.offsetWidth || 0; const bodyw = Canvas.getBody()?.offsetWidth || 0;
const width = rect.w < bodyw ? rect.w : bodyw; const width = rect.w < bodyw ? rect.w : bodyw;
style[keyWidth!] = autoWidth ? 'auto' : `${width}${unitWidth}`; style[keyWidth!] = autoWidth
? 'auto'
: this.convertPxToUnit({
el,
valuePx: width,
unit: unitWidth,
});
} }
if (!onlyWidth) { if (!onlyWidth) {
style[keyHeight!] = autoHeight ? 'auto' : `${rect.h}${unitHeight}`; style[keyHeight!] = autoHeight
? 'auto'
: this.convertPxToUnit({
el,
valuePx: rect.h,
unit: unitHeight,
});
} }
if (!skipPositionUpdate && em.getDragMode(component)) { if (!skipPositionUpdate && em.getDragMode(component)) {
@ -239,6 +256,7 @@ export default {
event, event,
style, style,
updateStyle, updateStyle,
convertPxToUnit: (props: Omit<ConvertPxToUnitProps, 'el'>) => this.convertPxToUnit({ el, ...props }),
delta: resizer.delta!, delta: resizer.delta!,
pointer: resizer.currentPos!, pointer: resizer.currentPos!,
}; };
@ -269,48 +287,70 @@ export default {
}, },
convertPxToUnit(props: ConvertPxToUnitProps): string { convertPxToUnit(props: ConvertPxToUnitProps): string {
const { el, valuePx, unit, dpi = 96 } = props; const { el, valuePx, unit, dpi = 96, roundDecimals = 3 } = props;
const win = el.ownerDocument.defaultView; const win = el.ownerDocument.defaultView;
const winWidth = win?.innerWidth || 1; const winWidth = win?.innerWidth || 1;
const winHeight = window.innerHeight || 1; const winHeight = window.innerHeight || 1;
let valueResult = valuePx;
let untiResult = unit;
switch (unit) { switch (unit) {
case 'pt': case 'pt':
return `${valuePx * (72 / dpi)}${unit}`; valueResult = valuePx * (72 / dpi);
break;
case 'pc': case 'pc':
return `${valuePx * (6 / dpi)}${unit}`; valueResult = valuePx * (6 / dpi);
break;
case 'in': case 'in':
return `${valuePx / dpi}${unit}`; valueResult = valuePx / dpi;
break;
case 'cm': case 'cm':
return `${valuePx / (dpi / 2.54)}${unit}`; valueResult = valuePx / (dpi / 2.54);
break;
case 'mm': case 'mm':
return `${valuePx / (dpi / 25.4)}${unit}`; valueResult = valuePx / (dpi / 25.4);
break;
case 'vw': case 'vw':
return `${(valuePx / winWidth) * 100}${unit}`; valueResult = (valuePx / winWidth) * 100;
break;
case 'vh': case 'vh':
return `${(valuePx / winHeight) * 100}${unit}`; valueResult = (valuePx / winHeight) * 100;
break;
case 'vmin': { case 'vmin': {
const vmin = Math.min(winWidth, winHeight); const vmin = Math.min(winWidth, winHeight);
return `${(valuePx / vmin) * 100}${unit}`; valueResult = (valuePx / vmin) * 100;
break;
} }
case 'vmax': { case 'vmax': {
const vmax = Math.max(winWidth, winHeight); const vmax = Math.max(winWidth, winHeight);
return `${(valuePx / vmax) * 100}${unit}`; valueResult = (valuePx / vmax) * 100;
break;
} }
case '%': { case '%': {
const parentSize = el.parentElement?.offsetWidth || 1; const parentSize = el.parentElement?.offsetWidth || 1;
return `${(valuePx / parentSize) * 100}${unit}`; valueResult = (valuePx / parentSize) * 100;
break;
} }
case 'svw': case 'svw':
case 'lvw': case 'lvw':
case 'dvw': case 'dvw':
return `${(valuePx / winWidth) * 100}${unit}`; valueResult = (valuePx / winWidth) * 100;
break;
case 'svh': case 'svh':
case 'lvh': case 'lvh':
case 'dvh': case 'dvh':
return `${(valuePx / winHeight) * 100}${unit}`; valueResult = (valuePx / winHeight) * 100;
break;
default: default:
return `${valuePx}px`; untiResult = 'px';
} }
return `${+valueResult.toFixed(roundDecimals)}${untiResult}`;
}, },
} as CommandObject<ComponentResizeOptions, { canvasResizer?: Resizer }>; } as CommandObject<
ComponentResizeOptions,
{
canvasResizer?: Resizer;
convertPxToUnit: (props: ConvertPxToUnitProps) => string;
}
>;

Loading…
Cancel
Save