Skip to content

Commit

Permalink
feat: batch selection with Shift
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 19, 2022
1 parent 3bb6725 commit 9c2b0e5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Index = () => {
<HopeProvider config={theme}>
<ErrorBoundary
fallback={(err) => {
console.log("error", err);
console.error("error", err);
return <Error msg={`System error: ${err.message}`} />;
}}
>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/home/folder/GridItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export const GridItem = (props: { obj: StoreObj; index: number }) => {
}}
as={LinkWithPush}
href={props.obj.name}
onMouseEnter={() => {
onMouseOver={() => {
setHover(true);
if (props.obj.is_dir) {
setPathAsDir(props.obj.name, true);
}
}}
onMouseLeave={() => {
onMouseOut={() => {
setHover(false);
}}
>
Expand All @@ -58,7 +58,7 @@ export const GridItem = (props: { obj: StoreObj; index: number }) => {
<Checkbox
pos="absolute"
left="$1"
top="$1"
top="$1"
// colorScheme="neutral"
// @ts-ignore
on:click={(e) => {
Expand Down
1 change: 0 additions & 1 deletion src/pages/manage/settings/Common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const CommonSettings = (props: CommonSettingsProps) => {
<Button
loading={saveLoading()}
onClick={async () => {
console.log(settings);
const resp: Resp<{}> = await saveSettings();
handleRresp(resp, () => notify.success(t("global.save_success")));
}}
Expand Down
8 changes: 8 additions & 0 deletions src/store/key-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const keyPressed: Record<string, boolean> = {};
document.addEventListener("keydown", (e) => {
keyPressed[e.key] = true;
});
document.addEventListener("keyup", (e) => {
// keyPressed[e.key] = false;
delete keyPressed[e.key];
});
48 changes: 38 additions & 10 deletions src/store/obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createSignal } from "solid-js";
import { createStore, produce } from "solid-js/store";
import { Obj, StoreObj } from "~/types";
import { log } from "~/utils";
import { keyPressed } from "./key-event";

export enum State {
Initial, // Initial state
Expand Down Expand Up @@ -47,6 +48,7 @@ const [selectedNum, setSelectedNum] = createSignal(0);

const setObjs = (objs: Obj[]) => {
setSelectedNum(0);
lastChecked = { index: -1, selected: false };
setObjStore("objs", objs);
};

Expand Down Expand Up @@ -101,17 +103,43 @@ export const appendObjs = (objs: Obj[]) => {
);
};

let lastChecked = {
index: -1,
selected: false,
};

export const selectIndex = (index: number, selected: boolean) => {
setObjStore(
"objs",
index,
produce((obj) => {
if (obj.selected !== selected) {
setSelectedNum(selected ? selectedNum() + 1 : selectedNum() - 1);
}
obj.selected = selected;
})
);
if (
keyPressed["Shift"] &&
lastChecked.index !== -1 &&
lastChecked.selected === selected
) {
const start = Math.min(lastChecked.index, index);
const end = Math.max(lastChecked.index, index);
const curCheckedNum = objStore.objs
.slice(start, end + 1)
.filter((o) => o.selected).length;

setObjStore("objs", { from: start, to: end }, () => ({
selected: selected,
}));
// update selected num
const newSelectedNum =
selectedNum() - curCheckedNum + (selected ? end - start + 1 : 0);
setSelectedNum(newSelectedNum);
} else {
setObjStore(
"objs",
index,
produce((obj) => {
if (obj.selected !== selected) {
setSelectedNum(selected ? selectedNum() + 1 : selectedNum() - 1);
}
obj.selected = selected;
})
);
}
lastChecked = { index, selected };
};

export const selectAll = (selected: boolean) => {
Expand Down

0 comments on commit 9c2b0e5

Please sign in to comment.