当前位置: 首页 > 工具软件 > Scratch View > 使用案例 >

scratch3.0二开替换默认角色修改默认项目名称

徐涵亮
2023-12-01

修改默认角色,这里有个比较便捷的方式,但不是很规范。

首先找到project-fetcher-hoc.jsx这个高阶组件,

默认项目在这里创建的

componentDidUpdate (prevProps) {
            if (prevProps.projectHost !== this.props.projectHost) {
                storage.setProjectHost(this.props.projectHost);
            }
            if (prevProps.assetHost !== this.props.assetHost) {
                storage.setAssetHost(this.props.assetHost);
            }
            if (this.props.isFetchingWithId && !prevProps.isFetchingWithId) {
                this.fetchProject(this.props.reduxProjectId, this.props.loadingState);
            }
            if (this.props.isShowingProject && !prevProps.isShowingProject) {
                this.props.onProjectUnchanged();
            }
            if (this.props.isShowingProject && (prevProps.isLoadingProject || prevProps.isCreatingNew)) {
                this.props.onActivateTab(BLOCKS_TAB_INDEX);
            }
        }

可以在这里嵌入一个全局配置,让它优先加载我们的全局默认项目,默认项目可以放到

static目录下;

componentDidUpdate (prevProps) {
            if (prevProps.projectHost !== this.props.projectHost) {
                storage.setProjectHost(this.props.projectHost);
            }
            if (prevProps.assetHost !== this.props.assetHost) {
                storage.setAssetHost(this.props.assetHost);
            }
            if (this.props.isFetchingWithId && !prevProps.isFetchingWithId) {
                if ('defaultProjectURL' in window.scratchConfig) {
                    const that = this;
                    fetch(window.scratchConfig.defaultProjectURL).then(r => r.blob())
                        .then(blob => {
                            const reader = new FileReader();
                            reader.onload = () => {
                                that.props.onFetchedProjectData(reader.result, that.props.loadingState);
                            };
                            reader.readAsArrayBuffer(blob);
                        });
                } else {
                    this.fetchProject(this.props.reduxProjectId, this.props.loadingState);
                }
            }
            if (this.props.isShowingProject && !prevProps.isShowingProject) {
                this.props.onProjectUnchanged();
            }
            if (this.props.isShowingProject && (prevProps.isLoadingProject || prevProps.isCreatingNew)) {
                this.props.onActivateTab(BLOCKS_TAB_INDEX);
            }
        }

升级后的代码,如果存在默认项目配置,系统会自动优先加载你定义的默认项目,这时候我们可以直接采取任意方式编程,创建一个含默认角色的项目,丢到static目录下就可以了。

但是,项目名称,确无法读取默认项目中的名称,还需要做个强制替换:

可以找到gui.jsx文件的下面方法,做个扩展点,来强制修改项目名称:


    componentDidUpdate (prevProps) {
        if (this.props.projectId !== prevProps.projectId && this.props.projectId !== null) {
            this.props.onUpdateProjectId(this.props.projectId);
        }
        if (this.props.isShowingProject && !prevProps.isShowingProject) {
            // this only notifies container when a project changes from not yet loaded to loaded
            // At this time the project view in www doesn't need to know when a project is unloaded
            this.props.onProjectLoaded();
        }
    }

修改后:

componentDidUpdate (prevProps) {
        if (this.props.projectId !== prevProps.projectId && this.props.projectId !== null) {
            this.props.onUpdateProjectId(this.props.projectId);
        }
        if (this.props.isShowingProject && !prevProps.isShowingProject) {
            // this only notifies container when a project changes from not yet loaded to loaded
            // At this time the project view in www doesn't need to know when a project is unloaded
            this.props.onProjectLoaded();

            if (window.scratchConfig && 'handleProjectLoaded' in window.scratchConfig) {
                window.scratchConfig.handleProjectLoaded();
            }

            if (!this.isDefaultProjectLoaded) {
                this.isDefaultProjectLoaded = true;
                if (window.scratchConfig && 'handleDefaultProjectLoaded' in window.scratchConfig) {
                    window.scratchConfig.handleDefaultProjectLoaded();
                }
            }
        }
    }

扩展函数,修改scratch3.0默认项目名称:

//默认作品加载完毕
          handleDefaultProjectLoaded: () => {
            setTimeout(() => {
              if (window.scratch) {
                /**
                 * 设置默认加载项目的名称
                 */
                window.scratch.setProjectName("柿子编程作品")
                console.log('默认作品加载完毕 handleDefaultProjectLoaded');
              } else {
                handleDefaultProjectLoaded();
                console.log('默认作品加载 等待重置默认作品名称 handleDefaultProjectLoaded');
              }
            }, 2)
          }

到这里,项目名称也终于修改好了!

 类似资料: