Completed tab CRUD flow

This commit is contained in:
itdominator 2024-01-21 23:45:29 -06:00
parent 386628a949
commit 4e06bc3334
7 changed files with 60 additions and 59 deletions

View File

@ -6,6 +6,7 @@ import base64
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
# Application imports
@ -52,35 +53,38 @@ class FilesController:
def handle_file_event(self, event):
match event.topic:
case "save":
content = base64.b64decode( event.content.encode() ).decode("utf-8")
basename = self.save_session(event.target, content)
content = base64.b64decode( event.content.encode() ).decode("utf-8")
ftype, fname, fpath = self.save_session(event.ftype, event.fpath, content)
if basename:
event_system.emit(f"updated_tab_{event.originator}", (event.target, basename,))
if ftype and fname and fpath:
event_system.emit(f"update_tab_{event.originator}", (event.fhash, fname,))
event_system.emit(f"updated_session_{event.originator}", (event.fhash, ftype, fname, fpath))
case "close":
event_system.emit(f"close_tab_{event.originator}", (event.target))
event_system.emit(f"close_tab_{event.originator}", (event.fhash))
case "load_buffer":
self.load_buffer(event.target)
event_system.emit(f"add_tab_{event.originator}", (event.target, "buffer",))
self.load_buffer(event.fhash)
event_system.emit(f"add_tab_{event.originator}", (event.fhash, "buffer",))
case "load_file":
content = base64.b64decode( event.content.encode() ).decode("utf-8")
event_system.emit(f"add_tab_with_name_{event.originator}", (event.target, content,))
event_system.emit(f"add_tab_with_name_{event.originator}", (event.fhash, content,))
case _:
return
def load_buffer(self, fhash):
self.opened_files[fhash] = {"file": None, "ftype": "buffer"}
def save_session(self, fhash, content):
ftype = self.opened_files[fhash]["ftype"]
def save_session(self, ftype, fpath, content):
gfile = event_system.emit_and_await(
"save_file_dialog", ("", None)
) if fhash == "buffer" else self.opened_files[fhash]["file"]
) if ftype == "buffer" else Gio.File.new_for_path(fpath)
file_written = self.write_to_file(fhash, gfile, content)
if fhash == "buffer" and file_written:
self.update_session(fhash, gfile)
return gfile.get_basename()
file_written = self.write_to_file(gfile, content)
if ftype == "buffer" and file_written:
info = gfile.query_info("standard::*", 0, cancellable = None)
ftype = info.get_content_type().replace("x-", "").split("/")[1]
return ftype, gfile.get_basename(), gfile.get_path()
def update_session(self, fhash, gfile):
info = gfile.query_info("standard::*", 0, cancellable = None)
@ -90,7 +94,7 @@ class FilesController:
return ftype, fhash
def write_to_file(self, fhash, gfile, content):
def write_to_file(self, gfile, content):
with open(gfile.get_path(), 'w') as outfile:
try:
outfile.write(content)

View File

@ -46,6 +46,7 @@ class AceEditor(WebKit2.WebView):
event_system.subscribe(f"load_file_{self.INDEX}", self.load_file)
event_system.subscribe(f"new_session_{self.INDEX}", self.new_session)
event_system.subscribe(f"switch_session_{self.INDEX}", self.switch_session)
event_system.subscribe(f"updated_session_{self.INDEX}", self.updated_session)
event_system.subscribe(f"close_session_{self.INDEX}", self.close_session)
event_system.subscribe(f"remove_session_{self.INDEX}", self.remove_session)
event_system.subscribe(f"ui_message_{self.INDEX}", self.ui_message)
@ -91,6 +92,10 @@ class AceEditor(WebKit2.WebView):
command = f"switchSession('{fhash}')"
self.run_javascript(command, None, None)
def updated_session(self, fhash, ftype, fname, fpath):
command = f"updateSession('{fhash}', '{ftype}', '{fname}', '{fpath}')"
self.run_javascript(command, None, None)
def close_session(self, fhash):
command = f"closeSession('{fhash}')"
self.run_javascript(command, None, None)

View File

@ -96,17 +96,13 @@ class TabBar(Gtk.Notebook):
self.set_current_page(page_num)
def update_tab(self, fhash, title = "[BAD TITLE]"):
# container = Gtk.EventBox()
# header = TabHeaderWidget(container, self._close_tab)
# page_num = self.append_page(container, header)
for child in self.get_children():
if child.fhash == fhash:
target = child
break
# header.label.set_label(title)
# self.set_tab_detachable(container, True)
# self.set_tab_reorderable(container, True)
# self.show_all()
# self.set_current_page(page_num)
...
label = self.get_tab_label(target).get_children()[0]
label.set_label(title)
def close_tab(self, fhash):
target = None

View File

@ -11,6 +11,8 @@ import gi
@dataclass
class Event:
topic: str
target: str
ftype: str
fhash: str
fpath: str
content: str
originator: int = -1

View File

@ -21,7 +21,7 @@ const editorCommands = [
name: "saveSession",
bindKey: {win: "ctrl-s", mac: "ctrl-s"},
exec: function(editor) {
saveSession();
saveSession(currentSession);
},
readOnly: true
}, {

View File

@ -33,12 +33,13 @@ const loadInitialSessionTab = () => {
const newSession = (eve = null, session = null) => {
let ftype = "buffer";
let fhash = Date.now().toString();
let fpath = ""
session = ( isNotNullOrUndefined(session) ) ? session : ace.createEditSession("");
aceSessions[fhash] = {"ftype": ftype, "fname": "", "path": "", "session": session};
aceSessions[fhash] = {"ftype": ftype, "fname": "", "fpath": fpath, "session": session};
setSession(ftype, fhash, session);
sendMessage("load_buffer", fhash, "");
sendMessage("load_buffer", ftype, fhash, fpath, "");
}
const switchSession = (fhash) => {
@ -57,9 +58,18 @@ const setSession = (ftype, fhash, session) => {
}
}
const updateSession = (fhash, ftype, fname, fpath) => {
aceSessions[fhash]["ftype"] = ftype;
aceSessions[fhash]["fname"] = fname;
aceSessions[fhash]["fpath"] = fpath;
}
const closeSession = (fhash) => {
let ftype = aceSessions["ftype"];
let fpath = aceSessions["fpath"];
delete aceSessions[fhash];
sendMessage("close", fhash, "");
sendMessage("close", ftype, fhash, fpath, "");
}
const removeSession = (fhash) => {
@ -69,35 +79,17 @@ const removeSession = (fhash) => {
const loadFile = (ftype, fname, fpath, content) => {
let fhash = Date.now().toString();
session = ace.createEditSession( atob(content) );
aceSessions[fhash] = {"ftype": ftype, "fname": fname, "path": fpath, "session": session};
aceSessions[fhash] = {"ftype": ftype, "fname": fname, "fpath": fpath, "session": session};
setSession(ftype, fhash, session);
sendMessage("load_file", fhash, fname);
sendMessage("load_file", ftype, fhash, fpath, fname);
}
const saveSession = (fhash) => {
let ftype = aceSessions[fhash]["ftype"];
let fpath = aceSessions[fhash]["fpath"];
let session = aceSessions[fhash]["session"];
let content = session.getValue();
const updatedTab = (ftype, fname) => {
// let elm = document.querySelectorAll(`[fhash="${currentSession}"]`)[0];
// let tabTitleElm = elm.children[0];
aceSessions[currentSession]["ftype"] = ftype;
aceSessions[currentSession]["file"] = fname;
// elm.setAttribute("ftype", ftype);
// tabTitleElm.textContent = fname;
sendMessage("save", ftype, fhash, fpath, content);
}
const saveSession = () => {
// let fhash = currentSession;
// let session = aceSessions[fhash]["session"];
// let data = session.getValue();
// sendMessage("save", fhash, data);
console.log("");
}

View File

@ -27,10 +27,12 @@ const displayMessage = (message, type, timeout, msgWindow = "page-alert-zone") =
}
}
const sendMessage = (topic, target, content) => {
const sendMessage = (topic, ftype, fhash, fpath, content) => {
const messageBody = {
"topic": topic,
'target': target,
"ftype": ftype,
"fhash": fhash,
"fpath": fpath,
"content": btoa(content)
};