Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Moved everything markdown related into its viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Oct 9, 2019
1 parent f8aed44 commit 9ecfb08
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 851 deletions.
2 changes: 1 addition & 1 deletion Docdown/Controls/EditorAndViewer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ColumnDefinition.Style>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Editor:MarkdownEditor Grid.Column="0" x:Name="MdEditor"/>
<Editor:DefaultEditor Grid.Column="0" x:Name="MdEditor"/>
<Border Grid.Column="1"
BorderThickness="1 0 0 0"
BorderBrush="{DynamicResource GrayBrush9}"
Expand Down
7 changes: 0 additions & 7 deletions Docdown/Docdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,6 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Editor\MarkdownEditor.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\EditorAndViewer.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -346,9 +342,6 @@
<Compile Include="Controls\DocumentViewer.xaml.cs">
<DependentUpon>DocumentViewer.xaml</DependentUpon>
</Compile>
<Compile Include="Editor\MarkdownEditor.xaml.cs">
<DependentUpon>MarkdownEditor.xaml</DependentUpon>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down
111 changes: 105 additions & 6 deletions Docdown/Editor/DefaultEditor.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.Windows.Controls;
using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Docdown.Util;
using Docdown.ViewModel;
using Docdown.ViewModel.Editing;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Folding;

namespace Docdown.Editor
Expand All @@ -13,16 +17,19 @@ public partial class DefaultEditor : UserControl, IEditor
private bool firstChange = false;

private FoldingManager foldingManager;
private CompletionWindow completionWindow;
private readonly ToolTip toolTip = new ToolTip();

public DefaultEditor()
{
InitializeComponent();
var debounced = ((Action)UpdateDebounced).Debounce(500);

Editor.TextChanged += delegate
{
if (DataContext is WorkspaceItemViewModel item)
{
item.Editor.Text = EditBox.Text;
item.Editor.Text = Editor.Text;
if (firstChange)
{
item.HasChanged = true;
Expand All @@ -36,25 +43,117 @@ public DefaultEditor()
{
foldingManager.UpdateFoldings(foldingStrategy.GenerateFoldings(), -1);
}
debounced();
}
firstChange = true;
};

Editor.TextArea.TextEntered += TextEntered;
Editor.TextArea.TextEntering += TextEntering;
Editor.TextArea.PreviewKeyDown += ShowCompletionWindowKeyboard;

Editor.MouseHover += TextEditorMouseHover;
Editor.MouseHoverStopped += TextEditorMouseHoverStopped;
}

private void UpdateDebounced()
{
Dispatcher.BeginInvoke((Action)(() =>
{
if (DataContext is WorkspaceItemViewModel item)
{
item.Editor.Update();
}
}));
}

void TextEditorMouseHover(object sender, MouseEventArgs e)
{
var pos = Editor.GetPositionFromPoint(e.GetPosition(Editor));
if (pos != null && DataContext is WorkspaceItemViewModel item)
{
int index = Editor.Document.GetOffset(pos.Value.Line, pos.Value.Column);
var editor = item.Editor;
var content = editor.FindHoverContent(index);
if (content != null)
{
toolTip.PlacementTarget = this;
toolTip.Content = content;
toolTip.IsOpen = true;
e.Handled = true;
}
}
}

void TextEditorMouseHoverStopped(object sender, MouseEventArgs e)
{
toolTip.IsOpen = false;
}

private void ShowCompletionWindowKeyboard(object sender, KeyEventArgs e)
{
if (completionWindow == null && e.Key == Key.Space && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
ShowCompletionWindow();
e.Handled = true;
}
}

private void TextEntered(object sender, TextCompositionEventArgs e)
{
if (e.Text.Length == 1 && DataContext is WorkspaceItemViewModel item)
{
var c = e.Text[0];
if (item.Editor.CompletionMarkers != null && item.Editor.CompletionMarkers.Contains(c))
{
ShowCompletionWindow();
}
}
}

void TextEntering(object sender, TextCompositionEventArgs e)
{
if (e.Text.Length > 0 && completionWindow != null && completionWindow.CompletionList.IsEmpty())
{
completionWindow.Close();
}
}

private void ShowCompletionWindow()
{
if (completionWindow == null && DataContext is WorkspaceItemViewModel item)
{
completionWindow = new CompletionWindow(Editor.TextArea);
var editor = item.Editor;
if (editor.FillCompletionList(completionWindow.CompletionList, Editor.SelectionStart))
{
completionWindow.Show();
completionWindow.Closed += delegate
{
completionWindow = null;
};
}
else
{
completionWindow = null;
}
}
}

private void SetFirst(WorkspaceItemViewModel item)
{
foreach (var lineTransformer in item.Editor.LineTransformers)
{
EditBox.TextArea.TextView.LineTransformers.Add(lineTransformer);
Editor.TextArea.TextView.LineTransformers.Add(lineTransformer);
}
foreach (var backgroundRenderer in item.Editor.BackgroundRenderers)
{
EditBox.TextArea.TextView.BackgroundRenderers.Add(backgroundRenderer);
Editor.TextArea.TextView.BackgroundRenderers.Add(backgroundRenderer);
}

if (foldingManager == null && item.Editor.FoldingStrategy != null)
{
foldingManager = FoldingManager.Install(EditBox.TextArea);
foldingManager = FoldingManager.Install(Editor.TextArea);
}
}
}
Expand Down
62 changes: 0 additions & 62 deletions Docdown/Editor/MarkdownEditor.xaml

This file was deleted.

Loading

0 comments on commit 9ecfb08

Please sign in to comment.