forked from gvwilson/tidyblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (69 loc) · 2.13 KB
/
Copy pathindex.js
File metadata and controls
80 lines (69 loc) · 2.13 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
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
'use strict'
const React = require('react') // eslint-disable-line no-unused-vars
const ReactDOM = require('react-dom')
const Blockly = require('blockly/blockly_compressed')
const blocks = require('./blocks/blocks')
const Env = require('./libs/env')
const UserInterface = require('./libs/gui')
const TidyBlocksApp = require('./libs/ui/ui').TidyBlocksApp // eslint-disable-line no-unused-vars
/**
* Define the bridge between React and the rest of our code. Encapsulating this
* here means that our tests don't have to depend on React.
*/
class ReactInterface extends UserInterface {
/**
* Build user interface object.
* @param {string} language What language to use for localizing blocks.
* @param {string} rootId HTML ID of root element.
*/
constructor (language, rootId) {
super(language)
// Create the Blockly settings.
const settings = this._createSettings()
// Create an environment so the React app can get the pre-loaded datasets.
const env = new Env(this)
// Render React.
const app = ReactDOM.render(
<TidyBlocksApp settings={settings} toolbox={blocks.XML_CONFIG} initialEnv={env}/>,
document.getElementById(rootId)
)
// Save a reference to the workspace (needed in the parent class).
this.workspace = app.getWorkspace().state.workspace
}
/**
* Get the XML representation of the workspace.
*/
getXML () {
const xml = Blockly.Xml.workspaceToDom(this.workspace)
return Blockly.Xml.domToText(xml)
}
/**
* Create the JSON settings used to initialize the workspace.
* @returns JSON settings object.
*/
_createSettings () {
return {
theme: blocks.THEME,
zoom: {
controls: true,
wheel: true,
startScale: 1.0,
maxScale: 3,
minScale: 0.3,
scaleSpeed: 1.2
},
renderer: 'thrasos'
}
}
}
/**
* Initialize the interface.
* @param {string} language What language to use for localizing blocks.
* @param {string} rootId HTML ID of element that will contain workspace.
*/
const setup = (language, rootId) => {
return new ReactInterface(language, rootId)
}
module.exports = {
setup
}