text.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2014 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 Dart for text blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Dart.texts');
  26. goog.require('Blockly.Dart');
  27. Blockly.Dart.addReservedWords('Html,Math');
  28. Blockly.Dart['text'] = function(block) {
  29. // Text value.
  30. var code = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
  31. return [code, Blockly.Dart.ORDER_ATOMIC];
  32. };
  33. Blockly.Dart['text_join'] = function(block) {
  34. // Create a string made up of any number of elements of any type.
  35. switch (block.itemCount_) {
  36. case 0:
  37. return ['\'\'', Blockly.Dart.ORDER_ATOMIC];
  38. case 1:
  39. var element = Blockly.Dart.valueToCode(block, 'ADD0',
  40. Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
  41. var code = element + '.toString()';
  42. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  43. default:
  44. var elements = new Array(block.itemCount_);
  45. for (var i = 0; i < block.itemCount_; i++) {
  46. elements[i] = Blockly.Dart.valueToCode(block, 'ADD' + i,
  47. Blockly.Dart.ORDER_NONE) || '\'\'';
  48. }
  49. var code = '[' + elements.join(',') + '].join()';
  50. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  51. }
  52. };
  53. Blockly.Dart['text_append'] = function(block) {
  54. // Append to a variable in place.
  55. var varName = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
  56. Blockly.Variables.NAME_TYPE);
  57. var value = Blockly.Dart.valueToCode(block, 'TEXT',
  58. Blockly.Dart.ORDER_NONE) || '\'\'';
  59. return varName + ' = [' + varName + ', ' + value + '].join();\n';
  60. };
  61. Blockly.Dart['text_length'] = function(block) {
  62. // String or array length.
  63. var text = Blockly.Dart.valueToCode(block, 'VALUE',
  64. Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
  65. return [text + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
  66. };
  67. Blockly.Dart['text_isEmpty'] = function(block) {
  68. // Is the string null or array empty?
  69. var text = Blockly.Dart.valueToCode(block, 'VALUE',
  70. Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
  71. return [text + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
  72. };
  73. Blockly.Dart['text_indexOf'] = function(block) {
  74. // Search the text for a substring.
  75. var operator = block.getFieldValue('END') == 'FIRST' ?
  76. 'indexOf' : 'lastIndexOf';
  77. var substring = Blockly.Dart.valueToCode(block, 'FIND',
  78. Blockly.Dart.ORDER_NONE) || '\'\'';
  79. var text = Blockly.Dart.valueToCode(block, 'VALUE',
  80. Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
  81. var code = text + '.' + operator + '(' + substring + ')';
  82. if (Blockly.Dart.ONE_BASED_INDEXING) {
  83. return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
  84. }
  85. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  86. };
  87. Blockly.Dart['text_charAt'] = function(block) {
  88. // Get letter at index.
  89. // Note: Until January 2013 this block did not have the WHERE input.
  90. var where = block.getFieldValue('WHERE') || 'FROM_START';
  91. var text = Blockly.Dart.valueToCode(block, 'VALUE',
  92. Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
  93. switch (where) {
  94. case 'FIRST':
  95. var code = text + '[0]';
  96. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  97. case 'FROM_START':
  98. var at = Blockly.Dart.getAdjusted(block, 'AT');
  99. var code = text + '[' + at + ']';
  100. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  101. case 'LAST':
  102. at = 1;
  103. // Fall through.
  104. case 'FROM_END':
  105. var at = Blockly.Dart.getAdjusted(block, 'AT', 1);
  106. var functionName = Blockly.Dart.provideFunction_(
  107. 'text_get_from_end',
  108. ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
  109. '(String text, num x) {',
  110. ' return text[text.length - x];',
  111. '}']);
  112. code = functionName + '(' + text + ', ' + at + ')';
  113. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  114. case 'RANDOM':
  115. Blockly.Dart.definitions_['import_dart_math'] =
  116. 'import \'dart:math\' as Math;';
  117. var functionName = Blockly.Dart.provideFunction_(
  118. 'text_random_letter',
  119. ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
  120. '(String text) {',
  121. ' int x = new Math.Random().nextInt(text.length);',
  122. ' return text[x];',
  123. '}']);
  124. code = functionName + '(' + text + ')';
  125. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  126. }
  127. throw 'Unhandled option (text_charAt).';
  128. };
  129. Blockly.Dart['text_getSubstring'] = function(block) {
  130. // Get substring.
  131. var text = Blockly.Dart.valueToCode(block, 'STRING',
  132. Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
  133. var where1 = block.getFieldValue('WHERE1');
  134. var where2 = block.getFieldValue('WHERE2');
  135. if (where1 == 'FIRST' && where2 == 'LAST') {
  136. var code = text;
  137. } else if (text.match(/^'?\w+'?$/) ||
  138. (where1 != 'FROM_END' && where2 == 'FROM_START')) {
  139. // If the text is a variable or literal or doesn't require a call for
  140. // length, don't generate a helper function.
  141. switch (where1) {
  142. case 'FROM_START':
  143. var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
  144. break;
  145. case 'FROM_END':
  146. var at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false,
  147. Blockly.Dart.ORDER_ADDITIVE);
  148. at1 = text + '.length - ' + at1;
  149. break;
  150. case 'FIRST':
  151. var at1 = '0';
  152. break;
  153. default:
  154. throw 'Unhandled option (text_getSubstring).';
  155. }
  156. switch (where2) {
  157. case 'FROM_START':
  158. var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1);
  159. break;
  160. case 'FROM_END':
  161. var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false,
  162. Blockly.Dart.ORDER_ADDITIVE);
  163. at2 = text + '.length - ' + at2;
  164. break;
  165. case 'LAST':
  166. break;
  167. default:
  168. throw 'Unhandled option (text_getSubstring).';
  169. }
  170. if (where2 == 'LAST') {
  171. var code = text + '.substring(' + at1 + ')';
  172. } else {
  173. var code = text + '.substring(' + at1 + ', ' + at2 + ')';
  174. }
  175. } else {
  176. var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
  177. var at2 = Blockly.Dart.getAdjusted(block, 'AT2');
  178. var functionName = Blockly.Dart.provideFunction_(
  179. 'text_get_substring',
  180. ['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
  181. '(text, where1, at1, where2, at2) {',
  182. ' int getAt(where, at) {',
  183. ' if (where == \'FROM_END\') {',
  184. ' at = text.length - 1 - at;',
  185. ' } else if (where == \'FIRST\') {',
  186. ' at = 0;',
  187. ' } else if (where == \'LAST\') {',
  188. ' at = text.length - 1;',
  189. ' } else if (where != \'FROM_START\') {',
  190. ' throw \'Unhandled option (text_getSubstring).\';',
  191. ' }',
  192. ' return at;',
  193. ' }',
  194. ' at1 = getAt(where1, at1);',
  195. ' at2 = getAt(where2, at2) + 1;',
  196. ' return text.substring(at1, at2);',
  197. '}']);
  198. var code = functionName + '(' + text + ', \'' +
  199. where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
  200. }
  201. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  202. };
  203. Blockly.Dart['text_changeCase'] = function(block) {
  204. // Change capitalization.
  205. var OPERATORS = {
  206. 'UPPERCASE': '.toUpperCase()',
  207. 'LOWERCASE': '.toLowerCase()',
  208. 'TITLECASE': null
  209. };
  210. var operator = OPERATORS[block.getFieldValue('CASE')];
  211. var textOrder = operator ? Blockly.Dart.ORDER_UNARY_POSTFIX :
  212. Blockly.Dart.ORDER_NONE;
  213. var text = Blockly.Dart.valueToCode(block, 'TEXT', textOrder) || '\'\'';
  214. if (operator) {
  215. // Upper and lower case are functions built into Dart.
  216. var code = text + operator;
  217. } else {
  218. // Title case is not a native Dart function. Define one.
  219. var functionName = Blockly.Dart.provideFunction_(
  220. 'text_toTitleCase',
  221. ['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
  222. '(String str) {',
  223. ' RegExp exp = new RegExp(r\'\\b\');',
  224. ' List<String> list = str.split(exp);',
  225. ' final title = new StringBuffer();',
  226. ' for (String part in list) {',
  227. ' if (part.length > 0) {',
  228. ' title.write(part[0].toUpperCase());',
  229. ' if (part.length > 0) {',
  230. ' title.write(part.substring(1).toLowerCase());',
  231. ' }',
  232. ' }',
  233. ' }',
  234. ' return title.toString();',
  235. '}']);
  236. var code = functionName + '(' + text + ')';
  237. }
  238. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  239. };
  240. Blockly.Dart['text_trim'] = function(block) {
  241. // Trim spaces.
  242. var OPERATORS = {
  243. 'LEFT': '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
  244. 'RIGHT': '.replaceFirst(new RegExp(r\'\\s+$\'), \'\')',
  245. 'BOTH': '.trim()'
  246. };
  247. var operator = OPERATORS[block.getFieldValue('MODE')];
  248. var text = Blockly.Dart.valueToCode(block, 'TEXT',
  249. Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
  250. return [text + operator, Blockly.Dart.ORDER_UNARY_POSTFIX];
  251. };
  252. Blockly.Dart['text_print'] = function(block) {
  253. // Print statement.
  254. var msg = Blockly.Dart.valueToCode(block, 'TEXT',
  255. Blockly.Dart.ORDER_NONE) || '\'\'';
  256. return 'print(' + msg + ');\n';
  257. };
  258. Blockly.Dart['text_prompt_ext'] = function(block) {
  259. // Prompt function.
  260. Blockly.Dart.definitions_['import_dart_html'] =
  261. 'import \'dart:html\' as Html;';
  262. if (block.getField('TEXT')) {
  263. // Internal message.
  264. var msg = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
  265. } else {
  266. // External message.
  267. var msg = Blockly.Dart.valueToCode(block, 'TEXT',
  268. Blockly.Dart.ORDER_NONE) || '\'\'';
  269. }
  270. var code = 'Html.window.prompt(' + msg + ', \'\')';
  271. var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
  272. if (toNumber) {
  273. Blockly.Dart.definitions_['import_dart_math'] =
  274. 'import \'dart:math\' as Math;';
  275. code = 'Math.parseDouble(' + code + ')';
  276. }
  277. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  278. };
  279. Blockly.Dart['text_prompt'] = Blockly.Dart['text_prompt_ext'];