feat:解决添加上下文搜索选择文件不匹配的问题

This commit is contained in:
Roe-xin
2026-03-02 15:43:33 +08:00
parent f700473967
commit 9ed0afee6b
2 changed files with 60 additions and 30 deletions

View File

@ -271,6 +271,7 @@ export function getContextButtonStyles(): string {
width: 14px;
height: 14px;
flex-shrink: 0;
pointer-events: none;
}
.context-menu-list-item label {
@ -335,6 +336,7 @@ export function getContextButtonScript(): string {
return `
// 上下文菜单状态
let currentListData = [];
let filteredListData = [];
let currentListType = '';
let selectedItems = new Set();
@ -392,6 +394,15 @@ export function getContextButtonScript(): string {
selectedItems.clear();
currentListData = [];
filteredListData = [];
clearContextSearchInput();
}
function clearContextSearchInput() {
const searchInput = document.getElementById('contextMenuSearch');
if (searchInput) {
searchInput.value = '';
}
}
// 切换到列表视图
@ -406,10 +417,12 @@ export function getContextButtonScript(): string {
titleEl.textContent = title;
currentListType = type;
currentListData = data;
currentListData = data || [];
filteredListData = currentListData;
selectedItems.clear();
renderList(data);
clearContextSearchInput();
renderList(filteredListData);
updateSelectedCount();
}
}
@ -419,32 +432,36 @@ export function getContextButtonScript(): string {
const body = document.getElementById('contextMenuListBody');
if (!body) return;
body.innerHTML = data.map((item, index) => \`
<div class="context-menu-list-item" onclick="toggleItemSelection(\${index})">
<input type="checkbox" id="item-\${index}" />
<label for="item-\${index}">\${item.relativePath}</label>
filteredListData = data || [];
body.innerHTML = filteredListData.map((item, index) => \`
<div class="context-menu-list-item \${selectedItems.has(item.path) ? 'selected' : ''}" onclick="toggleItemSelection(\${index})">
<input type="checkbox" id="item-\${index}" \${selectedItems.has(item.path) ? 'checked' : ''} />
<label>\${item.relativePath || item.path}</label>
</div>
\`).join('');
}
// 切换项选择
function toggleItemSelection(index) {
const selectedItem = filteredListData[index];
if (!selectedItem) return;
const selectedPath = selectedItem.path;
const checkbox = document.getElementById('item-' + index);
const item = document.querySelectorAll('.context-menu-list-item')[index];
if (checkbox && item) {
checkbox.checked = !checkbox.checked;
if (checkbox.checked) {
selectedItems.add(index);
item.classList.add('selected');
} else {
selectedItems.delete(index);
item.classList.remove('selected');
}
updateSelectedCount();
if (selectedItems.has(selectedPath)) {
selectedItems.delete(selectedPath);
if (checkbox) checkbox.checked = false;
if (item) item.classList.remove('selected');
} else {
selectedItems.add(selectedPath);
if (checkbox) checkbox.checked = true;
if (item) item.classList.add('selected');
}
updateSelectedCount();
}
// 更新选中数量
@ -457,15 +474,25 @@ export function getContextButtonScript(): string {
// 确认选择
function confirmSelection() {
const selected = Array.from(selectedItems).map(index => currentListData[index]);
try {
const selected = currentListData.filter(item => selectedItems.has(item.path));
if (selected.length > 0) {
selected.forEach(item => {
addContextItem(currentListType, item.path);
});
if (selected.length > 0) {
selected.forEach(item => {
addContextItem(currentListType, item.path, item.relativePath || item.path);
});
}
} finally {
const menu = document.getElementById('contextMenu');
const button = document.querySelector('.add-context-button');
if (menu) {
menu.classList.remove('show');
}
if (button) {
button.classList.remove('active');
}
backToMainMenu();
}
toggleContextMenu();
}
// 添加图片
@ -484,9 +511,9 @@ export function getContextButtonScript(): string {
const searchInput = document.getElementById('contextMenuSearch');
if (searchInput) {
searchInput.addEventListener('input', function(e) {
const keyword = e.target.value.toLowerCase();
const keyword = (e.target.value || '').toLowerCase().trim();
const filtered = currentListData.filter(item =>
item.relativePath.toLowerCase().includes(keyword)
(item.relativePath || item.path || '').toLowerCase().includes(keyword)
);
renderList(filtered);
});

View File

@ -137,9 +137,12 @@ export function getContextDisplayScript(): string {
}
// 添加上下文项
function addContextItem(type, path) {
function addContextItem(type, path, displayPath) {
const exists = contextItems.some(item => item.type === type && item.path === path);
if (exists) return;
const id = Date.now() + Math.random();
contextItems.push({ id, type, path });
contextItems.push({ id, type, path, displayPath: displayPath || '' });
renderContextItems();
}
@ -174,7 +177,7 @@ export function getContextDisplayScript(): string {
return \`
<div class="context-item" title="\${item.path}">
\${icon}
<span class="context-item-name">\${getFileName(item.path)}</span>
<span class="context-item-name">\${item.displayPath || getFileName(item.path)}</span>
<span class="context-item-remove" onclick="removeContextItem(\${item.id})">
\${getRemoveIcon()}
</span>