Fix merge conflicts, and cleanup saveFileAs code

dev
Michael Ball 2015-11-16 18:12:33 -08:00
rodzic fa6f168bc0
commit dafb6146f6
2 zmienionych plików z 28 dodań i 33 usunięć

Wyświetl plik

@ -766,16 +766,12 @@ CustomCommandBlockMorph.prototype.userMenu = function () {
menu.addItem( menu.addItem(
"script pic...", "script pic...",
function () { function () {
<<<<<<< HEAD
var ide = this.parentThatIsA(IDE_Morph); var ide = this.parentThatIsA(IDE_Morph);
ide.saveCanvasAs( ide.saveCanvasAs(
this.topBlock().fullImage(), this.topBlock().scriptPic(),
this.projectName + ' ' + localize('script pic'), this.projectName + ' ' + localize('script pic'),
true // request opening a new window true // request opening a new window
); );
=======
window.open(this.topBlock().scriptPic().toDataURL());
>>>>>>> master
}, },
'open a new window\nwith a picture of this script' 'open a new window\nwith a picture of this script'
); );

55
gui.js
Wyświetl plik

@ -3627,8 +3627,8 @@ IDE_Morph.prototype.openProject = function (name) {
} }
}; };
// Set the URL to a project's XML contents
IDE_Morph.prototype.setURL = function (str) { IDE_Morph.prototype.setURL = function (str) {
// Set the URL to a project's XML contents
if (this.projectsInURLs) { if (this.projectsInURLs) {
location.hash = str; location.hash = str;
} }
@ -3648,22 +3648,19 @@ IDE_Morph.prototype.saveFileAs = function (
- every other combo is unsupposed. - every other combo is unsupposed.
*/ */
var blobIsSupported = false, var blobIsSupported = false,
world = this.world(),
fileExt, fileExt,
dataURI, world, dlg; dataURI, dialog;
// fileType is a <kind>/<ext>;<charset> format. // fileType is a <kind>/<ext>;<charset> format.
fileExt = '.' + fileType.split('/')[1].split(';')[0] fileExt = '.' + fileType.split('/')[1].split(';')[0];
// Error Message Handling
world = this.world();
// This is a workaround for a known Chrome crash with large URLs // This is a workaround for a known Chrome crash with large URLs
function exhibitsChomeBug(contents) { function exhibitsChomeBug(contents) {
var MAX_LENGTH = 2e6, var MAX_LENGTH = 2e6,
isChrome = navigator.userAgent.indexOf('Chrome') !== -1, isChrome = navigator.userAgent.indexOf('Chrome') !== -1,
isTooLong = contents.length > MAX_LENGTH; return isChrome && contents.length > MAX_LENGTH;
}
return isChrome && isTooLong
};
function dataURItoBlob(text, mimeType) { function dataURItoBlob(text, mimeType) {
var i, var i,
@ -3678,11 +3675,10 @@ IDE_Morph.prototype.saveFileAs = function (
data[i] = text.charCodeAt(i); data[i] = text.charCodeAt(i);
} }
} }
return new Blob([data], {type: mimeType }); return new Blob([data], {type: mimeType });
} }
function dataURLFormat (text) { function dataURLFormat(text) {
var hasTypeStr = text.indexOf('data:') === 0; var hasTypeStr = text.indexOf('data:') === 0;
if (hasTypeStr) {return text; } if (hasTypeStr) {return text; }
return 'data:' + fileType + ',' + encodeURIComponent(text); return 'data:' + fileType + ',' + encodeURIComponent(text);
@ -3693,10 +3689,10 @@ IDE_Morph.prototype.saveFileAs = function (
} catch (e) {} } catch (e) {}
if (newWindow) { if (newWindow) {
dataURL = dataURLFormat(contents); dataURI = dataURLFormat(contents);
// Detect crashing errors - fallback to downloading if necessary // Detect crashing errors - fallback to downloading if necessary
if (!exhibitsChomeBug(dataURL)) { if (!exhibitsChomeBug(dataURI)) {
window.open(dataURL, fileName); window.open(dataURI, fileName);
} else { } else {
// (recursively) call this defauling newWindow to false // (recursively) call this defauling newWindow to false
this.showMessage('download to disk text'); this.showMessage('download to disk text');
@ -3710,21 +3706,24 @@ IDE_Morph.prototype.saveFileAs = function (
// false: Do not preprend a BOM to the file. // false: Do not preprend a BOM to the file.
saveAs(contents, fileName + fileExt, false); saveAs(contents, fileName + fileExt, false);
} else { } else {
// Error Case. TODO. dialog = new DialogBoxMorph();
dlg = new DialogBoxMorph(); dialog.inform(
dlg.inform('Uh oh!', 'unable to export text', world); localize('Could not export') + ' ' + fileName,
// btn = dlg.buttons.children[0]; 'unable to export text',
dlg.fixLayout(); world
dlg.drawNew(); );
dialog.fixLayout();
dialog.drawNew();
} }
} }
// This helps exporting a canvas as an image
// cavas.toBlob() is only supported in Firefox and IE, but is faster.
IDE_Morph.prototype.saveCanvasAs = function (canvas, fileName, newWindow) { IDE_Morph.prototype.saveCanvasAs = function (canvas, fileName, newWindow) {
var myself = this; // Export a Canvas object as a PNG image
if (canvas.toBlob) { // cavas.toBlob() is only supported in Firefox and IE, but is faster.
canvas.toBlob(function(blob) { var myself = this,
blobFunction = canvas.toBlob || canvas.msToBlob;
if (blobFunction) {
canvas[blobFunction](function (blob) {
this.saveFileAs(blob, 'image/png', fileName, newWindow); this.saveFileAs(blob, 'image/png', fileName, newWindow);
}); });
} else { } else {
@ -3732,9 +3731,9 @@ IDE_Morph.prototype.saveCanvasAs = function (canvas, fileName, newWindow) {
} }
} }
IDE_Morph.prototype.saveXMLAs = function(text, fileName, newWindow) { IDE_Morph.prototype.saveXMLAs = function(xml, fileName, newWindow) {
var typeTag = 'text/xml;chartset=utf-8'; // wrapper to saving XML files with a proper type tag.
this.saveFileAs(text, typeTag, fileName, newWindow); this.saveFileAs(xml, 'text/xml;chartset=utf-8', fileName, newWindow);
} }
IDE_Morph.prototype.switchToUserMode = function () { IDE_Morph.prototype.switchToUserMode = function () {