select and move multiple components #5359
Unanswered
badiuciprian
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Hello |
Beta Was this translation helpful? Give feedback.
0 replies
-
Not sure why you're reimplementing this, what is wrong with the current multiple selection? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys, is there a way to achieve this? I tried to implement a different approach but successfully failed:
i think this approach can be also used for grouping components.
I'm not a programmer so I tried putting pieces together and so far I have this, but obviously it is crap.
const selectedComponents = [];
let isDragging = false;
let offsetX, offsetY;
let wrapperComponent = null;
editor.on('component:selected', (component) => {
if (!selectedComponents.includes(component)) {
selectedComponents.push(component);
component.addStyle({ border: '2px solid blue' });
}
});
editor.on('component:deselected', (component) => {
const index = selectedComponents.indexOf(component);
if (index !== -1) {
selectedComponents.splice(index, 1);
component.addStyle({ border: 'none' });
}
});
editor.Canvas.getBody().addEventListener('mousedown', (event) => {
if (event.shiftKey) {
const clickedComponent = editor.getSelected();
if (clickedComponent && !selectedComponents.includes(clickedComponent)) {
selectedComponents.push(clickedComponent);
clickedComponent.addStyle({ border: '2px solid blue' });
}
isDragging = true;
offsetX = event.clientX;
offsetY = event.clientY;
}
});
editor.Canvas.getBody().addEventListener('mousemove', (event) => {
if (isDragging) {
if (!wrapperComponent) {
wrapperComponent = editor.DomComponents.addComponent({
tagName: 'div',
style: {
position: 'absolute',
left:
${selectedComponents[0].get('style').left}
,top:
${selectedComponents[0].get('style').top}
,},
});
selectedComponents.forEach((component) => {
wrapperComponent.get('components').add(component);
});
editor.getContainer().appendChild(wrapperComponent.view.el);
}
const dx = event.clientX - offsetX;
const dy = event.clientY - offsetY;
wrapperComponent.set('style', {
left:
${parseInt(wrapperComponent.getStyle().left) + dx}px
,top:
${parseInt(wrapperComponent.getStyle().top) + dy}px
,});
offsetX = event.clientX;
offsetY = event.clientY;
}
});
editor.Canvas.getBody().addEventListener('mouseup', () => {
isDragging = false;
if (wrapperComponent) {
wrapperComponent.remove();
wrapperComponent = null;
selectedComponents.forEach((component) => {
const componentStyle = component.get('style');
editor.setStyle(component, {
left: componentStyle.left,
top: componentStyle.top,
});
});
}
});
Beta Was this translation helpful? Give feedback.
All reactions