当前位置:网站首页>Blockly learning ----1 Work area, block, toolbox
Blockly learning ----1 Work area, block, toolbox
2022-06-13 04:33:00 【gohxc】
blockly Study
1. source :https://developers.google.com/blockly/guides/get-started/web
stay github Download the source code https://github.com/google/blockly
2. A simple example
1. Fixed width and height
<script src="blockly_compressed.js"></script>
<script src="blocks_compressed.js"></script>
<script src="msg/js/en.js"></script>
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
<xml id="toolbox" style="display: none">
<block type="controls_if"></block>
<block type="controls_repeat_ext"></block>
<block type="logic_compare"></block>
<block type="math_number"></block>
<block type="math_arithmetic"></block>
<block type="text"></block>
<block type="text_print"></block>
</xml>
<script>
var workspace = Blockly.inject('blocklyDiv',
{toolbox: document.getElementById('toolbox')});
</script>
2. Change the width and height
<div id="blocklyDiv" style="position: absolute"></div>
<script>
var blocklyDiv = document.getElementById('blocklyDiv');
var workspace = Blockly.inject(blocklyDiv,
{toolbox: document.getElementById('toolbox')});
var onresize = function(e) {
blocklyDiv.style.left = '100px';
blocklyDiv.style.top = '100px';
blocklyDiv.style.width = '500px';
blocklyDiv.style.height = '300px';
Blockly.svgResize(workspace);
};
window.addEventListener('resize', onresize, false);
onresize();
Blockly.svgResize(workspace);
</script>
3. Custom blocks
//json Express
Blockly.Blocks['string_length'] = {
init: function() {
this.jsonInit({
"message0": 'length of %1',
"args0": [
{
"type": "input_value",
"name": "VALUE",
"check": "String"
}
],
"output": "Number",
"colour": 160,
"tooltip": "Returns number of letters in the provided text.",
"helpUrl": "http://www.w3schools.com/jsref/jsref_length_string.asp"
});
}
};
//javascript Express
Blockly.Blocks['string_length'] = {
init: function() {
this.appendValueInput('VALUE')
.setCheck('String')
.appendField('length of');
this.setOutput(true, 'Number');
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
Add to tool
<xml id="toolbox" style="display: none">
<category name="Text">
<block type="string_length"></block>
</category>
...
</xml>
Add custom functions
Blockly.JavaScript['text_length'] = function(block) {
// String or array length.
var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '\'\'';
return [argument0 + '.length', Blockly.JavaScript.ORDER_MEMBER];
};
4. hold-all
use js Represents the toolbox
<script>
var toolbox = '<xml>';
toolbox += ' <block type="controls_if"></block>';
toolbox += ' <block type="controls_whileUntil"></block>';
toolbox += '</xml>';
var workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
</script>
Tool classification Categories
<xml id="toolbox" style="display: none">
<category name="Control">
<block type="controls_if"></block>
<block type="controls_whileUntil"></block>
<block type="controls_for">
</category>
<category name="Logic">
<block type="logic_compare"></block>
<block type="logic_operation"></block>
<block type="logic_boolean"></block>
</category>
</xml>
Custom color colour from 0-360
<xml id="toolbox" style="display: none">
<category name="Logic" colour="210">...</category>
<category name="Loops" colour="120">...</category>
<category name="Math" colour="230">...</category>
<category name="Colour" colour="20">...</category>
<category name="Variables" colour="330" custom="VARIABLE"></category>
<category name="Functions" colour="290" custom="PROCEDURE"></category>
</xml>
Customize classification categories
<category name="Variables" custom="VARIABLE"></category>
<category name="Functions" custom="PROCEDURE"></category>
Such as custom color classification
// Classification tools xml
<category name="Colours" custom="COLOUR_PALETTE"></category>
// Implementation of custom color group
var myApplication={};
myApplication.getPalette=function(){
return ["#FF0000","#00FF00"];
};
myApplication.coloursFlyoutCallback = function(workspace) {
// Returns an array of hex colours, e.g. ['#4286f4', '#ef0447']
var colourList = myApplication.getPalette();
var xmlList = [];
if (Blockly.Blocks['colour_picker']) {
for (var i = 0; i < colourList.length; i++) {
var blockText = '<xml>' +
'<block type="colour_picker">' +
'<field name="COLOUR">' + colourList[i] + '</field>' +
'</block>' +
'</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
xmlList.push(block);
}
}
return xmlList;
};
// Under registration
demoWorkspace.registerToolboxCategoryCallback(
'COLOUR_PALETTE', myApplication.coloursFlyoutCallback);
A tree representation of a classification
<xml id="toolbox" style="display: none">
<category name="Core">
<category name="Control">
<block type="controls_if"></block>
<block type="controls_whileUntil"></block>
</category>
<category name="Logic">
<block type="logic_compare"></block>
<block type="logic_operation"></block>
<block type="logic_boolean"></block>
</category>
</category>
<category name="Custom">
<block type="start"></block>
<category name="Move">
<block type="move_forward"></block>
<block type="move_backward"></block>
</category>
<category name="Turn">
<block type="turn_left"></block>
<block type="turn_right"></block>
</category>
</category>
</xml>
Block group
<xml id="toolbox" style="display: none">
<block type="logic_boolean"></block>
<block type="math_number">
<field name="NUM">42</field>
</block>
<block type="controls_for">
<value name="FROM">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
<value name="BY">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="B">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
</xml>
Shadow block shadow
Shadow blocks are placeholder blocks , Can perform a variety of functions :
- They indicate the default value of their parent block .
- They allow users to type values directly , Without getting a number or string block .
- Unlike regular blocks , If you place a block on it , Will replace them .
- They tell the user the expected value type .
You cannot build shadow blocks directly from code applications . contrary , Normal blocks can be used , And change the <block …> And in XML in <shadow …> and .
Be careful : Shadow blocks may not contain variable fields or have children that are not shadows .
separator
Adding a tag between any two categories creates a separator .
By default , Each block is separated from its neighbors 24 Pixel . have access to ’gap’ Property to change this separator , This attribute replaces the default gap .<sep gap="32"></sep>
Buttons and labels
<xml id="toolbox" style="display: none">
<block type="logic_operation"></block>
<label text="A label" web-class="myLabelStyle"></label>
<label text="Another label"></label>
<block type="logic_negate"></block>
<button text="A button" callbackKey="myFirstButtonPressed"></button>
<block type="logic_boolean"></block>
</xml>
<style>
.myLabelStyle>.blocklyFlyoutLabelText {
font-style: italic;
fill: green;
}
</style>
The button should have a callback function ; Labels should not . To set a callback for a given button , Please click on the , Use
yourWorkspace.registerButtonCallback(yourCallbackKey, yourFunction).
Disable block disabled
<block type="math_single" disabled="true"></block>
Change toolbox
Applications can change the blocks available in the toolkit at any time through a single function call :workspace.updateToolbox(newTree);
The limitation is that the mode cannot be changed ;
Multi Tool Demo :
https://blockly-demo.appspot.com/static/demos/toolbox/index.html
边栏推荐
猜你喜欢
一款开源的Markdown转富文本编辑器的实现原理剖析
R: Employee turnover forecast practice
CTFSHOW SQL注入篇(231-253)
Redis data persistence
Express framework knowledge - Art template template, cookie, session
小程序基础入门(黑马学习笔记)
CTFSHOW SQL注入篇(211-230)
120. triangle minimum path sum - Dynamic Planning
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
Reread the classic: end to end object detection with transformers
随机推荐
Common terms of electromagnetic compatibility
Idea Download
Ladder race
如何优雅的处理async/await错误信息
PAT 1054 The Dominant Color
Applet version update
Knife4j aggregation 2.0.9 supports automatic refresh of routing documents
2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
Introduction to RFM analysis
Google Chrome browser reports an error: net:: err_ BLOCKED_ BY_ CLIENT
A simple understanding of consistent hash
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
Ctfshow common postures (821-830)
Sword finger offer 56 - I. number of occurrences in the array
Day 007: go language string
CTFSHOW SQL注入篇(231-253)
力扣刷题338.比特位计数
The could not find com scwang. smart:refresh-layout-kernel:2.0.3. Required by: project: the app cannot load the third-party package
Dagger2学习之Module的应用(二)
Uni app dynamic add style dynamic bind background image invalid