Allow quick duplicating via ctrl/cmd + drag

This commit is contained in:
Andrew Kingston 2024-08-14 11:21:59 +01:00
parent b62371d1be
commit a755fe7630
No known key found for this signature in database
4 changed files with 25 additions and 10 deletions

View File

@ -144,7 +144,12 @@
const rootComponent = get(selectedScreen).props
const component = findComponent(rootComponent, data.id)
componentStore.copy(component)
await componentStore.paste(component)
await componentStore.paste(
component,
data.mode,
null,
data.selectComponent
)
} else if (type === "preview-loaded") {
// Wait for this event to show the client library if intelligent
// loading is supported

View File

@ -653,7 +653,7 @@ export class ComponentStore extends BudiStore {
* @param {object} targetScreen
* @returns
*/
async paste(targetComponent, mode, targetScreen) {
async paste(targetComponent, mode, targetScreen, selectComponent = true) {
const state = get(this.store)
if (!state.componentToPaste) {
return
@ -728,12 +728,13 @@ export class ComponentStore extends BudiStore {
await screenStore.patch(patch, targetScreenId)
// Select the new component
this.update(state => {
state.selectedScreenId = targetScreenId
state.selectedComponentId = newComponentId
return state
})
if (selectComponent) {
this.update(state => {
state.selectedScreenId = targetScreenId
state.selectedComponentId = newComponentId
return state
})
}
componentTreeNodesStore.makeNodeVisible(newComponentId)
}

View File

@ -120,6 +120,11 @@
id = component.dataset.id
}
// If holding ctrl/cmd then leave behind a duplicate of this component
if (mode === GridDragModes.Move && (e.ctrlKey || e.metaKey)) {
builderStore.actions.duplicateComponent(id, "above", false)
}
// Find grid parent and read from DOM
const domComponent = document.getElementsByClassName(id)[0]
const domGrid = domComponent?.closest(".grid")

View File

@ -49,8 +49,12 @@ const createBuilderStore = () => {
keyDown: (key, ctrlKey) => {
eventStore.actions.dispatchEvent("key-down", { key, ctrlKey })
},
duplicateComponent: id => {
eventStore.actions.dispatchEvent("duplicate-component", { id })
duplicateComponent: (id, mode = "below", selectComponent = true) => {
eventStore.actions.dispatchEvent("duplicate-component", {
id,
mode,
selectComponent,
})
},
deleteComponent: id => {
eventStore.actions.dispatchEvent("delete-component", { id })