text.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  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 Text blocks for Blockly.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Blocks.texts');
  26. goog.require('Blockly.Blocks');
  27. Blockscad.Toolbox = Blockscad.Toolbox || {};
  28. Blockly.Blocks.texts.HUE = Blockscad.Toolbox.HEX_TEXT;
  29. Blockly.Blocks['text'] = {
  30. /**
  31. * Block for text value.
  32. * @this Blockly.Block
  33. */
  34. init: function() {
  35. this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
  36. this.setColour(Blockscad.Toolbox.HEX_TEXT);
  37. this.appendDummyInput()
  38. .appendField(this.newQuote_(true))
  39. .appendField(new Blockly.FieldTextInput(Blockscad.Msg.TEXT_DEFAULT_VALUE), 'TEXT')
  40. .appendField(this.newQuote_(false));
  41. this.setOutput(true, 'String');
  42. this.setTooltip(Blockly.Msg.TEXT_TEXT_TOOLTIP);
  43. },
  44. /**
  45. * Create an image of an open or closed quote.
  46. * @param {boolean} open True if open quote, false if closed.
  47. * @return {!Blockly.FieldImage} The field image of the quote.
  48. * @this Blockly.Block
  49. * @private
  50. */
  51. newQuote_: function(open) {
  52. if (open == this.RTL) {
  53. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
  54. } else {
  55. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
  56. }
  57. return new Blockly.FieldImage(file, 10, 10, '');
  58. }
  59. };
  60. Blockly.Blocks['text_join'] = {
  61. /**
  62. * Block for creating a string made up of any number of elements of any type.
  63. * @this Blockly.Block
  64. */
  65. init: function() {
  66. this.setHelpUrl(Blockly.Msg.TEXT_JOIN_HELPURL);
  67. this.setColour(Blockly.Blocks.texts.HUE);
  68. this.itemCount_ = 2;
  69. this.updateShape_();
  70. this.setOutput(true, 'String');
  71. this.setMutator(new Blockly.Mutator(['text_create_join_item']));
  72. this.setTooltip(Blockly.Msg.TEXT_JOIN_TOOLTIP);
  73. },
  74. /**
  75. * Create XML to represent number of text inputs.
  76. * @return {!Element} XML storage element.
  77. * @this Blockly.Block
  78. */
  79. mutationToDom: function() {
  80. var container = document.createElement('mutation');
  81. container.setAttribute('items', this.itemCount_);
  82. return container;
  83. },
  84. /**
  85. * Parse XML to restore the text inputs.
  86. * @param {!Element} xmlElement XML storage element.
  87. * @this Blockly.Block
  88. */
  89. domToMutation: function(xmlElement) {
  90. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  91. this.updateShape_();
  92. },
  93. /**
  94. * Populate the mutator's dialog with this block's components.
  95. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  96. * @return {!Blockly.Block} Root block in mutator.
  97. * @this Blockly.Block
  98. */
  99. decompose: function(workspace) {
  100. var containerBlock = Blockly.Block.obtain(workspace,
  101. 'text_create_join_container');
  102. containerBlock.initSvg();
  103. var connection = containerBlock.getInput('STACK').connection;
  104. for (var i = 0; i < this.itemCount_; i++) {
  105. var itemBlock = Blockly.Block.obtain(workspace, 'text_create_join_item');
  106. itemBlock.initSvg();
  107. connection.connect(itemBlock.previousConnection);
  108. connection = itemBlock.nextConnection;
  109. }
  110. return containerBlock;
  111. },
  112. /**
  113. * Reconfigure this block based on the mutator dialog's components.
  114. * @param {!Blockly.Block} containerBlock Root block in mutator.
  115. * @this Blockly.Block
  116. */
  117. compose: function(containerBlock) {
  118. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  119. // Count number of inputs.
  120. var connections = [];
  121. var i = 0;
  122. while (itemBlock) {
  123. connections[i] = itemBlock.valueConnection_;
  124. itemBlock = itemBlock.nextConnection &&
  125. itemBlock.nextConnection.targetBlock();
  126. i++;
  127. }
  128. this.itemCount_ = i;
  129. this.updateShape_();
  130. // Reconnect any child blocks.
  131. for (var i = 0; i < this.itemCount_; i++) {
  132. if (connections[i]) {
  133. this.getInput('ADD' + i).connection.connect(connections[i]);
  134. }
  135. }
  136. },
  137. /**
  138. * Store pointers to any connected child blocks.
  139. * @param {!Blockly.Block} containerBlock Root block in mutator.
  140. * @this Blockly.Block
  141. */
  142. saveConnections: function(containerBlock) {
  143. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  144. var i = 0;
  145. while (itemBlock) {
  146. var input = this.getInput('ADD' + i);
  147. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  148. i++;
  149. itemBlock = itemBlock.nextConnection &&
  150. itemBlock.nextConnection.targetBlock();
  151. }
  152. },
  153. /**
  154. * Modify this block to have the correct number of inputs.
  155. * @private
  156. * @this Blockly.Block
  157. */
  158. updateShape_: function() {
  159. // Delete everything.
  160. if (this.getInput('EMPTY')) {
  161. this.removeInput('EMPTY');
  162. } else {
  163. var i = 0;
  164. while (this.getInput('ADD' + i)) {
  165. this.removeInput('ADD' + i);
  166. i++;
  167. }
  168. }
  169. // Rebuild block.
  170. if (this.itemCount_ == 0) {
  171. this.appendDummyInput('EMPTY')
  172. .appendField(this.newQuote_(true))
  173. .appendField(this.newQuote_(false));
  174. } else {
  175. for (var i = 0; i < this.itemCount_; i++) {
  176. var input = this.appendValueInput('ADD' + i);
  177. if (i == 0) {
  178. input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
  179. }
  180. }
  181. }
  182. },
  183. newQuote_: Blockly.Blocks['text'].newQuote_
  184. };
  185. Blockly.Blocks['text_create_join_container'] = {
  186. /**
  187. * Mutator block for container.
  188. * @this Blockly.Block
  189. */
  190. init: function() {
  191. this.setColour(Blockly.Blocks.texts.HUE);
  192. this.appendDummyInput()
  193. .appendField(Blockly.Msg.TEXT_CREATE_JOIN_TITLE_JOIN);
  194. this.appendStatementInput('STACK');
  195. this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_TOOLTIP);
  196. this.contextMenu = false;
  197. }
  198. };
  199. Blockly.Blocks['text_create_join_item'] = {
  200. /**
  201. * Mutator block for add items.
  202. * @this Blockly.Block
  203. */
  204. init: function() {
  205. this.setColour(Blockly.Blocks.texts.HUE);
  206. this.appendDummyInput()
  207. .appendField(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TITLE_ITEM);
  208. this.setPreviousStatement(true);
  209. this.setNextStatement(true);
  210. this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TOOLTIP);
  211. this.contextMenu = false;
  212. }
  213. };
  214. Blockly.Blocks['text_append'] = {
  215. /**
  216. * Block for appending to a variable in place.
  217. * @this Blockly.Block
  218. */
  219. init: function() {
  220. this.setHelpUrl(Blockly.Msg.TEXT_APPEND_HELPURL);
  221. this.setColour(Blockly.Blocks.texts.HUE);
  222. this.appendValueInput('TEXT')
  223. .appendField(Blockly.Msg.TEXT_APPEND_TO)
  224. .appendField(new Blockly.FieldVariable(
  225. Blockly.Msg.TEXT_APPEND_VARIABLE), 'VAR')
  226. .appendField(Blockly.Msg.TEXT_APPEND_APPENDTEXT);
  227. this.setPreviousStatement(true);
  228. this.setNextStatement(true);
  229. // Assign 'this' to a variable for use in the tooltip closure below.
  230. var thisBlock = this;
  231. this.setTooltip(function() {
  232. return Blockly.Msg.TEXT_APPEND_TOOLTIP.replace('%1',
  233. thisBlock.getFieldValue('VAR'));
  234. });
  235. },
  236. /**
  237. * Return all variables referenced by this block.
  238. * @return {!Array.<string>} List of variable names.
  239. * @this Blockly.Block
  240. */
  241. getVars: function() {
  242. return [this.getFieldValue('VAR')];
  243. },
  244. /**
  245. * Notification that a variable is renaming.
  246. * If the name matches one of this block's variables, rename it.
  247. * @param {string} oldName Previous name of variable.
  248. * @param {string} newName Renamed variable.
  249. * @this Blockly.Block
  250. */
  251. renameVar: function(oldName, newName) {
  252. if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
  253. this.setFieldValue(newName, 'VAR');
  254. }
  255. }
  256. };
  257. Blockly.Blocks['text_length'] = {
  258. /**
  259. * Block for string length.
  260. * @this Blockly.Block
  261. */
  262. init: function() {
  263. this.setHelpUrl(Blockly.Msg.TEXT_LENGTH_HELPURL);
  264. this.setColour(Blockly.Blocks.texts.HUE);
  265. this.interpolateMsg(Blockly.Msg.TEXT_LENGTH_TITLE,
  266. ['VALUE', ['String', 'Array'], Blockly.ALIGN_RIGHT],
  267. Blockly.ALIGN_RIGHT);
  268. this.setOutput(true, 'Number');
  269. this.setTooltip(Blockly.Msg.TEXT_LENGTH_TOOLTIP);
  270. }
  271. };
  272. Blockly.Blocks['text_isEmpty'] = {
  273. /**
  274. * Block for is the string null?
  275. * @this Blockly.Block
  276. */
  277. init: function() {
  278. this.setHelpUrl(Blockly.Msg.TEXT_ISEMPTY_HELPURL);
  279. this.setColour(Blockly.Blocks.texts.HUE);
  280. this.interpolateMsg(Blockly.Msg.TEXT_ISEMPTY_TITLE,
  281. ['VALUE', ['String', 'Array'], Blockly.ALIGN_RIGHT],
  282. Blockly.ALIGN_RIGHT);
  283. this.setOutput(true, 'Boolean');
  284. this.setTooltip(Blockly.Msg.TEXT_ISEMPTY_TOOLTIP);
  285. }
  286. };
  287. Blockly.Blocks['text_indexOf'] = {
  288. /**
  289. * Block for finding a substring in the text.
  290. * @this Blockly.Block
  291. */
  292. init: function() {
  293. var OPERATORS =
  294. [[Blockly.Msg.TEXT_INDEXOF_OPERATOR_FIRST, 'FIRST'],
  295. [Blockly.Msg.TEXT_INDEXOF_OPERATOR_LAST, 'LAST']];
  296. this.setHelpUrl(Blockly.Msg.TEXT_INDEXOF_HELPURL);
  297. this.setColour(Blockly.Blocks.texts.HUE);
  298. this.setOutput(true, 'Number');
  299. this.appendValueInput('VALUE')
  300. .setCheck('String')
  301. .appendField(Blockly.Msg.TEXT_INDEXOF_INPUT_INTEXT);
  302. this.appendValueInput('FIND')
  303. .setCheck('String')
  304. .appendField(new Blockly.FieldDropdown(OPERATORS), 'END');
  305. if (Blockly.Msg.TEXT_INDEXOF_TAIL) {
  306. this.appendDummyInput().appendField(Blockly.Msg.TEXT_INDEXOF_TAIL);
  307. }
  308. this.setInputsInline(true);
  309. this.setTooltip(Blockly.Msg.TEXT_INDEXOF_TOOLTIP);
  310. }
  311. };
  312. Blockly.Blocks['text_charAt'] = {
  313. /**
  314. * Block for getting a character from the string.
  315. * @this Blockly.Block
  316. */
  317. init: function() {
  318. this.WHERE_OPTIONS =
  319. [[Blockly.Msg.TEXT_CHARAT_FROM_START, 'FROM_START'],
  320. [Blockly.Msg.TEXT_CHARAT_FROM_END, 'FROM_END'],
  321. [Blockly.Msg.TEXT_CHARAT_FIRST, 'FIRST'],
  322. [Blockly.Msg.TEXT_CHARAT_LAST, 'LAST'],
  323. [Blockly.Msg.TEXT_CHARAT_RANDOM, 'RANDOM']];
  324. this.setHelpUrl(Blockly.Msg.TEXT_CHARAT_HELPURL);
  325. this.setColour(Blockly.Blocks.texts.HUE);
  326. this.setOutput(true, 'String');
  327. this.appendValueInput('VALUE')
  328. .setCheck('String')
  329. .appendField(Blockly.Msg.TEXT_CHARAT_INPUT_INTEXT);
  330. this.appendDummyInput('AT');
  331. this.setInputsInline(true);
  332. this.updateAt_(true);
  333. this.setTooltip(Blockly.Msg.TEXT_CHARAT_TOOLTIP);
  334. },
  335. /**
  336. * Create XML to represent whether there is an 'AT' input.
  337. * @return {!Element} XML storage element.
  338. * @this Blockly.Block
  339. */
  340. mutationToDom: function() {
  341. var container = document.createElement('mutation');
  342. var isAt = this.getInput('AT').type == Blockly.INPUT_VALUE;
  343. container.setAttribute('at', isAt);
  344. return container;
  345. },
  346. /**
  347. * Parse XML to restore the 'AT' input.
  348. * @param {!Element} xmlElement XML storage element.
  349. * @this Blockly.Block
  350. */
  351. domToMutation: function(xmlElement) {
  352. // Note: Until January 2013 this block did not have mutations,
  353. // so 'at' defaults to true.
  354. var isAt = (xmlElement.getAttribute('at') != 'false');
  355. this.updateAt_(isAt);
  356. },
  357. /**
  358. * Create or delete an input for the numeric index.
  359. * @param {boolean} isAt True if the input should exist.
  360. * @private
  361. * @this Blockly.Block
  362. */
  363. updateAt_: function(isAt) {
  364. // Destroy old 'AT' and 'ORDINAL' inputs.
  365. this.removeInput('AT');
  366. this.removeInput('ORDINAL', true);
  367. // Create either a value 'AT' input or a dummy input.
  368. if (isAt) {
  369. this.appendValueInput('AT').setCheck('Number');
  370. if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
  371. this.appendDummyInput('ORDINAL')
  372. .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
  373. }
  374. } else {
  375. this.appendDummyInput('AT');
  376. }
  377. if (Blockly.Msg.TEXT_CHARAT_TAIL) {
  378. this.removeInput('TAIL', true);
  379. this.appendDummyInput('TAIL')
  380. .appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
  381. }
  382. var menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, function(value) {
  383. var newAt = (value == 'FROM_START') || (value == 'FROM_END');
  384. // The 'isAt' variable is available due to this function being a closure.
  385. if (newAt != isAt) {
  386. var block = this.sourceBlock_;
  387. block.updateAt_(newAt);
  388. // This menu has been destroyed and replaced. Update the replacement.
  389. block.setFieldValue(value, 'WHERE');
  390. return null;
  391. }
  392. return undefined;
  393. });
  394. this.getInput('AT').appendField(menu, 'WHERE');
  395. }
  396. };
  397. Blockly.Blocks['text_getSubstring'] = {
  398. /**
  399. * Block for getting substring.
  400. * @this Blockly.Block
  401. */
  402. init: function() {
  403. this['WHERE_OPTIONS_1'] =
  404. [[Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_START, 'FROM_START'],
  405. [Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_END, 'FROM_END'],
  406. [Blockly.Msg.TEXT_GET_SUBSTRING_START_FIRST, 'FIRST']];
  407. this['WHERE_OPTIONS_2'] =
  408. [[Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_START, 'FROM_START'],
  409. [Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_END, 'FROM_END'],
  410. [Blockly.Msg.TEXT_GET_SUBSTRING_END_LAST, 'LAST']];
  411. this.setHelpUrl(Blockly.Msg.TEXT_GET_SUBSTRING_HELPURL);
  412. this.setColour(Blockly.Blocks.texts.HUE);
  413. this.appendValueInput('STRING')
  414. .setCheck('String')
  415. .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_INPUT_IN_TEXT);
  416. this.appendDummyInput('AT1');
  417. this.appendDummyInput('AT2');
  418. if (Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
  419. this.appendDummyInput('TAIL')
  420. .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
  421. }
  422. this.setInputsInline(true);
  423. this.setOutput(true, 'String');
  424. this.updateAt_(1, true);
  425. this.updateAt_(2, true);
  426. this.setTooltip(Blockly.Msg.TEXT_GET_SUBSTRING_TOOLTIP);
  427. },
  428. /**
  429. * Create XML to represent whether there are 'AT' inputs.
  430. * @return {!Element} XML storage element.
  431. * @this Blockly.Block
  432. */
  433. mutationToDom: function() {
  434. var container = document.createElement('mutation');
  435. var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE;
  436. container.setAttribute('at1', isAt1);
  437. var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE;
  438. container.setAttribute('at2', isAt2);
  439. return container;
  440. },
  441. /**
  442. * Parse XML to restore the 'AT' inputs.
  443. * @param {!Element} xmlElement XML storage element.
  444. * @this Blockly.Block
  445. */
  446. domToMutation: function(xmlElement) {
  447. var isAt1 = (xmlElement.getAttribute('at1') == 'true');
  448. var isAt2 = (xmlElement.getAttribute('at2') == 'true');
  449. this.updateAt_(1, isAt1);
  450. this.updateAt_(2, isAt2);
  451. },
  452. /**
  453. * Create or delete an input for a numeric index.
  454. * This block has two such inputs, independant of each other.
  455. * @param {number} n Specify first or second input (1 or 2).
  456. * @param {boolean} isAt True if the input should exist.
  457. * @private
  458. * @this Blockly.Block
  459. */
  460. updateAt_: function(n, isAt) {
  461. // Create or delete an input for the numeric index.
  462. // Destroy old 'AT' and 'ORDINAL' inputs.
  463. this.removeInput('AT' + n);
  464. this.removeInput('ORDINAL' + n, true);
  465. // Create either a value 'AT' input or a dummy input.
  466. if (isAt) {
  467. this.appendValueInput('AT' + n).setCheck('Number');
  468. if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
  469. this.appendDummyInput('ORDINAL' + n)
  470. .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
  471. }
  472. } else {
  473. this.appendDummyInput('AT' + n);
  474. }
  475. // Move tail, if present, to end of block.
  476. if (n == 2 && Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
  477. this.removeInput('TAIL', true);
  478. this.appendDummyInput('TAIL')
  479. .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
  480. }
  481. var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n],
  482. function(value) {
  483. var newAt = (value == 'FROM_START') || (value == 'FROM_END');
  484. // The 'isAt' variable is available due to this function being a closure.
  485. if (newAt != isAt) {
  486. var block = this.sourceBlock_;
  487. block.updateAt_(n, newAt);
  488. // This menu has been destroyed and replaced. Update the replacement.
  489. block.setFieldValue(value, 'WHERE' + n);
  490. return null;
  491. }
  492. return undefined;
  493. });
  494. this.getInput('AT' + n)
  495. .appendField(menu, 'WHERE' + n);
  496. if (n == 1) {
  497. this.moveInputBefore('AT1', 'AT2');
  498. }
  499. }
  500. };
  501. Blockly.Blocks['text_changeCase'] = {
  502. /**
  503. * Block for changing capitalization.
  504. * @this Blockly.Block
  505. */
  506. init: function() {
  507. var OPERATORS =
  508. [[Blockly.Msg.TEXT_CHANGECASE_OPERATOR_UPPERCASE, 'UPPERCASE'],
  509. [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_LOWERCASE, 'LOWERCASE'],
  510. [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_TITLECASE, 'TITLECASE']];
  511. this.setHelpUrl(Blockly.Msg.TEXT_CHANGECASE_HELPURL);
  512. this.setColour(Blockly.Blocks.texts.HUE);
  513. this.appendValueInput('TEXT')
  514. .setCheck('String')
  515. .appendField(new Blockly.FieldDropdown(OPERATORS), 'CASE');
  516. this.setOutput(true, 'String');
  517. this.setTooltip(Blockly.Msg.TEXT_CHANGECASE_TOOLTIP);
  518. }
  519. };
  520. Blockly.Blocks['text_trim'] = {
  521. /**
  522. * Block for trimming spaces.
  523. * @this Blockly.Block
  524. */
  525. init: function() {
  526. var OPERATORS =
  527. [[Blockly.Msg.TEXT_TRIM_OPERATOR_BOTH, 'BOTH'],
  528. [Blockly.Msg.TEXT_TRIM_OPERATOR_LEFT, 'LEFT'],
  529. [Blockly.Msg.TEXT_TRIM_OPERATOR_RIGHT, 'RIGHT']];
  530. this.setHelpUrl(Blockly.Msg.TEXT_TRIM_HELPURL);
  531. this.setColour(Blockly.Blocks.texts.HUE);
  532. this.appendValueInput('TEXT')
  533. .setCheck('String')
  534. .appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE');
  535. this.setOutput(true, 'String');
  536. this.setTooltip(Blockly.Msg.TEXT_TRIM_TOOLTIP);
  537. }
  538. };
  539. Blockly.Blocks['text_print'] = {
  540. /**
  541. * Block for print statement.
  542. * @this Blockly.Block
  543. */
  544. init: function() {
  545. this.setHelpUrl(Blockly.Msg.TEXT_PRINT_HELPURL);
  546. this.setColour(Blockly.Blocks.texts.HUE);
  547. this.interpolateMsg(Blockly.Msg.TEXT_PRINT_TITLE,
  548. ['TEXT', null, Blockly.ALIGN_RIGHT],
  549. Blockly.ALIGN_RIGHT);
  550. this.setPreviousStatement(true);
  551. this.setNextStatement(true);
  552. this.setTooltip(Blockly.Msg.TEXT_PRINT_TOOLTIP);
  553. }
  554. };
  555. Blockly.Blocks['text_prompt_ext'] = {
  556. /**
  557. * Block for prompt function (external message).
  558. * @this Blockly.Block
  559. */
  560. init: function() {
  561. var TYPES =
  562. [[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, 'TEXT'],
  563. [Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, 'NUMBER']];
  564. this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
  565. this.setColour(Blockly.Blocks.texts.HUE);
  566. // Assign 'this' to a variable for use in the closures below.
  567. var thisBlock = this;
  568. var dropdown = new Blockly.FieldDropdown(TYPES, function(newOp) {
  569. thisBlock.updateType_(newOp);
  570. });
  571. this.appendValueInput('TEXT')
  572. .appendField(dropdown, 'TYPE');
  573. this.setOutput(true, 'String');
  574. this.setTooltip(function() {
  575. return (thisBlock.getFieldValue('TYPE') == 'TEXT') ?
  576. Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
  577. Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
  578. });
  579. },
  580. /**
  581. * Modify this block to have the correct output type.
  582. * @param {string} newOp Either 'TEXT' or 'NUMBER'.
  583. * @private
  584. * @this Blockly.Block
  585. */
  586. updateType_: function(newOp) {
  587. if (newOp == 'NUMBER') {
  588. this.outputConnection.setCheck('Number');
  589. } else {
  590. this.outputConnection.setCheck('String');
  591. }
  592. },
  593. /**
  594. * Create XML to represent the output type.
  595. * @return {!Element} XML storage element.
  596. * @this Blockly.Block
  597. */
  598. mutationToDom: function() {
  599. var container = document.createElement('mutation');
  600. container.setAttribute('type', this.getFieldValue('TYPE'));
  601. return container;
  602. },
  603. /**
  604. * Parse XML to restore the output type.
  605. * @param {!Element} xmlElement XML storage element.
  606. * @this Blockly.Block
  607. */
  608. domToMutation: function(xmlElement) {
  609. this.updateType_(xmlElement.getAttribute('type'));
  610. }
  611. };
  612. Blockly.Blocks['text_prompt'] = {
  613. /**
  614. * Block for prompt function (internal message).
  615. * @this Blockly.Block
  616. */
  617. init: function() {
  618. var TYPES =
  619. [[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, 'TEXT'],
  620. [Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, 'NUMBER']];
  621. // Assign 'this' to a variable for use in the closure below.
  622. var thisBlock = this;
  623. this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
  624. this.setColour(Blockly.Blocks.texts.HUE);
  625. var dropdown = new Blockly.FieldDropdown(TYPES, function(newOp) {
  626. thisBlock.updateType_(newOp);
  627. });
  628. this.appendDummyInput()
  629. .appendField(dropdown, 'TYPE')
  630. .appendField(this.newQuote_(true))
  631. .appendField(new Blockly.FieldTextInput(''), 'TEXT')
  632. .appendField(this.newQuote_(false));
  633. this.setOutput(true, 'String');
  634. // Assign 'this' to a variable for use in the tooltip closure below.
  635. var thisBlock = this;
  636. this.setTooltip(function() {
  637. return (thisBlock.getFieldValue('TYPE') == 'TEXT') ?
  638. Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
  639. Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
  640. });
  641. },
  642. newQuote_: Blockly.Blocks['text'].newQuote_,
  643. updateType_: Blockly.Blocks['text_prompt_ext'].updateType_,
  644. mutationToDom: Blockly.Blocks['text_prompt_ext'].mutationToDom,
  645. domToMutation: Blockly.Blocks['text_prompt_ext'].domToMutation
  646. };