programmatically reduce the number of inputs in a custom block

snap8
Jens Mönig 2022-05-01 13:45:33 +02:00
rodzic 644c8fedcf
commit 105acbd7cd
3 zmienionych plików z 34 dodań i 3 usunięć

Wyświetl plik

@ -45,6 +45,9 @@
* **Translation Updates:**
* German
### 2022-05-01
* byob: programmatically reduce the number of inputs in a custom block
### 2022-04-28
* threads, byob: programmatically re-define custom blocks, experimental, under construction
* threads: programmatically re-categorize custom blocks

Wyświetl plik

@ -23,7 +23,7 @@
<script src="src/gui.js?version=2022-04-26"></script>
<script src="src/paint.js?version=2021-07-05"></script>
<script src="src/lists.js?version=2022-02-07"></script>
<script src="src/byob.js?version=2022-04-28"></script>
<script src="src/byob.js?version=2022-05-01"></script>
<script src="src/tables.js?version=2022-01-28"></script>
<script src="src/sketch.js?version=2021-11-03"></script>
<script src="src/video.js?version=2019-06-27"></script>

Wyświetl plik

@ -111,7 +111,7 @@ ArgLabelMorph*/
// Global stuff ////////////////////////////////////////////////////////
modules.byob = '2022-April-28';
modules.byob = '2022-May-01';
// Declarations
@ -569,7 +569,10 @@ CustomBlockDefinition.prototype.setBlockDefinition = function (aContext) {
declarations = this.declarations,
parts = [];
if (oldInputs.length !== newInputs.length) {
if (oldInputs.length > newInputs.length) {
this.removeInputs(oldInputs.length - newInputs.length);
spec = this.abstractBlockSpec();
} else if (oldInputs.length !== newInputs.length) {
throw new Error('expecting the number of inputs to match');
}
@ -601,6 +604,31 @@ CustomBlockDefinition.prototype.setBlockDefinition = function (aContext) {
this.body = aContext;
};
CustomBlockDefinition.prototype.removeInputs = function (count) {
// private - only to be called from a Process that also does housekeeping
var surplus = this.inputNames().slice(-count);
// remove the surplus input names from the spec
this.spec = this.parseSpec(this.spec).filter(str =>
!(str.length > 1 && (str[0]) === '%' && surplus.includes(str.slice(1)))
).join(' ').trim();
// remove the surplus input names from the slot declarations
surplus.forEach(name => this.declarations.delete(name));
};
CustomBlockDefinition.prototype.addInputs = function (count) {
// private
};
CustomBlockDefinition.prototype.gensym = function (existing) {
var count = 1;
while (contains(existing, '#' + count)) {
count += 1;
}
return '#' + count;
};
// CustomBlockDefinition picturing
CustomBlockDefinition.prototype.scriptsPicture = function () {