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,43 +46,67 @@ const fetchData = async (url) => {
/* ----------------------------------- Ace LSP -------------------------------*/ /* ----------------------------------- Ace LSP -------------------------------*/
async function fetchScript(url) { const fetchScript = async (url) => {
const response = await fetch(url); const response = await fetch(url);
if (!response.ok) { if (!response.ok) {
throw new Error(`Error fetching script: ${response.statusText}`); throw new Error(`Error fetching script: ${response.statusText}`);
} }
const scriptContent = await response.text(); const scriptContent = await response.text();
return scriptContent; return scriptContent;
} }
function createScriptBlob(scriptContent) { const createScriptBlob = (scriptContent) => {
const scriptBlob = new Blob([scriptContent], { type: 'application/javascript' }); const scriptBlob = new Blob([scriptContent], { type: 'application/javascript' });
return scriptBlob; return scriptBlob;
} }
function createBlobURL(scriptBlob) { const createBlobURL = (scriptBlob) => {
const blobURL = URL.createObjectURL(scriptBlob); const blobURL = URL.createObjectURL(scriptBlob);
return blobURL; return blobURL;
} }
async function importScriptFromNetwork(url) { const importScriptFromNetwork = async (url) => {
const text= await fetchScript(url); const text = await fetchScript(url);
// Create a Blob with the text content and MIME type "text/javascript". // Create a Blob with the text content and MIME type "text/javascript".
const blob = createScriptBlob(text); const blob = createScriptBlob(text);
// Create an object URL from the Blob. // Create an object URL from the Blob.
return createBlobURL(blob); return createBlobURL(blob);
} }
async function importJavaScriptFile(url) { const importScriptFromScriptStr = async (scriptStr) => {
const text= await fetchScript(url);
// Create a Blob with the text content and MIME type "text/javascript". // 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. // 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. // Create a new script element and set its src attribute to the object URL.
const scriptElement = document.createElement("script"); 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. // Append the script element to the document to execute the JavaScript code.
document.body.appendChild(scriptElement); document.body.appendChild(scriptElement);
} }

View File

@ -1,12 +1,28 @@
window.onload = (eve) => { window.onload = (eve) => {
console.log("Window Loaded..."); console.log("Window Loaded...");
console.log("Loading LSP client files...");
loadLSPClientJSFiles();
console.log("Loading editor...");
loadEditor(); loadEditor();
loadPreviewEditor(); loadPreviewEditor();
loadInitialSession(); loadInitialSession();
loadStartingFiles(); 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) { 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. // Note that col & error are new to the HTML 5 spec and may not be supported in every browser.
const suppressErrorAlert = false; const suppressErrorAlert = false;
@ -92,5 +108,4 @@ document.addEventListener("keydown", (eve) => {
default: default:
break break
} }
}); });

View File

@ -17,17 +17,14 @@ const editorOpts = {
mergeUndoDeltas: false mergeUndoDeltas: false
} }
const scriptBlobURLs = {};
let PythonMode = null; let editor = null;
//let PythonMode = ace.require("ace/mode/python").Mode; let previewEditor = null;
let aceSessions = {};
let currentSession = null;
let editor = null; let previewSel = null;
let previewEditor = null; let fontSize = 12;
let aceSessions = {}; let highlightLine = true;
let currentSession = null; let isControlDown = false;
let previewSel = null; let queryMarkers = [];
let fontSize = 12;
let highlightLine = true;
let isControlDown = false;
let queryMarkers = [];

View File

@ -57,7 +57,8 @@ const switchSession = (fhash) => {
setSession(ftype, fhash, session); setSession(ftype, fhash, session);
} }
const setSession = (ftype, fhash, session) => { // const setSession = (ftype, fhash, session) => {
const setSession = async (ftype, fhash, session) => {
currentSession = fhash; currentSession = fhash;
editor.setSession(session); editor.setSession(session);
@ -65,19 +66,23 @@ const setSession = (ftype, fhash, session) => {
editor.session.setMode("ace/mode/" + ftype); editor.session.setMode("ace/mode/" + ftype);
if (ftype === "python") { 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 () => { async () => {
let workerString = ` let workerString = `
!function () { !function () {
importScripts("${await importScriptFromNetwork(baseLink + "/service-manager.js")}"); importScripts( "${ scriptBlobURLs["service-manager.js"] }" );
let manager = new ServiceManager(self); let manager = new ServiceManager(self);
/* Works but isn't websocket */ /* Works but isn't websocket */
// manager.registerService("python", { // manager.registerService("python", {
// module: () => { // module: () => {
// importScripts("${await importScriptFromNetwork(baseLink + "/python-service.js")}"); // importScripts( "${ scriptBlobURLs["python-service.js"] }" );
// return {PythonService}; // return {PythonService};
// }, // },
// className: "PythonService", // className: "PythonService",
@ -87,23 +92,30 @@ const setSession = (ftype, fhash, session) => {
/* Works and is websocket */ /* Works and is websocket */
manager.registerServer("python", { manager.registerServer("python", {
module: () => { module: () => {
importScripts("${await importScriptFromNetwork(baseLink + "/language-client.js")}"); importScripts( "${ scriptBlobURLs["language-client.js"] }" );
return {LanguageClient}; return {LanguageClient};
}, },
modes: "python|python3", modes: "python|python3",
type: "socket", // "socket|worker" type: "socket", // "socket|worker"
socket: new WebSocket("ws://127.0.0.1:3030/python"), 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 worker = new Worker(createBlobURL(createScriptBlob(workerString)));
let provider = LanguageProvider.create(worker); let provider = LanguageProvider.create(worker);
provider.registerEditor(editor); provider.registerEditor(editor);
} }
); );
} }
} }

View File

@ -45,6 +45,8 @@ const sendMessage = (topic = null, ftype = "", fhash = "", fpath = "", content =
messenger.backend.postMessage( JSON.stringify(messageBody) ); messenger.backend.postMessage( JSON.stringify(messageBody) );
} }
const sleep = ms => new Promise(r => setTimeout(r, ms));
const getSHA256Hash = async (input) => { const getSHA256Hash = async (input) => {
let textAsBuffer = new TextEncoder().encode(input); let textAsBuffer = new TextEncoder().encode(input);
let hashBuffer = await window.crypto.subtle.digest("SHA-256", textAsBuffer); let hashBuffer = await window.crypto.subtle.digest("SHA-256", textAsBuffer);