diff --git a/static/app.js b/static/app.js index 2bb5785..b067abe 100644 --- a/static/app.js +++ b/static/app.js @@ -102,6 +102,8 @@ window.addEventListener('beforeunload', () => { } }); +// 🌟 特務的記憶體:記錄「上一次輪詢時,處於鎖定狀態的路徑」 +let activeLockState = new Set(); let lockPollingTimer = null; function startLockStatusPolling() { @@ -114,24 +116,46 @@ function startLockStatusPolling() { try { const result = await apiGetLockStatus(connInfo.host); if (result.status === 'success') { - const activeLocks = result.data; - - document.querySelectorAll('.edit-btn').forEach(btn => { - const path = btn.getAttribute('data-path'); - if (activeLocks[path] && activeLocks[path].user_id !== SESSION_USER_ID) { - btn.style.pointerEvents = 'none'; - btn.style.opacity = '0.2'; - btn.title = `🔒 此區塊正由 [${activeLocks[path].username}] 編輯中`; - btn.innerText = "🔒"; - } else { - if (btn.innerText === "🔒") { - btn.style.pointerEvents = 'auto'; - btn.style.opacity = '0.3'; - btn.title = "鎖定並編輯此項目"; - btn.innerText = "✏️"; - } + const currentLocks = result.data; + const newLockState = new Set(); + + // 🎯 任務 1:處理「新增鎖定」或「持續鎖定」的節點 + for (const [path, lockInfo] of Object.entries(currentLocks)) { + if (lockInfo.user_id !== SESSION_USER_ID) { + newLockState.add(path); + const safePath = path.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); + const targetBtns = document.querySelectorAll(`.edit-btn[data-path="${safePath}"]`); + + targetBtns.forEach(btn => { + if (btn.innerText !== "🔒") { + btn.style.pointerEvents = 'none'; + btn.style.opacity = '0.2'; + btn.title = `🔒 此區塊正由 [${lockInfo.username}] 編輯中`; + btn.innerText = "🔒"; + } + }); + } + } + + // 🔓 任務 2:處理「解除鎖定」的節點 + activeLockState.forEach(oldPath => { + if (!newLockState.has(oldPath)) { + const safePath = oldPath.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); + const targetBtns = document.querySelectorAll(`.edit-btn[data-path="${safePath}"]`); + + targetBtns.forEach(btn => { + if (btn.innerText === "🔒") { + btn.style.pointerEvents = 'auto'; + btn.style.opacity = '0.3'; + btn.title = "鎖定並編輯此項目"; + btn.innerText = "✏️"; + } + }); } }); + + // 💾 任務 3:更新特務的記憶 + activeLockState = newLockState; } } catch (error) { console.warn("獲取鎖定狀態失敗:", error);