Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪲 Fix bug in custom Skulpt module extensions #5763

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions static/vendor/skulpt-stdlib-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,71 @@ var $builtinmodule = function (name) {
keyElement.remove()
}, 1500);
}
}
}

var ongoingIfPressedCall = false;

function callIfPressedFunc(name, resolve, reject) {
var f = Sk.misceval.loadname(name, Sk.globals);
var currentProgram = window.sessionStorage.getItem("currentProgram");

Sk.misceval.asyncToPromise(() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh it makes sense! AsyncToPromise is the one to use

Sk.misceval.callOrSuspend(f), {}, currentProgram).then(() => {
resolve();
}).catch((e) => {
reject(e);
}).finally(() => {
ongoingIfPressedCall = false;
});
}

function keyBoardInputPromise(if_pressed_mapping) {
ongoingIfPressedCall = true;
$('#keybinding_modal').show();
return new Promise((resolve, reject) => {
window.addEventListener("keydown", (event) => {
let pressed_mapped_key = false;
try {
let pressed_mapped_key = false;

for (const [key, value] of Object.entries(if_pressed_mapping.entries)) {
// If mapped key is a variable (not char), we retrieve variable value and use that
// otherwise if char, use that.
const charOrVar = value[0].v;
for (const [key, value] of Object.entries(if_pressed_mapping.entries)) {
// if the mapped key is a variable, we retrieve variable value and use that
// if the mapped key is not a variable, use it as a char
const charOrVar = value[0].v;
let mapLetterKey = charOrVar;
if (Object.hasOwn(Sk.globals, charOrVar)) {
if (Sk.globals[charOrVar].hasOwnProperty('v')) {
mapLetterKey = Sk.globals[charOrVar].v;
} else {
mapLetterKey = Sk.globals[charOrVar].$d.entries['data'][1].v;
}
}

let mapLetterKey = charOrVar;
if (Object.hasOwn(Sk.globals, charOrVar)) {
if (Sk.globals[charOrVar].hasOwnProperty('v')) {
mapLetterKey = Sk.globals[charOrVar].v;
} else {
mapLetterKey = Sk.globals[charOrVar].$d.entries['data'][1].v;
if (event.key === `${mapLetterKey}`) {
pressed_mapped_key = true;
callIfPressedFunc(value[1].v, resolve, reject);
}
}

if (event.key === `${mapLetterKey}`){
pressed_mapped_key = true;
Sk.misceval.callOrSuspend(Sk.globals[value[1].v]);
if (!pressed_mapped_key) {
callIfPressedFunc(if_pressed_mapping.entries['else'][1].v, resolve, reject);
}
} catch (err) {
ongoingIfPressedCall = false;
reject(err);
} finally {
$('#keybinding_modal').hide();
}

if (!pressed_mapped_key){
Sk.misceval.callOrSuspend(Sk.globals[if_pressed_mapping.entries['else'][1].v]);
}

$('#keybinding_modal').hide();
resolve();
}, { once: true });
})
}

mod.if_pressed = new Sk.builtin.func(function (if_pressed_mapping) {
document.onkeydown = animateKeys;
return new Sk.misceval.promiseToSuspension(keyBoardInputPromise(
if_pressed_mapping
).then(() => {document.onkeydown = null; return Sk.builtin.none.none$}));
return new Sk.misceval.promiseToSuspension(
keyBoardInputPromise(if_pressed_mapping)
.then(() => { return Sk.builtin.none.none$ })
.finally(() => { document.onkeydown = null;})
);
});

return mod;
Expand Down
Loading