Skip to content

Commit e317dfc

Browse files
committed
Editor support for sharing with Parse per #64 (Currently Disabled)
1 parent 88d60e9 commit e317dfc

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

editor/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ <h4 class="modal-title" id="shareModalTitle">Share</h4>
222222

223223
<!-- End Snippets -->
224224

225+
<script src="//www.parsecdn.com/js/parse-1.3.1.min.js"></script>
226+
225227
<script src="/js/vendor/ace/ace.js" type="text/javascript" charset="utf-8"></script>
226228
<script src="/js/vendor/popcorn-complete.min.js" type="text/javascript" charset="utf-8"></script>
227229

js/editor.js

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ var helloEditor = {
204204
$("#shareButton").click(function () {
205205
helloEditor.confirmExit = false;
206206
helloEditor.createGist();
207+
//helloEditor.addToGallery();
207208
});
208209

209210
$("#resetExample").click(function () {
@@ -620,38 +621,91 @@ var helloEditor = {
620621
$('#errorModalText').html(outputMessage);
621622
$('#errorModal').modal('show');
622623
},
624+
623625
/**
624-
* Creates a new Gist with editor contents and shows share modal
626+
* Adds to the gallery hosted on Parse
627+
*/
628+
addToGallery: function() {
629+
630+
// Post to Parse
631+
632+
Parse.initialize("x8FmMInL8BbVeBqonPzgvS8WNKbPro65Li5DzTI0", "Y7PPNnhLPhCdFMAKgw7amBxGerz67gAnG3UKb53s");
633+
634+
var processingSource = this.editor.getValue();
635+
if (!(/size\(\s*\d+\s*,\s*\d+\s*\)/.test(processingSource))) {
636+
processingSource = "size(500,400);\n\n" + processingSource;
637+
}
638+
639+
var processingCanvas = document.getElementById("editorCanvas"),
640+
uri = processingCanvas.toDataURL('image/jpeg'),
641+
base64 = uri.slice(uri.indexOf(',') + 1),
642+
processingImage = new Parse.File("sketch.jpg", { base64: base64 });
643+
644+
var galleryData = {
645+
source: processingSource
646+
};
647+
648+
processingImage.save().then(function() {
649+
650+
var gallery = new Parse.Object("Gallery");
651+
gallery.set("source", processingSource);
652+
gallery.set("image", processingImage);
653+
gallery.save(galleryData).then(function(object) {
654+
var displayURL = "http://" + $(location).attr('hostname') + (($(location).attr('port') !== "") ? ":" + $(location).attr('port') : "") + "/display/#@" + object.id;
655+
helloEditor.showShare(displayURL);
656+
});
657+
658+
659+
}, function(error) {
660+
console.log("Error saving gallery image");
661+
});
662+
663+
// var GalleryObject = Parse.Object.extend("Gallery");
664+
// var gallery = new GalleryObject();
665+
// gallery.save(galleryData).then(function(object) {
666+
// var displayURL = "http://" + $(location).attr('hostname') + (($(location).attr('port') !== "") ? ":" + $(location).attr('port') : "") + "/display/#@" + object.id;
667+
// helloEditor.showShare(displayURL);
668+
// });
669+
670+
671+
},
672+
673+
/**
674+
* Creates a new Gist with editor contents and shows the share modal
625675
*/
626676
createGist: function () {
627677

628678
var processingSource = this.editor.getValue(),
629679
postURL = "https://api.github.com/gists",
630-
postData = {
680+
postData;
681+
682+
if (!(/size\(\s*\d+\s*,\s*\d+\s*\)/.test(processingSource))) {
683+
processingSource = "size(500,400);\n\n" + processingSource;
684+
}
685+
686+
postData = {
631687
"description": "Save for Processing Hour of Code",
632688
"public": true,
633689
"files": {
634690
"demo.pde": {
635691
"content": processingSource
636692
}
637693
}
638-
};
639-
640-
if (!(/size\(\s*\d+\s*,\s*\d+\s*\)/.test(processingSource))) {
641-
processingSource = "size(500,400);\n\n" + processingSource;
642-
}
694+
};
643695

644696
$.post(postURL, JSON.stringify(postData))
645697
.done(function (data) {
646-
var gistID = data.id,
647-
displayURL = "http://" + $(location).attr('hostname') + (($(location).attr('port') !== "") ? ":" + $(location).attr('port') : "") + "/display/#" + gistID;
698+
var displayURL = "http://" + $(location).attr('hostname') + (($(location).attr('port') !== "") ? ":" + $(location).attr('port') : "") + "/display/#" + data.id;
699+
helloEditor.showShare(displayURL);
700+
});
648701

649-
$('#shareModal').attr('data-url', displayURL);
702+
},
703+
showShare: function(displayURL) {
650704

651-
$('#shareModalLink').html($("<a/>").attr({'href': displayURL, target: "_blank"}).html(displayURL));
652-
$('#shareModal').modal('show');
705+
$('#shareModal').attr('data-url', displayURL);
653706

654-
});
707+
$('#shareModalLink').html($("<a/>").attr({'href': displayURL, target: "_blank"}).html(displayURL));
708+
$('#shareModal').modal('show');
655709

656710
},
657711
showRulers: function () {

0 commit comments

Comments
 (0)