From 895483fdd4aac310e00272026545b33c23f1b4a7 Mon Sep 17 00:00:00 2001 From: am009 Date: Thu, 18 Jul 2024 22:17:16 +0000 Subject: [PATCH] Sync Editor upload with settings upload. --- frontend/app/editor.tsx | 9 +++-- frontend/app/page.tsx | 77 ++++++++++++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/frontend/app/editor.tsx b/frontend/app/editor.tsx index fc02422..af29bfa 100644 --- a/frontend/app/editor.tsx +++ b/frontend/app/editor.tsx @@ -29,9 +29,9 @@ import dynamic from "next/dynamic"; const MonacoDiffEditor = dynamic(() => import("./monaco"), { ssr: false, loading: () => ( - -
Loading... -
) + +
Loading... +
) }); const { Sider, Content } = Layout; @@ -230,6 +230,9 @@ export default function DiffEditor({ + + + ) const DiffWindow = () => (<> diff --git a/frontend/app/page.tsx b/frontend/app/page.tsx index 9c888b8..f348587 100644 --- a/frontend/app/page.tsx +++ b/frontend/app/page.tsx @@ -24,15 +24,24 @@ import Col from 'antd/es/col'; import Form from 'antd/es/form'; import Divider from 'antd/es/divider'; import { Color, ColorFactory } from 'antd/es/color-picker/color'; +import { zipSync, Unzipped } from 'fflate/browser'; import Giscus from './giscus'; import message from 'antd/es/message'; import DiffEditor from './editor'; +import type { ValidateStatus } from 'antd/es/form/FormItem'; import { useState, useEffect, useRef } from 'react'; const { Option } = Select; export default function Home() { + const [oldValidateStatus, setOldValidateStatus] = useState('') + const [newValidateStatus, setNewValidateStatus] = useState('') + const [oldTreeData, setOldTreeData] = useState(null) + const [newTreeData, setNewTreeData] = useState(null) + + const hasTreeData = oldTreeData !== null || newTreeData !== null + const submitButton = useRef(null); const [dockerCmd, setDockerCmd] = useState("docker run --rm -v :/work am009/latexdiff-web-worker"); const [configJson, setConfigJson] = useState(""); @@ -144,12 +153,50 @@ export default function Home() { message.error("Failed to generate diff pdf.", 6); } } + + const treeToFile = (tree: Unzipped) => { + const data = zipSync(tree, { level: 0 }); + return new Blob([data], { type: 'application/zip' }) + } + // Submitting the form const onFinish = (values: any) => { - console.log("Success:", values); + console.log("Submit values:", values); + console.log("Tree values:", hasTreeData, oldTreeData, newTreeData); + if (hasTreeData) { + if (oldTreeData === null || newTreeData === null) { + message.error('Please select both old and new project zip file!') + setCurrentTab("editor"); + return + } + } else { + setOldValidateStatus(values.old_zip === undefined || values.old_zip.length === 0 ? 'error' : '') + setNewValidateStatus(values.new_zip === undefined || values.new_zip.length === 0 ? 'error' : '') + if (values.old_zip === undefined) { + message.error('Please select old project zip file!') + return + } + if (values.new_zip === undefined) { + message.error('Please select new project zip file!') + return + } + } + + let form = new FormData(); - form.append("old_zip", values.old_zip[0].originFileObj); - form.append("new_zip", values.new_zip[0].originFileObj); + if (hasTreeData) { + if (oldTreeData === null || newTreeData === null) { + message.error('Please select both old and new project zip file!') + setCurrentTab("editor"); + return + } + form.append("old_zip", treeToFile(oldTreeData), "old.zip"); + form.append("new_zip", treeToFile(newTreeData), "new.zip"); + } else { + form.append("old_zip", values.old_zip[0].originFileObj); + form.append("new_zip", values.new_zip[0].originFileObj); + } + let config = genConfig(); setConfigJson(JSON.stringify(config)); console.log("Current config:", config); @@ -301,22 +348,26 @@ export default function Home() { setFields({ ...fields, api_endpoint: e.target.value })} /> - - false} maxCount={1} accept='zip,application/zip,application/x-zip,application/x-zip-compressed'> - + help={oldValidateStatus === 'error' ? "*Please select old project zip file!" : undefined}> + false} maxCount={1} accept='zip,application/zip,application/x-zip,application/x-zip-compressed'> + - - false} maxCount={1} accept='zip,application/zip,application/x-zip,application/x-zip-compressed'> - + help={newValidateStatus === 'error' ? "*Please select new project zip file!" : undefined}> + false} maxCount={1} accept='zip,application/zip,application/x-zip,application/x-zip-compressed'> + @@ -440,7 +491,7 @@ export default function Home() { <a href="https://github.com/am009/git-latexdiff-web">git-latexdiff web</a> ); - let editor = () + let editor = () let right = currentTab === 'editor' ? next : submit2 return ( @@ -464,7 +515,7 @@ export default function Home() { }}> git-latexdiff web is an online tool based on latexdiff and git-latexdiff - Source code on Github / Use it offline + Source code on Github / Use it offline