hacked together deck import functionality

printing
koehr 4 years ago
parent 749ab36ac1
commit c7022133a0

@ -54,6 +54,10 @@ header {
header > p { header > p {
opacity: .6; opacity: .6;
} }
footer {
display: block;
margin-top: 1.5em;
}
section[name=notifications] { section[name=notifications] {
display: block; display: block;
max-width: 70rem; max-width: 70rem;
@ -63,18 +67,20 @@ section[name=notifications] {
} }
#popup { #popup {
display: flex;
justify-content: center;
align-items: center;
position: fixed; position: fixed;
top: 0; top: 0;
left: 0; left: 0;
width: 100vw; width: 100vw;
height: 100vh; height: 100vh;
display: block;
background-color: #0008; background-color: #0008;
overflow-y: auto;
} }
#popup > .popup-content { #popup > .popup-content {
max-width: calc(80rem); width: 75rem;
height: calc(100vh - 20rem); max-width: 100vw;
margin: 10rem auto;
} }
main.popup > :not(#popup) { main.popup > :not(#popup) {
@ -170,3 +176,7 @@ button.action-close {
.codex-editor--narrow .codex-editor__redactor { .codex-editor--narrow .codex-editor__redactor {
margin-right: 0; margin-right: 0;
} }
.centered {
margin: auto inherit;
text-align: center;
}

@ -2,6 +2,7 @@
<div id="new-deck-form" class="deck"> <div id="new-deck-form" class="deck">
<header>Create a new deck of cards</header> <header>Create a new deck of cards</header>
<DeckForm :deck="newDeck" @save="saveDeck" @close="$emit('close')" /> <DeckForm :deck="newDeck" @save="saveDeck" @close="$emit('close')" />
<footer class="centered">You can also <button @click="importDeck">import</button> an existing set.</footer>
</div> </div>
</template> </template>
@ -9,7 +10,7 @@
import { Component, Emit, Vue } from 'vue-property-decorator' import { Component, Emit, Vue } from 'vue-property-decorator'
import { Deck } from '@/types' import { Deck } from '@/types'
import DeckForm from './deck-form.vue' import DeckForm from './deck-form.vue'
import { defaultDeck, randomId } from '../lib' import { defaultDeck, randomId, isValidDeck } from '../lib'
@Component({ @Component({
components: { DeckForm } components: { DeckForm }
@ -17,6 +18,37 @@ import { defaultDeck, randomId } from '../lib'
export default class NewDeckForm extends Vue { export default class NewDeckForm extends Vue {
private newDeck: Deck = defaultDeck() private newDeck: Deck = defaultDeck()
private importDeck () {
const newFileSelector = document.createElement('input')
newFileSelector.setAttribute('type', 'file')
newFileSelector.onchange = event => {
if (event === null) return
const fileList = (event.target as HTMLInputElement).files
if (fileList === null || fileList.length < 1) return
const file = fileList[0]
if (!file) return
const seemsToBeJSON = file.type === 'application/json'
// TODO: more checks?
let fileOk = seemsToBeJSON
if (!seemsToBeJSON) {
fileOk = window.confirm(`This seems to be wrong file type (${file.type}). Should be JSON. Import anyway?`)
}
if (!fileOk) return
file.text().then((text: string) => {
const json = JSON.parse(text)
if (!isValidDeck(json)) window.alert('Sorry, that did\'t seem to be a valid deck.')
else this.$emit('save', this.$storage.saveDeck(json))
})
}
newFileSelector.click()
}
@Emit('save') @Emit('save')
private saveDeck (deck: Deck) { private saveDeck (deck: Deck) {
deck.id = randomId() // just to make sure deck.id = randomId() // just to make sure

Loading…
Cancel
Save