Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UserScript to sort WorkFlowy nodes using Ctrl+Shft+S

Sort WorkFlowy

  • IMPORTANT! v2.3 updated to new WorkFlowy API. For now, it only works on beta.workflowy.com
  • See blog post for more info.
  • Click here to install into your script manager.
  • Get bookmarklet version here.
// ==UserScript==
// @name Sort WorkFlowy
// @namespace https://rawbytz.wordpress.com
// @version 2.3.0
// @description Use Ctrl+Shift+S to sort the current zoom level children.
// @author rawbytz
// @match https://beta.workflowy.com/*
// @updateUrl https://gist.github.com/rawbytz/3b295652c24738b044dd4d4ee8b4a0e3/raw/sortWorkFlowy.user.js
// @downloadUrl https://gist.github.com/rawbytz/3b295652c24738b044dd4d4ee8b4a0e3/raw/sortWorkFlowy.user.js
// @grant none
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
function sortMe(maxChildren) {
function toastMsg(str, ms, err) {
WF.showMessage("<b>" + str + "</b>", err);
var t = ms ? ms : 2000;
setTimeout(function () { WF.hideMessage() }, t);
}
function sortAndMove(arr, reverse) {
arr.sort(function (a, b) {
return reverse ? b.getNameInPlainText().localeCompare(a.getNameInPlainText()) : a.getNameInPlainText().localeCompare(b.getNameInPlainText());
});
WF.editGroup(function () {
for (var i = 0; i < arr.length; i++) {
if (arr[i].getPriority() !== i) {
applyLocalMoveForProjectReferences([arr[i]], parent, i);
}
}
});
var dir = reverse ? "Z-A." : "A-Z.";
toastMsg("Sorted " + dir);
}
if (WF.currentSearchQuery()) {
return void toastMsg("Sorting is disabled when search is active.", 3000, true);
}
var parent = WF.currentItem();
if (parent.childrenAreInReadOnlyTree()) {
return void toastMsg("This outline is View-only.", 3000, true);
}
var children = parent.getChildren();
if (children.length < 2) {
return void toastMsg("Nothing to sort.", 3000, true);
}
if (children.length > maxChildren) {
return void toastMsg("Sorting more than " + maxChildren + " children upsets the WorkFlowy gods, and has been disabled.", 5000, true);
}
var parentName = parent.isMainTreeRoot() ? "Home" : parent.getName();
$(".ui-dialog:visible > .ui-dialog-content").dialog("close");
WF.showAlertDialog("Sort <b>" + children.length + "</b> children?", parentName, true, [{
text: "Sort A-Z",
click: function () {
$(this).dialog("close");
setTimeout(function () {
sortAndMove(children);
}, 50);
}
}, {
text: "Sort Z-A",
click: function () {
$(this).dialog("close");
setTimeout(function () {
sortAndMove(children, true);
}, 50);
}
}], "Cancel");
}
document.addEventListener("keydown", function (event) {
if (!event.altKey && event.ctrlKey && event.shiftKey && !event.metaKey) {
switch (event.keyCode) {
case 83: // Ctrl+Shft+S
sortMe(400);
break;
default:
break;
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment