@hep-code-runner/react 3.3.0 → 3.3.3
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 +71 -144
- package/dist/index.js +78 -12
- package/dist/index.mjs +561 -492
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,188 +1,103 @@
|
|
|
1
1
|
# @hep-code-runner/react
|
|
2
2
|
|
|
3
|
-
React
|
|
4
|
-
|
|
5
|
-
## 功能特性
|
|
6
|
-
|
|
7
|
-
- 支持多种编程语言(JavaScript、Python、Go、Java、C、Rust 等)
|
|
8
|
-
- 多版本语言支持(如 Python 2、Python 3)
|
|
9
|
-
- 代码语法高亮(PrismJS)
|
|
10
|
-
- 亮色/暗色主题切换
|
|
11
|
-
- 可拖拽调整编辑器宽度
|
|
12
|
-
- 支持自定义 Piston API 地址
|
|
13
|
-
- 完整的事件回调
|
|
3
|
+
React 代码运行器组件,基于 Piston API 支持多种编程语言在线执行。
|
|
14
4
|
|
|
15
5
|
## 安装
|
|
16
6
|
|
|
17
7
|
```bash
|
|
18
8
|
npm install @hep-code-runner/react
|
|
19
|
-
# 或
|
|
20
|
-
pnpm add @hep-code-runner/react
|
|
21
9
|
```
|
|
22
10
|
|
|
23
|
-
##
|
|
11
|
+
## 引入
|
|
24
12
|
|
|
25
13
|
```jsx
|
|
26
14
|
import { CodeRunner } from "@hep-code-runner/react";
|
|
27
|
-
|
|
28
|
-
function App() {
|
|
29
|
-
return <CodeRunner />;
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## Props 属性
|
|
34
|
-
|
|
35
|
-
| 属性 | 类型 | 默认值 | 说明 |
|
|
36
|
-
| -------------------- | ------------------------ | ------------- | ------------------- |
|
|
37
|
-
| pistonUrl | string | '/api/piston' | Piston API 地址 |
|
|
38
|
-
| language | string | 'javascript' | 当前语言(受控) |
|
|
39
|
-
| code | string | '' | 当前代码(受控) |
|
|
40
|
-
| theme | 'light' \| 'dark' | 'light' | 主题 |
|
|
41
|
-
| themeColor | string | - | 自定义主题色 |
|
|
42
|
-
| showLanguageSelector | boolean | true | 显示语言选择器 |
|
|
43
|
-
| showEditor | boolean | true | 显示代码编辑器 |
|
|
44
|
-
| editable | boolean | true | 是否可编辑 |
|
|
45
|
-
| executorLabel | string | '运行' | 运行按钮文字 |
|
|
46
|
-
| onExecuteStart | () => void | - | 代码开始执行回调 |
|
|
47
|
-
| onExecuteEnd | (result) => void | - | 代码执行完成回调 |
|
|
48
|
-
| onLanguageChange | (language, code) => void | - | 语言切换回调 |
|
|
49
|
-
| onLanguageUpdate | (language: string) => void | - | 语言更新回调 |
|
|
50
|
-
| onCodeUpdate | (code: string) => void | - | 代码更新回调 |
|
|
51
|
-
| onChange | (code: string) => void | - | 代码变化回调(实时)|
|
|
52
|
-
|
|
53
|
-
## 主题切换
|
|
54
|
-
|
|
55
|
-
```jsx
|
|
56
|
-
<CodeRunner theme="dark" />
|
|
57
15
|
```
|
|
58
16
|
|
|
59
|
-
|
|
17
|
+
---
|
|
60
18
|
|
|
61
|
-
##
|
|
19
|
+
## CodeRunner
|
|
62
20
|
|
|
63
|
-
|
|
21
|
+
### 用法
|
|
64
22
|
|
|
65
23
|
```jsx
|
|
66
|
-
|
|
67
|
-
<CodeRunner themeColor="#4fc3f7" theme="light" />
|
|
68
|
-
|
|
69
|
-
// 深色主题
|
|
70
|
-
<CodeRunner themeColor="#9c27b0" theme="dark" />
|
|
71
|
-
|
|
72
|
-
// 动态切换主题色
|
|
73
|
-
<CodeRunner themeColor={currentColor} />
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## 自定义 Piston API
|
|
77
|
-
|
|
78
|
-
```jsx
|
|
79
|
-
<CodeRunner pistonUrl="https://your-piston-api.com/api" />
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
## 示例
|
|
83
|
-
|
|
84
|
-
### 默认配置
|
|
24
|
+
import { CodeRunner } from "@hep-code-runner/react";
|
|
85
25
|
|
|
86
|
-
|
|
87
|
-
<CodeRunner
|
|
26
|
+
function App() {
|
|
27
|
+
return <CodeRunner />;
|
|
28
|
+
}
|
|
88
29
|
```
|
|
89
30
|
|
|
90
|
-
###
|
|
31
|
+
### Props
|
|
91
32
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
33
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
34
|
+
|------|------|--------|------|
|
|
35
|
+
| pistonUrl | string | '/api/piston' | Piston API 地址 |
|
|
36
|
+
| language | string | 'javascript' | 当前语言 |
|
|
37
|
+
| code | string | '' | 当前代码 |
|
|
38
|
+
| theme | 'light' \| 'dark' | 'light' | 主题 |
|
|
39
|
+
| themeColor | string | - | 自定义主题色 |
|
|
40
|
+
| showLanguageSelector | boolean | true | 显示语言选择器 |
|
|
41
|
+
| showEditor | boolean | true | 显示代码编辑器 |
|
|
42
|
+
| editable | boolean | true | 是否可编辑 |
|
|
43
|
+
| executorLabel | string | '运行' | 运行按钮文字 |
|
|
95
44
|
|
|
96
45
|
### 受控模式
|
|
97
46
|
|
|
98
|
-
组件完全受控,父组件通过 props + 回调控制 language 和 code:
|
|
99
|
-
|
|
100
47
|
```jsx
|
|
101
|
-
import { useState } from
|
|
48
|
+
import { useState } from "react";
|
|
102
49
|
import { CodeRunner } from "@hep-code-runner/react";
|
|
103
50
|
|
|
104
51
|
function App() {
|
|
105
|
-
const [
|
|
106
|
-
const [
|
|
107
|
-
|
|
108
|
-
return (
|
|
109
|
-
<CodeRunner
|
|
110
|
-
language={currentLanguage}
|
|
111
|
-
code={currentCode}
|
|
112
|
-
onLanguageUpdate={setCurrentLanguage}
|
|
113
|
-
onCodeUpdate={setCurrentCode}
|
|
114
|
-
/>
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### 监听事件
|
|
120
|
-
|
|
121
|
-
```jsx
|
|
122
|
-
function App() {
|
|
123
|
-
const [eventLog, setEventLog] = useState([]);
|
|
124
|
-
|
|
125
|
-
const handleStart = () => {
|
|
126
|
-
console.log("开始执行");
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
const handleEnd = (result) => {
|
|
130
|
-
console.log("执行完成", result);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const handleLanguageChange = (language, code) => {
|
|
134
|
-
console.log("语言切换", language);
|
|
135
|
-
};
|
|
52
|
+
const [language, setLanguage] = useState("javascript");
|
|
53
|
+
const [code, setCode] = useState('console.log("hello")');
|
|
136
54
|
|
|
137
55
|
return (
|
|
138
56
|
<CodeRunner
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
57
|
+
language={language}
|
|
58
|
+
code={code}
|
|
59
|
+
onLanguageUpdate={setLanguage}
|
|
60
|
+
onCodeUpdate={setCode}
|
|
142
61
|
/>
|
|
143
62
|
);
|
|
144
63
|
}
|
|
145
64
|
```
|
|
146
65
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
- react: ^16.8.0 || ^17.0.0 || ^18.0.0
|
|
150
|
-
- react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0
|
|
151
|
-
- prismjs: ^1.30.0
|
|
66
|
+
### 回调函数
|
|
152
67
|
|
|
153
|
-
|
|
68
|
+
| 属性 | 类型 | 说明 |
|
|
69
|
+
|------|------|------|
|
|
70
|
+
| onLanguageUpdate | (language: string) => void | 语言变化 |
|
|
71
|
+
| onCodeUpdate | (code: string) => void | 代码变化 |
|
|
72
|
+
| onExecuteStart | () => void | 代码开始执行 |
|
|
73
|
+
| onExecuteEnd | (result) => void | 代码执行完成 |
|
|
74
|
+
| onLanguageChange | (language, code) => void | 语言切换 |
|
|
75
|
+
| onChange | (code: string) => void | 代码变化(防抖) |
|
|
154
76
|
|
|
155
|
-
|
|
77
|
+
---
|
|
156
78
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
```jsx
|
|
160
|
-
import { CodeRunnerDialog } from "@hep-code-runner/react";
|
|
161
|
-
```
|
|
79
|
+
## CodeRunnerDialog
|
|
162
80
|
|
|
163
|
-
###
|
|
81
|
+
### 用法
|
|
164
82
|
|
|
165
83
|
```jsx
|
|
166
|
-
import {
|
|
84
|
+
import { useRef } from "react";
|
|
167
85
|
import { CodeRunnerDialog } from "@hep-code-runner/react";
|
|
168
86
|
|
|
169
87
|
function App() {
|
|
170
|
-
const
|
|
88
|
+
const dialogRef = useRef(null);
|
|
171
89
|
|
|
172
90
|
return (
|
|
173
91
|
<>
|
|
174
|
-
<button onClick={() =>
|
|
92
|
+
<button onClick={() => dialogRef.current?.open()}>打开</button>
|
|
175
93
|
|
|
176
94
|
<CodeRunnerDialog
|
|
177
|
-
|
|
178
|
-
onClose={() => setVisible(false)}
|
|
95
|
+
ref={dialogRef}
|
|
179
96
|
title="代码执行器"
|
|
180
|
-
width={800}
|
|
181
97
|
footer={
|
|
182
98
|
<>
|
|
183
|
-
<button onClick={() =>
|
|
99
|
+
<button onClick={() => dialogRef.current?.close()}>取消</button>
|
|
184
100
|
<button onClick={handleSave}>保存</button>
|
|
185
|
-
<button onClick={handleInsert}>插入代码</button>
|
|
186
101
|
</>
|
|
187
102
|
}
|
|
188
103
|
/>
|
|
@@ -191,26 +106,28 @@ function App() {
|
|
|
191
106
|
}
|
|
192
107
|
```
|
|
193
108
|
|
|
194
|
-
###
|
|
109
|
+
### 受控模式
|
|
195
110
|
|
|
196
111
|
```jsx
|
|
197
|
-
import {
|
|
112
|
+
import { useState } from "react";
|
|
198
113
|
import { CodeRunnerDialog } from "@hep-code-runner/react";
|
|
199
114
|
|
|
200
115
|
function App() {
|
|
201
|
-
const
|
|
116
|
+
const [visible, setVisible] = useState(false);
|
|
202
117
|
|
|
203
118
|
return (
|
|
204
119
|
<>
|
|
205
|
-
<button onClick={() =>
|
|
120
|
+
<button onClick={() => setVisible(true)}>打开</button>
|
|
206
121
|
|
|
207
122
|
<CodeRunnerDialog
|
|
208
|
-
|
|
123
|
+
open={visible}
|
|
124
|
+
onClose={() => setVisible(false)}
|
|
209
125
|
title="代码执行器"
|
|
126
|
+
width={800}
|
|
210
127
|
footer={
|
|
211
128
|
<>
|
|
212
|
-
<button onClick={() =>
|
|
213
|
-
<button onClick={
|
|
129
|
+
<button onClick={() => setVisible(false)}>取消</button>
|
|
130
|
+
<button onClick={handleSave}>保存</button>
|
|
214
131
|
</>
|
|
215
132
|
}
|
|
216
133
|
/>
|
|
@@ -219,23 +136,33 @@ function App() {
|
|
|
219
136
|
}
|
|
220
137
|
```
|
|
221
138
|
|
|
222
|
-
### Props
|
|
139
|
+
### Props
|
|
223
140
|
|
|
224
141
|
| 属性 | 类型 | 默认值 | 说明 |
|
|
225
142
|
|------|------|--------|------|
|
|
226
|
-
| open | boolean | - |
|
|
227
|
-
| defaultOpen | boolean | false |
|
|
143
|
+
| open | boolean | - | 控制显隐(受控模式) |
|
|
144
|
+
| defaultOpen | boolean | false | 初始状态(非受控模式) |
|
|
228
145
|
| title | string | '代码执行器' | 弹窗标题 |
|
|
229
146
|
| width | string \| number | 800 | 弹窗宽度 |
|
|
230
147
|
| onClose | () => void | - | 关闭回调 |
|
|
231
148
|
| footer | ReactNode | - | 底部自定义内容 |
|
|
149
|
+
| pistonUrl | string | '/api/piston' | Piston API 地址 |
|
|
150
|
+
| language | string | 'javascript' | 当前语言 |
|
|
151
|
+
| code | string | '' | 当前代码 |
|
|
232
152
|
| theme | 'light' \| 'dark' | 'light' | 主题 |
|
|
153
|
+
| themeColor | string | - | 自定义主题色 |
|
|
154
|
+
| showLanguageSelector | boolean | true | 显示语言选择器 |
|
|
155
|
+
| showEditor | boolean | true | 显示代码编辑器 |
|
|
156
|
+
| editable | boolean | true | 是否可编辑 |
|
|
157
|
+
| executorLabel | string | '运行' | 运行按钮文字 |
|
|
233
158
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
### Ref 方法
|
|
159
|
+
### Ref Methods
|
|
237
160
|
|
|
238
161
|
| 方法 | 说明 |
|
|
239
162
|
|------|------|
|
|
240
|
-
| open() | 打开弹窗 |
|
|
241
163
|
| close() | 关闭弹窗 |
|
|
164
|
+
|
|
165
|
+
## 依赖
|
|
166
|
+
|
|
167
|
+
- react: ^16.8.0
|
|
168
|
+
- prismjs: ^1.30.0
|