From 20c982755afdaa781aaa058300cacb0a0b459dc3 Mon Sep 17 00:00:00 2001 From: Lucio Chen Date: Wed, 10 Jun 2026 22:47:08 -0700 Subject: [PATCH] Collapsible locations tree, sorting mode, and drag selection fix The Locations page now shows only top-level locations by default; rows with children get a caret that reveals sub-locations inline, indented under their parent in the same table style. Rearranging is gated behind a new "Sorting mode" toggle next to "Show removed locations": when enabled, rows can be dragged onto another location (or a drop strip to move to top level). The same toggle appears on container array pages and gates drawer drag-to-swap. The state persists across reloads so it survives post-move refreshes. Dragging no longer selects text: drawer trays and draggable cards get user-select: none, the drawer drag suppresses selection globally for its duration, and mousedown on a tray prevents native selection. Co-Authored-By: Claude Fable 5 --- .claude/launch.json | 4 +- .gitignore | 1 + internal/i18n/locales/en.json | 5 +- internal/i18n/locales/zh.json | 5 +- internal/server/handlers.go | 50 +++++++++ internal/server/static/css/styles.css | 103 +++++++++++++++++- internal/server/static/js/main.js | 47 +++++++- .../server/templates/location_folder.html | 8 +- internal/server/templates/locations.html | 69 ++++++++++-- 9 files changed, 273 insertions(+), 19 deletions(-) diff --git a/.claude/launch.json b/.claude/launch.json index d09fc8c..b84deae 100644 --- a/.claude/launch.json +++ b/.claude/launch.json @@ -4,8 +4,8 @@ { "name": "solis", "runtimeExecutable": "go", - "runtimeArgs": ["run", "."], - "port": 8889 + "runtimeArgs": ["run", ".", "-c", "config-preview.toml"], + "port": 8899 } ] } diff --git a/.gitignore b/.gitignore index abf8360..0368b1b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ Thumbs.db # Config files (if you want to ignore actual config but keep example) config.toml +config-preview.toml diff --git a/internal/i18n/locales/en.json b/internal/i18n/locales/en.json index 0cdfacb..128a022 100644 --- a/internal/i18n/locales/en.json +++ b/internal/i18n/locales/en.json @@ -169,9 +169,12 @@ "locations.grid_cols": "Columns", "locations.grid_dims": "rows × columns (max 20×20)", "locations.container_array_hint": "Turns this location into a drawer cabinet. Open it from the folder view to add, color, and rearrange drawers.", + "locations.sorting_mode": "Sorting mode", + "locations.move_to_root": "Drop a location here to move it to the top level", + "locations.toggle_children": "Show or hide sub-locations", "drawer.section_title": "Drawers", - "drawer.hint": "Hover or tap a drawer to peek inside. Drag (or touch and hold) a drawer to swap it with another slot. Click an empty slot to add a drawer.", + "drawer.hint": "Hover or tap a drawer to peek inside. Click an empty slot to add a drawer. Enable sorting mode to drag (or touch and hold) drawers and swap them around.", "drawer.items": "item(s)", "drawer.empty_contents": "This drawer is empty.", "drawer.open": "Open", diff --git a/internal/i18n/locales/zh.json b/internal/i18n/locales/zh.json index 4bba572..77229a7 100644 --- a/internal/i18n/locales/zh.json +++ b/internal/i18n/locales/zh.json @@ -169,9 +169,12 @@ "locations.grid_cols": "列数", "locations.grid_dims": "行 × 列(最大 20×20)", "locations.container_array_hint": "将此位置变为抽屉柜。在文件夹视图中打开它即可添加、着色和重新排列抽屉。", + "locations.sorting_mode": "排序模式", + "locations.move_to_root": "将位置拖放到此处可移至顶层", + "locations.toggle_children": "显示或隐藏子位置", "drawer.section_title": "抽屉", - "drawer.hint": "悬停或轻触抽屉可预览内容。拖动(或长按)抽屉可与其他格子交换位置。点击空格子可添加抽屉。", + "drawer.hint": "悬停或轻触抽屉可预览内容。点击空格子可添加抽屉。启用排序模式后,可拖动(或长按)抽屉交换位置。", "drawer.items": "件物品", "drawer.empty_contents": "此抽屉是空的。", "drawer.open": "打开", diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 1f186d1..d72f894 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -56,6 +56,7 @@ type PageData struct { Breadcrumbs []db.Location ChildLocations []db.Location DrawerGrid [][]DrawerCell + LocationTree []LocationTreeRow } // DrawerCell is one slot of a container array grid; Drawer is nil for empty slots. @@ -65,6 +66,54 @@ type DrawerCell struct { Drawer *db.Drawer } +// LocationTreeRow is a location flattened from the hierarchy in pre-order, +// carrying its nesting depth for the collapsible Locations table. +type LocationTreeRow struct { + db.Location + Depth int + Indent int // indentation in px for the name cell + HasChildren bool +} + +// buildLocationTree flattens locations into pre-order rows. Locations whose +// parent is absent from the list (e.g. hidden removed parents) surface at the +// top level so they stay reachable. +func buildLocationTree(locations []db.Location) []LocationTreeRow { + inList := make(map[int64]bool, len(locations)) + for _, l := range locations { + inList[l.ID] = true + } + + childrenOf := map[int64][]db.Location{} + var roots []db.Location + for _, l := range locations { + if l.ParentID.Valid && l.ParentID.Int64 != l.ID && inList[l.ParentID.Int64] { + childrenOf[l.ParentID.Int64] = append(childrenOf[l.ParentID.Int64], l) + } else { + roots = append(roots, l) + } + } + + rows := make([]LocationTreeRow, 0, len(locations)) + visited := map[int64]bool{} + var walk func(l db.Location, depth int) + walk = func(l db.Location, depth int) { + if visited[l.ID] { + return + } + visited[l.ID] = true + kids := childrenOf[l.ID] + rows = append(rows, LocationTreeRow{Location: l, Depth: depth, Indent: depth * 22, HasChildren: len(kids) > 0}) + for _, k := range kids { + walk(k, depth+1) + } + } + for _, r := range roots { + walk(r, 0) + } + return rows +} + // ========================================== // 1. PUBLIC SHORT URL REDIRECT HANDLER @@ -742,6 +791,7 @@ func (s *Server) handleAdminLocations(w http.ResponseWriter, r *http.Request) { CSRFToken: csrf, ActiveNav: "locations", Locations: locations, + LocationTree: buildLocationTree(locations), EditingLocation: editLoc, ShowRemoved: showRemoved, Toast: r.URL.Query().Get("toast"), diff --git a/internal/server/static/css/styles.css b/internal/server/static/css/styles.css index 4e66d69..a693baf 100644 --- a/internal/server/static/css/styles.css +++ b/internal/server/static/css/styles.css @@ -1498,7 +1498,7 @@ tr:last-child .actions-dropdown-menu { inset: 0; transform-style: preserve-3d; transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1); - cursor: grab; + cursor: pointer; touch-action: pan-y; } @@ -1706,6 +1706,107 @@ tr:last-child .actions-dropdown-menu { border-color: #b91c1c; } +/* ========================================== + Locations tree table & sorting mode + ========================================== */ + +.loc-name-cell { + display: flex; + align-items: center; + gap: 0.35rem; +} + +.loc-caret { + display: inline-flex; + align-items: center; + justify-content: center; + width: 1.4rem; + height: 1.4rem; + border: none; + background: none; + color: var(--color-text-muted); + cursor: pointer; + border-radius: 4px; + padding: 0; + flex-shrink: 0; + transition: transform 0.18s ease, background-color 0.15s ease; +} + +.loc-caret:hover { + background: var(--color-bg-base); + color: var(--color-brand-primary); +} + +.loc-caret.expanded { + transform: rotate(90deg); +} + +.loc-caret-spacer { + display: inline-block; + width: 1.4rem; + flex-shrink: 0; +} + +.loc-grid-badge { + font-size: 0.68rem; + font-weight: 700; + color: var(--color-brand-primary); + background: var(--color-brand-light); + border: 1px solid var(--color-border-warm); + border-radius: 4px; + padding: 0.05rem 0.3rem; + margin-left: 0.35rem; + white-space: nowrap; +} + +/* Root drop strip, shown only while sorting */ +.sort-root-dropzone { + display: none; + align-items: center; + justify-content: center; + gap: 0.5rem; + margin-bottom: 1rem; + padding: 0.75rem; + border: 2px dashed var(--color-border-warm); + border-radius: 10px; + color: var(--color-brand-primary); + font-size: 0.9rem; + font-weight: 600; + background: var(--color-bg-surface); +} + +body.sorting-mode .sort-root-dropzone { + display: flex; +} + +body.sorting-mode .loc-row[draggable="true"] { + cursor: grab; +} + +body.sorting-mode .drawer-tray { + cursor: grab; +} + +body.sorting-mode .drawer-cabinet { + outline: 2px dashed var(--color-brand-accent); + outline-offset: 4px; +} + +/* Keep drags from selecting text */ +.folder-card[draggable="true"], +.item-card[draggable="true"], +.loc-row[draggable="true"], +.drawer-tray { + user-select: none; + -webkit-user-select: none; +} + +body.drag-no-select, +body.drag-no-select * { + user-select: none !important; + -webkit-user-select: none !important; +} + /* 404 Not Found page */ .notfound-container { display: flex; diff --git a/internal/server/static/js/main.js b/internal/server/static/js/main.js index 0af9d86..ad77309 100644 --- a/internal/server/static/js/main.js +++ b/internal/server/static/js/main.js @@ -21,10 +21,32 @@ document.addEventListener("DOMContentLoaded", () => { // 5. Mobile sidebar drawer interactions initMobileSidebar(); - // 6. Container array drawer grid (3D view) + // 6. Sorting mode (drag rearranging) toggle state + initSortingMode(); + + // 7. Container array drawer grid (3D view) initDrawerArray(); }); +// Sorting mode gates all drag-to-rearrange interactions; persisted so it +// survives the page reloads triggered after each successful move. +function sortingModeEnabled() { + return document.body.classList.contains("sorting-mode"); +} + +function initSortingMode() { + let enabled = false; + try { enabled = localStorage.getItem("solisSortingMode") === "1"; } catch (e) { /* private mode */ } + document.body.classList.toggle("sorting-mode", enabled); + document.querySelectorAll(".sorting-mode-toggle").forEach(cb => { cb.checked = enabled; }); +} + +function toggleSortingMode(checkbox) { + document.body.classList.toggle("sorting-mode", checkbox.checked); + document.querySelectorAll(".sorting-mode-toggle").forEach(cb => { cb.checked = checkbox.checked; }); + try { localStorage.setItem("solisSortingMode", checkbox.checked ? "1" : "0"); } catch (e) { /* private mode */ } +} + // Mobile off-canvas sidebar drawer function toggleSidebar() { const sidebar = document.getElementById("sidebar"); @@ -174,6 +196,11 @@ function initDragAndDrop() { draggables.forEach(draggable => { draggable.addEventListener("dragstart", (e) => { + // Elements marked sort-gated only drag while sorting mode is on + if (draggable.classList.contains("sort-gated") && !sortingModeEnabled()) { + e.preventDefault(); + return; + } draggedElement = draggable; draggable.classList.add("dragging"); @@ -397,6 +424,11 @@ function initDrawerArray() { cell.classList.add("drag-source"); hidePeek(); + // Suppress text selection for the duration of the drag + document.body.classList.add("drag-no-select"); + const selection = window.getSelection(); + if (selection) selection.removeAllRanges(); + const front = cell.querySelector(".drawer-front"); const rect = front.getBoundingClientRect(); const ghost = front.cloneNode(true); @@ -442,6 +474,7 @@ function initDrawerArray() { dragState.suppressClick = true; setTimeout(() => { dragState.suppressClick = false; }, 50); + document.body.classList.remove("drag-no-select"); cell.classList.remove("drag-source"); if (ghost) ghost.remove(); if (target) target.classList.remove("drop-target"); @@ -480,14 +513,17 @@ function initDrawerArray() { cells.forEach(cell => { const tray = cell.querySelector(".drawer-tray"); - // Mouse: drag starts after a small movement threshold; plain click navigates + // Mouse: in sorting mode, drag starts after a small movement threshold; + // plain click navigates either way tray.addEventListener("mousedown", (e) => { if (e.button !== 0) return; + // Avoid native text selection from a press-and-move on the drawer + e.preventDefault(); const startX = e.clientX, startY = e.clientY; let started = false; const onMove = (ev) => { - if (!started && Math.hypot(ev.clientX - startX, ev.clientY - startY) > 6) { + if (!started && sortingModeEnabled() && Math.hypot(ev.clientX - startX, ev.clientY - startY) > 6) { started = true; startDrag(cell, ev.clientX, ev.clientY); } @@ -502,13 +538,14 @@ function initDrawerArray() { document.addEventListener("mouseup", onUp); }); - // Touch: long-press (450ms) starts drag; quick tap toggles the peek + // Touch: in sorting mode, long-press (450ms) starts drag; quick tap + // toggles the peek either way tray.addEventListener("touchstart", (e) => { const touch = e.touches[0]; const startX = touch.clientX, startY = touch.clientY; let pressTimer = setTimeout(() => { pressTimer = null; - startDrag(cell, startX, startY); + if (sortingModeEnabled()) startDrag(cell, startX, startY); }, 450); const onTouchMove = (ev) => { diff --git a/internal/server/templates/location_folder.html b/internal/server/templates/location_folder.html index 9d8a1b4..05472a6 100644 --- a/internal/server/templates/location_folder.html +++ b/internal/server/templates/location_folder.html @@ -30,11 +30,17 @@
{{if .IsAuthenticated}} -
+
+ {{if .DrawerGrid}} + + {{end}}
{{end}} diff --git a/internal/server/templates/locations.html b/internal/server/templates/locations.html index 345154f..e2adf1f 100644 --- a/internal/server/templates/locations.html +++ b/internal/server/templates/locations.html @@ -8,7 +8,7 @@
-
+ {{if .EditingLocation}} {{end}} @@ -16,8 +16,19 @@ {{T "locations.show_removed"}} +
+
+ + + + {{T "locations.move_to_root"}} +
+
@@ -34,14 +45,34 @@ - {{if .Locations}} - {{range .Locations}} - + {{if .LocationTree}} + {{range .LocationTree}} + - {{.Name}} - {{if eq .LifecycleState "archived"}} - {{T "common.archived"}} - {{end}} +
+ {{if .HasChildren}} + + {{else}} + + {{end}} + {{.Name}} + {{if gt .GridRows 0}} + {{.GridRows}}×{{.GridCols}} + {{end}} + {{if eq .LifecycleState "archived"}} + {{T "common.archived"}} + {{end}} +
{{if .ParentName.Valid}} @@ -224,5 +255,27 @@ const fields = checkbox.closest(".form-group").querySelector(".grid-size-fields"); if (fields) fields.hidden = !checkbox.checked; } + + function toggleLocChildren(btn, id) { + const expanded = btn.classList.toggle("expanded"); + btn.setAttribute("aria-expanded", expanded ? "true" : "false"); + document.querySelectorAll('.loc-row[data-tree-parent="' + id + '"]').forEach(row => { + if (expanded) { + row.style.display = ""; + } else { + collapseLocRow(row); + } + }); + } + + function collapseLocRow(row) { + row.style.display = "none"; + const caret = row.querySelector(".loc-caret"); + if (caret) { + caret.classList.remove("expanded"); + caret.setAttribute("aria-expanded", "false"); + } + document.querySelectorAll('.loc-row[data-tree-parent="' + row.dataset.locationId + '"]').forEach(collapseLocRow); + } {{end}}