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 #3335: Detect and handle unavailable extensions in dragged PNGs #3354

Merged
merged 2 commits into from
Feb 7, 2025

Conversation

d1vyanshu-kumar
Copy link
Contributor

General items:

If your code changes how something works on the device (i.e., it affects the companion):

  • I branched from master
  • My pull request has master as the base

For all other changes:

  • I branched from master
  • My pull request has master as the base

What does this PR accomplish?

- To solve the issue of importing blocks from a PNG file that requires extensions not present in the project, I added the following steps:

1. Extract Block Types: created a function extractBlockTypes to extract the block types from the XML data embedded in the PNG file.
2. Validate Block Types: created a function validateBlockTypes to check if the extracted block types are available in the component database of the workspace.
3. Show Missing Extensions Dialog: created a function showMissingExtensionDialog to display an alert dialog listing the missing extensions if any.
4. Modify importPngAsBlock: modified the importPngAsBlock function to:

  • Extract block types from the XML.
  • Validate the block types against the component database.
  • Show an error dialog and abort the import if any required extensions are missing.

Fixes #3335

@AppInventorWorkerBee
Copy link
Collaborator

Can one of the admins verify this patch?

@d1vyanshu-kumar
Copy link
Contributor Author

3335-2.mov

@d1vyanshu-kumar
Copy link
Contributor Author

Hi @ewpatton and @SusanRatiLane,

Could you please review my pull request?
Thank you!

@ewpatton
Copy link
Member

@AppInventorWorkerBee ok to test

});

if (missingExtensions.length > 0) {
var message = 'This image references the following blocks which require extensions that are not present in the project:\n\n';
Copy link
Member

Choose a reason for hiding this comment

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

This should either be added to the Blockly.Msg object and a corresponding entry in the translation file, or we should add it to OdeMessages.java and figure out how to get it here so that it can be translated into the user's language.

*/
function validateBlockTypes(blockTypes, workspace) {
if (!blockTypes || blockTypes.length === 0) {
console.warn('No block types provided for validation.');
Copy link
Member

Choose a reason for hiding this comment

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

I don't think a warning is necessary here. It's logical that the empty set of block types is valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will remove this warning.

}

var componentDb = workspace.getComponentDatabase();
if (!componentDb) {
Copy link
Member

Choose a reason for hiding this comment

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

Note that the component database is allocated during the setup of the workspace, so this should never be true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok. I will remove this statement

Comment on lines 527 to 529
var blocks = xml.getElementsByTagName('block');
for (var i = 0; i < blocks.length; i++) {
var mutation = blocks[i].getElementsByTagName('mutation')[0];
Copy link
Member

Choose a reason for hiding this comment

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

Note that getElementsByTagName gets all descendants, so you could just do xml.getElementsByTagName('mutation') to get all of the mutations. Otherwise, this code ends up being quadratic in its runtime since for each block you may be looking up mutations on its children, grandchildren, etc. not just its own mutation.

Copy link
Member

@ewpatton ewpatton left a comment

Choose a reason for hiding this comment

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

See inline comments.

@d1vyanshu-kumar
Copy link
Contributor Author

Hi @ewpatton,

Thank you so much for reviewing my PR. I'll make the requested changes and submit an updated PR soon.

@d1vyanshu-kumar
Copy link
Contributor Author

@ewpatton I have made the requested changes. Please review and let me know if anything else is needed.

Copy link
Member

@ewpatton ewpatton left a comment

Choose a reason for hiding this comment

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

LGTM

@ewpatton ewpatton merged commit 62d3269 into mit-cml:master Feb 7, 2025
1 check passed
@d1vyanshu-kumar d1vyanshu-kumar deleted the fix-missing-extensions-png branch February 7, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect unavailable extensions in dragged PNGs
3 participants