Skip to content

Commit

Permalink
chore(ui): refactor selection logic - WF-148
Browse files Browse the repository at this point in the history
  • Loading branch information
madeindjs committed Dec 20, 2024
1 parent 4cc8151 commit 096a811
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
10 changes: 1 addition & 9 deletions src/ui/src/builder/BuilderApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,7 @@ function handleRendererClick(ev: PointerEvent): void {
ev.preventDefault();
ev.stopPropagation();
if (!ev.shiftKey && !ev.ctrlKey) {
return ssbm.setSelection(targetId, targetInstancePath, "click");
}
if (ssbm.isComponentIdSelected(targetId)) {
ssbm.removeSelectedComponentId(targetId);
} else {
ssbm.appendSelection(targetId, targetInstancePath, "click");
}
ssbm.handleSelectionFromEvent(ev, targetId, targetInstancePath, "click");
}
const handleRendererDragStart = (ev: DragEvent) => {
Expand Down
25 changes: 25 additions & 0 deletions src/ui/src/builder/builderManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,30 @@ describe(generateBuilderManager.name, () => {
expect(isComponentIdSelected("componentId")).toBeFalsy();
expect(selectionStatus.value).toBe(SelectionStatus.None);
});

it("should handle click events", () => {
const {
handleSelectionFromEvent,
isComponentIdSelected,
selectionStatus,
} = generateBuilderManager();

handleSelectionFromEvent({ ctrlKey: true } as KeyboardEvent, "1");

expect(selectionStatus.value).toBe(SelectionStatus.Single);
expect(isComponentIdSelected("1")).toBeTruthy();

handleSelectionFromEvent({ ctrlKey: true } as KeyboardEvent, "2");

expect(selectionStatus.value).toBe(SelectionStatus.Multiple);
expect(isComponentIdSelected("1")).toBeTruthy();
expect(isComponentIdSelected("2")).toBeTruthy();

handleSelectionFromEvent({ ctrlKey: true } as KeyboardEvent, "2");

expect(selectionStatus.value).toBe(SelectionStatus.Single);
expect(isComponentIdSelected("1")).toBeTruthy();
expect(isComponentIdSelected("2")).toBeFalsy();
});
});
});
18 changes: 18 additions & 0 deletions src/ui/src/builder/builderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ export function generateBuilderManager() {
});
};

const handleSelectionFromEvent = (
ev: MouseEvent | KeyboardEvent,
componentId: Component["id"],
instancePath?: string,
source?: SelectionSource,
) => {
if (!ev.shiftKey && !ev.ctrlKey) {
return setSelection(componentId, instancePath, source);
}

if (isComponentIdSelected(componentId)) {
removeSelectedComponentId(componentId);
} else {
appendSelection(componentId, instancePath, source);
}
};

const isComponentIdSelected = (componentId: string) => {
return state.value.selection.some((s) => s.componentId === componentId);
};
Expand Down Expand Up @@ -369,6 +386,7 @@ export function generateBuilderManager() {
removeSelectedComponentId,
setSelection,
appendSelection,
handleSelectionFromEvent,
getSelection,
getSelectedId,
setClipboard,
Expand Down
12 changes: 1 addition & 11 deletions src/ui/src/builder/sidebar/BuilderSidebarComponentTreeBranch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,7 @@ const name = computed(() => {
async function select(ev: MouseEvent | KeyboardEvent) {
goToComponentParentPage(props.componentId);
await nextTick();
if (!ev.shiftKey && !ev.ctrlKey) {
return wfbm.setSelection(props.componentId, undefined, "tree");
}
if (wfbm.isComponentIdSelected(props.componentId)) {
wfbm.removeSelectedComponentId(props.componentId);
} else {
wfbm.appendSelection(props.componentId, undefined, "tree");
}
wfbm.handleSelectionFromEvent(ev, props.componentId, undefined, "tree");
expand();
}
Expand Down

0 comments on commit 096a811

Please sign in to comment.