添加 CodeSpanControl 及相关功能改进
在 `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 内容格式。
This commit is contained in:
parent
b85080c2ce
commit
67d7e93217
|
@ -0,0 +1,32 @@
|
|||
<UserControl x:Class="EleCho.MdViewer.Controls.CodeSpanControl"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:EleCho.MdViewer.Controls"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="30" d:DesignWidth="160">
|
||||
<StackPanel Orientation="Horizontal" Margin="5">
|
||||
<local:SelectableTextBlock Text="{Binding Text}" VerticalAlignment="Center"/>
|
||||
<Button x:Name="Copy"
|
||||
Content="复制"
|
||||
Click="Copy_Click"
|
||||
VerticalAlignment="Top"
|
||||
HorizontalAlignment="Right"
|
||||
Padding="4,0"
|
||||
Opacity="1"
|
||||
Height="20"
|
||||
Background="#AAFFFFFF"
|
||||
BorderThickness="1">
|
||||
<Button.Style>
|
||||
<Style TargetType="Button">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="True">
|
||||
<Setter Property="Opacity" Value="0.8"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Button.Style>
|
||||
</Button>
|
||||
</StackPanel>
|
||||
</UserControl>
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// CodeSpanControl.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class CodeSpanControl : UserControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Text
|
||||
/// </summary>
|
||||
public string Text
|
||||
{
|
||||
get => (string)GetValue(TextProperty);
|
||||
|
||||
set => SetValue(TextProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Text property
|
||||
/// </summary>
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -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. 你一定要使用<think>开始告诉用户的思考,并使用</think>结束你的思考。
|
||||
7. 当你输出公式时,请使用以下格式进行输出:
|
||||
```Excel_{{列}}
|
||||
=SUM(A1:A100)
|
||||
```
|
||||
现在的时间是{DateTime.Now}。
|
||||
|
||||
当前用户提问的excel文件列信息为:{excelPrompt}
|
||||
|
|
|
@ -23,6 +23,12 @@
|
|||
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
|
||||
<OptimizationPreference>Size</OptimizationPreference>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Remove="Web\**" />
|
||||
<EmbeddedResource Remove="Web\**" />
|
||||
<None Remove="Web\**" />
|
||||
<Page Remove="Web\**" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="HL\SQL.xshd" />
|
||||
</ItemGroup>
|
||||
|
@ -69,9 +75,6 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0-rc.2.24473.5" />
|
||||
<PackageReference Include="TinyPinyin" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Web\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\EleCho.MdViewer\src\EleCho.MdViewer\EleCho.MdViewer.csproj" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -14,7 +14,7 @@ public class Excel2Prompt
|
|||
/// <param name="columns"></param>
|
||||
/// <param name="startCell">A</param>
|
||||
/// <returns></returns>
|
||||
public static string ConverterToPrompt(IEnumerable<dynamic> excelData, IEnumerable<string> columns, string startCell)
|
||||
public static string ConverterToPrompt(IEnumerable<dynamic> excelData, IEnumerable<string> columns, string startCell,string sheetName)
|
||||
{
|
||||
/*
|
||||
示例:
|
||||
|
@ -37,7 +37,7 @@ public class Excel2Prompt
|
|||
- Missing values: 7007
|
||||
*/
|
||||
var prompts = new List<string>();
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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 = @"
|
||||
<think>
|
||||
1231231
|
||||
</think>
|
||||
private static string TEST_AI_CONTENT = @"
|
||||
```Excel_H
|
||||
1231231122222222222222222222
|
||||
123
|
||||
1
|
||||
23
|
||||
12
|
||||
3
|
||||
12
|
||||
```
|
||||
";
|
||||
#region Props
|
||||
[ObservableProperty]
|
||||
|
|
Loading…
Reference in New Issue