princess-python-editor 0.2.0 → 0.3.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/README.md +105 -1
- package/dist/PythonEditor.vue.d.ts +12 -0
- package/dist/index.cjs.js +6 -6
- package/dist/index.d.ts +4 -0
- package/dist/index.es.js +1474 -1439
- package/dist/style.css +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -44,7 +44,13 @@ npm install princess-python-editor
|
|
|
44
44
|
yarn add princess-python-editor
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
##
|
|
47
|
+
## 使用方式
|
|
48
|
+
|
|
49
|
+
提供两种接入方式:安装 npm 包,或直接复制组件源码。两者使用相同的 Props、事件和实例方法,无需调用 `app.use`。
|
|
50
|
+
|
|
51
|
+
### npm 包方式
|
|
52
|
+
|
|
53
|
+
按上面的命令安装 `princess-python-editor` 后,在需要的页面中导入组件即可。CodeMirror 等运行依赖会自动安装。
|
|
48
54
|
|
|
49
55
|
在应用入口引入组件样式:
|
|
50
56
|
|
|
@@ -95,6 +101,104 @@ function onRunError(error: PyError) {
|
|
|
95
101
|
|
|
96
102
|
> `height="100%"` 需要父容器具有明确高度;也可以直接传入数字(按 px 处理)。
|
|
97
103
|
|
|
104
|
+
### 源码组件方式(直接复制 PythonEditor 目录)
|
|
105
|
+
|
|
106
|
+
适用于不安装 `princess-python-editor`、直接在其他 Vue 3 + TypeScript 项目中维护组件源码的场景。
|
|
107
|
+
|
|
108
|
+
#### 1. 复制完整目录
|
|
109
|
+
|
|
110
|
+
将本项目的 `src/components/PythonEditor/` 整个复制到目标项目的 `src/components/PythonEditor/`,保留目录结构:
|
|
111
|
+
|
|
112
|
+
```text
|
|
113
|
+
src/components/PythonEditor/
|
|
114
|
+
├── PythonEditor.vue
|
|
115
|
+
├── index.ts
|
|
116
|
+
├── types.ts
|
|
117
|
+
├── icons.ts
|
|
118
|
+
├── languages.ts
|
|
119
|
+
├── themes.ts
|
|
120
|
+
├── editor/
|
|
121
|
+
│ ├── createEditor.ts
|
|
122
|
+
│ ├── pythonLint.ts
|
|
123
|
+
│ └── theme.ts
|
|
124
|
+
├── runtime/
|
|
125
|
+
│ ├── PyodideRunner.ts
|
|
126
|
+
│ ├── files.ts
|
|
127
|
+
│ ├── python.ts
|
|
128
|
+
│ ├── sql.ts
|
|
129
|
+
│ ├── usePyodide.ts
|
|
130
|
+
│ └── workerSource.ts
|
|
131
|
+
└── styles/
|
|
132
|
+
└── editor.css
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
不能只复制 `PythonEditor.vue`:编辑器配置、运行器、图标和样式均通过相对路径引用同目录下的文件。无需复制本项目根目录的 `src/index.ts`、`playground` 或构建配置。
|
|
136
|
+
|
|
137
|
+
#### 2. 安装运行依赖
|
|
138
|
+
|
|
139
|
+
在目标项目根目录执行以下命令,版本范围与本项目一致(可逐行执行):
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
pnpm add "@codemirror/autocomplete@^6.18.6" "@codemirror/commands@^6.8.0"
|
|
143
|
+
pnpm add "@codemirror/lang-javascript@^6.2.5" "@codemirror/lang-json@^6.0.2" "@codemirror/lang-python@^6.1.7" "@codemirror/lang-sql@^6.10.0"
|
|
144
|
+
pnpm add "@codemirror/language@^6.10.8" "@codemirror/lint@^6.8.4"
|
|
145
|
+
pnpm add "@codemirror/state@^6.5.2" "@codemirror/view@^6.36.2" "@lezer/highlight@^1.2.5"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
- 宿主项目需使用 Vue `>= 3.3.0`,并支持编译 Vue 单文件组件和 TypeScript;已有 Vue 3 + TypeScript 的 Vite 项目通常无需调整构建配置。
|
|
149
|
+
- 上面的 CodeMirror 包和 `@lezer/highlight` 是源码直接引用的依赖。即使只编辑 Python,也应全部安装,因为 JavaScript、JSON、SQL 语言支持使用静态导入。
|
|
150
|
+
- **无需安装 Arco Design 或图标库**:Arco Design 只用于本仓库的演示页面,组件内部使用自带图标和样式。
|
|
151
|
+
- **无需安装 `pyodide` npm 包或本机 Python**:浏览器运行模式会从 CDN 加载 Pyodide;内网或离线部署需按下文“Pyodide 配置”自托管资源。
|
|
152
|
+
- **无需安装 `vite-plugin-dts` 或 `vite-plugin-vue-devtools`**:它们只用于本仓库的库构建和演示开发。已有 Vue 项目也无需额外配置专用 Vite 插件。
|
|
153
|
+
|
|
154
|
+
#### 3. 在页面中使用
|
|
155
|
+
|
|
156
|
+
以下示例假设页面文件为 `src/views/PythonPlayground.vue`,通过相对路径导入,不依赖 `@` 别名:
|
|
157
|
+
|
|
158
|
+
```vue
|
|
159
|
+
<script setup lang="ts">
|
|
160
|
+
import { ref } from 'vue'
|
|
161
|
+
import { PythonEditor } from '../components/PythonEditor'
|
|
162
|
+
import type { EditorLanguage, PyError, RunResult } from '../components/PythonEditor'
|
|
163
|
+
|
|
164
|
+
const code = ref('print("Hello, Python!")')
|
|
165
|
+
const fileName = ref('main.py')
|
|
166
|
+
const language = ref<EditorLanguage>('python')
|
|
167
|
+
|
|
168
|
+
function onRunFinish(result: RunResult) {
|
|
169
|
+
console.log(result.ok ? `执行完成,耗时 ${result.durationMs}ms` : '执行失败')
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function onRunError(error: PyError) {
|
|
173
|
+
console.error(error.type, error.message)
|
|
174
|
+
}
|
|
175
|
+
</script>
|
|
176
|
+
|
|
177
|
+
<template>
|
|
178
|
+
<PythonEditor
|
|
179
|
+
v-model="code"
|
|
180
|
+
v-model:file-name="fileName"
|
|
181
|
+
v-model:language="language"
|
|
182
|
+
:height="520"
|
|
183
|
+
:output-height="180"
|
|
184
|
+
@run-finish="onRunFinish"
|
|
185
|
+
@run-error="onRunError"
|
|
186
|
+
/>
|
|
187
|
+
</template>
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`PythonEditor.vue` 已通过 `<style src="./styles/editor.css">` 自动引入组件样式,源码方式**不需要**再导入 `princess-python-editor/style.css`,也不需要安装 `princess-python-editor`。
|
|
191
|
+
|
|
192
|
+
如需调用实例方法或导入类型、主题、`usePyodide` 等能力,同样从复制后的本地目录导入。下文示例中的 `'princess-python-editor'` 应替换为目标项目中该目录的实际导入路径。
|
|
193
|
+
|
|
194
|
+
#### 4. 按需接入后端
|
|
195
|
+
|
|
196
|
+
默认即可在浏览器内编辑和执行 Python,无需后端。服务端 Python、服务端文件管理和远程 SQL 查询属于可选功能,复制组件目录不会自动部署这些服务:
|
|
197
|
+
|
|
198
|
+
- 服务端 Python:配置 `python-endpoint`,后端需实现下文的会话接口。
|
|
199
|
+
- 服务端文件:配置 `files-endpoint`,后端需实现文件读写接口。
|
|
200
|
+
- 远程数据库:配置 `sql-config` 的 HTTP 网关,或传入 `sql-executor`;浏览器内 SQLite 无需后端。
|
|
201
|
+
|
|
98
202
|
### 双向绑定
|
|
99
203
|
|
|
100
204
|
| 绑定 | 类型 | 说明 |
|
|
@@ -17,6 +17,14 @@ export interface Props {
|
|
|
17
17
|
autofocus?: boolean;
|
|
18
18
|
/** 是否显示工具栏 */
|
|
19
19
|
showToolbar?: boolean;
|
|
20
|
+
/** 是否显示工具栏新建按钮及 Tab 栏新建入口 */
|
|
21
|
+
showNewFile?: boolean;
|
|
22
|
+
/** 是否显示工具栏打开本地文件按钮 */
|
|
23
|
+
showOpenFile?: boolean;
|
|
24
|
+
/** 是否显示工具栏下载当前文件按钮 */
|
|
25
|
+
showDownload?: boolean;
|
|
26
|
+
/** 是否显示工具栏虚拟文件入口 */
|
|
27
|
+
showVirtualFiles?: boolean;
|
|
20
28
|
/** 是否显示文件 Tab 栏 */
|
|
21
29
|
showTabs?: boolean;
|
|
22
30
|
/** 是否显示输出面板 */
|
|
@@ -171,6 +179,10 @@ declare const _default: import('vue').DefineComponent<Props, {
|
|
|
171
179
|
readOnly: boolean;
|
|
172
180
|
autofocus: boolean;
|
|
173
181
|
showToolbar: boolean;
|
|
182
|
+
showNewFile: boolean;
|
|
183
|
+
showOpenFile: boolean;
|
|
184
|
+
showDownload: boolean;
|
|
185
|
+
showVirtualFiles: boolean;
|
|
174
186
|
showTabs: boolean;
|
|
175
187
|
showOutput: boolean;
|
|
176
188
|
clearOutputOnRun: boolean;
|