(sourceFolder, destination)
| 2186 | Used by modal / inline moves so tree + selection stay in sync. |
| 2187 | ----------------------*/ |
| 2188 | export async function syncTreeAfterFolderMove(sourceFolder, destination) { |
| 2189 | if (!sourceFolder || !destination) return; |
| 2190 | |
| 2191 | const base = sourceFolder.split('/').pop(); |
| 2192 | const newPath = (destination === 'root' ? '' : destination + '/') + base; |
| 2193 | |
| 2194 | // carry color (best-effort) |
| 2195 | await carryFolderColor(sourceFolder, newPath); |
| 2196 | |
| 2197 | // migrate expansion state + keep parents open |
| 2198 | migrateExpansionStateOnMove(sourceFolder, newPath, [destination, getParentFolder(destination)]); |
| 2199 | |
| 2200 | const srcParent = getParentFolder(sourceFolder); |
| 2201 | const dstParent = destination; |
| 2202 | |
| 2203 | // clear caches so icons/chevrons/counts are recomputed |
| 2204 | invalidateFolderCaches(srcParent); |
| 2205 | invalidateFolderCaches(dstParent); |
| 2206 | clearPeekCache([srcParent, dstParent, sourceFolder, newPath]); |
| 2207 | |
| 2208 | // re-render src + dest ULs incrementally |
| 2209 | const srcUl = getULForFolder(srcParent); |
| 2210 | const dstUl = getULForFolder(dstParent); |
| 2211 | |
| 2212 | if (srcUl) { |
| 2213 | srcUl._renderedOnce = false; |
| 2214 | srcUl.innerHTML = ''; |
| 2215 | await ensureChildrenLoaded(srcParent, srcUl); |
| 2216 | } |
| 2217 | if (dstUl) { |
| 2218 | dstUl._renderedOnce = false; |
| 2219 | dstUl.innerHTML = ''; |
| 2220 | await ensureChildrenLoaded(dstParent, dstUl); |
| 2221 | } |
| 2222 | |
| 2223 | // dest definitely has a child now → chevron on |
| 2224 | updateToggleForOption(dstParent, true); |
| 2225 | ensureFolderIcon(dstParent); |
| 2226 | |
| 2227 | // source may have lost its last child → recompute |
| 2228 | const _srcUlLive = getULForFolder(srcParent); |
| 2229 | updateToggleForOption( |
| 2230 | srcParent, |
| 2231 | !!(_srcUlLive && _srcUlLive.querySelector(':scope > li.folder-item')) |
| 2232 | ); |
| 2233 | |
| 2234 | // restore any open branches we had saved |
| 2235 | await expandAndLoadSavedState(); |
| 2236 | |
| 2237 | // update currentFolder + sticky lastOpened |
| 2238 | if (window.currentFolder === sourceFolder) { |
| 2239 | window.currentFolder = newPath; |
| 2240 | } else if (window.currentFolder && window.currentFolder.startsWith(sourceFolder + '/')) { |
| 2241 | const suffix = window.currentFolder.slice(sourceFolder.length); // includes leading '/' |
| 2242 | window.currentFolder = newPath + suffix; |
| 2243 | } |
| 2244 | setLastOpenedFolder(window.currentFolder || newPath); |
| 2245 |
no test coverage detected