Skip to content

Commit 3cc61ed

Browse files
committed
编辑体验优化
1 parent 1d5f797 commit 3cc61ed

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

StickyHomeworks/Behaviors/RichTextBoxBindingBehavior.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,7 @@ public static void SetDocumentXaml(DependencyObject obj, string value)
6262
// Parse the XAML to a document (or use XamlReader.Parse())
6363

6464
var documentXaml = GetDocumentXaml(b);
65-
try
66-
{
67-
var stream = new MemoryStream(Encoding.UTF8.GetBytes(documentXaml));
68-
var doc = (FlowDocument)XamlReader.Load(stream);
69-
// Set the document
70-
richTextBox.Document = doc;
71-
}
72-
catch (Exception)
73-
{
74-
richTextBox.Document = new FlowDocument();
75-
var para = new Paragraph();
76-
para.Inlines.Add(documentXaml);
77-
richTextBox.Document.Blocks.Add(para);
78-
}
65+
richTextBox.Document = RichTextBoxHelper.ConvertDocument(documentXaml);
7966
richTextBox.Document.IsOptimalParagraphEnabled = true;
8067

8168
// When the document changes update the source

StickyHomeworks/Controls/HomeworkControl.xaml.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Windows.Navigation;
1515
using System.Windows.Shapes;
1616
using ElysiaFramework;
17+
using Stfu.Linq;
1718
using StickyHomeworks.Models;
1819
using StickyHomeworks.Views;
1920

@@ -72,19 +73,26 @@ private void IsEditingChanged(bool value)
7273
if (IsSelected && value)
7374
{
7475
Debug.WriteLine("RelatedRichTextBox updated because IsEditing changed");
75-
AppEx.GetService<HomeworkEditWindow>().RelatedRichTextBox = RichTextBox;
76-
RichTextBox.Focus();
76+
EnterEdit();
7777
}
7878
}
7979

80+
private async void EnterEdit()
81+
{
82+
AppEx.GetService<HomeworkEditWindow>().RelatedRichTextBox = RichTextBox;
83+
await System.Windows.Threading.Dispatcher.Yield();
84+
RichTextBox.Focus();
85+
//RichTextBox.Selection.Select(new TextPointer());
86+
RichTextBox.CaretPosition = RichTextBox.CaretPosition.DocumentEnd;
87+
}
88+
8089
private void IsSelectedChanged(bool value)
8190
{
8291
Debug.WriteLine($"IsSelected changed! {value} {IsEditing}");
8392
if (value && IsEditing)
8493
{
8594
Debug.WriteLine("RelatedRichTextBox updated because IsSelected changed");
86-
AppEx.GetService<HomeworkEditWindow>().RelatedRichTextBox = RichTextBox;
87-
RichTextBox.Focus();
95+
EnterEdit();
8896
}
8997
}
9098
}

StickyHomeworks/MainWindow.xaml.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,28 @@ public MainWindow(ProfileService profileService,
4646
Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler);
4747
InitializeComponent();
4848
ViewModel.PropertyChanged += ViewModelOnPropertyChanged;
49+
ViewModel.PropertyChanging += ViewModelOnPropertyChanging;
4950
DataContext = this;
5051
}
5152

53+
private void ViewModelOnPropertyChanging(object? sender, PropertyChangingEventArgs e)
54+
{
55+
if (e.PropertyName == nameof(ViewModel.SelectedHomework))
56+
{
57+
ExitEditingMode(true);
58+
}
59+
}
60+
5261
private void ViewModelOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
5362
{
5463
if (e.PropertyName == nameof(ViewModel.SelectedListBoxItem))
5564
{
5665
RepositionEditingWindow();
5766
}
67+
if (e.PropertyName == nameof(ViewModel.SelectedHomework))
68+
{
69+
ExitEditingMode(false);
70+
}
5871
}
5972

6073
private void OnFocusChangedHandler(object sender, AutomationFocusChangedEventArgs e)
@@ -83,10 +96,16 @@ private void OnFocusChangedHandler(object sender, AutomationFocusChangedEventArg
8396

8497
private void ExitEditingMode(bool hard=true)
8598
{
99+
if (ViewModel.IsCreatingMode)
100+
{
101+
ViewModel.IsCreatingMode = false;
102+
return;
103+
}
86104
if (hard)
87105
MainListView.SelectedIndex = -1;
88106
ViewModel.IsDrawerOpened = false;
89107
AppEx.GetService<HomeworkEditWindow>().TryClose();
108+
AppEx.GetService<ProfileService>().SaveProfile();
90109
}
91110

92111
private void SetPos()
@@ -162,7 +181,7 @@ private void ButtonCreateHomework_OnClick(object sender, RoutedEventArgs e)
162181
ViewModel.IsUpdatingHomeworkSubject = true;
163182
OnHomeworkEditorUpdated?.Invoke(this ,EventArgs.Empty);
164183
var lastSubject = ViewModel.EditingHomework.Subject;
165-
//ViewModel.IsCreatingMode = true;
184+
ViewModel.IsCreatingMode = true;
166185
ViewModel.IsDrawerOpened = true;
167186
var o = new Homework()
168187
{
@@ -365,6 +384,10 @@ private async void ButtonExport_OnClick(object sender, RoutedEventArgs e)
365384
}
366385

367386
var file = dialog.FileName!;
387+
var fd = new FlowDocument()
388+
{
389+
IsOptimalParagraphEnabled = true
390+
};
368391
try
369392
{
370393
await Task.Run(() =>
@@ -420,6 +443,6 @@ private void ButtonMore_Click(object sender, RoutedEventArgs e)
420443

421444
private void MainListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
422445
{
423-
ExitEditingMode(false);
446+
//ExitEditingMode(false);
424447
}
425448
}

StickyHomeworks/RichTextBoxHelper.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,25 @@
88

99
namespace StickyHomeworks;
1010

11-
public class RichTextBoxHelper : DependencyObject
11+
public static class RichTextBoxHelper
1212
{
13-
13+
public static FlowDocument ConvertDocument(string xaml)
14+
{
15+
try
16+
{
17+
var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml));
18+
var doc = (FlowDocument)XamlReader.Load(stream);
19+
doc.IsOptimalParagraphEnabled = true;
20+
return doc;
21+
}
22+
catch (Exception)
23+
{
24+
var doc = new FlowDocument();
25+
var para = new Paragraph();
26+
doc.IsOptimalParagraphEnabled = true;
27+
para.Inlines.Add(xaml);
28+
doc.Blocks.Add(para);
29+
return doc;
30+
}
31+
}
1432
}

StickyHomeworks/Views/HomeworkEditWindow.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Windows.Media;
1515
using System.Windows.Media.Imaging;
1616
using System.Windows.Shapes;
17+
using ElysiaFramework;
1718
using StickyHomeworks.Services;
1819
using StickyHomeworks.ViewModels;
1920

@@ -205,6 +206,7 @@ private void ButtonFontSizeIncrease_OnClick(object sender, RoutedEventArgs e)
205206
private void ButtonEditingDone_OnClick(object sender, RoutedEventArgs e)
206207
{
207208
EditingFinished?.Invoke(this, EventArgs.Empty);
209+
AppEx.GetService<ProfileService>().SaveProfile();
208210
}
209211

210212
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)

0 commit comments

Comments
 (0)