File size: 3,561 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const Cast = require('../../util/cast');

/**
 * Class for EasySave blocks
 * @constructor
 */
class jgEasySaveBlocks {
    constructor(runtime) {
        /**
         * The runtime instantiating this block package.
         * @type {Runtime}
         */
        this.runtime = runtime;
    }

    /**
     * @returns {object} metadata for this extension and its blocks.
     */
    getInfo() {
        return {
            id: 'jgEasySave',
            name: 'Easy Save',
            color1: '#48a3d4',
            color2: '#3d89b3',
            blocks: [
                {
                    blockType: BlockType.LABEL,
                    text: "Saving"
                },
                {
                    opcode: 'addVarToSave',
                    text: 'add value of variable [VAR] to save',
                    blockType: BlockType.COMMAND,
                    arguments: {
                        VAR: {
                            menu: "variable"
                        }
                    }
                },
                {
                    opcode: 'addListToSave',
                    text: 'add value of list [LIST] to save',
                    blockType: BlockType.COMMAND,
                    arguments: {
                        LIST: {
                            menu: "list"
                        }
                    }
                },
                {
                    blockType: BlockType.LABEL,
                    text: "Loading"
                },
            ],
            menus: {
                variable: {
                    acceptReporters: false,
                    items: "getVariables",
                },
                list: {
                    acceptReporters: false,
                    items: "getLists",
                },
            }
        };
    }

    getVariables() {
        const variables =
            // @ts-expect-error
            typeof Blockly === "undefined"
                ? []
                : // @ts-expect-error
                Blockly.getMainWorkspace()
                    .getVariableMap()
                    .getVariablesOfType("")
                    .map((model) => ({
                        text: model.name,
                        value: model.getId(),
                    }));
        if (variables.length > 0) {
            return variables;
        } else {
            return [{ text: "", value: "" }];
        }
    }
    getLists() {
        // using blockly causes unstable behavior
        // https://discord.com/channels/1033551490331197462/1038251742439149661/1202846831994863627
        const globalLists = Object.values(this.runtime.getTargetForStage().variables)
            .filter((x) => x.type == "list");
        const localLists = Object.values(this.runtime.vm.editingTarget.variables)
            .filter((x) => x.type == "list");
        const uniqueLists = [...new Set([...globalLists, ...localLists])];
        if (uniqueLists.length === 0) return [{ text: "", value: "" }];
        return uniqueLists.map((i) => ({ text: i.name, value: i.id }));
    }

    addVarToSave(args, util) {
        const variable = util.target.lookupVariableById(args.VAR);
        console.log(variable);
    }
    addListToSave(args, util) {
        console.log(args.LIST);
        const variable = util.target.lookupVariableById(args.LIST);
        console.log(variable);
    }
}

module.exports = jgEasySaveBlocks;