You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
5.2 KiB
144 lines
5.2 KiB
using Microsoft.Extensions.Logging;
|
|
using ReactiveUI;
|
|
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Reactive;
|
|
using AvaloniaEdit.Document;
|
|
|
|
namespace MyAvaloniaApp.ViewModels.Pages;
|
|
|
|
/// <summary>
|
|
/// 代码编辑器页面的 ViewModel
|
|
/// </summary>
|
|
public class EditorPageViewModel : ReactiveObject
|
|
{
|
|
private string _selectedLanguage = "C#";
|
|
private TextDocument _document;
|
|
|
|
private readonly ILogger<EditorPageViewModel>? _logger;
|
|
|
|
public ObservableCollection<string> AvailableLanguages { get; }
|
|
|
|
public string SelectedLanguage
|
|
{
|
|
get => _selectedLanguage;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedLanguage, value);
|
|
}
|
|
|
|
public TextDocument Document
|
|
{
|
|
get => _document;
|
|
set => this.RaiseAndSetIfChanged(ref _document, value);
|
|
}
|
|
|
|
// 响应式命令
|
|
public ReactiveCommand<Unit, Unit> ClearCodeCommand { get; }
|
|
public ReactiveCommand<Unit, Unit> FormatCodeCommand { get; }
|
|
|
|
public EditorPageViewModel(ILogger<EditorPageViewModel>? logger = null)
|
|
{
|
|
_logger = logger;
|
|
|
|
// 先初始化 Document,确保绑定时有值
|
|
_document = new TextDocument();
|
|
|
|
AvailableLanguages = new ObservableCollection<string>
|
|
{
|
|
"C#",
|
|
"JavaScript",
|
|
"TypeScript",
|
|
"HTML",
|
|
"CSS",
|
|
"JSON",
|
|
"XML",
|
|
"Python",
|
|
"Java",
|
|
"SQL"
|
|
};
|
|
|
|
_logger?.LogInformation("EditorPageViewModel 已创建");
|
|
|
|
// 初始化示例代码
|
|
InitializeDefaultCode();
|
|
|
|
// 创建命令
|
|
ClearCodeCommand = ReactiveCommand.Create(ClearCode);
|
|
FormatCodeCommand = ReactiveCommand.Create(FormatCode);
|
|
|
|
// 监听语言选择变化
|
|
this.WhenAnyValue(x => x.SelectedLanguage)
|
|
.Subscribe(lang =>
|
|
{
|
|
_logger?.LogInformation("切换语言: {Language}", lang);
|
|
UpdateCodeForLanguage(lang);
|
|
});
|
|
}
|
|
|
|
private void InitializeDefaultCode()
|
|
{
|
|
var defaultCode = "// 欢迎使用 AvaloniaEdit.TextMate 代码编辑器\n" +
|
|
"// 选择不同的编程语言查看语法高亮效果\n" +
|
|
"\n" +
|
|
"using System;\n" +
|
|
"\n" +
|
|
"namespace MyAvaloniaApp\n" +
|
|
"{\n" +
|
|
" public class Program\n" +
|
|
" {\n" +
|
|
" public static void Main(string[] args)\n" +
|
|
" {\n" +
|
|
" Console.WriteLine(\"Hello, AvaloniaEdit!\");\n" +
|
|
" }\n" +
|
|
" }\n" +
|
|
"}";
|
|
|
|
Document.Text = defaultCode;
|
|
}
|
|
|
|
private void UpdateCodeForLanguage(string language)
|
|
{
|
|
Document.Text = GetDefaultCode(language);
|
|
_logger?.LogInformation("代码已更新为: {Language}", language);
|
|
}
|
|
|
|
private string GetDefaultCode(string language)
|
|
{
|
|
return language switch
|
|
{
|
|
"C#" => "using System;\n\nnamespace Example\n{\n public class Demo\n {\n static void Main()\n {\n Console.WriteLine(\"Hello World!\");\n }\n }\n}",
|
|
|
|
"JavaScript" => "// JavaScript 示例\nfunction greet(name) {\n return `Hello, ${name}!`;\n}\n\nconst person = 'World';\nconsole.log(greet(person));",
|
|
|
|
"TypeScript" => "// TypeScript 示例\ninterface Person {\n name: string;\n age: number;\n}\n\nconst user: Person = {\n name: 'Alice',\n age: 30\n};\n\nconsole.log(user);",
|
|
|
|
"HTML" => "<!DOCTYPE html>\n<html>\n<head>\n <title>示例</title>\n</head>\n<body>\n <h1>Hello World</h1>\n</body>\n</html>",
|
|
|
|
"CSS" => "/* CSS 示例 */\nbody {\n font-family: Arial, sans-serif;\n margin: 0;\n padding: 20px;\n}\n\n.container {\n max-width: 1200px;\n margin: 0 auto;\n}",
|
|
|
|
"JSON" => "{\n \"name\": \"MyApp\",\n \"version\": \"1.0.0\",\n \"dependencies\": {}\n}",
|
|
|
|
"XML" => "<?xml version=\"1.0\"?>\n<root>\n <item>示例数据</item>\n</root>",
|
|
|
|
"Python" => "# Python 示例\ndef greet(name):\n return f'Hello, {name}!'\n\nprint(greet('World'))",
|
|
|
|
"Java" => "public class Example {\n public static void main(String[] args) {\n System.out.println(\"Hello World\");\n }\n}",
|
|
|
|
"SQL" => "-- SQL 示例\nSELECT * FROM Users;\n\nINSERT INTO Users (name, email) VALUES ('John', 'john@example.com');",
|
|
|
|
_ => "// 未支持的编程语言"
|
|
};
|
|
}
|
|
|
|
private void ClearCode()
|
|
{
|
|
Document.Text = string.Empty;
|
|
_logger?.LogInformation("代码已清空");
|
|
}
|
|
|
|
private void FormatCode()
|
|
{
|
|
_logger?.LogInformation("格式化代码: {Language}", SelectedLanguage);
|
|
// 这里可以添加代码格式化逻辑
|
|
}
|
|
}
|
|
|
|
|