Skip to content

Commit 04a7ce1

Browse files
authored
custom sprite image upload; closes #498
1 parent 2605cd1 commit 04a7ce1

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

create.html

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,7 @@ <h4 class="modal-body text-danger" :style="{display: !getID() ? 'block' : 'none'
13611361

13621362
firebase.database().ref().update(updates).then(function(){
13631363
window.location.hash = name
1364+
setUserUploadImageListener() // sync user-uploaded photos for this project name
13641365
myCode = firebase.auth().currentUser
13651366
makeClean()
13661367
}).catch(function(error){
@@ -1602,7 +1603,67 @@ <h4 class="modal-body text-danger" :style="{display: !getID() ? 'block' : 'none'
16021603
}
16031604
}, 10)
16041605
}
1605-
1606+
1607+
window.onmessage = function(e){
1608+
if (!(e.data instanceof File)){
1609+
return
1610+
}
1611+
if (!firebase.auth().currentUser) {
1612+
alert('Please login to upload images.')
1613+
return
1614+
}
1615+
if (!getID()) {
1616+
alert('Please save your project to upload images.')
1617+
return
1618+
}
1619+
if (!myCode) {
1620+
alert('Please save this project to your account to upload images.')
1621+
}
1622+
1623+
var uniqueId = Math.random().toString(36).substring(2)
1624+
+ (new Date()).getTime().toString(36);
1625+
1626+
var ref = firebase.storage().ref()
1627+
.child('UserImageUpload')
1628+
.child(firebase.auth().currentUser.uid)
1629+
.child(getID())
1630+
.child(uniqueId)
1631+
1632+
var file = e.data
1633+
ref.put(file)
1634+
.then(function(snapshot){
1635+
if (getID()) {
1636+
firebase.database().ref()
1637+
.child('code-image-uploads')
1638+
.child(getID())
1639+
.child(uniqueId)
1640+
.set(snapshot.downloadURL)
1641+
}
1642+
}).catch(function(error){
1643+
alert('There was an error uploading your image: ' + error.message)
1644+
})
1645+
};
1646+
1647+
var ref
1648+
setUserUploadImageListener = function() {
1649+
ref && ref.off()
1650+
ref = firebase.database().ref()
1651+
.child('code-image-uploads')
1652+
.child(getID())
1653+
ref.on('value', function(snapshot) {
1654+
var addImagesFunc = () => {
1655+
var images = snapshot.val() || {}
1656+
scratch.contentWindow.app.setUserUploadImages(Object.values(images))
1657+
}
1658+
if (scratch.contentWindow.app){
1659+
addImagesFunc()
1660+
} else {
1661+
scratch.contentDocument.addEventListener('DOMContentLoaded', addImagesFunc, false);
1662+
}
1663+
})
1664+
}
1665+
getID() && setUserUploadImageListener()
1666+
16061667
function debounce(func, wait, immediate) {
16071668
var timeout;
16081669
return function() {

docs/index.html

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@
6868
e.target.parentElement.nextElementSibling.childNodes[2].childNodes[0].innerHTML = e.target.value
6969
e.target.parentElement.nextElementSibling.childNodes[0].setAttribute("data-clipboard-text", e.target.value)
7070
}
71+
72+
var browseImages = function() {
73+
document.getElementById("uploadInput").click()
74+
}
75+
76+
var uploadFile = function(files) {
77+
if (files.length === 1) {
78+
window.top.postMessage(files[0], '*')
79+
}
80+
}
7181

7282
Vue.component('vue-block', {
7383
template: "#block-template",
@@ -472,7 +482,11 @@
472482
}
473483
],
474484
Image: [
475-
485+
{
486+
html: '<button class="btn btn-medium btn-info" onclick="browseImages()">Upload Image</button>' +
487+
'<input id="uploadInput" onchange="uploadFile(this.files)" style="display:none" type="file" name="pic" accept="image/*">',
488+
tags: "picture image custom external upload sprite"
489+
},
476490
{
477491
url: "./images/kpU9y5M.png",
478492
code: 'var unicorn = new Image({\n url: "./docs/images/unicorn.png",\n width: 120, \n height: 80,\n})',
@@ -2077,6 +2091,21 @@
20772091
},
20782092
},
20792093
methods: {
2094+
setUserUploadImages: function(images) {
2095+
var newImages = this.menus['Sprites & Backgrounds']['Image'].filter(
2096+
block => !block.userUploadImage
2097+
)
2098+
images.reverse() // show the newest first
2099+
newImages.splice(1, 0, ...images.map(imageURL => {
2100+
return {
2101+
url: imageURL,
2102+
code: 'var sprite1 = new Image({url: "' + imageURL + '"})',
2103+
tags: "upload image picture",
2104+
userUploadImage: true
2105+
}
2106+
}))
2107+
this.menus['Sprites & Backgrounds']['Image'] = newImages
2108+
},
20802109
searchMenu: function(menu) {
20812110
if (this.search == '') {
20822111
return menu

0 commit comments

Comments
 (0)