Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update var-and-list.js #692

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions extensions/qxsck/var-and-list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

(function(Scratch) {
'use strict';
Scratch.translate.setup({
Expand Down Expand Up @@ -29,6 +30,7 @@
{
opcode: 'getVar',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data'],
text: Scratch.translate({ id: 'getVar', default: 'value of [VAR]' }),
arguments: {
VAR: {
Expand All @@ -40,6 +42,7 @@
{
opcode: 'seriVarsToJson',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data'],
text: Scratch.translate({ id: 'seriVarsToJson', default: 'convert all variables starting with [START] to json' }),
arguments: {
START: {
Expand All @@ -51,6 +54,7 @@
{
opcode: 'setVar',
blockType: Scratch.BlockType.COMMAND,
extensions: ['colours_data'],
text: Scratch.translate({ id: 'setVar', default: 'set the value of [VAR] to [VALUE]' }),
arguments: {
VAR: {
Expand All @@ -66,6 +70,7 @@
{
opcode: 'getList',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'getList', default: 'value of [LIST]' }),
arguments: {
LIST: {
Expand All @@ -77,6 +82,7 @@
{
opcode: 'getValueOfList',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'getValueOfList', default: 'item [INDEX] of [LIST]' }),
arguments: {
LIST: {
Expand All @@ -92,6 +98,7 @@
{
opcode: 'seriListsToJson',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'seriListsToJson', default: 'convert all lists starting with [START] to json' }),
arguments: {
START: {
Expand All @@ -103,6 +110,7 @@
{
opcode: 'clearList',
blockType: Scratch.BlockType.COMMAND,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'clearList', default: 'delete all of [LIST]' }),
arguments: {
LIST: {
Expand All @@ -114,6 +122,7 @@
{
opcode: 'deleteOfList',
blockType: Scratch.BlockType.COMMAND,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'deleteOfList', default: 'delete [INDEX] of [LIST]' }),
arguments: {
LIST: {
Expand All @@ -129,6 +138,7 @@
{
opcode: 'addValueInList',
blockType: Scratch.BlockType.COMMAND,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'addValueInList', default: 'add [VALUE] to [LIST]' }),
arguments: {
LIST: {
Expand All @@ -144,6 +154,7 @@
{
opcode: 'replaceOfList',
blockType: Scratch.BlockType.COMMAND,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'replaceOfList', default: 'replace item [INDEX] of [LIST] with [VALUE]' }),
arguments: {
LIST: {
Expand All @@ -163,6 +174,7 @@
{
opcode: 'getIndexOfList',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'getIndexOfList', default: 'first index of [VALUE] in [LIST]' }),
arguments: {
LIST: {
Expand All @@ -178,6 +190,7 @@
{
opcode: 'getIndexesOfList',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'getIndexesOfList', default: 'indexes of [VALUE] in [LIST]' }),
arguments: {
LIST: {
Expand All @@ -193,6 +206,7 @@
{
opcode: 'length',
blockType: Scratch.BlockType.REPORTER,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'length', default: 'length of [LIST]' }),
arguments: {
LIST: {
Expand All @@ -204,6 +218,7 @@
{
opcode: 'listContains',
blockType: Scratch.BlockType.BOOLEAN,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'listContains', default: '[LIST] contains [VALUE] ?' }),
arguments: {
LIST: {
Expand All @@ -219,6 +234,7 @@
{
opcode: 'copyList',
blockType: Scratch.BlockType.COMMAND,
extensions: ['colours_data_lists'],
text: Scratch.translate({ id: 'copyList', default: 'copy [LIST1] to [LIST2]' }),
arguments: {
LIST1: {
Expand All @@ -234,7 +250,6 @@
]
};
}

getVar(args, util) {
const variable = util.target.lookupVariableByNameAndType(Scratch.Cast.toString(args.VAR), '');
return variable ? variable.value : '';
Expand Down Expand Up @@ -365,8 +380,8 @@
for (var i = 0;i < variable.value.length;i++) {
if (Scratch.Cast.compare(variable.value[i], value) === 0) return true;
}
}
return false;
}
}
copyList(args, util) {
/** @type {VM.ListVariable} */
Expand All @@ -377,5 +392,16 @@
}
}
}

const runtime = Scratch.vm.runtime;
const cbfsb = runtime._convertBlockForScratchBlocks.bind(runtime);
runtime._convertBlockForScratchBlocks = function(blockInfo, categoryInfo) {
const res = cbfsb(blockInfo, categoryInfo);
if (blockInfo.extensions) {
if (!res.json.extensions) res.json.extensions = [];
res.json.extensions.push(...blockInfo.extensions);
}
return res;
};
Scratch.extensions.register(new VarAndList());
}(Scratch));