Allow bulk selection by holding shift

This commit is contained in:
Gabriel Veit
2024-05-11 21:37:30 +01:00
parent 0267d05e83
commit 3cb7d4a0fe
3 changed files with 44 additions and 6 deletions

View File

@@ -28,3 +28,4 @@ English | [中文版本](./README-CN.md)
- Select the conversations you wish to delete.
- Click the "Bulk delete" button, and the selected conversations will be deleted.
- If needed, you can click the "Remove checkboxes" button to hide the checkboxes.
- It's possible to select all checkboxes between your last selection and the one being selected by holding shift.

View File

@@ -17,13 +17,50 @@ function toggleCheckbox(event) {
const checkbox = parentElement ? parentElement.querySelector(`.${CHECKBOX_CLASS}`) : null;
if (checkbox) {
checkbox.checked = !checkbox.checked;
checkPreviousCheckboxes(checkbox);
}
event.stopPropagation();
}
// 阻止事件冒泡
function preventEventPropagation(event) {
function handleCheckboxClick(event) {
// 阻止事件冒泡
event.stopPropagation();
checkPreviousCheckboxes(event.target);
}
function checkPreviousCheckboxes(clickedCheckbox) {
if (shiftPressed && clickedCheckbox.checked) {
const allCheckboxes = Array.from(document.querySelectorAll(`.${CHECKBOX_CLASS}`));
const previousCheckboxes = allCheckboxes.slice(0, allCheckboxes.indexOf(clickedCheckbox));
let index = previousCheckboxes.length - 1;
while (index >= 0 && !previousCheckboxes[index].checked) {
index--;
}
// A negative index means no previous checkbox is checked
if (index >= 0) {
previousCheckboxes.slice(index).forEach((checkbox) => {
checkbox.checked = true;
});
}
}
}
function addShiftKeyEventListeners() {
console.log('Adding Shift key event listeners...');
document.addEventListener('keydown', (event) => {
if (event.key === 'Shift') {
shiftPressed = true;
}
});
document.addEventListener('keyup', (event) => {
if (event.key === 'Shift') {
shiftPressed = false;
}
});
}
// 添加复选框到每个对话
@@ -46,7 +83,7 @@ function addCheckboxes() {
checkbox.className = CHECKBOX_CLASS;
checkbox.dataset.index = index;
checkbox.checked = isChecked;
checkbox.addEventListener('click', preventEventPropagation);
checkbox.addEventListener('click', handleCheckboxClick);
conversation.insertAdjacentElement('afterbegin', checkbox);
// 为对话标题添加点击事件
@@ -77,6 +114,8 @@ function addCheckboxes() {
}
}
});
addShiftKeyEventListeners();
}
// 执行主函数

View File

@@ -3,8 +3,6 @@ if (typeof window.globalsLoaded === 'undefined') {
window.globalsLoaded = true;
let lastChecked = null;
let Selectors = {
// Plus 用户的选择器
conversationsCheckbox: '.conversation-checkbox:checked',
@@ -17,7 +15,7 @@ if (typeof window.globalsLoaded === 'undefined') {
let CHECKBOX_CLASS = 'conversation-checkbox';
window.lastChecked = lastChecked;
window.shiftPressed = false;
window.Selectors = Selectors;
window.CHECKBOX_CLASS = CHECKBOX_CLASS;
} else {