From 83a681143ec87be14df87faa7e0a5dac7ea9fce0 Mon Sep 17 00:00:00 2001 From: Markus Bordihn Date: Sat, 4 Feb 2017 19:11:15 +0100 Subject: [PATCH] Removed modal patch for Blockly and used native version from https://github.com/google/blockly/pull/703 . --- app/manifest.json | 2 +- build/external/extra_files.js | 1 - package.json | 2 +- patches/blockly/modal_support_patch.js | 180 ------------------------- src/ui/blockly/blockly.js | 29 +--- src/utils/dialog/dialog.js | 25 ++++ src/utils/dialog/dialog.soy | 23 ++++ third_party/blockly | 2 +- 8 files changed, 57 insertions(+), 207 deletions(-) delete mode 100644 patches/blockly/modal_support_patch.js diff --git a/app/manifest.json b/app/manifest.json index c0d8e95f8..4765d71cc 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -9,7 +9,7 @@ "short_name": "Coding with Chrome", "description": "Learn, improve, or teach coding skills within a Chrome browser.", "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgSWdCJSwkgFTY9d993Quot277oCS2PCM2H/F6U28NE8D3Z21GXS/7qW8XCGShB054Wboq62pP679f9tB9F+GRHzOh/an4zIu+Uhb3ZKVzZLJbHsP8N+E5wk3wgwOBgE+UXCy9I8i1RpfzutJ4aha+YEzvCsVpo6UwsuWiXORB9yRU5NuooYX+fhVpWZmrZJo4vqzQhuoJ/uWiQujABXXo4qdrclwSw8+JlpWLMqHHQ6HE8Sf7VWDdGvRdYP6LRZobWqovb659qEumJ0bxhwXtHkWgPHQWtW35T/r/oH8uuke35pH+YtBtaOqN5U3bYGPinP0bYrI33OSLUJx/DrC1wIDAQAB", - "version": "4.1.5", + "version": "4.2.4", "oauth2": { "client_id": "418162477970-2qmsb2sp3m522hf292154favsjgpsj9n.apps.googleusercontent.com", "scopes": [ diff --git a/build/external/extra_files.js b/build/external/extra_files.js index 5442d1860..692d6be3f 100755 --- a/build/external/extra_files.js +++ b/build/external/extra_files.js @@ -129,7 +129,6 @@ closureBuilder.build({ BlocklyPath + 'blocks_compressed.js', BlocklyPath + 'javascript_compressed.js', 'patches/blockly/audio_preload_patch.js', - 'patches/blockly/modal_support_patch.js' ], out: 'genfiles/external/blockly/' }); diff --git a/package.json b/package.json index 5067a0c5a..1522cd3a0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "coding-with-chrome", "description": "Coding with Chrome", - "version": "4.1.5", + "version": "4.2.4", "author": "Markus Bordihn (mbordihn@google.com)", "license": "Apache-2.0", "private": true, diff --git a/patches/blockly/modal_support_patch.js b/patches/blockly/modal_support_patch.js deleted file mode 100644 index f66e0595f..000000000 --- a/patches/blockly/modal_support_patch.js +++ /dev/null @@ -1,180 +0,0 @@ -/** - * @fileoverview Modal support patch for Blockly. - * - * @license Copyright 2016 The Coding with Chrome Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * @author mbordihn@google.com (Markus Bordihn) - */ - - - -/** - * Return a sorted list of variable names for variable dropdown menus. - * Include a special option at the end for creating a new variable name. - * @return {!Array.} Array of variable names. - * @this {!Blockly.FieldVariable} - */ -Blockly.FieldVariable.dropdownCreate = function() { - if (this.sourceBlock_ && this.sourceBlock_.workspace) { - // Get a copy of the list, so that adding rename and new variable options - // doesn't modify the workspace's list. - var variableList = this.sourceBlock_.workspace.variableList.slice(0); - } else { - var variableList = []; - } - // Ensure that the currently selected variable is an option. - var name = this.getText(); - if (name && variableList.indexOf(name) == -1) { - variableList.push(name); - } - variableList.sort(goog.string.caseInsensitiveCompare); - if (typeof window.prompt !== 'undefined' || - (workspace && workspace.options.modalOptions.prompt)) { - variableList.push(Blockly.Msg.RENAME_VARIABLE); - } - variableList.push(Blockly.Msg.DELETE_VARIABLE.replace('%1', name)); - // Variables are not language-specific, use the name as both the user-facing - // text and the internal representation. - var options = []; - for (var x = 0; x < variableList.length; x++) { - options[x] = [variableList[x], variableList[x]]; - } - return options; -}; - - -/** - * Event handler for a change in variable name. - * Special case the 'New variable...' and 'Rename variable...' options. - * In both of these special cases, prompt the user for a new name. - * @param {string} text The selected dropdown menu option. - * @return {null|undefined|string} An acceptable new variable name, or null if - * change is to be either aborted (cancel button) or has been already - * handled (rename), or undefined if an existing variable was chosen. - */ -Blockly.FieldVariable.prototype.classValidator = function(text) { - var workspace = this.sourceBlock_.workspace; - var newVarName = Blockly.Variables.generateUniqueName(workspace); - var oldVar = this.getText(); - var callbackFunc = function(variable_name) { - Blockly.hideChaff(); - if (!variable_name) { - return; - } - // Merge runs of whitespace. Strip leading and trailing whitespace. - // Beyond this, all names are legal. - variable_name = variable_name - .replace(/[\s\xa0]+/g, ' ') - .replace(/^ | $/g, ''); - if (!variable_name || - variable_name == Blockly.Msg.RENAME_VARIABLE || - variable_name == Blockly.Msg.NEW_VARIABLE || - variable_name == Blockly.Msg.DELETE_VARIABLE) { - // Ok, not ALL names are legal... - return; - } - - if (text == Blockly.Msg.RENAME_VARIABLE) { - workspace.renameVariable(oldVar, variable_name, workspace); - } else if (text == Blockly.Msg.NEW_VARIABLE) { - workspace.renameVariable(newVarName, variable_name, workspace); - } - }; - - var promptDialog = workspace.options.modalOptions.prompt; - if (text == Blockly.Msg.RENAME_VARIABLE) { - promptDialog(Blockly.Msg.RENAME_VARIABLE_TITLE.replace('%1', oldVar), - oldVar, callbackFunc, text); - return null; - } else if (text == Blockly.Msg.NEW_VARIABLE) { - window.setTimeout(function() { - promptDialog(Blockly.Msg.NEW_VARIABLE_TITLE, newVarName, callbackFunc, - text); - }, 100); - return newVarName; - } else if (text == Blockly.Msg.DELETE_VARIABLE.replace('%1', oldVar)) { - workspace.deleteVariable(oldVar, workspace); - return null; - } - return undefined; -}; - - -/** - * Create a new variable on the given workspace. - * @param {!Blockly.Workspace} workspace The workspace on which to create the - * variable. - * @return {null|undefined|string} An acceptable new variable name, or null if - * change is to be aborted (cancel button), or undefined if an existing - * variable was chosen. - */ -Blockly.Variables.createVariable = function(workspace) { - var promptDialog = workspace.options.modalOptions.prompt; - - var callbackFunc = function(text) { - if (text && workspace.variableIndexOf(text) === -1) { - workspace.createVariable(text); - } - }; - - promptDialog(Blockly.Msg.NEW_VARIABLE_TITLE, '', callbackFunc); -}; - - -/** - * Delete a variables and all of its uses from this workspace. - * @param {string} name Name of variable to delete. - */ -Blockly.Workspace.prototype.deleteVariable = function(name, workspace) { - var variableIndex = this.variableIndexOf(name); - if (variableIndex != -1) { - var uses = this.getVariableUses(name); - if (uses.length > 1) { - for (var i = 0, block; block = uses[i]; i++) { - if (block.type == 'procedures_defnoreturn' || - block.type == 'procedures_defreturn') { - var procedureName = block.getFieldValue('NAME'); - var alertDialog = workspace.options.modalOptions.alert; - alertDialog( - Blockly.Msg.CANNOT_DELETE_VARIABLE_PROCEDURE.replace('%1', name). - replace('%2', procedureName)); - return; - } - } - var confirmDialog = workspace.options.modalOptions.confirm; - var confirmCallback = function(del) { - if (!del) { - return; - } - Blockly.Events.setGroup(true); - for (var i = 0; i < uses.length; i++) { - uses[i].dispose(true, false); - } - Blockly.Events.setGroup(false); - this.variableList.splice(variableIndex, 1); - }; - confirmDialog(Blockly.Msg.DELETE_VARIABLE_CONFIRMATION - .replace('%1', uses.length) - .replace('%2', name), confirmCallback.bind(this)); - } else { - Blockly.Events.setGroup(true); - for (var i = 0; i < uses.length; i++) { - uses[i].dispose(true, false); - } - Blockly.Events.setGroup(false); - this.variableList.splice(variableIndex, 1); - } - } -}; diff --git a/src/ui/blockly/blockly.js b/src/ui/blockly/blockly.js index 27eeb1a36..d6dd50f8f 100644 --- a/src/ui/blockly/blockly.js +++ b/src/ui/blockly/blockly.js @@ -81,15 +81,6 @@ cwc.ui.Blockly = function(helper) { /** @type {cwc.ui.BlocklyToolbar} */ this.toolbar = null; - /** @type {Function} */ - this.modalAlert = null; - - /** @type {Function} */ - this.modalConfirm = null; - - /** @type {Function} */ - this.modalPrompt = null; - /** @type {Blockly.Workspace} */ this.workspace = null; @@ -135,16 +126,15 @@ cwc.ui.Blockly.prototype.decorate = function(node, opt_toolbox, opt_prefix, // Modal window var dialogInstance = this.helper.getInstance('dialog'); if (dialogInstance) { - this.modalAlert = function(promptText, opt_title) { - dialogInstance.showContent(opt_title || 'Blockly', promptText); + Blockly['alert'] = function(message, callback) { + dialogInstance.showAlert('Blockly alert', message).then(callback); }; - this.modalConfirm = function(promptText, callback, opt_title) { - dialogInstance.showYesNo( - opt_title || 'Blockly', promptText).then(callback); + Blockly['confirm'] = function(message, callback) { + dialogInstance.showYesNo('Blockly confirm', message).then(callback); }; - this.modalPrompt = function(promptText, defaultText, callback, opt_title) { + Blockly['prompt'] = function(message, default_value, callback) { dialogInstance.showPrompt( - opt_title || 'Blockly', promptText, defaultText).then(callback); + 'Blockly prompt', message, default_value).then(callback); }; } @@ -175,13 +165,6 @@ cwc.ui.Blockly.prototype.decorate = function(node, opt_toolbox, opt_prefix, this.log.info('Decorating Blockly node', this.nodeEditor, 'with', options); this.workspace = Blockly.inject(this.nodeEditor, options); - // Adding Modal support - this.setWorkspaceOption('modalOptions', { - 'alert': this.modalAlert, - 'confirm': this.modalConfirm, - 'prompt': this.modalPrompt - }); - // Monitor changes var viewportMonitor = new goog.dom.ViewportSizeMonitor(); if (viewportMonitor) { diff --git a/src/utils/dialog/dialog.js b/src/utils/dialog/dialog.js index d2577cb8e..93ba617ca 100644 --- a/src/utils/dialog/dialog.js +++ b/src/utils/dialog/dialog.js @@ -220,6 +220,31 @@ cwc.utils.Dialog.prototype.setButtonText = function(id, text) { }; +/** + * @param {!string} title + * @param {!string} content + * @export + */ +cwc.utils.Dialog.prototype.showAlert = function(title, content) { + return new Promise((resolve, reject) => { + if (this.getDialog_()) { + this.render(title, content, cwc.soy.Dialog.alertTemplate); + var okButton = goog.dom.getElement(this.prefix + 'ok'); + okButton.addEventListener('click', this.close.bind(this)); + if (this.defaultCloseHandler_) { + okButton.addEventListener('click', this.defaultCloseHandler_); + } + okButton.addEventListener('click', function() { + resolve(true); + }); + this.showModal(); + } else { + reject(); + } + }); +}; + + /** * @param {!string} title * @param {!string} content diff --git a/src/utils/dialog/dialog.soy b/src/utils/dialog/dialog.soy index 436cc039c..339ed113d 100644 --- a/src/utils/dialog/dialog.soy +++ b/src/utils/dialog/dialog.soy @@ -51,6 +51,29 @@ {/template} + +/** + * Alert template. + */ +{template .alertTemplate} + {@param prefix: string} + {@param title: string} + {@param content: string} + +

+ {msg desc=""}{$title}{/msg} +

+
+ {$content} +
+
+ +
+{/template} + + /** * Yes / no template. */ diff --git a/third_party/blockly b/third_party/blockly index a2a4d4194..d15d3ab9e 160000 --- a/third_party/blockly +++ b/third_party/blockly @@ -1 +1 @@ -Subproject commit a2a4d4194df43e81407df65d6b0c09c6ad290d29 +Subproject commit d15d3ab9e675dbea1787e0fe30956f14a4ee2245