programmatically change the type of unused custom blocks

snap8
Jens Mönig 2022-05-02 11:15:16 +02:00
rodzic 2a7d1bcd87
commit c4ad10a310
4 zmienionych plików z 43 dodań i 9 usunięć

Wyświetl plik

@ -45,6 +45,9 @@
* **Translation Updates:**
* German
### 2022-05-02
* blocks, threads: programmatically change the type of unused custom blocks
### 2022-05-01
* byob: programmatically reduce the number of inputs in a custom block
* byob: programmatically add inputs to a custom block

Wyświetl plik

@ -16,8 +16,8 @@
<script src="src/morphic.js?version=2022-04-26"></script>
<script src="src/symbols.js?version=2021-03-03"></script>
<script src="src/widgets.js?version=2021-17-09"></script>
<script src="src/blocks.js?version=2022-04-28"></script>
<script src="src/threads.js?version=2022-04-28"></script>
<script src="src/blocks.js?version=2022-05-02"></script>
<script src="src/threads.js?version=2022-05-02"></script>
<script src="src/objects.js?version=2022-04-28"></script>
<script src="src/scenes.js?version=2022-03-03"></script>
<script src="src/gui.js?version=2022-04-26"></script>

Wyświetl plik

@ -161,7 +161,7 @@ CostumeIconMorph, SoundIconMorph, SVG_Costume, embedMetadataPNG*/
// Global stuff ////////////////////////////////////////////////////////
modules.blocks = '2022-April-28';
modules.blocks = '2022-May-02';
var SyntaxElementMorph;
var BlockMorph;
@ -805,7 +805,8 @@ SyntaxElementMorph.prototype.labelParts = {
'definition': ['definition'],
'category': ['category'],
'custom?': ['custom?'],
'global?': ['global?']
'global?': ['global?'],
'type': ['type']
}
},
'%byob': {
@ -814,7 +815,8 @@ SyntaxElementMorph.prototype.labelParts = {
menu: {
'label': ['label'],
'definition': ['definition'],
'category': ['category']
'category': ['category'],
'type': ['type']
}
},

Wyświetl plik

@ -65,7 +65,7 @@ StagePickerMorph*/
/*jshint esversion: 11, bitwise: false, evil: true*/
modules.threads = '2022-April-28';
modules.threads = '2022-May-02';
var ThreadManager;
var Process;
@ -5620,6 +5620,10 @@ Process.prototype.reportBasicBlockAttribute = function (attribute, block) {
return expr ? !!expr.isCustomBlock : false;
case 'global?':
return (expr && expr.isCustomBlock) ? !!expr.isGlobal : true;
case 'type':
return ['command', 'reporter', 'predicate'].indexOf(
this.reportTypeOf(block)
) + 1;
}
return '';
};
@ -5629,13 +5633,15 @@ Process.prototype.doSetBlockAttribute = function (attribute, block, val) {
var choice = this.inputOption(attribute),
rcvr = this.blockReceiver(),
ide = rcvr.parentThatIsA(IDE_Morph),
oldSpec,
types = ['command', 'reporter', 'predicate'],
count = 1,
oldSpec,
expr,
def,
template;
template,
type;
this.assertType(block, ['command', 'reporter', 'predicate']);
this.assertType(block, types);
expr = block.expression;
if (!expr.isCustomBlock) {
throw new Error('expecting a custom block\nbut getting a primitive');
@ -5643,6 +5649,15 @@ Process.prototype.doSetBlockAttribute = function (attribute, block, val) {
def = expr.isGlobal ? expr.definition : rcvr.getMethod(expr.semanticSpec);
oldSpec = def.blockSpec();
function isInUse() {
if (def.isGlobal) {
return ide.sprites.asArray().concat([ide.stage]).some((any, idx) =>
any.usesBlockInstance(def, false, idx)
);
}
return rcvr.allDependentInvocationsOf(oldSpec).length > 0;
}
switch (choice) {
case 'label':
def.setBlockLabel(val);
@ -5663,6 +5678,20 @@ Process.prototype.doSetBlockAttribute = function (attribute, block, val) {
def.category = SpriteMorph.prototype.allCategories()[+val - 1] ||
'other';
break;
case 'type':
if (isInUse()) {
throw new Error('cannot change a block\nthat is in use');
}
this.assertType(val, ['number', 'text']);
if (this.reportTypeOf(val) === 'text') {
type = val;
} else {
type = types[val - 1] || '';
}
if (!types.includes(type)) {return;}
def.type = type;
break;
default:
return;
}