Skip to content

Commit 758c924

Browse files
committed
update: 重新加入从电脑打开的选项,并且设置了从电脑打开时的上传到BlockCode逻辑
1 parent 07610bb commit 758c924

File tree

2 files changed

+111
-24
lines changed

2 files changed

+111
-24
lines changed

src/components/menu-bar/menu-bar.jsx

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import {
4242
manualUpdateProject,
4343
requestNewProject,
4444
remixProject,
45-
saveProjectAsCopy
45+
saveProjectAsCopy,
46+
createProject
4647
} from '../../reducers/project-state';
4748
import {
4849
openAboutMenu,
@@ -88,6 +89,8 @@ import oldtimeyLogo from './oldtimey-logo.svg';
8889

8990
import sharedMessages from '../../lib/shared-messages';
9091

92+
import storage from '../../lib/storage.js';
93+
9194
const MenuBarItemTooltip = ({
9295
children,
9396
className,
@@ -163,6 +166,7 @@ class MenuBar extends React.Component {
163166
'handleClickRemix',
164167
'handleClickSave',
165168
'handleClickSaveAsCopy',
169+
'handleClickCreateProject',
166170
'handleSetMode',
167171
'handleKeyPress',
168172
'handleRestoreOption',
@@ -203,6 +207,10 @@ class MenuBar extends React.Component {
203207
this.props.onClickSaveAsCopy();
204208
this.props.onRequestCloseFile();
205209
}
210+
handleClickCreateProject () {
211+
this.props.onClickCreateProject();
212+
this.props.onRequestCloseFile();
213+
}
206214
handleSetMode (mode) {
207215
return () => {
208216
// Turn on/off filters for modes.
@@ -440,18 +448,44 @@ class MenuBar extends React.Component {
440448
}
441449
>
442450
<MenuSection>
443-
<MenuItem
444-
isRtl={this.props.isRtl}
445-
onClick={this.handleClickSave}
446-
>
447-
保存到 BlockCode
448-
</MenuItem>
449-
<MenuItem
450-
isRtl={this.props.isRtl}
451-
onClick={this.handleClickSaveAsCopy}
452-
>
453-
另存为新作品到 BlockCode
454-
</MenuItem>
451+
{storage.reduxStore.getState()
452+
.scratchGui.projectState
453+
.loadingState ===
454+
"SHOWING_WITH_ID" && (
455+
<MenuItem
456+
isRtl={this.props.isRtl}
457+
onClick={this.handleClickSave}
458+
>
459+
保存到 BlockCode
460+
</MenuItem>
461+
)}
462+
{storage.reduxStore.getState()
463+
.scratchGui.projectState
464+
.loadingState ===
465+
"SHOWING_WITH_ID" && (
466+
<MenuItem
467+
isRtl={this.props.isRtl}
468+
onClick={
469+
this.handleClickSaveAsCopy
470+
}
471+
>
472+
另存为新作品到 BlockCode
473+
</MenuItem>
474+
)}
475+
{storage.reduxStore.getState()
476+
.scratchGui.projectState
477+
.loadingState ===
478+
"SHOWING_WITHOUT_ID" && (
479+
<MenuItem
480+
isRtl={this.props.isRtl}
481+
onClick={
482+
this
483+
.handleClickCreateProject
484+
}
485+
>
486+
保存此作品到 BlockCode
487+
</MenuItem>
488+
)}
455489
</MenuSection>
456490
{(this.props.canSave ||
457491
this.props.canCreateCopy ||
@@ -488,6 +522,16 @@ class MenuBar extends React.Component {
488522
</MenuSection>
489523
)}
490524
<MenuSection>
525+
<MenuItem
526+
onClick={
527+
this.props
528+
.onStartSelectingFileUpload
529+
}
530+
>
531+
{this.props.intl.formatMessage(
532+
sharedMessages.loadFromComputerTitle
533+
)}
534+
</MenuItem>
491535
<SB3Downloader>
492536
{(
493537
className,
@@ -859,6 +903,7 @@ MenuBar.propTypes = {
859903
onClickRemix: PropTypes.func,
860904
onClickSave: PropTypes.func,
861905
onClickSaveAsCopy: PropTypes.func,
906+
onClickCreateProject: PropTypes.func,
862907
onClickSettings: PropTypes.func,
863908
onLogOut: PropTypes.func,
864909
onOpenRegistration: PropTypes.func,
@@ -954,6 +999,7 @@ const mapDispatchToProps = dispatch => ({
954999
onClickRemix: () => dispatch(remixProject()),
9551000
onClickSave: () => dispatch(manualUpdateProject()),
9561001
onClickSaveAsCopy: () => dispatch(saveProjectAsCopy()),
1002+
onClickCreateProject: () => dispatch(createProject()),
9571003
onSeeCommunity: () => dispatch(setPlayer(true)),
9581004
onSetTimeTravelMode: mode => dispatch(setTimeTravel(mode))
9591005
});

src/lib/project-saver-hoc.jsx

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,59 @@ const ProjectSaverHOC = function (WrappedComponent) {
227227
}
228228
return this.props.onUpdatedProject(this.props.loadingState);
229229
}
230-
createNewProjectToStorage() {
231-
return this.storeProject(null)
232-
.then((response) => {
233-
this.props.onCreatedProject(
234-
response.id.toString(),
235-
this.props.loadingState
236-
);
230+
async createNewProjectToStorage() {
231+
const {
232+
data: {
233+
session: { user },
234+
},
235+
} = await supabase.auth.getSession();
236+
const fileName = this.props.reduxProjectTitle;
237+
const uuid = uuidv4();
238+
const filePath = `${user.id}/${uuid}.sb3`;
239+
const thumbnailPath = `${user.id}/${uuid}.png`;
240+
const blob = await this.props.vm.saveProjectSb3();
241+
// 上传sb3
242+
const { error: uploadError } = await supabase.storage
243+
.from("files")
244+
.upload(filePath, blob);
245+
if (uploadError) {
246+
this.props.onShowAlert("savingError");
247+
return this.props.onProjectError(errorData);
248+
}
249+
// 上传缩略图
250+
this.getProjectThumbnail(async (dataURI) => {
251+
const blob = dataURItoBlob(dataURI);
252+
const { data: updateData, error: updateError } =
253+
await supabase.storage
254+
.from("files")
255+
.upload(thumbnailPath, blob);
256+
if (updateError) {
257+
this.props.onShowAlert("savingError");
258+
return this.props.onProjectError(errorData);
259+
}
260+
});
261+
// 上传record
262+
const { data: insertData, error: insertError } = await supabase
263+
.from("files")
264+
.insert({
265+
user_id: user.id, // ⚠️ 必须是 auth 用户 id
266+
file_name: fileName,
267+
file_path: filePath,
268+
thumbnail_path: thumbnailPath,
237269
})
238-
.catch((err) => {
239-
this.props.onShowAlert("creatingError");
240-
this.props.onProjectError(err);
241-
});
270+
.select();
271+
if (insertError) {
272+
this.props.onShowAlert("savingError");
273+
return this.props.onProjectError(errorData);
274+
}
275+
// 打开新的文件
276+
const {
277+
data: { signedUrl: fileUrl },
278+
} = await supabase.storage
279+
.from("files")
280+
.createSignedUrl(insertData[0].file_path, 60 * 60);
281+
localStorage.setItem("project-id", insertData[0].id);
282+
this.props.onCreatedProject(fileUrl, this.props.loadingState);
242283
}
243284
async createCopyToStorage() {
244285
const {

0 commit comments

Comments
 (0)