Skip to content

Commit d95cd5b

Browse files
committed
Feature: Mobile layput for container widget & it's children
1 parent 1a719ce commit d95cd5b

File tree

6 files changed

+113
-25
lines changed

6 files changed

+113
-25
lines changed

frontend/src/Editor/Components/Container.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const Container = function Container({
3030
<SubCustomDragLayer
3131
parent={id}
3232
parentRef={parentRef}
33+
currentLayout={containerProps.currentLayout}
3334
/>
3435
</div>
3536
);

frontend/src/Editor/Editor.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
onQueryConfirm,
2525
onQueryCancel,
2626
runQuery,
27-
previewQuery
27+
setStateAsync
2828
} from '@/_helpers/appUtils';
2929
import { Confirm } from './Viewer/Confirm';
3030
import ReactTooltip from 'react-tooltip';
@@ -245,7 +245,8 @@ class Editor extends React.Component {
245245
};
246246

247247
componentDefinitionChanged = (newDefinition) => {
248-
this.setState({
248+
let _self = this;
249+
return setStateAsync(_self, {
249250
appDefinition: {
250251
...this.state.appDefinition,
251252
components: {

frontend/src/Editor/Inspector/Inspector.jsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const Inspector = ({
7373

7474
// User wants to show the widget on mobile devices
7575
if(param.name === 'showOnMobile' && value === true) {
76+
7677
let newComponent = {
7778
...component
7879
};
@@ -90,7 +91,34 @@ export const Inspector = ({
9091
}
9192

9293
setComponent(newComponent);
93-
componentDefinitionChanged(newComponent);
94+
componentDefinitionChanged(newComponent).then(() => {
95+
96+
// Child componets should also have a mobile layout
97+
const childComponents = Object.keys(allComponents).filter((key) => allComponents[key].parent === component.id);
98+
99+
childComponents.forEach((componentId) => {
100+
let newChild = {
101+
id: componentId,
102+
...allComponents[componentId]
103+
};
104+
105+
const { width, height } = newChild.layouts['desktop'];
106+
107+
newChild['layouts'] = {
108+
...newChild.layouts,
109+
mobile: {
110+
top: 100,
111+
left: 0,
112+
width: Math.min(width, 445),
113+
height: height
114+
}
115+
}
116+
117+
componentDefinitionChanged(newChild);
118+
119+
});
120+
});
121+
94122
}
95123
}
96124

frontend/src/Editor/SubContainer.jsx

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ export const SubContainer = ({
3131
zoomLevel,
3232
parent,
3333
parentRef,
34-
configHandleClicked
34+
configHandleClicked,
35+
deviceWindowWidth,
36+
scaleValue,
37+
selectedComponent,
38+
currentLayout
3539
}) => {
3640
zoomLevel = zoomLevel || 1;
3741

@@ -117,14 +121,40 @@ export const SubContainer = ({
117121
let left = 0;
118122
let top = 0;
119123

124+
let layouts = item['layouts'];
125+
const currentLayoutOptions = layouts ? layouts[item.currentLayout] : {};
126+
120127
const canvasBoundingRect = parentRef.current.getElementsByClassName('real-canvas')[0].getBoundingClientRect();
121128

122129
// Component already exists and this is just a reposition event
123130
if (id) {
124131
const delta = monitor.getDifferenceFromInitialOffset();
125132
componentData = item.component;
126-
left = Math.round(item.left + delta.x);
127-
top = Math.round(item.top + delta.y);
133+
left = Math.round(currentLayoutOptions.left + delta.x);
134+
top = Math.round(currentLayoutOptions.top + delta.y);
135+
136+
if (snapToGrid) {
137+
[left, top] = doSnapToGrid(left, top);
138+
}
139+
140+
let newBoxes = {
141+
...boxes,
142+
[id]: {
143+
...boxes[id],
144+
parent: parent,
145+
layouts: {
146+
...boxes[id]['layouts'],
147+
[item.currentLayout]: {
148+
...boxes[id]['layouts'][item.currentLayout],
149+
top: top,
150+
left: left,
151+
}
152+
}
153+
}
154+
};
155+
156+
setBoxes(newBoxes);
157+
128158
} else {
129159
// This is a new component
130160
componentMeta = componentTypes.find((component) => component.component === item.component.component);
@@ -146,15 +176,24 @@ export const SubContainer = ({
146176
[left, top] = doSnapToGrid(left, top);
147177
}
148178

179+
if(item.currentLayout === 'mobile') {
180+
componentData.definition.others.showOnDesktop.value = false;
181+
componentData.definition.others.showOnMobile.value = true;
182+
}
183+
149184
setBoxes({
150185
...boxes,
151186
[id]: {
152-
top: top,
153-
left: left,
154-
width: item.width > 0 ? item.width : componentMeta.defaultSize.width,
155-
height: item.height > 0 ? item.height : componentMeta.defaultSize.height,
156187
component: componentData,
157-
parent: parent
188+
parent: parent,
189+
layouts: {
190+
[item.currentLayout]: {
191+
top: top,
192+
left: left,
193+
width: componentMeta.defaultSize.width,
194+
height: componentMeta.defaultSize.height,
195+
}
196+
}
158197
}
159198
});
160199

@@ -170,21 +209,36 @@ export const SubContainer = ({
170209

171210
let { x, y } = position;
172211

173-
let { left, top, width, height } = boxes[id];
212+
const defaultData = {
213+
top: 100,
214+
left: 0,
215+
width: 445,
216+
height: 500
217+
};
218+
219+
let { left, top, width, height } = boxes[id]['layouts'][currentLayout] || defaultData;
174220

175221
top = y;
176222
left = x;
177223

178224
width = width + deltaWidth;
179225
height = height + deltaHeight
180226

181-
setBoxes(
182-
update(boxes, {
183-
[id]: {
184-
$merge: { width, height, top, left }
227+
let newBoxes = {
228+
...boxes,
229+
[id]: {
230+
...boxes[id],
231+
layouts: {
232+
...boxes[id]['layouts'],
233+
[currentLayout]: {
234+
...boxes[id]['layouts'][currentLayout],
235+
width, height, top, left
236+
}
185237
}
186-
})
187-
);
238+
}
239+
};
240+
241+
setBoxes(newBoxes);
188242
}
189243

190244
function paramUpdated(id, param, value) {
@@ -229,6 +283,10 @@ export const SubContainer = ({
229283
inCanvas={true}
230284
zoomLevel={zoomLevel}
231285
configHandleClicked={configHandleClicked}
286+
currentLayout={currentLayout}
287+
scaleValue={scaleValue}
288+
deviceWindowWidth={deviceWindowWidth}
289+
isSelectedComponent={selectedComponent? selectedComponent.id === key : false}
232290
/>
233291
))}
234292

frontend/src/Editor/SubCustomDragLayer.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const layerStyles = {
1212
height: '100%'
1313
};
1414

15-
function getItemStyles(delta, item, initialOffset, currentOffset, parentRef, parent) {
15+
function getItemStyles(delta, item, initialOffset, currentOffset, parentRef, parent, currentLayout) {
1616
if (!initialOffset || !currentOffset || !parentRef.current) {
1717
return {
1818
display: 'none'
@@ -32,8 +32,8 @@ function getItemStyles(delta, item, initialOffset, currentOffset, parentRef, par
3232
const realCanvasBoundingRect = parentRef.current.getElementsByClassName('real-canvas')[0].getBoundingClientRect();
3333

3434
if (id) { // Dragging within the canvas
35-
x = Math.round(item.left + delta.x);
36-
y = Math.round(item.top + delta.y);
35+
x = Math.round(item.layouts[currentLayout].left + delta.x);
36+
y = Math.round(item.layouts[currentLayout].top + delta.y);
3737
} else { // New component being dragged from components sidebar
3838
const offsetFromTopOfWindow = realCanvasBoundingRect.top;
3939
const offsetFromLeftOfWindow = realCanvasBoundingRect.left;
@@ -51,7 +51,7 @@ function getItemStyles(delta, item, initialOffset, currentOffset, parentRef, par
5151
WebkitTransform: transform
5252
};
5353
}
54-
export const SubCustomDragLayer = ({parentRef, parent}) => {
54+
export const SubCustomDragLayer = ({parentRef, parent, currentLayout}) => {
5555
const {
5656
itemType, isDragging, item, initialOffset, currentOffset, delta
5757
} = useDragLayer((monitor) => ({
@@ -65,7 +65,7 @@ export const SubCustomDragLayer = ({parentRef, parent}) => {
6565
function renderItem() {
6666
switch (itemType) {
6767
case ItemTypes.BOX:
68-
return <BoxDragPreview item={item} />;
68+
return <BoxDragPreview item={item} currentLayout={currentLayout} />;
6969
default:
7070
return null;
7171
}
@@ -76,7 +76,7 @@ export const SubCustomDragLayer = ({parentRef, parent}) => {
7676

7777
return (
7878
<div style={layerStyles} className="sub-custom-drag-layer">
79-
<div style={getItemStyles(delta, item, initialOffset, currentOffset, parentRef, parent)}>
79+
<div style={getItemStyles(delta, item, initialOffset, currentOffset, parentRef, parent, currentLayout)}>
8080
{renderItem()}
8181
</div>
8282
</div>

frontend/src/_helpers/appUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import _ from 'lodash';
66
import moment from 'moment';
77
import Tooltip from 'react-bootstrap/Tooltip';
88

9-
function setStateAsync(_ref, state) {
9+
export function setStateAsync(_ref, state) {
1010
return new Promise((resolve) => {
1111
_ref.setState(state, resolve);
1212
});

0 commit comments

Comments
 (0)