From 32a79032cc284e9b34ed6b729470f003db5205e6 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sun, 24 Mar 2024 17:48:27 -0500 Subject: [PATCH] Updated method signature; added sleep method; moved to use backend loaded js files on LSP --- .../context_path/resources/js/newton/ajax.js | 46 ++++++++++++++----- .../resources/js/newton/events.js | 19 +++++++- .../resources/js/newton/globals.js | 23 ++++------ .../resources/js/newton/ui-logic.js | 28 +++++++---- .../context_path/resources/js/newton/utils.js | 2 + 5 files changed, 84 insertions(+), 34 deletions(-) diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/ajax.js b/user_config/usr/share/newton/context_path/resources/js/newton/ajax.js index e3c2df3..65653ef 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/ajax.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/ajax.js @@ -46,43 +46,67 @@ const fetchData = async (url) => { /* ----------------------------------- Ace LSP -------------------------------*/ -async function fetchScript(url) { +const fetchScript = async (url) => { const response = await fetch(url); + if (!response.ok) { throw new Error(`Error fetching script: ${response.statusText}`); } + const scriptContent = await response.text(); return scriptContent; } -function createScriptBlob(scriptContent) { +const createScriptBlob = (scriptContent) => { const scriptBlob = new Blob([scriptContent], { type: 'application/javascript' }); return scriptBlob; } -function createBlobURL(scriptBlob) { +const createBlobURL = (scriptBlob) => { const blobURL = URL.createObjectURL(scriptBlob); return blobURL; } -async function importScriptFromNetwork(url) { - const text= await fetchScript(url); +const importScriptFromNetwork = async (url) => { + const text = await fetchScript(url); // Create a Blob with the text content and MIME type "text/javascript". const blob = createScriptBlob(text); // Create an object URL from the Blob. return createBlobURL(blob); + } -async function importJavaScriptFile(url) { - const text= await fetchScript(url); - +const importScriptFromScriptStr = async (scriptStr) => { // Create a Blob with the text content and MIME type "text/javascript". - const blob = createScriptBlob(text); + const blob = createScriptBlob(scriptStr); // Create an object URL from the Blob. - const objectURL = createBlobURL(blob); + return createBlobURL(blob); +} + +const importScriptFromBackendResponse = async (scriptName, dataStr) => { + backendResponse = atob(dataStr); + scriptBlobURLs[scriptName] = await importScriptFromScriptStr(backendResponse); +} +const importJavaScriptFileFromBlobURL = async (objectURL) => { + // Create a new script element and set its src attribute to the object URL. + const scriptElement = document.createElement("script"); + scriptElement.src = objectURL; + + // Add a listener to revoke the object URL when the script has loaded. + // scriptElement.addEventListener("load", () => { + // URL.revokeObjectURL(objectURL); + // }); + + // Append the script element to the document to execute the JavaScript code. + document.body.appendChild(scriptElement); +} + + +const importJavaScriptFile = async (url) => { + const objectURL = importScriptFromNetwork(url); // Create a new script element and set its src attribute to the object URL. const scriptElement = document.createElement("script"); @@ -95,4 +119,4 @@ async function importJavaScriptFile(url) { // Append the script element to the document to execute the JavaScript code. document.body.appendChild(scriptElement); -} +} \ No newline at end of file diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/events.js b/user_config/usr/share/newton/context_path/resources/js/newton/events.js index b7e3a48..e2de7bc 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/events.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/events.js @@ -1,12 +1,28 @@ window.onload = (eve) => { console.log("Window Loaded..."); + console.log("Loading LSP client files..."); + loadLSPClientJSFiles(); + + console.log("Loading editor..."); loadEditor(); loadPreviewEditor(); loadInitialSession(); loadStartingFiles(); } + +const loadLSPClientJSFiles = () => { + const baseLink = `${window.location.href}resources/js/libs/ace_editor/lsp`; + + sendMessage(topic = "load_javascript", ftype = "", fhash = "", fpath = `${baseLink}/ace-linters.js`, content = ""); + sendMessage(topic = "load_javascript", ftype = "", fhash = "", fpath = `${baseLink}/service-manager.js`, content = ""); + sendMessage(topic = "load_javascript", ftype = "", fhash = "", fpath = `${baseLink}/language-client.js`, content = ""); + + // Note: If using builtin services tghan connecting to a socket. + // sendMessage(topic = "load_javascript", ftype = "", fhash = "", fpath = `${baseLink}/python-service.js`, content = ""); +} + window.onerror = function(msg, url, line, col, error) { // Note that col & error are new to the HTML 5 spec and may not be supported in every browser. const suppressErrorAlert = false; @@ -92,5 +108,4 @@ document.addEventListener("keydown", (eve) => { default: break } -}); - +}); \ No newline at end of file diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/globals.js b/user_config/usr/share/newton/context_path/resources/js/newton/globals.js index 64ca63d..f4c6e4d 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/globals.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/globals.js @@ -17,17 +17,14 @@ const editorOpts = { mergeUndoDeltas: false } +const scriptBlobURLs = {}; -let PythonMode = null; -//let PythonMode = ace.require("ace/mode/python").Mode; - - -let editor = null; -let previewEditor = null; -let aceSessions = {}; -let currentSession = null; -let previewSel = null; -let fontSize = 12; -let highlightLine = true; -let isControlDown = false; -let queryMarkers = []; \ No newline at end of file +let editor = null; +let previewEditor = null; +let aceSessions = {}; +let currentSession = null; +let previewSel = null; +let fontSize = 12; +let highlightLine = true; +let isControlDown = false; +let queryMarkers = []; \ No newline at end of file diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js b/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js index e36b9db..5d0393e 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/ui-logic.js @@ -57,7 +57,8 @@ const switchSession = (fhash) => { setSession(ftype, fhash, session); } -const setSession = (ftype, fhash, session) => { +// const setSession = (ftype, fhash, session) => { +const setSession = async (ftype, fhash, session) => { currentSession = fhash; editor.setSession(session); @@ -65,19 +66,23 @@ const setSession = (ftype, fhash, session) => { editor.session.setMode("ace/mode/" + ftype); if (ftype === "python") { - const baseLink = "http://0.0.0.0:4880"; + // const baseLink = "http://0.0.0.0:4880"; + //importJavaScriptFile(baseLink + "/ace-linters.js").then( + // importScripts("${await importScriptFromNetwork(baseLink + "/service-manager.js")}"); + // importScripts("${await importScriptFromNetwork(baseLink + "/python-service.js")}"); + // importScripts("${await importScriptFromNetwork(baseLink + "/language-client.js")}"); - importJavaScriptFile(baseLink + "/ace-linters.js").then( + importJavaScriptFileFromBlobURL( scriptBlobURLs["ace-linters.js"] ).then( async () => { let workerString = ` !function () { - importScripts("${await importScriptFromNetwork(baseLink + "/service-manager.js")}"); + importScripts( "${ scriptBlobURLs["service-manager.js"] }" ); let manager = new ServiceManager(self); /* Works but isn't websocket */ // manager.registerService("python", { // module: () => { - // importScripts("${await importScriptFromNetwork(baseLink + "/python-service.js")}"); + // importScripts( "${ scriptBlobURLs["python-service.js"] }" ); // return {PythonService}; // }, // className: "PythonService", @@ -87,23 +92,30 @@ const setSession = (ftype, fhash, session) => { /* Works and is websocket */ manager.registerServer("python", { module: () => { - importScripts("${await importScriptFromNetwork(baseLink + "/language-client.js")}"); + importScripts( "${ scriptBlobURLs["language-client.js"] }" ); return {LanguageClient}; }, modes: "python|python3", type: "socket", // "socket|worker" socket: new WebSocket("ws://127.0.0.1:3030/python"), - initializationOptions: {} + initializationOptions: { + "pylsp.plugins.jedi.extra_paths": [ + "/home/abaddon/Portable_Apps/py-venvs/flask-apps-venv/venv/lib/python3.10/site-packages", + "/home/abaddon/Portable_Apps/py-venvs/gtk-apps-venv/venv/lib/python3.10/site-packages/gi" + ] + } }); }() `; - + let worker = new Worker(createBlobURL(createScriptBlob(workerString))); let provider = LanguageProvider.create(worker); provider.registerEditor(editor); + } ); + } } diff --git a/user_config/usr/share/newton/context_path/resources/js/newton/utils.js b/user_config/usr/share/newton/context_path/resources/js/newton/utils.js index e6d361b..11232b0 100644 --- a/user_config/usr/share/newton/context_path/resources/js/newton/utils.js +++ b/user_config/usr/share/newton/context_path/resources/js/newton/utils.js @@ -45,6 +45,8 @@ const sendMessage = (topic = null, ftype = "", fhash = "", fpath = "", content = messenger.backend.postMessage( JSON.stringify(messageBody) ); } +const sleep = ms => new Promise(r => setTimeout(r, ms)); + const getSHA256Hash = async (input) => { let textAsBuffer = new TextEncoder().encode(input); let hashBuffer = await window.crypto.subtle.digest("SHA-256", textAsBuffer);