public static bool SaveFileAs(Editor editor, string defaultFilter = "markdown") { const int markdown = 1; const int html = 2; var filterIndex = markdown; if (defaultFilter == "html" || defaultFilter == "html-with-template") { filterIndex = html; } var dialog = new SaveFileDialog { FilterIndex = filterIndex, OverwritePrompt = true, RestoreDirectory = true, FileName = Markdown.SuggestFilenameFromTitle(editor.EditBox.Text), Filter = "Markdown files (*.md)|*.md|" + "HTML files (*.html)|*.html|" + "All files (*.*)|*.*" }; if (dialog.ShowDialog() == false) { return(false); } var filename = dialog.FileNames[0]; if (dialog.FilterIndex == html) { return(SaveAsHtml(editor.Text, filename, "html-with-template")); } var currentFileName = editor.FileName; editor.FileName = filename; var offset = editor.EditBox.SelectionStart; if (!Save(editor) || !LoadFile(editor, filename, false)) { editor.FileName = currentFileName; return(false); } var max = editor.EditBox.Text.Length; editor.EditBox.SelectionStart = Math.Min(max, offset); return(true); }
public static bool SaveFileAs(Editor editor, string defaultFilter = "markdown") { const int markdown = 1; const int html = 2; const int pdf = 3; const int docx = 4; var filterIndex = markdown; if (defaultFilter == "html" || defaultFilter == "html-with-template") filterIndex = html; if (defaultFilter == "pdf") filterIndex = pdf; if (defaultFilter == "docx") filterIndex = docx; var dialog = new SaveFileDialog { FilterIndex = filterIndex, OverwritePrompt = true, RestoreDirectory = true, FileName = Markdown.SuggestFilenameFromTitle(editor.EditBox.Text), Filter = "Markdown files (*.markdown)|*.markdown|" + "Markdown files (*.md)|*.md|" + "HTML files (*.html)|*.html|" + "PDF files (*.pdf)|*.pdf|" + "Docx files (*.docx)|*.docx|" + "All files (*.*)|*.*" }; if (dialog.ShowDialog() == false) return false; var filename = dialog.FileNames[0]; if (dialog.FilterIndex == html) return SaveAsHtml(editor.Text, filename, "html-with-template"); if (dialog.FilterIndex == pdf) return SaveAsPdf(editor.Text, filename); if (dialog.FilterIndex == docx) return SaveAsDocx(editor.Text, filename); var currentFileName = editor.FileName; editor.FileName = filename; var offset = editor.EditBox.SelectionStart; if (!Save(editor) || !LoadFile(editor, filename, false)) { editor.FileName = currentFileName; return false; } var max = editor.EditBox.Text.Length; editor.EditBox.SelectionStart = Math.Min(max, offset); return true; }