diff --git a/src/main/java/cc/fascinated/backend/config/Config.java b/src/main/java/cc/fascinated/backend/config/Config.java index 2789a77..fea88ef 100644 --- a/src/main/java/cc/fascinated/backend/config/Config.java +++ b/src/main/java/cc/fascinated/backend/config/Config.java @@ -4,10 +4,18 @@ import lombok.NonNull; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -@Configuration -public class Config { +@Configuration @EnableWebMvc +public class Config implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry + .addResourceHandler("/static/**") + .addResourceLocations("classpath:/static/"); + } @Bean public WebMvcConfigurer configureCors() { diff --git a/src/main/resources/static/assets/script.js b/src/main/resources/static/assets/script.js new file mode 100644 index 0000000..e0d3d02 --- /dev/null +++ b/src/main/resources/static/assets/script.js @@ -0,0 +1,40 @@ +// Handle custom key binds behavior +document.addEventListener("keydown", function (event) { + // Upload the paste when Ctrl + Enter is pressed + if (event.ctrlKey && event.key === "Enter") { + event.preventDefault(); + upload(); + } +}); + +// Upload the paste when the paste button is clicked +document.getElementById("paste-button").addEventListener("click", () => upload()); + +// Upload the paste to the server +const upload = async () => { + var pasteInput = document.getElementById("paste-input"); + var paste = pasteInput.value; + + if (!paste || paste.trim() === "") { + pasteInput.focus(); + alert("Your paste is empty!"); + return; + } + + console.log("Uploading paste..."); + try { + const response = await fetch("/api/upload", { + method: "POST", + body: paste, + }); + const data = await response.json(); + if (!response.ok) { + throw new Error(data.message); + } + + window.location.href = "/" + data.id; + } catch (error) { + console.error("Error:", error); + alert(`${error.message || "An error occurred while uploading the paste."}`); + } +}; diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index c79fb51..540532a 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -29,39 +29,5 @@ - + \ No newline at end of file