From 842635cee643fce14714f165093a24e0cb7467fb Mon Sep 17 00:00:00 2001 From: Eugenio Parodi Date: Tue, 7 May 2024 11:28:33 +0100 Subject: [PATCH] Sandbox Updated --- sandbox/js/ttkproxy.js | 128 ++++++++++++++++++-- sandbox/modules/binaryRepo | 2 +- sandbox/modules/rubbish | 2 +- sandbox/sandbox.NerdFont.html | 8 +- sandbox/sandbox.html | 8 +- sandbox/standalone.fullscreen.NerdFont.html | 8 +- 6 files changed, 129 insertions(+), 27 deletions(-) diff --git a/sandbox/js/ttkproxy.js b/sandbox/js/ttkproxy.js index 04d4151b..fa9fd325 100644 --- a/sandbox/js/ttkproxy.js +++ b/sandbox/js/ttkproxy.js @@ -22,19 +22,95 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +async function permissionsCheck() { + if (navigator.clipboard){ + const read = await navigator.permissions.query({ + name: 'clipboard-read', + }); + const write = await navigator.permissions.query({ + name: 'clipboard-write', + }); + return write.state === 'granted' && read.state !== 'denied'; + } + return false +} + // Declaration class TTkProxy { constructor(term) { this.term = term + document.getElementById("file-input").addEventListener("change", (e) => { + e.preventDefault(); + const file = e.target.files[0] + let reader = new FileReader() + reader.onload = (event) => { + let data = { + 'type':file.type, + 'name':file.name, + 'size':file.size, + 'data':event.target.result} + this.ttk_fileOpen(data) + } + if (file.type.startsWith('application/json') || + file.type.startsWith('text')){ + reader.readAsText(file); + } + if (file.type.startsWith('image')){ + //reader.readAsBinaryString(file); + reader.readAsArrayBuffer(file); + } + }) + document.getElementById("terminal").addEventListener("dragover", (e) => { + e.preventDefault(); //preventing from default behaviour + //dropArea.classList.add("active"); + //dragFile.textContent = "Release to Upload File"; + }) + document.getElementById("terminal").addEventListener("dragleave", () => { + //dropArea.classList.remove("active"); + // dragFile.textContent = "Drag files here to upload"; + }) + document.getElementById("terminal").addEventListener("drop", (e) => { + e.preventDefault(); + const target = e.dataTransfer; + const file = target.files[0] + let reader = new FileReader() + reader.onload = (event) => { + let data = { + 'type':file.type, + 'name':file.name, + 'size':file.size, + 'data':event.target.result} + this.ttk_dragOpen(data) + } + if (file.type.startsWith('application/json') || + file.type.startsWith('text')){ + reader.readAsText(file); + } + if (file.type.startsWith('image')){ + //reader.readAsBinaryString(file); + reader.readAsArrayBuffer(file); + } + }) } pyodideProxy = { - copy: function(text){ + openFile: function(encoding){ + let input = document.getElementById("file-input") + input.accept = encoding + input.click(); + }, + saveFile: function(name, content, encoding){ + const blob = new Blob([content], {type: encoding}); + saveAs(blob, name); + }, + copy: async function(text){ console.log("Copying:",text) - if(navigator.clipboard){ + const permission = await permissionsCheck() + if(permission){ navigator.clipboard.writeText(text) return //codes below wont be executed } + const active = document.activeElement const textArea = document.createElement("textarea") textArea.value = text @@ -46,13 +122,23 @@ class TTkProxy { document.execCommand('copy') document.body.removeChild(textArea) + active.focus() }, paste: function(){ - if(navigator.clipboard){ - text = navigator.clipboard.readText() - console.log("Pasted:",text) - return text + /* + const permission = await permissionsCheck() + if(permission){ + try { + // let text = null + let text = await navigator.clipboard.readText().then((txt) => txt) + // const text = navigator.clipboard.readText() + console.log('Pasted content: ', text) + return text + } catch (err) { + console.error('Failed to read clipboard contents: ', err); + } } + */ return null }, consoleLog: function(m){ @@ -98,6 +184,10 @@ class TTkProxy { this.pyodide.unpackArchive(zipBinary, ".tar.gz"); } + async loadPackage(pkg) { + await this.pyodide.loadPackage(pkg); + } + async loadFile(fileUri,file){ this.pyodide.FS.writeFile(this.pyodide.FS.currentPath+'/'+file, await (await fetch(fileUri)).text()); } @@ -135,6 +225,16 @@ class TTkProxy { from TermTk.TTkCore.TTkTerm.input import TTkInput import pyodideProxy + def ttk_dragOpen(data): + data = data.to_py() + ttk.ttkEmitDragOpen(data['type'],data) + # ttk_log(f"{type(data.to_py())=}, {str(data.to_py())}") + + def ttk_fileOpen(data): + data = data.to_py() + ttk.ttkEmitFileOpen(data['type'],data) + # ttk_log(f"{type(data.to_py())=}, {str(data.to_py())}") + def ttk_input(val): kevt,mevt,paste = TTkInput.key_process(val) if kevt or mevt: @@ -155,7 +255,7 @@ class TTkProxy { def ttk_log(val): # hex = [f"0x{ord(x):02x}" for x in val] ttk.TTkLog.debug("---> "+val.replace("\\033","") + " - ") - ttk.TTkHelper.paintAll() + # ttk.TTkHelper.paintAll() def ttk_clean(): if ttk.TTkHelper._rootWidget: @@ -187,12 +287,14 @@ class TTkProxy { `,{ globals: this.namespace } ); - this.ttk_log = this.namespace.get("ttk_log"); - this.ttk_input = this.namespace.get("ttk_input"); - this.ttk_timer = this.namespace.get("ttk_timer"); - this.ttk_resize = this.namespace.get("ttk_resize"); - this.ttk_clean = this.namespace.get("ttk_clean"); - this.ttk_init = this.namespace.get("ttk_init"); + this.ttk_log = this.namespace.get("ttk_log"); + this.ttk_input = this.namespace.get("ttk_input"); + this.ttk_timer = this.namespace.get("ttk_timer"); + this.ttk_resize = this.namespace.get("ttk_resize"); + this.ttk_clean = this.namespace.get("ttk_clean"); + this.ttk_init = this.namespace.get("ttk_init"); + this.ttk_dragOpen = this.namespace.get("ttk_dragOpen"); + this.ttk_fileOpen = this.namespace.get("ttk_fileOpen"); this.pyodideProxy.ttk_timer = this.ttk_timer this.pyodideProxy.term = this.term diff --git a/sandbox/modules/binaryRepo b/sandbox/modules/binaryRepo index 994e8efe..46ae5c54 160000 --- a/sandbox/modules/binaryRepo +++ b/sandbox/modules/binaryRepo @@ -1 +1 @@ -Subproject commit 994e8efe41f7b2080cb87eb6f38aee13321d18a1 +Subproject commit 46ae5c54e8145227bf83042967075cf7e7f4308e diff --git a/sandbox/modules/rubbish b/sandbox/modules/rubbish index d03b426b..19f7b6a9 160000 --- a/sandbox/modules/rubbish +++ b/sandbox/modules/rubbish @@ -1 +1 @@ -Subproject commit d03b426b512faa604eeb19bb059f01b2b5645390 +Subproject commit 19f7b6a9911f503d2899bd7cf42339d733ab9ffa diff --git a/sandbox/sandbox.NerdFont.html b/sandbox/sandbox.NerdFont.html index d0740d6a..bdac0341 100644 --- a/sandbox/sandbox.NerdFont.html +++ b/sandbox/sandbox.NerdFont.html @@ -29,9 +29,9 @@ /* @font-face { font-family: "NerdFont"; - src: url(www/nerdfonts/HurmitNerdFontMono-Regular.otf) format("opentype"); - src: url(www/nerdfonts/DejaVuSansMNerdFont-Regular.ttf) format("truetype"); - src: url(www/nerdfonts/DejaVuSansMNerdFont-Regular.ttf) format("truetype"); + src: url(www/fonts/nerdfonts/HurmitNerdFontMono-Regular.otf) format("opentype"); + src: url(www/fonts/nerdfonts/DejaVuSansMNerdFont-Regular.ttf) format("truetype"); + src: url(www/fonts/nerdfonts/DejaVuSansMNerdFont-Regular.ttf) format("truetype"); } */ @@ -84,7 +84,7 @@