Spaces:
Runtime error
Runtime error
File size: 10,936 Bytes
8fd7a1d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import {defineMessages, intlShape, injectIntl} from 'react-intl';
import VM from 'scratch-vm';
import Box from '../components/box/box.jsx';
import {
openExtensionLibrary,
closeExtensionLibrary,
openCustomExtensionModal
} from '../reducers/modals';
import {activateTab, BLOCKS_TAB_INDEX, EXTENSIONS_TAB_INDEX} from '../reducers/editor-tab';
import styles from './extensions-tab.css';
import addExtensionIcon from '../components/gui/icon--extensions.svg';
const messages = defineMessages({
addExtension: {
defaultMessage: 'Add Extension',
description: 'Button to add an extension in the extensions tab',
id: 'gui.extensionsTab.addExtension'
}
});
class ExtensionsTab extends React.Component {
constructor (props) {
super(props);
this.handleAddExtensionClick = this.handleAddExtensionClick.bind(this);
this.handleExtensionClick = this.handleExtensionClick.bind(this);
this.updateLoadedExtensions = this.updateLoadedExtensions.bind(this);
this.handleExtensionAdded = this.handleExtensionAdded.bind(this);
this.handleEnableProcedureReturns = this.handleEnableProcedureReturns.bind(this);
this.handleCategorySelected = this.handleCategorySelected.bind(this);
this.state = {
loadedExtensions: []
};
}
componentDidMount () {
this.updateLoadedExtensions();
// Listen for extension loading changes
if (this.props.vm) {
this.props.vm.on('EXTENSION_ADDED', this.handleExtensionAdded);
this.props.vm.runtime.on('PROJECT_LOADED', this.updateLoadedExtensions);
}
}
componentWillUnmount () {
if (this.props.vm) {
this.props.vm.off('EXTENSION_ADDED', this.handleExtensionAdded);
this.props.vm.runtime.off('PROJECT_LOADED', this.updateLoadedExtensions);
}
}
componentDidUpdate (prevProps) {
// Recalculate block counts every time the extensions tab becomes active
if (this.props.activeTabIndex === EXTENSIONS_TAB_INDEX &&
prevProps.activeTabIndex !== EXTENSIONS_TAB_INDEX) {
console.log('π·οΈ Extensions tab became active - recalculating block counts');
this.updateLoadedExtensions();
}
}
handleAddExtensionClick () {
this.props.onExtensionButtonClick();
}
handleExtensionClick (extensionId) {
// Handle clicks on loaded extensions - could open documentation or settings
console.log('Extension clicked:', extensionId);
}
/**
* Handle extension added event with automatic tab navigation
*/
handleExtensionAdded () {
// First, navigate to blocks tab to ensure extension blocks are loaded
this.props.onActivateBlocksTab();
}
/**
* Count blocks used by a specific extension across all targets
* @param {string} extensionId - The extension ID to count blocks for
* @returns {number} - Number of blocks used by this extension
*/
countExtensionBlocks (extensionId) {
if (!this.props.vm || !this.props.vm.runtime) return 0;
let blockCount = 0;
// Iterate through all targets (sprites and stage)
this.props.vm.runtime.targets.forEach(target => {
if (!target.blocks) return;
// Get all blocks for this target
const blocks = target.blocks._blocks;
if (!blocks) return;
// Count blocks that belong to this extension
Object.values(blocks).forEach(block => {
if (block && block.opcode && block.opcode.startsWith(extensionId + '_')) {
blockCount++;
}
});
});
return blockCount;
}
/**
* Get information about a loaded extension
* @param {string} extensionId - The extension ID
* @returns {object} - Extension information including name and URL
*/
getExtensionInfo (extensionId) {
if (!this.props.vm || !this.props.vm.runtime) return { name: extensionId, url: null };
// Try to get extension info from runtime block info
const blockInfo = this.props.vm.runtime._blockInfo || [];
const extensionInfo = blockInfo.find(info => info.id === extensionId);
if (extensionInfo) {
return {
name: extensionInfo.name || extensionId,
url: this.getExtensionURL(extensionId)
};
}
return {
name: extensionId,
url: this.getExtensionURL(extensionId)
};
}
/**
* Get the URL for an extension if it's a custom extension
* @param {string} extensionId - The extension ID
* @returns {string|null} - The extension URL or null if it's a built-in extension
*/
getExtensionURL (extensionId) {
if (!this.props.vm || !this.props.vm.extensionManager) return null;
const extensionURLs = this.props.vm.extensionManager.getExtensionURLs();
return extensionURLs[extensionId] || null;
}
/**
* Update the list of loaded extensions with their block counts
*/
updateLoadedExtensions () {
if (!this.props.vm || !this.props.vm.extensionManager) {
this.setState({ loadedExtensions: [] });
return;
}
console.log('π Recalculating extension block counts...');
const loadedExtensionIds = Array.from(this.props.vm.extensionManager._loadedExtensions.keys());
const extensionsWithCounts = loadedExtensionIds.map(extensionId => {
const blockCount = this.countExtensionBlocks(extensionId);
const info = this.getExtensionInfo(extensionId);
console.log(`π¦ Extension "${info.name}" (${extensionId}): ${blockCount} blocks`);
return {
id: extensionId,
name: info.name,
url: info.url,
blockCount: blockCount
};
});
// Sort by block count (descending) and then by name
extensionsWithCounts.sort((a, b) => {
if (a.blockCount !== b.blockCount) {
return b.blockCount - a.blockCount;
}
return a.name.localeCompare(b.name);
});
this.setState({ loadedExtensions: extensionsWithCounts });
console.log('β
Extension block counts updated');
}
/**
* Handle enabling procedure returns - set flag for blocks component to handle
*/
handleEnableProcedureReturns () {
console.log('ExtensionsTab: handleEnableProcedureReturns called - setting pending flag');
// Set a flag for the blocks component to handle when it becomes active
if (this.props.vm) {
this.props.vm._pendingProcedureReturns = true;
console.log('ExtensionsTab: Set _pendingProcedureReturns flag');
}
}
/**
* Handle category selection - set flag for blocks component to handle
*/
handleCategorySelected (categoryId) {
console.log('ExtensionsTab: handleCategorySelected called with', categoryId, '- setting pending flag');
// Set a flag for the blocks component to handle when it becomes active
if (this.props.vm) {
this.props.vm._pendingCategorySelection = categoryId;
console.log('ExtensionsTab: Set _pendingCategorySelection flag to', categoryId);
}
}
render () {
const {
intl,
onCategorySelected,
vm
} = this.props;
return (
<Box className={styles.extensionsTab}>
<Box className={styles.extensionsGrid}>
{/* Add Extension Button - always first */}
<Box className={styles.extensionGridItem}>
<button
className={styles.addExtensionButton}
title={intl.formatMessage(messages.addExtension)}
onClick={this.handleAddExtensionClick}
>
<img
className={styles.addExtensionIcon}
draggable={false}
src={addExtensionIcon}
/>
<span className={styles.addExtensionText}>
{intl.formatMessage(messages.addExtension)}
</span>
</button>
</Box>
{/* Loaded Extensions */}
{this.state.loadedExtensions.map(extension => (
<Box
key={extension.id}
className={styles.extensionGridItem}
onClick={() => this.handleExtensionClick(extension.id)}
>
<Box className={styles.extensionCard}>
<Box className={styles.extensionName}>
{extension.name}
</Box>
<Box className={styles.extensionBlockCount}>
<Box className={styles.blockCountNumber}>
{extension.blockCount}
</Box>
<Box className={styles.blockCountLabel}>
{extension.blockCount === 1 ? 'block' : 'blocks'}
</Box>
</Box>
</Box>
</Box>
))}
</Box>
</Box>
);
}
}
ExtensionsTab.propTypes = {
activeTabIndex: PropTypes.number,
intl: intlShape.isRequired,
onCategorySelected: PropTypes.func,
onExtensionButtonClick: PropTypes.func,
onActivateBlocksTab: PropTypes.func,
onActivateExtensionsTab: PropTypes.func,
onOpenCustomExtensionModal: PropTypes.func,
vm: PropTypes.instanceOf(VM)
};
const mapStateToProps = state => ({
activeTabIndex: state.scratchGui.editorTab.activeTabIndex
});
const mapDispatchToProps = dispatch => ({
onExtensionButtonClick: () => dispatch(openExtensionLibrary()),
onActivateBlocksTab: () => dispatch(activateTab(BLOCKS_TAB_INDEX)),
onActivateExtensionsTab: () => dispatch(activateTab(EXTENSIONS_TAB_INDEX)),
onOpenCustomExtensionModal: () => dispatch(openCustomExtensionModal())
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(injectIntl(ExtensionsTab));
|