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

Commit

Permalink
Added new file window
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Oct 17, 2019
1 parent 334c520 commit be78ab2
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Docdown/Docdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
<Compile Include="Model\IWorkspaceItem.cs" />
<Compile Include="Model\IWorkspaceItemHandler.cs" />
<Compile Include="Model\Language.cs" />
<Compile Include="Model\NewFile.cs" />
<Compile Include="Model\Outline.cs" />
<Compile Include="Model\OutlineItem.cs" />
<Compile Include="Model\Template.cs" />
Expand Down Expand Up @@ -263,6 +264,7 @@
<Compile Include="ViewModel\MessageBoxViewModel.cs" />
<Compile Include="ViewModel\MessageQueue.cs" />
<Compile Include="ViewModel\Message.cs" />
<Compile Include="ViewModel\NewFileViewModel.cs" />
<Compile Include="ViewModel\ObservableList.cs" />
<Compile Include="ViewModel\ObservableObject.cs" />
<Compile Include="ViewModel\OutlineItemViewModel.cs" />
Expand All @@ -284,6 +286,9 @@
<Compile Include="Windows\MessageWindow.xaml.cs">
<DependentUpon>MessageWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Windows\NewFileWindow.xaml.cs">
<DependentUpon>NewFileWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Windows\SettingsWindow.xaml.cs">
<DependentUpon>SettingsWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -400,6 +405,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Windows\NewFileWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Windows\SettingsWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -446,6 +455,8 @@
<EmbeddedResource Include="Resources\Validation\Discouraged\German.properties" />
<EmbeddedResource Include="Resources\Validation\Discouraged\English.properties" />
<EmbeddedResource Include="Resources\Completion\Html\German.properties" />
<EmbeddedResource Include="Resources\Templates\Default.bib" />
<EmbeddedResource Include="Resources\Templates\Markdown.md" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
9 changes: 9 additions & 0 deletions Docdown/Model/NewFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Docdown.Model
{
public class NewFile
{
public string Extension { get; set; }
public byte[] Content { get; set; }
public string Name { get; set; }
}
}
8 changes: 8 additions & 0 deletions Docdown/Resources/Templates/Default.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@book{Sample.2019,
year = {2019},
title = {A sample book name},
address = {Cham},
publisher = {{Springer International Publishing}},
isbn = {178-2-389-02895-1},
doi = {10.1007/178-2-389-02895-1}
}
24 changes: 24 additions & 0 deletions Docdown/Resources/Templates/Markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
lang: de-DE
disable-arabic-start: true
...

\section*{List of Abbreviations}
\addcontentsline{toc}{section}{List of Abbreviations}
\markright{List of Abbreviations}

\begin{acronym}[List of Abbreviations]
\acro{SE}{Software Engineering}
\acro{JSON}{Java Script Object Notation}
\acro{HTML}{Hypertext Markup Language}
\end{acronym}

\pagenumbering{arabic}

\pagebreak
# Introduction

\pagebreak
\pagenumbering{roman}
\setcounter{page}{3}
# References
23 changes: 22 additions & 1 deletion Docdown/ViewModel/Commands/CreateNewFileCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Docdown.Model;
using Docdown.Windows;
using System;
using System.Windows;

namespace Docdown.ViewModel.Commands
{
Expand All @@ -10,6 +12,7 @@ public CreateNewFileCommand(WorkspaceItemViewModel workspaceItem)
{
}

[STAThread]
[Delegate]
private async static void OpenCreateNewFileWindow(WorkspaceItemViewModel workspaceItem, bool isDirectory = false)
{
Expand All @@ -26,14 +29,32 @@ private async static void OpenCreateNewFileWindow(WorkspaceItemViewModel workspa
}
else
{
newItem = await workspaceItem.Data.CreateNewFile(Language.Current.Get("File.New.File"), ".md");
var window = new NewFileWindow();
var viewModel = new NewFileViewModel();
await viewModel.LoadAsync();
window.DataContext = viewModel;
window.Owner = Application.Current.MainWindow;

if (window.ShowDialog().Value)
{
var sel = viewModel.Selected;
newItem = await workspaceItem.Data.CreateNewFile(viewModel.Name, "." + sel.Extension, sel.Content);
}
else
{
return;
}
}
var vm = new WorkspaceItemViewModel(workspace, workspaceItem, newItem);
if (isDirectory)
{
vm.IsNameChanging = true;
}
workspaceItem.AddChild(vm);
if (!isDirectory)
{
workspaceItem.Workspace.SelectedItem = vm;
}
}
}
}
73 changes: 73 additions & 0 deletions Docdown/ViewModel/NewFileViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Docdown.Model;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Docdown.ViewModel
{
public class NewFileViewModel : ObservableList<NewFile>
{
public NewFile Selected
{
get => selected;
set => Set(ref selected, value);
}

public string Name
{
get => name;
set => Set(ref name, value);
}

private NewFile selected;
private string name;


public NewFileViewModel()
{

}

[ChangeListener(nameof(Selected))]
private void SelectedChanged()
{
if (selected != null)
{
Name = $"{selected.Name.ToLower()}.{selected.Extension}";
}
}

public async Task LoadAsync()
{
var asm = typeof(NewFileViewModel).Assembly;
var names = asm.GetManifestResourceNames().Where(e => e.StartsWith("Docdown.Resources.Templates."));

foreach (var name in names)
{
var extension = Path.GetExtension(name).Substring(1);
var pre = Path.GetFileNameWithoutExtension(name);
var fileName = Path.GetExtension(pre).Substring(1);

var file = new NewFile
{
Name = fileName,
Extension = extension
};

using (var ms = new MemoryStream())
{
using (var rs = asm.GetManifestResourceStream(name))
{
await rs.CopyToAsync(ms);
}
file.Content = ms.ToArray();
}
Add(file);
}
if (Count > 0)
{
Selected = this[0];
}
}
}
}
47 changes: 47 additions & 0 deletions Docdown/Windows/NewFileWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<Metro:MetroWindow x:Class="Docdown.Windows.NewFileWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Docdown.Windows"
xmlns:Metro="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
Style="{StaticResource SlimWindowStyle}"
TitleTemplate="{StaticResource DefaultTitleTemplate}"
IconTemplate="{x:Null}"
Title="Create New File" Height="450" Width="400" Background="{DynamicResource WhiteBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView Grid.Row="0" ItemsSource="{Binding}" SelectedItem="{Binding Selected}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="4 8">
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Extension}" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Name: " VerticalAlignment="Center" Margin="4" Foreground="{DynamicResource BlackBrush}"/>
<TextBox Grid.Column="1" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Margin="4"/>
</Grid>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="OK" Style="{StaticResource MetroFlatButton}" Margin="4" Click="OKClicked"/>
<Button Content="Close" Style="{StaticResource MetroFlatButton}" Margin="4" Click="CloseClicked"/>
</StackPanel>
</Grid>
</Metro:MetroWindow>
22 changes: 22 additions & 0 deletions Docdown/Windows/NewFileWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Windows;

namespace Docdown.Windows
{
public partial class NewFileWindow
{
public NewFileWindow()
{
InitializeComponent();
}

private void OKClicked(object sender, RoutedEventArgs e)
{
DialogResult = true;
}

private void CloseClicked(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
}
}

0 comments on commit be78ab2

Please sign in to comment.