Create a gist now

Instantly share code, notes, and snippets.

Embed
UserScript to sort WorkFlowy nodes using Ctrl+Shft+S
// ==UserScript==
// @name Sort WorkFlowy
// @namespace https://rawbytz.wordpress.com
// @version 2.0
// @description Use Ctrl+Shift+S to sort the current zoom level children.
// @author rawbytz
// @match https://workflowy.com/*
// @grant none
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
function sortMe(maxChildren) {
function toastMsg(msg, msecs, err) {
if ($("#dropdownMessages").css("position") !== "fixed") {
$("#dropdownMessages").css("position", "fixed");
}
void 0 === msecs && (msecs = 2000);
void 0 === err && (err = !1);
var r = $("#message");
r.removeClass(),
err && r.addClass("errorMessage"),
r.find(".messageContent").html("<b>" + msg + "</b>"),
r.is(":visible") || (r.children(".close").hide(),
r.slideDown("normal", function () {
$(this).find(".close").show()
}));
setTimeout(function () {
r.children(".close").hide(),
r.find(".messageContent").html(""),
r.slideUp();
}, msecs);
}
function showAlertDialog(e, t, n, r, o) {
void 0 === t && (t = ""),
void 0 === n && (n = !0),
void 0 === r && (r = []),
void 0 === o && (o = "Close");
var i = document.getElementById("documentView");
if (i) {
var a = $('<div class="alertDialog">' + e + "</div>");
i.appendChild(a[0]);
var s = r.concat([{
text: o,
click: function () {
$(this).dialog("close")
}
}]);
a.dialog({
width: 500,
modal: n,
position: ["center", 150],
title: t,
close: function () {
$(this).dialog("destroy").remove()
},
buttons: s
})
}
}
function sortAndMove(arr, reverse) {
arr.sort(function (a, b) {
return reverse ? b.getNameInPlainText().localeCompare(a.getNameInPlainText()) : a.getNameInPlainText().localeCompare(b.getNameInPlainText());
});
undo_redo.startOperationBatch();
for (var i = 0; i < arr.length; i++) {
if (arr[i].getPriority() !== i) {
project_tree.applyLocalMoveForProjectReferences([arr[i]], parent, i);
}
}
undo_redo.finishOperationBatch();
var dir = reverse ? "Z-A." : "A-Z.";
toastMsg("Sorted " + dir, 2000);
}
//Fix for WorkFlowy's dropdown message bug.
if ($("#dropdownMessages").css("position") !== "fixed") {
$("#dropdownMessages").css("position", "fixed");
}
if (search.inSearchMode()) {
toastMsg("Sorting is disabled when search is active.", 3000, true);
return;
}
var parent = project_tree.getProjectReferenceFromDomProject(selectOnActivePage('.selected'));
if (parent.childrenAreInReadOnlyTree()) {
toastMsg("This outline is View-only.", 3000, true);
return;
}
var children = parent.getChildren();
if (children.length < 2) {
toastMsg("Nothing to sort.", 3000, true);
return;
}
if (children.length > maxChildren) {
toastMsg("Sorting more than " + maxChildren + " children upsets the WorkFlowy gods, and has been disabled.", 5000, true);
return;
}
var parentName = location_history.getCurrentLocation().getNameText();
$(".ui-dialog:visible > .ui-dialog-content").dialog("close");
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;
}
}
});
})();
@rawbytz

This comment has been minimized.

Show comment
Hide comment
@rawbytz

rawbytz Jun 7, 2018

Get bookmarklet version here.

Owner

rawbytz commented Jun 7, 2018

Get bookmarklet version here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment