-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: drag-and-drop in dedicated component
Outsource complexity from main component and prepare new component to be exposed as standalone public component.
- Loading branch information
Showing
4 changed files
with
97 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<template lang="html"> | ||
<div | ||
@drop.prevent.stop="onDrop" | ||
@dragover.prevent.stop | ||
@dragenter.prevent.stop | ||
@dragleave.prevent.stop> | ||
<slot></slot> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { scan } from '../misc/scanner.js' | ||
import { imageDataFromFile, imageDataFromUrl } from '../misc/image-data.js' | ||
import CommonAPI from '../mixins/CommonAPI.vue' | ||
export default { | ||
mixins: [ CommonAPI ], | ||
methods: { | ||
onDrop ({ dataTransfer }) { | ||
const droppedFiles = [...dataTransfer.files] | ||
const droppedUrl = dataTransfer.getData('text') | ||
droppedFiles.forEach(file => { | ||
this.onDetect(this.processFile(file)) | ||
}) | ||
if (droppedUrl !== '') { | ||
this.onDetect(this.processUrl(droppedUrl)) | ||
} | ||
}, | ||
async processFile (file) { | ||
const imageData = await imageDataFromFile(file) | ||
const scanResult = await scan(imageData) | ||
return { source: 'file', ...scanResult } | ||
}, | ||
async processUrl (url) { | ||
const imageData = await imageDataFromUrl(url) | ||
const scanResult = await scan(imageData) | ||
return { source: 'url', ...scanResult } | ||
}, | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script> | ||
export default { | ||
methods: { | ||
async onDetect (resultPromise) { | ||
this.$emit('detect', resultPromise) | ||
try { | ||
const { content } = await resultPromise | ||
if (content !== null) { | ||
this.$emit('decode', content) | ||
} | ||
} catch (error) { | ||
// fail silently | ||
} | ||
}, | ||
}, | ||
} | ||
</script> |