Skip to content

Webflow edit page #6611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion supervisor/shared/web_workflow/static/directory.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<body>
<h1><a href="/"><img src="/favicon.ico"/></a>&nbsp;<span id="path"></span></h1>
<div id="usbwarning" style="display: none;">🛈 USB is using the storage. Only allowing reads. See <a href="https://learn.adafruit.com/circuitpython-essentials/circuitpython-storage">the CircuitPython Essentials: Storage guide</a> for details.</div>
<template id="row"><tr><td></td><td></td><td><a></a></td><td></td><td><button class="delete">🗑️</button></td></tr></template>
<template id="row"><tr><td></td><td></td><td><a></a></td><td></td><td><button class="delete">🗑️</button></td><td><a class="edit_link" href="">Edit</a></td></tr></template>
<table>
<thead><tr><th>Type</th><th>Size</th><th>Path</th><th>Modified</th><th></th></tr></thead>
<tbody></tbody>
Expand Down
3 changes: 3 additions & 0 deletions supervisor/shared/web_workflow/static/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ async function refresh_list() {
delete_button.disabled = !editable;
delete_button.onclick = del;

let edit_url = new URL("/edit/#" + f.name, url_base);
let edit_link = clone.querySelector(".edit_link");
edit_link.href = edit_url

new_children.push(clone);
}
Expand Down
26 changes: 26 additions & 0 deletions supervisor/shared/web_workflow/static/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code Edit</title>
<style>
#code_textarea {
width: 90%;
height: 600px;
}

#output_text {
margin: 0;
font-size: 0.7em;
}
</style>

</head>
<body>
<button id="save_btn">Save</button>
<p id="output_text">Loading</p>
<textarea id="code_textarea"></textarea>

<script src="/edit.js" defer=true></script>
</body>
</html>
47 changes: 47 additions & 0 deletions supervisor/shared/web_workflow/static/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
let $editor = document.querySelector("#code_textarea");
let filename = location.hash.substring(1);
let $output_text = document.querySelector("#output_text");

fetch(`/fs/${filename}`)
.then(function (response) {
$output_text.innerText = `Loading Status: ${response.status}`;
return response.status === 200 ? response.text() : "";
})
.then(function (data) {
$editor.value = data;
});

function save() {
$output_text.innerText = "Saving..."
const requestOptions = {
method: 'PUT',
body: $editor.value
};
fetch(`/fs/${filename}`, requestOptions)
.then(function (response) {
$output_text.innerText = `Saving Status: ${response.status}`;
return response.text();
})
.then(function (data) {
console.log("after fetch: " + data);
});
}

document.querySelector("#save_btn").onclick = function () {
console.log("Click Save!");
save();
}

let isCtrl = false;
document.onkeyup=function(e){
if(e.keyCode === 17) isCtrl=false;
}

document.onkeydown=function(e){
if(e.keyCode === 17) isCtrl=true;
if(e.keyCode === 83 && isCtrl === true) {
//ctrl-s pressed
save();
return false;
}
}
14 changes: 14 additions & 0 deletions supervisor/shared/web_workflow/web_workflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@ STATIC_FILE(directory_html);
STATIC_FILE(directory_js);
STATIC_FILE(welcome_html);
STATIC_FILE(welcome_js);
STATIC_FILE(edit_html);
STATIC_FILE(edit_js);
STATIC_FILE(style_css);
STATIC_FILE(serial_html);
STATIC_FILE(serial_js);
Expand Down Expand Up @@ -1065,6 +1067,16 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
}
}
}
} else if (strcmp(request->path, "/edit/") == 0) {
if (!request->authenticated) {
if (_api_password[0] != '\0') {
_reply_unauthorized(socket, request);
} else {
_reply_forbidden(socket, request);
}
} else {
_REPLY_STATIC(socket, request, edit_html);
}
} else if (strncmp(request->path, "/cp/", 4) == 0) {
const char *path = request->path + 3;
if (strcasecmp(request->method, "OPTIONS") == 0) {
Expand Down Expand Up @@ -1104,6 +1116,8 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
_REPLY_STATIC(socket, request, welcome_js);
} else if (strcmp(request->path, "/serial.js") == 0) {
_REPLY_STATIC(socket, request, serial_js);
} else if (strcmp(request->path, "/edit.js") == 0) {
_REPLY_STATIC(socket, request, edit_js);
} else if (strcmp(request->path, "/style.css") == 0) {
_REPLY_STATIC(socket, request, style_css);
} else if (strcmp(request->path, "/favicon.ico") == 0) {
Expand Down
4 changes: 3 additions & 1 deletion tools/gen_web_workflow_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
if f.name.endswith(".html"):
uncompressed = minify_html.minify(uncompressed.decode("utf-8")).encode("utf-8")
elif f.name.endswith(".js"):
uncompressed = jsmin.jsmin(uncompressed.decode("utf-8")).encode("utf-8")
uncompressed = jsmin.jsmin(uncompressed.decode("utf-8"), quote_chars="'\"`").encode(
"utf-8"
)
compressed = gzip.compress(uncompressed)
clen = len(compressed)
compressed = ", ".join([hex(x) for x in compressed])
Expand Down