diff --git a/README.md b/README.md index 0e2bd32..ac89acd 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/addCheckboxes.js b/addCheckboxes.js index b44459e..bb02a3b 100644 --- a/addCheckboxes.js +++ b/addCheckboxes.js @@ -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(); } // 执行主函数 diff --git a/globals.js b/globals.js index e8ad938..8b1740f 100644 --- a/globals.js +++ b/globals.js @@ -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 {