@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 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
- 支持通过 `themeColor` 属性自定义主题色,组件会自动根据颜色亮度调整文字颜色,确保可读性。
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
- ```jsx
87
- <CodeRunner />
26
+ function App() {
27
+ return <CodeRunner />;
28
+ }
88
29
  ```
89
30
 
90
- ### 深色主题
31
+ ### Props
91
32
 
92
- ```jsx
93
- <CodeRunner theme="dark" language="python" />
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 'react';
48
+ import { useState } from "react";
102
49
  import { CodeRunner } from "@hep-code-runner/react";
103
50
 
104
51
  function App() {
105
- const [currentLanguage, setCurrentLanguage] = useState('javascript');
106
- const [currentCode, setCurrentCode] = useState('console.log("hello")');
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
- onExecuteStart={handleStart}
140
- onExecuteEnd={handleEnd}
141
- onLanguageChange={handleLanguageChange}
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
- ## CodeRunnerDialog 弹窗组件
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 { useState } from "react";
84
+ import { useRef } from "react";
167
85
  import { CodeRunnerDialog } from "@hep-code-runner/react";
168
86
 
169
87
  function App() {
170
- const [visible, setVisible] = useState(false);
88
+ const dialogRef = useRef(null);
171
89
 
172
90
  return (
173
91
  <>
174
- <button onClick={() => setVisible(true)}>打开弹窗</button>
92
+ <button onClick={() => dialogRef.current?.open()}>打开</button>
175
93
 
176
94
  <CodeRunnerDialog
177
- open={visible}
178
- onClose={() => setVisible(false)}
95
+ ref={dialogRef}
179
96
  title="代码执行器"
180
- width={800}
181
97
  footer={
182
98
  <>
183
- <button onClick={() => setVisible(false)}>取消</button>
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 { useRef } from "react";
112
+ import { useState } from "react";
198
113
  import { CodeRunnerDialog } from "@hep-code-runner/react";
199
114
 
200
115
  function App() {
201
- const dialogRef = useRef(null);
116
+ const [visible, setVisible] = useState(false);
202
117
 
203
118
  return (
204
119
  <>
205
- <button onClick={() => dialogRef.current?.open()}>打开弹窗</button>
120
+ <button onClick={() => setVisible(true)}>打开</button>
206
121
 
207
122
  <CodeRunnerDialog
208
- ref={dialogRef}
123
+ open={visible}
124
+ onClose={() => setVisible(false)}
209
125
  title="代码执行器"
126
+ width={800}
210
127
  footer={
211
128
  <>
212
- <button onClick={() => dialogRef.current?.close()}>取消</button>
213
- <button onClick={handleInsert}>插入代码</button>
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
- 其他 Props 会透传给内部 CodeRunner 组件。
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