Updated method signature; added sleep method; moved to use backend loaded js files on LSP

This commit is contained in:
itdominator 2024-03-24 17:48:27 -05:00
parent be5e159fd4
commit 32a79032cc
5 changed files with 84 additions and 34 deletions

View File

@ -46,26 +46,28 @@ 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 importScriptFromNetwork = async (url) => {
const text = await fetchScript(url);
// Create a Blob with the text content and MIME type "text/javascript".
@ -73,16 +75,38 @@ async function importScriptFromNetwork(url) {
// 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");

View File

@ -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;
@ -93,4 +109,3 @@ document.addEventListener("keydown", (eve) => {
break
}
});

View File

@ -17,10 +17,7 @@ const editorOpts = {
mergeUndoDeltas: false
}
let PythonMode = null;
//let PythonMode = ace.require("ace/mode/python").Mode;
const scriptBlobURLs = {};
let editor = null;
let previewEditor = null;

View File

@ -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,13 +92,18 @@ 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"
]
}
});
}()
@ -102,8 +112,10 @@ const setSession = (ftype, fhash, session) => {
let worker = new Worker(createBlobURL(createScriptBlob(workerString)));
let provider = LanguageProvider.create(worker);
provider.registerEditor(editor);
}
);
}
}

View File

@ -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);