Instantly share code, notes, and snippets.

Embed
What would you like to do?

Sort WorkFlowy

  • Zoom on a bullet.
  • Activate the bookmarklet or use the userscript shortcut Ctrl+Shft+S.
  • A prompt appears with 2 sorting buttons: A-Z and Z-A.
  • Only the zoom level children get sorted.
  • Grandchildren stay with their parents, but are not sorted.
  • Completed children are sorted, even when hidden.
  • After sorting is complete, a message confirms the sort.
  • Undo the sort using WorkFlowy's standard undo feature.
  • For transparency, the bookmarklet will not sort if search is active.
  • To limit the load on WorkFlowy's servers, the maximum number of children that can be sorted is 400.

Links:

// ==UserScript==
// @name Sort WorkFlowy
// @namespace https://rawbytz.wordpress.com
// @version 2.9
// @description Use Ctrl+Shift+S to sort the current zoom level children.
// @author rawbytz
// @match https://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, sec, err) {
WF.showMessage("<b>" + str + "</b>", err);
setTimeout(function () { WF.hideMessage() }, (sec || 2) * 1000);
}
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, 1)
}
if (WF.currentSearchQuery()) {
return void toastMsg("Sorting is disabled when search is active.", 3, true);
}
const parent = WF.currentItem();
const children = parent.getChildren();
if (children.length < 2) {
return void toastMsg("Nothing to sort.", 3, true);
}
if (children.length > maxChildren) {
return void toastMsg("Sorting more than " + maxChildren + " children upsets the WorkFlowy gods, and has been disabled.", 5, true);
}
const title = parent.isMainTreeRoot() ? "Home" : parent.getNameInPlainText();
const body = 'Sort <b>' + children.length + '</b> children?';
const Button1 = 'A-Z';
const Button2 = 'Z-A';
const buttonStyle = '<style>.butt{font-size:18px;background-color:#49baf2;border:2px solid;border-radius:20px;color:#fff;padding:5px 15px;margin-top:16px;margin-right:16px}.butt:focus{border-color:#c4c4c4}</style>';
const buttons = '<button type="button" class="butt" id="btn1">' + Button1 + '</button><button type="button" class="butt" id="btn2">' + Button2 + '</button>';
WF.showAlertDialog(body + '<br><div>' + buttonStyle + buttons + '</div>', title);
$("#btn1").focus();
$("#btn1").click(function () {
WF.hideDialog();
setTimeout(function () {
sortAndMove(children)
}, 50);
});
$("#btn2").click(function () {
WF.hideDialog();
setTimeout(function () {
sortAndMove(children, true)
}, 50);
});
}
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