File size: 3,665 Bytes
8086c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
(window["webpackJsonpGUI"] = window["webpackJsonpGUI"] || []).push([["addon-entry-block-count"],{

/***/ "./src/addons/addons/block-count/_runtime_entry.js":
/*!*********************************************************!*\
  !*** ./src/addons/addons/block-count/_runtime_entry.js ***!
  \*********************************************************/
/*! exports provided: resources */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "resources", function() { return resources; });
/* harmony import */ var _blockcount_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./blockcount.js */ "./src/addons/addons/block-count/blockcount.js");
/* generated by pull.js */

const resources = {
  "blockcount.js": _blockcount_js__WEBPACK_IMPORTED_MODULE_0__["default"]
};

/***/ }),

/***/ "./src/addons/addons/block-count/blockcount.js":
/*!*****************************************************!*\
  !*** ./src/addons/addons/block-count/blockcount.js ***!
  \*****************************************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony default export */ __webpack_exports__["default"] = (async function (_ref) {
  let {
    addon,
    console,
    msg
  } = _ref;
  const vm = addon.tab.traps.vm;
  const getBlockCount = () => {
    let blockCount = 0;
    let scriptCount = 0;
    let sprites = new Set(vm.runtime.targets.map(i => i.sprite.blocks._blocks));
    sprites.forEach((sprite, i) => {
      scriptCount += Object.values(sprite).filter(o => !o.parent).length; // Filter blocks that don't have a parent (meaning it's the top of a stack)
      blockCount += Object.values(sprite).filter(o => !o.shadow).length; // shadow blocks should be filtered out
    });
    return {
      blockCount,
      scriptCount,
      spriteCount: sprites.size - 1 // Backdrop counts as a target so we can subtract it
    };
  };
  const addLiveBlockCount = async () => {
    if (vm.editingTarget) {
      let handler = null;
      while (true) {
        const topBar = await addon.tab.waitForElement("[class^='menu-bar_main-menu']", {
          markAsSeen: true,
          reduxEvents: ["scratch-gui/mode/SET_PLAYER", "fontsLoaded/SET_FONTS_LOADED", "scratch-gui/locales/SELECT_LOCALE"],
          reduxCondition: state => !state.scratchGui.mode.isPlayerOnly
        });
        let display = topBar.appendChild(document.createElement("span"));
        addon.tab.displayNoneWhileDisabled(display);
        display.style.order = 1;
        display.style.padding = "9px";
        display.innerText = msg("blocks", {
          num: getBlockCount().blockCount
        });
        let debounce; // debouncing values because of the way 'PROJECT_CHANGED' works
        if (handler) {
          vm.off("PROJECT_CHANGED", handler);
          vm.runtime.off("PROJECT_LOADED", handler);
        }
        handler = async () => {
          clearTimeout(debounce);
          debounce = setTimeout(async () => {
            display.innerText = msg("blocks", {
              num: getBlockCount().blockCount
            });
          }, 1000);
        };
        vm.on("PROJECT_CHANGED", handler);
        vm.runtime.on("PROJECT_LOADED", handler);
      }
    } else {
      let timeout = setTimeout(function () {
        addLiveBlockCount();
        clearTimeout(timeout);
      }, 1000);
    }
  };
  addLiveBlockCount();
});

/***/ })

}]);
//# sourceMappingURL=addon-entry-block-count.js.map