Skip to content

Commit 577a7ef

Browse files
somehow I forgot to push everything important before, because I didn't add it in git
1 parent b63ec19 commit 577a7ef

11 files changed

Lines changed: 429 additions & 1 deletion

File tree

dev/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<h1>JavaScript</h1>
1010
<p>JavaScript code example here</p>
1111
<!-- web component examples go here -->
12-
<wc-code mode="javascript" theme="monokai">
12+
<wc-code mode="javascript" theme="blackboard">
1313
<script type="wc-content">
1414
const resp = await fetch("https://sv443.net/jokeapi/v2/joke/Any?blacklistFlags=nsfw,racist,sexist&format=txt");
1515
const text = await resp.text();

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@vanillawc/wc-codemirror';

index.min.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import"@vanillawc/wc-codemirror";

src/.wc-code.js.swp

20 KB
Binary file not shown.

src/console.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// the contains code for the output console
2+
3+
class WCCodeConsole {
4+
constructor (wccode) {
5+
this.elements = {}
6+
this.createDiv(wccode)
7+
}
8+
9+
/**
10+
* creates the console div for wc-code
11+
*
12+
* @param wccode - a wc-code instance
13+
*/
14+
createDiv (wccode) {
15+
this.elements.console = document.createElement('div')
16+
this.elements.console.classList.add('wc-code-console')
17+
this.elements.consoleList = document.createElement('ol')
18+
19+
this.elements.console.appendChild(this.elements.consoleList)
20+
wccode.appendChild(this.elements.console)
21+
}
22+
23+
/**
24+
* add an element inside a console,
25+
*
26+
* this will be put in an 'li' element befor being put inside
27+
*
28+
* @param el - the HTMLElement to be put inside
29+
*/
30+
addEl (el) {
31+
const li = document.createElement('li')
32+
li.appendChild(el)
33+
this.elements.consoleList.appendChild(li)
34+
}
35+
36+
/**
37+
* easy method to put a span element inside the console with some text inside
38+
*
39+
* @param {String} text - the text to put inside the console
40+
*/
41+
addText (text) {
42+
const span = document.createElement('span')
43+
span.innerText = text
44+
this.addEl(span)
45+
}
46+
47+
/** clear the console **/
48+
clear () {
49+
this.elements.consoleList.innerText = ''
50+
}
51+
}
52+
53+
export default WCCodeConsole

src/language-details.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const ModesBaseLoc = 'https://cdn.jsdelivr.net/gh/vanillawc/wc-codemirror@1.8.7/mode/'
2+
3+
/**
4+
* these attributes, when loaded, load the
5+
* script file for the language dynamically,
6+
* as well the code running file
7+
*/
8+
export default {
9+
metaUrl: import.meta.url,
10+
languages: {
11+
javascript: {
12+
CMLanguageLoc: ModesBaseLoc + 'javascript/javascript.js',
13+
languageFile: './languages/javascript.js'
14+
},
15+
python: {
16+
CMLanguageLoc: ModesBaseLoc + 'python/python.js',
17+
languageFile: './languages/python.js'
18+
}
19+
}
20+
}

src/languages/javascript.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint no-undef: 0 */
2+
// we're running javascript in javascript
3+
// nothing special is required
4+
WCCode.languages.javascript = {
5+
meta: { url: document.currentScript.src },
6+
fileExt: ".js",
7+
// alternate console for the environment
8+
createAlternateConsole (webcomponent) {
9+
return {
10+
/**
11+
* console.log alternative
12+
*/
13+
log (val) {
14+
webcomponent.console.addText(val)
15+
}
16+
}
17+
},
18+
19+
/**
20+
* @param webcomponent - the webcomponent
21+
*/
22+
async run (webcomponent) {
23+
const code = `return (async function(){
24+
const __webcomponent = WCCode.getElement(${webcomponent.WCCodeID})
25+
var console = WCCode
26+
.languages
27+
.javascript
28+
.createAlternateConsole(__webcomponent)
29+
${webcomponent.value}
30+
})`
31+
32+
// eslint-disable-next-line
33+
await Function(code)()()
34+
}
35+
}

src/languages/python.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint no-undef: 0 */
2+
// we're running python in javascript,
3+
// we need pyiodide
4+
'use strict'
5+
6+
window.languagePluginUrl = 'https://pyodide-cdn2.iodide.io/v0.15.0/full/'
7+
8+
WCCode.languages.python = {
9+
meta: { url: document.currentScript.src },
10+
fileExt: ".py",
11+
additionalScripts: [
12+
'https://pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.js'
13+
],
14+
async init () {
15+
await languagePluginLoader
16+
await pyodide.loadPackage(['micropip'])
17+
},
18+
/**
19+
* @param webcomponent - the webcomponent
20+
*/
21+
async run (webcomponent) {
22+
pyodide.runPythonAsync(`
23+
import js as __js
24+
25+
def print(*val):
26+
for v in val:
27+
wc = __js.WCCode.getElement(${webcomponent.WCCodeID})
28+
wc.console.addText(v)
29+
30+
${webcomponent.value}`)
31+
}
32+
}

src/utils.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const addedScripts = new Set()
2+
const addedCSSLinks = new Set()
3+
4+
/**
5+
* import a script if we need to import a script,
6+
* else don't, useful for language syntax/theme additions,
7+
* adding themes, stuff like that
8+
*/
9+
export async function addScriptIfRequired (_url, base) {
10+
const url = new URL(_url, base)
11+
if (addedScripts.has(url.href)) return
12+
const script = document.createElement('script')
13+
script.src = url.href
14+
document.body.appendChild(script)
15+
16+
await new Promise(resolve => {
17+
script.addEventListener('load', resolve)
18+
})
19+
addedScripts.add(url.href)
20+
}
21+
22+
/**
23+
* add a css link if required
24+
*/
25+
export async function addCSSLinkIfRequired (_url, base) {
26+
const url = new URL(_url, base)
27+
if (addedCSSLinks.has(url.href)) return
28+
var link = document.createElement('link')
29+
link.setAttribute('rel', 'stylesheet')
30+
link.setAttribute('type', 'text/css')
31+
link.setAttribute('href', url.href)
32+
document.body.appendChild(link)
33+
await new Promise(resolve => {
34+
link.addEventListener('load', resolve)
35+
})
36+
addedCSSLinks.add(url.href)
37+
}
38+
39+
/**
40+
* see addScriptIfRequired, this is for multiple scripts
41+
*/
42+
export async function addScriptsIfRequired (urls, base) {
43+
for (var url of urls) {
44+
await addScriptIfRequired(url, base)
45+
}
46+
}
47+
48+
/**
49+
* see addCSSLinkIfRequired, this is for multiple links
50+
*/
51+
export async function addCSSLinksIfRequired (urls, base) {
52+
for (var url of urls) {
53+
await addCSSLinkIfRequired(url, base)
54+
}
55+
}

src/wc-code.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
wc-code input[type="button"] {
2+
border: 1px solid grey;
3+
border-radius: 3px;
4+
margin: 5px 5px;
5+
padding: 4px 18px;
6+
background: white;
7+
}
8+
9+
10+
wc-code .wc-code-console{
11+
font-family: monospace;
12+
}
13+
14+
wc-code .wc-code-loading-bar{
15+
font-family: monospace;
16+
border-radius: 3px;
17+
padding: 5px 10px;
18+
color: white;
19+
}
20+
21+
wc-code .wc-code-loading-bar-loading{
22+
background : red;
23+
}
24+
25+
wc-code .wc-code-loading-bar-done{
26+
background : green;
27+
}

0 commit comments

Comments
 (0)