-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinject.js
218 lines (205 loc) · 7.56 KB
/
inject.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Visual Blocks Editor
*
* Copyright 2011 Google Inc.
* http://code.google.com/p/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Functions for injecting Blockly into a web page.
* @author fraser@google.com (Neil Fraser)
*/
/**
* Initialise the SVG document with various handlers.
* @param {!Element} container Containing element.
* @param {Object} opt_options Optional dictionary of options.
*/
Blockly.inject = function(container, opt_options) {
if (opt_options) {
Blockly.parseOptions_(opt_options);
}
Blockly.createDom_(container);
Blockly.init_();
};
/**
* Configure Blockly to behave according to a set of options.
* @param {!Object} options Dictionary of options.
* @private
*/
Blockly.parseOptions_ = function(options) {
Blockly.RTL = !!options['rtl'];
Blockly.editable = !options['readOnly'];
Blockly.pathToBlockly = options['path'] || './';
};
/**
* Create the SVG image.
* @param {!Element} container Containing element.
* @private
*/
Blockly.createDom_ = function(container) {
// Find the document for the container.
var doc = container;
while (doc.parentNode) {
doc = doc.parentNode;
}
Blockly.svgDoc = doc;
// Load CSS.
//<link href="blockly.css" rel="stylesheet" type="text/css" />
var link = doc.createElement('link');
link.setAttribute('href', Blockly.pathToBlockly + 'blockly.css');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('onload', 'Blockly.cssLoaded()');
var head = doc.head || doc.getElementsByTagName('head')[0];
if (!head) {
throw 'No head in document.';
}
head.appendChild(link);
// Build the SVG DOM.
/*
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
class="blocklySvg">
...
</svg>
*/
var svg = Blockly.createSvgElement('svg', {
'xmlns': 'http://www.w3.org/2000/svg',
'xmlns:html': 'http://www.w3.org/1999/xhtml',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
'version': '1.1',
'class': 'blocklySvg'
}, null);
/*
<defs>
... filters go here ...
</defs>
*/
var defs = Blockly.createSvgElement('defs', {}, svg);
var filter, feSpecularLighting, feMerge;
/*
<!--
Blocks are highlighted from a light source at the top-left.
In RTL languages we wish to keep this top-left light source.
-->
<filter id="blocklyEmboss">
<feGaussianBlur in="SourceAlpha" stdDeviation="1" result="blur"/>
<feSpecularLighting in="blur" surfaceScale="1" specularConstant="0.5"
specularExponent="10" lighting-color="white"
result="specOut">
<fePointLight x="-5000" y="-10000" z="20000"/>
</feSpecularLighting>
<feComposite in="specOut" in2="SourceAlpha" operator="in"
result="specOut"/>
<feComposite in="SourceGraphic" in2="specOut" operator="arithmetic"
k1="0" k2="1" k3="1" k4="0"/>
</filter>
*/
filter = Blockly.createSvgElement('filter', {id: 'blocklyEmboss'}, defs);
Blockly.createSvgElement('feGaussianBlur',
{'in': 'SourceAlpha', stdDeviation: 1, result: 'blur'}, filter);
feSpecularLighting = Blockly.createSvgElement('feSpecularLighting',
{'in': 'blur', surfaceScale: 1, specularConstant: 0.5,
specularExponent: 10, 'lighting-color': 'white', result: 'specOut'},
filter);
Blockly.createSvgElement('fePointLight',
{x: -5000, y: -10000, z: 20000}, feSpecularLighting);
Blockly.createSvgElement('feComposite',
{'in': 'specOut', in2: 'SourceAlpha', operator: 'in', result: 'specOut'},
filter);
Blockly.createSvgElement('feComposite',
{'in': 'SourceGraphic', in2: 'specOut', operator: 'arithmetic',
k1: 0, k2: 1, k3: 1, k4: 0}, filter);
/*
<filter id="blocklyTrashcanShadowFilter">
<feGaussianBlur in="SourceAlpha" stdDeviation="2" result="blur"/>
<feOffset in="blur" dx="1" dy="1" result="offsetBlur"/>
<feMerge>
<feMergeNode in="offsetBlur"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
*/
filter = Blockly.createSvgElement('filter',
{id: 'blocklyTrashcanShadowFilter'}, defs);
Blockly.createSvgElement('feGaussianBlur',
{'in': 'SourceAlpha', stdDeviation: 2, result: 'blur'}, filter);
Blockly.createSvgElement('feOffset',
{'in': 'blur', dx: 1, dy: 1, result: 'offsetBlur'}, filter);
feMerge = Blockly.createSvgElement('feMerge', {}, filter);
Blockly.createSvgElement('feMergeNode', {'in': 'offsetBlur'}, feMerge);
Blockly.createSvgElement('feMergeNode', {'in': 'SourceGraphic'}, feMerge);
/*
<filter id="blocklyShadowFilter">
<feGaussianBlur stdDeviation="2"/>
</filter>
*/
filter = Blockly.createSvgElement('filter',
{id: 'blocklyShadowFilter'}, defs);
Blockly.createSvgElement('feGaussianBlur', {stdDeviation: 2}, filter);
Blockly.mainWorkspace = new Blockly.Workspace(Blockly.editable);
svg.appendChild(Blockly.mainWorkspace.createDom());
svg.appendChild(Blockly.FieldTextInput.createDom());
Blockly.commentCanvas = Blockly.createSvgElement('g', {}, svg);
if (Blockly.Toolbox && Blockly.editable) {
svg.appendChild(Blockly.Toolbox.createDom());
}
if (Blockly.Mutator && Blockly.editable) {
svg.appendChild(Blockly.Mutator.createDom());
}
Blockly.Tooltip && svg.appendChild(Blockly.Tooltip.createDom());
if (Blockly.editable) {
svg.appendChild(Blockly.FieldDropdown.createDom());
}
if (Blockly.ContextMenu) {
svg.appendChild(Blockly.ContextMenu.createDom());
}
// The SVG is now fuly assembled. Add it to the container.
container.appendChild(svg);
Blockly.svg = svg;
Blockly.svgResize();
};
/**
* Initialise Blockly with various handlers.
* @private
*/
Blockly.init_ = function() {
var doc = Blockly.svgDoc;
Blockly.bindEvent_(window, 'resize', doc, Blockly.svgResize);
// Bind events for scrolling the workspace.
// Most of these events should be bound to the SVG's surface.
// However, 'mouseup' has to be on the whole document so that a block dragged
// out of bounds and released will know that it has been released.
// Also, 'keydown' has to be on the whole document since the browser doesn't
// understand a concept of focus on the SVG image.
Blockly.bindEvent_(Blockly.svg, 'mousedown', null, Blockly.onMouseDown_);
Blockly.bindEvent_(doc, 'mouseup', null, Blockly.onMouseUp_);
Blockly.bindEvent_(Blockly.svg, 'mousemove', null, Blockly.onMouseMove_);
Blockly.bindEvent_(Blockly.svg, 'contextmenu', null, Blockly.onContextMenu_);
Blockly.bindEvent_(doc, 'keydown', null, Blockly.onKeyDown_);
if (Blockly.editable) {
Blockly.Toolbox && Blockly.Toolbox.init();
Blockly.Mutator && Blockly.Mutator.init();
}
Blockly.mainWorkspace.addTrashcan(Blockly.getMainWorkspaceMetrics);
Blockly.mainWorkspace.scrollbar = new Blockly.ScrollbarPair(
Blockly.mainWorkspace.getCanvas(),
Blockly.getMainWorkspaceMetrics, this.setMainWorkspaceMetrics);
// Load the sounds.
Blockly.loadAudio_('click');
Blockly.loadAudio_('delete');
};