text.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating JavaScript for text blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.JavaScript.texts');
  26. goog.require('Blockly.JavaScript');
  27. Blockly.JavaScript['text'] = function(block) {
  28. // Text value.
  29. var code = Blockly.JavaScript.quote_(block.getFieldValue('TEXT'));
  30. return [code, Blockly.JavaScript.ORDER_ATOMIC];
  31. };
  32. Blockly.JavaScript['text_join'] = function(block) {
  33. // Create a string made up of any number of elements of any type.
  34. var code;
  35. if (block.itemCount_ == 0) {
  36. return ['\'\'', Blockly.JavaScript.ORDER_ATOMIC];
  37. } else if (block.itemCount_ == 1) {
  38. var argument0 = Blockly.JavaScript.valueToCode(block, 'ADD0',
  39. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  40. code = 'String(' + argument0 + ')';
  41. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  42. } else if (block.itemCount_ == 2) {
  43. var argument0 = Blockly.JavaScript.valueToCode(block, 'ADD0',
  44. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  45. var argument1 = Blockly.JavaScript.valueToCode(block, 'ADD1',
  46. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  47. code = 'String(' + argument0 + ') + String(' + argument1 + ')';
  48. return [code, Blockly.JavaScript.ORDER_ADDITION];
  49. } else {
  50. code = new Array(block.itemCount_);
  51. for (var n = 0; n < block.itemCount_; n++) {
  52. code[n] = Blockly.JavaScript.valueToCode(block, 'ADD' + n,
  53. Blockly.JavaScript.ORDER_COMMA) || '\'\'';
  54. }
  55. code = '[' + code.join(',') + '].join(\'\')';
  56. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  57. }
  58. };
  59. Blockly.JavaScript['text_append'] = function(block) {
  60. // Append to a variable in place.
  61. var varName = Blockly.JavaScript.variableDB_.getName(
  62. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  63. var argument0 = Blockly.JavaScript.valueToCode(block, 'TEXT',
  64. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  65. return varName + ' = String(' + varName + ') + String(' + argument0 + ');\n';
  66. };
  67. Blockly.JavaScript['text_length'] = function(block) {
  68. // String or array length.
  69. var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
  70. Blockly.JavaScript.ORDER_FUNCTION_CALL) || '\'\'';
  71. return [argument0 + '.length', Blockly.JavaScript.ORDER_MEMBER];
  72. };
  73. Blockly.JavaScript['text_isEmpty'] = function(block) {
  74. // Is the string null or array empty?
  75. var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
  76. Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
  77. return ['!' + argument0 + '.length', Blockly.JavaScript.ORDER_LOGICAL_NOT];
  78. };
  79. Blockly.JavaScript['text_indexOf'] = function(block) {
  80. // Search the text for a substring.
  81. var operator = block.getFieldValue('END') == 'FIRST' ?
  82. 'indexOf' : 'lastIndexOf';
  83. var argument0 = Blockly.JavaScript.valueToCode(block, 'FIND',
  84. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  85. var argument1 = Blockly.JavaScript.valueToCode(block, 'VALUE',
  86. Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
  87. var code = argument1 + '.' + operator + '(' + argument0 + ') + 1';
  88. return [code, Blockly.JavaScript.ORDER_MEMBER];
  89. };
  90. Blockly.JavaScript['text_charAt'] = function(block) {
  91. // Get letter at index.
  92. // Note: Until January 2013 this block did not have the WHERE input.
  93. var where = block.getFieldValue('WHERE') || 'FROM_START';
  94. var at = Blockly.JavaScript.valueToCode(block, 'AT',
  95. Blockly.JavaScript.ORDER_UNARY_NEGATION) || '1';
  96. var text = Blockly.JavaScript.valueToCode(block, 'VALUE',
  97. Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
  98. switch (where) {
  99. case 'FIRST':
  100. var code = text + '.charAt(0)';
  101. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  102. case 'LAST':
  103. var code = text + '.slice(-1)';
  104. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  105. case 'FROM_START':
  106. // Blockly uses one-based indicies.
  107. if (Blockly.isNumber(at)) {
  108. // If the index is a naked number, decrement it right now.
  109. at = parseFloat(at) - 1;
  110. } else {
  111. // If the index is dynamic, decrement it in code.
  112. at += ' - 1';
  113. }
  114. var code = text + '.charAt(' + at + ')';
  115. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  116. case 'FROM_END':
  117. var code = text + '.slice(-' + at + ').charAt(0)';
  118. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  119. case 'RANDOM':
  120. var functionName = Blockly.JavaScript.provideFunction_(
  121. 'text_random_letter',
  122. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
  123. '(text) {',
  124. ' var x = Math.floor(Math.random() * text.length);',
  125. ' return text[x];',
  126. '}']);
  127. code = functionName + '(' + text + ')';
  128. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  129. }
  130. throw 'Unhandled option (text_charAt).';
  131. };
  132. Blockly.JavaScript['text_getSubstring'] = function(block) {
  133. // Get substring.
  134. var text = Blockly.JavaScript.valueToCode(block, 'STRING',
  135. Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
  136. var where1 = block.getFieldValue('WHERE1');
  137. var where2 = block.getFieldValue('WHERE2');
  138. var at1 = Blockly.JavaScript.valueToCode(block, 'AT1',
  139. Blockly.JavaScript.ORDER_NONE) || '1';
  140. var at2 = Blockly.JavaScript.valueToCode(block, 'AT2',
  141. Blockly.JavaScript.ORDER_NONE) || '1';
  142. if (where1 == 'FIRST' && where2 == 'LAST') {
  143. var code = text;
  144. } else {
  145. var functionName = Blockly.JavaScript.provideFunction_(
  146. 'text_get_substring',
  147. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
  148. '(text, where1, at1, where2, at2) {',
  149. ' function getAt(where, at) {',
  150. ' if (where == \'FROM_START\') {',
  151. ' at--;',
  152. ' } else if (where == \'FROM_END\') {',
  153. ' at = text.length - at;',
  154. ' } else if (where == \'FIRST\') {',
  155. ' at = 0;',
  156. ' } else if (where == \'LAST\') {',
  157. ' at = text.length - 1;',
  158. ' } else {',
  159. ' throw \'Unhandled option (text_getSubstring).\';',
  160. ' }',
  161. ' return at;',
  162. ' }',
  163. ' at1 = getAt(where1, at1);',
  164. ' at2 = getAt(where2, at2) + 1;',
  165. ' return text.slice(at1, at2);',
  166. '}']);
  167. var code = functionName + '(' + text + ', \'' +
  168. where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
  169. }
  170. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  171. };
  172. Blockly.JavaScript['text_changeCase'] = function(block) {
  173. // Change capitalization.
  174. var OPERATORS = {
  175. 'UPPERCASE': '.toUpperCase()',
  176. 'LOWERCASE': '.toLowerCase()',
  177. 'TITLECASE': null
  178. };
  179. var operator = OPERATORS[block.getFieldValue('CASE')];
  180. var code;
  181. if (operator) {
  182. // Upper and lower case are functions built into JavaScript.
  183. var argument0 = Blockly.JavaScript.valueToCode(block, 'TEXT',
  184. Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
  185. code = argument0 + operator;
  186. } else {
  187. // Title case is not a native JavaScript function. Define one.
  188. var functionName = Blockly.JavaScript.provideFunction_(
  189. 'text_toTitleCase',
  190. [ 'function ' +
  191. Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(str) {',
  192. ' return str.replace(/\\S+/g,',
  193. ' function(txt) {return txt[0].toUpperCase() + ' +
  194. 'txt.substring(1).toLowerCase();});',
  195. '}']);
  196. var argument0 = Blockly.JavaScript.valueToCode(block, 'TEXT',
  197. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  198. code = functionName + '(' + argument0 + ')';
  199. }
  200. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  201. };
  202. Blockly.JavaScript['text_trim'] = function(block) {
  203. // Trim spaces.
  204. var OPERATORS = {
  205. 'LEFT': ".replace(/^[\\s\\xa0]+/, '')",
  206. 'RIGHT': ".replace(/[\\s\\xa0]+$/, '')",
  207. 'BOTH': '.trim()'
  208. };
  209. var operator = OPERATORS[block.getFieldValue('MODE')];
  210. var argument0 = Blockly.JavaScript.valueToCode(block, 'TEXT',
  211. Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
  212. return [argument0 + operator, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  213. };
  214. Blockly.JavaScript['text_print'] = function(block) {
  215. // Print statement.
  216. var argument0 = Blockly.JavaScript.valueToCode(block, 'TEXT',
  217. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  218. return 'window.alert(' + argument0 + ');\n';
  219. };
  220. Blockly.JavaScript['text_prompt_ext'] = function(block) {
  221. // Prompt function.
  222. if (block.getField('TEXT')) {
  223. // Internal message.
  224. var msg = Blockly.JavaScript.quote_(block.getFieldValue('TEXT'));
  225. } else {
  226. // External message.
  227. var msg = Blockly.JavaScript.valueToCode(block, 'TEXT',
  228. Blockly.JavaScript.ORDER_NONE) || '\'\'';
  229. }
  230. var code = 'window.prompt(' + msg + ')';
  231. var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
  232. if (toNumber) {
  233. code = 'parseFloat(' + code + ')';
  234. }
  235. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  236. };
  237. Blockly.JavaScript['text_prompt'] = Blockly.JavaScript['text_prompt_ext'];