/**
* Return a random integer between [`min`, `max`].
*
* @param {number} min - The lowest possible integer.
* @param {number} max - The highest possible integer (inclusive).
* @returns {number} A random integer.
*/
function randomInteger(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
/**
* Indents the given string
* @param {string} str The string to be indented.
* @param {number} numOfIndents The amount of indentations to place at the
* beginning of each line of the string.
* @param {number=} opt_spacesPerIndent Optional. If specified, this should be
* the number of spaces to be used for each tab that would ordinarily be
* used to indent the text. These amount of spaces will also be used to
* replace any tab characters that already exist within the string.
* @return {string} The new string with each line beginning with the desired
* amount of indentation.
*/
function indent(str, numOfIndents, opt_spacesPerIndent) {
str = str.replace(/^(?=.)/gm, new Array(numOfIndents + 1).join('\t'));
numOfIndents = new Array(opt_spacesPerIndent + 1 || 0).join(' '); // re-use
return opt_spacesPerIndent
? str.replace(/^\t+/g, function(tabs) {
return tabs.replace(/./g, numOfIndents);
})
: str;
}
/**
* Encodes some text so that it can be safely written into an HTML box.
* This includes replacing special HTML characters (&, <, >, etc.).
*
* @param {string} str - The text to be converted.
* @return {string} The HTML-safe text.
*/
function encodeHTML(str) {
return str.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/**
* Shuffle the blocks in the workspace
*/
Blockly.WorkspaceSvg.prototype.shuffle = function() {
var metrics = this.getMetrics();
var width = metrics.viewWidth / 2,
height = metrics.viewHeight;
var blocks = this.getTopBlocks(false);
var y = 5, x = 0,
maximal_increase = height/blocks.length;
for (var i = 0; i < blocks.length; i++){
// Get a block
var block = blocks[i];
var properties = block.getRelativeToSurfaceXY();
if (i == 0) {
x = 5;
} else {
x = -properties.x+randomInteger(10, width);
}
block.moveBy(x,
-properties.y+y);
y = y + randomInteger(5, maximal_increase);
}
}