Workflowy Sort

Alphabetically sort open lists in workflowy

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Workflowy Sort
// @namespace    https://workflowy.com
// @version      0.1
// @description  Alphabetically sort open lists in workflowy
// @author       Roland
// @match        https://workflowy.com
// @run-at       document-idle
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // On page load, listen for bullets to populate, then sort them
    var bulletLoaded = setInterval(function(){
        console.log('checking bullets');
        if( document.getElementById('loadingScreen').style.display === 'none' ) {
            console.log('bullets loaded!');
            sortBullets();
            clearInterval(bulletLoaded);
        }
    }, 200);

    // sort bullets on click events (bullet expansion and clicking into a bullet)
    document.addEventListener ("mousedown", autoSort);

    // sort bullets on hotkey press (Ctrl+Shift+S by default)
    document.addEventListener('keydown', keyDownSortBullets);

    function autoSort() {
        $(".bullet, a.content, #expandButton").unbind();
        $('.bullet, a.content').click(function(event){
            setTimeout(function(){
                sortBullets();
                console.log('bullet sort');
            }, 250);
        });
        $('#expandButton').click(function(event){
           // if($(this).attr('data-open-on-last-click') === "true" || typeof $(this).attr('data-open-on-last-click') === 'undefined'){
                setTimeout(function(){
                    sortBullets();
                    console.log('expand sort');
                }, 50);
           // }
        });

    }

    function sortBullets() {
        jQuery.fn.sortDomElements = (function() {
            return function(comparator) {
                return Array.prototype.sort.call(this, comparator).each(function(i) {
                    this.parentNode.appendChild(this);
                });
            };
        })();
        $('div.children').children('.project').sortDomElements(function(a,b){
            var akey = $(a).children('.name').children('.content').text().toLowerCase();
            var bkey = $(b).children('.name').children('.content').text().toLowerCase();
            if (akey == bkey) return 0;
            if (akey < bkey) return -1;
            if (akey > bkey) return 1;
        });
        return true;
    }

    function keyDownSortBullets(e) {
        if(e.ctrlKey && e.shiftKey && e.keyCode === 83){
            e.preventDefault();
            console.log('sorting...');
            sortBullets();
            return false;
            //saveAll();
        }
    }
})();