Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions frontend/src/components/panels/Layers.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
let justFinishedDrag = false; // Used to prevent click events after a drag
let dragInPanel = false;

// Alt+Drag duplication
let altKeyPressedDuringDrag = false;
let originalLayersBeforeDuplication: bigint[] | undefined = undefined;
let duplicatedLayerIds: bigint[] | undefined = undefined;

// Interactive clipping
let layerToClipUponClick: LayerListingInfo | undefined = undefined;
let layerToClipAltKeyPressed = false;
Expand Down Expand Up @@ -107,6 +112,8 @@
addEventListener("pointermove", draggingPointerMove);
addEventListener("mousedown", draggingMouseDown);
addEventListener("keydown", draggingKeyDown);
addEventListener("keyup", draggingKeyUp);
addEventListener("blur", () => internalDragState?.active && abortDrag());

addEventListener("pointermove", clippingHover);
addEventListener("keydown", clippingKeyPress);
Expand All @@ -124,6 +131,7 @@
removeEventListener("pointermove", draggingPointerMove);
removeEventListener("mousedown", draggingMouseDown);
removeEventListener("keydown", draggingKeyDown);
removeEventListener("keyup", draggingKeyUp);

removeEventListener("pointermove", clippingHover);
removeEventListener("keydown", clippingKeyPress);
Expand Down Expand Up @@ -469,11 +477,43 @@
abortDrag();
}

async function startDuplicates() {
originalLayersBeforeDuplication = [...$nodeGraph.selected];
editor.handle.duplicateSelectedLayers();

await tick();
duplicatedLayerIds = [...$nodeGraph.selected];
}

function stopDuplicates() {
if (!originalLayersBeforeDuplication || !duplicatedLayerIds) return;

duplicatedLayerIds.forEach(layerId => {
editor.handle.deleteNode(layerId);
});

editor.handle.deselectAllLayers();
originalLayersBeforeDuplication.forEach((layerId, index) => {
const ctrl = index > 0;
editor.handle.selectLayer(layerId, ctrl, false);
});

originalLayersBeforeDuplication = undefined;
duplicatedLayerIds = undefined;
}

function abortDrag() {
if (altKeyPressedDuringDrag && originalLayersBeforeDuplication) {
stopDuplicates();
}

internalDragState = undefined;
draggingData = undefined;
fakeHighlightOfNotYetSelectedLayerBeingDragged = undefined;
dragInPanel = false;
altKeyPressedDuringDrag = false;
originalLayersBeforeDuplication = undefined;
duplicatedLayerIds = undefined;
}

function draggingMouseDown(e: MouseEvent) {
Expand All @@ -489,6 +529,18 @@
justFinishedDrag = true;
abortDrag();
}

if (e.key === "Alt" && !altKeyPressedDuringDrag) {
altKeyPressedDuringDrag = true;
startDuplicates();
}
}

function draggingKeyUp(e: KeyboardEvent) {
if (e.key === "Alt" && altKeyPressedDuringDrag) {
altKeyPressedDuringDrag = false;
stopDuplicates();
}
}

function fileDragOver(e: DragEvent) {
Expand Down
7 changes: 7 additions & 0 deletions frontend/wasm/src/editor_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,13 @@ impl EditorHandle {
self.dispatch(message);
}

/// Duplicate the currently selected layers
#[wasm_bindgen(js_name = duplicateSelectedLayers)]
pub fn duplicate_selected_layers(&self) {
let message = DocumentMessage::DuplicateSelectedLayers;
self.dispatch(message);
}

/// Move a layer to within a folder and placed down at the given index.
/// If the folder is `None`, it is inserted into the document root.
/// If the insert index is `None`, it is inserted at the start of the folder.
Expand Down