From 67d7e93217a8d1a55296454d414ae89318a8fccb Mon Sep 17 00:00:00 2001 From: Ling0925 <2449858657a@gmail.com> Date: Tue, 4 Mar 2025 23:39:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20CodeSpanControl=20?= =?UTF-8?q?=E5=8F=8A=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `CodeSpanControl.xaml` 中添加了用户控件,包含可选择的文本块和复制按钮,按钮样式在鼠标悬停时改变透明度。 在 `CodeSpanControl.xaml.cs` 中实现了 `Text` 属性的依赖属性支持,并添加了空的 `Copy_Click` 方法。 修改了 `MarkdownWpfRenderer.cs` 中的代码块渲染逻辑,移除进度条并在特定条件下使用 `CodeSpanControl`。 在 `PromptUtil.cs` 中更新提示文本,增加对 WPS 的支持,并调整思考过程格式。 移除 `ExcelHelper.csproj` 中与 Web 相关的项目项。 在 `Excel2Prompt.cs` 中修改 `ConverterToPrompt` 方法,增加 `sheetName` 参数并调整提示文本格式。 更新 `ImportViewModel.cs` 中对 `Excel2Prompt.ConverterToPrompt` 方法的调用,传递新参数并修改测试 AI 内容格式。 --- .../Controls/CodeSpanControl.xaml | 32 +++++++++ .../Controls/CodeSpanControl.xaml.cs | 51 +++++++++++++++ .../Markdown/MarkdownWpfRenderer.cs | 65 ++++++++++++------- src/ExcelHelper/AI/PromptUtil.cs | 9 ++- src/ExcelHelper/ExcelHelper.csproj | 9 ++- src/ExcelHelper/Utils/Excel2Prompt.cs | 6 +- .../Views/ViewModels/ImportViewModel.cs | 18 +++-- 7 files changed, 151 insertions(+), 39 deletions(-) create mode 100644 src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml create mode 100644 src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml.cs diff --git a/src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml b/src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml new file mode 100644 index 0000000..7a2e1c9 --- /dev/null +++ b/src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml @@ -0,0 +1,32 @@ + + + + + + diff --git a/src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml.cs b/src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml.cs new file mode 100644 index 0000000..c5a76d6 --- /dev/null +++ b/src/EleCho.MdViewer/src/EleCho.MdViewer/Controls/CodeSpanControl.xaml.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace EleCho.MdViewer.Controls +{ + /// + /// CodeSpanControl.xaml 的交互逻辑 + /// + public partial class CodeSpanControl : UserControl + { + /// + /// Text + /// + public string Text + { + get => (string)GetValue(TextProperty); + + set => SetValue(TextProperty, value); + } + + /// + /// Text property + /// + public static readonly DependencyProperty TextProperty = + DependencyProperty.Register("Text", typeof(string), typeof(CodeSpanControl), new PropertyMetadata(string.Empty)); + + + public CodeSpanControl() + { + InitializeComponent(); + this.DataContext = this; + } + + private void Copy_Click(object sender, RoutedEventArgs e) + { + + } + } +} diff --git a/src/EleCho.MdViewer/src/EleCho.MdViewer/Markdown/MarkdownWpfRenderer.cs b/src/EleCho.MdViewer/src/EleCho.MdViewer/Markdown/MarkdownWpfRenderer.cs index 17dd35f..e559b50 100644 --- a/src/EleCho.MdViewer/src/EleCho.MdViewer/Markdown/MarkdownWpfRenderer.cs +++ b/src/EleCho.MdViewer/src/EleCho.MdViewer/Markdown/MarkdownWpfRenderer.cs @@ -371,8 +371,12 @@ public class MarkdownWpfRenderer htmlText.Text = text; var expander = new Expander(); var header = new StackPanel(); - header.Children.Add(new ProgressBar() { IsIndeterminate = true,Width = 30 }); + header.Orientation = Orientation.Horizontal; + header.HorizontalAlignment = HorizontalAlignment.Center; + header.Children.Add(new TextBlock() { Text="思考内容",Margin = new Thickness(0,0,5,0) }); + //header.Children.Add(new ProgressBar() { IsIndeterminate = true,Width = 30,Height= 5 }); expander.Header = header; + expander.IsExpanded = false; expander.Content = htmlText; return expander; @@ -408,38 +412,49 @@ public class MarkdownWpfRenderer CornerRadius = new CornerRadius(8), Margin = new Thickness(0, 0, 0, NormalSize) }; - - var codeContentElement = new SelectableTextBlock() + if (!fencedCodeBlock.Info.StartsWith("Excel", StringComparison.CurrentCultureIgnoreCase)) { - TextWrapping = TextWrapping.Wrap, - Padding = new Thickness(NormalSize / 2), + var codeContentElement = new SelectableTextBlock() + { + TextWrapping = TextWrapping.Wrap, + Padding = new Thickness(NormalSize / 2), - FontSize = NormalSize, - FontFamily = GetCodeTextFontFamily(), - }; + FontSize = NormalSize, + FontFamily = GetCodeTextFontFamily(), + }; - codeElement.Child = - codeContentElement; - codeElement.BindCodeBlockBackground(); + codeElement.Child = + codeContentElement; + codeElement.BindCodeBlockBackground(); - codeContentElement.BindCodeBlockForeground(); + codeContentElement.BindCodeBlockForeground(); - var language = ColorCode.Languages.FindById(fencedCodeBlock.Info); + var language = ColorCode.Languages.FindById(fencedCodeBlock.Info); - /*StyleDictionary styleDict = ColorMode switch + /*StyleDictionary styleDict = ColorMode switch + { + ColorMode.Light => StyleDictionary.DefaultLight, + ColorMode.Dark => StyleDictionary.DefaultDark, + + _ => StyleDictionary.DefaultLight + };*/ + StyleDictionary styleDict = StyleDictionary.DefaultDark; + + WpfSyntaxHighLighting writer = new(styleDict); + writer.FormatTextBlock(fencedCodeBlock.Lines.ToString(), language, codeContentElement); + + } + else { - ColorMode.Light => StyleDictionary.DefaultLight, - ColorMode.Dark => StyleDictionary.DefaultDark, - - _ => StyleDictionary.DefaultLight - };*/ - StyleDictionary styleDict = StyleDictionary.DefaultDark; - - WpfSyntaxHighLighting writer = new(styleDict); - writer.FormatTextBlock(fencedCodeBlock.Lines.ToString(), language, codeContentElement); + var codeSpanControl = new CodeSpanControl(); + codeSpanControl.SetResourceReference(Control.ForegroundProperty, MarkdownResKey.MainForeground); + codeSpanControl.SetResourceReference(Control.BackgroundProperty, MarkdownResKey.CodeBlockBackground); + codeSpanControl.Text = fencedCodeBlock.Lines.ToString(); + codeElement.Child = codeSpanControl; + } - return codeElement; + return codeElement; } public FrameworkElement RenderCodeBlock(CodeBlock codeBlock, CancellationToken cancellationToken) @@ -546,7 +561,7 @@ public class MarkdownWpfRenderer SelectableTextBlock paragraphElement = new SelectableTextBlock() { TextWrapping = TextWrapping.Wrap, - Margin = new Thickness(0, 0, 0, NormalSize), + Margin = new Thickness(0, NormalSize, 0, NormalSize), FontSize = NormalSize, }; diff --git a/src/ExcelHelper/AI/PromptUtil.cs b/src/ExcelHelper/AI/PromptUtil.cs index 338dec7..9e8733d 100644 --- a/src/ExcelHelper/AI/PromptUtil.cs +++ b/src/ExcelHelper/AI/PromptUtil.cs @@ -11,7 +11,7 @@ class PromptUtil public static string UsePrompt(string msg,string excelPrompt) { return $@" -你是一个Excel公式大师,你需要帮助用户解答Excel公式相关的问题。 +你是一个Excel和WPS公式大师,你需要帮助用户解答Excel与WPS公式相关的问题。 你在回复用户的问题时,需要遵守以下规则: 1. 优先保证答案的准确性,其次是简洁明了。 @@ -19,8 +19,11 @@ class PromptUtil 3. 可以例举1到2个例子来帮助用户更好地理解你的回答中的Excel公式。 4. 一定要保证文档格式的正确性,避免出现无法显示的内容。 5. 尽量使用比较简短与易懂的公式来回答用户的问题,避免用户无法理解。 - 6. 你不能跳过你的思考过程。 - + 6. 你一定要使用开始告诉用户的思考,并使用结束你的思考。 + 7. 当你输出公式时,请使用以下格式进行输出: + ```Excel_{{列}} + =SUM(A1:A100) + ``` 现在的时间是{DateTime.Now}。 当前用户提问的excel文件列信息为:{excelPrompt} diff --git a/src/ExcelHelper/ExcelHelper.csproj b/src/ExcelHelper/ExcelHelper.csproj index 34fd77f..ea7fb42 100644 --- a/src/ExcelHelper/ExcelHelper.csproj +++ b/src/ExcelHelper/ExcelHelper.csproj @@ -23,6 +23,12 @@ true Size + + + + + + @@ -69,9 +75,6 @@ - - - diff --git a/src/ExcelHelper/Utils/Excel2Prompt.cs b/src/ExcelHelper/Utils/Excel2Prompt.cs index bb2512d..0d80b39 100644 --- a/src/ExcelHelper/Utils/Excel2Prompt.cs +++ b/src/ExcelHelper/Utils/Excel2Prompt.cs @@ -14,7 +14,7 @@ public class Excel2Prompt /// /// A /// - public static string ConverterToPrompt(IEnumerable excelData, IEnumerable columns, string startCell) + public static string ConverterToPrompt(IEnumerable excelData, IEnumerable columns, string startCell,string sheetName) { /* 示例: @@ -37,7 +37,7 @@ public class Excel2Prompt - Missing values: 7007 */ var prompts = new List(); - prompts.Add("This Excel file contains the following columns:"); + prompts.Add($"This Excel file contains the following columns:"); var dataList = excelData.ToList(); // 解析 startCell,获取起始列的索引 @@ -68,6 +68,8 @@ public class Excel2Prompt $" - Sample values: ['{string.Join("', '", sampleValues)}']"; prompts.Add(prompt); } + prompts.Add($"Sheet Name: {sheetName}"); + prompts.Add($"Max Row: {excelData.Count()}"); return string.Join(Environment.NewLine + Environment.NewLine, prompts); } diff --git a/src/ExcelHelper/Views/ViewModels/ImportViewModel.cs b/src/ExcelHelper/Views/ViewModels/ImportViewModel.cs index 3f9f567..fce9351 100644 --- a/src/ExcelHelper/Views/ViewModels/ImportViewModel.cs +++ b/src/ExcelHelper/Views/ViewModels/ImportViewModel.cs @@ -92,7 +92,7 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel } MaxRow = ExcelData.Count(); - ExcelPromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns,StartCell); + ExcelPromptString = Excel2Prompt.ConverterToPrompt(excelData, columns,StartCell,currentSheetName); } catch (Exception ex) { @@ -285,7 +285,7 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel }; Messages.Add(aiChat); -#if !DEBUG +#if DEBUG var results = TEST_AI_CONTENT.Split('\n'); foreach(var result in results) { @@ -311,10 +311,16 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel } - private const string TEST_AI_CONTENT = @" - -1231231 - + private static string TEST_AI_CONTENT = @" +```Excel_H +1231231122222222222222222222 +123 +1 +23 +12 +3 +12 +``` "; #region Props [ObservableProperty]