using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; using System.Diagnostics; using Microsoft.Win32; using System.IO; using CefSharp; using Markdig; using Path = System.IO.Path; using Ookii.Dialogs.Wpf; using System.ComponentModel; using System.Drawing; namespace MarkdownEditor { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public string path; public string rootPath; public string tempPath = Path.GetTempPath() + "markdowneditor_temp.html"; private string prevTxt; public bool isOpened = false; public MainWindow() { InitializeComponent(); prevTxt = txtRaw.Text; rootPath = ""; path = ""; System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = TimeSpan.FromSeconds(.5f); dispatcherTimer.Start(); File.WriteAllText(tempPath, ""); browser.Load(tempPath); } private void dispatcherTimer_Tick(object sender, EventArgs e) { if (prevTxt != txtRaw.Text) { var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); string font = "\r\n\r\n"; // CSS styling. Please forgive me. string style = @""; var result = Markdown.ToHtml(txtRaw.Text, pipeline); File.WriteAllText(tempPath, font + style + result); string htmlPath = "file:///" + tempPath; if (browser.Address != htmlPath.Replace('\\','/')) { browser.Load(tempPath); } else { browser.Reload(); } prevTxt = txtRaw.Text; } 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(); saveFileDialog.Filter = "Markdown (*.md)|*.md|Text (*.txt)|*.txt|All (*.*)|*.*"; if (saveFileDialog.ShowDialog() == true) { path = saveFileDialog.FileName; File.WriteAllText(path, text); isOpened = true; } } else { File.WriteAllText(path, text); } } private void openBtn_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Markdown Files (*.md)|*.md|Text Files (*.txt)|*.txt|All (*.*)|*.*"; if (openFileDialog.ShowDialog() == true) { path = openFileDialog.FileName; rootPath = ""; txtRaw.Text = File.ReadAllText(path); var fileButton = new Button(); fileButton.Content = Path.GetFileName(path); fileButton.ToolTip = path; fileButton.Click += fileBtn_Click; filePanel.Items.Insert(0, fileButton); isOpened = true; } } private void exportBtn_Click(object sender, RoutedEventArgs e) { string text = Markdig.Markdown.ToHtml(txtRaw.Text); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "HTML (*.html)|*.html|Text (*.txt)|*.txt|All (*.*)|*.*"; if (saveFileDialog.ShowDialog() == true) { path = saveFileDialog.FileName; File.WriteAllText(path, text); } } private void fileBtn_Click(object sender, RoutedEventArgs e) { var element = (FrameworkElement)sender; string tooltip = element.ToolTip.ToString(); txtRaw.Text = File.ReadAllText(tooltip); path = tooltip; isOpened = true; } private void clearBtn_Click(object sender, RoutedEventArgs e) { filePanel.Items.Clear(); } private void openFolderBtn_Click(object sender, RoutedEventArgs e) { 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(); for (int i = files.Length - 1; i >= 0; i--) { var fileButton = new Button(); string str = files[i]; str = str.Substring(path.Length); fileButton.Content = str; fileButton.ToolTip = files[i]; fileButton.Click += fileBtn_Click; filePanel.Items.Insert(0, fileButton); } } } 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); } } private void Window_Closing(object sender, CancelEventArgs e) { File.Delete(tempPath); } public void browser_LoadError(object sender, EventArgs e) { browser.Load(tempPath); } private void aboutBtn_Click(object sender, RoutedEventArgs e) { Window window = new AboutWindow(); window.Show(); } } }