Skip to content

Commit 53520dd

Browse files
committed
feat (language): 支持 TypeScript By Node.js 引擎语言
1 parent 9b51c21 commit 53520dd

8 files changed

Lines changed: 288 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ CodeForge 是一款轻量级、高性能的桌面代码执行器,专为开发
3636
- **Rust**
3737
- **Shell**
3838
- **Swift**
39+
- **TypeScript (Node.js)**
3940

4041
## 安装
4142

public/icons/typescript-nodejs.svg

Lines changed: 7 additions & 0 deletions
Loading
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// JavaScript 示例代码 - CodeForge 代码执行环境
2+
3+
function greetUser(name) {
4+
return `Hello, ${name}! 👋`;
5+
}
6+
7+
// 函数定义
8+
function addNumbers(a, b) {
9+
return {
10+
result: a + b,
11+
operation: 'addition'
12+
};
13+
}
14+
15+
function printArray(arr, label) {
16+
console.log(`${label}: ${arr.join(' ')}`);
17+
}
18+
19+
function fibonacci(n) {
20+
if (n <= 1) {
21+
return n;
22+
}
23+
return fibonacci(n - 1) + fibonacci(n - 2);
24+
}
25+
26+
// 枚举模拟
27+
const Weekday = {
28+
MONDAY: 1,
29+
TUESDAY: 2,
30+
WEDNESDAY: 3,
31+
THURSDAY: 4,
32+
FRIDAY: 5,
33+
SATURDAY: 6,
34+
SUNDAY: 7
35+
};
36+
37+
// 主函数
38+
function main() {
39+
console.log("🎉 欢迎使用 CodeForge!");
40+
console.log("Welcome to CodeForge!");
41+
console.log("");
42+
43+
console.log("=========================================");
44+
console.log(" CodeForge JavaScript ");
45+
console.log("=========================================");
46+
console.log("");
47+
48+
// 基本输出示例
49+
console.log("✅ JavaScript 运行成功! (JavaScript is working!)");
50+
console.log("⚡ 这是 JavaScript 程序 (This is JavaScript program)");
51+
console.log("");
52+
53+
// 变量操作
54+
const name = "CodeForge";
55+
const version = "JavaScript";
56+
const number1 = 10;
57+
const number2 = 20;
58+
const calculation = addNumbers(number1, number2);
59+
60+
console.log("🔢 简单计算 (Simple calculation):");
61+
console.log(`${number1} + ${number2} = ${calculation.result}`);
62+
console.log("");
63+
64+
// 字符串操作
65+
console.log("📝 字符串操作 (String operations):");
66+
console.log(`平台名称 (Platform): ${name}`);
67+
console.log(`语言版本 (Language): ${version}`);
68+
console.log(`完整信息 (Full info): ${name} - ${version}`);
69+
console.log("");
70+
71+
// 循环示例
72+
console.log("🔄 循环输出 (Loop output):");
73+
for (let i = 1; i <= 5; i++) {
74+
console.log(`第 ${i} 次输出 (Output #${i}): Hello from CodeForge!`);
75+
}
76+
console.log("");
77+
78+
// 数组操作
79+
console.log("🍎 数组示例 (Array example):");
80+
const fruits = ["苹果", "香蕉", "橙子", "葡萄"];
81+
fruits.forEach((fruit, index) => {
82+
console.log(`${index + 1}. ${fruit}`);
83+
});
84+
console.log("");
85+
86+
// 条件判断
87+
const score = 85;
88+
console.log("📊 成绩评估 (Score evaluation):");
89+
if (score >= 90) {
90+
console.log("优秀! (Excellent!)");
91+
} else if (score >= 80) {
92+
console.log("良好! (Good!)");
93+
} else if (score >= 60) {
94+
console.log("及格 (Pass)");
95+
} else {
96+
console.log("需要努力 (Need improvement)");
97+
}
98+
console.log("");
99+
100+
// 对象示例
101+
console.log("👤 对象示例 (Object example):");
102+
const person = {
103+
name: "张三",
104+
age: 25,
105+
height: 175.5
106+
};
107+
108+
console.log(`姓名: ${person.name}, 年龄: ${person.age}, 身高: ${person.height} cm`);
109+
console.log("");
110+
111+
// 函数示例
112+
console.log("🎭 函数示例 (Function example):");
113+
console.log(greetUser("CodeForge用户"));
114+
console.log("");
115+
116+
// 数组方法示例
117+
console.log("💾 数组方法示例 (Array methods example):");
118+
const dynamicArray = Array.from({ length: 5 }, (_, i) => (i + 1) * 10);
119+
printArray(dynamicArray, "动态数组");
120+
121+
// Map 和 Filter 示例
122+
const squares = dynamicArray.map(x => x * x);
123+
const evenNumbers = dynamicArray.filter(x => x % 20 === 0);
124+
printArray(squares, "平方数");
125+
printArray(evenNumbers, "能被20整除的数");
126+
console.log("");
127+
128+
// 解构赋值示例
129+
console.log("🔍 解构赋值示例 (Destructuring example):");
130+
const [first, second, ...rest] = fruits;
131+
console.log(`第一个水果: ${first}`);
132+
console.log(`第二个水果: ${second}`);
133+
console.log(`其余水果: ${rest.join(', ')}`);
134+
135+
const { name: personName, age } = person;
136+
console.log(`通过解构获取: ${personName}, ${age}岁`);
137+
console.log("");
138+
139+
// 递归示例
140+
console.log("🔄 递归示例 (Recursion example):");
141+
const fibN = 7;
142+
const fibResult = fibonacci(fibN);
143+
console.log(`斐波那契数列第${fibN}项: ${fibResult}`);
144+
console.log("");
145+
146+
// 数学库示例
147+
console.log("📐 数学库示例 (Math library example):");
148+
const angle = 45.0;
149+
const radians = (angle * Math.PI) / 180.0;
150+
console.log(`sin(${angle}°) = ${Math.sin(radians).toFixed(4)}`);
151+
console.log(`cos(${angle}°) = ${Math.cos(radians).toFixed(4)}`);
152+
console.log(`sqrt(16) = ${Math.sqrt(16).toFixed(2)}`);
153+
console.log("");
154+
155+
// 位操作示例
156+
console.log("🔧 位操作示例 (Bitwise operations):");
157+
const a = 12; // 1100 in binary
158+
const b = 10; // 1010 in binary
159+
console.log(`${a} & ${b} = ${a & b} (AND)`);
160+
console.log(`${a} | ${b} = ${a | b} (OR)`);
161+
console.log(`${a} ^ ${b} = ${a ^ b} (XOR)`);
162+
console.log(`~${a} = ${~a} (NOT)`);
163+
console.log("");
164+
165+
// 枚举示例
166+
console.log("📋 枚举示例 (Enum example):");
167+
const today = Weekday.WEDNESDAY;
168+
console.log(`今天是星期${today}`);
169+
console.log("");
170+
171+
// Promise 和 async/await 示例
172+
console.log("⚡ 异步示例 (Async example):");
173+
const asyncExample = async () => {
174+
return new Promise((resolve) => {
175+
setTimeout(() => resolve("异步操作完成!"), 100);
176+
});
177+
};
178+
179+
asyncExample().then((message) => {
180+
console.log(message);
181+
console.log("");
182+
183+
// 类示例
184+
console.log("🎯 类示例 (Class example):");
185+
class Calculator {
186+
constructor() {
187+
this.history = [];
188+
}
189+
190+
add(a, b) {
191+
const result = a + b;
192+
this.history.push(`${a} + ${b} = ${result}`);
193+
return result;
194+
}
195+
196+
getHistory() {
197+
return [...this.history];
198+
}
199+
}
200+
201+
const calc = new Calculator();
202+
calc.add(5, 3);
203+
calc.add(10, 7);
204+
console.log("计算历史:", calc.getHistory());
205+
console.log("");
206+
207+
console.log("🎯 CodeForge JavaScript 代码执行完成!");
208+
console.log("🎯 CodeForge JavaScript execution completed!");
209+
console.log("");
210+
console.log("感谢使用 CodeForge 代码执行环境! 🚀");
211+
console.log("Thank you for using CodeForge! 🚀");
212+
});
213+
}
214+
215+
// 运行主函数
216+
main();

src-tauri/src/plugins/javascript_nodejs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use super::{LanguagePlugin, PluginConfig};
22
use std::vec;
33

4-
pub struct LanguageNodeJsPlugin;
4+
pub struct JavaScriptNodeJsPlugin;
55

6-
impl LanguagePlugin for LanguageNodeJsPlugin {
6+
impl LanguagePlugin for JavaScriptNodeJsPlugin {
77
fn get_order(&self) -> i32 {
88
13
99
}

src-tauri/src/plugins/manager.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::plugins::c::CPlugin;
44
use crate::plugins::clojure::ClojurePlugin;
55
use crate::plugins::go::GoPlugin;
66
use crate::plugins::java::JavaPlugin;
7-
use crate::plugins::javascript_nodejs::LanguageNodeJsPlugin;
7+
use crate::plugins::javascript_nodejs::JavaScriptNodeJsPlugin;
88
use crate::plugins::kotlin::KotlinPlugin;
99
use crate::plugins::nodejs::NodeJSPlugin;
1010
use crate::plugins::python2::Python2Plugin;
@@ -14,6 +14,7 @@ use crate::plugins::rust::RustPlugin;
1414
use crate::plugins::scala::ScalaPlugin;
1515
use crate::plugins::shell::ShellPlugin;
1616
use crate::plugins::swift::SwiftPlugin;
17+
use crate::plugins::typescript_nodejs::TypeScriptNodeJsPlugin;
1718
use std::collections::HashMap;
1819

1920
pub struct PluginManager {
@@ -40,7 +41,11 @@ impl PluginManager {
4041
plugins.insert("applescript".to_string(), Box::new(AppleScriptPlugin));
4142
plugins.insert(
4243
"javascript-nodejs".to_string(),
43-
Box::new(LanguageNodeJsPlugin),
44+
Box::new(JavaScriptNodeJsPlugin),
45+
);
46+
plugins.insert(
47+
"typescript-nodejs".to_string(),
48+
Box::new(TypeScriptNodeJsPlugin),
4449
);
4550

4651
Self { plugins }

src-tauri/src/plugins/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,5 +361,6 @@ pub mod rust;
361361
pub mod scala;
362362
pub mod shell;
363363
pub mod swift;
364+
pub mod typescript_nodejs;
364365

365366
pub use manager::PluginManager;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use super::{LanguagePlugin, PluginConfig};
2+
use std::vec;
3+
4+
pub struct TypeScriptNodeJsPlugin;
5+
6+
impl LanguagePlugin for TypeScriptNodeJsPlugin {
7+
fn get_order(&self) -> i32 {
8+
16
9+
}
10+
11+
fn get_language_name(&self) -> &'static str {
12+
"TypeScript (Node.js)"
13+
}
14+
15+
fn get_language_key(&self) -> &'static str {
16+
"typescript-nodejs"
17+
}
18+
19+
fn get_file_extension(&self) -> String {
20+
self.get_config()
21+
.map(|config| config.extension.clone())
22+
.unwrap_or_else(|| "ts".to_string())
23+
}
24+
25+
fn get_version_args(&self) -> Vec<&'static str> {
26+
vec!["--version"]
27+
}
28+
29+
fn get_path_command(&self) -> String {
30+
"which node".to_string()
31+
}
32+
33+
fn get_default_config(&self) -> PluginConfig {
34+
PluginConfig {
35+
enabled: true,
36+
language: String::from("typescript-nodejs"),
37+
before_compile: None,
38+
extension: String::from("ts"),
39+
execute_home: None,
40+
run_command: Some(String::from("node $filename")),
41+
after_compile: None,
42+
template: Some(String::from("// 在这里输入 TypeScript (Node.js) 代码")),
43+
timeout: Some(30),
44+
}
45+
}
46+
47+
fn get_default_command(&self) -> String {
48+
self.get_config()
49+
.and_then(|config| config.run_command.clone())
50+
.unwrap_or_else(|| "node".to_string())
51+
}
52+
}

src/composables/useCodeMirrorEditor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ export function useCodeMirrorEditor(props: Props)
184184
return StreamLanguage.define(clojure)
185185
case 'ruby':
186186
return StreamLanguage.define(ruby)
187+
case 'typescript-nodejs':
188+
return javascript({typescript: true})
187189
default:
188190
return null
189191
}

0 commit comments

Comments
 (0)