-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathblock_mirror.js
244 lines (198 loc) · 6.78 KB
/
block_mirror.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
External visible stuff
Changing mode/code can fail on the block side
setMode(mode) -> bool
setCode(filename, code) -> bool
setHighlight(line) -> bool
setReadOnly(isReadOnly)
setToolbox(string)
'basic'
'advanced'
'complete'
list[list[string]]
onChange(event) ->
onModeChange
onCodeChange
getCode() -> string
getMode() -> string
getImage(callback)
lastBlockConversionFailure: {} or null
*/
/**
*
*/
function BlockMirror(configuration) {
this.validateConfiguration(configuration);
this.initializeVariables();
if (!this.configuration.skipSkulpt) {
this.loadSkulpt();
}
this.textToBlocks = new BlockMirrorTextToBlocks(this);
this.textEditor = new BlockMirrorTextEditor(this);
this.blockEditor = new BlockMirrorBlockEditor(this);
this.setMode(this.configuration.viewMode);
}
BlockMirror.prototype.validateConfiguration = function (configuration) {
this.configuration = {};
// Container
if ('container' in configuration) {
this.configuration.container = configuration.container;
} else {
throw new Error('Invalid configuration: Missing "container" property.')
}
// blocklyPath
if ('blocklyMediaPath' in configuration) {
this.configuration.blocklyMediaPath = configuration.blocklyMediaPath;
} else {
this.configuration.blocklyMediaPath = '../../blockly/media/';
}
// Run function
if ('run' in configuration) {
this.configuration.run = configuration.run;
} else {
this.configuration.run = function () {
console.log('Ran!');
};
}
// readOnly
this.configuration['readOnly'] = configuration['readOnly'] || false;
// height
this.configuration.height = configuration.height || 500;
// viewMode
this.configuration.viewMode = configuration.viewMode || 'split';
// Need to load skulpt?
this.configuration.skipSkulpt = configuration.skipSkulpt || false;
// Delay?
this.configuration.blockDelay = configuration.blockDelay || false;
// Toolbox
this.configuration.toolbox = configuration.toolbox || "normal";
this.configuration.renderer = configuration.renderer || 'Thrasos';
// Convert image URLs?
this.configuration.imageUploadHook = configuration.imageUploadHook || (old => Promise.resolve(old));
this.configuration.imageDownloadHook = configuration.imageDownloadHook || (old => old);
this.configuration.imageLiteralHook = configuration.imageLiteralHook || (old => old);
this.configuration.imageDetection = configuration.imageDetection || 'string';
this.configuration.imageMode = configuration.imageMode || false;
};
BlockMirror.prototype.initializeVariables = function () {
this.tags = {
'toolbar': document.createElement('div'),
'blockContainer': document.createElement('div'),
'blockEditor': document.createElement('div'),
'blockArea': document.createElement('div'),
'textSidebar': document.createElement('div'),
'textContainer': document.createElement('div'),
'textArea': document.createElement('textarea'),
};
// Toolbar
this.configuration.container.appendChild(this.tags.toolbar);
// Block side
this.configuration.container.appendChild(this.tags.blockContainer);
this.tags.blockContainer.appendChild(this.tags.blockEditor);
this.tags.blockContainer.appendChild(this.tags.blockArea);
// Text side
this.configuration.container.appendChild(this.tags.textContainer);
this.tags.textContainer.appendChild(this.tags.textSidebar);
this.tags.textContainer.appendChild(this.tags.textArea);
for (let name in this.tags) {
this.tags[name].style['box-sizing'] = 'border-box';
}
// Files
this.code_ = "";
this.mode_ = null;
// Update Flags
this.silenceBlock = false;
this.silenceBlockTimer = null;
this.silenceText = false;
this.silenceModel = 0;
this.blocksFailed = false;
this.blocksFailedTimeout = null;
this.triggerOnChange = null;
this.firstEdit = true;
// Toolbox width
this.blocklyToolboxWidth = 0;
// Listeners
this.listeners_ = [];
};
BlockMirror.prototype.loadSkulpt = function () {
Sk.configure({
__future__: Sk.python3,
read: function (filename) {
if (Sk.builtinFiles === undefined ||
Sk.builtinFiles["files"][filename] === undefined) {
throw "File not found: '" + filename + "'";
}
return Sk.builtinFiles["files"][filename];
}
});
};
BlockMirror.prototype.removeAllChangeListeners = function () {
this.listeners_.length = 0;
};
BlockMirror.prototype.removeChangeListener = function (callback) {
let index = this.listeners_.indexOf(callback);
if (index !== -1) {
this.listeners_.splice(index, 1);
}
};
BlockMirror.prototype.addChangeListener = function (callback) {
this.listeners_.push(callback);
};
BlockMirror.prototype.fireChangeListener = function (event) {
for (let i = 0, func; func = this.listeners_[i]; i++) {
func(event);
}
};
BlockMirror.prototype.setCode = function (code, quietly) {
this.code_ = code;
if (!quietly) {
this.blockEditor.setCode(code, true);
this.textEditor.setCode(code, true);
}
this.fireChangeListener({'name': 'changed', 'value': code});
};
BlockMirror.prototype.getCode = function () {
return this.code_;
};
BlockMirror.prototype.getMode = function () {
return this.mode_;
};
BlockMirror.prototype.setMode = function (mode) {
this.mode_ = mode;
this.blockEditor.setMode(mode);
this.textEditor.setMode(mode);
};
BlockMirror.prototype.setImageMode = function (imageMode) {
this.configuration.imageMode = imageMode;
if (imageMode) {
this.textEditor.enableImages();
} else {
this.textEditor.disableImages();
}
console.log(imageMode);
};
BlockMirror.prototype.setReadOnly = function (isReadOnly) {
this.textEditor.setReadOnly(isReadOnly);
this.blockEditor.setReadOnly(isReadOnly);
$(this.configuration.container).toggleClass("block-mirror-read-only", isReadOnly);
};
BlockMirror.prototype.refresh = function() {
this.blockEditor.resized();
this.textEditor.codeMirror.refresh();
};
BlockMirror.prototype.forceBlockRefresh = function() {
this.blockEditor.setCode(this.code_, true);
};
BlockMirror.prototype.VISIBLE_MODES = {
'block': ['block', 'split'],
'text': ['text', 'split']
};
BlockMirror.prototype.BREAK_WIDTH = 675;
BlockMirror.prototype.setHighlightedLines = function(lines, style) {
this.textEditor.setHighlightedLines(lines, style);
//this.blockEditor.highlightLines(lines, style);
};
BlockMirror.prototype.clearHighlightedLines = function(style=null) {
this.textEditor.clearHighlightedLines(style);
//this.blockEditor.unhighlightLines(lines, style);
};