Scratch3.0初始化加載七牛云上的sbs文件的方法
下面通過代碼介紹下Scratch3.0初始化加載七牛云上的sbs文件,代碼如下所示:
編寫組件
import PropTypes from 'prop-types'; import React from 'react'; import {connect} from 'react-redux'; import {injectIntl, intlShape} from 'react-intl'; import analytics from '../lib/analytics'; import log from '../lib/log'; import {LoadingStates, onLoadedProject, onProjectUploadStarted} from '../reducers/project-state'; import {openLoadingProject,closeLoadingProject} from '../reducers/modals'; /** 獲取作品的編號(hào) **/ function getProjectId() { if(document.getElementById("projectId")){ return $("#projectId").val(); } else { alert("sb3-downloader-qiniu.jsx文件提示:頁面不存在id屬性為projectId的對(duì)象!"); return null; } } /** * 從七牛云加載sb3文件 */ class SB3DownloaderQiniu extends React.Component { constructor (props) { super(props); } componentDidMount() { var _this = this; if(getProjectId()==null){ return; } // 作品所在存放地址 var sb3Path = null; $.ajax({ dataType:"json", async:false, url:"/project/checkProjectByProjectId", data: {id: getProjectId()}, success:function(res){ if(res.success==true){ sb3Path = res.sb3Path; } } }); /** * 必須使用 $(window).on("load",function(){}); * 否則頁面在未加載完的情況下,有些組件會(huì)來不及加載,影響二次文件保存 */ $(window).on("load",function(){ let reader = new FileReader(); let request = new XMLHttpRequest(); request.open('GET', sb3Path, true); request.responseType = "blob"; request.onload = function() { if(request.status==404){ alert("未找到sb3類型的資源文件"); location.href='/scratch'; } let blobs = request.response reader.readAsArrayBuffer(blobs); reader.onload = () => _this.props.vm.loadProject(reader.result).then(() => { analytics.event({ category: 'project', action: 'Import Project File', nonInteraction: true }); _this.props.onLoadingFinished(_this.props.loadingState); }).catch(error => { log.warn(error); }); } request.send(); }); } render () { return this.props.children(this.props.className); } } SB3DownloaderQiniu.propTypes = { children: PropTypes.func, className: PropTypes.string, intl: intlShape.isRequired, loadingState: PropTypes.oneOf(LoadingStates), onLoadingFinished: PropTypes.func, vm: PropTypes.shape({ loadProject: PropTypes.func }) }; SB3DownloaderQiniu.defaultProps = { className: '' }; const mapStateToProps = state => ({ loadingState: state.scratchGui.projectState.loadingState, vm: state.scratchGui.vm }); const mapDispatchToProps = (dispatch, ownProps) => ({ onLoadingFinished: loadingState => { console.dir("sb3文件加載完畢!"); dispatch(onLoadedProject(loadingState, ownProps.canSave)); dispatch(closeLoadingProject()); } }); // Allow incoming props to override redux-provided props. Used to mock in tests. const mergeProps = (stateProps, dispatchProps, ownProps) => Object.assign( {}, stateProps, dispatchProps, ownProps ); export default connect( mapStateToProps, mapDispatchToProps, mergeProps )(injectIntl(SB3DownloaderQiniu));
使用組件
<SB3DownloaderQiniu /** 初始化加載文件到項(xiàng)目 **/> {(className, loadProject) => ( <button onClick={loadProject} className={classNames(styles.scratchHide)}></button> )} </SB3DownloaderQiniu>
好了,下面看下如何自動(dòng)加載scratch3.0的頁面上實(shí)現(xiàn)自動(dòng)加載原有的作品
首先,我們?cè)诎惭bscratch3。0后,瀏覽器默認(rèn)打開的是編程的頁面。如下圖:
那么我們希望開發(fā)一個(gè)功能,就是打開的時(shí)候默認(rèn)加入某一個(gè)SB3的開發(fā)文件
1.首先,我們需要有一個(gè).SB3的開發(fā)文件,建議上傳到STATIC目錄下
2、找到scratch-gui-develop>src>container》gui.jsx文件
找到44行的componentDidMount函數(shù)
新增以下代碼
const url="/static/123.sb3"; fetch(url,{ method: 'GET' }) .then(response=>response.blob()) .then(blob=>{ const reader=new FileReader(); reader.onload=()=>this.props.vm.loadProject(reader.result) .then(()=>{ GoogleAnalytics.event({ category:'project', action:'Import Project File', nonInteraction:true }) }) reader.readAsArrayBuffer(blob) }) .catch(error=>{ alert(`遠(yuǎn)程加載文件錯(cuò)誤!${error}`) })
文件加載完畢
此外,我們例如希望開發(fā)像修改作業(yè)之類的,我們可以需要進(jìn)行文件的傳遞
我們需要將上面的第一行
consturl="/static/123.sb3";
更改為
consturl=window.projecturl;
然后呢。在首頁,例如paly.html添加上以上代碼,或者自己用參數(shù)來傳遞
<script> window.projectUrl="https://steam.nosdn.127.net/885318eb-ad83-44c4-afe3-d3bea0a0d2ab.sb3"; </script>
到此這篇關(guān)于Scratch3.0初始化加載七牛云上的sbs文件的文章就介紹到這了,更多相關(guān)Scratch加載sbs文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
高性能WEB開發(fā) JS、CSS的合并、壓縮、緩存管理
本篇文章主要討論下目前JS,CSS 合并、壓縮、緩存管理存在的一些問題,然后分享下自己項(xiàng)目中用到的1個(gè)處理方案,并提供1個(gè)實(shí)例下載。2010-05-05git push 本地項(xiàng)目推送到遠(yuǎn)程分支的方法(git命令版)
這篇文章主要介紹了git push 本地項(xiàng)目推送到遠(yuǎn)程分支的方法(git命令版),需要的朋友可以參考下2020-09-09idea集成Git實(shí)現(xiàn)團(tuán)隊(duì)合作分工的原理詳解
這篇文章主要介紹了idea集成Git實(shí)現(xiàn)團(tuán)隊(duì)合作分工的原理,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-12-12HTTP請(qǐng)求首部字段及響應(yīng)首部字段詳解
這篇文章主要介紹了HTTP請(qǐng)求首部字段及響應(yīng)首部字段,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06