Write a single html/css/js file tool that can unzip zipfiles dragged on it, store the comprised text files in a indexDB and allows searching and editing the texts as well es reordering by drag and drop
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zip File Tool</title>
<style>
/* Basic styling for the drop zone */
#dropzone {
width: 100%;
height: 100px;
border: 2px dashed #bbb;
text-align: center;
line-height: 100px;
font-size: 16px;
cursor: pointer;
}
#dropzone.dragover {
border-color: #000;
background-color: #f0f0f0;
}
/* File list styling */
#fileList {
list-style-type: none;
padding: 0;
margin: 20px 0;
}
#fileList li {
padding: 10px;
border: 1px solid #ccc;
margin: 5px 0;
cursor: move; /* Indicates draggable */
background-color: #fff;
}
#fileList li.hidden {
display: none;
}
/* Editor styling */
#editor {
margin-top: 20px;
}
#editArea {
width: 100%;
height: 200px;
resize: vertical;
}
/* Search input styling */
#searchInput {
width: 100%;
padding: 10px;
margin-top: 10px;
font-size: 16px;
}
/* General layout */
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
</style>
</head>
<body>
<div id="dropzone">Drag and drop a zip file here</div>
<input type="text" id="searchInput" placeholder="Search file names">
<ul id="fileList"></ul>
<div id="editor">
<textarea id="editArea"></textarea>
<button id="saveButton">Save</button>
</div>
<!-- Include JSZip from CDN for unzipping -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script>
// Database setup
const dbName = "ZipToolDB";
const storeName = "files";
let db;
let currentEditingId = null;
const dbRequest = indexedDB.open(dbName, 1);
dbRequest.onupgradeneeded = function(event) {
db = event.target.result;
if (!db.objectStoreNames.contains(storeName)) {
const objectStore = db.createObjectStore(storeName, { keyPath: "id", autoIncrement: true });
objectStore.createIndex("name", "name", { unique: false });
objectStore.createIndex("order", "order", { unique: false });
}
};
dbRequest.onsuccess = function(event) {
db = event.target.result;
loadFilesIntoUI(); // Load files when DB is open
};
dbRequest.onerror = function(event) {
console.error("IndexedDB error:", event.target.error);
};
// Drop zone event handlers
const dropzone = document.getElementById("dropzone");
dropzone.addEventListener("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
dropzone.classList.add("dragover");
});
dropzone.addEventListener("dragleave", function(e) {
e.preventDefault();
e.stopPropagation();
dropzone.classList.remove("dragover");
});
dropzone.addEventListener("drop", function(e) {
e.preventDefault();
e.stopPropagation();
dropzone.classList.remove("dragover");
const files = e.dataTransfer.files;
if (files.length > 0) {
const file = files[0];
if (file.name.endsWith(".zip")) {
unzipAndStore(file);
} else {
alert("Please drop a zip file.");
}
}
});
// Unzip and store files in IndexedDB
async function unzipAndStore(zipFile) {
try {
const zip = await JSZip.loadAsync(zipFile);
const fileEntries = [];
zip.forEach((relativePath, zipEntry) => {
if (!zipEntry.dir) { // Only handle files, not directories
fileEntries.push({ path: relativePath, entry: zipEntry });
}
});
fileEntries.sort((a, b) => a.path.localeCompare(b.path)); // Sort files alphabetically
const transaction = db.transaction([storeName], "readwrite");
const objectStore = transaction.objectStore(storeName);
const clearRequest = objectStore.clear(); // Clear existing data
clearRequest.onsuccess = function() {
const addPromises = fileEntries.map((item, index) =>
item.entry.async("string").then(content =>
new Promise((resolve, reject) => {
const request = objectStore.add({ name: item.path, content: content, order: index });
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
})
)
);
Promise.all(addPromises).then(() => {
transaction.oncomplete = function() {
loadFilesIntoUI(); // Refresh UI after storing
};
}).catch(error => {
console.error("Error adding files:", error);
});
};
clearRequest.onerror = function() {
console.error("Error clearing store");
};
} catch (error) {
console.error("Error unzipping file:", error);
}
}
// Load files from IndexedDB and display in UI
function loadFilesIntoUI() {
const transaction = db.transaction([storeName], "readonly");
const objectStore = transaction.objectStore(storeName);
const request = objectStore.getAll();
request.onsuccess = function(event) {
const files = event.target.result.sort((a, b) => a.order - b.order); // Sort by order field
const fileList = document.getElementById("fileList");
fileList.innerHTML = ""; // Clear existing list
files.forEach(file => {
const li = document.createElement("li");
li.textContent = file.name;
li.setAttribute("draggable", true);
li.dataset.id = file.id;
li.addEventListener("click", () => openEditor(file.id));
li.addEventListener("dragstart", dragStart);
li.addEventListener("dragover", dragOver);
li.addEventListener("drop", dropHandler);
li.addEventListener("dragend", dragEnd);
fileList.appendChild(li);
});
};
}
// Open editor for a specific file
function openEditor(id) {
const transaction = db.transaction([storeName], "readonly");
const objectStore = transaction.objectStore(storeName);
const request = objectStore.get(id);
request.onsuccess = function() {
const file = request.result;
if (file) {
const editArea = document.getElementById("editArea");
editArea.value = file.content; // Load content into textarea
currentEditingId = id; // Track the currently editing file
}
};
}
// Save the currently editing file
document.getElementById("saveButton").addEventListener("click", function() {
if (currentEditingId !== null) {
const content = document.getElementById("editArea").value;
const transaction = db.transaction([storeName], "readwrite");
const objectStore = transaction.objectStore(storeName);
const request = objectStore.get(currentEditingId);
request.onsuccess = function() {
const file = request.result;
if (file) {
file.content = content; // Update content
const putRequest = objectStore.put(file);
putRequest.onsuccess = function() {
console.log("File saved successfully");
};
}
};
} else {
alert("No file selected to save.");
}
});
// Search functionality
document.getElementById("searchInput").addEventListener("input", function(e) {
const searchTerm = e.target.value.toLowerCase();
const fileListItems = document.querySelectorAll("#fileList li");
fileListItems.forEach(li => {
const name = li.textContent.toLowerCase();
if (name.includes(searchTerm)) {
li.style.display = ""; // Show if matches
} else {
li.style.display = "none"; // Hide if no match
}
});
});
// Drag and drop handlers for reordering
function dragStart(e) {
e.dataTransfer.setData("text/plain", e.target.dataset.id);
e.dataTransfer.effectAllowed = "move";
}
function dragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = "move";
}
function dropHandler(e) {
e.preventDefault();
const draggedId = e.dataTransfer.getData("text/plain");
const targetId = e.currentTarget.dataset.id;
if (draggedId !== targetId) {
const fileList = document.getElementById("fileList");
const draggedElement = document.querySelector(`#fileList li[data-id="${draggedId}"]`);
const targetElement = e.currentTarget;
// Insert dragged element before target element
fileList.insertBefore(draggedElement, targetElement);
// Get new order of IDs
const newOrderElements = Array.from(fileList.children);
const newOrderIds = newOrderElements.map(el => el.dataset.id);
updateOrderInDB(newOrderIds); // Update order in database
}
}
function dragEnd(e) {
// Optional: Clean up after drag ends
}
// Update order in IndexedDB
function updateOrderInDB(newOrderIds) {
const transaction = db.transaction([storeName], "readwrite");
const objectStore = transaction.objectStore(storeName);
const updatePromises = newOrderIds.map((id, index) =>
new Promise((resolve, reject) => {
const getRequest = objectStore.get(parseInt(id)); // Ensure id is parsed as number if autoIncrement
getRequest.onsuccess = function() {
const file = getRequest.result;
if (file) {
file.order = index; // Update order field
const putRequest = objectStore.put(file);
putRequest.onsuccess = () => resolve();
putRequest.onerror = () => reject(putRequest.error);
} else {
resolve(); // Resolve if file not found (shouldn't happen)
}
};
getRequest.onerror = () => reject(getRequest.error);
})
);
Promise.all(updatePromises).then(() => {
console.log("Order updated in database");
}).catch(error => {
console.error("Error updating order:", error);
});
}
</script>
</body>
</html>
Explanation:
HTML Structure: Includes a drop zone for dragging zip files, a search input for filtering file names, a list (ul#fileList) for displaying files with drag-and-drop reordering, and an editor section with a textarea and save button.
CSS: Provides basic styling for layout, drag-and-drop feedback, and hiding elements during search.
JavaScript:
Uses IndexedDB to store file metadata (name, content, order) with auto-incrementing IDs.
Handles zip file unzipping using JSZip (loaded from CDN). When a zip is dropped, it clears the existing database, unzips the file, stores text files with an initial order, and refreshes the UI.
Search: Filters the file list based on file names as the user types in the search input.
Editing: Clicking a file loads its content into the textarea. A save button updates the content in IndexedDB.
Reordering: Implements HTML5 drag-and-drop to reorder files in the list. After reordering, it updates the order in IndexedDB.
Dependencies: This tool relies on JSZip for unzipping, included via a CDN script tag. To make it a truly single file without internet access, you could download and embed the JSZip minified code directly into the script tag, but it's left as a CDN reference for simplicity.
Limitations:
Only text files within the zip are handled (assumes all files are text; binary files may not work correctly).
Search is limited to file names; content search is not implemented to avoid performance issues.
Unsaved changes in the editor are lost when switching files; consider adding auto-save or confirmation in future enhancements.
Usage: Drag a zip file onto the drop zone to load and store its text files. Search, edit, and reorder files as needed. All data is stored locally in the browser's IndexedDB.