diff options
| author | historia <[not public]> | 2026-07-09 17:12:03 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-07-09 17:12:03 -0400 |
| commit | b888414c63c0e5fae0d33b3c5faea8a604d195fa (patch) | |
| tree | 1c5a54101c774d6126c3d08fa5b6e9b44d0c3c04 /internal/admin | |
| parent | b8c90886ef1f3afb8d908aac89028bd177836cae (diff) | |
| download | thehouseoficarus-b888414c63c0e5fae0d33b3c5faea8a604d195fa.tar.gz | |
feat(admin): multi-select and delete rooms
Diffstat (limited to 'internal/admin')
| -rw-r--r-- | internal/admin/api_rooms_bulk_delete.go | 132 | ||||
| -rw-r--r-- | internal/admin/server.go | 1 | ||||
| -rw-r--r-- | internal/admin/static/map.js | 87 |
3 files changed, 209 insertions, 11 deletions
diff --git a/internal/admin/api_rooms_bulk_delete.go b/internal/admin/api_rooms_bulk_delete.go new file mode 100644 index 0000000..6aa0181 --- /dev/null +++ b/internal/admin/api_rooms_bulk_delete.go @@ -0,0 +1,132 @@ +package admin + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + + "thehouseoficarus/internal/world" +) + +type bulkDeleteRequest struct { + IDs []int `json:"ids"` +} + +func (s *AdminServer) handleBulkDelete(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + + body, readErr := io.ReadAll(r.Body) + r.Body.Close() + if readErr != nil { + writeJSON(w, map[string]any{"error": fmt.Sprintf("read body: %v", readErr)}) + return + } + + var req bulkDeleteRequest + if err := json.Unmarshal(body, &req); err != nil { + writeJSON(w, map[string]any{"error": fmt.Sprintf("invalid json: %v", err)}) + return + } + + if len(req.IDs) == 0 { + writeJSON(w, map[string]any{"error": "no room ids"}) + return + } + + type roomSnapshot struct { + id int + path string + oldContent []byte + } + + snapshots := make([]roomSnapshot, 0, len(req.IDs)) + for _, id := range req.IDs { + path, ok := s.world.GetRoomPath(id) + if !ok { + writeJSON(w, map[string]any{"error": fmt.Sprintf("room %d not found", id)}) + return + } + oldContent, snapErr := snapshotFile(path) + if snapErr != nil { + writeJSON(w, map[string]any{"error": fmt.Sprintf("snapshot room %d: %v", id, snapErr)}) + return + } + snapshots = append(snapshots, roomSnapshot{id: id, path: path, oldContent: oldContent}) + } + + deletedSet := make(map[int]bool, len(req.IDs)) + for _, snap := range snapshots { + s.detachRoomFromCourse(snap.id) + if err := os.Remove(snap.path); err != nil { + writeJSON(w, map[string]any{"error": fmt.Sprintf("delete room %d: %v", snap.id, err)}) + return + } + deletedSet[snap.id] = true + } + + s.world.RebuildRoomIndex(s.dataDir) + for _, snap := range snapshots { + s.world.ClearRoomState(snap.id) + } + + allIDs, _ := listRoomIDs(s.dataDir) + var extraFiles []ExtraFile + var exitCleaned int + for _, otherID := range allIDs { + if deletedSet[otherID] { + continue + } + otherRoom, loadErr := s.world.LoadRoom(otherID) + if loadErr != nil { + continue + } + changed := false + for _, dir := range world.ExitOrder { + exit, exists := otherRoom.Exits[dir] + if !exists || !deletedSet[exit.Room] { + continue + } + delete(otherRoom.Exits, dir) + changed = true + } + if changed { + otherPath, pathOk := s.world.GetRoomPath(otherID) + if pathOk { + oldExtra, _ := snapshotFile(otherPath) + newContent, writeErr := writeYAMLFile(otherPath, otherRoom) + if writeErr != nil { + continue + } + extraFiles = append(extraFiles, ExtraFile{ + FilePath: otherPath, + OldContent: oldExtra, + NewContent: newContent, + }) + exitCleaned++ + } + } + } + + for i := 1; i < len(snapshots); i++ { + extraFiles = append(extraFiles, ExtraFile{ + FilePath: snapshots[i].path, + OldContent: snapshots[i].oldContent, + IsDelete: true, + }) + } + + s.undoStack.Push(ChangeDesc{ + Description: fmt.Sprintf("delete %d rooms (cleaned %d exits)", len(req.IDs), exitCleaned), + FilePath: snapshots[0].path, + OldContent: snapshots[0].oldContent, + IsDelete: true, + ExtraFiles: extraFiles, + }) + + writeJSON(w, map[string]any{"ok": true, "count": len(req.IDs)}) +} diff --git a/internal/admin/server.go b/internal/admin/server.go index c33bd21..67de0ab 100644 --- a/internal/admin/server.go +++ b/internal/admin/server.go @@ -126,6 +126,7 @@ func NewServer(cfg *config.Config, useTLS bool, accountStore *player.AccountStor apiMux.HandleFunc("/api/rooms/move", s.handleRoomMove) apiMux.HandleFunc("/api/rooms/rename", s.handleRoomRename) apiMux.HandleFunc("/api/rooms/bulk-edit", s.handleBulkEdit) + apiMux.HandleFunc("/api/rooms/bulk-delete", s.handleBulkDelete) apiMux.HandleFunc("/api/rooms", s.handleRooms) apiMux.HandleFunc("/api/rooms/new-empty", s.handleCreateEmptyRoom) apiMux.HandleFunc("/api/rooms/", s.handleRoomByID) diff --git a/internal/admin/static/map.js b/internal/admin/static/map.js index 1f67275..e053dda 100644 --- a/internal/admin/static/map.js +++ b/internal/admin/static/map.js @@ -42,7 +42,7 @@ function onMapMouseMove(e) { if (!dragging) return; var moved = Math.abs(e.clientX - startX) >= 4 || Math.abs(e.clientY - startY) >= 4; - if (delFromId && moved && !delDrag) { delDrag = true; } + if (delFromId && moved && !delDrag && multiSelectedIds.size === 0) { delDrag = true; } if (delDrag && moved) { delOneWay = e.ctrlKey; updateDelDragTarget(e); @@ -81,7 +81,11 @@ function onMapMouseUp(e) { var moved = Math.abs(e.clientX - startX) >= 4 || Math.abs(e.clientY - startY) >= 4; if (delFromId && !delDrag && !moved) { - showRoomContextMenu(startX, startY, delFromId); + if (multiSelectedIds.size > 0) { + showBulkDeleteContextMenu(startX, startY, delFromId); + } else { + showRoomContextMenu(startX, startY, delFromId); + } clearDelHighlight(); dragging = false; dragRoom = false; delFromId = null; delTargetId = null; delDrag = false; if (document.activeElement && document.activeElement.blur) document.activeElement.blur(); @@ -368,7 +372,7 @@ function renderMap(data) { var onUdBtn = e.target.classList.contains('ud-btn'); var onNavBtn = e.target.classList.contains('nav-btn'); - if (!e.shiftKey && multiSelectedIds.size > 0) { + if (e.button === 0 && !e.shiftKey && multiSelectedIds.size > 0) { clearMultiSelect(); } @@ -2263,6 +2267,58 @@ function showRoomContextMenu(x, y, id) { clampMenuToViewport(menu); } +function showBulkDeleteContextMenu(x, y, clickedId) { + var overlay = document.createElement('div'); + overlay.className = 'cm-overlay'; + var closeAll = function() { + if (overlay._subHideTimer) { clearTimeout(overlay._subHideTimer); overlay._subHideTimer = null; } + overlay.remove(); menu.remove(); + }; + overlay.onclick = closeAll; + overlay.addEventListener('contextmenu', function(e) { + e.preventDefault(); + e.stopPropagation(); + closeAll(); + }); + + var menu = document.createElement('div'); + menu.className = 'cm-menu'; + menu.style.left = x + 'px'; + menu.style.top = y + 'px'; + menu.oncontextmenu = function(e) { e.preventDefault(); }; + + var n = multiSelectedIds.size; + var delBtn = document.createElement('button'); + delBtn.textContent = 'Delete ' + n + ' room' + (n !== 1 ? 's' : ''); + delBtn.className = 'danger'; + delBtn.onclick = function() { + closeAll(); + deleteMultiRooms(); + }; + menu.appendChild(delBtn); + + document.body.appendChild(overlay); + document.body.appendChild(menu); + + clampMenuToViewport(menu); +} + +function deleteMultiRooms() { + var n = multiSelectedIds.size; + var cb = document.getElementById('confirmDelete'); + if (!cb || cb.checked) { + if (!confirm('Delete ' + n + ' room' + (n !== 1 ? 's' : '') + '?')) return; + } + var ids = Array.from(multiSelectedIds); + API.post('/api/rooms/bulk-delete', {ids: ids}).then(function(res) { + var msg = 'Deleted ' + n + ' room' + (n !== 1 ? 's' : ''); + notify(msg, 'success'); + updateUndoBar(); + clearMultiSelect(); + loadMap(); + }).catch(function(e) { notify('Delete failed: ' + extractError(e), 'error'); }); +} + // buildSubmenuItem constructs a parent menu row ("Insert Room"/"Remove Room") // that carries a nested submenu of the room's exits. The submenu is // revealed by both hover (Windows-style: mouseenter with a small dismiss delay @@ -2572,6 +2628,19 @@ function applyLinkHighlight(fromId, toId) { g.appendChild(line); } +function restoreRoomStroke(el, id) { + if (multiSelectedIds.has(id)) { + el.setAttribute('stroke', '#5cf'); + el.setAttribute('stroke-width', '3.5'); + } else if (selectedRoom === id) { + el.setAttribute('stroke', '#fff'); + el.setAttribute('stroke-width', '3'); + } else { + el.setAttribute('stroke', el.getAttribute('fill') || '#3a3a6a'); + el.setAttribute('stroke-width', '1.5'); + } +} + function clearLinkHighlight() { var line = document.getElementById('__hl_line'); if (line) line.remove(); @@ -2579,15 +2648,13 @@ function clearLinkHighlight() { if (linkFromId) { var el = document.querySelector('.rm[data-id="'+linkFromId+'"]'); if (el) { - el.setAttribute('stroke', selectedRoom === linkFromId ? '#fff' : (el.getAttribute('fill') || '#3a3a6a')); - el.setAttribute('stroke-width', selectedRoom === linkFromId ? '3' : '1.5'); + restoreRoomStroke(el, linkFromId); } } if (linkTargetId) { var el = document.querySelector('.rm[data-id="'+linkTargetId+'"]'); if (el) { - el.setAttribute('stroke', selectedRoom === linkTargetId ? '#fff' : (el.getAttribute('fill') || '#3a3a6a')); - el.setAttribute('stroke-width', selectedRoom === linkTargetId ? '3' : '1.5'); + restoreRoomStroke(el, linkTargetId); } } } @@ -2666,15 +2733,13 @@ function clearDelHighlight() { if (delFromId) { var el = document.querySelector('.rm[data-id="'+delFromId+'"]'); if (el) { - el.setAttribute('stroke', selectedRoom === delFromId ? '#fff' : (el.getAttribute('fill') || '#3a3a6a')); - el.setAttribute('stroke-width', selectedRoom === delFromId ? '3' : '1.5'); + restoreRoomStroke(el, delFromId); } } if (delTargetId) { var el = document.querySelector('.rm[data-id="'+delTargetId+'"]'); if (el) { - el.setAttribute('stroke', selectedRoom === delTargetId ? '#fff' : (el.getAttribute('fill') || '#3a3a6a')); - el.setAttribute('stroke-width', selectedRoom === delTargetId ? '3' : '1.5'); + restoreRoomStroke(el, delTargetId); } } } |
