Skip to content

Commit 89a69fb

Browse files
authored
Improved plugin support 🔧
1 parent 7c37ed9 commit 89a69fb

File tree

1 file changed

+71
-57
lines changed

1 file changed

+71
-57
lines changed

src/code-runner-wc.js

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -207,50 +207,6 @@ class CodeRunner extends HTMLElement {
207207

208208

209209

210-
let loadedFiles = false;
211-
let create = (info) => {
212-
return new Promise(function(resolve, reject) {
213-
let gfgData = document.createElement('script');
214-
gfgData.src = info;
215-
gfgData.async = false;
216-
gfgData.onload = () => {
217-
resolve(info);
218-
};
219-
gfgData.onerror = () => {
220-
reject(info);
221-
};
222-
document.body.appendChild(gfgData);
223-
});
224-
};
225-
let gfgScript = ['https://cdn.jsdelivr.net/npm/[email protected]/ace.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/ext-modelist.js']
226-
let promiseData = [];
227-
228-
229-
// function to load Ace Editor to page
230-
231-
let isAceLoadedAlready = false
232-
async function loadAceEditor(){
233-
if(isAceLoadedAlready === false){
234-
235-
gfgScript.forEach(function(info) {
236-
promiseData.push(create(info));
237-
});
238-
const data = await Promise.all(promiseData).then(async function() {
239-
return {loaded: "true"}
240-
}).catch(function(gfgData) {
241-
isAceLoadedAlready = true
242-
return {loaded: "false"}
243-
console.log(gfgData + ' failed to load!');
244-
});
245-
246-
return data
247-
} else{
248-
// Ace Editor has already been loaded to page
249-
}
250-
}
251-
252-
253-
254210

255211
// if WC is using Piston API
256212
if(!this.hasAttribute("custom-compiler")){
@@ -292,18 +248,6 @@ See the repo if there is any plugins that support "${this.getAttribute("language
292248
} else{
293249
// Code Runner WC - is NOT using Piston API & a plugin has been loaded.
294250

295-
// make sure the editor is only set for non-terminal.
296-
if (!this.getAttribute("custom-compiler").includes("shell") && !this.getAttribute("custom-compiler").includes("terminal")){
297-
// load ace editor for custom compiler plugin
298-
async function AceEditorForPistonAPI(element){
299-
// Wait till AceEditor is loaded - so no errors occur.
300-
let isAceLoaded = await loadAceEditor()
301-
if (isAceLoaded.loaded === "true"){
302-
CreateAceCodeEditor(element, element.getAttribute("language").toLowerCase())//
303-
}
304-
}
305-
AceEditorForPistonAPI(this)
306-
}
307251
}
308252

309253

@@ -432,6 +376,56 @@ function handleclick(codeRunner){
432376
}
433377

434378

379+
380+
381+
// function to load Ace Editor to page
382+
383+
384+
let loadedFiles = false;
385+
let create = (info) => {
386+
return new Promise(function(resolve, reject) {
387+
let gfgData = document.createElement('script');
388+
gfgData.src = info;
389+
gfgData.async = false;
390+
gfgData.onload = () => {
391+
resolve(info);
392+
};
393+
gfgData.onerror = () => {
394+
reject(info);
395+
};
396+
document.body.appendChild(gfgData);
397+
});
398+
};
399+
let gfgScript = ['https://cdn.jsdelivr.net/npm/[email protected]/ace.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/ext-modelist.js']
400+
let promiseData = [];
401+
402+
403+
404+
405+
let isAceLoadedAlready = false
406+
async function loadAceEditor(){
407+
if(isAceLoadedAlready === false){
408+
409+
gfgScript.forEach(function(info) {
410+
promiseData.push(create(info));
411+
});
412+
const data = await Promise.all(promiseData).then(async function() {
413+
return {loaded: "true"}
414+
}).catch(function(gfgData) {
415+
isAceLoadedAlready = true
416+
return {loaded: "false"}
417+
console.log(gfgData + ' failed to load!');
418+
});
419+
420+
return data
421+
} else{
422+
// Ace Editor has already been loaded to page
423+
}
424+
}
425+
426+
427+
428+
435429
/// function to create Ace Editors for CodeRunner-WC
436430

437431
function CreateAceCodeEditor(html_element, language){
@@ -447,10 +441,12 @@ var editor = ace.edit(html_element.querySelector("#codetorun"));
447441
editor.$blockScrolling = Infinity;
448442

449443
editor.setTheme("ace/theme/monokai");
444+
if (language){
450445
SetAceEditor_Mode()
446+
}
451447
function SetAceEditor_Mode(){
452448

453-
var modelist = ace.require('ace/ext/modelist');
449+
let modelist = ace.require('ace/ext/modelist');
454450
if(modelist.modesByName[language] != undefined) {
455451

456452
editor.getSession().setMode(`ace/mode/${language}`)
@@ -479,4 +475,22 @@ editor.setOptions({
479475
}
480476

481477

478+
async function CreateAceEditorForPlugin(element, language){
479+
480+
// make sure the editor is only set for non-terminal.
481+
if (!element.getAttribute("custom-compiler").includes("shell") && !element.getAttribute("custom-compiler").includes("terminal")){
482+
// load ace editor for custom compiler plugin
483+
// Wait till AceEditor is loaded - so no errors occur.
484+
485+
let isAceLoaded = await loadAceEditor()
486+
if (isAceLoaded.loaded === "true"){
487+
488+
CreateAceCodeEditor(element, language)//
489+
}
490+
491+
}
492+
493+
}
494+
495+
482496
window.customElements.define('code-runner', CodeRunner);

0 commit comments

Comments
 (0)