sort-tabs-advanced icon indicating copy to clipboard operation
sort-tabs-advanced copied to clipboard

Sort tabs with less steps

Open piroor opened this issue 5 years ago • 4 comments

Hello,

Currently this addon tries to move all tabs always, even if they don't need to be moved. This approach may cause some troubles:

  • It may produce too much tab moves. When there are too many tabs (for example, over 1000+) it may take much time.
  • It may scramble tabs unexpectedly and unnecessarily. It is critically dangerous on some kinds of addons (like Tree Style Tab).

So, instead only minimum tabs should be moved based on the algorithm of "diff". This PR contains a ported version of diff and changes to move tabs based on the calculated difference. The main logic is ported from Tree Style Tab, and this PR is based on my another PR for Firefox Multi-Account Containers.

How about this change?

piroor avatar Apr 18 '19 08:04 piroor

Here are some additional information for reviewing:

  • The largest addition differ.js is a ported version from the diff module of npm. I just modified it to remove needless codes for this usecase.
  • The differ detects three type differences: "added", "removed", and "unchanged". Rearranging of tabs is based on a logic:
    1. Prepare three arrays: initial order (beforeIds), final order (afterIds), and the current order (currentIds: this is required because tabs.move() is asynchronous and we need to track the order of tabs after tab moves.)
    2. Calculate differences between the initial order and the final order.
    3. Extract only "added" differences. Any "removed" difference should be ignored, because those tabs are moved with corresponding "addded" differences. Of course there is no removed tab while while rearranging.
    4. Calculate destination position of "added" tabs, based on the current order (currentIds).
    5. Move "added" tabs actually via tabs.move().
    6. Update the current order (currentIds) for the next calculation.
    7. Repeat steps 4-7 for all "added" differences.

piroor avatar Apr 18 '19 16:04 piroor

Here is a blog post describing the logic: How to sort tabs smartly on Firefox addons and Chromium extensions? - Qiita

piroor avatar Apr 19 '19 17:04 piroor

@piroor Looks like this addon was abandoned by his author. No updates since 2017. :(

perdolka avatar Oct 19 '19 22:10 perdolka

Thank you for the contribution, this is nice work! Also very cool writeup.

Indeed I had abandoned the addon for a long time, but am back to it. Need a bit of time to integrate your code better, since I made some rearrangements - there is now a background script, in which the sorting happens.

monomon avatar Nov 05 '20 22:11 monomon