forked from google/blockly-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (86 loc) · 2.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Test page for example plugin showing custom tooltip rendering.
*/
/**
* Create and register the custom tooltip rendering function.
* This could be extracted into a plugin if desired.
*/
function initTooltips() {
// Create a custom rendering function. This function will be called whenever
// a tooltip is shown in Blockly. The first argument is the div to render
// the content into. The second argument is the element to show the tooltip
// for.
const customTooltip = function(div, element) {
if (element instanceof Blockly.BlockSvg) {
// You can access the block being moused over.
// Here we get the color of the block to set the background color.
div.style.backgroundColor = element.getColour();
}
const tip = Blockly.Tooltip.getTooltipOfObject(element);
const text = document.createElement('div');
text.textContent = tip;
const container = document.createElement('div');
container.style.display = 'flex';
// Check to see if the custom property we added in the block definition is
// present.
if (element.tooltipImg) {
const img = document.createElement('img');
img.setAttribute('src', element.tooltipImg);
container.appendChild(img);
}
container.appendChild(text);
div.appendChild(container);
};
// Register the function in the Blockly.Tooltip so that Blockly calls it
// when needed.
Blockly.Tooltip.setCustomTooltip(customTooltip);
}
/**
* Create a workspace.
* @param {HTMLElement} blocklyDiv The blockly container div.
* @param {!Blockly.BlocklyOptions} options The Blockly options.
* @return {!Blockly.WorkspaceSvg} The created workspace.
*/
function createWorkspace(blocklyDiv, options) {
const workspace = Blockly.inject(blocklyDiv, options);
initTooltips();
return workspace;
}
document.addEventListener('DOMContentLoaded', function() {
Blockly.Blocks['custom_tooltip_1'] = {
init: function() {
this.appendDummyInput().appendField('This is a test block.');
this.setColour(150);
this.setTooltip('This is a regular tooltip.');
this.setHelpUrl('');
},
};
Blockly.Blocks['custom_tooltip_2'] = {
init: function() {
this.appendDummyInput().appendField('Mouse over me.');
this.setColour(150);
this.setTooltip('Tip: This tooltip has an image.');
// We will check for this property in our custom rendering code.
this.tooltipImg = 'lightbulb.png';
this.setHelpUrl('');
},
};
const defaultOptions = {
toolbox: {
'kind': 'flyoutToolbox',
'contents': [
{'kind': 'block', 'type': 'custom_tooltip_1'},
{'kind': 'block', 'type': 'custom_tooltip_2'},
],
},
};
// createPlayground is from @blockly/dev-tools.
// eslint-disable-next-line no-undef
createPlayground(
document.getElementById('root'), createWorkspace, defaultOptions);
});