Skip to content

Commit c3c0d72

Browse files
authored
Merge pull request #1037 from rgantzos/main
Fix more-block-themes
2 parents b8ddef5 + 499ddb9 commit c3c0d72

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

api/vm.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,51 @@ async function alertForUpdates() {
231231
}
232232
}
233233

234-
function getScratchBlocks() {
235-
var blocksWrapper = document.querySelector(
236-
'div[class^="gui_blocks-wrapper"]'
237-
);
238-
var key = Object.keys(blocksWrapper).find((key) =>
239-
key.startsWith("__reactInternalInstance$")
240-
);
241-
const internal = blocksWrapper[key];
242-
var recent = internal.child;
243-
while (!recent.stateNode?.ScratchBlocks) {
244-
recent = recent.child;
234+
window.__steScratchBlocks = null
235+
236+
async function _getBlocksWrapperComponent() {
237+
const BLOCKS_CLASS = '[class^="gui_blocks-wrapper"]';
238+
let elem = document.querySelector(BLOCKS_CLASS);
239+
if (!elem) {
240+
elem = document.querySelector(BLOCKS_CLASS);
245241
}
246-
return recent.stateNode.ScratchBlocks || null;
242+
return _getBlocksComponent(elem);
243+
}
244+
245+
function _getBlocksComponent(wrapper) {
246+
const internal = wrapper[getInternalKey(wrapper)];
247+
let childable = internal;
248+
while (((childable = childable.child), !childable || !childable.stateNode || !childable.stateNode.ScratchBlocks)) {}
249+
return childable;
250+
}
251+
252+
function getInternalKey(elem) {
253+
return Object.keys(elem).find((key) => key.startsWith("__react"))
254+
}
255+
256+
function _getBlocksComponent(wrapper) {
257+
const internal = wrapper[getInternalKey(wrapper)];
258+
let childable = internal;
259+
while (((childable = childable.child), !childable || !childable.stateNode || !childable.stateNode.ScratchBlocks)) {}
260+
return childable;
261+
}
262+
263+
async function getBlockly() {
264+
const childable = await _getBlocksWrapperComponent();
265+
return childable.stateNode.ScratchBlocks
266+
}
267+
268+
window.__steRedux.target.addEventListener("statechanged", async function() {
269+
try {
270+
let blockly = await getBlockly()
271+
if (blockly) {
272+
window.__steScratchBlocks = blockly
273+
}
274+
} catch(err) {}
275+
})
276+
277+
function getScratchBlocks() {
278+
return window.__steScratchBlocks
247279
}
248280

249281
ScratchTools.waitForElements(

features/more-block-themes/script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default async function ({ feature, console, scratchClass }) {
3232
if (firstTheme) {
3333
let css = await (await fetch(feature.self.getResource(firstTheme))).text();
3434

35-
feature.traps.gui().theme.theme = "default";
35+
feature.traps.gui().settings.theme = "default";
3636
feature.traps.vm.emitWorkspaceUpdate();
3737

3838
let style = document.createElement("style");
@@ -158,7 +158,7 @@ export default async function ({ feature, console, scratchClass }) {
158158
.classList?.remove(SELECTED_CLASS);
159159
this.querySelector("img").classList.add(SELECTED_CLASS);
160160

161-
feature.traps.gui().theme.theme = "default";
161+
feature.traps.gui().settings.theme = "default";
162162
feature.traps.vm.emitWorkspaceUpdate();
163163
feature.traps.gui().menus.settingsMenu = false;
164164

0 commit comments

Comments
 (0)