|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +let workspace = null; |
| 4 | + |
| 5 | +function start() { |
| 6 | + registerHelloWorldItem(); |
| 7 | + registerHelpItem(); |
| 8 | + registerDisplayItem(); |
| 9 | + Blockly.ContextMenuRegistry.registry.unregister('workspaceDelete'); |
| 10 | + registerSeparators(); |
| 11 | + |
| 12 | + Blockly.ContextMenuItems.registerCommentOptions(); |
| 13 | + // Create main workspace. |
| 14 | + workspace = Blockly.inject('blocklyDiv', { |
| 15 | + toolbox: toolboxSimple, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function registerHelloWorldItem() { |
| 20 | + const helloWorldItem = { |
| 21 | + displayText: 'Hello World', |
| 22 | + preconditionFn: function (scope) { |
| 23 | + // Only display this option for workspaces and blocks. |
| 24 | + if ( |
| 25 | + scope.focusedNode instanceof Blockly.WorkspaceSvg || |
| 26 | + scope.focusedNode instanceof Blockly.BlockSvg |
| 27 | + ) { |
| 28 | + // Enable for the first 30 seconds of every minute; disable for the next 30 seconds. |
| 29 | + const now = new Date(Date.now()); |
| 30 | + if (now.getSeconds() < 30) { |
| 31 | + return 'enabled'; |
| 32 | + } |
| 33 | + return 'disabled'; |
| 34 | + } |
| 35 | + return 'hidden'; |
| 36 | + }, |
| 37 | + callback: function (scope) {}, |
| 38 | + id: 'hello_world', |
| 39 | + weight: 100, |
| 40 | + }; |
| 41 | + // Register. |
| 42 | + Blockly.ContextMenuRegistry.registry.register(helloWorldItem); |
| 43 | +} |
| 44 | + |
| 45 | +function registerHelpItem() { |
| 46 | + const helpItem = { |
| 47 | + displayText: 'Help! There are no blocks', |
| 48 | + preconditionFn: function (scope) { |
| 49 | + // Only display this option on workspace context menus. |
| 50 | + if (!(scope.focusedNode instanceof Blockly.WorkspaceSvg)) return 'hidden'; |
| 51 | + // Use the focused node, which is a WorkspaceSvg, to check for blocks on the workspace. |
| 52 | + if (!scope.focusedNode.getTopBlocks().length) { |
| 53 | + return 'enabled'; |
| 54 | + } |
| 55 | + return 'hidden'; |
| 56 | + }, |
| 57 | + // Use the focused node in the callback function to add a block to the workspace. |
| 58 | + callback: function (scope) { |
| 59 | + Blockly.serialization.blocks.append( |
| 60 | + { |
| 61 | + type: 'text', |
| 62 | + fields: { |
| 63 | + TEXT: 'Now there is a block', |
| 64 | + }, |
| 65 | + }, |
| 66 | + scope.focusedNode, |
| 67 | + ); |
| 68 | + }, |
| 69 | + id: 'help_no_blocks', |
| 70 | + weight: 100, |
| 71 | + }; |
| 72 | + Blockly.ContextMenuRegistry.registry.register(helpItem); |
| 73 | +} |
| 74 | + |
| 75 | +function registerDisplayItem() { |
| 76 | + const displayItem = { |
| 77 | + // Use the focused node (a BlockSvg) to set display text dynamically based on the type of the block. |
| 78 | + displayText: function (scope) { |
| 79 | + if (scope.focusedNode.type.startsWith('text')) { |
| 80 | + return 'Text block'; |
| 81 | + } else if (scope.focusedNode.type.startsWith('controls')) { |
| 82 | + return 'Controls block'; |
| 83 | + } else { |
| 84 | + return 'Some other block'; |
| 85 | + } |
| 86 | + }, |
| 87 | + preconditionFn: function (scope) { |
| 88 | + return scope.focusedNode instanceof Blockly.BlockSvg |
| 89 | + ? 'enabled' |
| 90 | + : 'hidden'; |
| 91 | + }, |
| 92 | + callback: function (scope) {}, |
| 93 | + id: 'display_text_example', |
| 94 | + weight: 100, |
| 95 | + }; |
| 96 | + Blockly.ContextMenuRegistry.registry.register(displayItem); |
| 97 | +} |
| 98 | + |
| 99 | +function registerSeparators() { |
| 100 | + const workspaceSeparator = { |
| 101 | + id: 'workspace_separator', |
| 102 | + scopeType: Blockly.ContextMenuRegistry.ScopeType.WORKSPACE, |
| 103 | + weight: 99, |
| 104 | + separator: true, |
| 105 | + }; |
| 106 | + Blockly.ContextMenuRegistry.registry.register(workspaceSeparator); |
| 107 | + |
| 108 | + const blockSeparator = { |
| 109 | + id: 'block_separator', |
| 110 | + scopeType: Blockly.ContextMenuRegistry.ScopeType.BLOCK, |
| 111 | + weight: 99, |
| 112 | + separator: true, |
| 113 | + }; |
| 114 | + Blockly.ContextMenuRegistry.registry.register(blockSeparator); |
| 115 | +} |
0 commit comments