Add window.onbeforeunload check and empty title validation
authorAdam Dullage <redacted>
Fri, 10 May 2024 12:05:10 +0000 (13:05 +0100)
committerAdam Dullage <redacted>
Fri, 10 May 2024 12:05:10 +0000 (13:05 +0100)
client/views/Note.vue
entrypoint.sh
healthcheck.sh

index 617220f3a8693100538d4ca3881db4ac1e683461..e921e102ca2b232e54b606f36cd3329ec0c8cde9 100644 (file)
@@ -19,7 +19,7 @@
         }}</span>
         <input
           v-show="editMode"
-          v-model="newTitle"
+          v-model.trim="newTitle"
           class="flex-1 bg-theme-background outline-none"
           placeholder="Title"
         />
@@ -148,11 +148,12 @@ function init() {
   } else {
     newTitle.value = "";
     note.value = new Note();
-    editMode.value = true;
+    editHandler();
     loadingIndicator.value.setLoaded();
   }
 }
 
+// Helpers
 function entityTooLargeToast(entityName) {
   toast.add(
     getToastOptions(
@@ -167,13 +168,25 @@ function badFilenameToast(entityName) {
   toast.add(
     getToastOptions(
       `Invalid ${entityName}`,
-      "Due to filename restrictions, the following characters are not allowed: <>:\"/\\|?*",
+      'Due to filename restrictions, the following characters are not allowed: <>:"/\\|?*',
       true,
     ),
   );
 }
 
+function setBeforeUnloadConfirmation(enable = true) {
+  if (enable) {
+    window.onbeforeunload = () => {
+      return true;
+    };
+  } else {
+    window.onbeforeunload = null;
+  }
+}
+
+// Button Handlers
 function editHandler() {
+  setBeforeUnloadConfirmation(true);
   newTitle.value = note.value.title;
   editMode.value = true;
 }
@@ -182,18 +195,8 @@ function deleteHandler() {
   isDeleteModalVisible.value = true;
 }
 
-function deleteConfirmedHandler() {
-  deleteNote(note.value.title)
-    .then(() => {
-      toast.add(getToastOptions("Success", "Note deleted."));
-      router.push({ name: "home" });
-    })
-    .catch((error) => {
-      apiErrorHandler(error, toast);
-    });
-}
-
 function cancelHandler() {
+  setBeforeUnloadConfirmation(false);
   editMode.value = false;
   if (!props.title) {
     router.push({ name: "home" });
@@ -203,10 +206,20 @@ function cancelHandler() {
 }
 
 function saveHandler() {
+  // Empty Title Validation
+  if (!newTitle.value) {
+    toast.add(
+      getToastOptions("Invalid", "Cannot save note without a title", true),
+    );
+    return;
+  }
+
+  // Invalid Character Validation
   if (reservedFilenameCharacters.test(newTitle.value)) {
     badFilenameToast("Title");
     return;
   }
+
   let newContent = toastEditor.value.getMarkdown();
   if (isNewNote.value) {
     saveNew(newTitle.value, newContent);
@@ -215,6 +228,18 @@ function saveHandler() {
   }
 }
 
+// Additional Logic
+function deleteConfirmedHandler() {
+  deleteNote(note.value.title)
+    .then(() => {
+      toast.add(getToastOptions("Success", "Note deleted."));
+      router.push({ name: "home" });
+    })
+    .catch((error) => {
+      apiErrorHandler(error, toast);
+    });
+}
+
 function saveNew(newTitle, newContent) {
   createNote(newTitle, newContent)
     .then((data) => {
@@ -258,6 +283,7 @@ function noteSaveFailure(error) {
 }
 
 function noteSaveSuccess() {
+  setBeforeUnloadConfirmation(false);
   editMode.value = false;
   toast.add(getToastOptions("Success", "Note saved successfully."));
 }
index 963ea64164f4b9072b16b217b3b125f8cd184503..4d52468c046c9acdae801d76b4717910a29d05da 100644 (file)
@@ -1,43 +1,43 @@
-#!/bin/sh\r
-\r
-[ "$EXEC_TOOL" ] || EXEC_TOOL=gosu\r
-[ "$FLATNOTES_PORT" ] || FLATNOTES_PORT=8080\r
-\r
-set -e\r
-\r
-echo "\\r
-======================================\r
-======== Welcome to flatnotes ========\r
-======================================\r
-\r
-If you enjoy using flatnotes, please\r
-consider sponsoring the project at:\r
-\r
-https://sponsor.flatnotes.io\r
-\r
-It would really make my day 🙏.\r
-\r
-──────────────────────────────────────\r
-"\r
-\r
-flatnotes_command="python -m \\r
-                  uvicorn \\r
-                  main:app \\r
-                  --app-dir server \\r
-                  --host 0.0.0.0 \\r
-                  --port ${FLATNOTES_PORT} \\r
-                  --proxy-headers \\r
-                  --forwarded-allow-ips '*'"\r
-\r
-if [ `id -u` -eq 0 ] && [ `id -g` -eq 0 ]; then\r
-    echo Setting file permissions...\r
-    chown -R ${PUID}:${PGID} ${FLATNOTES_PATH}\r
-\r
-    echo Starting flatnotes as user ${PUID}...\r
-    exec ${EXEC_TOOL} ${PUID}:${PGID} ${flatnotes_command}\r
-\r
-else\r
-    echo "A user was set by docker, skipping file permission changes."\r
-    echo Starting flatnotes as user $(id -u)...\r
-    exec ${flatnotes_command}\r
-fi\r
+#!/bin/sh
+
+[ "$EXEC_TOOL" ] || EXEC_TOOL=gosu
+[ "$FLATNOTES_PORT" ] || FLATNOTES_PORT=8080
+
+set -e
+
+echo "\
+======================================
+======== Welcome to flatnotes ========
+======================================
+
+If you enjoy using flatnotes, please
+consider sponsoring the project at:
+
+https://sponsor.flatnotes.io
+
+It would really make my day 🙏.
+
+──────────────────────────────────────
+"
+
+flatnotes_command="python -m \
+                  uvicorn \
+                  main:app \
+                  --app-dir server \
+                  --host 0.0.0.0 \
+                  --port ${FLATNOTES_PORT} \
+                  --proxy-headers \
+                  --forwarded-allow-ips '*'"
+
+if [ `id -u` -eq 0 ] && [ `id -g` -eq 0 ]; then
+    echo Setting file permissions...
+    chown -R ${PUID}:${PGID} ${FLATNOTES_PATH}
+
+    echo Starting flatnotes as user ${PUID}...
+    exec ${EXEC_TOOL} ${PUID}:${PGID} ${flatnotes_command}
+
+else
+    echo "A user was set by docker, skipping file permission changes."
+    echo Starting flatnotes as user $(id -u)...
+    exec ${flatnotes_command}
+fi
index 3719d716545188066406c4d55d428de6287b2c4b..8d01824416f580df512e62bdc5cb0656cee63ad1 100644 (file)
@@ -1,3 +1,3 @@
-#!/bin/sh\r
-\r
-curl -f http://localhost:${FLATNOTES_PORT}/health || exit 1\r
+#!/bin/sh
+
+curl -f http://localhost:${FLATNOTES_PORT}/health || exit 1
git clone https://git.99rst.org/PROJECT