Skip to content

Commit 8e0d8f0

Browse files
committed
v1.0.3
1 parent 3626a14 commit 8e0d8f0

8 files changed

Lines changed: 157 additions & 249 deletions

File tree

dist/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
{
22
"name": "acode-plugin-python_runner",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Acode plugin to python code",
55
"main": "python_runner.js",
66
"repository": "https://github.com/deadlyjack/acode-plugin-python.git",
77
"author": "Ajit <me@ajitkumar.dev>",
88
"license": "MIT",
99
"dependencies": {
10-
"autoprefixer": "^10.4.7",
11-
"html-tag-js": "^1.0.4",
12-
"sass-loader": "^13.0.0"
10+
"html-tag-js": "^1.0.4"
1311
},
1412
"devDependencies": {
1513
"@babel/cli": "^7.17.10",
1614
"@babel/core": "^7.18.5",
1715
"@babel/preset-env": "^7.18.2",
16+
"autoprefixer": "^10.4.7",
1817
"babel-loader": "^8.2.5",
19-
"css-loader": "^6.7.1",
20-
"mini-css-extract-plugin": "^2.6.0",
2118
"node-sass": "^7.0.1",
19+
"postcss": "^8.4.14",
2220
"postcss-loader": "^7.0.0",
2321
"raw-loader": "^4.0.2",
22+
"sass-loader": "^13.0.0",
2423
"style-loader": "^3.3.1",
2524
"webpack": "^5.73.0",
2625
"webpack-cli": "^4.10.0"

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "acode.plugin.python",
33
"name": "Python",
44
"main": "dist/main.js",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"readme": "README.md",
77
"icon": "icon.png",
88
"type": "pro",

postcss.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
plugins: [
33
require('autoprefixer')({})
4-
]
4+
],
55
};

src/main.js

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import style from './style.scss';
12
import tag from 'html-tag-js';
23

34
class Python {
@@ -8,35 +9,31 @@ class Python {
89
#onRunError;
910
#cacheFile;
1011
#cacheFileUrl;
12+
#workerInitialized = false;
1113
name = 'Python';
1214
baseUrl = '';
1315
pyodide = null;
1416
$input = null;
1517
$page = null;
1618
$runBtn = null;
19+
$style = null;
1720

1821
async init($page, cacheFile, cacheFileUrl) {
1922

2023
this.#cacheFileUrl = cacheFileUrl;
2124

2225
$page.id = 'acode-plugin-python';
23-
this.$refresh = tag('span', {
24-
className: 'icon refresh',
25-
attr: {
26-
action: 'refresh',
27-
},
28-
onclick: async () => {
29-
await this.initWorker();
30-
this.run();
31-
},
32-
});
3326
this.$page = $page;
3427
this.$page.settitle('Python');
35-
this.$page.get('header').append(this.$refresh);
28+
3629
this.#cacheFile = cacheFile;
30+
3731
const onhide = $page.onhide;
3832
$page.onhide = () => {
39-
this.#cacheFile.writeFile('\0');
33+
if (this.#workerInitialized) {
34+
this.#worker.terminate();
35+
this.#workerInitialized = false;
36+
}
4037
onhide();
4138
}
4239

@@ -59,27 +56,25 @@ class Python {
5956
onclick: this.run.bind(this),
6057
});
6158

62-
this.$input = tag('textarea', {
63-
onkeydown: this.#onkeydown.bind(this),
64-
style: {
65-
backgroundColor: 'transparent',
66-
color: 'inherit',
67-
width: '100%',
68-
border: 'none',
69-
},
59+
this.$style = tag('style', {
60+
textContent: style,
61+
});
62+
63+
this.$input = tag('div', {
64+
className: 'print input',
65+
child: tag('textarea', {
66+
onkeydown: this.#onkeydown.bind(this),
67+
}),
7068
});
69+
7170
this.checkRunnable();
7271
editorManager.on('switch-file', this.checkRunnable.bind(this));
7372
editorManager.on('rename-file', this.checkRunnable.bind(this));
74-
75-
await this.initWorker();
73+
document.head.append(this.$style);
7674
}
7775

7876
async initWorker() {
7977
if (this.#worker) this.#worker.terminate();
80-
if (window.toast) {
81-
window.toast('Python is loading...');
82-
}
8378
this.#worker = new Worker(this.baseUrl + 'worker.js');
8479
this.#worker.postMessage({
8580
action: 'init',
@@ -91,46 +86,56 @@ class Python {
9186
this.#onInitSuccess = resolve;
9287
this.#onInitError = error;
9388
});
94-
if (window.toast) {
95-
window.toast('Python is loaded.');
96-
}
89+
this.#workerInitialized = true;
9790
}
9891

9992
async run() {
100-
await this.#cacheFile.writeFile('');
101-
this.$page.get('.main').innerHTML = '';
102-
this.#append(this.$input);
103-
93+
const $main = this.$page.get('.main');
10494
if (!this.$page.isConnected) {
10595
this.$page.classList.remove('hide');
10696
this.$page.show();
10797
}
108-
setTimeout(async () => {
109-
const code = editorManager.editor.getValue();
110-
this.#worker.postMessage({
111-
action: 'run',
112-
code,
98+
99+
$main.innerHTML = '';
100+
101+
await this.#cacheFile.writeFile('');
102+
this.#append(this.$input);
103+
104+
this.print('Python initializing');
105+
try {
106+
await this.initWorker();
107+
} catch (error) {
108+
this.print(error, 'error');
109+
return;
110+
}
111+
112+
const code = editorManager.editor.getValue();
113+
this.#worker.postMessage({
114+
action: 'run',
115+
code,
116+
});
117+
try {
118+
const res = await new Promise((resolve, error) => {
119+
this.#onRunSuccess = resolve;
120+
this.#onRunError = error;
113121
});
114-
try {
115-
const res = await new Promise((resolve, error) => {
116-
this.#onRunSuccess = resolve;
117-
this.#onRunError = error;
118-
});
119-
this.print(res, 'output');
120-
} catch (error) {
121-
this.print(error, 'error');
122-
}
123-
}, 600);
122+
this.print(res, 'output');
123+
} catch (error) {
124+
this.print(error, 'error');
125+
}
124126
}
125127

126128
destroy() {
127129
if (this.$runBtn) {
128130
this.$runBtn.onclick = null;
129131
this.$runBtn.remove();
130132
}
131-
if (this.#worker) this.#worker.terminate();
133+
134+
if (this.#workerInitialized) this.#worker.terminate();
135+
132136
editorManager.off('switch-file', this.checkRunnable.bind(this));
133137
editorManager.off('rename-file', this.checkRunnable.bind(this));
138+
this.$style.remove();
134139
}
135140

136141
checkRunnable() {
@@ -150,14 +155,9 @@ class Python {
150155
print(res, type) {
151156
if (!this.$page.isConnected) return;
152157
const $output = tag('div', {
153-
className: 'python-output',
154-
});
155-
$output.appendChild(tag('pre', {
158+
className: `print ${type || ''}`,
156159
textContent: res,
157-
style: {
158-
color: type === 'error' ? 'orangered' : 'inherit',
159-
}
160-
}));
160+
});
161161
this.#append($output, this.$input);
162162
}
163163

@@ -191,7 +191,7 @@ class Python {
191191
if (action === 'input') {
192192
if (text) this.print(text);
193193
await this.#cacheFile.writeFile('');
194-
this.$input.focus();
194+
this.$input.get('textarea').focus();
195195
}
196196
if (action === 'stdout') {
197197
this.print(text);
@@ -204,8 +204,8 @@ class Python {
204204
#onkeydown(e) {
205205
if (e.key === 'Enter') {
206206
e.preventDefault();
207-
const value = this.$input.value;
208-
this.print(value);
207+
const value = e.target.value + '\0';
208+
this.print(value, 'input');
209209
this.#cacheFile.writeFile(value);
210210
e.target.value = '';
211211
}

src/style.scss

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#acode-plugin-python{
2+
3+
textarea{
4+
background-color: transparent;
5+
color: inherit;
6+
border: none;
7+
outline: none;
8+
resize: none;
9+
flex: 1;
10+
}
11+
12+
.print{
13+
display: flex;
14+
white-space: pre;
15+
overflow: auto;
16+
17+
&:not(:empty){
18+
&::before{
19+
content: '>>';
20+
padding: 0 10px 0 0;
21+
background: rgb(255, 255, 255);
22+
background: var(--secondary-color);
23+
position: sticky;
24+
left: 0;
25+
}
26+
27+
&.input::before{
28+
content: '<<';
29+
}
30+
31+
&.error{
32+
color: orangered;
33+
}
34+
}
35+
}
36+
}

webpack.config.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const path = require('path');
2-
const fs = require('fs');
3-
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
42

53
module.exports = (env, options) => {
64
const { mode = 'development' } = options;
@@ -21,18 +19,7 @@ module.exports = (env, options) => {
2119
{
2220
test: /\.(sa|sc|c)ss$/,
2321
use: [
24-
{
25-
loader: MiniCssExtractPlugin.loader,
26-
options: {
27-
publicPath: '../../',
28-
},
29-
},
30-
{
31-
loader: 'css-loader',
32-
options: {
33-
url: false,
34-
},
35-
},
22+
'raw-loader',
3623
'postcss-loader',
3724
'sass-loader',
3825
],
@@ -53,11 +40,6 @@ module.exports = (env, options) => {
5340
module: {
5441
rules,
5542
},
56-
plugins: [
57-
new MiniCssExtractPlugin({
58-
filename: '[name].css',
59-
}),
60-
],
6143
};
6244

6345
return [main];

0 commit comments

Comments
 (0)