Skip to content

Commit

Permalink
Merge pull request #59 from glaciyan/drag-reorder
Browse files Browse the repository at this point in the history
Reorder builds by dragging
  • Loading branch information
glaciyan authored Jan 9, 2022
2 parents 67d891c + 66d4bc7 commit 1a5a148
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 24 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gscale",
"license": "MIT",
"version": "2.1.4",
"version": "2.2.0",
"scripts": {
"pre": "npm run gen:version",
"dev": "npm run pre && vite",
Expand All @@ -26,7 +26,8 @@
"dexie": "^3.2.0",
"fuse.js": "^6.4.6",
"vue": "^3.2.16",
"vue-router": "^4"
"vue-router": "^4",
"vuedraggable": "^4.1.0"
},
"devDependencies": {
"@types/lodash": "^4.14.178",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import TheFooter from "./components/TheFooter.vue";

<style>
/* @import url(./assets/fonts/OpenSans.css); */
@import url(./styles/global.less);
@import url(./styles/transition/fade.css);
@import url(./styles/transition/build-preview.css);
@import url(./styles/dark.css);
Expand Down
7 changes: 6 additions & 1 deletion src/components/ItemCardClickable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const tooltip = computed(() => {
</script>

<template>
<div v-tooltip="tooltip" class="cursor-pointer m-1 relative select-none" @click="$emit('click', fillAmount)">
<div
v-tooltip="tooltip"
class="cursor-pointer m-1 relative select-none focus-visible:ring"
tabindex="0"
@click="$emit('click', fillAmount)"
>
<ItemCard :itemWithAmount="item" :grayscale="done" class="!m-0" />
<div
:class="[
Expand Down
18 changes: 16 additions & 2 deletions src/components/character/CharacterBuildPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ import ItemChecklist from "../ItemChecklist.vue";
import RangeLevelDisplay from "../levelRange/display/RangeLevelDisplay.vue";
import RangeTalentDisplay from "../levelRange/display/RangeTalentDisplay.vue";
import Modal from "../Modal.vue";
import HandleIcon from "../icons/HandleIcon.vue";
const props = defineProps<{ character: ICharacter | ITraveler; items: ItemWithAmount[]; data: Build }>();
const props =
defineProps<{ character: ICharacter | ITraveler; items: ItemWithAmount[]; data: Build; handle?: boolean }>();
const emit = defineEmits(["deleted"]);
watch(
() => props.data.order,
(value) => {
console.log(value);
}
);
//#region Deleting
const deleteBuild = async () => {
if (props.data.id) {
Expand Down Expand Up @@ -109,7 +118,12 @@ const edit = () => {
</div>
</div>
</div>
<div class="flex bg-dark-600/70 border-t-2 border-dark-300 py-3 px-6 justify-end">
<div class="flex bg-dark-600/70 border-t-2 border-dark-300 py-3 px-6 items-center">
<div class="flex-1">
<div title="Drag to reorder" class="w-max">
<HandleIcon class="cursor-move h-5 text-light-ternary w-5 handle" />
</div>
</div>
<EButton look="ghost" element="neutral" class="mr-2 !h-9 !text-light-ternary" @click="deleteDialog.reveal">
Delete
</EButton>
Expand Down
5 changes: 5 additions & 0 deletions src/components/icons/HandleIcon.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
</svg>
</template>
5 changes: 5 additions & 0 deletions src/lib/offlineDatabase/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Talent } from "../types/Talent";
export interface Build {
id?: number;
order?: number;
orderChanged?: number;
completed?: Array<ItemWithAmount>;
type: string;
entityId: string;
Expand Down Expand Up @@ -53,6 +54,10 @@ export class CustomDexie extends Dexie {
}
});
});

this.version(3).stores({
builds: "++id, order, orderChanged, *completed, type, entityId, level, normal, elemental, burst",
});
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/lib/util/sortingBuilds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Build } from "../offlineDatabase/db";

export default (a: Build, b: Build) => {
if (a.order === b.order) {
return (b.orderChanged ?? 0) - (a.orderChanged ?? 0);
}

return (a.order ?? Infinity) - (b.order ?? Infinity);
};
65 changes: 46 additions & 19 deletions src/pages/Builds.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Container from "../components/PageContainer";
import { RouterLink } from "vue-router";
import clearDb from "~/lib/dev/clearDb";
import tonsOfBuilds from "~/lib/dev/tonsOfBuilds";
import Draggable from "vuedraggable";
import sortingBuilds from "~/lib/util/sortingBuilds";
const DEV = import.meta.env.DEV;
Expand All @@ -23,10 +25,15 @@ const buildsReady = computed(() => buildsData.value !== undefined);
const getBuilds = async () => {
const buildsFromDb = await db.builds.where("type").equals("character").toArray();
buildsData.value = buildsFromDb.map((build) => {
let lastOrder = 0;
buildsData.value = buildsFromDb.sort(sortingBuilds).map((build) => {
const character = repo.needCharacter(build.entityId);
const items = totalBuildItems(character, build);
if (build.order) lastOrder = build.order;
else build.order = ++lastOrder;
return {
character,
items,
Expand Down Expand Up @@ -64,6 +71,15 @@ const hideTotal = () => {
scrollLock.value = false;
};
//#endregion
const handleChange = async ({ moved }: { moved: { element: any; newIndex: number } }) => {
const element = toRaw(moved.element) as { character: ICharacter | ITraveler; items: ItemWithAmount[]; data: Build };
const timeChanged = Date.now();
console.log(`Moved ${element.character.name} to ${moved.newIndex} at ${timeChanged}`);
await db.builds.update(element.data.id!, { order: moved.newIndex, orderChanged: timeChanged });
};
</script>

<template>
Expand All @@ -75,24 +91,35 @@ const hideTotal = () => {
<div v-if="buildsData!.length > 0" class="flex space-x-2 mb-4">
<GButton @click="showTotal">Show Total</GButton>
</div>
<transition-group tag="div" name="build-preview" w:grid="gap-5 cols-2 <sm:cols-1" class="grid">
<CharacterBuildPreview
v-for="build in buildsData"
:key="build.data.id"
:character="build.character"
:items="build.items"
:data="build.data"
@deleted="onDelete"
/>
<RouterLink
v-if="buildsData!.length < 2"
to="/"
class="border-dashed rounded-xl flex flex-col h-full border-4 border-dark-600 min-h-96 py-4 px-6 transition-colors text-light-ternary/70 block items-center justify-center hover:text-light-ternary"
>
<p class="font-bold text-xl">Add a new build</p>
<p class="font-bold text-xl">+</p>
</RouterLink>
</transition-group>
<Draggable
v-model="buildsData"
tag="transition-group"
:componentData="{ tag: 'div', name: 'build-preview' }"
class="grid gap-5 grid-cols-2 <sm:grid-cols-1"
:itemKey="(item: any) => item.data.id"
handle=".handle"
@change="handleChange"
>
<template #item="{ element }">
<CharacterBuildPreview
:key="element.data.id"
:character="element.character"
:items="element.items"
:data="element.data"
@deleted="onDelete"
/>
</template>
<template #footer>
<RouterLink
v-if="buildsData!.length < 2"
to="/"
class="border-dashed rounded-xl flex flex-col h-full border-4 border-dark-600 min-h-96 py-4 px-6 transition-colors text-light-ternary/70 block items-center justify-center hover:text-light-ternary"
>
<p class="font-bold text-xl">Add a new build</p>
<p class="font-bold text-xl">+</p>
</RouterLink>
</template>
</Draggable>
</Container>
<teleport to="#modal">
<PopOver
Expand Down
4 changes: 4 additions & 0 deletions src/styles/global.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.sortable-ghost {
transition: opacity 0.3s;
opacity: 0.6;
}

1 comment on commit 1a5a148

@vercel
Copy link

@vercel vercel bot commented on 1a5a148 Jan 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.