diff options
| author | altaf-creator <64111012+altaf-creator@users.noreply.github.com> | 2023-05-10 22:02:28 +0700 |
|---|---|---|
| committer | altaf-creator <64111012+altaf-creator@users.noreply.github.com> | 2023-05-10 22:02:28 +0700 |
| commit | d9977bd7d661ba93978713b5584bb4d4aa1854e1 (patch) | |
| tree | 99f1b4d0013f48467aa4bc34925a4198599ed90b /MarkdownEditor/MainWindow.xaml.cs | |
| parent | 060455998a74b93eb8ac3b8627d54ca7b9e61146 (diff) | |
v0.2 Update: Added "New" button, added window toggles, added unsaved warning
Diffstat (limited to 'MarkdownEditor/MainWindow.xaml.cs')
| -rw-r--r-- | MarkdownEditor/MainWindow.xaml.cs | 285 |
1 files changed, 235 insertions, 50 deletions
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; + } } } } |
