aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MarkdownEditor/AboutWindow.xaml6
-rw-r--r--MarkdownEditor/MainWindow.xaml21
-rw-r--r--MarkdownEditor/MainWindow.xaml.cs285
3 files changed, 253 insertions, 59 deletions
diff --git a/MarkdownEditor/AboutWindow.xaml b/MarkdownEditor/AboutWindow.xaml
index f7aff6c..2037799 100644
--- a/MarkdownEditor/AboutWindow.xaml
+++ b/MarkdownEditor/AboutWindow.xaml
@@ -20,7 +20,7 @@
<ItemsControl Grid.Column="0">
<Image Source="icon_markdowneditor.png" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="30"/>
<TextBlock HorizontalAlignment="Center" FontWeight="Bold" FontSize="22">MarkdownEditor</TextBlock>
- <TextBlock HorizontalAlignment="Center" FontSize="18" Foreground="LightGray">v0.1</TextBlock>
+ <TextBlock HorizontalAlignment="Center" FontSize="18" Foreground="LightGray" Text="v0.2"/>
<TextBlock HorizontalAlignment="Center" FontSize="16" Foreground="LightGray">
<Hyperlink NavigateUri="https://github.com/altaf-creator/MarkdownEditor" Foreground="LightBlue" RequestNavigate="Hyperlink_RequestNavigate">
github.com/altaf-creator/<LineBreak />MarkdownEditor
@@ -64,8 +64,8 @@
</ItemsControl>
<TextBlock FontSize="16" Foreground="LightGray" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="20" Grid.Column="1">
- <Hyperlink NavigateUri="https://mit-license.org/" Foreground="LightBlue" RequestNavigate="Hyperlink_RequestNavigate">
- View License
+ <Hyperlink NavigateUri="https://altaf.mit-license.org/" Foreground="LightBlue" RequestNavigate="Hyperlink_RequestNavigate">
+ Licensed under the MIT License
</Hyperlink>
</TextBlock>
</Grid>
diff --git a/MarkdownEditor/MainWindow.xaml b/MarkdownEditor/MainWindow.xaml
index f15d489..8c14a6a 100644
--- a/MarkdownEditor/MainWindow.xaml
+++ b/MarkdownEditor/MainWindow.xaml
@@ -20,19 +20,28 @@
<Button Name="saveBtn" Margin="10,10,0,0" Width="28" Click="saveBtn_Click" Padding="0" ToolTip="Save">💾</Button>
<Button Name="saveAsBtn" Margin="43,10,0,0" Width="28" Padding="0" ToolTip="Save As" Click="saveAsBtn_Click">
<Button.Foreground>
- <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
+ <SolidColorBrush Color="LightSkyBlue"/>
</Button.Foreground> 💾
</Button>
+ <Button Name="newBtn" Margin="109,10,0,0" Width="28" Click="newBtn_Click" FontSize="15" Padding="0" ToolTip="New Markdown File">📄</Button>
<Button Name="exportBtn" Margin="76,10,0,0" Width="28" Click="exportBtn_Click" FontSize="15" Padding="0" ToolTip="Export to HTML">📤</Button>
- <Button Name="openBtn" Margin="109,10,0,0" Width="28" Click="openBtn_Click" Padding="0" ToolTip="Open File">📂</Button>
- <Button Name="openFolderBtn" Margin="142,10,0,0" Width="28" Padding="0" ToolTip="Open Folder" Click="openFolderBtn_Click">🗃️</Button>
+ <Button Name="openBtn" Margin="142,10,0,0" Width="28" Click="openBtn_Click" Padding="0" ToolTip="Open File">📂</Button>
+ <Button Name="openFolderBtn" Margin="175,10,0,0" Width="28" Padding="0" ToolTip="Open Folder" Click="openFolderBtn_Click">🗃️</Button>
+
+ <Button Name="previewWindowBtn" Margin="0,10,53,0" Width="28" Padding="0" ToolTip="Toggle Preview" HorizontalAlignment="Right" Click="previewWindowBtn_Click" Foreground="LightSkyBlue">🌐
+ </Button>
+ <Button Name="mainWindowBtn" Margin="0,10,86,0" Width="28" Padding="0" ToolTip="Toggle Markdown Source" HorizontalAlignment="Right" Click="mainWindowBtn_Click" Foreground="LightSkyBlue">.md
+ </Button>
+ <Button Name="explorerWindowBtn" Margin="0,10,119,0" Width="28" Padding="0" ToolTip="Toggle 'Open Folder'" HorizontalAlignment="Right" Click="explorerWindowBtn_Click" Foreground="LightSkyBlue">📂
+ </Button>
+
<Button Name="aboutBtn" Margin="0,10,10,0" Width="28" Padding="0" ToolTip="About" HorizontalAlignment="Right" FontSize="18" FontWeight="Bold" Click="aboutBtn_Click">i</Button>
<Grid Grid.Row="1" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
- <ColumnDefinition Width="300"/>
- <ColumnDefinition />
- <ColumnDefinition />
+ <ColumnDefinition Width="300" Name="fileColumn"/>
+ <ColumnDefinition Name="mainColumn" />
+ <ColumnDefinition Name="previewColumn" />
</Grid.ColumnDefinitions>
<ScrollViewer Margin="10, 32, 5, 47" VerticalScrollBarVisibility="Auto">
diff --git a/MarkdownEditor/MainWindow.xaml.cs b/MarkdownEditor/MainWindow.xaml.cs
index 68c90f2..d0e81de 100644
--- a/MarkdownEditor/MainWindow.xaml.cs
+++ b/MarkdownEditor/MainWindow.xaml.cs
@@ -37,6 +37,11 @@ namespace MarkdownEditor
public string tempPath = Path.GetTempPath() + "markdowneditor_temp.html";
private string prevTxt;
public bool isOpened = false;
+ public bool isModified = true;
+
+ private bool isExplorerOpen = true;
+ private bool isSourceOpen = true;
+ private bool isPreviewOpen = true;
public MainWindow()
{
@@ -143,7 +148,7 @@ namespace MarkdownEditor
string htmlPath = "file:///" + tempPath;
- if (browser.Address != htmlPath.Replace('\\','/'))
+ if (browser.Address != htmlPath.Replace('\\', '/'))
{
browser.Load(tempPath);
}
@@ -151,19 +156,41 @@ namespace MarkdownEditor
{
browser.Reload();
}
+
prevTxt = txtRaw.Text;
}
+ if (isOpened)
+ {
+ isModified = txtRaw.Text != File.ReadAllText(path);
+ Title = Path.GetFileName(path) + " - MarkdownEditor";
+
+ if (isModified)
+ {
+ Title = Path.GetFileName(path) + "*" + " - MarkdownEditor";
+ }
+ else
+ {
+ Title = "MarkdownEditor - a basic markdown viewer and editor";
+ }
+ }
+ else
+ {
+ isModified = false;
+ }
+
string formattedPath = path;
-
+
pathTxt.Text = formattedPath.Substring(rootPath.Length);
browser.LoadError += browser_LoadError;
}
+ // ============================================
+
private void saveBtn_Click(object sender, RoutedEventArgs e)
{
string text = txtRaw.Text;
-
+
if (!isOpened)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
@@ -174,6 +201,9 @@ namespace MarkdownEditor
path = saveFileDialog.FileName;
File.WriteAllText(path, text);
isOpened = true;
+ isModified = false;
+
+ addListButton(path);
}
}
else
@@ -182,24 +212,59 @@ namespace MarkdownEditor
}
}
+ private void saveAsBtn_Click(object sender, RoutedEventArgs e)
+ {
+ string text = txtRaw.Text;
+ SaveFileDialog saveFileDialog = new SaveFileDialog();
+ saveFileDialog.Filter = "Markdown (*.md)|*.md|Text (*.txt)|*.txt|All (*.*)|*.*";
+
+ if (saveFileDialog.ShowDialog() == true)
+ {
+ path = saveFileDialog.FileName;
+ File.WriteAllText(path, text);
+
+ addListButton(path);
+ }
+
+ }
+
private void openBtn_Click(object sender, RoutedEventArgs e)
{
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Markdown Files (*.md)|*.md|Text Files (*.txt)|*.txt|All (*.*)|*.*";
+ if (isModified)
+ {
+ var result = MessageBox.Show("There are unsaved changes. Are you sure you want to open another file?", "Unsaved Changes", MessageBoxButton.YesNo);
+ if (result == MessageBoxResult.Yes)
+ {
+ OpenFileDialog openFileDialog = new OpenFileDialog();
+ openFileDialog.Filter = "Markdown Files (*.md)|*.md|Text Files (*.txt)|*.txt|All (*.*)|*.*";
- if (openFileDialog.ShowDialog() == true)
+ if (openFileDialog.ShowDialog() == true)
+ {
+ path = openFileDialog.FileName;
+ rootPath = "";
+ txtRaw.Text = File.ReadAllText(path);
+
+ addListButton(path);
+
+ isOpened = true;
+ }
+ }
+ }
+ else
{
- path = openFileDialog.FileName;
- rootPath = "";
- txtRaw.Text = File.ReadAllText(path);
+ OpenFileDialog openFileDialog = new OpenFileDialog();
+ openFileDialog.Filter = "Markdown Files (*.md)|*.md|Text Files (*.txt)|*.txt|All (*.*)|*.*";
- var fileButton = new Button();
- fileButton.Content = Path.GetFileName(path);
- fileButton.ToolTip = path;
- fileButton.Click += fileBtn_Click;
- filePanel.Items.Insert(0, fileButton);
+ if (openFileDialog.ShowDialog() == true)
+ {
+ path = openFileDialog.FileName;
+ rootPath = "";
+ txtRaw.Text = File.ReadAllText(path);
- isOpened = true;
+ addListButton(path);
+
+ isOpened = true;
+ }
}
}
@@ -211,20 +276,34 @@ namespace MarkdownEditor
if (saveFileDialog.ShowDialog() == true)
{
- path = saveFileDialog.FileName;
- File.WriteAllText(path, text);
+ File.WriteAllText(saveFileDialog.FileName, text);
}
}
private void fileBtn_Click(object sender, RoutedEventArgs e)
{
+ if (isModified)
+ {
+ var result = MessageBox.Show("There are unsaved changes. Are you sure you want to open another file?", "Unsaved Changes", MessageBoxButton.YesNo);
+ if (result == MessageBoxResult.Yes)
+ {
+ var element = (FrameworkElement)sender;
+ string tooltip = element.ToolTip.ToString();
- var element = (FrameworkElement)sender;
- string tooltip = element.ToolTip.ToString();
+ txtRaw.Text = File.ReadAllText(tooltip);
+ path = tooltip;
+ isOpened = true;
+ }
+ }
+ else
+ {
+ var element = (FrameworkElement)sender;
+ string tooltip = element.ToolTip.ToString();
- txtRaw.Text = File.ReadAllText(tooltip);
- path = tooltip;
- isOpened = true;
+ txtRaw.Text = File.ReadAllText(tooltip);
+ path = tooltip;
+ isOpened = true;
+ }
}
private void clearBtn_Click(object sender, RoutedEventArgs e)
@@ -234,51 +313,104 @@ namespace MarkdownEditor
private void openFolderBtn_Click(object sender, RoutedEventArgs e)
{
- var dialog = new VistaFolderBrowserDialog();
- dialog.Description = "Select Folder";
- dialog.UseDescriptionForTitle = true;
-
- if (dialog.ShowDialog() == true)
+ if (isModified)
{
- path = dialog.SelectedPath;
- rootPath = dialog.SelectedPath;
+ var result = MessageBox.Show("There are unsaved changes. Are you sure you want to open another file?", "Unsaved Changes", MessageBoxButton.YesNo);
+ if (result == MessageBoxResult.Yes)
+ {
+ var dialog = new VistaFolderBrowserDialog();
+ dialog.Description = "Select Folder";
+ dialog.UseDescriptionForTitle = true;
+
+ if (dialog.ShowDialog() == true)
+ {
+ path = dialog.SelectedPath;
+ rootPath = dialog.SelectedPath;
+
+ string[] files = Directory.GetFiles(path, "*.md", SearchOption.AllDirectories);
+
+ filePanel.Items.Clear();
- string[] files = Directory.GetFiles(path, "*.md", SearchOption.AllDirectories);
+ for (int i = files.Length - 1; i >= 0; i--)
+ {
+ string str = files[i];
- filePanel.Items.Clear();
+ str = str.Substring(path.Length);
- for (int i = files.Length - 1; i >= 0; i--)
+ addListButton(files[i]);
+ }
+ }
+ }
+ }
+ else
+ {
+ var dialog = new VistaFolderBrowserDialog();
+ dialog.Description = "Select Folder";
+ dialog.UseDescriptionForTitle = true;
+
+ if (dialog.ShowDialog() == true)
{
- var fileButton = new Button();
- string str = files[i];
+ path = dialog.SelectedPath;
+ rootPath = dialog.SelectedPath;
+
+ string[] files = Directory.GetFiles(path, "*.md", SearchOption.AllDirectories);
+
+ filePanel.Items.Clear();
+
+ for (int i = files.Length - 1; i >= 0; i--)
+ {
+ string str = files[i];
- str = str.Substring(path.Length);
+ str = str.Substring(path.Length);
- fileButton.Content = str;
- fileButton.ToolTip = files[i];
- fileButton.Click += fileBtn_Click;
- filePanel.Items.Insert(0, fileButton);
+ addListButton(files[i]);
+ }
}
}
}
- private void saveAsBtn_Click(object sender, RoutedEventArgs e)
+ private void newBtn_Click(object sender, RoutedEventArgs e)
{
- string text = txtRaw.Text;
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "Markdown (*.md)|*.md|Text (*.txt)|*.txt|All (*.*)|*.*";
-
- if (saveFileDialog.ShowDialog() == true)
+ if (isModified)
{
- path = saveFileDialog.FileName;
- File.WriteAllText(path, text);
+ var result = MessageBox.Show("There are unsaved changes. Are you sure you want to make a new file?", "Unsaved Changes", MessageBoxButton.YesNo);
+ if (result == MessageBoxResult.Yes)
+ {
+ path = "";
+ rootPath = "";
+ txtRaw.Text = "";
+ isOpened = false;
+ }
+ }
+ else
+ {
+ path = "";
+ rootPath = "";
+ txtRaw.Text = "";
+ isOpened = false;
}
+ }
+ private void aboutBtn_Click(object sender, RoutedEventArgs e)
+ {
+ Window window = new AboutWindow();
+ window.Show();
}
+ // ============================================
+
private void Window_Closing(object sender, CancelEventArgs e)
{
File.Delete(tempPath);
+
+ if (isModified)
+ {
+ var result = MessageBox.Show("There are unsaved changes. Are you sure you want to quit?", "Unsaved Changes", MessageBoxButton.YesNo);
+ if (result != MessageBoxResult.Yes)
+ {
+ e.Cancel = true;
+ }
+ }
}
public void browser_LoadError(object sender, EventArgs e)
@@ -286,10 +418,63 @@ namespace MarkdownEditor
browser.Load(tempPath);
}
- private void aboutBtn_Click(object sender, RoutedEventArgs e)
+ void addListButton(string filePath)
{
- Window window = new AboutWindow();
- window.Show();
+ var fileButton = new Button();
+ fileButton.Content = Path.GetFileName(filePath);
+ fileButton.ToolTip = filePath;
+ fileButton.Click += fileBtn_Click;
+ filePanel.Items.Insert(0, fileButton);
+ }
+
+ // ============================================
+
+ private void explorerWindowBtn_Click(object sender, RoutedEventArgs e)
+ {
+ if (isExplorerOpen)
+ {
+ fileColumn.Width = new GridLength(0);
+ explorerWindowBtn.Foreground = System.Windows.Media.Brushes.Gray;
+ isExplorerOpen = false;
+ }
+ else
+ {
+ fileColumn.Width = new GridLength(300);
+ explorerWindowBtn.Foreground = System.Windows.Media.Brushes.LightSkyBlue;
+ isExplorerOpen = true;
+ }
+ }
+
+ private void mainWindowBtn_Click(object sender, RoutedEventArgs e)
+ {
+ if (isSourceOpen)
+ {
+ mainColumn.Width = new GridLength(0);
+ mainWindowBtn.Foreground = System.Windows.Media.Brushes.Gray;
+ isSourceOpen = false;
+ }
+ else
+ {
+ mainColumn.Width = new GridLength(1, GridUnitType.Star);
+ mainWindowBtn.Foreground = System.Windows.Media.Brushes.LightSkyBlue;
+ isSourceOpen = true;
+ }
+ }
+
+ private void previewWindowBtn_Click(object sender, RoutedEventArgs e)
+ {
+ if (isPreviewOpen)
+ {
+ previewColumn.Width = new GridLength(0);
+ previewWindowBtn.Foreground = System.Windows.Media.Brushes.Gray;
+ isPreviewOpen = false;
+ }
+ else
+ {
+ previewColumn.Width = new GridLength(1, GridUnitType.Star);
+ previewWindowBtn.Foreground = System.Windows.Media.Brushes.LightSkyBlue;
+ isPreviewOpen = true;
+ }
}
}
}