Skip to content

Commit 6c4a1b3

Browse files
committed
加载上传项目
1 parent 238a2ef commit 6c4a1b3

File tree

9 files changed

+141
-32
lines changed

9 files changed

+141
-32
lines changed

src/editor/ScratchJr.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let layerAboveBottom = 4;
4747
let dragginLayer = 7000;
4848

4949
let currentProject = undefined; //项目ID
50+
let filepath = undefined;
5051
let editmode;
5152

5253
let isDebugging = false;
@@ -128,6 +129,14 @@ export default class ScratchJr {
128129
return currentProject;
129130
}
130131

132+
static set currentProject(v){
133+
currentProject = v;
134+
}
135+
136+
static get filepath(){
137+
return filepath;
138+
}
139+
131140
static get editmode () {
132141
return editmode;
133142
}
@@ -190,6 +199,9 @@ export default class ScratchJr {
190199
Project.loadIcon.src = 'assets/loading.png';
191200
ScratchJr.log('blocks init', ScratchJr.getTime(), 'sec', BlockSpecs.loadCount);
192201
currentProject = urlvars.pmd5;
202+
if("filepath" in urlvars){
203+
filepath = urlvars.filepath
204+
}
193205
editmode = urlvars.mode;
194206
libInit();
195207
Project.init();
@@ -203,7 +215,7 @@ export default class ScratchJr {
203215
runtime = new Runtime();
204216
Undo.init();
205217
ScratchJr.editorEvents();
206-
Project.load(currentProject);
218+
Project.load();
207219
Events.init();
208220
if (window.Settings.autoSaveInterval > 0) {
209221
autoSaveSetInterval = window.setInterval(function () {
@@ -214,6 +226,27 @@ export default class ScratchJr {
214226
}
215227
}, window.Settings.autoSaveInterval);
216228
}
229+
230+
window.scratchjr = {}
231+
window.scratchjr.projectName = IO.zipFileName
232+
//获取sjr项目
233+
window.scratchjr.getProjectSjr = function(cb){
234+
let md5 = ScratchJr.currentProject + ""
235+
Project.prepareToSave(md5, function () {
236+
IO.zipProject(md5, function (contents) {
237+
ScratchJr.onHold = false;
238+
cb(contents, IO.zipFileName)
239+
});
240+
});
241+
}
242+
//载入网络sjr项目
243+
window.scratchjr.loadProjectSjr = function(url){
244+
Project.downloadProject(url, (md5)=>{
245+
// window.location.href = 'editor.html?pmd5=' + md5 + '&mode=edit';
246+
ScratchJr.currentProject = md5
247+
IO.getObject(md5+"", Project.dataRecieved);
248+
});
249+
}
217250
}
218251

219252
// Event handler for when a story is started
@@ -406,7 +439,7 @@ export default class ScratchJr {
406439
}
407440
}
408441

409-
if (!currentProject) {
442+
if (!currentProject && !filepath) {
410443
return 'home.html?place=home';
411444
}
412445

src/editor/ui/Project.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ export default class Project {
8383
ScratchJr.log('all UI assets recieved - procced to call server', ScratchJr.getTime(), 'sec');
8484
Project.setProgress(20);
8585
UI.layout();
86-
IO.getObject(ScratchJr.currentProject, Project.dataRecieved);
86+
if(ScratchJr.filepath){
87+
Project.downloadProject(ScratchJr.filepath, (md5)=>{
88+
// window.location.href = 'editor.html?pmd5=' + md5 + '&mode=edit';
89+
ScratchJr.currentProject = md5
90+
history.replaceState(null,document.title,location.href.split("?")[0] + '?pmd5=' + md5 + '&mode=edit');
91+
IO.getObject(md5+"", Project.dataRecieved);
92+
});
93+
}else{
94+
IO.getObject(ScratchJr.currentProject, Project.dataRecieved);
95+
}
8796
}
8897

8998
static dataRecieved (str) {
@@ -323,6 +332,30 @@ export default class Project {
323332
// load project data
324333
//////////////////////////////////////////////////
325334

335+
static downloadProject(filepath, cb){
336+
var xhr = new XMLHttpRequest();
337+
xhr.open("GET", filepath);
338+
xhr.responseType = "arraybuffer";
339+
xhr.onload = function(){
340+
if(xhr.readyState == 4 && xhr.status == 200){
341+
var req = xhr.response;
342+
console.log("download finished");
343+
IO.loadProjectFromSjr(req, (md5)=>{
344+
cb(md5)
345+
})
346+
} else if(xhr.readyState == 4 && xhr.status == 404){
347+
cb(null);
348+
};
349+
}
350+
xhr.upload.onprogress = function(evt){
351+
var loaded = evt.loaded;
352+
var tot = evt.total;
353+
var per = Math.floor(100*loaded/tot);
354+
console.log("download progress:" + per);
355+
}
356+
xhr.send()
357+
}
358+
326359
static recreate (data) {
327360
ScratchJr.log('Project data structures start loading', ScratchJr.getTime(), 'sec');
328361
mediaCount = 0;

src/editor/ui/UI.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,26 +196,33 @@ export default class UI {
196196
});
197197

198198
onTouchStartBind(saveCloudButton,function (e) {
199-
alert("未对接API")
200-
return;
201199
UI.handleTextFieldSave(true);
202-
203-
// Save any changes made to the project
204200
ScratchJr.onHold = true; // Freeze the editing UI
205201
ScratchJr.stopStripsFromTop(e);
206-
207202
Project.prepareToSave(ScratchJr.currentProject, function () {
208-
// Alert.close();
209-
// Package the project as a .sjr file
210203
IO.zipProject(ScratchJr.currentProject, function (contents) {
211-
212-
ScratchJr.onHold = false; // Unfreeze the editing UI
213-
// var emailSubject = Localization.localize('SHARING_EMAIL_SUBJECT', {
214-
// PROJECT_NAME: IO.shareName
215-
// });
216-
// OS.sendSjrToShareDialog(IO.zipFileName, emailSubject, Localization.localize('SHARING_EMAIL_TEXT'),
217-
// shareType, contents);
218-
204+
var form = new FormData();
205+
contents.name = IO.zipFileName + ".sjr";
206+
form.append("file", contents);
207+
form.append("biz", "sjr")
208+
// XMLHttpRequest 对象
209+
var xhr = new XMLHttpRequest();
210+
xhr.open("POST", "/api/sys/common/upload", true);
211+
xhr.onload = function () {
212+
if(xhr.readyState == 4 && xhr.status == 200){
213+
var req = xhr.responseText;
214+
} else if(xhr.readyState == 4 && xhr.status == 404){
215+
return;
216+
};
217+
};
218+
xhr.upload.onprogress = function(evt){
219+
var loaded = evt.loaded;
220+
var tot = evt.total;
221+
var per = Math.floor(100*loaded/tot);
222+
console.log("upload progress:" + per);
223+
}
224+
xhr.send(form)
225+
ScratchJr.onHold = false;
219226
shareLoadingGif.style.visibility = 'hidden';
220227
});
221228
});
@@ -323,7 +330,6 @@ export default class UI {
323330

324331
// Package the project as a .sjr file
325332
IO.zipProject(ScratchJr.currentProject, function (contents) {
326-
console.log("save file")
327333
FileSaver.saveAs(contents, IO.zipFileName)
328334
ScratchJr.onHold = false; // Unfreeze the editing UI
329335
// var emailSubject = Localization.localize('SHARING_EMAIL_SUBJECT', {

src/lobby/Home.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export default class Home {
4141
static openProjectThumbnail (parent) {
4242
var tb = newHTML('div', 'projectthumb', parent);
4343
newHTML('div', 'aproject open', tb);
44+
var fileinput = newHTML('input', "fileinput", tb);
45+
fileinput.setAttribute("type", "file")
46+
fileinput.setAttribute("id", "fileinput");
47+
fileinput.setAttribute("accept", ".sjr")
48+
fileinput.setAttribute("style",'visibility:hidden');
4449
tb.id = 'openproject';
4550
}
4651

@@ -171,7 +176,21 @@ export default class Home {
171176
}
172177

173178
static openProject(){
174-
alert("暂未实现")
179+
document.getElementById('fileinput').addEventListener('change', e => {
180+
const reader = new FileReader();
181+
const thisFileInput = e.target;
182+
reader.onload = () => {
183+
console.log(reader);
184+
IO.loadProjectFromSjr(reader.result, (md5)=>{
185+
console.log("load project:" + md5);
186+
OS.setfile('homescroll.sjr', gn('wrapc').scrollTop, function () {
187+
window.location.href = 'editor.html?pmd5=' + md5 + '&mode=edit';
188+
});
189+
})
190+
};
191+
reader.readAsArrayBuffer(thisFileInput.files[0]);
192+
});
193+
document.getElementById('fileinput').click();
175194
}
176195

177196
static createNewProject () {
@@ -198,7 +217,7 @@ export default class Home {
198217
var pn = [];
199218
var div = gn('scrollarea');
200219
for (var i = 0; i < div.childElementCount; i++) {
201-
if (div.childNodes[i].id == 'newproject') {
220+
if (div.childNodes[i].id == 'newproject' || div.childNodes[i].id == 'openproject') {
202221
continue;
203222
}
204223
pn.push(div.childNodes[i].childNodes[1].childNodes[0].textContent);
@@ -263,7 +282,7 @@ export default class Home {
263282
div.removeChild(div.childNodes[0]);
264283
}
265284
Home.emptyProjectThumbnail(div);
266-
//Home.openProjectThumbnail(div);
285+
Home.openProjectThumbnail(div);
267286
for (var i = 0; i < data.length; i++) {
268287
Home.addProjectLink(div, data[i]);
269288
}

src/tablet/IO.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,16 +524,18 @@ export default class IO {
524524
}
525525

526526
// Receive a base64-encoded zip from iOS (upon open a project)
527-
static loadProjectFromSjr (b64data) {
527+
static loadProjectFromSjr (b64data, callback) {
528528
// Together, these two provide a "progress" indication
529529
// that lets us know when to refresh the lobby (when sE/sA = 1)
530530
var saveExpected = 0; // How many assets we expect to save - updated as we process the zip
531531
var saveActual = 0; // How many assets actually saved - updated as we make IO saves
532+
var projectMd5 = null;
532533

533-
var receivedZip = JSZip();
534-
receivedZip.load(b64data, {
535-
'base64': true
536-
});
534+
var receivedZip = JSZip(b64data);
535+
// var receivedZip = JSZip();
536+
// receivedZip.load(b64data, {
537+
// 'base64': true
538+
// });
537539

538540
// To store character MD5 -> character name map
539541
// The character name is stored in the project JSON; when we load
@@ -559,7 +561,9 @@ export default class IO {
559561

560562
IO.uniqueProjectName(jsonData, function (jsonData) {
561563
jsonData.isgift = '1'; // Project will display with a bow and ribbon
562-
IO.createProject(jsonData, function () {});
564+
IO.createProject(jsonData, function (md5) {
565+
projectMd5 = md5
566+
});
563567
});
564568

565569
// Build map of character filename -> character name
@@ -714,6 +718,13 @@ export default class IO {
714718
if (gn('hometab') !== null) { // Check if we're on the lobby page
715719
if (saveActual == saveExpected) {
716720
Lobby.setPage('home');
721+
callback(projectMd5)
722+
} else { // Waiting for assets to be saved
723+
setTimeout(refreshLobby, 100);
724+
}
725+
}else{
726+
if (saveActual == saveExpected) {
727+
callback(projectMd5)
717728
} else { // Waiting for assets to be saved
718729
setTimeout(refreshLobby, 100);
719730
}

static/assets/lobby/back.png

14.1 KB
Loading

static/assets/lobby/cef_debug.log

Whitespace-only changes.

static/css/lobby.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ div.frame {
6262
}
6363

6464
#topbar ul.topbar-nav li.logo {
65-
width: ${css_vh(32)};
65+
width: ${css_vh(12)};
6666
}
6767

6868
#topbar ul.topbar-nav .logo .logo-icon {
6969
display: block;
70-
width: ${css_vh(28.65)};
70+
width: ${css_vh(10)};
7171
height: ${css_vh(10.03)};
72-
background: url('../assets/lobby/scratchJrlogo.svg');
72+
background: url('../assets/lobby/back.png');
7373
background-size: 101%;
7474
padding: 0;
7575
margin: auto auto;

webpack.config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ module.exports = {
1010
devServer: {
1111
contentBase: path.resolve(__dirname, 'static'),
1212
host: '127.0.0.1',
13-
port: process.env.PORT || 8080
13+
port: process.env.PORT || 8601,
14+
proxy: {
15+
'/api': {
16+
target: 'http://localhost:8080',
17+
ws: false,
18+
changeOrigin: true
19+
},
20+
}
1421
},
1522
output: {
1623
path: __dirname + '/static',

0 commit comments

Comments
 (0)