ScratchAddons icon indicating copy to clipboard operation
ScratchAddons copied to clipboard

New addon: sort assets by alphabetical order

Open Jazza-231 opened this issue 1 year ago • 37 comments

Resolves suggestion from the community server

Changes

Adds an option to the edit menu to sort the assets by alphabetical order.

Reason for changes

In a future version of this when I make it, it could be used to fix the order of broken gifs? will add settings for different sorting methods, current is basic js array sort.

Tests

works on brave for both costumes and sounds, sprites are not done yet, and i need pointers on how to go about that. if its not feasible, sprites will be ommited (they arent that useful to order anyway)

Jazza-231 avatar Mar 03 '24 05:03 Jazza-231

Alphabetical Current implementation allows costumes and sounds to be sorted using .sort()

Jazza-231 avatar Mar 03 '24 05:03 Jazza-231

I have implemented a better sorting system, which uses natural sort. this is just...better, but it is slower, and when a lot of things are sorting, it can take a while. I am not sure if this can be improved. besides that, sprites still need to be done, and for consistency i would like to find a way to make the menu close when the item is clicked on while it is greyed out (which you will see when you click on the grayed out "restore" button) i am not sure how to do either of those, and believe me i have tried. if you have ideas, please tell me, and if you think those things are fine as is, i guess no changes to them need to be made

Jazza-231 avatar Mar 03 '24 11:03 Jazza-231

https://github.com/ScratchAddons/ScratchAddons/assets/81956724/913d45d6-f2f9-4e07-b39e-f187ed71e8dc

oh, and anyone know why it doesnt come back when the addon is reenabled while the menu is open and it was disabled before the menu was opened, but it does come back when it was enabled before the menu was opened, disabled when the menu was open, and reenabled when the menu was still open?

Jazza-231 avatar Mar 03 '24 11:03 Jazza-231

For sprite sorting a function exists similar to reorderSound or reorderCostume, it's called reorderTarget.

Joeclinton1 avatar Mar 03 '24 12:03 Joeclinton1

For sprite sorting a function exists similar to reorderSound or reorderCostume, it's called reorderTarget.

oh my god thank you how did i not see that

Jazza-231 avatar Mar 03 '24 12:03 Jazza-231

(latest commit does not work)

Jazza-231 avatar Mar 03 '24 12:03 Jazza-231

ok im pretty sure it works, sprites are done, the performance is actually really good, now just need to figure out how to close that menu

Jazza-231 avatar Mar 03 '24 12:03 Jazza-231

Chatgpt thinks your natural sort can be done by using localCompare, which would make it alot more concise. Could you test?

Array.prototype.naturalSort = function() {
    return this.sort((a, b) => 
        String(a).localeCompare(String(b), undefined, {numeric: true, sensitivity: 'base'})
    );
};

Joeclinton1 avatar Mar 03 '24 13:03 Joeclinton1

Chatgpt thinks your natural sort can be done by using localCompare, which would make it alot more concise. Could you test?

Array.prototype.naturalSort = function() {
    return this.sort((a, b) => 
        String(a).localeCompare(String(b), undefined, {numeric: true, sensitivity: 'base'})
    );
};

well ill be damned it seems to sort the same

Jazza-231 avatar Mar 03 '24 13:03 Jazza-231

ok im pretty sure it works, sprites are done, the performance is actually really good, now just need to figure out how to close that menu

See here

You need to get the menu-bar and call this.props.onRequestCloseEdit()

Edit: if you look at what onRequestCloseEdit calls, you can probably close the menu with a redux dispatch

Joeclinton1 avatar Mar 03 '24 14:03 Joeclinton1

I like thatOn Mar 3, 2024, at 9:13 AM, Joe Clinton @.***> wrote:

ok im pretty sure it works, sprites are done, the performance is actually really good, now just need to figure out how to close that menu

See here You need to get the menu-bar and call this.props.onRequestCloseEdit()

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

BlueGoat11 avatar Mar 03 '24 14:03 BlueGoat11

ok im pretty sure it works, sprites are done, the performance is actually really good, now just need to figure out how to close that menu

See here

You need to get the menu-bar and call this.props.onRequestCloseEdit()

Edit: if you look at what onRequestCloseEdit calls, you can probably close the menu with a redux dispatch

How do you find this shit, I looked so hard...I can't do this tonight so remind me to do it tmr

Jazza-231 avatar Mar 03 '24 14:03 Jazza-231

I am happy to have figured out that redux event on my own, redux is one of my weaknesses so that felt good, anyway i think this is all done now!

Jazza-231 avatar Mar 04 '24 03:03 Jazza-231

  1. Done
  2. Done
  3. See discord reply
  4. Done

Jazza-231 avatar Mar 06 '24 04:03 Jazza-231

Has a bug

image

Edit Menu also doesn't close anymore when clicked. Might be caused by this bug.

Joeclinton1 avatar Mar 06 '24 05:03 Joeclinton1

Has a bug

image

Edit Menu doesn't close when option is clicked due to this bug. Tried to reorder sprites for context, but also happens with the other tabs.

you were on the editor tab yeah? i think i know why this is happening

Jazza-231 avatar Mar 06 '24 05:03 Jazza-231

should be fixed now, i cant repo the edit menu not closing as it always does for me, but this should be working now.

EDIT: nope, the edit menu closing was something else, fixing that too in the next commit (it currently doesnt close after it succesfully orders them, it should.)

Jazza-231 avatar Mar 06 '24 05:03 Jazza-231

I think this just needs reviews and approvals

Jazza-231 avatar Mar 25 '24 01:03 Jazza-231

I think this just needs reviews and approvals

Joeclinton1 has already approved this pull request, so you don't have to request another review until you make changes.

DNin01 avatar Mar 25 '24 02:03 DNin01

What's the expected behavior of Ctrl+Z/undo after sorting alphabetically?

WorldLanguages avatar Apr 08 '24 17:04 WorldLanguages

What's the expected behavior of Ctrl+Z/undo after sorting alphabetically?

Well one would assume it will undo the reordering.. I'm not sure if this actually happens, I think all I need to do is call some function in the VM and that will save it as a snapshot that can be undone.

Jazza-231 avatar Apr 09 '24 00:04 Jazza-231

What's the expected behavior of Ctrl+Z/undo after sorting alphabetically?

Are you talking about the undo buttons in the costume editor?

Ctrl+Z can't usually undo costume deletions, additions, or reorders, AFAIK.

DNin01 avatar Apr 09 '24 01:04 DNin01

Ctrl+Z can't usually undo costume deletions, additions, or reorders, AFAIK.

That is correct, but perhaps in this case it should? Or maybe change the sort button to be restore previous order?

Jazza-231 avatar Apr 09 '24 01:04 Jazza-231

Ctrl+Z can't usually undo costume deletions, additions, or reorders, AFAIK.

Well, I meant Edit > Restore Costume then. Of course, Scratch won't let you undo reorders, because you can only do one reorder at a time, so it's trivial to undo it manually. But if we're going to do multiple reorders with a click of a button, it sounds sensible to add a way to undo it...

WorldLanguages avatar Apr 09 '24 16:04 WorldLanguages

Related addon idea: notification with undo button when deleting things or doing other destructive actions, including alphabetizing a sprite's costumes or sounds.

DNin01 avatar Apr 09 '24 18:04 DNin01

Ctrl+Z can't usually undo costume deletions, additions, or reorders, AFAIK.

Well, I meant Edit > Restore Costume then. Of course, Scratch won't let you undo reorders, because you can only do one reorder at a time, so it's trivial to undo it manually. But if we're going to do multiple reorders with a click of a button, it sounds sensible to add a way to undo it...

I am about to add functionality to the edit menu to restore the previous order using restore

Jazza-231 avatar Jun 16 '24 03:06 Jazza-231

The while loop which keeps checking if it isSorted is really questionable. You should just sort it once, then directly sort it with reorder, there's no need to check if it's actually sorted if you could just sort it correctly in one pass. There absolutely should not be anything checking iteration counts.

I probably shouldn't have approved this, I missed the while loop logic.

Feel free to commit changes, ill give you contributor access. I seem to be having trouble with the whole idea of sorting and I'm not sure why

Jazza-231 avatar Jun 16 '24 09:06 Jazza-231

The while loop which keeps checking if it isSorted is really questionable. You should just sort it once, then directly sort it with reorder, there's no need to check if it's actually sorted if you could just sort it correctly in one pass. There absolutely should not be anything checking iteration counts. I probably shouldn't have approved this, I missed the while loop logic.

Feel free to commit changes, ill give you contributor access. I seem to be having trouble with the whole idea of sorting and I'm not sure why

If I'm making changes i'm going to redo most of your logic.

Before I do that, I'll just say how I would do it. I want the lists (this.sprite.sounds, this.sprite.costumes, this.runtime.targets) directly sorted with natural sort. No sorting of names or anything, just a direct sort and then a projectEmitUpdate.

Joeclinton1 avatar Jun 16 '24 09:06 Joeclinton1

If I'm making changes i'm going to redo most of your logic

Fair enough, if you want to do that then go ahead, feel free to add yourself to the credits too, I am genuinely fully stumped as to why its not working

Jazza-231 avatar Jun 16 '24 09:06 Jazza-231

I did the rewrite, seems to work now.

Perhaps an option could be added to settings for descending vs ascending sort?

Also the addon should be renamed to "sort assets by name" since "sort by alphabetical order" implies the existence of other sorts

Joeclinton1 avatar Jun 18 '24 00:06 Joeclinton1