Skip to content

Commit ece2c94

Browse files
authored
Merge branch 'sugarlabs:master' into master
2 parents dc26c6b + fdbef2f commit ece2c94

File tree

4 files changed

+409
-179
lines changed

4 files changed

+409
-179
lines changed

js/activity.js

Lines changed: 119 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,38 +1393,26 @@ class Activity {
13931393
* Switches between beginner/advanced mode
13941394
*/
13951395
const doSwitchMode = (activity) => {
1396-
activity._doSwitchMode();
1397-
};
1398-
1399-
/**
1400-
* Switches between beginner and advanced modes.
1401-
* Displays a message prompting browser refresh to apply mode change.
1402-
* @private
1403-
*/
1404-
this._doSwitchMode = () => {
1405-
this.blocks.activeBlock = null;
1406-
const mode = this.storage.beginnerMode;
1407-
1408-
const MSGPrefix =
1409-
"<a href='#' " +
1410-
"onClick='window.location.reload()'" +
1411-
"onMouseOver='this.style.opacity = 0.5'" +
1412-
"onMouseOut='this.style.opacity = 1'>";
1413-
const MSGSuffix = "</a>";
1414-
1415-
if (mode === null || mode === undefined || mode === "true") {
1416-
this.textMsg(
1417-
_(MSGPrefix + _("Refresh your browser to change to advanced mode.") + MSGSuffix)
1418-
);
1419-
this.storage.setItem("beginnerMode", false);
1420-
} else {
1421-
this.textMsg(
1422-
_(MSGPrefix + _("Refresh your browser to change to beginner mode.") + MSGSuffix)
1423-
);
1424-
this.storage.setItem("beginnerMode", true);
1396+
// Update toolbar
1397+
activity.toolbar.renderSaveIcons(
1398+
activity.save.saveHTML.bind(activity.save),
1399+
doSVG,
1400+
activity.save.saveSVG.bind(activity.save),
1401+
activity.save.savePNG.bind(activity.save),
1402+
activity.save.saveWAV.bind(activity.save),
1403+
activity.save.saveLilypond.bind(activity.save),
1404+
activity.save.saveAbc.bind(activity.save),
1405+
activity.save.saveMxml.bind(activity.save),
1406+
activity.save.saveBlockArtwork.bind(activity.save)
1407+
);
1408+
1409+
// Regenerate palettes
1410+
if (activity.regeneratePalettes) {
1411+
activity.regeneratePalettes();
14251412
}
1426-
1427-
this.refreshCanvas();
1413+
1414+
// Force immediate canvas refresh
1415+
activity.refreshCanvas();
14281416
};
14291417

14301418
/*
@@ -6402,12 +6390,9 @@ class Activity {
64026390
this.toolbar.renderPlanetIcon(this.planet, doOpenSamples);
64036391
this.toolbar.renderMenuIcon(showHideAuxMenu);
64046392
this.toolbar.renderHelpIcon(showHelp);
6405-
this.toolbar.renderModeSelectIcon(doSwitchMode);
6393+
this.toolbar.renderModeSelectIcon(doSwitchMode, doRecordButton, doAnalytics, doOpenPlugin, deletePlugin, setScroller);
64066394
this.toolbar.renderRunSlowlyIcon(doSlowButton);
64076395
this.toolbar.renderRunStepIcon(doStepButton);
6408-
this.toolbar.renderAdvancedIcons(
6409-
doRecordButton, doAnalytics, doOpenPlugin, deletePlugin, setScroller
6410-
);
64116396
this.toolbar.renderMergeIcon(_doMergeLoad);
64126397
this.toolbar.renderRestoreIcon(restoreTrash);
64136398
if (_THIS_IS_MUSIC_BLOCKS_) {
@@ -6963,6 +6948,105 @@ class Activity {
69636948
}
69646949
};
69656950
}
6951+
6952+
/**
6953+
* Saves the current state locally
6954+
* @returns {void}
6955+
*/
6956+
saveLocally() {
6957+
try {
6958+
localStorage.setItem('beginnerMode', this.beginnerMode.toString());
6959+
} catch (e) {
6960+
console.error('Error saving to localStorage:', e);
6961+
}
6962+
}
6963+
6964+
/**
6965+
* Regenerates all palettes based on current mode
6966+
* @returns {void}
6967+
*/
6968+
regeneratePalettes() {
6969+
try {
6970+
// Store current palette positions
6971+
const palettePositions = {};
6972+
if (this.palettes && this.palettes.dict) {
6973+
for (const name in this.palettes.dict) {
6974+
const palette = this.palettes.dict[name];
6975+
if (palette && palette.container && typeof palette.container.x !== 'undefined') {
6976+
palettePositions[name] = {
6977+
x: palette.container.x,
6978+
y: palette.container.y,
6979+
visible: !!palette.visible
6980+
};
6981+
}
6982+
}
6983+
}
6984+
6985+
// Safely hide and clear existing palettes
6986+
if (!this.palettes) {
6987+
console.warn('Palettes object not initialized');
6988+
return;
6989+
}
6990+
6991+
if (typeof this.palettes.hide !== 'function') {
6992+
console.warn('Palettes hide method not available');
6993+
} else {
6994+
this.palettes.hide();
6995+
}
6996+
6997+
if (typeof this.palettes.clear !== 'function') {
6998+
console.warn('Palettes clear method not available');
6999+
// Fallback clear implementation
7000+
this.palettes.dict = {};
7001+
this.palettes.visible = false;
7002+
this.palettes.activePalette = null;
7003+
this.palettes.paletteObject = null;
7004+
} else {
7005+
this.palettes.clear();
7006+
}
7007+
7008+
// Reinitialize palettes
7009+
initPalettes(this.palettes);
7010+
7011+
// Reinitialize blocks
7012+
if (this.blocks) {
7013+
initBasicProtoBlocks(this);
7014+
}
7015+
7016+
// Restore palette positions
7017+
if (this.palettes && this.palettes.dict) {
7018+
for (const name in palettePositions) {
7019+
const palette = this.palettes.dict[name];
7020+
const pos = palettePositions[name];
7021+
7022+
if (palette && palette.container && pos) {
7023+
palette.container.x = pos.x;
7024+
palette.container.y = pos.y;
7025+
7026+
if (pos.visible) {
7027+
palette.showMenu(true);
7028+
}
7029+
}
7030+
}
7031+
}
7032+
7033+
// Update the palette display
7034+
if (this.palettes && typeof this.palettes.updatePalettes === 'function') {
7035+
this.palettes.updatePalettes();
7036+
}
7037+
7038+
// Update blocks
7039+
if (this.blocks && typeof this.blocks.updateBlockPositions === 'function') {
7040+
this.blocks.updateBlockPositions();
7041+
}
7042+
7043+
this.refreshCanvas();
7044+
7045+
} catch (e) {
7046+
console.error('Error regenerating palettes:', e);
7047+
this.errorMsg(_('Error regenerating palettes. Please refresh the page.'));
7048+
}
7049+
}
69667050
}
69677051

69687052
const activity = new Activity();

js/palette.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,53 @@ class Palettes {
385385
docById("palette").style.visibility = "visible";
386386
}
387387

388+
clear() {
389+
try {
390+
// First hide all palettes
391+
for (const name in this.dict) {
392+
if (this.dict.hasOwnProperty(name)) {
393+
const palette = this.dict[name];
394+
if (palette && typeof palette.hideMenu === 'function') {
395+
palette.hideMenu();
396+
}
397+
}
398+
}
399+
400+
// Remove the palette DOM element if it exists
401+
const paletteElement = docById("palette");
402+
if (paletteElement) {
403+
paletteElement.parentNode.removeChild(paletteElement);
404+
}
405+
406+
// Clear the dictionary and reset state
407+
this.dict = {};
408+
this.visible = false;
409+
this.activePalette = null;
410+
this.paletteObject = null;
411+
412+
// Recreate the palette using the original initialization code
413+
const element = document.createElement("div");
414+
element.id = "palette";
415+
element.setAttribute("class", "disable_highlighting");
416+
element.classList.add('flex-palette');
417+
element.setAttribute(
418+
"style",
419+
`position: fixed; z-index: 1000; left: 0px; top: ${60+this.top}px; overflow-y: auto;`
420+
);
421+
element.innerHTML =
422+
'<div style="height:fit-content"><table width ="' +
423+
1.5 * this.cellSize +
424+
'px"bgcolor="white"><thead><tr></tr></thead></table><table width ="' +
425+
4.5 * this.cellSize +
426+
'px"bgcolor="white"><thead><tr><td style= "width:28px"></tr></thead><tbody></tbody></table></div>';
427+
element.childNodes[0].style.border = `1px solid ${platformColor.selectorSelected}`;
428+
document.body.appendChild(element);
429+
430+
} catch (e) {
431+
console.error('Error clearing palettes:', e);
432+
}
433+
}
434+
388435
setBlocks(blocks) {
389436
this.blocks = blocks;
390437
return this;

js/piemenus.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3652,20 +3652,34 @@ const piemenuGrid = (activity) => {
36523652
activity.turtles._exitWheel.slicePathCustom.minRadiusPercent = 0.0;
36533653
activity.turtles._exitWheel.slicePathCustom.maxRadiusPercent = 0.3;
36543654
activity.turtles._exitWheel.sliceSelectedPathCustom =
3655-
activity.turtles._exitWheel.slicePathCustom;
3655+
activity.turtles._exitWheel.slicePathCustom;
36563656
activity.turtles._exitWheel.sliceInitPathCustom = activity.turtles._exitWheel.slicePathCustom;
36573657
activity.turtles._exitWheel.clickModeRotate = false;
36583658
activity.turtles._exitWheel.createWheel(["×", " "]);
36593659

36603660
activity.turtles._exitWheel.navItems[0].navigateFunction = () => {
3661-
docById("wheelDivptm").style.display = "none";
3662-
activity.turtles.gridWheel.removeWheel();
3663-
activity.turtles._exitWheel.removeWheel();
3661+
hidePiemenu(activity);
36643662
};
36653663

36663664
if (docById("helpfulWheelDiv").style.display !== "none") {
36673665
docById("helpfulWheelDiv").style.display = "none";
36683666
}
3667+
3668+
const hidePiemenu = (activity) => {
3669+
docById("wheelDivptm").style.display = "none";
3670+
activity.turtles.gridWheel.removeWheel();
3671+
activity.turtles._exitWheel.removeWheel();
3672+
};
3673+
3674+
const clickOutsideHandler = (event) => {
3675+
const piemenu = docById("wheelDivptm");
3676+
if (!piemenu.contains(event.target)) {
3677+
hidePiemenu(activity);
3678+
document.removeEventListener("mousedown", clickOutsideHandler);
3679+
}
3680+
};
3681+
3682+
document.addEventListener("mousedown", clickOutsideHandler);
36693683
};
36703684

36713685
const piemenuKey = (activity) => {
@@ -4000,4 +4014,4 @@ const piemenuKey = (activity) => {
40004014
if (j !== -1) {
40014015
modenameWheel.navigateWheel(j);
40024016
}
4003-
};
4017+
};

0 commit comments

Comments
 (0)