Skip to content

Commit

Permalink
Add prefill options
Browse files Browse the repository at this point in the history
  • Loading branch information
indr committed Sep 11, 2017
1 parent b5e7da0 commit 3caf634
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.idea/
dev/
node_modules/
dist/
Expand Down
19 changes: 12 additions & 7 deletions PictureInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export default {
type: [String, File],
default: ''
},
prefillOptions: {
type: Object,
default: {}
},
crop: {
type: Boolean,
default: true
Expand Down Expand Up @@ -126,7 +130,7 @@ export default {
watch: {
prefill () {
if (this.prefill) {
this.preloadImage(this.prefill)
this.preloadImage(this.prefill, this.prefillOptions)
} else {
this.removeImage()
}
Expand Down Expand Up @@ -157,7 +161,7 @@ export default {
mounted () {
this.updateStrings()
if (this.prefill) {
this.preloadImage(this.prefill)
this.preloadImage(this.prefill, this.prefillOptions)
}
this.$nextTick(() => {
Expand Down Expand Up @@ -425,7 +429,8 @@ export default {
}
reader.readAsArrayBuffer(file.slice(0, 65536))
},
preloadImage (source) {
preloadImage (source, options) {
options = Object.assign({}, options)
if (typeof source === 'object') {
this.imageSelected = true
this.image = ''
Expand All @@ -447,10 +452,10 @@ export default {
})
.then(imageBlob => {
let e = { target: { files: [] } }
const fileName = source.split('/').slice(-1)[0]
let fileType = fileName.split('.').slice(-1)[0]
fileType = fileType.replace('jpg', 'jpeg')
e.target.files[0] = new File([imageBlob], fileName, { type: 'image/' + fileType })
const fileName = options.fileName || source.split('/').slice(-1)[0]
let mediaType = options.mediaType || ('image/' + options.fileType || fileName.split('.').slice(-1)[0])
mediaType = mediaType.replace('jpg', 'jpeg')
e.target.files[0] = new File([imageBlob], fileName, { type: mediaType })
this.onFileChange(e)
})
.catch(err => {
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ You can find an example project here: https://github.com/alessiomaffeis/vue-pict
- **removeButtonClass**: (string, optional) the class which will be applied to the 'Remove Photo' button.
Default value: 'btn btn-secondary button secondary'.
- **prefill**: (image url or File object, optional) use this to specify the path to a default image (or a File object) to prefill the input with. Default value: empty.
- **prefillOptions**: (object, optional) use this if you prefill with a data uri scheme to specify a file name and a media or file type:
```
fileName: (string, optional) the file name
fileType: (string, optional) the file type of the image, i.e. "png", or
mediaType: (string, optional) the media type of the image, i.e. "image/png"
```
- **toggleAspectRatio**: (boolean, optional) set *:toggleAspectRatio="true"* to show a button for toggling the canvas aspect ratio (Landscape/Portrait) on a non-square canvas. Default value: false.
- **autoToggleAspectRatio**: (boolean, optional) set *:autoToggleAspectRatio="true"* to enable automatic canvas aspect ratio change to match the selected picture's. Default value: false.
- **aspectButtonClass**: (string, optional) the class which will be applied to the 'Landscape/Portrait' button.
Expand Down

0 comments on commit 3caf634

Please sign in to comment.