JS editor update: Ability to convert JS code from editor to blocks (simple flow, pitch, tone, and rhythm blocks)#4591
Conversation
|
✅ All Jest tests passed! This PR is ready to merge. |
|
@ebeetles I m bit confused with this like how this actually works ? |
Since this doesn't work on all blocks yet, this won't actually be visible to the user yet. To test the functionality for yourself locally, just add the following function in jseditor.js and call it in _runcode() shown below. All you need to do now in the webapp is to write code in the jseditor and then press run, which will generate the according blocks. |
I recommend that you make the feature functional within this branch without the need to change anything. The way our development works, it won't be visible to the user until it gets merged into master. |
Thank you for the reply! I'm asking this because if I update jseditor.js as part of this pull request and it gets merged, it won't work for many blocks as of now. |
The way we typically do a large project like this, especially one that requires considerable refactoring, is in one PR. We can continue testing it throughout. The other consideration, however, is: do you expect that changes you make will touch upon files other than |
|
I think it is pretty interesting to be able to go back and forth between the block editor and the JS editor. But please consider @pikurasa 's suggestion. |
|
Thank you @walterbender and @pikurasa! I’m happy to follow suggestions on best practice. Also, I’d like to answer @pikurasa’s question. For the JavaScript-to-MusicBlocks conversion functionality, I expect to add / change the following files:
My first commit is a proof-of-concept supporting around 30 block types, including start, settimbre, newnote, pitch (solfege only), if, for, all number blocks (supporting arbitrary expressions like 1 / 2, Math.abs(-1) / 2, MathUtility.doRandom(1, 2) / 4, etc.), and all boolean blocks (supporting arbitrary boolean expressions like MathUtility.doRandom(0, 1) == 1, false | MathUtility.doRandom(0, 1) > 0, etc.). However, the total number of blocks to be supported is probably more than 10x this current amount. I was originally going to make small changes and get them reviewed and submitted, since it seems faster and easier to review smaller changes. My plan was to only integrate the functionality with jseditor.js at the end and heavily rely on unit tests in the meantime (we should know exactly what the block list looks like for each snippet of JS code) so the earlier changes would've only contained ast2blocklist.js and ast2blocklist.test.js. I’m open to completing the entire implementation in one PR, but I'm a little confused about the review process since I would prefer to receive timely feedback throughout the development rather than a single lengthy review at the end. This is my first time contributing to an open-source project, so please forgive me if my question seems ignorant due to lack of experience with this environment. |
|
I think we should take the following approach: |
Sounds good! I currently have basic note, pitch, arithmetic functions, start, and repeat blocks working. I would like to add the action block before the full initial PR. Am I correct in assuming that the action block serves as a function in the MusicBlocks language? Thank you! |
|
yes. FWIW, you should probably use MB to write a few programs so as to get a better understanding. |
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Hi @walterbender @pikurasa ! The action block conversion is complete (also works for recursion cases). Please see the test cases in js/js-export/test/ast2blocklist.test.js. I have also included all the other file changes accordingly that allows testing the functionality in the JS editor. To test locally, change the enableCodeToBlocks boolean in line 423 of js/widgets/jseditor.js to true. Please review, and see if the current comments are enough to understand the changes. Also please let me know if you'd prefer a document for explanation. Thanks! |
|
Can you please remind me how to test this? |
To test, simply change the enableCodeToBlocks boolean in line 423 of js/widgets/jseditor.js to true. Then, type valid JS code in the JS editor and click the play button in the editor, which will update the canvas with the according blocks. The screenrecording I originally attached is an example of how to test it. Please let me know if there are further questions! |
|
Works well. I think we'll want to make this a separate action from Play but I think it is a nice direction for the JavaScript editor. |
Thanks, the play button is just a temporary placeholder for testing purposes, but I will definitely come up with something different later. |
|
❌ Some Jest tests failed. Please check the logs and fix the issues before merging. Failed Tests: |
|
✅ All Jest tests passed! This PR is ready to merge. |
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Hi @walterbender and @pikurasa, |
|
Maybe you can add a UX element (button) so we can merge this, even incomplete. I think it is really fun. |
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Hi @walterbender , |
js/widgets/jseditor.js
Outdated
| convertBtn.onclick = this._codeToBlocks.bind(this); | ||
| menuLeft.appendChild(convertBtn); | ||
| menubar.appendChild(menuLeft); | ||
| generateTooltip(convertBtn, "Convert JavaScript to Blocks"); |
There was a problem hiding this comment.
please put the string inside _() so it is available for translation.
There was a problem hiding this comment.
Thanks, the commit has been made
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Not sure why it is looking for toolbar.test.js. It is in the master branch, but you checked out your branch before that. Maybe a rebase will help? |
meganindya
left a comment
There was a problem hiding this comment.
Perhaps unrelated, but I'm getting an error on clicking the button for code to blocks.
Uncaught (in promise) Error: Not enough space to save locally
at ProjectStorage.set (ProjectStorage.js:186:19)
at async ProjectStorage.save (ProjectStorage.js:207:9)We should address this separately, but for things like these, we should consider using the IndexedDB or the Cache API instead of LocalStorage.
Had a few comments in the code. But otherwise fantastic work!
|
@walterbender @pikurasa @meganindya, thank you so much for the reviews. I have lots of finals this week so I will be very busy the next few days, but I will find time to fix the issues ASAP. |
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Hi @walterbender, @pikurasa, @meganindya, I ran git pull upstream master --rebase before making new changes, and now it seems like the commits in this PR includes commits from everyone else. I'm not very familiar with using git, so is there a command to remove everyone else's commits, or do I have to clean up the PR and restart? If I need to restart, how should I do so to not lose the review comments? My last commit does include everything to address the review comments, (v-spacers, minified acorn library, and removed driver code from ast2blocklist.js). Thanks! |
|
Hmm. I usually just |
|
✅ All Jest tests passed! This PR is ready to merge. |
…t can be loaded into the MusicBlocks editor. This is the first commit for such functionality, which allows users to write JavaScript code in the JS Editor directly and convert the code back to music blocks. Blocks supported in this commit are: 1. settimbre 2. newnote 3. pitch (only supports solfege) 4. if 5. for 6. All number blocks. Supports arbitrary expressions like 1 / 2, Math.abs(-1) / 2, MathUtility.doRandom(1, 2) / 4, etc. 7. All boolean blocks. Supports arbitrary boolean expressions like MathUtility.doRandom(0, 1) == 1, false | MathUtility.doRandom(0, 1) == 1, etc. 8. Action (supports recursion) 9. Dictionary (setValue, getValue, function overloading) New files in the commit: 1. ast2blocklist.js - main logic to convert an AST generated from JavaScript code to block list format that can be loaded into the block editor 2. ast2blocklist.test.js - unit tests that cover all supported blocks conversion 3. acorn.js - library that converts JavaScript code to AST
…musicblocks. Updated structure of ast2blocklist.js for better readability, and makes adding more block support easier. Minor change: Play pitch now supports notes (A-G) in addition to solfege.
|
✅ All Jest tests passed! This PR is ready to merge. |
…guments, and adds v-spacers based on the information 2. Instead of console.error for errors, it now throws error and shows in console panel of jseditor 3. Uses minified version of acorn library for faster loading 4. Removed driver code from ast2blocklist
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Hi @walterbender @pikurasa @meganindya, Also @meganindya, regarding your error in this comment #4591 (review), I am unable to reproduce it. If you can still reproduce the error, may I see the whole stack trace so I will know which line of my code caused it? |
|
I've not tried anything too sophisticated, but it is working for me. |
|
There are some linting errors. (I thought we had things arranged so that libraries don't get linted -- we don't want to fix acorn.min.js here.) |
|
✅ All Jest tests passed! This PR is ready to merge. |



AST2BlockList function enables user to convert JS code from the JS editor into simple flow, pitch, tone, and rhythm blocks.
Screen-Recording.mp4