前言
例如:scratch-gui可以选择扩展, 只需要拿出来部分功能就可以做自动导入扩展。
提示:以下是本篇文章正文内容,下面案例可供参考
一、scratch-gui扩展是什么?
示例:scratch-gui扩展,就是素材库,添加更多的素材去组合你的作品。
二、使用步骤
1.上代码
代码如下(示例):
// 省略资源引入部分,无更改引入
import ...
class Blocks extends React.Component {
constructor (props) {
super(props);
this.ScratchBlocks = VMScratchBlocks(props.vm);
bindAll(this, [
'attachVM',
'detachVM',
'getToolboxXML',
'handleCategorySelected',
'handleConnectionModalStart',
'handleDrop',
'handleStatusButtonUpdate',
'handleOpenSoundRecorder',
'handlePromptStart',
'handlePromptCallback',
'handlePromptClose',
'handleCustomProceduresClose',
'onScriptGlowOn',
'onScriptGlowOff',
'onBlockGlowOn',
'onBlockGlowOff',
'handleMonitorsUpdate',
'handleExtensionAdded',
'handleBlocksInfoUpdate',
'onTargetsUpdate',
'onVisualReport',
'onWorkspaceUpdate',
'onWorkspaceMetricsChange',
'setBlocks',
'setLocale',
'automaticImportComponent' // 绑定事件
]);
this.ScratchBlocks.prompt = this.handlePromptStart;
this.ScratchBlocks.statusButtonCallback = this.handleConnectionModalStart;
this.ScratchBlocks.recordSoundCallback = this.handleOpenSoundRecorder;
this.state = {
prompt: null,
extensions: ['music', 'pen'] // 自动导入扩展 参数 扩展id
};
this.onTargetsUpdate = debounce(this.onTargetsUpdate, 100);
this.toolboxUpdateQueue = [];
}
componentDidMount () {
this.ScratchBlocks.FieldColourSlider.activateEyedropper_ = this.props.onActivateColorPicker;
this.ScratchBlocks.Procedures.externalProcedureDefCallback = this.props.onActivateCustomProcedures;
this.ScratchBlocks.ScratchMsgs.setLocale(this.props.locale);
const workspaceConfig = defaultsDeep({},
Blocks.defaultOptions,
this.props.options,
{rtl: this.props.isRtl, toolbox: this.props.toolboxXML}
);
this.workspace = this.ScratchBlocks.inject(this.blocks, workspaceConfig);
// Register buttons under new callback keys for creating variables,
// lists, and procedures from extensions.
const toolboxWorkspace = this.workspace.getFlyout().getWorkspace();
const varListButtonCallback = type =>
(() => this.ScratchBlocks.Variables.createVariable(this.workspace, null, type));
const procButtonCallback = () => {
this.ScratchBlocks.Procedures.createProcedureDefCallback_(this.workspace);
};
toolboxWorkspace.registerButtonCallback('MAKE_A_VARIABLE', varListButtonCallback(''));
toolboxWorkspace.registerButtonCallback('MAKE_A_LIST', varListButtonCallback('list'));
toolboxWorkspace.registerButtonCallback('MAKE_A_PROCEDURE', procButtonCallback);
// Store the xml of the toolbox that is actually rendered.
// This is used in componentDidUpdate instead of prevProps, because
// the xml can change while e.g. on the costumes tab.
this._renderedToolboxXML = this.props.toolboxXML;
// we actually never want the workspace to enable "refresh toolbox" - this basically re-renders the
// entire toolbox every time we reset the workspace. We call updateToolbox as a part of
// componentDidUpdate so the toolbox will still correctly be updated
this.setToolboxRefreshEnabled = this.workspace.setToolboxRefreshEnabled.bind(this.workspace);
this.workspace.setToolboxRefreshEnabled = () => {
this.setToolboxRefreshEnabled(false);
};
// @todo change this when blockly supports UI events
addFunctionListener(this.workspace, 'translate', this.onWorkspaceMetricsChange);
addFunctionListener(this.workspace, 'zoom', this.onWorkspaceMetricsChange);
this.attachVM();
// Only update blocks/vm locale when visible to avoid sizing issues
// If locale changes while not visible it will get handled in didUpdate
if (this.props.isVisible) {
this.setLocale();
}
// 扩展导入调用
this.state.extensions.forEach(this.automaticImportComponent)
}
// 自动导入组件h函数
automaticImportComponent(key = ''){
let id, url = key
if (this.props.vm.extensionManager.isExtensionLoaded(url)) {
this.props.handleCategorySelected(id);
} else {
this.props.vm.extensionManager.loadExtensionURL(url).then(() => {
this.props.handleCategorySelected(id);
});
}
}
... // 省略
}
2.效果
效果如下(示例):
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了scratch自动导入扩展的使用,欢迎各位大佬,关注评论点赞。