Skip to content

Web workflow edit page #6592

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

Closed
wants to merge 6 commits into from
Closed
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 extmod/ulab
Submodule ulab updated 109 files
2 changes: 1 addition & 1 deletion frozen/pew-pewpew-lcd
Submodule pew-pewpew-lcd updated 1 files
+24 −1 pew.py
54 changes: 10 additions & 44 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2356,10 +2356,6 @@ msgstr ""
msgid "array and index length must be equal"
msgstr ""

#: extmod/ulab/code/numpy/io/io.c
msgid "array has too many dimensions"
msgstr ""

#: py/objarray.c shared-bindings/alarm/SleepMemory.c
#: shared-bindings/nvm/ByteArray.c
msgid "array/bytes required on right side"
Expand Down Expand Up @@ -2727,10 +2723,6 @@ msgstr ""
msgid "convolve arguments must not be empty"
msgstr ""

#: extmod/ulab/code/numpy/io/io.c
msgid "corrupted file"
msgstr ""

#: extmod/ulab/code/numpy/poly.c
msgid "could not invert Vandermonde matrix"
msgstr ""
Expand Down Expand Up @@ -2822,10 +2814,6 @@ msgstr ""
msgid "empty"
msgstr ""

#: extmod/ulab/code/numpy/io/io.c
msgid "empty file"
msgstr ""

#: extmod/moduasyncio.c extmod/moduheapq.c extmod/modutimeq.c
msgid "empty heap"
msgstr ""
Expand Down Expand Up @@ -2929,7 +2917,7 @@ msgstr ""
msgid "first argument must be a tuple of ndarrays"
msgstr ""

#: extmod/ulab/code/numpy/transform.c extmod/ulab/code/numpy/vector.c
#: extmod/ulab/code/numpy/vector.c
msgid "first argument must be an ndarray"
msgstr ""

Expand Down Expand Up @@ -3077,7 +3065,7 @@ msgstr ""
msgid "incorrect padding"
msgstr ""

#: extmod/ulab/code/ndarray.c extmod/ulab/code/numpy/transform.c
#: extmod/ulab/code/ndarray.c
msgid "index is out of bounds"
msgstr ""

Expand Down Expand Up @@ -3144,10 +3132,6 @@ msgstr ""
msgid "input matrix is singular"
msgstr ""

#: extmod/ulab/code/numpy/create.c
msgid "input must be 1- or 2-d"
msgstr ""

#: extmod/ulab/code/numpy/carray/carray.c
msgid "input must be a 1D ndarray"
msgstr ""
Expand All @@ -3156,7 +3140,11 @@ msgstr ""
msgid "input must be a dense ndarray"
msgstr ""

#: extmod/ulab/code/user/user.c
#: extmod/ulab/code/numpy/create.c
msgid "input must be a tensor of rank 2"
msgstr ""

#: extmod/ulab/code/numpy/create.c extmod/ulab/code/user/user.c
msgid "input must be an ndarray"
msgstr ""

Expand Down Expand Up @@ -3357,7 +3345,7 @@ msgid "max_length must be 0-%d when fixed_length is %s"
msgstr ""

#: extmod/ulab/code/ndarray.c
msgid "maximum number of dimensions is "
msgid "maximum number of dimensions is 4"
msgstr ""

#: py/runtime.c
Expand Down Expand Up @@ -3606,7 +3594,7 @@ msgstr ""
msgid "off"
msgstr ""

#: extmod/ulab/code/utils/utils.c
#: extmod/ulab/code/numpy/create.c extmod/ulab/code/utils/utils.c
msgid "offset is too large"
msgstr ""

Expand Down Expand Up @@ -4192,14 +4180,6 @@ msgstr ""
msgid "unsupported types for %q: '%q', '%q'"
msgstr ""

#: extmod/ulab/code/numpy/io/io.c
msgid "usecols is too high"
msgstr ""

#: extmod/ulab/code/numpy/io/io.c
msgid "usecols keyword must be specified"
msgstr ""

#: py/objint.c
#, c-format
msgid "value must fit in %d byte(s)"
Expand Down Expand Up @@ -4241,28 +4221,14 @@ msgstr ""
msgid "wrong axis specified"
msgstr ""

#: extmod/ulab/code/numpy/io/io.c
msgid "wrong dtype"
msgstr ""

#: extmod/ulab/code/numpy/transform.c
msgid "wrong index type"
msgstr ""

#: extmod/ulab/code/numpy/compare.c extmod/ulab/code/numpy/create.c
#: extmod/ulab/code/numpy/io/io.c extmod/ulab/code/numpy/transform.c
#: extmod/ulab/code/numpy/vector.c
#: extmod/ulab/code/numpy/compare.c extmod/ulab/code/numpy/vector.c
msgid "wrong input type"
msgstr ""

#: extmod/ulab/code/numpy/transform.c
msgid "wrong length of condition array"
msgstr ""

#: extmod/ulab/code/numpy/transform.c
msgid "wrong length of index array"
msgstr ""

#: extmod/ulab/code/numpy/create.c py/objarray.c py/objstr.c
msgid "wrong number of arguments"
msgstr ""
Expand Down
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 @@ -8,7 +8,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
22 changes: 14 additions & 8 deletions supervisor/shared/web_workflow/static/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function refresh_list() {
// Clone the new row and insert it into the table
var clone = template.content.cloneNode(true);
var td = clone.querySelectorAll("td");
var icon = "";
var icon = "&#11015;&#65039;";
var file_path = current_path + f.name;
let api_url = new URL("/fs" + file_path, url_base);
if (f.directory) {
Expand All @@ -71,26 +71,32 @@ async function refresh_list() {
}

if (f.directory) {
icon = "🗀";
icon = "&#128193;";
} else if(f.name.endsWith(".txt") ||
f.name.endsWith(".py") ||
f.name.endsWith(".js") ||
f.name.endsWith(".json")) {
icon = "🖹";
f.name.endsWith(".py") ||
f.name.endsWith(".js") ||
f.name.endsWith(".json")) {
icon = "&#128196;";
} else if (f.name.endsWith(".html")) {
icon = "🌐";
icon = "&#127760;";
}
td[0].textContent = icon;
td[0].innerHTML = icon;

td[1].textContent = f.file_size;
var path = clone.querySelector("a");
path.href = file_path;
path.textContent = f.name;
td[3].textContent = (new Date(f.modified_ns / 1000000)).toLocaleString();

var delete_button = clone.querySelector("button.delete");
delete_button.value = api_url;
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
40 changes: 40 additions & 0 deletions supervisor/shared/web_workflow/static/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"
integrity="sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.6.0/ace.min.js"
integrity="sha512-Ky7AOm/5oRYp5QzV9diL95tE/OKjzfAkugQ+llHy1scOlzIyAt2SoyriapPAZTvtZNL/xbYI1Gvt5jJYurPBdw=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.6.0/mode-python.min.js"
integrity="sha512-SdtfSOaR+TnSvGsZ2dmErFrcMC/CK6J/l2kNaXv3AU9BtNzqLDtK69ImVp6zOAY9Udii9GrtH7NssygOh/w0hg=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.6.0/ext-settings_menu.min.js"
integrity="sha512-GqA/hV/4FrUn8lUmY+5EWvrB4Bbw3iv0TMY4wVFLWp+OyMqhKhYWXCtYA0+X68TdEA8nUq3og+d6VLTtwprvyw=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
<button id="save_btn">Save</button>
<button id="docs_btn">Docs</button>
<button id="undo_btn">Undo</button>
<button id="redo_btn">Redo</button>
<p id="output_text">Loading</p>
<div id="code_textarea"></div>

<script src="/edit.js" defer=true></script>
</body>
</html>
90 changes: 90 additions & 0 deletions supervisor/shared/web_workflow/static/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
let editor;
require(["ace/ace", "ace/ext/settings_menu"], function (ace) {
editor = ace.edit("code_textarea");
ace.config.set('basePath', 'https://cdnjs.cloudflare.com/ajax/libs/ace/1.6.0/');
console.log("after create editor");
console.log(editor);

editor.session.setMode("ace/mode/python");
ace.require('ace/ext/settings_menu').init(editor);
editor.commands.addCommands([{
name: "showSettingsMenu",
bindKey: {win: "Ctrl-e", mac: "Ctrl-e"},
exec: function (editor) {
console.log("ctrl-e")
editor.showSettingsMenu();
},
readOnly: true
}, {
name: "infoDocsSearch",
bindKey: {win: "Ctrl-i", mac: "Ctrl-i"},
exec: function (editor) {
window.open(`https://docs.circuitpython.org/en/latest/search.html?q=${editor.getSelectedText()}`, '_blank');
},
readOnly: true
},{
name: 'Save',
bindKey: {win: 'Ctrl-S', mac: 'Command-S'},
exec: function (editor) {
console.log("ctrl-s save");
save();

}
},{
name: "replaceCtrlR",
bindKey: {win: "Ctrl-r", mac: "Ctrl-r"},
exec: function (editor_arg) {
console.log("override ctrl r");
editor.execCommand('replace');
console.log(editor);
},
readOnly: true
}]);


});

let filename = location.hash.substring(1);
let $output_text = document.querySelector("#output_text");
/*let $code_text = document.querySelector("#code_textarea");*/

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

function save() {
$output_text.innerText = "Saving..."
const requestOptions = {
method: 'PUT',
body: editor.getValue()
};
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();
}
document.querySelector("#docs_btn").onclick = function () {
window.open(`https://docs.circuitpython.org/en/latest/search.html?q=${editor.getSelectedText()}`, '_blank');
}

document.querySelector("#undo_btn").onclick = function () {
editor.undo();
}

document.querySelector("#redo_btn").onclick = function () {
editor.redo();
}
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 @@ -838,6 +838,8 @@ static void _write_file_and_reply(socketpool_socket_obj_t *socket, _request *req
STATIC_FILE(directory_html);
STATIC_FILE(directory_js);
STATIC_FILE(welcome_html);
STATIC_FILE(edit_html);
STATIC_FILE(edit_js);
STATIC_FILE(welcome_js);
STATIC_FILE(serial_html);
STATIC_FILE(serial_js);
Expand Down Expand Up @@ -1011,6 +1013,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 (memcmp(request->path, "/cp/", 4) == 0) {
const char *path = request->path + 3;
if (strcmp(request->method, "GET") != 0) {
Expand Down Expand Up @@ -1045,6 +1057,8 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
_REPLY_STATIC(socket, request, directory_js);
} else if (strcmp(request->path, "/welcome.js") == 0) {
_REPLY_STATIC(socket, request, welcome_js);
} else if (strcmp(request->path, "/edit.js") == 0) {
_REPLY_STATIC(socket, request, edit_js);
} else if (strcmp(request->path, "/serial.js") == 0) {
_REPLY_STATIC(socket, request, serial_js);
} else if (strcmp(request->path, "/favicon.ico") == 0) {
Expand Down