-
Notifications
You must be signed in to change notification settings - Fork 1
/
workflowy-sort.js
90 lines (83 loc) · 2.98 KB
/
workflowy-sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// ==UserScript==
// @name Workflowy Sort
// @namespace http://rsynnest.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);
// }
});
$('#searchCancel').click(function(event){
setTimeout(function(){
sortBullets();
console.log('searchCancel 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();
setTimeout(function(){
console.log('sorting...');
sortBullets();
}, 25);
return false;
//saveAll();
}
if(e.keyCode === 27){
setTimeout(function(){
sortBullets();
console.log('sorting');
}, 25);
}
}
})();