princess-python-editor 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.html +1013 -0
- package/README.md +461 -2
- package/dist/PythonEditor.vue.d.ts +157 -0
- package/dist/editor/createEditor.d.ts +18 -0
- package/dist/editor/pythonLint.d.ts +8 -0
- package/dist/icons.d.ts +23 -0
- package/dist/index.cjs.js +228 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.es.js +1582 -0
- package/dist/languages.d.ts +15 -0
- package/dist/runtime/PyodideRunner.d.ts +66 -0
- package/dist/runtime/usePyodide.d.ts +42 -0
- package/dist/runtime/workerSource.d.ts +8 -0
- package/dist/style.css +1 -0
- package/dist/types.d.ts +115 -0
- package/package.json +90 -4
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,1582 @@
|
|
|
1
|
+
var Ht = Object.defineProperty;
|
|
2
|
+
var Tt = (u, r, a) => r in u ? Ht(u, r, { enumerable: !0, configurable: !0, writable: !0, value: a }) : u[r] = a;
|
|
3
|
+
var L = (u, r, a) => Tt(u, typeof r != "symbol" ? r + "" : r, a);
|
|
4
|
+
import { h as Ke, defineComponent as Nt, ref as F, computed as ie, onMounted as jt, onBeforeUnmount as _t, watch as V, nextTick as Ae, openBlock as g, createElementBlock as v, normalizeStyle as qe, normalizeClass as B, createVNode as $, unref as S, createElementVNode as c, createBlock as We, withDirectives as Dt, vModelText as Ut, toDisplayString as W, Fragment as se, renderList as de, createCommentVNode as z, withModifiers as Vt, onScopeDispose as Bt } from "vue";
|
|
5
|
+
import { EditorState as Se, StateEffect as zt, StateField as Kt, Compartment as ce } from "@codemirror/state";
|
|
6
|
+
import { EditorView as K, keymap as Je, lineNumbers as At, highlightActiveLineGutter as qt, highlightActiveLine as Wt, drawSelection as Jt, placeholder as Zt, Decoration as Ee } from "@codemirror/view";
|
|
7
|
+
import { oneDark as Fe } from "@codemirror/theme-one-dark";
|
|
8
|
+
import { linter as Gt, forceLinting as Xt } from "@codemirror/lint";
|
|
9
|
+
import { autocompletion as Yt, closeBrackets as Qt, closeBracketsKeymap as en } from "@codemirror/autocomplete";
|
|
10
|
+
import { history as tn, defaultKeymap as nn, historyKeymap as rn, indentWithTab as an } from "@codemirror/commands";
|
|
11
|
+
import { indentUnit as sn, bracketMatching as on } from "@codemirror/language";
|
|
12
|
+
import { python as Qe } from "@codemirror/lang-python";
|
|
13
|
+
import { javascript as ln } from "@codemirror/lang-javascript";
|
|
14
|
+
import { json as un } from "@codemirror/lang-json";
|
|
15
|
+
import { sql as dn } from "@codemirror/lang-sql";
|
|
16
|
+
const cn = "'JetBrains Mono', 'Fira Code', 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace", et = zt.define();
|
|
17
|
+
function pn(u, r) {
|
|
18
|
+
const a = u.filter((i) => Number.isFinite(i) && i >= 1 && i <= r.lines).sort((i, s) => i - s);
|
|
19
|
+
return Ee.set(
|
|
20
|
+
a.map((i) => Ee.line({ class: "vpe-error-line" }).range(r.line(i).from))
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const fn = Kt.define({
|
|
24
|
+
create: () => Ee.none,
|
|
25
|
+
update: (u, r) => {
|
|
26
|
+
let a = u;
|
|
27
|
+
for (const i of r.effects)
|
|
28
|
+
i.is(et) && (a = pn(i.value, r.state.doc));
|
|
29
|
+
return r.docChanged && (a = a.map(r.changes)), a;
|
|
30
|
+
},
|
|
31
|
+
provide: (u) => K.decorations.from(u)
|
|
32
|
+
}), hn = K.theme({
|
|
33
|
+
"&": { height: "100%", fontSize: "13px" },
|
|
34
|
+
".cm-scroller": { fontFamily: cn, lineHeight: "1.65" },
|
|
35
|
+
".cm-content": { padding: "10px 0 14px" },
|
|
36
|
+
".cm-gutters": {
|
|
37
|
+
borderRight: "none",
|
|
38
|
+
backgroundColor: "transparent",
|
|
39
|
+
paddingLeft: "2px",
|
|
40
|
+
opacity: "0.8"
|
|
41
|
+
},
|
|
42
|
+
"&.cm-focused": { outline: "none" }
|
|
43
|
+
});
|
|
44
|
+
function mn(u, r) {
|
|
45
|
+
const a = K.updateListener.of((P) => {
|
|
46
|
+
P.docChanged && r.onDocChange(P.state.doc.toString());
|
|
47
|
+
}), i = Je.of([
|
|
48
|
+
{
|
|
49
|
+
key: "Mod-Enter",
|
|
50
|
+
preventDefault: !0,
|
|
51
|
+
run: () => (r.onRunShortcut(), !0)
|
|
52
|
+
}
|
|
53
|
+
]), s = Se.create({
|
|
54
|
+
doc: r.value,
|
|
55
|
+
extensions: [
|
|
56
|
+
hn,
|
|
57
|
+
At(),
|
|
58
|
+
qt(),
|
|
59
|
+
Wt(),
|
|
60
|
+
Jt(),
|
|
61
|
+
tn(),
|
|
62
|
+
sn.of(" "),
|
|
63
|
+
r.language ?? Qe(),
|
|
64
|
+
Yt(),
|
|
65
|
+
Qt(),
|
|
66
|
+
on(),
|
|
67
|
+
K.lineWrapping,
|
|
68
|
+
Zt(r.placeholderText),
|
|
69
|
+
fn,
|
|
70
|
+
i,
|
|
71
|
+
Je.of([...en, ...nn, ...rn, an]),
|
|
72
|
+
...r.dark ? [Fe] : [],
|
|
73
|
+
...r.extensions,
|
|
74
|
+
a
|
|
75
|
+
]
|
|
76
|
+
});
|
|
77
|
+
return new K({ state: s, parent: u });
|
|
78
|
+
}
|
|
79
|
+
function yn(u, r = 600) {
|
|
80
|
+
return Gt(
|
|
81
|
+
async (a) => {
|
|
82
|
+
const i = a.state.doc.toString();
|
|
83
|
+
if (!i.trim()) return [];
|
|
84
|
+
let s = null;
|
|
85
|
+
try {
|
|
86
|
+
s = await u.checkSyntax(i);
|
|
87
|
+
} catch {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
if (!s) return [];
|
|
91
|
+
const P = Math.min(Math.max(Math.round(s.line) || 1, 1), a.state.doc.lines), w = a.state.doc.line(P), I = Math.min(Math.max(Math.round(s.column) || 1, 1), w.length + 1), H = w.from + I - 1, C = Math.min(H + 1, w.to);
|
|
92
|
+
return [
|
|
93
|
+
{
|
|
94
|
+
from: H,
|
|
95
|
+
to: C,
|
|
96
|
+
severity: "error",
|
|
97
|
+
message: s.message,
|
|
98
|
+
source: "python-syntax"
|
|
99
|
+
}
|
|
100
|
+
];
|
|
101
|
+
},
|
|
102
|
+
{ delay: r }
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const gn = [
|
|
106
|
+
"import ast, json",
|
|
107
|
+
"def _pye_check(src):",
|
|
108
|
+
" try:",
|
|
109
|
+
" ast.parse(src)",
|
|
110
|
+
" return json.dumps({'ok': True})",
|
|
111
|
+
" except SyntaxError as e:",
|
|
112
|
+
" return json.dumps({'ok': False, 'line': int(e.lineno or 1), 'column': int(e.offset or 1), 'message': str(e.msg or 'invalid syntax')})"
|
|
113
|
+
];
|
|
114
|
+
function vn(u) {
|
|
115
|
+
const r = u.endsWith("/") ? u : u + "/";
|
|
116
|
+
return `
|
|
117
|
+
var INDEX_URL = ${JSON.stringify(r)};
|
|
118
|
+
var CHECK_SOURCE = ${JSON.stringify(gn.join(`
|
|
119
|
+
`))};
|
|
120
|
+
var pyodide = null;
|
|
121
|
+
var checkFn = null;
|
|
122
|
+
var currentId = 0;
|
|
123
|
+
|
|
124
|
+
function post(msg) { self.postMessage(msg); }
|
|
125
|
+
|
|
126
|
+
function parseError(e) {
|
|
127
|
+
var msg = String((e && e.message) || e);
|
|
128
|
+
var line;
|
|
129
|
+
var re = /File "<exec>", line (\\d+)/g;
|
|
130
|
+
var m;
|
|
131
|
+
while ((m = re.exec(msg)) !== null) { line = Number(m[1]); }
|
|
132
|
+
var lines = msg.trim().split('\\n');
|
|
133
|
+
var last = lines[lines.length - 1];
|
|
134
|
+
var m2 = /^([A-Za-z_][\\w.]*)\\s*(?::\\s*(.*))?$/.exec(last);
|
|
135
|
+
var type = m2 ? m2[1] : 'Error';
|
|
136
|
+
var detail = m2 && m2[2] ? m2[2] : last;
|
|
137
|
+
return { type: type, message: detail, line: line, traceback: msg };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function fsError(e) {
|
|
141
|
+
return {
|
|
142
|
+
type: String((e && e.name) || 'OSError'),
|
|
143
|
+
message: String((e && e.message) || e),
|
|
144
|
+
traceback: ''
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** 将相对路径解析为虚拟文件系统中的绝对路径(默认基于 /home/pyodide) */
|
|
149
|
+
function normPath(p) {
|
|
150
|
+
var raw = String(p == null ? '' : p);
|
|
151
|
+
if (!raw) return pyodide.FS.cwd();
|
|
152
|
+
var full = raw.charAt(0) === '/' ? raw : pyodide.FS.cwd() + '/' + raw;
|
|
153
|
+
var parts = full.split('/');
|
|
154
|
+
var out = [];
|
|
155
|
+
for (var i = 0; i < parts.length; i++) {
|
|
156
|
+
var seg = parts[i];
|
|
157
|
+
if (!seg || seg === '.') continue;
|
|
158
|
+
if (seg === '..') {
|
|
159
|
+
if (out.length) out.pop();
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
out.push(seg);
|
|
163
|
+
}
|
|
164
|
+
return '/' + out.join('/');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** 将预置文本按行切分,逐行提供给 input();text 为 null 时立即 EOF */
|
|
168
|
+
function makeStdin(text) {
|
|
169
|
+
var lines = null;
|
|
170
|
+
if (text !== undefined && text !== null) {
|
|
171
|
+
lines = String(text).split(/\\r\\n|\\n|\\r/);
|
|
172
|
+
if (lines.length && lines[lines.length - 1] === '') lines.pop();
|
|
173
|
+
}
|
|
174
|
+
var index = 0;
|
|
175
|
+
return function () {
|
|
176
|
+
if (!lines || index >= lines.length) return null;
|
|
177
|
+
var line = lines[index];
|
|
178
|
+
index += 1;
|
|
179
|
+
return line + '\\n';
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 递归列出虚拟文件;跳过缓存目录并限制深度与条目数,避免大目录阻塞 */
|
|
184
|
+
function listFs(path, recursive) {
|
|
185
|
+
var entries = [];
|
|
186
|
+
function walk(dir, depth) {
|
|
187
|
+
var names = pyodide.FS.readdir(dir);
|
|
188
|
+
for (var i = 0; i < names.length; i++) {
|
|
189
|
+
var name = names[i];
|
|
190
|
+
if (name === '.' || name === '..') continue;
|
|
191
|
+
if (name === '__pycache__' || name === '.cache') continue;
|
|
192
|
+
var full = dir === '/' ? '/' + name : dir + '/' + name;
|
|
193
|
+
var stat;
|
|
194
|
+
try {
|
|
195
|
+
stat = pyodide.FS.stat(full);
|
|
196
|
+
} catch (e) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
var isDir = pyodide.FS.isDir(stat.mode);
|
|
200
|
+
entries.push({
|
|
201
|
+
name: name,
|
|
202
|
+
path: full,
|
|
203
|
+
isDirectory: isDir,
|
|
204
|
+
size: isDir ? 0 : stat.size
|
|
205
|
+
});
|
|
206
|
+
if (isDir && recursive && depth < 4 && entries.length < 500) {
|
|
207
|
+
walk(full, depth + 1);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
walk(path, 0);
|
|
212
|
+
entries.sort(function (a, b) {
|
|
213
|
+
if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1;
|
|
214
|
+
return a.path < b.path ? -1 : a.path > b.path ? 1 : 0;
|
|
215
|
+
});
|
|
216
|
+
return entries;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function init() {
|
|
220
|
+
try {
|
|
221
|
+
self.importScripts(INDEX_URL + 'pyodide.js');
|
|
222
|
+
pyodide = await self.loadPyodide({ indexURL: INDEX_URL });
|
|
223
|
+
pyodide.setStdout({ batched: function (s) {
|
|
224
|
+
post({ kind: 'output', runId: currentId, stream: 'stdout', text: s });
|
|
225
|
+
} });
|
|
226
|
+
pyodide.setStderr({ batched: function (s) {
|
|
227
|
+
post({ kind: 'output', runId: currentId, stream: 'stderr', text: s });
|
|
228
|
+
} });
|
|
229
|
+
pyodide.runPython(CHECK_SOURCE);
|
|
230
|
+
checkFn = pyodide.globals.get('_pye_check');
|
|
231
|
+
post({ kind: 'ready', version: pyodide.version });
|
|
232
|
+
} catch (e) {
|
|
233
|
+
post({ kind: 'init-error', message: String((e && e.message) || e) });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
init();
|
|
238
|
+
|
|
239
|
+
self.onmessage = async function (ev) {
|
|
240
|
+
var msg = ev.data;
|
|
241
|
+
if (!msg || !msg.kind) return;
|
|
242
|
+
|
|
243
|
+
if (msg.kind === 'run') {
|
|
244
|
+
currentId = msg.id;
|
|
245
|
+
if (!pyodide) {
|
|
246
|
+
currentId = 0;
|
|
247
|
+
post({ kind: 'result', id: msg.id, ok: false, error: { type: 'RuntimeError', message: 'Pyodide 未初始化', traceback: '' } });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
pyodide.setStdin({ stdin: makeStdin(msg.stdin) });
|
|
252
|
+
var result = await pyodide.runPythonAsync(msg.code);
|
|
253
|
+
var value;
|
|
254
|
+
if (result !== undefined && result !== null) {
|
|
255
|
+
try { value = String(result); } catch (e2) { value = undefined; }
|
|
256
|
+
if (result && typeof result.destroy === 'function') {
|
|
257
|
+
try { result.destroy(); } catch (e3) { /* 忽略释放异常 */ }
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
post({ kind: 'result', id: msg.id, ok: true, value: value });
|
|
261
|
+
} catch (e) {
|
|
262
|
+
post({ kind: 'result', id: msg.id, ok: false, error: parseError(e) });
|
|
263
|
+
}
|
|
264
|
+
currentId = 0;
|
|
265
|
+
|
|
266
|
+
} else if (msg.kind === 'check') {
|
|
267
|
+
currentId = msg.id;
|
|
268
|
+
try {
|
|
269
|
+
var json = checkFn(msg.code);
|
|
270
|
+
var parsed = JSON.parse(json);
|
|
271
|
+
post({ kind: 'checked', id: msg.id, ok: !!parsed.ok, line: parsed.line, column: parsed.column, message: parsed.message });
|
|
272
|
+
} catch (e) {
|
|
273
|
+
// 检查器自身异常时不误报语法错误
|
|
274
|
+
post({ kind: 'checked', id: msg.id, ok: true });
|
|
275
|
+
}
|
|
276
|
+
currentId = 0;
|
|
277
|
+
|
|
278
|
+
} else if (msg.kind === 'install') {
|
|
279
|
+
currentId = msg.id;
|
|
280
|
+
try {
|
|
281
|
+
await pyodide.loadPackage(msg.packages);
|
|
282
|
+
post({ kind: 'installed', id: msg.id, ok: true });
|
|
283
|
+
} catch (e1) {
|
|
284
|
+
try {
|
|
285
|
+
await pyodide.loadPackage('micropip');
|
|
286
|
+
var micropip = pyodide.pyimport('micropip');
|
|
287
|
+
await micropip.install(msg.packages);
|
|
288
|
+
post({ kind: 'installed', id: msg.id, ok: true });
|
|
289
|
+
} catch (e2) {
|
|
290
|
+
post({ kind: 'installed', id: msg.id, ok: false, error: parseError(e2) });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
currentId = 0;
|
|
294
|
+
|
|
295
|
+
} else if (msg.kind === 'fs-write') {
|
|
296
|
+
if (!pyodide) {
|
|
297
|
+
post({ kind: 'fs-written', id: msg.id, ok: false, error: { type: 'RuntimeError', message: 'Pyodide 未初始化', traceback: '' } });
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
try {
|
|
301
|
+
var writePath = normPath(msg.path);
|
|
302
|
+
var slash = writePath.lastIndexOf('/');
|
|
303
|
+
var dir = slash <= 0 ? '/' : writePath.slice(0, slash);
|
|
304
|
+
try {
|
|
305
|
+
pyodide.FS.mkdirTree(dir);
|
|
306
|
+
} catch (e0) {
|
|
307
|
+
// 目录已存在时忽略
|
|
308
|
+
}
|
|
309
|
+
pyodide.FS.writeFile(writePath, String(msg.content == null ? '' : msg.content), { encoding: 'utf8' });
|
|
310
|
+
post({ kind: 'fs-written', id: msg.id, ok: true, path: writePath });
|
|
311
|
+
} catch (e) {
|
|
312
|
+
post({ kind: 'fs-written', id: msg.id, ok: false, error: fsError(e) });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
} else if (msg.kind === 'fs-read') {
|
|
316
|
+
if (!pyodide) {
|
|
317
|
+
post({ kind: 'fs-read', id: msg.id, ok: false, error: { type: 'RuntimeError', message: 'Pyodide 未初始化', traceback: '' } });
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
var readPath = normPath(msg.path);
|
|
322
|
+
var content = pyodide.FS.readFile(readPath, { encoding: 'utf8' });
|
|
323
|
+
post({ kind: 'fs-read', id: msg.id, ok: true, path: readPath, content: content });
|
|
324
|
+
} catch (e) {
|
|
325
|
+
post({ kind: 'fs-read', id: msg.id, ok: false, error: fsError(e) });
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
} else if (msg.kind === 'fs-list') {
|
|
329
|
+
if (!pyodide) {
|
|
330
|
+
post({ kind: 'fs-list', id: msg.id, ok: false, error: { type: 'RuntimeError', message: 'Pyodide 未初始化', traceback: '' } });
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
var listPath = normPath(msg.path);
|
|
335
|
+
var entries = listFs(listPath, msg.recursive !== false);
|
|
336
|
+
post({ kind: 'fs-list', id: msg.id, ok: true, path: listPath, entries: entries });
|
|
337
|
+
} catch (e) {
|
|
338
|
+
post({ kind: 'fs-list', id: msg.id, ok: false, error: fsError(e) });
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
`;
|
|
343
|
+
}
|
|
344
|
+
const tt = "https://cdn.jsdelivr.net/pyodide/v0.27.2/full/";
|
|
345
|
+
class kn {
|
|
346
|
+
constructor(r = {}) {
|
|
347
|
+
L(this, "status", "idle");
|
|
348
|
+
L(this, "version", "");
|
|
349
|
+
L(this, "pyodideUrl");
|
|
350
|
+
L(this, "worker", null);
|
|
351
|
+
L(this, "pending", /* @__PURE__ */ new Map());
|
|
352
|
+
L(this, "seq", 1);
|
|
353
|
+
L(this, "chain", Promise.resolve());
|
|
354
|
+
/** Worker 生命周期代数:interrupt 后递增,用于废弃旧的 waitReady 等待 */
|
|
355
|
+
L(this, "generation", 0);
|
|
356
|
+
L(this, "statusHandlers", /* @__PURE__ */ new Set());
|
|
357
|
+
L(this, "outputHandlers", /* @__PURE__ */ new Set());
|
|
358
|
+
L(this, "initErrorHandlers", /* @__PURE__ */ new Set());
|
|
359
|
+
this.pyodideUrl = r.pyodideUrl || tt;
|
|
360
|
+
}
|
|
361
|
+
on(r, a) {
|
|
362
|
+
if (r === "status") {
|
|
363
|
+
const s = a;
|
|
364
|
+
return this.statusHandlers.add(s), () => this.statusHandlers.delete(s);
|
|
365
|
+
}
|
|
366
|
+
if (r === "output") {
|
|
367
|
+
const s = a;
|
|
368
|
+
return this.outputHandlers.add(s), () => this.outputHandlers.delete(s);
|
|
369
|
+
}
|
|
370
|
+
const i = a;
|
|
371
|
+
return this.initErrorHandlers.add(i), () => this.initErrorHandlers.delete(i);
|
|
372
|
+
}
|
|
373
|
+
/** 预加载 Pyodide(返回 Promise,在就绪或失败时 settle) */
|
|
374
|
+
waitReady(r = 18e4) {
|
|
375
|
+
const a = this.generation;
|
|
376
|
+
return this.ensureWorker(), this.status === "ready" || this.status === "running" ? Promise.resolve() : new Promise((i, s) => {
|
|
377
|
+
let P = () => {
|
|
378
|
+
}, w = () => {
|
|
379
|
+
};
|
|
380
|
+
const I = () => {
|
|
381
|
+
clearTimeout(H), P(), w();
|
|
382
|
+
}, H = setTimeout(() => {
|
|
383
|
+
I(), s(new Error("Pyodide 加载超时"));
|
|
384
|
+
}, r);
|
|
385
|
+
P = this.on("status", (C) => {
|
|
386
|
+
if (this.generation !== a) {
|
|
387
|
+
I(), s(new Error("执行已被中断"));
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
C === "ready" && (I(), i());
|
|
391
|
+
}), w = this.on("initError", (C) => {
|
|
392
|
+
I(), s(new Error(C));
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
/** 执行一段 Python 代码(支持顶层 await);运行器未就绪时先等待初始化完成 */
|
|
397
|
+
async run(r, a) {
|
|
398
|
+
await this.waitReady();
|
|
399
|
+
const i = performance.now();
|
|
400
|
+
this.setStatus("running");
|
|
401
|
+
const s = await this.send(
|
|
402
|
+
{ kind: "run", id: this.seq++, code: r, stdin: a == null ? void 0 : a.stdin },
|
|
403
|
+
a == null ? void 0 : a.onOutput
|
|
404
|
+
);
|
|
405
|
+
return this.status === "running" && this.setStatus("ready"), {
|
|
406
|
+
ok: s.ok,
|
|
407
|
+
value: s.value,
|
|
408
|
+
error: s.error,
|
|
409
|
+
interrupted: s.interrupted,
|
|
410
|
+
durationMs: performance.now() - i
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
/** 语法检查:仅在运行器就绪时可用,否则返回 null(静默跳过) */
|
|
414
|
+
async checkSyntax(r) {
|
|
415
|
+
if (this.status !== "ready" || !r.trim()) return null;
|
|
416
|
+
const a = await this.send({ kind: "check", id: this.seq++, code: r });
|
|
417
|
+
return a.ok ? null : {
|
|
418
|
+
line: a.line ?? 1,
|
|
419
|
+
column: a.column ?? 1,
|
|
420
|
+
message: a.message ?? "语法错误"
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
/** 写入虚拟文件系统(UTF-8 文本),相对路径基于当前工作目录 */
|
|
424
|
+
async writeFile(r, a) {
|
|
425
|
+
await this.waitReady();
|
|
426
|
+
const i = await this.send({ kind: "fs-write", id: this.seq++, path: r, content: a });
|
|
427
|
+
return { ok: i.ok, path: i.path, error: i.error };
|
|
428
|
+
}
|
|
429
|
+
/** 读取虚拟文件系统中的文本文件;二进制内容会返回失败 */
|
|
430
|
+
async readFile(r) {
|
|
431
|
+
await this.waitReady();
|
|
432
|
+
const a = await this.send({ kind: "fs-read", id: this.seq++, path: r });
|
|
433
|
+
return { ok: a.ok, path: a.path, content: a.content, error: a.error };
|
|
434
|
+
}
|
|
435
|
+
/** 列出虚拟文件系统目录(递归,跳过 __pycache__ / .cache) */
|
|
436
|
+
async listFiles(r = ".") {
|
|
437
|
+
await this.waitReady();
|
|
438
|
+
const a = await this.send({
|
|
439
|
+
kind: "fs-list",
|
|
440
|
+
id: this.seq++,
|
|
441
|
+
path: r,
|
|
442
|
+
recursive: !0
|
|
443
|
+
});
|
|
444
|
+
return { ok: a.ok, path: a.path, entries: a.entries, error: a.error };
|
|
445
|
+
}
|
|
446
|
+
/** 安装第三方包:先尝试内置 wheel(loadPackage),失败后回退 micropip */
|
|
447
|
+
async loadPackages(r, a) {
|
|
448
|
+
await this.waitReady();
|
|
449
|
+
const i = performance.now();
|
|
450
|
+
this.setStatus("running");
|
|
451
|
+
const s = await this.send(
|
|
452
|
+
{ kind: "install", id: this.seq++, packages: r },
|
|
453
|
+
a == null ? void 0 : a.onOutput
|
|
454
|
+
);
|
|
455
|
+
return this.status === "running" && this.setStatus("ready"), { ok: s.ok, error: s.error, durationMs: performance.now() - i };
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* 中断当前执行:terminate Worker(Pyodide 无法真正中断同步计算),
|
|
459
|
+
* 所有未决请求以 interrupted 结果结束;下次请求时自动重建 Worker。
|
|
460
|
+
*/
|
|
461
|
+
interrupt() {
|
|
462
|
+
if (!this.worker) return;
|
|
463
|
+
this.generation += 1, this.worker.terminate(), this.worker = null;
|
|
464
|
+
const r = {
|
|
465
|
+
ok: !1,
|
|
466
|
+
interrupted: !0,
|
|
467
|
+
error: { type: "KeyboardInterrupt", message: "执行已被用户中断", traceback: "" }
|
|
468
|
+
};
|
|
469
|
+
this.pending.forEach((a) => a.resolve(r)), this.pending.clear(), this.chain = Promise.resolve(), this.setStatus("idle");
|
|
470
|
+
}
|
|
471
|
+
ensureWorker() {
|
|
472
|
+
if (this.worker) return this.worker;
|
|
473
|
+
const r = new Blob([vn(this.pyodideUrl)], { type: "text/javascript" }), a = new Worker(URL.createObjectURL(r));
|
|
474
|
+
return a.onmessage = (i) => this.handleMessage(i.data), this.worker = a, this.setStatus("loading"), a;
|
|
475
|
+
}
|
|
476
|
+
handleMessage(r) {
|
|
477
|
+
switch (r.kind) {
|
|
478
|
+
case "ready":
|
|
479
|
+
this.version = r.version, this.setStatus("ready");
|
|
480
|
+
break;
|
|
481
|
+
case "init-error":
|
|
482
|
+
this.setStatus("idle"), this.initErrorHandlers.forEach((a) => a(r.message));
|
|
483
|
+
break;
|
|
484
|
+
case "output": {
|
|
485
|
+
const a = r.runId ? this.pending.get(r.runId) : void 0;
|
|
486
|
+
a != null && a.onOutput ? a.onOutput(r.stream, r.text) : this.outputHandlers.forEach((i) => i(r.stream, r.text));
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
case "result": {
|
|
490
|
+
const a = this.pending.get(r.id);
|
|
491
|
+
a && (this.pending.delete(r.id), a.resolve({ ok: r.ok, value: r.value, error: r.error }));
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
case "checked": {
|
|
495
|
+
const a = this.pending.get(r.id);
|
|
496
|
+
a && (this.pending.delete(r.id), a.resolve({
|
|
497
|
+
ok: r.ok,
|
|
498
|
+
line: r.line,
|
|
499
|
+
column: r.column,
|
|
500
|
+
message: r.message
|
|
501
|
+
}));
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
case "installed": {
|
|
505
|
+
const a = this.pending.get(r.id);
|
|
506
|
+
a && (this.pending.delete(r.id), a.resolve({ ok: r.ok, error: r.error }));
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
case "fs-written": {
|
|
510
|
+
const a = this.pending.get(r.id);
|
|
511
|
+
a && (this.pending.delete(r.id), a.resolve({ ok: r.ok, path: r.path, error: r.error }));
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
case "fs-read": {
|
|
515
|
+
const a = this.pending.get(r.id);
|
|
516
|
+
a && (this.pending.delete(r.id), a.resolve({
|
|
517
|
+
ok: r.ok,
|
|
518
|
+
path: r.path,
|
|
519
|
+
content: r.content,
|
|
520
|
+
error: r.error
|
|
521
|
+
}));
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
case "fs-list": {
|
|
525
|
+
const a = this.pending.get(r.id);
|
|
526
|
+
a && (this.pending.delete(r.id), a.resolve({
|
|
527
|
+
ok: r.ok,
|
|
528
|
+
path: r.path,
|
|
529
|
+
entries: r.entries,
|
|
530
|
+
error: r.error
|
|
531
|
+
}));
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
send(r, a) {
|
|
537
|
+
const i = this.ensureWorker(), s = this.chain.then(
|
|
538
|
+
() => new Promise((P) => {
|
|
539
|
+
this.pending.set(r.id, { resolve: P, onOutput: a }), i.postMessage(r);
|
|
540
|
+
})
|
|
541
|
+
);
|
|
542
|
+
return this.chain = s.then(
|
|
543
|
+
() => {
|
|
544
|
+
},
|
|
545
|
+
() => {
|
|
546
|
+
}
|
|
547
|
+
), s;
|
|
548
|
+
}
|
|
549
|
+
setStatus(r) {
|
|
550
|
+
this.status !== r && (this.status = r, this.statusHandlers.forEach((a) => a(r, this.version)));
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
let be = null;
|
|
554
|
+
function nt(u) {
|
|
555
|
+
return be ? u != null && u.pyodideUrl && u.pyodideUrl !== tt && console.warn(
|
|
556
|
+
"[princess-python-editor] Pyodide 运行器已初始化,后续传入的 pyodideUrl 将被忽略。"
|
|
557
|
+
) : be = new kn(u), be;
|
|
558
|
+
}
|
|
559
|
+
const fe = [
|
|
560
|
+
{ value: "python", label: "Python", extension: "py" },
|
|
561
|
+
{ value: "sql", label: "SQL", extension: "sql" },
|
|
562
|
+
{ value: "javascript", label: "JavaScript", extension: "js" },
|
|
563
|
+
{ value: "json", label: "JSON", extension: "json" }
|
|
564
|
+
];
|
|
565
|
+
function pe(u) {
|
|
566
|
+
return fe.find((r) => r.value === u) ?? fe[0];
|
|
567
|
+
}
|
|
568
|
+
function we(u) {
|
|
569
|
+
switch (u) {
|
|
570
|
+
case "sql":
|
|
571
|
+
return dn();
|
|
572
|
+
case "javascript":
|
|
573
|
+
return ln();
|
|
574
|
+
case "json":
|
|
575
|
+
return un();
|
|
576
|
+
case "python":
|
|
577
|
+
default:
|
|
578
|
+
return Qe();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
function bn(u) {
|
|
582
|
+
const r = u.lastIndexOf(".");
|
|
583
|
+
if (r < 0) return "python";
|
|
584
|
+
switch (u.slice(r + 1).toLowerCase()) {
|
|
585
|
+
case "sql":
|
|
586
|
+
return "sql";
|
|
587
|
+
case "js":
|
|
588
|
+
case "mjs":
|
|
589
|
+
case "cjs":
|
|
590
|
+
case "jsx":
|
|
591
|
+
case "ts":
|
|
592
|
+
return "javascript";
|
|
593
|
+
case "json":
|
|
594
|
+
return "json";
|
|
595
|
+
default:
|
|
596
|
+
return "python";
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
function R(u) {
|
|
600
|
+
const r = Array.isArray(u) ? u : [u];
|
|
601
|
+
return () => Ke(
|
|
602
|
+
"svg",
|
|
603
|
+
{
|
|
604
|
+
viewBox: "0 0 24 24",
|
|
605
|
+
width: "1em",
|
|
606
|
+
height: "1em",
|
|
607
|
+
fill: "currentColor",
|
|
608
|
+
"aria-hidden": "true"
|
|
609
|
+
},
|
|
610
|
+
r.map((a) => Ke("path", { d: a }))
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
const wn = R(
|
|
614
|
+
"M8 5.14v13.72a1 1 0 0 0 1.54.84l10.3-6.86a1 1 0 0 0 0-1.68L9.54 4.3A1 1 0 0 0 8 5.14Z"
|
|
615
|
+
), xn = R("M6 6h12v12H6Z"), Ze = R(
|
|
616
|
+
"M9 3h6a1 1 0 0 1 1 1v1h4a1 1 0 1 1 0 2h-1.06l-.87 12.13A2 2 0 0 1 16.08 21H7.92a2 2 0 0 1-1.99-1.87L5.06 7H4a1 1 0 0 1 0-2h4V4a1 1 0 0 1 1-1Zm1 2h4V5h-4v0Zm-2.92 2h9.84l-.85 12.07a.5.5 0 0 1-.5.43H7.93a.5.5 0 0 1-.5-.43Z"
|
|
617
|
+
), Sn = R(
|
|
618
|
+
"M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm2 14h-3v3h-2v-3H8v-2h3v-3h2v3h3v2zm-3-7V3.5L18.5 9H13z"
|
|
619
|
+
), En = R(
|
|
620
|
+
"M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"
|
|
621
|
+
), Ge = R(
|
|
622
|
+
"M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm2 16H8v-2h8v2zm0-4H8v-2h8v2zm-3-5V3.5L18.5 9H13z"
|
|
623
|
+
), Xe = R("M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"), Fn = R(
|
|
624
|
+
"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"
|
|
625
|
+
), Pn = R("M9 16.17 4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"), Ye = R(
|
|
626
|
+
"M18.3 5.71a1 1 0 0 0-1.41 0L12 10.59 7.11 5.7A1 1 0 0 0 5.7 7.11L10.59 12 5.7 16.89a1 1 0 1 0 1.41 1.41L12 13.41l4.89 4.89a1 1 0 0 0 1.41-1.41L13.41 12l4.89-4.89a1 1 0 0 0 0-1.4Z"
|
|
627
|
+
), Cn = R("M11 5h2v6h6v2h-6v6h-2v-6H5v-2h6V5z"), On = {
|
|
628
|
+
key: 0,
|
|
629
|
+
class: "vpe-toolbar"
|
|
630
|
+
}, $n = ["title", "disabled"], Ln = ["disabled"], In = ["disabled"], Mn = ["title", "aria-label"], Rn = ["disabled"], Hn = {
|
|
631
|
+
class: "vpe-file",
|
|
632
|
+
title: "当前文件名(下载时使用)"
|
|
633
|
+
}, Tn = ["value", "disabled"], Nn = ["value"], jn = ["aria-selected", "data-file-id", "title", "onClick", "onKeydown"], _n = { class: "vpe-tab-name" }, Dn = ["title", "aria-label", "disabled", "onClick"], Un = ["disabled"], Vn = { class: "vpe-output-head" }, Bn = {
|
|
634
|
+
key: 1,
|
|
635
|
+
class: "vpe-empty"
|
|
636
|
+
}, zn = {
|
|
637
|
+
key: 3,
|
|
638
|
+
class: "vpe-fs-panel",
|
|
639
|
+
role: "dialog",
|
|
640
|
+
"aria-label": "虚拟文件"
|
|
641
|
+
}, Kn = { class: "vpe-fs-head" }, An = ["disabled"], qn = { class: "vpe-fs-body" }, Wn = {
|
|
642
|
+
key: 0,
|
|
643
|
+
class: "vpe-fs-empty"
|
|
644
|
+
}, Jn = {
|
|
645
|
+
key: 0,
|
|
646
|
+
class: "vpe-fs-empty"
|
|
647
|
+
}, Zn = ["title"], Gn = { class: "vpe-fs-size" }, Xn = ["title", "aria-label", "onClick"], xe = 1, Yn = /* @__PURE__ */ Nt({
|
|
648
|
+
__name: "PythonEditor",
|
|
649
|
+
props: {
|
|
650
|
+
modelValue: { default: "" },
|
|
651
|
+
theme: { default: "light" },
|
|
652
|
+
height: { default: 520 },
|
|
653
|
+
outputHeight: { default: 180 },
|
|
654
|
+
placeholder: { default: "" },
|
|
655
|
+
readOnly: { type: Boolean, default: !1 },
|
|
656
|
+
autofocus: { type: Boolean, default: !1 },
|
|
657
|
+
showToolbar: { type: Boolean, default: !0 },
|
|
658
|
+
showTabs: { type: Boolean, default: !0 },
|
|
659
|
+
showOutput: { type: Boolean, default: !0 },
|
|
660
|
+
clearOutputOnRun: { type: Boolean, default: !0 },
|
|
661
|
+
lintDelay: { default: 600 },
|
|
662
|
+
disableLint: { type: Boolean, default: !1 },
|
|
663
|
+
preload: { type: Boolean, default: !1 },
|
|
664
|
+
pyodideUrl: { default: "" },
|
|
665
|
+
fileName: { default: "main.py" },
|
|
666
|
+
language: { default: "python" },
|
|
667
|
+
stdin: { default: "" },
|
|
668
|
+
syncFilesOnRun: { type: Boolean, default: !0 },
|
|
669
|
+
storageKey: { default: "" },
|
|
670
|
+
preloadPackages: { default: () => [] }
|
|
671
|
+
},
|
|
672
|
+
emits: ["update:modelValue", "ready", "statusChange", "runStart", "runOutput", "runError", "runFinish", "loadError", "update:fileName", "update:language", "newFile", "fileClose", "fileSwitch", "copy", "download", "fileOpen", "fsDownload"],
|
|
673
|
+
setup(u, { expose: r, emit: a }) {
|
|
674
|
+
const i = u, s = a, P = F(), w = F(), I = F(), H = F(), C = F([]), M = F("idle"), T = F(!1), D = F(!1), N = F(!1), j = F([]);
|
|
675
|
+
let J = !1, Z, he = !1, oe = !1, G = !1, o = null, h = 0, x = 0, O = null, U = null, X;
|
|
676
|
+
function le(e, t, n) {
|
|
677
|
+
return { id: ++x, name: e, language: t, code: n };
|
|
678
|
+
}
|
|
679
|
+
const f = F([le(i.fileName, i.language, i.modelValue)]), _ = F(f.value[0].id), y = ie(
|
|
680
|
+
() => f.value.find((e) => e.id === _.value) ?? f.value[0]
|
|
681
|
+
), A = ie({
|
|
682
|
+
get: () => y.value.name,
|
|
683
|
+
set: (e) => {
|
|
684
|
+
const t = y.value;
|
|
685
|
+
t.name !== e && (t.name = e, s("update:fileName", e));
|
|
686
|
+
}
|
|
687
|
+
}), Pe = new ce(), Ce = new ce(), me = new ce(), ye = new ce(), b = nt(i.pyodideUrl ? { pyodideUrl: i.pyodideUrl } : void 0), rt = ie(() => ({
|
|
688
|
+
height: typeof i.height == "number" ? `${i.height}px` : i.height
|
|
689
|
+
})), at = ie(
|
|
690
|
+
() => typeof i.outputHeight == "number" ? `${i.outputHeight}px` : i.outputHeight
|
|
691
|
+
), it = ie(
|
|
692
|
+
() => ({
|
|
693
|
+
idle: "未加载",
|
|
694
|
+
loading: "正在加载 Pyodide…",
|
|
695
|
+
ready: "就绪",
|
|
696
|
+
running: "运行中"
|
|
697
|
+
})[M.value]
|
|
698
|
+
);
|
|
699
|
+
function Y(e) {
|
|
700
|
+
return { ...e };
|
|
701
|
+
}
|
|
702
|
+
function Q(e, t) {
|
|
703
|
+
C.value.push({ id: ++h, stream: e, text: t });
|
|
704
|
+
}
|
|
705
|
+
function p(e) {
|
|
706
|
+
Q("system", e);
|
|
707
|
+
}
|
|
708
|
+
function st(e) {
|
|
709
|
+
return fe.some((t) => t.value === e);
|
|
710
|
+
}
|
|
711
|
+
function ot() {
|
|
712
|
+
if (!i.storageKey || typeof window > "u") return null;
|
|
713
|
+
try {
|
|
714
|
+
const e = window.localStorage.getItem(i.storageKey);
|
|
715
|
+
if (!e) return null;
|
|
716
|
+
const t = JSON.parse(e);
|
|
717
|
+
if (t.version !== xe || !Array.isArray(t.files)) return null;
|
|
718
|
+
const n = [];
|
|
719
|
+
for (const m of t.files) {
|
|
720
|
+
if (!m || typeof m.name != "string" || typeof m.code != "string" || !m.name.trim()) continue;
|
|
721
|
+
const k = st(m.language) ? m.language : "python";
|
|
722
|
+
n.push({ name: m.name, language: k, code: m.code });
|
|
723
|
+
}
|
|
724
|
+
if (!n.length) return null;
|
|
725
|
+
const l = typeof t.activeIndex == "number" ? t.activeIndex : 0, d = Math.min(Math.max(Math.trunc(l) || 0, 0), n.length - 1);
|
|
726
|
+
return { version: xe, files: n, activeIndex: d };
|
|
727
|
+
} catch {
|
|
728
|
+
return null;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
function lt() {
|
|
732
|
+
const e = ot();
|
|
733
|
+
if (!e) return;
|
|
734
|
+
f.value = e.files.map((n) => le(n.name, n.language, n.code));
|
|
735
|
+
const t = f.value[e.activeIndex] ?? f.value[0];
|
|
736
|
+
_.value = t.id, p(`已恢复上次编辑内容(${f.value.length} 个文件)`), s("update:modelValue", t.code), s("update:fileName", t.name), s("update:language", t.language);
|
|
737
|
+
}
|
|
738
|
+
function Oe() {
|
|
739
|
+
if (!(!J || oe || !i.storageKey || typeof window > "u"))
|
|
740
|
+
try {
|
|
741
|
+
const e = {
|
|
742
|
+
version: xe,
|
|
743
|
+
files: f.value.map((t) => ({
|
|
744
|
+
name: t.name,
|
|
745
|
+
language: t.language,
|
|
746
|
+
code: t.code
|
|
747
|
+
})),
|
|
748
|
+
activeIndex: Math.max(
|
|
749
|
+
f.value.findIndex((t) => t.id === _.value),
|
|
750
|
+
0
|
|
751
|
+
)
|
|
752
|
+
};
|
|
753
|
+
window.localStorage.setItem(i.storageKey, JSON.stringify(e));
|
|
754
|
+
} catch (e) {
|
|
755
|
+
he || (he = !0, p(`本地草稿保存失败:${e instanceof Error ? e.message : String(e)}`));
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
function ut() {
|
|
759
|
+
!J || !i.storageKey || typeof window > "u" || (oe = !1, window.clearTimeout(Z), Z = window.setTimeout(Oe, 400));
|
|
760
|
+
}
|
|
761
|
+
function dt() {
|
|
762
|
+
if (!(!i.storageKey || typeof window > "u")) {
|
|
763
|
+
window.clearTimeout(Z);
|
|
764
|
+
try {
|
|
765
|
+
window.localStorage.removeItem(i.storageKey), oe = !0, p("已清除本地草稿");
|
|
766
|
+
} catch (e) {
|
|
767
|
+
p(`清除本地草稿失败:${e instanceof Error ? e.message : String(e)}`);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
function ee(e) {
|
|
772
|
+
if (!o) return;
|
|
773
|
+
const t = e ? [e] : [];
|
|
774
|
+
o.dispatch({ effects: et.of(t) });
|
|
775
|
+
}
|
|
776
|
+
function ct() {
|
|
777
|
+
const e = i.modelValue;
|
|
778
|
+
if (o) {
|
|
779
|
+
e !== o.state.doc.toString() && o.dispatch({ changes: { from: 0, to: o.state.doc.length, insert: e } });
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
const t = y.value;
|
|
783
|
+
t.code !== e && (t.code = e);
|
|
784
|
+
}
|
|
785
|
+
function te() {
|
|
786
|
+
return o ? o.state.doc.toString() : y.value.code;
|
|
787
|
+
}
|
|
788
|
+
function ge(e) {
|
|
789
|
+
if (o) {
|
|
790
|
+
e !== o.state.doc.toString() && o.dispatch({ changes: { from: 0, to: o.state.doc.length, insert: e } });
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
const t = y.value;
|
|
794
|
+
t.code = e, e !== i.modelValue && s("update:modelValue", e);
|
|
795
|
+
}
|
|
796
|
+
function ve(e) {
|
|
797
|
+
return i.disableLint || e !== "python" ? [] : yn(b, i.lintDelay);
|
|
798
|
+
}
|
|
799
|
+
function $e(e) {
|
|
800
|
+
const t = y.value;
|
|
801
|
+
if (t.language !== e) {
|
|
802
|
+
if (t.language = e, ee(void 0), o == null || o.dispatch({
|
|
803
|
+
effects: [
|
|
804
|
+
me.reconfigure(we(e)),
|
|
805
|
+
ye.reconfigure(ve(e))
|
|
806
|
+
]
|
|
807
|
+
}), /^untitled(-\d+)?\.[^.]+$/.test(t.name)) {
|
|
808
|
+
const l = `${t.name.slice(0, t.name.lastIndexOf("."))}.${pe(e).extension}`;
|
|
809
|
+
f.value.some((d) => d.id !== t.id && d.name === l) || (t.name = l, s("update:fileName", l));
|
|
810
|
+
}
|
|
811
|
+
s("update:language", e);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
function Le(e, t) {
|
|
815
|
+
const l = e.trim().replace(/[\\/:*?"<>|]+/g, "_").replace(/\.+$/, "") || "untitled";
|
|
816
|
+
return /\.[^.\\/]+$/.test(l) ? l : `${l}.${pe(t).extension}`;
|
|
817
|
+
}
|
|
818
|
+
function pt(e) {
|
|
819
|
+
const t = pe(e).extension, n = new Set(f.value.map((m) => m.name));
|
|
820
|
+
let l = `untitled.${t}`, d = 2;
|
|
821
|
+
for (; n.has(l); )
|
|
822
|
+
l = `untitled-${d}.${t}`, d += 1;
|
|
823
|
+
return l;
|
|
824
|
+
}
|
|
825
|
+
function ft(e) {
|
|
826
|
+
if (!f.value.some((k) => k.name === e)) return e;
|
|
827
|
+
const t = e.lastIndexOf("."), n = t > 0 ? e.slice(0, t) : e, l = t > 0 ? e.slice(t) : "";
|
|
828
|
+
let d = 2, m = `${n}-${d}${l}`;
|
|
829
|
+
for (; f.value.some((k) => k.name === m); )
|
|
830
|
+
d += 1, m = `${n}-${d}${l}`;
|
|
831
|
+
return m;
|
|
832
|
+
}
|
|
833
|
+
let ne = 0;
|
|
834
|
+
function ht(e) {
|
|
835
|
+
return !!e && e !== "." && e !== ".." && !e.includes("/") && !e.includes("\\");
|
|
836
|
+
}
|
|
837
|
+
async function mt() {
|
|
838
|
+
var t;
|
|
839
|
+
if (!i.syncFilesOnRun) return;
|
|
840
|
+
const e = f.value.filter((n) => n.language === "python");
|
|
841
|
+
for (const n of e) {
|
|
842
|
+
if (!ht(n.name)) {
|
|
843
|
+
p(`跳过同步文件:${n.name}(文件名不能包含路径分隔符)`);
|
|
844
|
+
continue;
|
|
845
|
+
}
|
|
846
|
+
const l = await b.writeFile(n.name, n.code);
|
|
847
|
+
l.ok || p(`同步文件失败:${n.name}(${((t = l.error) == null ? void 0 : t.message) ?? "未知错误"})`);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
async function Ie() {
|
|
851
|
+
var d, m, k;
|
|
852
|
+
if (M.value === "loading" || M.value === "running") return;
|
|
853
|
+
const e = y.value.language;
|
|
854
|
+
if (e !== "python") {
|
|
855
|
+
p(`当前文件为 ${pe(e).label},仅支持执行 Python 代码`);
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
ne += 1;
|
|
859
|
+
const t = ne, n = y.value.id;
|
|
860
|
+
ee(void 0), i.clearOutputOnRun && (C.value = []), p("开始执行"), s("runStart");
|
|
861
|
+
const l = te();
|
|
862
|
+
try {
|
|
863
|
+
await mt();
|
|
864
|
+
const E = await b.run(l, {
|
|
865
|
+
stdin: i.stdin,
|
|
866
|
+
onOutput: (re, ae) => {
|
|
867
|
+
Q(re, ae), s("runOutput", { stream: re, text: ae });
|
|
868
|
+
}
|
|
869
|
+
});
|
|
870
|
+
if (t !== ne) return;
|
|
871
|
+
E.interrupted ? p("执行已中断") : E.ok ? p(`执行完成,耗时 ${(E.durationMs / 1e3).toFixed(2)}s`) : (p(`执行出错:${((d = E.error) == null ? void 0 : d.type) ?? "未知错误"}`), (m = E.error) != null && m.traceback && (Q("stderr", E.error.traceback), s("runOutput", { stream: "stderr", text: E.error.traceback })), y.value.id === n && ee((k = E.error) == null ? void 0 : k.line), E.error && s("runError", E.error)), s("runFinish", E);
|
|
872
|
+
} catch (E) {
|
|
873
|
+
if (t !== ne) return;
|
|
874
|
+
const re = E instanceof Error ? E.message : String(E);
|
|
875
|
+
p(`执行失败:${re}`);
|
|
876
|
+
const ae = { type: "RuntimeError", message: re, traceback: "" };
|
|
877
|
+
s("runError", ae), s("runFinish", { ok: !1, durationMs: 0, error: ae });
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
async function yt() {
|
|
881
|
+
var t;
|
|
882
|
+
if (G) return;
|
|
883
|
+
const e = i.preloadPackages;
|
|
884
|
+
if (e.length) {
|
|
885
|
+
G = !0, p(`正在安装预置包:${e.join(", ")}`);
|
|
886
|
+
try {
|
|
887
|
+
const n = await b.loadPackages(e, {
|
|
888
|
+
onOutput: (l, d) => Q(l, d)
|
|
889
|
+
});
|
|
890
|
+
p(
|
|
891
|
+
n.ok ? `预置包安装完成(${(n.durationMs / 1e3).toFixed(1)}s)` : `预置包安装失败:${((t = n.error) == null ? void 0 : t.message) ?? "未知错误"}`
|
|
892
|
+
);
|
|
893
|
+
} catch (n) {
|
|
894
|
+
p(`预置包安装失败:${n instanceof Error ? n.message : String(n)}`);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
jt(() => {
|
|
899
|
+
const e = P.value;
|
|
900
|
+
if (!e) return;
|
|
901
|
+
lt(), J = !0;
|
|
902
|
+
const t = [
|
|
903
|
+
Pe.of(i.theme === "dark" ? [Fe] : []),
|
|
904
|
+
Ce.of([
|
|
905
|
+
Se.readOnly.of(i.readOnly),
|
|
906
|
+
K.editable.of(!i.readOnly)
|
|
907
|
+
]),
|
|
908
|
+
ye.of(ve(y.value.language))
|
|
909
|
+
];
|
|
910
|
+
try {
|
|
911
|
+
o = mn(e, {
|
|
912
|
+
value: y.value.code,
|
|
913
|
+
dark: i.theme === "dark",
|
|
914
|
+
placeholderText: i.placeholder,
|
|
915
|
+
language: me.of(we(y.value.language)),
|
|
916
|
+
extensions: t,
|
|
917
|
+
onDocChange: (n) => {
|
|
918
|
+
y.value.code = n, n !== i.modelValue && s("update:modelValue", n);
|
|
919
|
+
},
|
|
920
|
+
onRunShortcut: Ie
|
|
921
|
+
});
|
|
922
|
+
} catch (n) {
|
|
923
|
+
const l = n instanceof Error ? n.message : String(n);
|
|
924
|
+
console.error("[princess-python-editor] 编辑器初始化失败", n), p(`编辑器初始化失败:${l}`), e.innerHTML = "";
|
|
925
|
+
const d = document.createElement("div");
|
|
926
|
+
d.className = "vpe-init-error", d.textContent = `编辑器初始化失败:${l}`, e.appendChild(d);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
i.autofocus && o.focus(), O = b.on("status", (n, l) => {
|
|
930
|
+
M.value = n, n === "ready" && (l && p(`Pyodide ${l} 就绪`), s("ready"), o && !i.disableLint && y.value.language === "python" && Xt(o)), s("statusChange", n);
|
|
931
|
+
}), U = b.on("initError", (n) => {
|
|
932
|
+
p(`Pyodide 加载失败:${n}`), s("loadError", n);
|
|
933
|
+
}), M.value = b.status, i.preloadPackages.length ? b.waitReady().then(() => yt()).catch((n) => p(n.message)) : i.preload && b.waitReady().catch((n) => p(n.message));
|
|
934
|
+
}), _t(() => {
|
|
935
|
+
O == null || O(), U == null || U(), window.clearTimeout(X), window.clearTimeout(Z), Oe(), o == null || o.destroy(), o = null;
|
|
936
|
+
}), V(
|
|
937
|
+
() => i.modelValue,
|
|
938
|
+
() => ct()
|
|
939
|
+
), V(
|
|
940
|
+
() => i.fileName,
|
|
941
|
+
(e) => {
|
|
942
|
+
e && e !== y.value.name && (A.value = e);
|
|
943
|
+
}
|
|
944
|
+
), V(
|
|
945
|
+
() => i.language,
|
|
946
|
+
(e) => {
|
|
947
|
+
e !== y.value.language && $e(e);
|
|
948
|
+
}
|
|
949
|
+
), V(
|
|
950
|
+
() => i.theme,
|
|
951
|
+
(e) => {
|
|
952
|
+
o == null || o.dispatch({ effects: Pe.reconfigure(e === "dark" ? [Fe] : []) });
|
|
953
|
+
}
|
|
954
|
+
), V(
|
|
955
|
+
() => i.readOnly,
|
|
956
|
+
(e) => {
|
|
957
|
+
o == null || o.dispatch({
|
|
958
|
+
effects: Ce.reconfigure([Se.readOnly.of(e), K.editable.of(!e)])
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
), V(
|
|
962
|
+
() => C.value.length,
|
|
963
|
+
async () => {
|
|
964
|
+
await Ae();
|
|
965
|
+
const e = w.value;
|
|
966
|
+
e && (e.scrollTop = e.scrollHeight);
|
|
967
|
+
}
|
|
968
|
+
), V([f, _], () => ut(), { deep: !0 });
|
|
969
|
+
function Me() {
|
|
970
|
+
Ie();
|
|
971
|
+
}
|
|
972
|
+
function Re() {
|
|
973
|
+
M.value === "running" && (ne += 1, b.interrupt(), p("执行已中断"));
|
|
974
|
+
}
|
|
975
|
+
function He() {
|
|
976
|
+
C.value = [];
|
|
977
|
+
}
|
|
978
|
+
function ke() {
|
|
979
|
+
if (i.readOnly) return;
|
|
980
|
+
const e = y.value.language, t = le(pt(e), e, "");
|
|
981
|
+
f.value.push(t), q(t.id), s("newFile", Y(t));
|
|
982
|
+
}
|
|
983
|
+
function q(e) {
|
|
984
|
+
if (e === _.value) {
|
|
985
|
+
ue();
|
|
986
|
+
return;
|
|
987
|
+
}
|
|
988
|
+
const t = f.value.find((n) => n.id === e);
|
|
989
|
+
t && (_.value = e, Ne(t));
|
|
990
|
+
}
|
|
991
|
+
function Te(e) {
|
|
992
|
+
if (f.value.length <= 1) return;
|
|
993
|
+
const t = f.value.findIndex((d) => d.id === e);
|
|
994
|
+
if (t === -1) return;
|
|
995
|
+
const n = f.value.splice(t, 1)[0];
|
|
996
|
+
if (!n || (s("fileClose", Y(n)), e !== _.value)) return;
|
|
997
|
+
const l = f.value[Math.min(t, f.value.length - 1)];
|
|
998
|
+
l && (_.value = l.id, Ne(l));
|
|
999
|
+
}
|
|
1000
|
+
function Ne(e) {
|
|
1001
|
+
ee(void 0), ge(e.code), o == null || o.dispatch({
|
|
1002
|
+
effects: [
|
|
1003
|
+
me.reconfigure(we(e.language)),
|
|
1004
|
+
ye.reconfigure(ve(e.language))
|
|
1005
|
+
]
|
|
1006
|
+
}), s("update:fileName", e.name), s("update:language", e.language), s("fileSwitch", Y(e)), ue();
|
|
1007
|
+
}
|
|
1008
|
+
function je() {
|
|
1009
|
+
i.readOnly || (ee(void 0), ge(""), ue());
|
|
1010
|
+
}
|
|
1011
|
+
async function _e() {
|
|
1012
|
+
var n;
|
|
1013
|
+
const e = te();
|
|
1014
|
+
let t = !1;
|
|
1015
|
+
try {
|
|
1016
|
+
(n = navigator.clipboard) != null && n.writeText && (await navigator.clipboard.writeText(e), t = !0);
|
|
1017
|
+
} catch {
|
|
1018
|
+
t = !1;
|
|
1019
|
+
}
|
|
1020
|
+
return t || (t = gt(e)), t ? (T.value = !0, window.clearTimeout(X), X = window.setTimeout(() => {
|
|
1021
|
+
T.value = !1;
|
|
1022
|
+
}, 1600), s("copy", e), !0) : (p("复制失败,请手动选择代码复制"), !1);
|
|
1023
|
+
}
|
|
1024
|
+
function gt(e) {
|
|
1025
|
+
const t = document.createElement("textarea");
|
|
1026
|
+
t.value = e, t.setAttribute("readonly", ""), t.style.position = "fixed", t.style.top = "0", t.style.left = "-9999px", t.style.opacity = "0", document.body.appendChild(t), t.select(), t.setSelectionRange(0, t.value.length);
|
|
1027
|
+
let n = !1;
|
|
1028
|
+
try {
|
|
1029
|
+
n = document.execCommand("copy");
|
|
1030
|
+
} catch {
|
|
1031
|
+
n = !1;
|
|
1032
|
+
}
|
|
1033
|
+
return t.remove(), n;
|
|
1034
|
+
}
|
|
1035
|
+
function De() {
|
|
1036
|
+
const e = y.value, t = Le(e.name, e.language);
|
|
1037
|
+
A.value = t;
|
|
1038
|
+
const n = new Blob([te()], {
|
|
1039
|
+
type: e.language === "python" ? "text/x-python;charset=utf-8" : "text/plain;charset=utf-8"
|
|
1040
|
+
}), l = URL.createObjectURL(n), d = document.createElement("a");
|
|
1041
|
+
return d.href = l, d.download = t, d.style.display = "none", document.body.appendChild(d), d.click(), d.remove(), window.setTimeout(() => URL.revokeObjectURL(l), 1e3), s("download", t), t;
|
|
1042
|
+
}
|
|
1043
|
+
function vt() {
|
|
1044
|
+
D.value = !D.value, D.value && Ue();
|
|
1045
|
+
}
|
|
1046
|
+
async function Ue() {
|
|
1047
|
+
var e;
|
|
1048
|
+
if (!N.value) {
|
|
1049
|
+
N.value = !0;
|
|
1050
|
+
try {
|
|
1051
|
+
const t = await b.listFiles(".");
|
|
1052
|
+
if (!t.ok) {
|
|
1053
|
+
j.value = [], p(`读取虚拟文件失败:${((e = t.error) == null ? void 0 : e.message) ?? "未知错误"}`);
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
j.value = t.entries ?? [];
|
|
1057
|
+
} catch (t) {
|
|
1058
|
+
j.value = [], p(`读取虚拟文件失败:${t instanceof Error ? t.message : String(t)}`);
|
|
1059
|
+
} finally {
|
|
1060
|
+
N.value = !1;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
function kt(e) {
|
|
1065
|
+
switch (e.slice(e.lastIndexOf(".") + 1).toLowerCase()) {
|
|
1066
|
+
case "csv":
|
|
1067
|
+
return "text/csv";
|
|
1068
|
+
case "json":
|
|
1069
|
+
return "application/json";
|
|
1070
|
+
case "html":
|
|
1071
|
+
case "htm":
|
|
1072
|
+
return "text/html";
|
|
1073
|
+
case "md":
|
|
1074
|
+
return "text/markdown";
|
|
1075
|
+
case "py":
|
|
1076
|
+
return "text/x-python";
|
|
1077
|
+
default:
|
|
1078
|
+
return "text/plain";
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
function bt(e) {
|
|
1082
|
+
return e < 1024 ? `${e} B` : e < 1024 * 1024 ? `${(e / 1024).toFixed(1)} KB` : `${(e / 1024 / 1024).toFixed(1)} MB`;
|
|
1083
|
+
}
|
|
1084
|
+
async function Ve(e) {
|
|
1085
|
+
var t;
|
|
1086
|
+
try {
|
|
1087
|
+
const n = await b.readFile(e);
|
|
1088
|
+
if (!n.ok || n.content === void 0)
|
|
1089
|
+
return p(`下载失败:${e}(${((t = n.error) == null ? void 0 : t.message) ?? "未知错误"})`), null;
|
|
1090
|
+
const l = e.slice(e.lastIndexOf("/") + 1) || "download.txt", d = new Blob([n.content], {
|
|
1091
|
+
type: `${kt(l)};charset=utf-8`
|
|
1092
|
+
}), m = URL.createObjectURL(d), k = document.createElement("a");
|
|
1093
|
+
return k.href = m, k.download = l, k.style.display = "none", document.body.appendChild(k), k.click(), k.remove(), window.setTimeout(() => URL.revokeObjectURL(m), 1e3), s("fsDownload", { path: n.path ?? e, name: l }), p(`已下载虚拟文件:${l}`), l;
|
|
1094
|
+
} catch (n) {
|
|
1095
|
+
return p(`下载失败:${n instanceof Error ? n.message : String(n)}`), null;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
function wt(e, t) {
|
|
1099
|
+
return b.writeFile(e, t);
|
|
1100
|
+
}
|
|
1101
|
+
function xt(e) {
|
|
1102
|
+
return b.readFile(e);
|
|
1103
|
+
}
|
|
1104
|
+
function St(e = ".") {
|
|
1105
|
+
return b.listFiles(e);
|
|
1106
|
+
}
|
|
1107
|
+
function Be() {
|
|
1108
|
+
var e;
|
|
1109
|
+
(e = H.value) == null || e.click();
|
|
1110
|
+
}
|
|
1111
|
+
async function Et(e) {
|
|
1112
|
+
var l;
|
|
1113
|
+
const t = e.target, n = (l = t.files) == null ? void 0 : l[0];
|
|
1114
|
+
if (t.value = "", !!n) {
|
|
1115
|
+
if (n.size > 2 * 1024 * 1024) {
|
|
1116
|
+
p(`文件过大:${(n.size / 1024 / 1024).toFixed(1)} MB(最多 2 MB)`);
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1119
|
+
try {
|
|
1120
|
+
const d = await n.text(), m = bn(n.name), k = le(ft(n.name), m, d);
|
|
1121
|
+
f.value.push(k), q(k.id), p(`已打开文件:${k.name}`), s("fileOpen", { name: k.name, code: d, language: m });
|
|
1122
|
+
} catch (d) {
|
|
1123
|
+
const m = d instanceof Error ? d.message : String(d);
|
|
1124
|
+
p(`读取文件失败:${m}`);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
function Ft() {
|
|
1129
|
+
A.value = Le(A.value, y.value.language);
|
|
1130
|
+
}
|
|
1131
|
+
function Pt(e) {
|
|
1132
|
+
e.key === "Enter" && (e.preventDefault(), e.target.blur());
|
|
1133
|
+
}
|
|
1134
|
+
function Ct(e) {
|
|
1135
|
+
$e(e.target.value);
|
|
1136
|
+
}
|
|
1137
|
+
async function ze(e) {
|
|
1138
|
+
var t, n;
|
|
1139
|
+
await Ae(), (n = (t = I.value) == null ? void 0 : t.querySelector(`[data-file-id="${e}"]`)) == null || n.focus();
|
|
1140
|
+
}
|
|
1141
|
+
function Ot(e, t) {
|
|
1142
|
+
if (e.target !== e.currentTarget) return;
|
|
1143
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
1144
|
+
e.preventDefault(), q(t.id), ze(t.id);
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
|
|
1148
|
+
e.preventDefault();
|
|
1149
|
+
const n = f.value.findIndex((m) => m.id === t.id);
|
|
1150
|
+
if (n < 0) return;
|
|
1151
|
+
const l = e.key === "ArrowLeft" ? -1 : 1, d = f.value[(n + l + f.value.length) % f.value.length];
|
|
1152
|
+
d && (q(d.id), ze(d.id));
|
|
1153
|
+
}
|
|
1154
|
+
function ue() {
|
|
1155
|
+
o == null || o.focus();
|
|
1156
|
+
}
|
|
1157
|
+
async function $t(e) {
|
|
1158
|
+
var n;
|
|
1159
|
+
p(`正在安装:${e.join(", ")}`);
|
|
1160
|
+
let t;
|
|
1161
|
+
try {
|
|
1162
|
+
t = await b.loadPackages(e, {
|
|
1163
|
+
onOutput: (l, d) => Q(l, d)
|
|
1164
|
+
});
|
|
1165
|
+
} catch (l) {
|
|
1166
|
+
const d = l instanceof Error ? l.message : String(l);
|
|
1167
|
+
return p(`安装失败:${d}`), { ok: !1, durationMs: 0, error: { type: "RuntimeError", message: d } };
|
|
1168
|
+
}
|
|
1169
|
+
return p(t.ok ? "安装完成" : `安装失败:${((n = t.error) == null ? void 0 : n.message) ?? "未知错误"}`), t;
|
|
1170
|
+
}
|
|
1171
|
+
function Lt() {
|
|
1172
|
+
return b.checkSyntax(te());
|
|
1173
|
+
}
|
|
1174
|
+
function It() {
|
|
1175
|
+
return o;
|
|
1176
|
+
}
|
|
1177
|
+
function Mt() {
|
|
1178
|
+
return f.value.map(Y);
|
|
1179
|
+
}
|
|
1180
|
+
function Rt() {
|
|
1181
|
+
return Y(y.value);
|
|
1182
|
+
}
|
|
1183
|
+
return r({
|
|
1184
|
+
run: Me,
|
|
1185
|
+
stop: Re,
|
|
1186
|
+
clearOutput: He,
|
|
1187
|
+
clearCode: je,
|
|
1188
|
+
newFile: ke,
|
|
1189
|
+
switchFile: q,
|
|
1190
|
+
closeFile: Te,
|
|
1191
|
+
copyCode: _e,
|
|
1192
|
+
download: De,
|
|
1193
|
+
openFile: Be,
|
|
1194
|
+
focus: ue,
|
|
1195
|
+
install: $t,
|
|
1196
|
+
checkSyntax: Lt,
|
|
1197
|
+
getEditorView: It,
|
|
1198
|
+
getCode: te,
|
|
1199
|
+
setCode: ge,
|
|
1200
|
+
getFiles: Mt,
|
|
1201
|
+
getActiveFile: Rt,
|
|
1202
|
+
writeFile: wt,
|
|
1203
|
+
readFile: xt,
|
|
1204
|
+
listFiles: St,
|
|
1205
|
+
downloadFile: Ve,
|
|
1206
|
+
clearPersistedState: dt
|
|
1207
|
+
}), (e, t) => (g(), v("div", {
|
|
1208
|
+
class: B(["vpe-root", { "vpe-dark": u.theme === "dark" }]),
|
|
1209
|
+
style: qe(rt.value)
|
|
1210
|
+
}, [
|
|
1211
|
+
u.showToolbar ? (g(), v("div", On, [
|
|
1212
|
+
M.value !== "running" ? (g(), v("button", {
|
|
1213
|
+
key: 0,
|
|
1214
|
+
class: "vpe-btn vpe-btn--primary",
|
|
1215
|
+
type: "button",
|
|
1216
|
+
title: y.value.language === "python" ? "运行 (Ctrl+Enter)" : "仅支持执行 Python 文件",
|
|
1217
|
+
disabled: M.value === "loading" || y.value.language !== "python",
|
|
1218
|
+
onClick: Me
|
|
1219
|
+
}, [
|
|
1220
|
+
$(S(wn)),
|
|
1221
|
+
t[2] || (t[2] = c("span", null, "运行", -1))
|
|
1222
|
+
], 8, $n)) : (g(), v("button", {
|
|
1223
|
+
key: 1,
|
|
1224
|
+
class: "vpe-btn vpe-btn--stop",
|
|
1225
|
+
type: "button",
|
|
1226
|
+
title: "中断执行",
|
|
1227
|
+
onClick: Re
|
|
1228
|
+
}, [
|
|
1229
|
+
$(S(xn)),
|
|
1230
|
+
t[3] || (t[3] = c("span", null, "停止", -1))
|
|
1231
|
+
])),
|
|
1232
|
+
t[6] || (t[6] = c("span", {
|
|
1233
|
+
class: "vpe-divider",
|
|
1234
|
+
"aria-hidden": "true"
|
|
1235
|
+
}, null, -1)),
|
|
1236
|
+
c("button", {
|
|
1237
|
+
class: "vpe-btn",
|
|
1238
|
+
type: "button",
|
|
1239
|
+
title: "新建文件",
|
|
1240
|
+
"aria-label": "新建文件",
|
|
1241
|
+
disabled: u.readOnly,
|
|
1242
|
+
onClick: ke
|
|
1243
|
+
}, [
|
|
1244
|
+
$(S(Sn))
|
|
1245
|
+
], 8, Ln),
|
|
1246
|
+
c("button", {
|
|
1247
|
+
class: "vpe-btn",
|
|
1248
|
+
type: "button",
|
|
1249
|
+
title: "打开本地文件",
|
|
1250
|
+
"aria-label": "打开本地文件",
|
|
1251
|
+
disabled: u.readOnly,
|
|
1252
|
+
onClick: Be
|
|
1253
|
+
}, [
|
|
1254
|
+
$(S(En))
|
|
1255
|
+
], 8, In),
|
|
1256
|
+
c("button", {
|
|
1257
|
+
class: "vpe-btn",
|
|
1258
|
+
type: "button",
|
|
1259
|
+
title: "下载当前文件",
|
|
1260
|
+
"aria-label": "下载当前文件",
|
|
1261
|
+
onClick: De
|
|
1262
|
+
}, [
|
|
1263
|
+
$(S(Xe))
|
|
1264
|
+
]),
|
|
1265
|
+
c("button", {
|
|
1266
|
+
class: "vpe-btn",
|
|
1267
|
+
type: "button",
|
|
1268
|
+
title: "虚拟文件(下载 Python 生成的文件)",
|
|
1269
|
+
"aria-label": "虚拟文件",
|
|
1270
|
+
onClick: vt
|
|
1271
|
+
}, [
|
|
1272
|
+
$(S(Ge)),
|
|
1273
|
+
t[4] || (t[4] = c("span", null, "虚拟文件", -1))
|
|
1274
|
+
]),
|
|
1275
|
+
c("button", {
|
|
1276
|
+
class: B(["vpe-btn", { "vpe-btn--copied": T.value }]),
|
|
1277
|
+
type: "button",
|
|
1278
|
+
title: T.value ? "已复制" : "复制代码",
|
|
1279
|
+
"aria-label": T.value ? "已复制" : "复制代码",
|
|
1280
|
+
onClick: _e
|
|
1281
|
+
}, [
|
|
1282
|
+
T.value ? (g(), We(S(Pn), { key: 0 })) : (g(), We(S(Fn), { key: 1 }))
|
|
1283
|
+
], 10, Mn),
|
|
1284
|
+
c("button", {
|
|
1285
|
+
class: "vpe-btn",
|
|
1286
|
+
type: "button",
|
|
1287
|
+
title: "清空当前文件",
|
|
1288
|
+
"aria-label": "清空当前文件",
|
|
1289
|
+
disabled: u.readOnly,
|
|
1290
|
+
onClick: je
|
|
1291
|
+
}, [
|
|
1292
|
+
$(S(Ze))
|
|
1293
|
+
], 8, Rn),
|
|
1294
|
+
t[7] || (t[7] = c("span", {
|
|
1295
|
+
class: "vpe-divider",
|
|
1296
|
+
"aria-hidden": "true"
|
|
1297
|
+
}, null, -1)),
|
|
1298
|
+
c("label", Hn, [
|
|
1299
|
+
$(S(Ge)),
|
|
1300
|
+
Dt(c("input", {
|
|
1301
|
+
"onUpdate:modelValue": t[0] || (t[0] = (n) => A.value = n),
|
|
1302
|
+
class: "vpe-file-input",
|
|
1303
|
+
type: "text",
|
|
1304
|
+
spellcheck: "false",
|
|
1305
|
+
"aria-label": "文件名",
|
|
1306
|
+
onBlur: Ft,
|
|
1307
|
+
onKeydown: Pt
|
|
1308
|
+
}, null, 544), [
|
|
1309
|
+
[Ut, A.value]
|
|
1310
|
+
])
|
|
1311
|
+
]),
|
|
1312
|
+
t[8] || (t[8] = c("span", {
|
|
1313
|
+
class: "vpe-spacer",
|
|
1314
|
+
"aria-hidden": "true"
|
|
1315
|
+
}, null, -1)),
|
|
1316
|
+
c("span", {
|
|
1317
|
+
class: B(["vpe-status", `vpe-status--${M.value}`])
|
|
1318
|
+
}, [
|
|
1319
|
+
t[5] || (t[5] = c("span", {
|
|
1320
|
+
class: "vpe-status-dot",
|
|
1321
|
+
"aria-hidden": "true"
|
|
1322
|
+
}, null, -1)),
|
|
1323
|
+
c("span", null, W(it.value), 1)
|
|
1324
|
+
], 2),
|
|
1325
|
+
c("select", {
|
|
1326
|
+
class: "vpe-lang",
|
|
1327
|
+
value: y.value.language,
|
|
1328
|
+
title: "编辑器语言",
|
|
1329
|
+
"aria-label": "编辑器语言",
|
|
1330
|
+
disabled: u.readOnly,
|
|
1331
|
+
onChange: Ct
|
|
1332
|
+
}, [
|
|
1333
|
+
(g(!0), v(se, null, de(S(fe), (n) => (g(), v("option", {
|
|
1334
|
+
key: n.value,
|
|
1335
|
+
value: n.value
|
|
1336
|
+
}, W(n.label), 9, Nn))), 128))
|
|
1337
|
+
], 40, Tn)
|
|
1338
|
+
])) : z("", !0),
|
|
1339
|
+
u.showTabs ? (g(), v("div", {
|
|
1340
|
+
key: 1,
|
|
1341
|
+
ref_key: "tabsEl",
|
|
1342
|
+
ref: I,
|
|
1343
|
+
class: "vpe-tabs",
|
|
1344
|
+
role: "tablist",
|
|
1345
|
+
"aria-label": "文件"
|
|
1346
|
+
}, [
|
|
1347
|
+
(g(!0), v(se, null, de(f.value, (n) => (g(), v("div", {
|
|
1348
|
+
key: n.id,
|
|
1349
|
+
class: B(["vpe-tab", { "vpe-tab--active": n.id === y.value.id }]),
|
|
1350
|
+
role: "tab",
|
|
1351
|
+
tabindex: "0",
|
|
1352
|
+
"aria-selected": n.id === y.value.id,
|
|
1353
|
+
"data-file-id": n.id,
|
|
1354
|
+
title: n.name,
|
|
1355
|
+
onClick: (l) => q(n.id),
|
|
1356
|
+
onKeydown: (l) => Ot(l, n)
|
|
1357
|
+
}, [
|
|
1358
|
+
c("span", {
|
|
1359
|
+
class: B(["vpe-tab-dot", `vpe-tab-dot--${n.language}`]),
|
|
1360
|
+
"aria-hidden": "true"
|
|
1361
|
+
}, null, 2),
|
|
1362
|
+
c("span", _n, W(n.name), 1),
|
|
1363
|
+
f.value.length > 1 ? (g(), v("button", {
|
|
1364
|
+
key: 0,
|
|
1365
|
+
class: "vpe-tab-close",
|
|
1366
|
+
type: "button",
|
|
1367
|
+
title: `关闭 ${n.name}`,
|
|
1368
|
+
"aria-label": `关闭 ${n.name}`,
|
|
1369
|
+
disabled: u.readOnly,
|
|
1370
|
+
onClick: Vt((l) => Te(n.id), ["stop"])
|
|
1371
|
+
}, [
|
|
1372
|
+
$(S(Ye))
|
|
1373
|
+
], 8, Dn)) : z("", !0)
|
|
1374
|
+
], 42, jn))), 128)),
|
|
1375
|
+
c("button", {
|
|
1376
|
+
class: "vpe-tabs-add",
|
|
1377
|
+
type: "button",
|
|
1378
|
+
title: "新建文件",
|
|
1379
|
+
"aria-label": "新建文件",
|
|
1380
|
+
disabled: u.readOnly,
|
|
1381
|
+
onClick: ke
|
|
1382
|
+
}, [
|
|
1383
|
+
$(S(Cn))
|
|
1384
|
+
], 8, Un)
|
|
1385
|
+
], 512)) : z("", !0),
|
|
1386
|
+
c("input", {
|
|
1387
|
+
ref_key: "fileInputEl",
|
|
1388
|
+
ref: H,
|
|
1389
|
+
class: "vpe-file-hidden",
|
|
1390
|
+
type: "file",
|
|
1391
|
+
accept: ".py,.pyw,.sql,.js,.mjs,.cjs,.jsx,.ts,.json,.txt,text/x-python,text/plain,application/json",
|
|
1392
|
+
onChange: Et
|
|
1393
|
+
}, null, 544),
|
|
1394
|
+
c("div", {
|
|
1395
|
+
ref_key: "editorEl",
|
|
1396
|
+
ref: P,
|
|
1397
|
+
class: "vpe-editor"
|
|
1398
|
+
}, null, 512),
|
|
1399
|
+
u.showOutput ? (g(), v("div", {
|
|
1400
|
+
key: 2,
|
|
1401
|
+
class: "vpe-output",
|
|
1402
|
+
style: qe({ height: at.value })
|
|
1403
|
+
}, [
|
|
1404
|
+
c("div", Vn, [
|
|
1405
|
+
t[9] || (t[9] = c("span", null, "输出", -1)),
|
|
1406
|
+
c("button", {
|
|
1407
|
+
class: "vpe-btn vpe-btn--sm",
|
|
1408
|
+
type: "button",
|
|
1409
|
+
title: "清空输出",
|
|
1410
|
+
onClick: He
|
|
1411
|
+
}, [
|
|
1412
|
+
$(S(Ze))
|
|
1413
|
+
])
|
|
1414
|
+
]),
|
|
1415
|
+
c("pre", {
|
|
1416
|
+
ref_key: "outputEl",
|
|
1417
|
+
ref: w,
|
|
1418
|
+
class: "vpe-output-body"
|
|
1419
|
+
}, [
|
|
1420
|
+
C.value.length ? (g(!0), v(se, { key: 0 }, de(C.value, (n) => (g(), v("span", {
|
|
1421
|
+
key: n.id,
|
|
1422
|
+
class: B(["vpe-line", `vpe-line--${n.stream}`])
|
|
1423
|
+
}, W(n.text) + `
|
|
1424
|
+
`, 3))), 128)) : (g(), v("span", Bn, "暂无输出,点击“运行”或按 Ctrl+Enter 执行代码"))
|
|
1425
|
+
], 512)
|
|
1426
|
+
], 4)) : z("", !0),
|
|
1427
|
+
D.value ? (g(), v("div", zn, [
|
|
1428
|
+
c("div", Kn, [
|
|
1429
|
+
t[10] || (t[10] = c("span", { class: "vpe-fs-title" }, "虚拟文件", -1)),
|
|
1430
|
+
t[11] || (t[11] = c("span", {
|
|
1431
|
+
class: "vpe-spacer",
|
|
1432
|
+
"aria-hidden": "true"
|
|
1433
|
+
}, null, -1)),
|
|
1434
|
+
c("button", {
|
|
1435
|
+
class: "vpe-btn vpe-btn--sm",
|
|
1436
|
+
type: "button",
|
|
1437
|
+
title: "刷新文件列表",
|
|
1438
|
+
disabled: N.value,
|
|
1439
|
+
onClick: Ue
|
|
1440
|
+
}, " 刷新 ", 8, An),
|
|
1441
|
+
c("button", {
|
|
1442
|
+
class: "vpe-btn vpe-btn--sm",
|
|
1443
|
+
type: "button",
|
|
1444
|
+
title: "关闭虚拟文件面板",
|
|
1445
|
+
"aria-label": "关闭虚拟文件面板",
|
|
1446
|
+
onClick: t[1] || (t[1] = (n) => D.value = !1)
|
|
1447
|
+
}, [
|
|
1448
|
+
$(S(Ye))
|
|
1449
|
+
])
|
|
1450
|
+
]),
|
|
1451
|
+
c("div", qn, [
|
|
1452
|
+
N.value ? (g(), v("div", Wn, "正在读取虚拟文件…")) : (g(), v(se, { key: 1 }, [
|
|
1453
|
+
j.value.length ? z("", !0) : (g(), v("div", Jn, " 暂无文件。运行 Python 代码创建文件后再刷新。 ")),
|
|
1454
|
+
(g(!0), v(se, null, de(j.value, (n) => (g(), v("div", {
|
|
1455
|
+
key: n.path,
|
|
1456
|
+
class: B(["vpe-fs-item", { "vpe-fs-item--dir": n.isDirectory }])
|
|
1457
|
+
}, [
|
|
1458
|
+
c("span", {
|
|
1459
|
+
class: "vpe-fs-name",
|
|
1460
|
+
title: n.path
|
|
1461
|
+
}, W(n.path), 9, Zn),
|
|
1462
|
+
c("span", Gn, W(n.isDirectory ? "目录" : bt(n.size)), 1),
|
|
1463
|
+
n.isDirectory ? z("", !0) : (g(), v("button", {
|
|
1464
|
+
key: 0,
|
|
1465
|
+
class: "vpe-btn vpe-btn--sm",
|
|
1466
|
+
type: "button",
|
|
1467
|
+
title: `下载 ${n.name}`,
|
|
1468
|
+
"aria-label": `下载 ${n.name}`,
|
|
1469
|
+
onClick: (l) => Ve(n.path)
|
|
1470
|
+
}, [
|
|
1471
|
+
$(S(Xe))
|
|
1472
|
+
], 8, Xn))
|
|
1473
|
+
], 2))), 128))
|
|
1474
|
+
], 64))
|
|
1475
|
+
])
|
|
1476
|
+
])) : z("", !0)
|
|
1477
|
+
], 6));
|
|
1478
|
+
}
|
|
1479
|
+
});
|
|
1480
|
+
function pr(u = {}) {
|
|
1481
|
+
var G;
|
|
1482
|
+
const r = nt(u.pyodideUrl ? { pyodideUrl: u.pyodideUrl } : void 0), a = F(r.status), i = F(""), s = F([]);
|
|
1483
|
+
let P = 0;
|
|
1484
|
+
const w = (o, h) => {
|
|
1485
|
+
s.value.push({ id: ++P, stream: o, text: h });
|
|
1486
|
+
}, I = r.on("status", (o, h) => {
|
|
1487
|
+
a.value = o, h && (i.value = h);
|
|
1488
|
+
}), H = r.on("initError", (o) => {
|
|
1489
|
+
w("system", `[pyodide] 加载失败:${o}`);
|
|
1490
|
+
});
|
|
1491
|
+
Bt(() => {
|
|
1492
|
+
I(), H();
|
|
1493
|
+
});
|
|
1494
|
+
const C = async (o, h = {}) => {
|
|
1495
|
+
var x;
|
|
1496
|
+
try {
|
|
1497
|
+
const O = await r.run(o, {
|
|
1498
|
+
stdin: h.stdin,
|
|
1499
|
+
onOutput: (U, X) => w(U, X)
|
|
1500
|
+
});
|
|
1501
|
+
return !O.ok && ((x = O.error) != null && x.traceback) && w("stderr", O.error.traceback), O;
|
|
1502
|
+
} catch (O) {
|
|
1503
|
+
return {
|
|
1504
|
+
ok: !1,
|
|
1505
|
+
durationMs: 0,
|
|
1506
|
+
error: { type: "RuntimeError", message: O instanceof Error ? O.message : String(O), traceback: "" }
|
|
1507
|
+
};
|
|
1508
|
+
}
|
|
1509
|
+
}, M = async (o, h) => {
|
|
1510
|
+
try {
|
|
1511
|
+
return await r.writeFile(o, h);
|
|
1512
|
+
} catch (x) {
|
|
1513
|
+
return { ok: !1, error: { type: "RuntimeError", message: x instanceof Error ? x.message : String(x), traceback: "" } };
|
|
1514
|
+
}
|
|
1515
|
+
}, T = async (o) => {
|
|
1516
|
+
try {
|
|
1517
|
+
return await r.readFile(o);
|
|
1518
|
+
} catch (h) {
|
|
1519
|
+
return { ok: !1, error: { type: "RuntimeError", message: h instanceof Error ? h.message : String(h), traceback: "" } };
|
|
1520
|
+
}
|
|
1521
|
+
}, D = async (o = ".") => {
|
|
1522
|
+
try {
|
|
1523
|
+
return await r.listFiles(o);
|
|
1524
|
+
} catch (h) {
|
|
1525
|
+
return { ok: !1, error: { type: "RuntimeError", message: h instanceof Error ? h.message : String(h), traceback: "" } };
|
|
1526
|
+
}
|
|
1527
|
+
}, N = async (o) => {
|
|
1528
|
+
try {
|
|
1529
|
+
return await r.loadPackages(o, { onOutput: (h, x) => w(h, x) });
|
|
1530
|
+
} catch (h) {
|
|
1531
|
+
return {
|
|
1532
|
+
ok: !1,
|
|
1533
|
+
durationMs: 0,
|
|
1534
|
+
error: { type: "RuntimeError", message: h instanceof Error ? h.message : String(h), traceback: "" }
|
|
1535
|
+
};
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1538
|
+
let j = !1;
|
|
1539
|
+
const J = async () => {
|
|
1540
|
+
var x;
|
|
1541
|
+
if (j) return;
|
|
1542
|
+
const o = u.preloadPackages ?? [];
|
|
1543
|
+
if (!o.length) return;
|
|
1544
|
+
j = !0, w("system", `[pyodide] 正在安装预置包:${o.join(", ")}`);
|
|
1545
|
+
const h = await N(o);
|
|
1546
|
+
w(
|
|
1547
|
+
"system",
|
|
1548
|
+
h.ok ? `[pyodide] 预置包安装完成(${(h.durationMs / 1e3).toFixed(1)}s)` : `[pyodide] 预置包安装失败:${((x = h.error) == null ? void 0 : x.message) ?? "未知错误"}`
|
|
1549
|
+
);
|
|
1550
|
+
};
|
|
1551
|
+
return (u.preload || (((G = u.preloadPackages) == null ? void 0 : G.length) ?? 0) > 0) && r.waitReady().then(() => J()).catch(() => {
|
|
1552
|
+
}), {
|
|
1553
|
+
status: a,
|
|
1554
|
+
version: i,
|
|
1555
|
+
outputs: s,
|
|
1556
|
+
runPython: C,
|
|
1557
|
+
install: N,
|
|
1558
|
+
checkSyntax: (o) => r.checkSyntax(o),
|
|
1559
|
+
interrupt: () => r.interrupt(),
|
|
1560
|
+
clearOutputs: () => {
|
|
1561
|
+
s.value = [];
|
|
1562
|
+
},
|
|
1563
|
+
writeFile: M,
|
|
1564
|
+
readFile: T,
|
|
1565
|
+
listFiles: D,
|
|
1566
|
+
/** 预加载并在就绪后 resolve */
|
|
1567
|
+
ready: () => r.waitReady()
|
|
1568
|
+
};
|
|
1569
|
+
}
|
|
1570
|
+
const fr = Yn;
|
|
1571
|
+
export {
|
|
1572
|
+
tt as DEFAULT_PYODIDE_URL,
|
|
1573
|
+
cn as MONO_FONT_STACK,
|
|
1574
|
+
kn as PyodideRunner,
|
|
1575
|
+
fr as PythonEditor,
|
|
1576
|
+
mn as createEditor,
|
|
1577
|
+
Yn as default,
|
|
1578
|
+
nt as getRunner,
|
|
1579
|
+
yn as makePythonLinter,
|
|
1580
|
+
et as setErrorLines,
|
|
1581
|
+
pr as usePyodide
|
|
1582
|
+
};
|