Spaces:
Runtime error
Runtime error
/** | |
* Used for the automatic browser full screen setting | |
* and for hiding the scrollbar in full screen. | |
*/ | |
export default async function ({ addon, console }) { | |
const vm = addon.tab.traps.vm; | |
const updateStageSize = () => { | |
document.documentElement.style.setProperty('--sa-fullscreen-width', vm.runtime.stageWidth); | |
document.documentElement.style.setProperty('--sa-fullscreen-height', vm.runtime.stageHeight); | |
}; | |
updateStageSize(); | |
vm.on('STAGE_SIZE_CHANGED', updateStageSize); | |
// "Browser fullscreen" is defined as the mode that hides the browser UI. | |
function updateBrowserFullscreen() { | |
if (addon.settings.get("browserFullscreen") && !addon.self.disabled) { | |
// If Scratch fullscreen is enabled, then browser fullscreen should also | |
// be enabled, and vice versa for disabling. | |
if (addon.tab.redux.state.scratchGui.mode.isFullScreen && document.fullscreenElement === null) { | |
document.documentElement.requestFullscreen(); | |
} else if (!addon.tab.redux.state.scratchGui.mode.isFullScreen && document.fullscreenElement !== null) { | |
document.exitFullscreen(); | |
} | |
} | |
} | |
// "Scratch fullscreen" is defined as the mode normally toggled by the | |
// rightmost button above the stage. | |
function updateScratchFullscreen() { | |
if (addon.settings.get("browserFullscreen") && !addon.self.disabled) { | |
// If browser fullscreen is disabled, then Scratch fullscreen should also | |
// be disabled. | |
if (document.fullscreenElement === null && addon.tab.redux.state.scratchGui.mode.isFullScreen) { | |
addon.tab.redux.dispatch({ | |
type: "scratch-gui/mode/SET_FULL_SCREEN", | |
isFullScreen: false, | |
}); | |
} | |
} | |
} | |
async function setPageScrollbar() { | |
const body = await addon.tab.waitForElement(".sa-body-editor"); | |
if (addon.tab.redux.state.scratchGui.mode.isFullScreen) { | |
body.classList.add("sa-fullscreen"); | |
} else { | |
body.classList.remove("sa-fullscreen"); | |
} | |
} | |
// Properly resize the canvas and scale variable monitors on stage resize. | |
let monitorScaler, resizeObserver, stage; | |
async function initScaler() { | |
monitorScaler = await addon.tab.waitForElement("[class*=monitor-list_monitor-list-scaler]"); | |
stage = await addon.tab.waitForElement('[class*="stage-wrapper_full-screen"] [class*="stage_stage"]'); | |
resizeObserver = new ResizeObserver(() => { | |
const stageSize = stage.getBoundingClientRect(); | |
// When switching between project page and editor, the canvas | |
// is removed from the DOM and inserted again in a different place. | |
// This causes the size to be reported as 0x0. | |
if (!stageSize.width || !stageSize.height) return; | |
// Width and height attributes of the canvas need to match the actual size. | |
const renderer = addon.tab.traps.vm.runtime.renderer; | |
if (renderer) renderer.resize(stageSize.width, stageSize.height); | |
// Scratch uses the `transform` CSS property on a stage overlay element | |
// to control the scaling of variable monitors. | |
const scale = stageSize.width / vm.runtime.stageWidth; | |
monitorScaler.style.transform = `scale(${scale}, ${scale})`; | |
}); | |
resizeObserver.observe(stage); | |
} | |
initScaler(); | |
// Running this on page load handles the case of the project initially | |
// loading in Scratch fullscreen mode. | |
setPageScrollbar(); | |
updateBrowserFullscreen(); | |
// Changing to or from Scratch fullscreen is signified by a state change | |
// (URL change doesn't work when editing project without project page) | |
addon.tab.redux.initialize(); | |
addon.tab.redux.addEventListener("statechanged", (e) => { | |
if (e.detail.action.type === "scratch-gui/mode/SET_FULL_SCREEN") { | |
initScaler(); | |
updateBrowserFullscreen(); | |
setPageScrollbar(); | |
} | |
}); | |
// Changing to or from browser fullscreen is signified by a window resize. | |
window.addEventListener("resize", () => { | |
updateScratchFullscreen(); | |
}); | |
// Handles the case of F11 full screen AND document full screen being enabled | |
// at the same time. | |
document.addEventListener("fullscreenchange", () => { | |
if (document.fullscreenElement === null && addon.tab.redux.state.scratchGui.mode.isFullScreen) { | |
addon.tab.redux.dispatch({ | |
type: "scratch-gui/mode/SET_FULL_SCREEN", | |
isFullScreen: false, | |
}); | |
} | |
}); | |
// These handle the case of the user already being in Scratch fullscreen | |
// (without being in browser fullscreen) when the addon or sync option are | |
// dynamically enabled. | |
addon.settings.addEventListener("change", () => { | |
updateBrowserFullscreen(); | |
}); | |
addon.self.addEventListener("disabled", () => { | |
resizeObserver.disconnect(); | |
}); | |
addon.self.addEventListener("reenabled", () => { | |
resizeObserver.observe(stage); | |
updateBrowserFullscreen(); | |
}); | |
} | |