Skip to content

Commit

Permalink
Merge pull request #18 from Lenni009/dev
Browse files Browse the repository at this point in the history
add drag'n'drop support for file upload
  • Loading branch information
Lenni009 authored Nov 11, 2023
2 parents 8de3dc1 + b07d41a commit 9f44f1c
Show file tree
Hide file tree
Showing 11 changed files with 1,367 additions and 1,062 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Build App
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Test Build
uses: Lenni009/test-build-vite-action@main
2,356 changes: 1,322 additions & 1,034 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "rewardtable-tools-v2",
"name": "rewardtable-tools",
"version": "0.0.0",
"private": true,
"scripts": {
Expand Down
Binary file removed public/favicon.ico
Binary file not shown.
39 changes: 32 additions & 7 deletions src/components/inputs/file-input.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useDocumentStore } from 'src/stores/document';
import { ref } from 'vue';
defineProps<{
inputId: string;
}>()
const fileInput = ref<HTMLInputElement | null>(null);
const fileName = ref('');
const dragActive = ref(false);
function updateLabel() {
if (fileInput.value?.files && fileInput.value.files.length > 0) {
fileName.value = fileInput.value.files[0].name
}
const documentStore = useDocumentStore();
function updateLabel(file: File) {
fileName.value = file.name
}
function addFile(file: File | null = null) {
const uploadedFile = file ?? fileInput.value?.files?.[0];
if (!uploadedFile) return;
updateLabel(uploadedFile);
documentStore.readFile(uploadedFile);
}
function dropFile(e: DragEvent) {
dragActive.value = false;
const uploadedFile = e.dataTransfer?.files?.[0];
if (uploadedFile) addFile(uploadedFile);
}
</script>

<template>
<div class="file has-name is-boxed">
<label class="file-label">
<input ref="fileInput" class="file-input" type="file" :id="inputId" @change="updateLabel">
<label :class="{'drag-active': dragActive}" class="file-label" :for="inputId" @dragenter="dragActive = true" @dragleave="dragActive = false" @drop.prevent="dropFile" @dragover.prevent>
<input ref="fileInput" class="file-input" type="file" :id="inputId" @change="addFile()" />
<span class="file-cta">
<span class="file-icon">
<i class="material-icons file-icon-element">upload</i>
Expand All @@ -36,6 +50,17 @@ function updateLabel() {
</template>

<style scoped lang="scss">
.file-label {
* {
pointer-events: none;
}
&.drag-active .file-cta,
&:hover .file-cta {
background-color: #eeeeee;
}
}
.file-icon {
margin-right: 0;
Expand Down
5 changes: 1 addition & 4 deletions src/components/inputs/file-upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import TextLabel from 'src/components/text-label.vue';
import Tooltip from 'src/components/tooltip.vue';
import FileInput from './file-input.vue';
import { useDocumentStore } from 'src/stores/document';
const documentStore = useDocumentStore();
</script>

<template>
Expand All @@ -13,7 +10,7 @@ const documentStore = useDocumentStore();
<text-label inputId="fileUpload">Upload REWARDTABLE.EXML</text-label>
<tooltip>Can be found in METADATA/REALITY/TABLES.</tooltip>
</div>
<file-input type="file" input-id="fileUpload" @change="documentStore.readFile($event.target)" />
<file-input type="file" input-id="fileUpload" />
</div>
</template>

Expand Down
6 changes: 3 additions & 3 deletions src/components/output/chances-table.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, reactive, ref, type Ref } from 'vue';
import { computed } from 'vue';
import TextLabel from 'src/components/text-label.vue';
import { rewardChances, searchRewardSection } from 'src/logic/logic';
import { useRewardStore } from 'src/stores/reward';
Expand Down Expand Up @@ -49,12 +49,12 @@ const divTable = computed(() => {
<style scoped lang="scss">
.chancesTable {
display: grid;
grid-template-columns: 0.1fr 1.5fr 1fr 0.3fr;
grid-template-columns: repeat(4, auto);
border: 1px solid black;
&>* {
border: 1px solid black;
padding: 0 2px;
padding-inline: 3px;
white-space: nowrap;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/output/exml-snippet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ watch(data, (newValue) => {
if (newValue.exmlOutput) newValue.exmlOutput.innerText = xmlString;
})
function getXmlString(fileXmlDoc: XMLDocument | undefined, rewardSearchTerm: string) {
function getXmlString(fileXmlDoc: XMLDocument | null, rewardSearchTerm: string) {
if (!fileXmlDoc || !rewardSearchTerm) return;
const domSection = searchRewardSection(fileXmlDoc, rewardSearchTerm);
if (!domSection) return;
Expand Down
2 changes: 1 addition & 1 deletion src/logic/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function processEXML(contents: string) {
/**
* searches the EXML section for a given reward and returns its DOM element
*/
export function searchRewardSection(xmlDoc: XMLDocument | undefined, rewardId: string): Element | undefined {
export function searchRewardSection(xmlDoc: XMLDocument | null, rewardId: string): Element | undefined {
if (!xmlDoc) return;
const xmlSectionDom = xmlDoc.querySelector(`[value="GcGenericRewardTableEntry.xml"] > [name="Id"][value=${rewardId} i], [value="GcRewardTableEntry.xml"] > [name="Id"][value=${rewardId} i]`)?.parentNode as Element | undefined;
return xmlSectionDom;
Expand Down
13 changes: 4 additions & 9 deletions src/stores/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@ import { processEXML } from 'src/logic/logic';
import { defineStore } from 'pinia'

interface DocumentState {
fileXmlDoc: XMLDocument | undefined;
fileXmlDoc: XMLDocument | null;
}

export const useDocumentStore = defineStore('document', {
state: (): DocumentState => {
return {
fileXmlDoc: undefined,
fileXmlDoc: null,
}
},

actions: {
readFile(element: HTMLInputElement) {
if (!element?.files) return;

const file = element.files[0];
if (!file) return;

readFile(file: File) {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = (e) => {
const contents = e.target?.result;
this.fileXmlDoc = typeof contents === 'string' ? processEXML(contents) : undefined;
this.fileXmlDoc = typeof contents === 'string' ? processEXML(contents) : null;
}
},
}
Expand Down

0 comments on commit 9f44f1c

Please sign in to comment.