1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2024-11-23 17:42:46 +01:00
TweetDuck/resources/Content/tweetdeck/restore_cleared_column.js

47 lines
1.2 KiB
JavaScript

import { $ } from "../api/jquery.js";
import { replaceFunction } from "../api/patch.js";
import { TD } from "../api/td.js";
import { checkPropertyExists } from "../api/utils.js";
/**
* Allows restoring cleared columns by holding Shift.
*/
export default function() {
let holdingShift = false;
const updateShiftState = (pressed) => {
if (holdingShift !== pressed) {
holdingShift = pressed;
$("button[data-action='clear']").children("span").text(holdingShift ? "Restore" : "Clear");
}
};
document.addEventListener("keydown", function(e) {
if (e.shiftKey && (document.activeElement === null || !("value" in document.activeElement))) {
updateShiftState(true);
}
});
document.addEventListener("keyup", function(e) {
if (!e.shiftKey) {
updateShiftState(false);
}
});
if (checkPropertyExists(TD, "vo", "Column", "prototype")) {
replaceFunction(TD.vo.Column.prototype, "clear", function(func, args) {
window.setTimeout(function() {
document.activeElement.blur(); // unfocuses the Clear button, otherwise it steals keyboard input
}, 0);
if (holdingShift) {
this.model.setClearedTimestamp(0);
this.reloadTweets();
}
else {
func.apply(this, args);
}
});
}
};