Skip to content

Commit 0d12f26

Browse files
chore: improve plugin
1) Support kotlin target platform 2) Support header before the snippet (option) 3) Support text description before the snippet (option)
1 parent d7ae74f commit 0d12f26

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

app.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
1+
const CONFIGURATION_PROPS = {
2+
PLATFORM: 'platform',
3+
HEAD: 'head',
4+
TASK: 'task',
5+
CODE: 'code'
6+
};
7+
8+
const SELECTORS = {
9+
HEAD: 'head',
10+
TASK: 'task',
11+
CODE: 'code'
12+
};
13+
14+
const KOTLIN_PLAYGROUND_ATTRIBUTES = {
15+
PLATFORM: 'data-target-platform',
16+
THEME: 'theme'
17+
};
18+
19+
const DEFAULT_THEME = 'idea';
20+
121
function initiateApp(configuration) {
2-
let node = document.getElementById('code');
3-
node.textContent = configuration["code"];
4-
KotlinPlayground('code');
22+
23+
if (configuration[CONFIGURATION_PROPS.HEAD]) buildHeader(configuration[CONFIGURATION_PROPS.HEAD]);
24+
if (configuration[CONFIGURATION_PROPS.TASK]) buildTask(configuration[CONFIGURATION_PROPS.TASK]);
25+
26+
const platform = configuration[CONFIGURATION_PROPS.PLATFORM];
27+
const code = configuration[CONFIGURATION_PROPS.CODE];
28+
if (platform && code) buildPlayground(code, platform);
29+
30+
KotlinPlayground(SELECTORS.CODE);
31+
}
32+
33+
function buildHeader(name) {
34+
return buildNode(name, SELECTORS.HEAD)
35+
}
36+
37+
function buildTask(text) {
38+
return buildNode(text, SELECTORS.TASK)
39+
}
40+
41+
function buildPlayground(code, platform) {
42+
const node = document.createElement(SELECTORS.CODE);
43+
node.setAttribute(KOTLIN_PLAYGROUND_ATTRIBUTES.THEME, DEFAULT_THEME);
44+
node.setAttribute(KOTLIN_PLAYGROUND_ATTRIBUTES.PLATFORM, platform);
45+
node.textContent = code;
46+
document.body.appendChild(node);
47+
}
48+
49+
function buildNode(text, selector) {
50+
const node = document.createElement('div');
51+
node.className = selector;
52+
node.textContent = text;
53+
document.body.appendChild(node);
554
}
655

756
courseraApi.callMethod({

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<head>
44
<meta charset="utf-8">
55
<link rel="stylesheet" href="https://www.coursera.org/widget/coursera-connect.css">
6+
<link rel="stylesheet" href="./style.css">
67
</head>
7-
<body>
8-
<code id="code" theme="idea"></code>
8+
<body class="playground">
99
<script src="https://www.coursera.org/widget/coursera-widget-connect-v0.js"></script>
1010
<script src="https://unpkg.com/kotlin-playground@1"></script>
1111
<script src="./app.js"></script>

manifest.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
{
1313
"name": "Hello world!",
1414
"configuration": {
15-
"code": "fun main(args: Array<String>) {\n println(\"Hello world\")\n}"
15+
"code": "fun main(args: Array<String>) {\n println(\"Hello world\")\n}",
16+
"platform": "java",
17+
"task": "We declare a package-level function main which returns Unit and takes\nan Array of strings as a parameter. Note that semicolons are optional.",
18+
"head": "Hello World!"
1619
}
1720
}
1821
],

style.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
code {
2+
display: none;
3+
}
4+
5+
.playground {
6+
margin: 10px;
7+
font-family: 'OpenSans-Light', Arial, sans-serif;
8+
}
9+
10+
.head {
11+
text-align: center;
12+
color: #27282c;
13+
font-size: 2.125rem;
14+
line-height: 2.625rem;
15+
font-weight: normal;
16+
margin-bottom: 20px;
17+
}
18+
19+
.task {
20+
letter-spacing: normal;
21+
white-space: pre-wrap;
22+
margin-left: 20px;
23+
color: #757575;
24+
font-size: 14px;
25+
line-height: 21px;
26+
margin-bottom: 15px;
27+
}

0 commit comments

Comments
 (0)