123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- import { MoveableManagerInterface, Renderer } from 'moveable';
- import { HorizontalConstraint, VerticalConstraint } from '../types';
- import { Scene } from './scene';
- export const dimensionViewable = {
- name: 'dimensionViewable',
- props: {},
- events: {},
- render(moveable: MoveableManagerInterface<any, any>, React: Renderer) {
- const rect = moveable.getRect();
- return (
- <div
- key={'dimension-viewable'}
- className={'moveable-dimension'}
- style={{
- position: 'absolute',
- left: `${rect.width / 2}px`,
- top: `${rect.height + 20}px`,
- background: '#4af',
- borderRadius: '2px',
- padding: '2px 4px',
- color: 'white',
- fontSize: '13px',
- whiteSpace: 'nowrap',
- fontWeight: 'bold',
- willChange: 'transform',
- transform: 'translate(-50%, 0px)',
- zIndex: 100,
- }}
- >
- {Math.round(rect.offsetWidth)} x {Math.round(rect.offsetHeight)}
- </div>
- );
- },
- };
- export const constraintViewable = (scene: Scene) => ({
- name: 'constraintViewable',
- props: {},
- events: {},
- render(moveable: MoveableManagerInterface<any, any>, React: Renderer) {
- const rect = moveable.getRect();
- const targetElement = scene.findElementByTarget(moveable.state.target);
- // If target is currently in motion or selection is more than 1 element don't display constraint visualizations
- if (
- targetElement?.isMoving ||
- (scene.selecto?.getSelectedTargets() && scene.selecto?.getSelectedTargets().length > 1)
- ) {
- return;
- }
- let verticalConstraintVisualization = null;
- let horizontalConstraintVisualization = null;
- const constraint = targetElement?.tempConstraint ?? targetElement?.options.constraint ?? {};
- const borderStyle = '1px dashed #4af';
- const centerIndicatorLineOne = React.createElement('div', {
- style: {
- position: 'absolute',
- left: `${rect.width / 2}px`,
- top: `${rect.height / 2 - rect.height / 16}px`,
- borderLeft: borderStyle,
- height: `${rect.height / 8}px`,
- transform: 'rotate(45deg)',
- },
- });
- const centerIndicatorLineTwo = React.createElement('div', {
- style: {
- position: 'absolute',
- left: `${rect.width / 2}px`,
- top: `${rect.height / 2 - rect.height / 16}px`,
- borderLeft: borderStyle,
- height: `${rect.height / 8}px`,
- transform: 'rotate(-45deg)',
- },
- });
- const centerIndicator = React.createElement('div', {}, [centerIndicatorLineOne, centerIndicatorLineTwo]);
- const verticalConstraintTop = React.createElement('div', {
- style: {
- position: 'absolute',
- left: `${rect.width / 2}px`,
- bottom: '0px',
- borderLeft: borderStyle,
- height: '100vh',
- },
- });
- const verticalConstraintBottom = React.createElement('div', {
- style: {
- position: 'absolute',
- left: `${rect.width / 2}px`,
- top: `${rect.height}px`,
- borderLeft: borderStyle,
- height: '100vh',
- },
- });
- const verticalConstraintTopBottom = React.createElement('div', {}, [
- verticalConstraintTop,
- verticalConstraintBottom,
- ]);
- const verticalConstraintCenterLine = React.createElement('div', {
- style: {
- position: 'absolute',
- left: `${rect.width / 2}px`,
- top: `${rect.height / 4}px`,
- borderLeft: borderStyle,
- height: `${rect.height / 2}px`,
- },
- });
- const verticalConstraintCenter = React.createElement('div', {}, [verticalConstraintCenterLine, centerIndicator]);
- switch (constraint.vertical) {
- case VerticalConstraint.Top:
- verticalConstraintVisualization = verticalConstraintTop;
- break;
- case VerticalConstraint.Bottom:
- verticalConstraintVisualization = verticalConstraintBottom;
- break;
- case VerticalConstraint.TopBottom:
- verticalConstraintVisualization = verticalConstraintTopBottom;
- break;
- case VerticalConstraint.Center:
- verticalConstraintVisualization = verticalConstraintCenter;
- break;
- }
- const horizontalConstraintLeft = React.createElement('div', {
- style: {
- position: 'absolute',
- right: '0px',
- top: `${rect.height / 2}px`,
- borderTop: borderStyle,
- width: '100vw',
- },
- });
- const horizontalConstraintRight = React.createElement('div', {
- style: {
- position: 'absolute',
- left: `${rect.width}px`,
- top: `${rect.height / 2}px`,
- borderTop: borderStyle,
- width: '100vw',
- },
- });
- const horizontalConstraintLeftRight = React.createElement('div', {}, [
- horizontalConstraintLeft,
- horizontalConstraintRight,
- ]);
- const horizontalConstraintCenterLine = React.createElement('div', {
- style: {
- position: 'absolute',
- left: `${rect.width / 4}px`,
- top: `${rect.height / 2}px`,
- borderTop: borderStyle,
- width: `${rect.width / 2}px`,
- },
- });
- const horizontalConstraintCenter = React.createElement('div', {}, [
- horizontalConstraintCenterLine,
- centerIndicator,
- ]);
- switch (constraint.horizontal) {
- case HorizontalConstraint.Left:
- horizontalConstraintVisualization = horizontalConstraintLeft;
- break;
- case HorizontalConstraint.Right:
- horizontalConstraintVisualization = horizontalConstraintRight;
- break;
- case HorizontalConstraint.LeftRight:
- horizontalConstraintVisualization = horizontalConstraintLeftRight;
- break;
- case HorizontalConstraint.Center:
- horizontalConstraintVisualization = horizontalConstraintCenter;
- break;
- }
- const constraintVisualization = React.createElement('div', {}, [
- verticalConstraintVisualization,
- horizontalConstraintVisualization,
- ]);
- return constraintVisualization;
- },
- });
|