diff --git a/web/blocks.html b/web/blocks.html index 0997608bd..5a450bfca 100644 --- a/web/blocks.html +++ b/web/blocks.html @@ -130,7 +130,7 @@ >  Share Folder - - Please keep in mind that you are about to share code that contains errors. You can see what those errors are by actually running the program.

', + }; + + function constructMessage(message, withErrors) { + return `${withErrors ? messages.errorsDetected : ''}${message}`; + } + let offerSource = true; - function go() { + function showDialog(hash, deployHash) { + const { origin } = window.location; + let url; - let msg; + let message; let showConfirm; let confirmText; + let withErrors = false; - if (!window.deployHash) { - url = window.location.href; - msg = 'Copy this link to share your program and code with others!'; - showConfirm = false; - } else if (offerSource) { - url = window.location.href; - msg = 'Copy this link to share your program and code with others!'; - showConfirm = true; - confirmText = 'Share Without Code'; - } else { - const a = document.createElement('a'); - a.href = `run.html?mode=${window.buildMode}&dhash=${window.deployHash}`; - - url = a.href; - msg = 'Copy this link to share your program (but not code) with others!'; - showConfirm = true; - confirmText = 'Share With Code'; - } + sendHttp('POST', 'errorCheck', data, ({ status }) => { + if (status === 400) { + withErrors = true; + } - sweetAlert({ - title: Alert.title('Share', 'mdi-share'), - html: msg, - input: 'text', - inputValue: url, - showConfirmButton: showConfirm, - confirmButtonText: confirmText, - closeOnConfirm: false, - showCancelButton: true, - cancelButtonText: 'Done', - animation: 'slide-from-bottom', - }).then((result) => { - if (result && result.value) { - offerSource = !offerSource; - go(); + const constructErrorAwareMessage = partialRight(constructMessage, [ + withErrors, + ]); + + if (!deployHash) { + url = `${origin}/#${hash}`; + message = constructErrorAwareMessage(messages.includeCode); + showConfirm = false; + } else if (offerSource) { + url = `${origin}/#${hash}`; + message = constructErrorAwareMessage(messages.includeCode); + showConfirm = true; + confirmText = 'Share Without Code'; + } else { + url = `${origin}/run.html?mode=${window.buildMode}&dhash=${deployHash}`; + message = constructErrorAwareMessage(messages.noCode); + showConfirm = true; + confirmText = 'Share With Code'; } - }); - } - if (window.runningGeneration) { - if (!window.codeworldEditor.getDoc().isClean(window.runningGeneration)) { sweetAlert({ - type: 'warning', - text: - 'You have changed your code since running the program. ' + - ' Rebuild so that you can share your latest code?', - confirmButtonText: 'Yes, Rebuild', - cancelButtonText: 'No, Share Old Program', - showConfirmButton: true, + title: Alert.title('Share', 'mdi-share'), + html: message, + input: 'text', + inputValue: url, + showConfirmButton: showConfirm, + confirmButtonText: confirmText, + closeOnConfirm: false, showCancelButton: true, + cancelButtonText: 'Done', + animation: 'slide-from-bottom', }).then((result) => { if (result && result.value) { - compile(); - } else { - go(); + offerSource = !offerSource; + showDialog(hash, deployHash); } }); - return; - } + }); } - go(); + function storeCode(callback) { + sendHttp('POST', 'compile', data, (request) => { + let hash; + let deployHash; + + if (request.status < 500) { + if (request.responseText.length === 23) { + hash = request.responseText; + deployHash = null; + } else { + try { + const obj = JSON.parse(request.responseText); + hash = obj.hash; + deployHash = obj.dhash; + } catch (e) { + hash = ''; + } + } + } + + if (!hash) { + sweetAlert({ + title: Alert.title('Could not share'), + text: 'Sharing is currently unavailable. Please try again later.', + type: 'error', + }); + return; + } + + callback(hash, deployHash); + }); + } + + if (hash && deployHash) { + showDialog(hash, deployHash); + } else { + storeCode(showDialog); + } } function shareFolder_(mode) { @@ -1966,10 +2010,8 @@ function run(hash, dhash, msg, error, generation) { if (hash) { window.location.hash = `#${hash}`; - document.getElementById('shareButton').style.display = ''; } else { window.location.hash = ''; - document.getElementById('shareButton').style.display = 'none'; } if (dhash) { diff --git a/web/js/funblocks.js b/web/js/funblocks.js index d3a752c71..1398d055b 100644 --- a/web/js/funblocks.js +++ b/web/js/funblocks.js @@ -289,14 +289,10 @@ function run(xmlHash, codeHash, msg, error, dhash) { if (hash) { window.location.hash = `#${xmlHash}`; - document.getElementById('shareButton').style.display = ''; - $shareFolderButton.hide(); } else { window.location.hash = ''; - document.getElementById('shareButton').style.display = 'none'; - $shareFolderButton.show(); } diff --git a/web/js/utils/functional.js b/web/js/utils/functional.js new file mode 100644 index 000000000..4ae0f9045 --- /dev/null +++ b/web/js/utils/functional.js @@ -0,0 +1,4 @@ +const partialRight = (fn, presetArgs) => (...laterArgs) => + fn(...laterArgs, ...presetArgs); + +export { partialRight };