princess-python-editor 0.0.0-stage → 0.1.0
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.md +354 -2
- package/dist/PythonEditor.vue.d.ts +130 -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 +102 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.es.js +1120 -0
- package/dist/languages.d.ts +15 -0
- package/dist/runtime/PyodideRunner.d.ts +58 -0
- package/dist/runtime/usePyodide.d.ts +35 -0
- package/dist/runtime/workerSource.d.ts +8 -0
- package/dist/style.css +1 -0
- package/dist/types.d.ts +74 -0
- package/package.json +90 -4
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,1120 @@
|
|
|
1
|
+
var at = Object.defineProperty;
|
|
2
|
+
var ot = (s, n, a) => n in s ? at(s, n, { enumerable: !0, configurable: !0, writable: !0, value: a }) : s[n] = a;
|
|
3
|
+
var C = (s, n, a) => ot(s, typeof n != "symbol" ? n + "" : n, a);
|
|
4
|
+
import { h as $e, defineComponent as it, ref as O, computed as W, onMounted as st, onBeforeUnmount as lt, watch as U, nextTick as Ie, openBlock as v, createElementBlock as b, normalizeStyle as Ne, normalizeClass as V, createVNode as $, unref as S, createElementVNode as p, createBlock as Pe, withDirectives as ut, vModelText as dt, toDisplayString as J, Fragment as se, renderList as le, createCommentVNode as G, withModifiers as ct, onScopeDispose as pt } from "vue";
|
|
5
|
+
import { EditorState as ce, StateEffect as ft, StateField as ht, Compartment as X } from "@codemirror/state";
|
|
6
|
+
import { EditorView as R, keymap as Re, lineNumbers as mt, highlightActiveLineGutter as yt, highlightActiveLine as vt, drawSelection as gt, placeholder as kt, Decoration as pe } from "@codemirror/view";
|
|
7
|
+
import { oneDark as fe } from "@codemirror/theme-one-dark";
|
|
8
|
+
import { linter as bt, forceLinting as wt } from "@codemirror/lint";
|
|
9
|
+
import { autocompletion as xt } from "@codemirror/autocomplete";
|
|
10
|
+
import { history as Et, defaultKeymap as St, historyKeymap as Ct, indentWithTab as Ot } from "@codemirror/commands";
|
|
11
|
+
import { indentUnit as Lt } from "@codemirror/language";
|
|
12
|
+
import { python as Te } from "@codemirror/lang-python";
|
|
13
|
+
import { javascript as Ft } from "@codemirror/lang-javascript";
|
|
14
|
+
import { json as Ht } from "@codemirror/lang-json";
|
|
15
|
+
import { sql as Mt } from "@codemirror/lang-sql";
|
|
16
|
+
const $t = "'JetBrains Mono', 'Fira Code', 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace", Ue = ft.define();
|
|
17
|
+
function It(s, n) {
|
|
18
|
+
const a = s.filter((o) => Number.isFinite(o) && o >= 1 && o <= n.lines).sort((o, i) => o - i);
|
|
19
|
+
return pe.set(
|
|
20
|
+
a.map((o) => pe.line({ class: "vpe-error-line" }).range(n.line(o).from))
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const Nt = ht.define({
|
|
24
|
+
create: () => pe.none,
|
|
25
|
+
update: (s, n) => {
|
|
26
|
+
let a = s;
|
|
27
|
+
for (const o of n.effects)
|
|
28
|
+
o.is(Ue) && (a = It(o.value, n.state.doc));
|
|
29
|
+
return n.docChanged && (a = a.map(n.changes)), a;
|
|
30
|
+
},
|
|
31
|
+
provide: (s) => R.decorations.from(s)
|
|
32
|
+
}), Pt = R.theme({
|
|
33
|
+
"&": { height: "100%", fontSize: "13px" },
|
|
34
|
+
".cm-scroller": { fontFamily: $t, 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 Rt(s, n) {
|
|
45
|
+
const a = R.updateListener.of((k) => {
|
|
46
|
+
k.docChanged && n.onDocChange(k.state.doc.toString());
|
|
47
|
+
}), o = Re.of([
|
|
48
|
+
{
|
|
49
|
+
key: "Mod-Enter",
|
|
50
|
+
preventDefault: !0,
|
|
51
|
+
run: () => (n.onRunShortcut(), !0)
|
|
52
|
+
}
|
|
53
|
+
]), i = ce.create({
|
|
54
|
+
doc: n.value,
|
|
55
|
+
extensions: [
|
|
56
|
+
Pt,
|
|
57
|
+
mt(),
|
|
58
|
+
yt(),
|
|
59
|
+
vt(),
|
|
60
|
+
gt(),
|
|
61
|
+
Et(),
|
|
62
|
+
Lt.of(" "),
|
|
63
|
+
n.language ?? Te(),
|
|
64
|
+
xt(),
|
|
65
|
+
R.lineWrapping,
|
|
66
|
+
kt(n.placeholderText),
|
|
67
|
+
Nt,
|
|
68
|
+
o,
|
|
69
|
+
Re.of([...St, ...Ct, Ot]),
|
|
70
|
+
...n.dark ? [fe] : [],
|
|
71
|
+
...n.extensions,
|
|
72
|
+
a
|
|
73
|
+
]
|
|
74
|
+
});
|
|
75
|
+
return new R({ state: i, parent: s });
|
|
76
|
+
}
|
|
77
|
+
function jt(s, n = 600) {
|
|
78
|
+
return bt(
|
|
79
|
+
async (a) => {
|
|
80
|
+
const o = a.state.doc.toString();
|
|
81
|
+
if (!o.trim()) return [];
|
|
82
|
+
let i = null;
|
|
83
|
+
try {
|
|
84
|
+
i = await s.checkSyntax(o);
|
|
85
|
+
} catch {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
if (!i) return [];
|
|
89
|
+
const k = Math.min(Math.max(Math.round(i.line) || 1, 1), a.state.doc.lines), w = a.state.doc.line(k), L = Math.min(Math.max(Math.round(i.column) || 1, 1), w.length + 1), I = w.from + L - 1, x = Math.min(I + 1, w.to);
|
|
90
|
+
return [
|
|
91
|
+
{
|
|
92
|
+
from: I,
|
|
93
|
+
to: x,
|
|
94
|
+
severity: "error",
|
|
95
|
+
message: i.message,
|
|
96
|
+
source: "python-syntax"
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
},
|
|
100
|
+
{ delay: n }
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const Tt = [
|
|
104
|
+
"import ast, json",
|
|
105
|
+
"def _pye_check(src):",
|
|
106
|
+
" try:",
|
|
107
|
+
" ast.parse(src)",
|
|
108
|
+
" return json.dumps({'ok': True})",
|
|
109
|
+
" except SyntaxError as e:",
|
|
110
|
+
" return json.dumps({'ok': False, 'line': int(e.lineno or 1), 'column': int(e.offset or 1), 'message': str(e.msg or 'invalid syntax')})"
|
|
111
|
+
];
|
|
112
|
+
function Ut(s) {
|
|
113
|
+
const n = s.endsWith("/") ? s : s + "/";
|
|
114
|
+
return `
|
|
115
|
+
var INDEX_URL = ${JSON.stringify(n)};
|
|
116
|
+
var CHECK_SOURCE = ${JSON.stringify(Tt.join(`
|
|
117
|
+
`))};
|
|
118
|
+
var pyodide = null;
|
|
119
|
+
var checkFn = null;
|
|
120
|
+
var currentId = 0;
|
|
121
|
+
|
|
122
|
+
function post(msg) { self.postMessage(msg); }
|
|
123
|
+
|
|
124
|
+
function parseError(e) {
|
|
125
|
+
var msg = String((e && e.message) || e);
|
|
126
|
+
var line;
|
|
127
|
+
var re = /File "<exec>", line (\\d+)/g;
|
|
128
|
+
var m;
|
|
129
|
+
while ((m = re.exec(msg)) !== null) { line = Number(m[1]); }
|
|
130
|
+
var lines = msg.trim().split('\\n');
|
|
131
|
+
var last = lines[lines.length - 1];
|
|
132
|
+
var m2 = /^([A-Za-z_][\\w.]*)\\s*(?::\\s*(.*))?$/.exec(last);
|
|
133
|
+
var type = m2 ? m2[1] : 'Error';
|
|
134
|
+
var detail = m2 && m2[2] ? m2[2] : last;
|
|
135
|
+
return { type: type, message: detail, line: line, traceback: msg };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function init() {
|
|
139
|
+
try {
|
|
140
|
+
self.importScripts(INDEX_URL + 'pyodide.js');
|
|
141
|
+
pyodide = await self.loadPyodide({ indexURL: INDEX_URL });
|
|
142
|
+
pyodide.setStdout({ batched: function (s) {
|
|
143
|
+
post({ kind: 'output', runId: currentId, stream: 'stdout', text: s });
|
|
144
|
+
} });
|
|
145
|
+
pyodide.setStderr({ batched: function (s) {
|
|
146
|
+
post({ kind: 'output', runId: currentId, stream: 'stderr', text: s });
|
|
147
|
+
} });
|
|
148
|
+
pyodide.runPython(CHECK_SOURCE);
|
|
149
|
+
checkFn = pyodide.globals.get('_pye_check');
|
|
150
|
+
post({ kind: 'ready', version: pyodide.version });
|
|
151
|
+
} catch (e) {
|
|
152
|
+
post({ kind: 'init-error', message: String((e && e.message) || e) });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
init();
|
|
157
|
+
|
|
158
|
+
self.onmessage = async function (ev) {
|
|
159
|
+
var msg = ev.data;
|
|
160
|
+
if (!msg || !msg.kind) return;
|
|
161
|
+
|
|
162
|
+
if (msg.kind === 'run') {
|
|
163
|
+
currentId = msg.id;
|
|
164
|
+
if (!pyodide) {
|
|
165
|
+
currentId = 0;
|
|
166
|
+
post({ kind: 'result', id: msg.id, ok: false, error: { type: 'RuntimeError', message: 'Pyodide 未初始化', traceback: '' } });
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
var result = await pyodide.runPythonAsync(msg.code);
|
|
171
|
+
var value;
|
|
172
|
+
if (result !== undefined && result !== null) {
|
|
173
|
+
try { value = String(result); } catch (e2) { value = undefined; }
|
|
174
|
+
if (result && typeof result.destroy === 'function') {
|
|
175
|
+
try { result.destroy(); } catch (e3) { /* 忽略释放异常 */ }
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
post({ kind: 'result', id: msg.id, ok: true, value: value });
|
|
179
|
+
} catch (e) {
|
|
180
|
+
post({ kind: 'result', id: msg.id, ok: false, error: parseError(e) });
|
|
181
|
+
}
|
|
182
|
+
currentId = 0;
|
|
183
|
+
|
|
184
|
+
} else if (msg.kind === 'check') {
|
|
185
|
+
currentId = msg.id;
|
|
186
|
+
try {
|
|
187
|
+
var json = checkFn(msg.code);
|
|
188
|
+
var parsed = JSON.parse(json);
|
|
189
|
+
post({ kind: 'checked', id: msg.id, ok: !!parsed.ok, line: parsed.line, column: parsed.column, message: parsed.message });
|
|
190
|
+
} catch (e) {
|
|
191
|
+
// 检查器自身异常时不误报语法错误
|
|
192
|
+
post({ kind: 'checked', id: msg.id, ok: true });
|
|
193
|
+
}
|
|
194
|
+
currentId = 0;
|
|
195
|
+
|
|
196
|
+
} else if (msg.kind === 'install') {
|
|
197
|
+
currentId = msg.id;
|
|
198
|
+
try {
|
|
199
|
+
await pyodide.loadPackage(msg.packages);
|
|
200
|
+
post({ kind: 'installed', id: msg.id, ok: true });
|
|
201
|
+
} catch (e1) {
|
|
202
|
+
try {
|
|
203
|
+
await pyodide.loadPackage('micropip');
|
|
204
|
+
var micropip = pyodide.pyimport('micropip');
|
|
205
|
+
await micropip.install(msg.packages);
|
|
206
|
+
post({ kind: 'installed', id: msg.id, ok: true });
|
|
207
|
+
} catch (e2) {
|
|
208
|
+
post({ kind: 'installed', id: msg.id, ok: false, error: parseError(e2) });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
currentId = 0;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
`;
|
|
215
|
+
}
|
|
216
|
+
const Ve = "https://cdn.jsdelivr.net/pyodide/v0.27.2/full/";
|
|
217
|
+
class Vt {
|
|
218
|
+
constructor(n = {}) {
|
|
219
|
+
C(this, "status", "idle");
|
|
220
|
+
C(this, "version", "");
|
|
221
|
+
C(this, "pyodideUrl");
|
|
222
|
+
C(this, "worker", null);
|
|
223
|
+
C(this, "pending", /* @__PURE__ */ new Map());
|
|
224
|
+
C(this, "seq", 1);
|
|
225
|
+
C(this, "chain", Promise.resolve());
|
|
226
|
+
/** Worker 生命周期代数:interrupt 后递增,用于废弃旧的 waitReady 等待 */
|
|
227
|
+
C(this, "generation", 0);
|
|
228
|
+
C(this, "statusHandlers", /* @__PURE__ */ new Set());
|
|
229
|
+
C(this, "outputHandlers", /* @__PURE__ */ new Set());
|
|
230
|
+
C(this, "initErrorHandlers", /* @__PURE__ */ new Set());
|
|
231
|
+
this.pyodideUrl = n.pyodideUrl || Ve;
|
|
232
|
+
}
|
|
233
|
+
on(n, a) {
|
|
234
|
+
if (n === "status") {
|
|
235
|
+
const i = a;
|
|
236
|
+
return this.statusHandlers.add(i), () => this.statusHandlers.delete(i);
|
|
237
|
+
}
|
|
238
|
+
if (n === "output") {
|
|
239
|
+
const i = a;
|
|
240
|
+
return this.outputHandlers.add(i), () => this.outputHandlers.delete(i);
|
|
241
|
+
}
|
|
242
|
+
const o = a;
|
|
243
|
+
return this.initErrorHandlers.add(o), () => this.initErrorHandlers.delete(o);
|
|
244
|
+
}
|
|
245
|
+
/** 预加载 Pyodide(返回 Promise,在就绪或失败时 settle) */
|
|
246
|
+
waitReady(n = 18e4) {
|
|
247
|
+
const a = this.generation;
|
|
248
|
+
return this.ensureWorker(), this.status === "ready" || this.status === "running" ? Promise.resolve() : new Promise((o, i) => {
|
|
249
|
+
let k = () => {
|
|
250
|
+
}, w = () => {
|
|
251
|
+
};
|
|
252
|
+
const L = () => {
|
|
253
|
+
clearTimeout(I), k(), w();
|
|
254
|
+
}, I = setTimeout(() => {
|
|
255
|
+
L(), i(new Error("Pyodide 加载超时"));
|
|
256
|
+
}, n);
|
|
257
|
+
k = this.on("status", (x) => {
|
|
258
|
+
if (this.generation !== a) {
|
|
259
|
+
L(), i(new Error("执行已被中断"));
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
x === "ready" && (L(), o());
|
|
263
|
+
}), w = this.on("initError", (x) => {
|
|
264
|
+
L(), i(new Error(x));
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/** 执行一段 Python 代码(支持顶层 await);运行器未就绪时先等待初始化完成 */
|
|
269
|
+
async run(n, a) {
|
|
270
|
+
await this.waitReady();
|
|
271
|
+
const o = performance.now();
|
|
272
|
+
this.setStatus("running");
|
|
273
|
+
const i = await this.send(
|
|
274
|
+
{ kind: "run", id: this.seq++, code: n },
|
|
275
|
+
a == null ? void 0 : a.onOutput
|
|
276
|
+
);
|
|
277
|
+
return this.status === "running" && this.setStatus("ready"), {
|
|
278
|
+
ok: i.ok,
|
|
279
|
+
value: i.value,
|
|
280
|
+
error: i.error,
|
|
281
|
+
interrupted: i.interrupted,
|
|
282
|
+
durationMs: performance.now() - o
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/** 语法检查:仅在运行器就绪时可用,否则返回 null(静默跳过) */
|
|
286
|
+
async checkSyntax(n) {
|
|
287
|
+
if (this.status !== "ready" || !n.trim()) return null;
|
|
288
|
+
const a = await this.send({ kind: "check", id: this.seq++, code: n });
|
|
289
|
+
return a.ok ? null : {
|
|
290
|
+
line: a.line ?? 1,
|
|
291
|
+
column: a.column ?? 1,
|
|
292
|
+
message: a.message ?? "语法错误"
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
/** 安装第三方包:先尝试内置 wheel(loadPackage),失败后回退 micropip */
|
|
296
|
+
async loadPackages(n, a) {
|
|
297
|
+
await this.waitReady();
|
|
298
|
+
const o = performance.now();
|
|
299
|
+
this.setStatus("running");
|
|
300
|
+
const i = await this.send(
|
|
301
|
+
{ kind: "install", id: this.seq++, packages: n },
|
|
302
|
+
a == null ? void 0 : a.onOutput
|
|
303
|
+
);
|
|
304
|
+
return this.status === "running" && this.setStatus("ready"), { ok: i.ok, error: i.error, durationMs: performance.now() - o };
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* 中断当前执行:terminate Worker(Pyodide 无法真正中断同步计算),
|
|
308
|
+
* 所有未决请求以 interrupted 结果结束;下次请求时自动重建 Worker。
|
|
309
|
+
*/
|
|
310
|
+
interrupt() {
|
|
311
|
+
if (!this.worker) return;
|
|
312
|
+
this.generation += 1, this.worker.terminate(), this.worker = null;
|
|
313
|
+
const n = {
|
|
314
|
+
ok: !1,
|
|
315
|
+
interrupted: !0,
|
|
316
|
+
error: { type: "KeyboardInterrupt", message: "执行已被用户中断", traceback: "" }
|
|
317
|
+
};
|
|
318
|
+
this.pending.forEach((a) => a.resolve(n)), this.pending.clear(), this.chain = Promise.resolve(), this.setStatus("idle");
|
|
319
|
+
}
|
|
320
|
+
ensureWorker() {
|
|
321
|
+
if (this.worker) return this.worker;
|
|
322
|
+
const n = new Blob([Ut(this.pyodideUrl)], { type: "text/javascript" }), a = new Worker(URL.createObjectURL(n));
|
|
323
|
+
return a.onmessage = (o) => this.handleMessage(o.data), this.worker = a, this.setStatus("loading"), a;
|
|
324
|
+
}
|
|
325
|
+
handleMessage(n) {
|
|
326
|
+
switch (n.kind) {
|
|
327
|
+
case "ready":
|
|
328
|
+
this.version = n.version, this.setStatus("ready");
|
|
329
|
+
break;
|
|
330
|
+
case "init-error":
|
|
331
|
+
this.setStatus("idle"), this.initErrorHandlers.forEach((a) => a(n.message));
|
|
332
|
+
break;
|
|
333
|
+
case "output": {
|
|
334
|
+
const a = n.runId ? this.pending.get(n.runId) : void 0;
|
|
335
|
+
a != null && a.onOutput ? a.onOutput(n.stream, n.text) : this.outputHandlers.forEach((o) => o(n.stream, n.text));
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
case "result": {
|
|
339
|
+
const a = this.pending.get(n.id);
|
|
340
|
+
a && (this.pending.delete(n.id), a.resolve({ ok: n.ok, value: n.value, error: n.error }));
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case "checked": {
|
|
344
|
+
const a = this.pending.get(n.id);
|
|
345
|
+
a && (this.pending.delete(n.id), a.resolve({
|
|
346
|
+
ok: n.ok,
|
|
347
|
+
line: n.line,
|
|
348
|
+
column: n.column,
|
|
349
|
+
message: n.message
|
|
350
|
+
}));
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
case "installed": {
|
|
354
|
+
const a = this.pending.get(n.id);
|
|
355
|
+
a && (this.pending.delete(n.id), a.resolve({ ok: n.ok, error: n.error }));
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
send(n, a) {
|
|
361
|
+
const o = this.ensureWorker(), i = this.chain.then(
|
|
362
|
+
() => new Promise((k) => {
|
|
363
|
+
this.pending.set(n.id, { resolve: k, onOutput: a }), o.postMessage(n);
|
|
364
|
+
})
|
|
365
|
+
);
|
|
366
|
+
return this.chain = i.then(
|
|
367
|
+
() => {
|
|
368
|
+
},
|
|
369
|
+
() => {
|
|
370
|
+
}
|
|
371
|
+
), i;
|
|
372
|
+
}
|
|
373
|
+
setStatus(n) {
|
|
374
|
+
this.status !== n && (this.status = n, this.statusHandlers.forEach((a) => a(n, this.version)));
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
let ue = null;
|
|
378
|
+
function ze(s) {
|
|
379
|
+
return ue ? s != null && s.pyodideUrl && s.pyodideUrl !== Ve && console.warn(
|
|
380
|
+
"[princess-python-editor] Pyodide 运行器已初始化,后续传入的 pyodideUrl 将被忽略。"
|
|
381
|
+
) : ue = new Vt(s), ue;
|
|
382
|
+
}
|
|
383
|
+
const he = [
|
|
384
|
+
{ value: "python", label: "Python", extension: "py" },
|
|
385
|
+
{ value: "sql", label: "SQL", extension: "sql" },
|
|
386
|
+
{ value: "javascript", label: "JavaScript", extension: "js" },
|
|
387
|
+
{ value: "json", label: "JSON", extension: "json" }
|
|
388
|
+
];
|
|
389
|
+
function Y(s) {
|
|
390
|
+
return he.find((n) => n.value === s) ?? he[0];
|
|
391
|
+
}
|
|
392
|
+
function de(s) {
|
|
393
|
+
switch (s) {
|
|
394
|
+
case "sql":
|
|
395
|
+
return Mt();
|
|
396
|
+
case "javascript":
|
|
397
|
+
return Ft();
|
|
398
|
+
case "json":
|
|
399
|
+
return Ht();
|
|
400
|
+
case "python":
|
|
401
|
+
default:
|
|
402
|
+
return Te();
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function zt(s) {
|
|
406
|
+
const n = s.lastIndexOf(".");
|
|
407
|
+
if (n < 0) return "python";
|
|
408
|
+
switch (s.slice(n + 1).toLowerCase()) {
|
|
409
|
+
case "sql":
|
|
410
|
+
return "sql";
|
|
411
|
+
case "js":
|
|
412
|
+
case "mjs":
|
|
413
|
+
case "cjs":
|
|
414
|
+
case "jsx":
|
|
415
|
+
case "ts":
|
|
416
|
+
return "javascript";
|
|
417
|
+
case "json":
|
|
418
|
+
return "json";
|
|
419
|
+
default:
|
|
420
|
+
return "python";
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function F(s) {
|
|
424
|
+
const n = Array.isArray(s) ? s : [s];
|
|
425
|
+
return () => $e(
|
|
426
|
+
"svg",
|
|
427
|
+
{
|
|
428
|
+
viewBox: "0 0 24 24",
|
|
429
|
+
width: "1em",
|
|
430
|
+
height: "1em",
|
|
431
|
+
fill: "currentColor",
|
|
432
|
+
"aria-hidden": "true"
|
|
433
|
+
},
|
|
434
|
+
n.map((a) => $e("path", { d: a }))
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
const Dt = F(
|
|
438
|
+
"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"
|
|
439
|
+
), _t = F("M6 6h12v12H6Z"), je = F(
|
|
440
|
+
"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"
|
|
441
|
+
), Bt = F(
|
|
442
|
+
"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"
|
|
443
|
+
), At = F(
|
|
444
|
+
"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"
|
|
445
|
+
), qt = F(
|
|
446
|
+
"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"
|
|
447
|
+
), Kt = F("M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"), Wt = F(
|
|
448
|
+
"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"
|
|
449
|
+
), Zt = F("M9 16.17 4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"), Jt = F(
|
|
450
|
+
"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"
|
|
451
|
+
), Gt = F("M11 5h2v6h6v2h-6v6h-2v-6H5v-2h6V5z"), Xt = {
|
|
452
|
+
key: 0,
|
|
453
|
+
class: "vpe-toolbar"
|
|
454
|
+
}, Yt = ["title", "disabled"], Qt = ["disabled"], en = ["disabled"], tn = ["title", "aria-label"], nn = ["disabled"], rn = {
|
|
455
|
+
class: "vpe-file",
|
|
456
|
+
title: "当前文件名(下载时使用)"
|
|
457
|
+
}, an = ["value", "disabled"], on = ["value"], sn = ["aria-selected", "data-file-id", "title", "onClick", "onKeydown"], ln = { class: "vpe-tab-name" }, un = ["title", "aria-label", "disabled", "onClick"], dn = ["disabled"], cn = { class: "vpe-output-head" }, pn = {
|
|
458
|
+
key: 1,
|
|
459
|
+
class: "vpe-empty"
|
|
460
|
+
}, fn = /* @__PURE__ */ it({
|
|
461
|
+
__name: "PythonEditor",
|
|
462
|
+
props: {
|
|
463
|
+
modelValue: { default: "" },
|
|
464
|
+
theme: { default: "light" },
|
|
465
|
+
height: { default: 520 },
|
|
466
|
+
outputHeight: { default: 180 },
|
|
467
|
+
placeholder: { default: "" },
|
|
468
|
+
readOnly: { type: Boolean, default: !1 },
|
|
469
|
+
autofocus: { type: Boolean, default: !1 },
|
|
470
|
+
showToolbar: { type: Boolean, default: !0 },
|
|
471
|
+
showTabs: { type: Boolean, default: !0 },
|
|
472
|
+
showOutput: { type: Boolean, default: !0 },
|
|
473
|
+
clearOutputOnRun: { type: Boolean, default: !0 },
|
|
474
|
+
lintDelay: { default: 600 },
|
|
475
|
+
disableLint: { type: Boolean, default: !1 },
|
|
476
|
+
preload: { type: Boolean, default: !1 },
|
|
477
|
+
pyodideUrl: { default: "" },
|
|
478
|
+
fileName: { default: "main.py" },
|
|
479
|
+
language: { default: "python" }
|
|
480
|
+
},
|
|
481
|
+
emits: ["update:modelValue", "ready", "statusChange", "runStart", "runOutput", "runError", "runFinish", "loadError", "update:fileName", "update:language", "newFile", "fileClose", "fileSwitch", "copy", "download", "fileOpen"],
|
|
482
|
+
setup(s, { expose: n, emit: a }) {
|
|
483
|
+
const o = s, i = a, k = O(), w = O(), L = O(), I = O(), x = O([]), H = O("idle"), P = O(!1);
|
|
484
|
+
let d = null, me = 0, E = 0, h = null, M = null, Q;
|
|
485
|
+
function ee(e, t, r) {
|
|
486
|
+
return { id: ++E, name: e, language: t, code: r };
|
|
487
|
+
}
|
|
488
|
+
const f = O([ee(o.fileName, o.language, o.modelValue)]), z = O(f.value[0].id), c = W(
|
|
489
|
+
() => f.value.find((e) => e.id === z.value) ?? f.value[0]
|
|
490
|
+
), j = W({
|
|
491
|
+
get: () => c.value.name,
|
|
492
|
+
set: (e) => {
|
|
493
|
+
const t = c.value;
|
|
494
|
+
t.name !== e && (t.name = e, i("update:fileName", e));
|
|
495
|
+
}
|
|
496
|
+
}), ye = new X(), ve = new X(), te = new X(), ne = new X(), N = ze(o.pyodideUrl ? { pyodideUrl: o.pyodideUrl } : void 0), De = W(() => ({
|
|
497
|
+
height: typeof o.height == "number" ? `${o.height}px` : o.height
|
|
498
|
+
})), _e = W(
|
|
499
|
+
() => typeof o.outputHeight == "number" ? `${o.outputHeight}px` : o.outputHeight
|
|
500
|
+
), Be = W(
|
|
501
|
+
() => ({
|
|
502
|
+
idle: "未加载",
|
|
503
|
+
loading: "正在加载 Pyodide…",
|
|
504
|
+
ready: "就绪",
|
|
505
|
+
running: "运行中"
|
|
506
|
+
})[H.value]
|
|
507
|
+
);
|
|
508
|
+
function D(e) {
|
|
509
|
+
return { ...e };
|
|
510
|
+
}
|
|
511
|
+
function re(e, t) {
|
|
512
|
+
x.value.push({ id: ++me, stream: e, text: t });
|
|
513
|
+
}
|
|
514
|
+
function y(e) {
|
|
515
|
+
re("system", e);
|
|
516
|
+
}
|
|
517
|
+
function _(e) {
|
|
518
|
+
if (!d) return;
|
|
519
|
+
const t = e ? [e] : [];
|
|
520
|
+
d.dispatch({ effects: Ue.of(t) });
|
|
521
|
+
}
|
|
522
|
+
function Ae() {
|
|
523
|
+
const e = o.modelValue;
|
|
524
|
+
if (d) {
|
|
525
|
+
e !== d.state.doc.toString() && d.dispatch({ changes: { from: 0, to: d.state.doc.length, insert: e } });
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
const t = c.value;
|
|
529
|
+
t.code !== e && (t.code = e);
|
|
530
|
+
}
|
|
531
|
+
function B() {
|
|
532
|
+
return d ? d.state.doc.toString() : c.value.code;
|
|
533
|
+
}
|
|
534
|
+
function ae(e) {
|
|
535
|
+
if (d) {
|
|
536
|
+
e !== d.state.doc.toString() && d.dispatch({ changes: { from: 0, to: d.state.doc.length, insert: e } });
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const t = c.value;
|
|
540
|
+
t.code = e, e !== o.modelValue && i("update:modelValue", e);
|
|
541
|
+
}
|
|
542
|
+
function oe(e) {
|
|
543
|
+
return o.disableLint || e !== "python" ? [] : jt(N, o.lintDelay);
|
|
544
|
+
}
|
|
545
|
+
function ge(e) {
|
|
546
|
+
const t = c.value;
|
|
547
|
+
if (t.language !== e) {
|
|
548
|
+
if (t.language = e, _(void 0), d == null || d.dispatch({
|
|
549
|
+
effects: [
|
|
550
|
+
te.reconfigure(de(e)),
|
|
551
|
+
ne.reconfigure(oe(e))
|
|
552
|
+
]
|
|
553
|
+
}), /^untitled(-\d+)?\.[^.]+$/.test(t.name)) {
|
|
554
|
+
const l = `${t.name.slice(0, t.name.lastIndexOf("."))}.${Y(e).extension}`;
|
|
555
|
+
f.value.some((u) => u.id !== t.id && u.name === l) || (t.name = l, i("update:fileName", l));
|
|
556
|
+
}
|
|
557
|
+
i("update:language", e);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
function ke(e, t) {
|
|
561
|
+
const l = e.trim().replace(/[\\/:*?"<>|]+/g, "_").replace(/\.+$/, "") || "untitled";
|
|
562
|
+
return /\.[^.\\/]+$/.test(l) ? l : `${l}.${Y(t).extension}`;
|
|
563
|
+
}
|
|
564
|
+
function qe(e) {
|
|
565
|
+
const t = Y(e).extension, r = new Set(f.value.map((g) => g.name));
|
|
566
|
+
let l = `untitled.${t}`, u = 2;
|
|
567
|
+
for (; r.has(l); )
|
|
568
|
+
l = `untitled-${u}.${t}`, u += 1;
|
|
569
|
+
return l;
|
|
570
|
+
}
|
|
571
|
+
function Ke(e) {
|
|
572
|
+
if (!f.value.some((m) => m.name === e)) return e;
|
|
573
|
+
const t = e.lastIndexOf("."), r = t > 0 ? e.slice(0, t) : e, l = t > 0 ? e.slice(t) : "";
|
|
574
|
+
let u = 2, g = `${r}-${u}${l}`;
|
|
575
|
+
for (; f.value.some((m) => m.name === g); )
|
|
576
|
+
u += 1, g = `${r}-${u}${l}`;
|
|
577
|
+
return g;
|
|
578
|
+
}
|
|
579
|
+
let A = 0;
|
|
580
|
+
async function be() {
|
|
581
|
+
var u, g;
|
|
582
|
+
if (H.value === "loading" || H.value === "running") return;
|
|
583
|
+
const e = c.value.language;
|
|
584
|
+
if (e !== "python") {
|
|
585
|
+
y(`当前文件为 ${Y(e).label},仅支持执行 Python 代码`);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
A += 1;
|
|
589
|
+
const t = A, r = c.value.id;
|
|
590
|
+
_(void 0), o.clearOutputOnRun && (x.value = []), y("开始执行"), i("runStart");
|
|
591
|
+
const l = B();
|
|
592
|
+
try {
|
|
593
|
+
const m = await N.run(l, {
|
|
594
|
+
onOutput: (q, K) => {
|
|
595
|
+
re(q, K), i("runOutput", { stream: q, text: K });
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
if (t !== A) return;
|
|
599
|
+
m.interrupted ? y("执行已中断") : m.ok ? y(`执行完成,耗时 ${(m.durationMs / 1e3).toFixed(2)}s`) : (y(`执行出错:${((u = m.error) == null ? void 0 : u.type) ?? "未知错误"}`), c.value.id === r && _((g = m.error) == null ? void 0 : g.line), m.error && i("runError", m.error)), i("runFinish", m);
|
|
600
|
+
} catch (m) {
|
|
601
|
+
if (t !== A) return;
|
|
602
|
+
const q = m instanceof Error ? m.message : String(m);
|
|
603
|
+
y(`执行失败:${q}`);
|
|
604
|
+
const K = { type: "RuntimeError", message: q, traceback: "" };
|
|
605
|
+
i("runError", K), i("runFinish", { ok: !1, durationMs: 0, error: K });
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
st(() => {
|
|
609
|
+
const e = k.value;
|
|
610
|
+
if (!e) return;
|
|
611
|
+
const t = [
|
|
612
|
+
ye.of(o.theme === "dark" ? [fe] : []),
|
|
613
|
+
ve.of([
|
|
614
|
+
ce.readOnly.of(o.readOnly),
|
|
615
|
+
R.editable.of(!o.readOnly)
|
|
616
|
+
]),
|
|
617
|
+
ne.of(oe(c.value.language))
|
|
618
|
+
];
|
|
619
|
+
try {
|
|
620
|
+
d = Rt(e, {
|
|
621
|
+
value: c.value.code,
|
|
622
|
+
dark: o.theme === "dark",
|
|
623
|
+
placeholderText: o.placeholder,
|
|
624
|
+
language: te.of(de(c.value.language)),
|
|
625
|
+
extensions: t,
|
|
626
|
+
onDocChange: (r) => {
|
|
627
|
+
c.value.code = r, r !== o.modelValue && i("update:modelValue", r);
|
|
628
|
+
},
|
|
629
|
+
onRunShortcut: be
|
|
630
|
+
});
|
|
631
|
+
} catch (r) {
|
|
632
|
+
const l = r instanceof Error ? r.message : String(r);
|
|
633
|
+
console.error("[princess-python-editor] 编辑器初始化失败", r), y(`编辑器初始化失败:${l}`), e.innerHTML = "";
|
|
634
|
+
const u = document.createElement("div");
|
|
635
|
+
u.className = "vpe-init-error", u.textContent = `编辑器初始化失败:${l}`, e.appendChild(u);
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
o.autofocus && d.focus(), h = N.on("status", (r, l) => {
|
|
639
|
+
H.value = r, r === "ready" && (l && y(`Pyodide ${l} 就绪`), i("ready"), d && !o.disableLint && c.value.language === "python" && wt(d)), i("statusChange", r);
|
|
640
|
+
}), M = N.on("initError", (r) => {
|
|
641
|
+
y(`Pyodide 加载失败:${r}`), i("loadError", r);
|
|
642
|
+
}), H.value = N.status, o.preload && N.waitReady().catch((r) => y(r.message));
|
|
643
|
+
}), lt(() => {
|
|
644
|
+
h == null || h(), M == null || M(), window.clearTimeout(Q), d == null || d.destroy(), d = null;
|
|
645
|
+
}), U(
|
|
646
|
+
() => o.modelValue,
|
|
647
|
+
() => Ae()
|
|
648
|
+
), U(
|
|
649
|
+
() => o.fileName,
|
|
650
|
+
(e) => {
|
|
651
|
+
e && e !== c.value.name && (j.value = e);
|
|
652
|
+
}
|
|
653
|
+
), U(
|
|
654
|
+
() => o.language,
|
|
655
|
+
(e) => {
|
|
656
|
+
e !== c.value.language && ge(e);
|
|
657
|
+
}
|
|
658
|
+
), U(
|
|
659
|
+
() => o.theme,
|
|
660
|
+
(e) => {
|
|
661
|
+
d == null || d.dispatch({ effects: ye.reconfigure(e === "dark" ? [fe] : []) });
|
|
662
|
+
}
|
|
663
|
+
), U(
|
|
664
|
+
() => o.readOnly,
|
|
665
|
+
(e) => {
|
|
666
|
+
d == null || d.dispatch({
|
|
667
|
+
effects: ve.reconfigure([ce.readOnly.of(e), R.editable.of(!e)])
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
), U(
|
|
671
|
+
() => x.value.length,
|
|
672
|
+
async () => {
|
|
673
|
+
await Ie();
|
|
674
|
+
const e = w.value;
|
|
675
|
+
e && (e.scrollTop = e.scrollHeight);
|
|
676
|
+
}
|
|
677
|
+
);
|
|
678
|
+
function we() {
|
|
679
|
+
be();
|
|
680
|
+
}
|
|
681
|
+
function xe() {
|
|
682
|
+
H.value === "running" && (A += 1, N.interrupt(), y("执行已中断"));
|
|
683
|
+
}
|
|
684
|
+
function Ee() {
|
|
685
|
+
x.value = [];
|
|
686
|
+
}
|
|
687
|
+
function ie() {
|
|
688
|
+
if (o.readOnly) return;
|
|
689
|
+
const e = c.value.language, t = ee(qe(e), e, "");
|
|
690
|
+
f.value.push(t), T(t.id), i("newFile", D(t));
|
|
691
|
+
}
|
|
692
|
+
function T(e) {
|
|
693
|
+
if (e === z.value) {
|
|
694
|
+
Z();
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
const t = f.value.find((r) => r.id === e);
|
|
698
|
+
t && (z.value = e, Ce(t));
|
|
699
|
+
}
|
|
700
|
+
function Se(e) {
|
|
701
|
+
if (f.value.length <= 1) return;
|
|
702
|
+
const t = f.value.findIndex((u) => u.id === e);
|
|
703
|
+
if (t === -1) return;
|
|
704
|
+
const r = f.value.splice(t, 1)[0];
|
|
705
|
+
if (!r || (i("fileClose", D(r)), e !== z.value)) return;
|
|
706
|
+
const l = f.value[Math.min(t, f.value.length - 1)];
|
|
707
|
+
l && (z.value = l.id, Ce(l));
|
|
708
|
+
}
|
|
709
|
+
function Ce(e) {
|
|
710
|
+
_(void 0), ae(e.code), d == null || d.dispatch({
|
|
711
|
+
effects: [
|
|
712
|
+
te.reconfigure(de(e.language)),
|
|
713
|
+
ne.reconfigure(oe(e.language))
|
|
714
|
+
]
|
|
715
|
+
}), i("update:fileName", e.name), i("update:language", e.language), i("fileSwitch", D(e)), Z();
|
|
716
|
+
}
|
|
717
|
+
function Oe() {
|
|
718
|
+
o.readOnly || (_(void 0), ae(""), Z());
|
|
719
|
+
}
|
|
720
|
+
async function Le() {
|
|
721
|
+
var r;
|
|
722
|
+
const e = B();
|
|
723
|
+
let t = !1;
|
|
724
|
+
try {
|
|
725
|
+
(r = navigator.clipboard) != null && r.writeText && (await navigator.clipboard.writeText(e), t = !0);
|
|
726
|
+
} catch {
|
|
727
|
+
t = !1;
|
|
728
|
+
}
|
|
729
|
+
return t || (t = We(e)), t ? (P.value = !0, window.clearTimeout(Q), Q = window.setTimeout(() => {
|
|
730
|
+
P.value = !1;
|
|
731
|
+
}, 1600), i("copy", e), !0) : (y("复制失败,请手动选择代码复制"), !1);
|
|
732
|
+
}
|
|
733
|
+
function We(e) {
|
|
734
|
+
const t = document.createElement("textarea");
|
|
735
|
+
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);
|
|
736
|
+
let r = !1;
|
|
737
|
+
try {
|
|
738
|
+
r = document.execCommand("copy");
|
|
739
|
+
} catch {
|
|
740
|
+
r = !1;
|
|
741
|
+
}
|
|
742
|
+
return t.remove(), r;
|
|
743
|
+
}
|
|
744
|
+
function Fe() {
|
|
745
|
+
const e = c.value, t = ke(e.name, e.language);
|
|
746
|
+
j.value = t;
|
|
747
|
+
const r = new Blob([B()], {
|
|
748
|
+
type: e.language === "python" ? "text/x-python;charset=utf-8" : "text/plain;charset=utf-8"
|
|
749
|
+
}), l = URL.createObjectURL(r), u = document.createElement("a");
|
|
750
|
+
return u.href = l, u.download = t, u.style.display = "none", document.body.appendChild(u), u.click(), u.remove(), window.setTimeout(() => URL.revokeObjectURL(l), 1e3), i("download", t), t;
|
|
751
|
+
}
|
|
752
|
+
function He() {
|
|
753
|
+
var e;
|
|
754
|
+
(e = I.value) == null || e.click();
|
|
755
|
+
}
|
|
756
|
+
async function Ze(e) {
|
|
757
|
+
var l;
|
|
758
|
+
const t = e.target, r = (l = t.files) == null ? void 0 : l[0];
|
|
759
|
+
if (t.value = "", !!r) {
|
|
760
|
+
if (r.size > 2 * 1024 * 1024) {
|
|
761
|
+
y(`文件过大:${(r.size / 1024 / 1024).toFixed(1)} MB(最多 2 MB)`);
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
try {
|
|
765
|
+
const u = await r.text(), g = zt(r.name), m = ee(Ke(r.name), g, u);
|
|
766
|
+
f.value.push(m), T(m.id), y(`已打开文件:${m.name}`), i("fileOpen", { name: m.name, code: u, language: g });
|
|
767
|
+
} catch (u) {
|
|
768
|
+
const g = u instanceof Error ? u.message : String(u);
|
|
769
|
+
y(`读取文件失败:${g}`);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function Je() {
|
|
774
|
+
j.value = ke(j.value, c.value.language);
|
|
775
|
+
}
|
|
776
|
+
function Ge(e) {
|
|
777
|
+
e.key === "Enter" && (e.preventDefault(), e.target.blur());
|
|
778
|
+
}
|
|
779
|
+
function Xe(e) {
|
|
780
|
+
ge(e.target.value);
|
|
781
|
+
}
|
|
782
|
+
async function Me(e) {
|
|
783
|
+
var t, r;
|
|
784
|
+
await Ie(), (r = (t = L.value) == null ? void 0 : t.querySelector(`[data-file-id="${e}"]`)) == null || r.focus();
|
|
785
|
+
}
|
|
786
|
+
function Ye(e, t) {
|
|
787
|
+
if (e.target !== e.currentTarget) return;
|
|
788
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
789
|
+
e.preventDefault(), T(t.id), Me(t.id);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
|
|
793
|
+
e.preventDefault();
|
|
794
|
+
const r = f.value.findIndex((g) => g.id === t.id);
|
|
795
|
+
if (r < 0) return;
|
|
796
|
+
const l = e.key === "ArrowLeft" ? -1 : 1, u = f.value[(r + l + f.value.length) % f.value.length];
|
|
797
|
+
u && (T(u.id), Me(u.id));
|
|
798
|
+
}
|
|
799
|
+
function Z() {
|
|
800
|
+
d == null || d.focus();
|
|
801
|
+
}
|
|
802
|
+
async function Qe(e) {
|
|
803
|
+
var r;
|
|
804
|
+
y(`正在安装:${e.join(", ")}`);
|
|
805
|
+
let t;
|
|
806
|
+
try {
|
|
807
|
+
t = await N.loadPackages(e, {
|
|
808
|
+
onOutput: (l, u) => re(l, u)
|
|
809
|
+
});
|
|
810
|
+
} catch (l) {
|
|
811
|
+
const u = l instanceof Error ? l.message : String(l);
|
|
812
|
+
return y(`安装失败:${u}`), { ok: !1, durationMs: 0, error: { type: "RuntimeError", message: u } };
|
|
813
|
+
}
|
|
814
|
+
return y(t.ok ? "安装完成" : `安装失败:${((r = t.error) == null ? void 0 : r.message) ?? "未知错误"}`), t;
|
|
815
|
+
}
|
|
816
|
+
function et() {
|
|
817
|
+
return N.checkSyntax(B());
|
|
818
|
+
}
|
|
819
|
+
function tt() {
|
|
820
|
+
return d;
|
|
821
|
+
}
|
|
822
|
+
function nt() {
|
|
823
|
+
return f.value.map(D);
|
|
824
|
+
}
|
|
825
|
+
function rt() {
|
|
826
|
+
return D(c.value);
|
|
827
|
+
}
|
|
828
|
+
return n({
|
|
829
|
+
run: we,
|
|
830
|
+
stop: xe,
|
|
831
|
+
clearOutput: Ee,
|
|
832
|
+
clearCode: Oe,
|
|
833
|
+
newFile: ie,
|
|
834
|
+
switchFile: T,
|
|
835
|
+
closeFile: Se,
|
|
836
|
+
copyCode: Le,
|
|
837
|
+
download: Fe,
|
|
838
|
+
openFile: He,
|
|
839
|
+
focus: Z,
|
|
840
|
+
install: Qe,
|
|
841
|
+
checkSyntax: et,
|
|
842
|
+
getEditorView: tt,
|
|
843
|
+
getCode: B,
|
|
844
|
+
setCode: ae,
|
|
845
|
+
getFiles: nt,
|
|
846
|
+
getActiveFile: rt
|
|
847
|
+
}), (e, t) => (v(), b("div", {
|
|
848
|
+
class: V(["vpe-root", { "vpe-dark": s.theme === "dark" }]),
|
|
849
|
+
style: Ne(De.value)
|
|
850
|
+
}, [
|
|
851
|
+
s.showToolbar ? (v(), b("div", Xt, [
|
|
852
|
+
H.value !== "running" ? (v(), b("button", {
|
|
853
|
+
key: 0,
|
|
854
|
+
class: "vpe-btn vpe-btn--primary",
|
|
855
|
+
type: "button",
|
|
856
|
+
title: c.value.language === "python" ? "运行 (Ctrl+Enter)" : "仅支持执行 Python 文件",
|
|
857
|
+
disabled: H.value === "loading" || c.value.language !== "python",
|
|
858
|
+
onClick: we
|
|
859
|
+
}, [
|
|
860
|
+
$(S(Dt)),
|
|
861
|
+
t[1] || (t[1] = p("span", null, "运行", -1))
|
|
862
|
+
], 8, Yt)) : (v(), b("button", {
|
|
863
|
+
key: 1,
|
|
864
|
+
class: "vpe-btn vpe-btn--stop",
|
|
865
|
+
type: "button",
|
|
866
|
+
title: "中断执行",
|
|
867
|
+
onClick: xe
|
|
868
|
+
}, [
|
|
869
|
+
$(S(_t)),
|
|
870
|
+
t[2] || (t[2] = p("span", null, "停止", -1))
|
|
871
|
+
])),
|
|
872
|
+
t[4] || (t[4] = p("span", {
|
|
873
|
+
class: "vpe-divider",
|
|
874
|
+
"aria-hidden": "true"
|
|
875
|
+
}, null, -1)),
|
|
876
|
+
p("button", {
|
|
877
|
+
class: "vpe-btn",
|
|
878
|
+
type: "button",
|
|
879
|
+
title: "新建文件",
|
|
880
|
+
"aria-label": "新建文件",
|
|
881
|
+
disabled: s.readOnly,
|
|
882
|
+
onClick: ie
|
|
883
|
+
}, [
|
|
884
|
+
$(S(Bt))
|
|
885
|
+
], 8, Qt),
|
|
886
|
+
p("button", {
|
|
887
|
+
class: "vpe-btn",
|
|
888
|
+
type: "button",
|
|
889
|
+
title: "打开本地文件",
|
|
890
|
+
"aria-label": "打开本地文件",
|
|
891
|
+
disabled: s.readOnly,
|
|
892
|
+
onClick: He
|
|
893
|
+
}, [
|
|
894
|
+
$(S(At))
|
|
895
|
+
], 8, en),
|
|
896
|
+
p("button", {
|
|
897
|
+
class: "vpe-btn",
|
|
898
|
+
type: "button",
|
|
899
|
+
title: "下载当前文件",
|
|
900
|
+
"aria-label": "下载当前文件",
|
|
901
|
+
onClick: Fe
|
|
902
|
+
}, [
|
|
903
|
+
$(S(Kt))
|
|
904
|
+
]),
|
|
905
|
+
p("button", {
|
|
906
|
+
class: V(["vpe-btn", { "vpe-btn--copied": P.value }]),
|
|
907
|
+
type: "button",
|
|
908
|
+
title: P.value ? "已复制" : "复制代码",
|
|
909
|
+
"aria-label": P.value ? "已复制" : "复制代码",
|
|
910
|
+
onClick: Le
|
|
911
|
+
}, [
|
|
912
|
+
P.value ? (v(), Pe(S(Zt), { key: 0 })) : (v(), Pe(S(Wt), { key: 1 }))
|
|
913
|
+
], 10, tn),
|
|
914
|
+
p("button", {
|
|
915
|
+
class: "vpe-btn",
|
|
916
|
+
type: "button",
|
|
917
|
+
title: "清空当前文件",
|
|
918
|
+
"aria-label": "清空当前文件",
|
|
919
|
+
disabled: s.readOnly,
|
|
920
|
+
onClick: Oe
|
|
921
|
+
}, [
|
|
922
|
+
$(S(je))
|
|
923
|
+
], 8, nn),
|
|
924
|
+
t[5] || (t[5] = p("span", {
|
|
925
|
+
class: "vpe-divider",
|
|
926
|
+
"aria-hidden": "true"
|
|
927
|
+
}, null, -1)),
|
|
928
|
+
p("label", rn, [
|
|
929
|
+
$(S(qt)),
|
|
930
|
+
ut(p("input", {
|
|
931
|
+
"onUpdate:modelValue": t[0] || (t[0] = (r) => j.value = r),
|
|
932
|
+
class: "vpe-file-input",
|
|
933
|
+
type: "text",
|
|
934
|
+
spellcheck: "false",
|
|
935
|
+
"aria-label": "文件名",
|
|
936
|
+
onBlur: Je,
|
|
937
|
+
onKeydown: Ge
|
|
938
|
+
}, null, 544), [
|
|
939
|
+
[dt, j.value]
|
|
940
|
+
])
|
|
941
|
+
]),
|
|
942
|
+
t[6] || (t[6] = p("span", {
|
|
943
|
+
class: "vpe-spacer",
|
|
944
|
+
"aria-hidden": "true"
|
|
945
|
+
}, null, -1)),
|
|
946
|
+
p("span", {
|
|
947
|
+
class: V(["vpe-status", `vpe-status--${H.value}`])
|
|
948
|
+
}, [
|
|
949
|
+
t[3] || (t[3] = p("span", {
|
|
950
|
+
class: "vpe-status-dot",
|
|
951
|
+
"aria-hidden": "true"
|
|
952
|
+
}, null, -1)),
|
|
953
|
+
p("span", null, J(Be.value), 1)
|
|
954
|
+
], 2),
|
|
955
|
+
p("select", {
|
|
956
|
+
class: "vpe-lang",
|
|
957
|
+
value: c.value.language,
|
|
958
|
+
title: "编辑器语言",
|
|
959
|
+
"aria-label": "编辑器语言",
|
|
960
|
+
disabled: s.readOnly,
|
|
961
|
+
onChange: Xe
|
|
962
|
+
}, [
|
|
963
|
+
(v(!0), b(se, null, le(S(he), (r) => (v(), b("option", {
|
|
964
|
+
key: r.value,
|
|
965
|
+
value: r.value
|
|
966
|
+
}, J(r.label), 9, on))), 128))
|
|
967
|
+
], 40, an)
|
|
968
|
+
])) : G("", !0),
|
|
969
|
+
s.showTabs ? (v(), b("div", {
|
|
970
|
+
key: 1,
|
|
971
|
+
ref_key: "tabsEl",
|
|
972
|
+
ref: L,
|
|
973
|
+
class: "vpe-tabs",
|
|
974
|
+
role: "tablist",
|
|
975
|
+
"aria-label": "文件"
|
|
976
|
+
}, [
|
|
977
|
+
(v(!0), b(se, null, le(f.value, (r) => (v(), b("div", {
|
|
978
|
+
key: r.id,
|
|
979
|
+
class: V(["vpe-tab", { "vpe-tab--active": r.id === c.value.id }]),
|
|
980
|
+
role: "tab",
|
|
981
|
+
tabindex: "0",
|
|
982
|
+
"aria-selected": r.id === c.value.id,
|
|
983
|
+
"data-file-id": r.id,
|
|
984
|
+
title: r.name,
|
|
985
|
+
onClick: (l) => T(r.id),
|
|
986
|
+
onKeydown: (l) => Ye(l, r)
|
|
987
|
+
}, [
|
|
988
|
+
p("span", {
|
|
989
|
+
class: V(["vpe-tab-dot", `vpe-tab-dot--${r.language}`]),
|
|
990
|
+
"aria-hidden": "true"
|
|
991
|
+
}, null, 2),
|
|
992
|
+
p("span", ln, J(r.name), 1),
|
|
993
|
+
f.value.length > 1 ? (v(), b("button", {
|
|
994
|
+
key: 0,
|
|
995
|
+
class: "vpe-tab-close",
|
|
996
|
+
type: "button",
|
|
997
|
+
title: `关闭 ${r.name}`,
|
|
998
|
+
"aria-label": `关闭 ${r.name}`,
|
|
999
|
+
disabled: s.readOnly,
|
|
1000
|
+
onClick: ct((l) => Se(r.id), ["stop"])
|
|
1001
|
+
}, [
|
|
1002
|
+
$(S(Jt))
|
|
1003
|
+
], 8, un)) : G("", !0)
|
|
1004
|
+
], 42, sn))), 128)),
|
|
1005
|
+
p("button", {
|
|
1006
|
+
class: "vpe-tabs-add",
|
|
1007
|
+
type: "button",
|
|
1008
|
+
title: "新建文件",
|
|
1009
|
+
"aria-label": "新建文件",
|
|
1010
|
+
disabled: s.readOnly,
|
|
1011
|
+
onClick: ie
|
|
1012
|
+
}, [
|
|
1013
|
+
$(S(Gt))
|
|
1014
|
+
], 8, dn)
|
|
1015
|
+
], 512)) : G("", !0),
|
|
1016
|
+
p("input", {
|
|
1017
|
+
ref_key: "fileInputEl",
|
|
1018
|
+
ref: I,
|
|
1019
|
+
class: "vpe-file-hidden",
|
|
1020
|
+
type: "file",
|
|
1021
|
+
accept: ".py,.pyw,.sql,.js,.mjs,.cjs,.jsx,.ts,.json,.txt,text/x-python,text/plain,application/json",
|
|
1022
|
+
onChange: Ze
|
|
1023
|
+
}, null, 544),
|
|
1024
|
+
p("div", {
|
|
1025
|
+
ref_key: "editorEl",
|
|
1026
|
+
ref: k,
|
|
1027
|
+
class: "vpe-editor"
|
|
1028
|
+
}, null, 512),
|
|
1029
|
+
s.showOutput ? (v(), b("div", {
|
|
1030
|
+
key: 2,
|
|
1031
|
+
class: "vpe-output",
|
|
1032
|
+
style: Ne({ height: _e.value })
|
|
1033
|
+
}, [
|
|
1034
|
+
p("div", cn, [
|
|
1035
|
+
t[7] || (t[7] = p("span", null, "输出", -1)),
|
|
1036
|
+
p("button", {
|
|
1037
|
+
class: "vpe-btn vpe-btn--sm",
|
|
1038
|
+
type: "button",
|
|
1039
|
+
title: "清空输出",
|
|
1040
|
+
onClick: Ee
|
|
1041
|
+
}, [
|
|
1042
|
+
$(S(je))
|
|
1043
|
+
])
|
|
1044
|
+
]),
|
|
1045
|
+
p("pre", {
|
|
1046
|
+
ref_key: "outputEl",
|
|
1047
|
+
ref: w,
|
|
1048
|
+
class: "vpe-output-body"
|
|
1049
|
+
}, [
|
|
1050
|
+
x.value.length ? (v(!0), b(se, { key: 0 }, le(x.value, (r) => (v(), b("span", {
|
|
1051
|
+
key: r.id,
|
|
1052
|
+
class: V(["vpe-line", `vpe-line--${r.stream}`])
|
|
1053
|
+
}, J(r.text) + `
|
|
1054
|
+
`, 3))), 128)) : (v(), b("span", pn, "暂无输出,点击“运行”或按 Ctrl+Enter 执行代码"))
|
|
1055
|
+
], 512)
|
|
1056
|
+
], 4)) : G("", !0)
|
|
1057
|
+
], 6));
|
|
1058
|
+
}
|
|
1059
|
+
});
|
|
1060
|
+
function Ln(s = {}) {
|
|
1061
|
+
const n = ze(s.pyodideUrl ? { pyodideUrl: s.pyodideUrl } : void 0), a = O(n.status), o = O(""), i = O([]);
|
|
1062
|
+
let k = 0;
|
|
1063
|
+
const w = (E, h) => {
|
|
1064
|
+
i.value.push({ id: ++k, stream: E, text: h });
|
|
1065
|
+
}, L = n.on("status", (E, h) => {
|
|
1066
|
+
a.value = E, h && (o.value = h);
|
|
1067
|
+
}), I = n.on("initError", (E) => {
|
|
1068
|
+
w("system", `[pyodide] 加载失败:${E}`);
|
|
1069
|
+
});
|
|
1070
|
+
return pt(() => {
|
|
1071
|
+
L(), I();
|
|
1072
|
+
}), s.preload && n.waitReady().catch(() => {
|
|
1073
|
+
}), {
|
|
1074
|
+
status: a,
|
|
1075
|
+
version: o,
|
|
1076
|
+
outputs: i,
|
|
1077
|
+
runPython: async (E) => {
|
|
1078
|
+
try {
|
|
1079
|
+
return await n.run(E, { onOutput: (h, M) => w(h, M) });
|
|
1080
|
+
} catch (h) {
|
|
1081
|
+
return {
|
|
1082
|
+
ok: !1,
|
|
1083
|
+
durationMs: 0,
|
|
1084
|
+
error: { type: "RuntimeError", message: h instanceof Error ? h.message : String(h), traceback: "" }
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
},
|
|
1088
|
+
install: async (E) => {
|
|
1089
|
+
try {
|
|
1090
|
+
return await n.loadPackages(E, { onOutput: (h, M) => w(h, M) });
|
|
1091
|
+
} catch (h) {
|
|
1092
|
+
return {
|
|
1093
|
+
ok: !1,
|
|
1094
|
+
durationMs: 0,
|
|
1095
|
+
error: { type: "RuntimeError", message: h instanceof Error ? h.message : String(h), traceback: "" }
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
},
|
|
1099
|
+
checkSyntax: (E) => n.checkSyntax(E),
|
|
1100
|
+
interrupt: () => n.interrupt(),
|
|
1101
|
+
clearOutputs: () => {
|
|
1102
|
+
i.value = [];
|
|
1103
|
+
},
|
|
1104
|
+
/** 预加载并在就绪后 resolve */
|
|
1105
|
+
ready: () => n.waitReady()
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
const Fn = fn;
|
|
1109
|
+
export {
|
|
1110
|
+
Ve as DEFAULT_PYODIDE_URL,
|
|
1111
|
+
$t as MONO_FONT_STACK,
|
|
1112
|
+
Vt as PyodideRunner,
|
|
1113
|
+
Fn as PythonEditor,
|
|
1114
|
+
Rt as createEditor,
|
|
1115
|
+
fn as default,
|
|
1116
|
+
ze as getRunner,
|
|
1117
|
+
jt as makePythonLinter,
|
|
1118
|
+
Ue as setErrorLines,
|
|
1119
|
+
Ln as usePyodide
|
|
1120
|
+
};
|