Allow bulk selection by holding shift
This commit is contained in:
@@ -28,3 +28,4 @@ English | [中文版本](./README-CN.md)
|
|||||||
- Select the conversations you wish to delete.
|
- Select the conversations you wish to delete.
|
||||||
- Click the "Bulk delete" button, and the selected conversations will be deleted.
|
- 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.
|
- 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.
|
||||||
|
|||||||
@@ -17,13 +17,50 @@ function toggleCheckbox(event) {
|
|||||||
const checkbox = parentElement ? parentElement.querySelector(`.${CHECKBOX_CLASS}`) : null;
|
const checkbox = parentElement ? parentElement.querySelector(`.${CHECKBOX_CLASS}`) : null;
|
||||||
if (checkbox) {
|
if (checkbox) {
|
||||||
checkbox.checked = !checkbox.checked;
|
checkbox.checked = !checkbox.checked;
|
||||||
|
checkPreviousCheckboxes(checkbox);
|
||||||
}
|
}
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleCheckboxClick(event) {
|
||||||
// 阻止事件冒泡
|
// 阻止事件冒泡
|
||||||
function preventEventPropagation(event) {
|
|
||||||
event.stopPropagation();
|
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.className = CHECKBOX_CLASS;
|
||||||
checkbox.dataset.index = index;
|
checkbox.dataset.index = index;
|
||||||
checkbox.checked = isChecked;
|
checkbox.checked = isChecked;
|
||||||
checkbox.addEventListener('click', preventEventPropagation);
|
checkbox.addEventListener('click', handleCheckboxClick);
|
||||||
conversation.insertAdjacentElement('afterbegin', checkbox);
|
conversation.insertAdjacentElement('afterbegin', checkbox);
|
||||||
|
|
||||||
// 为对话标题添加点击事件
|
// 为对话标题添加点击事件
|
||||||
@@ -77,6 +114,8 @@ function addCheckboxes() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
addShiftKeyEventListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行主函数
|
// 执行主函数
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ if (typeof window.globalsLoaded === 'undefined') {
|
|||||||
|
|
||||||
window.globalsLoaded = true;
|
window.globalsLoaded = true;
|
||||||
|
|
||||||
let lastChecked = null;
|
|
||||||
|
|
||||||
let Selectors = {
|
let Selectors = {
|
||||||
// Plus 用户的选择器
|
// Plus 用户的选择器
|
||||||
conversationsCheckbox: '.conversation-checkbox:checked',
|
conversationsCheckbox: '.conversation-checkbox:checked',
|
||||||
@@ -17,7 +15,7 @@ if (typeof window.globalsLoaded === 'undefined') {
|
|||||||
|
|
||||||
let CHECKBOX_CLASS = 'conversation-checkbox';
|
let CHECKBOX_CLASS = 'conversation-checkbox';
|
||||||
|
|
||||||
window.lastChecked = lastChecked;
|
window.shiftPressed = false;
|
||||||
window.Selectors = Selectors;
|
window.Selectors = Selectors;
|
||||||
window.CHECKBOX_CLASS = CHECKBOX_CLASS;
|
window.CHECKBOX_CLASS = CHECKBOX_CLASS;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user