-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathworkspace.js
241 lines (217 loc) · 6.33 KB
/
workspace.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
const PROCESS_OPERATIONS = require("./operations");
const MainController = require("./src/controller/main");
const WriteImage = require("./src/operator/basic/WriteImage");
var blocklyDiv = document.getElementById("blocklyDiv");
var toolbox = document.getElementById("toolbox");
const mainController = new MainController();
const { dialog } = require("electron").remote;
// workspace configuration
var workspace = Blockly.inject(blocklyDiv, {
toolbox: toolbox,
media: "./node_modules/blockly/media/",
scrollbars: true,
trashcan: true,
sounds: true,
oneBasedIndex: true,
zoom: {
controls: true,
wheel: false,
startScale: 0.7,
maxScale: 2,
minScale: 0.4,
scaleSpeed: 1.2,
},
renderer: "zelos"
});
// enable searching on workspace by using ctrl +f
const workspaceSearch = new WorkspaceSearch(workspace);
workspaceSearch.init();
/**
* @override
* responsible for fixing flyout scale even when you zoom the workspace
*/
Blockly.VerticalFlyout.prototype.getFlyoutScale = function () {
return 0.6;
};
// Coloring the information-pane when you select a block.
workspace.addChangeListener(function (event) {
if (event.type === Blockly.Events.SELECTED) {
document.getElementById("operator_name").innerText = workspace.getBlockById(
event.newElementId
)?.type;
document.getElementById("operator_description").innerText =
workspace.getBlockById(event.newElementId)?.tooltip;
}
if (event.type === Blockly.Events.BLOCK_CREATE) {
mainController.addOperator(
workspace.getBlockById(event.blockId).type,
event.blockId
);
} else if (
event.type === Blockly.Events.BLOCK_DRAG ||
event.type === Blockly.Events.BLOCK_MOVE
) {
if (event?.newParentId && event?.blockId) {
mainController.arrangeBlocks(
workspace.getBlockById(event?.newParentId).type,
workspace.getBlockById(event?.blockId).type
);
}
} else if (event.type === Blockly.Events.BLOCK_CHANGE) {
const blockType = workspace.getBlockById(event.blockId).type;
const paramType = event.name;
const value = event.newValue;
try {
mainController.changeValuesOfBlocks(blockType, paramType, value);
} catch (error) {
showDialog("Error Occured", error.message, "error");
}
} else if (event.type === Blockly.Events.DELETE) {
mainController.deleteBlock(event.blockId);
}
if (event.type === Blockly.Events.SELECTED) {
if (Blockly.selected === null) {
document.getElementById("information-pane").style.backgroundColor =
"#ccc";
document.getElementById("information-pane").style.border =
"3px solid transparent";
} else {
document.getElementById("information-pane").style.backgroundColor =
Blockly.selected.getColour();
document.getElementById("information-pane").style.border =
"3px solid yellow";
}
}
});
/**
* This method handles the undo function
*/
function undo() {
workspace.undo(false);
}
function redo() {
workspace.undo(true);
}
/**
*
* @param {String} title
* @param {String} message
* @param {String} type
*
* This method generates an output dialog according to the params passed
*/
function showDialog(title, message, type) {
dialog.showMessageBox(null, {
title: title,
buttons: ["Dismiss"],
type: type,
message: message,
});
}
// zoom in function used in preview pane by the zooming in icon
function zoomin() {
var myImg = document.getElementById("image-preview");
var currWidth = myImg.clientWidth;
if (currWidth == 2500) return false;
else {
myImg.style.width = currWidth + 100 + "px";
}
}
// zoom out function used in preview pane by the zooming out icon
function zoomout() {
var myImg = document.getElementById("image-preview");
var currWidth = myImg.clientWidth;
if (currWidth == 100) return false;
else {
myImg.style.width = currWidth - 100 + "px";
}
}
/**
* This function computes the image processing
* which are selected by the user
*/
function executeProcess() {
const preview = document.getElementById("image-preview");
try {
mainController.computeAll(preview);
} catch (error) {
console.log(error);
showDialog("Error Occured", error.message, "error");
}
}
/**
* This function is responsible for downloading the output image
* to the user's local machine
*/
function downloadImage() {
const preview = document.getElementById("image-preview");
var link = document.createElement("a");
link.download = "Processed_image.jpg";
link.href = preview.toDataURL();
link.click();
}
/**
* This function is responsible for setting the location of the output image
*/
function setDirectory() {
const location = document.getElementById("outputDirectory");
console.log("Function called");
location.addEventListener("load", (e) => {
console.log("Output location is: ", e);
});
}
function resetWorkspace() {
//Prompt the user to confirm the action
dialog.showMessageBox(null, {
title: "Caution!",
buttons: ["Cancel", "Reset"],
type: "warning",
message: "Please note that resetting the workspace will result in the loss of all unsaved work.",
}).then(result => {
if (result.response === 1) {
//Reset the workspace
workspace.clear();
mainController.resetTheWorkpace();
}
});
}
/**
* This function is responsible for downloading the processed image
*/
function downloadImage() {
const downloadImage = mainController.getProcessedImage();
if(downloadImage != null) {
var link = document.createElement("a");
link.download = "Processed_image.jpg";
link.href = downloadImage;
link.click();
} else {
showDialog("Error", "No processed image found!", "error");
}
}
/**
* This function loads the selected image and
* set the image in the main controller
*/
function loadFile() {
const preview = document.getElementById("input-image"); // image-preview is the element of image will displayed on the preview pane.
const file = document.querySelector("input[type=file]").files[0];
const reader = new FileReader();
var url = "";
reader.addEventListener(
"load",
function () {
// convert image file to base64 string
url = URL.createObjectURL(file);
preview.src = url;
},
false
);
preview.onload = () => {
// Here preview is the main image
mainController.setOriginalImage(preview);
};
if (file) {
reader.readAsDataURL(file);
}
}