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 button not working on Gandi #98

Merged
merged 3 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
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
27 changes: 14 additions & 13 deletions src/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,6 @@ class EurekaLoader {
break;
}

// Use for avoid conflict between different extensions use one button func.
const prefix = extensionInfo?.id;
if (!prefix) {
warn(`Cannot assign prefix for button: ${blockInfo.func}`);
break;
}
const callbackName = `${prefix}_${blockInfo.func}`;

const funcName = blockInfo.func;
const buttonCallback = (() => {
// Maybe there's a worker
Expand All @@ -518,8 +510,8 @@ class EurekaLoader {
extensionObject[funcName]();
})();
// @ts-expect-error internal hack
blockInfo.callback = buttonCallback;
blockInfo.func = callbackName;
blockInfo.callFunc = buttonCallback;
blockInfo.func = funcName;
break;
}
case BlockType.LABEL:
Expand Down Expand Up @@ -632,15 +624,24 @@ class EurekaLoader {
// @ts-expect-error private method
this.vm.runtime._convertButtonForScratchBlocks = function (
buttonInfo: ExtensionBlockMetadata,
categoryInfo: ExtensionMetadata,
...args: unknown[]
) {
const supportedCallbackKeys = ['MAKE_A_LIST', 'MAKE_A_PROCEDURE', 'MAKE_A_VARIABLE'];
if (window.eureka.blockly && !supportedCallbackKeys.includes(buttonInfo.func!)) {
if (window.eureka.blockly && buttonInfo.func && !supportedCallbackKeys.includes(buttonInfo.func)) {
const workspace = window.Blockly.getMainWorkspace();
// @ts-expect-error lazy to extend Runtime interface
const extensionMessageContext = this.makeMessageContextForTarget();
const buttonText = maybeFormatMessage(buttonInfo.text, extensionMessageContext)!;

// @ts-expect-error lazy to extend ExtensionBlockMetadata interface
workspace.registerButtonCallback(buttonInfo.func, buttonInfo.callback);
workspace.registerButtonCallback(`${categoryInfo.id}_${buttonInfo.func}`, buttonInfo.callFunc);
return {
info: buttonInfo,
xml: `<button text="${xmlEscape(buttonText)}" callbackKey="${xmlEscape(`${categoryInfo.id}_${buttonInfo.func}`)}"></button>`
};
}
return origConvButtonFunc.call(this, buttonInfo, ...args);
return origConvButtonFunc.call(this, buttonInfo, categoryInfo, ...args);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/maybe-format-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MenuItemFunction } from '../typings';
* @param {string} [locale] - the locale to pass to `formatMessage` if it gets called.
* @return {string|*} - the formatted message OR the original `maybeMessage` input.
*/
export const maybeFormatMessage = function <T extends Record<string, unknown>> (maybeMessage?: T | string | MenuItemFunction, args?: object, locale?: Locales) {
export const maybeFormatMessage = function <T extends Record<string, unknown> | string | MenuItemFunction> (maybeMessage?: T, args?: object, locale?: Locales) {
if (maybeMessage && typeof maybeMessage === 'object' && maybeMessage.id && maybeMessage.default) {
return formatMessage(maybeMessage as unknown as MessageObject, args, locale);
}
Expand Down
Loading