implement file editor

This commit is contained in:
joshuaboud 2021-05-26 17:15:28 -03:00
parent f6fb166b07
commit 19620e11af
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
3 changed files with 73 additions and 1 deletions

View File

@ -145,4 +145,23 @@
flex-flow: row nowrap;
justify-content: flex-end;
align-items: flex-start;
}
.editor-header {
font-weight: bold;
}
.edit-file-contents {
height: 100%;
flex-basis: 0;
flex-grow: 8;
flex-flow: column nowrap;
align-items: stretch;
}
.edit-file-contents > textarea {
flex-grow: 1;
white-space: pre;
overflow: auto;
resize: none;
}

View File

@ -55,6 +55,17 @@
<div class="vertical-spacer"></div>
<div class="flex-row inner-container">
<div class="contents-view" id="nav-contents-view"></div>
<div class="edit-file-contents nav-hidden" id="nav-edit-contents-view">
<div class="editor-header" id="nav-edit-contents-header"></div>
<div class="vertical-spacer"></div>
<textarea id="nav-edit-contents-textarea"></textarea>
<div class="vertical-spacer"></div>
<div class="nav-btn-group">
<button class="pf-c-button pf-m-danger" id="nav-cancel-edit-contents-btn"><i class="fas fa-times"></i></button>
<div class="horizontal-spacer"></div>
<button class="pf-c-button pf-m-primary" id="nav-continue-edit-contents-btn"><i class="fas fa-save"></i></button>
</div>
</div>
<div class="horizontal-spacer"></div>
<div class="nav-info-column" id="nav-info-column">
<div id="nav-show-properties">

View File

@ -160,8 +160,23 @@ class NavFile extends NavEntry {
super(path, stat, nav_window_ref);
this.nav_type = "file";
this.dom_element.nav_item_icon.classList.add("fas", "fa-file");
this.double_click = false;
}
handleEvent(e) {
switch(e.type){
case "click":
if(this.double_click)
this.show_edit_file_contents();
else{ // single click
this.double_click = true;
if(this.timeout)
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.double_click = false;
}, 500);
}
break;
}
super.handleEvent(e);
}
async rm() {
@ -174,6 +189,32 @@ class NavFile extends NavEntry {
});
await proc;
}
async show_edit_file_contents() {
var proc_output = await cockpit.spawn(["file", "--mime-type", this.path_str()], {superuser: "try"});
var fields = proc_output.split(':');
var type = fields[1].trim();
if(!(type.match(/^text/) || type.match(/^inode\/x-empty$/) || this.stat["size"] === 0)){
if(!window.confirm("File is of type `" + type + "`. Are you sure you want to edit it?"))
return;
}
var contents = await cockpit.spawn(["cat", this.path_str()], {superuser: "try"});
document.getElementById("nav-edit-contents-textarea").value = contents;
document.getElementById("nav-cancel-edit-contents-btn").onclick = this.hide_edit_file_contents.bind(this);
document.getElementById("nav-continue-edit-contents-btn").onclick = this.write_to_file.bind(this);
document.getElementById("nav-edit-contents-header").innerText = "Editing " + this.path_str();
document.getElementById("nav-contents-view").style.display = "none";
document.getElementById("nav-edit-contents-view").style.display = "flex";
}
async write_to_file() {
var new_contents = document.getElementById("nav-edit-contents-textarea").value;
await cockpit.script("echo -n \"$1\" > $2", [new_contents, this.path_str()], {superuser: "try"});
this.nav_window_ref.refresh();
this.hide_edit_file_contents();
}
hide_edit_file_contents() {
document.getElementById("nav-edit-contents-view").style.display = "none";
document.getElementById("nav-contents-view").style.display = "flex";
}
}
class NavDir extends NavEntry {
@ -309,7 +350,8 @@ class NavWindow {
this.refresh();
}
up() {
this.cd(new NavDir(this.pwd().parent_dir()));
if(this.pwd().path_str() !== '/')
this.cd(new NavDir(this.pwd().parent_dir()));
}
show_selected_properties() {
this.selected_entry.show_properties();