forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitializeBlockPreview.js
More file actions
36 lines (32 loc) · 1.22 KB
/
initializeBlockPreview.js
File metadata and controls
36 lines (32 loc) · 1.22 KB
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
function update(blockSpace, container, editor) {
try {
// populate with new xml; because we do this every time the editor
// updates, there is the potential for many different kinds of
// errors here as we will end up updating as the levelbuilder is
// typing out the new content. Because of that and the fact that
// this is simply an informative but not functional view, we simply
// catch and ignore all errors.
var xml = Blockly.Xml.textToDom(editor.getValue());
blockSpace.clear();
Blockly.Xml.domToBlockSpace(blockSpace, xml);
} catch (e) {
return;
}
// resize
var metrics = blockSpace.getMetrics();
var height = metrics.contentHeight + metrics.contentTop;
container.style.height = height + "px";
}
module.exports = function (editor, container) {
var xml = Blockly.Xml.textToDom(editor.getValue() || "<xml></xml>");
var blockSpace = Blockly.BlockSpace.createReadOnlyBlockSpace(container, xml, {
noScrolling: true
});
editor.on('update', function () {
update(blockSpace, container, editor);
});
// need to update twice initially to counter Blockly's weird sizing
// requirements
update(blockSpace, container, editor);
update(blockSpace, container, editor);
};