From: Adam Dullage Date: Fri, 10 May 2024 12:05:10 +0000 (+0100) Subject: Add window.onbeforeunload check and empty title validation X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=a8a6beb6ffbd98b25a541f711c7dbc974b021c85;p=flatnotes.git Add window.onbeforeunload check and empty title validation --- diff --git a/client/views/Note.vue b/client/views/Note.vue index 617220f..e921e10 100644 --- a/client/views/Note.vue +++ b/client/views/Note.vue @@ -19,7 +19,7 @@ }} @@ -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.")); } diff --git a/entrypoint.sh b/entrypoint.sh index 963ea64..4d52468 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,43 +1,43 @@ -#!/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 +#!/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 diff --git a/healthcheck.sh b/healthcheck.sh index 3719d71..8d01824 100644 --- a/healthcheck.sh +++ b/healthcheck.sh @@ -1,3 +1,3 @@ -#!/bin/sh - -curl -f http://localhost:${FLATNOTES_PORT}/health || exit 1 +#!/bin/sh + +curl -f http://localhost:${FLATNOTES_PORT}/health || exit 1