]> OzVa Git service - ozva-cloud/commitdiff
feat: support new file (#180)
authorsigoden <sigoden@gmail.com>
Tue, 21 Feb 2023 00:45:52 +0000 (08:45 +0800)
committerGitHub <noreply@github.com>
Tue, 21 Feb 2023 00:45:52 +0000 (08:45 +0800)
assets/index.html
assets/index.js

index 0f35410711ca0415689ee3135900a7068bad5283..6694f357ed212dfa80b56d4bafe55be7931563ee 100644 (file)
             d="M13.5 10a.5.5 0 0 1 .5.5V12h1.5a.5.5 0 1 1 0 1H14v1.5a.5.5 0 1 1-1 0V13h-1.5a.5.5 0 0 1 0-1H13v-1.5a.5.5 0 0 1 .5-.5z" />
         </svg>
       </div>
+      <div class="control new-file hidden" title="New File">
+        <svg width="16" height="16" viewBox="0 0 16 16">
+          <path
+            d="M8 6.5a.5.5 0 0 1 .5.5v1.5H10a.5.5 0 0 1 0 1H8.5V11a.5.5 0 0 1-1 0V9.5H6a.5.5 0 0 1 0-1h1.5V7a.5.5 0 0 1 .5-.5z" />
+          <path
+            d="M14 4.5V14a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V2a2 2 0 0 1 2-2h5.5L14 4.5zm-3 0A1.5 1.5 0 0 1 9.5 3V1H4a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V4.5h-2z" />
+        </svg>
+      </div>
     </div>
     <form class="searchbar hidden">
       <div class="icon">
-        <svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
+        <svg width="16" height="16" viewBox="0 0 16 16">
           <path
             d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z" />
         </svg>
index 7c66b8b84306150e49ac3b5cc564be0d573a5b6c..4349c38595679eec23ce3e476d129563edaf32f3 100644 (file)
@@ -84,7 +84,6 @@ function ready() {
 
   if (DATA.kind == "Index") {
     document.title = `Index of ${DATA.href} - Dufs`;
-
     document.querySelector(".index-page").classList.remove("hidden");
 
     if (DATA.allow_search) {
@@ -101,9 +100,12 @@ function ready() {
     if (DATA.allow_upload) {
       dropzone();
       setupUpload();
+      setupNewFile();
     }
   } else if (DATA.kind == "Edit") {
     document.title = `Edit of ${DATA.href} - Dufs`;
+    document.querySelector(".editor-page").classList.remove("hidden");;
+
 
     setupEditor();
   }
@@ -497,9 +499,16 @@ function setupUpload() {
   });
 }
 
-async function setupEditor() {
-  document.querySelector(".editor-page").classList.remove("hidden");;
+function setupNewFile() {
+  const $newFile = document.querySelector(".new-file");
+  $newFile.classList.remove("hidden");
+  $newFile.addEventListener("click", () => {
+    const name = prompt("Enter file name");
+    if (name) createFile(name);
+  });
+}
 
+async function setupEditor() {
   const $download = document.querySelector(".download")
   $download.classList.remove("hidden");
   $download.href = baseUrl()
@@ -557,6 +566,20 @@ async function createFolder(name) {
   }
 }
 
+async function createFile(name) {
+  const url = newUrl(name);
+  try {
+    const res = await fetch(url, {
+      method: "PUT",
+      body: "",
+    });
+    await assertFetch(res);
+    location.href = url + "?edit";
+  } catch (err) {
+    alert(`Cannot create file \`${name}\`, ${err.message}`);
+  }
+}
+
 async function addFileEntries(entries, dirs) {
   for (const entry of entries) {
     if (entry.isFile) {