private void OnInsertFile(string documentRelativeDestinationPath) { TryIt(droppedFilePath => { string title; var image = new byte[0]; if (_vm.UseClipboardImage) { var name = PromptDialog.Prompt((string)TranslationProvider.Translate("aslocalfile-save-file-as")); if (string.IsNullOrWhiteSpace(name)) { return; } image = Images.ClipboardDibToBitmapSource().ToPngArray(); title = name + ".png"; } else { title = Path.GetFileName(droppedFilePath); } var link = Path.Combine(documentRelativeDestinationPath, title); var destination = Path.Combine(Path.GetDirectoryName(_vm.DocumentFileName), link); if (link.Contains(" ")) { link = $"<{link}>"; } if (File.Exists(destination)) { var message = (string)TranslationProvider.Translate("image-drop-overwrite-file"); if (Notify.ConfirmYesNo(message) != MessageBoxResult.Yes) { return; } } if (_vm.UseClipboardImage) { File.WriteAllBytes(destination, image); } else { File.Copy(droppedFilePath, destination, true); } InsertImageTag(TextEditor, DragEventArgs, link, title); }); }
public void OnSaveAs(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(_vm.DocumentFileName)) { return; } Close(); // Yep, you're reading this right. SaveFileDialog.ShowDialog exits immediately // unless I first close this window. Something to do with nested ShowDialog() // calls I suspect. Application.Current.Dispatcher.InvokeAsync(() => Guard(droppedFilePath => { var dialog = new SaveFileDialog { OverwritePrompt = true, RestoreDirectory = true, Filter = "All files (*.*)|*.*" }; if (dialog.ShowDialog() == false) { return(string.Empty); } if (string.IsNullOrEmpty(dialog.FileName)) { return(string.Empty); } var fileName = dialog.FileName; string title; var image = new byte[0]; if (_vm.UseClipboardImage) { image = Images.ClipboardDibToBitmapSource().ToPngArray(); title = fileName + ".png"; } else { title = Path.GetFileName(droppedFilePath); } if (File.Exists(fileName)) { var message = (string)TranslationProvider.Translate("image-drop-overwrite-file"); if (Notify.ConfirmYesNo(message) != MessageBoxResult.Yes) { return(string.Empty); } } if (_vm.UseClipboardImage) { File.WriteAllBytes(fileName, image); } else { File.Copy(droppedFilePath, fileName, true); } var link = FileExtensions.MakeRelativePath(_vm.DocumentFileName, fileName); return(CreateImageTag(link, title)); })); }