Add 'Save as new' button

This commit is contained in:
Nikolay Puzanov 2022-12-02 14:53:43 +03:00
parent ec02787747
commit 680e9f3aa5
2 changed files with 29 additions and 19 deletions

View File

@ -94,7 +94,8 @@
<body> <body>
<div id="buttons"> <div id="buttons">
<button onclick="save_code()"><span class="text">Save (ctrl-s)</span></button> <button onclick="save_code('%SAVECODEURI%')"><span class="text">Save (ctrl-s)</span></button>
<button onclick="save_code('%SAVEASURI%')"><span class="text">Save as new</span></button>
Sim: Sim:
<button onclick="send_to_icarus()"><span class="text">Icarus</span></button> <button onclick="send_to_icarus()"><span class="text">Icarus</span></button>
<!-- button><span class="text">Verilator</span></button --> <!-- button><span class="text">Verilator</span></button -->
@ -136,8 +137,8 @@
.then((text) => { log_area.innerHTML = text; }); .then((text) => { log_area.innerHTML = text; });
}; };
function save_code() { function save_code(uri) {
fetch('%SAVECODEURI%', fetch(uri,
{ {
method: 'POST', method: 'POST',
headers: { headers: {

View File

@ -41,6 +41,7 @@
(define URI-IVERILOG "iverilog") (define URI-IVERILOG "iverilog")
(define URI-SAVE-CODE "save") (define URI-SAVE-CODE "save")
(define URI-SAVEAS-CODE "saveas")
(define LOG-DBG 3) (define LOG-DBG 3)
(define LOG-VERBOSE 2) (define LOG-VERBOSE 2)
@ -393,10 +394,13 @@
(root (encode-and-join-uri-path root-path)) (root (encode-and-join-uri-path root-path))
(iverilog-path (append root-path `(,URI-IVERILOG))) (iverilog-path (append root-path `(,URI-IVERILOG)))
(savecode-path (append root-path `(,URI-SAVE-CODE))) (savecode-path (append root-path `(,URI-SAVE-CODE)))
(saveas-path (append root-path `(,URI-SAVEAS-CODE)))
(iverilog-post-uri (encode-and-join-uri-path iverilog-path)) (iverilog-post-uri (encode-and-join-uri-path iverilog-path))
(savecode-post-uri (encode-and-join-uri-path savecode-path)) (savecode-post-uri (encode-and-join-uri-path savecode-path))
(saveas-post-uri (encode-and-join-uri-path saveas-path))
(index-html (read-template-text index-file `(("IVERILOGPOSTURI" ,iverilog-post-uri) (index-html (read-template-text index-file `(("IVERILOGPOSTURI" ,iverilog-post-uri)
("SAVECODEURI" ,savecode-post-uri))))) ("SAVECODEURI" ,savecode-post-uri)
("SAVEASURI" ,saveas-post-uri)))))
(lambda (request request-body) (lambda (request request-body)
(let (;; Requested resource path (let (;; Requested resource path
@ -484,21 +488,26 @@
#:type 'text/plain)) #:type 'text/plain))
;; Save snippet ;; Save snippet
((equal? path savecode-path) ((or (equal? path savecode-path)
(logger LOG-DBG "Request code saving") (equal? path saveas-path))
(let ((stor-dir (let ((saveas (equal? path saveas-path)))
(or ref-stor-dir (logger LOG-DBG "Request code saving~a"
(basename (if saveas " as new snippet" ""))
(mkdtemp (let ((stor-dir
(path+ (if (or saveas
stor-base (not ref-stor-dir))
(format "~a-XXXXXX" (basename
(current-time)))))))) (mkdtemp
(save-to-storage (path+ stor-base stor-dir) code) (path+
(make-response stor-base
(encode-and-join-uri-path (format "~a-XXXXXX"
(append root-path `(,stor-dir))) (current-time)))))
#:type 'text/plain))) ref-stor-dir)))
(save-to-storage (path+ stor-base stor-dir) code)
(make-response
(encode-and-join-uri-path
(append root-path `(,stor-dir)))
#:type 'text/plain))))
;; Wrong POST request ;; Wrong POST request
(else (else