-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4ndyJSON.js
More file actions
221 lines (209 loc) · 6.64 KB
/
4ndyJSON.js
File metadata and controls
221 lines (209 loc) · 6.64 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
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
class FourndyJSON {
constructor() {
this.latestUploadedFile = null;
this.latestFileContent = '';
this.latestFileSizeMB = 0;
this.fileUploaded = false;
this.uploadTimestamp = null;
}
getInfo() {
return {
id: '4ndyJSON',
name: '4ndy JSON',
color1: '#9b4d99', // Dark magenta
color2: '#a957a9',
color3: '#bb6da0',
blocks: [
{
opcode: 'createJSONFile',
blockType: Scratch.BlockType.COMMAND,
text: 'Create a JSON file named [NAME] with the content [CONTENT]',
arguments: {
NAME: { type: Scratch.ArgumentType.STRING, defaultValue: 'example' },
CONTENT: { type: Scratch.ArgumentType.STRING, defaultValue: '{"key":"value"}' },
},
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'importJSONFile',
blockType: Scratch.BlockType.REPORTER,
text: 'Import JSON file',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'parseJSONFile',
blockType: Scratch.BlockType.REPORTER,
text: 'Parse the content of the JSON file',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'fileIsUploaded',
blockType: Scratch.BlockType.BOOLEAN,
text: 'Is a JSON file uploaded?',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'fileIsDownloaded',
blockType: Scratch.BlockType.BOOLEAN,
text: 'Is a JSON file downloaded?',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'deleteFile',
blockType: Scratch.BlockType.COMMAND,
text: 'Delete the uploaded JSON file',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'clearFileData',
blockType: Scratch.BlockType.COMMAND,
text: 'Clear JSON file data (uploaded and downloaded)',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'getFileContent',
blockType: Scratch.BlockType.REPORTER,
text: 'Get the content of the latest uploaded JSON file',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'getFileSize',
blockType: Scratch.BlockType.REPORTER,
text: 'Get the size of the latest uploaded JSON file',
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
},
{
opcode: 'turnIntoJSONAndDownload',
blockType: Scratch.BlockType.COMMAND,
text: 'Turn [DATA] into JSON and download it as [NAME].json',
arguments: {
DATA: { type: Scratch.ArgumentType.STRING, defaultValue: '{"key":"value"}' },
NAME: { type: Scratch.ArgumentType.STRING, defaultValue: 'example' },
},
color1: '#9b4d99',
color2: '#a957a9',
color3: '#bb6da0'
}
]
};
}
createJSONFile(args) {
const { NAME, CONTENT } = args;
const fileName = NAME + '.json';
try {
const blob = new Blob([CONTENT], { type: 'application/json' });
const file = new File([blob], fileName, { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(file);
link.download = fileName;
link.click();
this.latestUploadedFile = fileName;
this.latestFileSizeMB = file.size / (1024 * 1024); // Calculate file size in MB
this.fileUploaded = true;
this.uploadTimestamp = Date.now();
return `JSON file "${fileName}" created and downloaded.`;
} catch (error) {
console.error('Error creating JSON file:', error);
return 'Error creating JSON file.';
}
}
importJSONFile() {
return new Promise((resolve, reject) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
this.latestFileContent = e.target.result;
this.latestFileSizeMB = file.size / (1024 * 1024); // Calculate file size in MB
this.fileUploaded = true;
resolve(file.name); // Return the file name
};
reader.onerror = (error) => {
reject('Error reading JSON file: ' + error);
};
reader.readAsText(file);
}
};
input.click();
});
}
parseJSONFile() {
try {
return JSON.parse(this.latestFileContent);
} catch (error) {
console.error('Error parsing JSON:', error);
return 'Error parsing JSON.';
}
}
fileIsUploaded() {
if (this.fileUploaded && Date.now() - this.uploadTimestamp < 300000) {
return true;
} else {
this.fileUploaded = false;
return false;
}
}
fileIsDownloaded() {
return this.fileUploaded; // This can now be simply checked with fileUploaded
}
deleteFile() {
this.latestUploadedFile = null;
return 'JSON files deleted.';
}
clearFileData() {
this.latestUploadedFile = null;
this.latestFileContent = '';
this.latestFileSizeMB = 0; // Reset file size
return 'All JSON file data cleared.';
}
getFileContent() {
return this.latestFileContent;
}
getFileSize() {
return this.latestFileSizeMB;
}
turnIntoJSONAndDownload(args) {
const { DATA, NAME } = args;
const fileName = NAME + '.json';
try {
const jsonContent = JSON.stringify(JSON.parse(DATA)); // Convert data into JSON format
const blob = new Blob([jsonContent], { type: 'application/json' });
const file = new File([blob], fileName, { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(file);
link.download = fileName;
link.click();
this.latestUploadedFile = fileName;
this.latestFileSizeMB = file.size / (1024 * 1024); // Calculate file size in MB
this.fileUploaded = true;
this.uploadTimestamp = Date.now();
return `JSON data turned into file "${fileName}" and downloaded.`;
} catch (error) {
console.error('Error converting data to JSON:', error);
return 'Error converting data to JSON.';
}
}
}
Scratch.extensions.register(new FourndyJSON());