not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
9.8 KiB

  1. var ExampleExtension = function () {
  2. };
  3. /**
  4. * @return {object} This extension's metadata.
  5. */
  6. ExampleExtension.prototype.getInfo = function () {
  7. return {
  8. // Required: the machine-readable name of this extension.
  9. // Will be used as the extension's namespace. Must not contain a '.' character.
  10. id: 'someBlocks',
  11. // Optional: the human-readable name of this extension as string.
  12. // This and any other string to be displayed in the Scratch UI may either be
  13. // a string or a call to `intlDefineMessage`; a plain string will not be
  14. // translated whereas a call to `intlDefineMessage` will connect the string
  15. // to the translation map (see below). The `intlDefineMessage` call is
  16. // similar to `defineMessages` from `react-intl` in form, but will actually
  17. // call some extension support code to do its magic. For example, we will
  18. // internally namespace the messages such that two extensions could have
  19. // messages with the same ID without colliding.
  20. // See also: https://github.com/yahoo/react-intl/wiki/API#definemessages
  21. name: 'Some Blocks',
  22. // Optional: URI for an icon for this extension. Data URI OK.
  23. // If not present, use a generic icon.
  24. // TODO: what file types are OK? All web images? Just PNG?
  25. iconURI: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAFCAAAAACyOJm3AAAAFklEQVQYV2P4DwMMEMgAI/+DE' +
  26. 'UIMBgAEWB7i7uidhAAAAABJRU5ErkJggg==',
  27. // Optional: Link to documentation content for this extension.
  28. // If not present, offer no link.
  29. docsURI: 'https://....',
  30. // Required: the list of blocks implemented by this extension,
  31. // in the order intended for display.
  32. blocks: [
  33. {
  34. opcode: 'example-noop',
  35. blockType: Scratch.BlockType.COMMAND,
  36. blockAllThreads: false,
  37. text: 'do nothing',
  38. func: 'noop'
  39. },
  40. {
  41. opcode: 'example-conditional',
  42. blockType: Scratch.BlockType.CONDITIONAL,
  43. branchCount: 4,
  44. isTerminal: true,
  45. blockAllThreads: false,
  46. text: 'choose [BRANCH]',
  47. arguments: {
  48. BRANCH: {
  49. type: Scratch.ArgumentType.NUMBER,
  50. defaultValue: 1
  51. }
  52. },
  53. func: 'noop'
  54. },
  55. {
  56. // Required: the machine-readable name of this operation.
  57. // This will appear in project JSON. Must not contain a '.' character.
  58. opcode: 'myReporter', // becomes 'someBlocks.myReporter'
  59. // Required: the kind of block we're defining, from a predefined list:
  60. // 'command' - a normal command block, like "move {} steps"
  61. // 'reporter' - returns a value, like "direction"
  62. // 'Boolean' - same as 'reporter' but returns a Boolean value
  63. // 'hat' - starts a stack if its value is truthy
  64. // 'conditional' - control flow, like "if {}" or "repeat {}"
  65. // A 'conditional' block may return the one-based index of a branch
  66. // to run, or it may return zero/falsy to run no branch. Each time a
  67. // child branch finishes, the block is called again. This is only a
  68. // slight change to the current model for control flow blocks, and is
  69. // also compatible with returning true/false for an "if" or "repeat"
  70. // block.
  71. // TODO: Consider Blockly-like nextStatement, previousStatement, and
  72. // output attributes as an alternative. Those are more flexible, but
  73. // allow bad combinations.
  74. blockType: Scratch.BlockType.REPORTER,
  75. // Required for conditional blocks, ignored for others: the number of
  76. // child branches this block controls. An "if" or "repeat" block would
  77. // specify a branch count of 1; an "if-else" block would specify a
  78. // branch count of 2.
  79. // TODO: should we support dynamic branch count for "switch"-likes?
  80. branchCount: 0,
  81. // Optional, default false: whether or not this block ends a stack.
  82. // The "forever" and "stop all" blocks would specify true here.
  83. isTerminal: true,
  84. // Optional, default false: whether or not to block all threads while
  85. // this block is busy. This is for things like the "touching color"
  86. // block in compatibility mode, and is only needed if the VM runs in a
  87. // worker. We might even consider omitting it from extension docs...
  88. blockAllThreads: false,
  89. // Required: the human-readable text on this block, including argument
  90. // placeholders. Argument placeholders should be in [MACRO_CASE] and
  91. // must be [ENCLOSED_WITHIN_SQUARE_BRACKETS].
  92. text: 'letter [LETTER_NUM] of [TEXT]',
  93. // Required: describe each argument.
  94. // Note that this is an array: the order of arguments will be used
  95. arguments: {
  96. // Required: the ID of the argument, which will be the name in the
  97. // args object passed to the implementation function.
  98. LETTER_NUM: {
  99. // Required: type of the argument / shape of the block input
  100. type: Scratch.ArgumentType.NUMBER,
  101. // Optional: the default value of the argument
  102. defaultValue: 1
  103. },
  104. // Required: the ID of the argument, which will be the name in the
  105. // args object passed to the implementation function.
  106. TEXT: {
  107. // Required: type of the argument / shape of the block input
  108. type: Scratch.ArgumentType.STRING,
  109. // Optional: the default value of the argument
  110. defaultValue: 'text'
  111. }
  112. },
  113. // Optional: a string naming the function implementing this block.
  114. // If this is omitted, use the opcode string.
  115. func: 'myReporter',
  116. // Optional: list of target types for which this block should appear.
  117. // If absent, assume it applies to all builtin targets -- that is:
  118. // ['sprite', 'stage']
  119. filter: ['someBlocks.wedo2', 'sprite', 'stage']
  120. },
  121. {
  122. opcode: 'example-Boolean',
  123. blockType: Scratch.BlockType.BOOLEAN,
  124. text: 'return true',
  125. func: 'returnTrue'
  126. },
  127. {
  128. opcode: 'example-hat',
  129. blockType: Scratch.BlockType.HAT,
  130. text: 'after forever',
  131. func: 'returnFalse'
  132. },
  133. {
  134. // Another block...
  135. }
  136. ],
  137. // Optional: define extension-specific menus here.
  138. menus: {
  139. // Required: an identifier for this menu, unique within this extension.
  140. menuA: [
  141. // Static menu: list items which should appear in the menu.
  142. {
  143. // Required: the value of the menu item when it is chosen.
  144. value: 'itemId1',
  145. // Optional: the human-readable label for this item.
  146. // Use `value` as the text if this is absent.
  147. text: 'Item One'
  148. },
  149. // The simplest form of a list item is a string which will be used as
  150. // both value and text.
  151. 'itemId2'
  152. ],
  153. // Dynamic menu: a string naming a function which returns an array as above.
  154. // Called each time the menu is opened.
  155. menuB: 'getItemsForMenuB'
  156. },
  157. // Optional: translations
  158. translation_map: {
  159. de: {
  160. 'extensionName': 'Einige Blöcke',
  161. 'myReporter': 'Buchstabe [LETTER_NUM] von [TEXT]',
  162. 'myReporter.TEXT_default': 'Text',
  163. 'menuA_item1': 'Artikel eins',
  164. // Dynamic menus can be translated too
  165. 'menuB_example': 'Beispiel',
  166. // This message contains ICU placeholders (see `myReporter()` below)
  167. 'myReporter.result': 'Buchstabe {LETTER_NUM} von {TEXT} ist {LETTER}.'
  168. },
  169. it: {
  170. // ...
  171. }
  172. },
  173. // Optional: list new target type(s) provided by this extension.
  174. targetTypes: [
  175. 'wedo2', // automatically transformed to 'someBlocks.wedo2'
  176. 'speech' // automatically transformed to 'someBlocks.speech'
  177. ]
  178. };
  179. };
  180. /**
  181. * Implement myReporter.
  182. * @param {object} args - the block's arguments.
  183. * @property {number} LETTER_NUM - the string value of the argument.
  184. * @property {string} TEXT - the string value of the argument.
  185. * @returns {string} a string which includes the block argument value.
  186. */
  187. ExampleExtension.prototype.myReporter = function (args) {
  188. // Note: this implementation is not Unicode-clean; it's just here as an example.
  189. const result = args.TEXT.charAt(args.LETTER_NUM);
  190. return ['Letter ', args.LETTER_NUM, ' of ', args.TEXT, ' is ', result, '.'].join('');
  191. };
  192. ExampleExtension.prototype.noop = function () {
  193. };
  194. ExampleExtension.prototype.returnTrue = function () {
  195. return true;
  196. };
  197. ExampleExtension.prototype.returnFalse = function () {
  198. return false;
  199. };
  200. Scratch.extensions.register(new ExampleExtension());