Ejemplo n.º 1
0
        private void EditorSettingsService_OnDefaultLineEndingChanged(object sender, LineEnding lineEnding)
        {
            if (Sets.Items == null)
            {
                return;
            }
            foreach (SetsViewItem setItem in Sets.Items)
            {
                if (!(setItem.Content is TextEditor textEditor))
                {
                    continue;
                }
                if (textEditor.EditingFile != null)
                {
                    continue;
                }

                textEditor.LineEnding = lineEnding;
                if (textEditor == ((SetsViewItem)Sets.SelectedItem)?.Content && StatusBar != null)
                {
                    LineEndingIndicator.Text = LineEndingUtility.GetLineEndingDisplayText(textEditor.LineEnding);
                }
            }
        }
Ejemplo n.º 2
0
        private async Task<BackupMetadata> SaveLastSavedChangesAsync(TextEditor textEditor)
        {
            TextFile originalSnapshot = textEditor.OriginalSnapshot;
            StorageFile backupFile;

            try
            {
                backupFile = await SessionUtility.CreateNewBackupFileAsync(textEditor.Id.ToString("N") + "-LastSaved");
                await FileSystemUtility.WriteToFile(LineEndingUtility.ApplyLineEnding(originalSnapshot.Content, originalSnapshot.LineEnding), originalSnapshot.Encoding, backupFile);
            }
            catch (Exception ex)
            {
                LoggingService.LogError($"Failed to save backup file: {ex.Message}");
                return null;
            }

            return new BackupMetadata
            {
                BackupFilePath = backupFile.Path,
                Encoding = originalSnapshot.Encoding,
                LineEnding = originalSnapshot.LineEnding,
                DateModified = originalSnapshot.DateModifiedFileTime
            };
        }
Ejemplo n.º 3
0
        private async Task <BackupMetadata> SaveLastSavedChangesAsync(TextEditor textEditor)
        {
            TextFile    originalSnapshot = textEditor.OriginalSnapshot;
            StorageFile backupFile;

            try
            {
                backupFile = await SessionUtility.CreateNewBackupFileAsync(textEditor.Id.ToString("N") + "-LastSaved");

                await FileSystemUtility.WriteToFile(LineEndingUtility.ApplyLineEnding(originalSnapshot.Content, originalSnapshot.LineEnding), originalSnapshot.Encoding, backupFile);
            }
            catch
            {
                return(null);
            }

            return(new BackupMetadata
            {
                BackupFilePath = backupFile.Path,
                Encoding = originalSnapshot.Encoding,
                LineEnding = originalSnapshot.LineEnding,
                DateModified = originalSnapshot.DateModifiedFileTime
            });
        }
Ejemplo n.º 4
0
 private void UpdateLineEndingIndicator(LineEnding lineEnding)
 {
     if (StatusBar == null) return;
     LineEndingIndicator.Text = LineEndingUtility.GetLineEndingDisplayText(lineEnding);
 }
Ejemplo n.º 5
0
        private async Task <ITextEditor> RecoverTextEditorAsync(TextEditorSessionDataV1 editorSessionData)
        {
            StorageFile editingFile = null;

            if (editorSessionData.EditingFileFutureAccessToken != null)
            {
                editingFile = await FileSystemUtility.GetFileFromFutureAccessList(editorSessionData.EditingFileFutureAccessToken);
            }

            string lastSavedFile = editorSessionData.LastSavedBackupFilePath;
            string pendingFile   = editorSessionData.PendingBackupFilePath;

            ITextEditor textEditor;

            if (editingFile == null && lastSavedFile == null && pendingFile == null)
            {
                textEditor = null;
            }
            else if (editingFile != null && lastSavedFile == null && pendingFile == null) // File without pending changes
            {
                textEditor = await _notepadsCore.CreateTextEditor(editorSessionData.Id, editingFile, ignoreFileSizeLimit : true);

                textEditor.ResetEditorState(editorSessionData.StateMetaData);
            }
            else // File with pending changes
            {
                string lastSavedText = string.Empty;
                string pendingText   = null;

                if (lastSavedFile != null)
                {
                    TextFile lastSavedTextFile = await FileSystemUtility.ReadFile(lastSavedFile, ignoreFileSizeLimit : true,
                                                                                  EncodingUtility.GetEncodingByName(editorSessionData.StateMetaData.LastSavedEncoding));

                    lastSavedText = lastSavedTextFile.Content;
                }

                var textFile = new TextFile(lastSavedText,
                                            EncodingUtility.GetEncodingByName(editorSessionData.StateMetaData.LastSavedEncoding),
                                            LineEndingUtility.GetLineEndingByName(editorSessionData.StateMetaData.LastSavedLineEnding),
                                            editorSessionData.StateMetaData.DateModifiedFileTime);

                textEditor = _notepadsCore.CreateTextEditor(
                    editorSessionData.Id,
                    textFile,
                    editingFile,
                    editorSessionData.StateMetaData.IsModified);

                if (pendingFile != null)
                {
                    TextFile pendingTextFile = await FileSystemUtility.ReadFile(pendingFile,
                                                                                ignoreFileSizeLimit : true,
                                                                                EncodingUtility.GetEncodingByName(editorSessionData.StateMetaData.LastSavedEncoding));

                    pendingText = pendingTextFile.Content;
                }

                textEditor.ResetEditorState(editorSessionData.StateMetaData, pendingText);
            }

            return(textEditor);
        }
Ejemplo n.º 6
0
        private async void Sets_Drop(object sender, DragEventArgs args)
        {
            if (!(sender is SetsView))
            {
                return;
            }

            var sets = sender as SetsView;

            // Handle non Notepads drop event
            if (string.IsNullOrEmpty(args.DataView?.Properties?.ApplicationName) ||
                !string.Equals(args.DataView?.Properties?.ApplicationName, App.ApplicationName))
            {
                if (args.DataView == null || !args.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    return;
                }
                var fileDropDeferral = args.GetDeferral();
                var storageItems     = await args.DataView.GetStorageItemsAsync();

                StorageItemsDropped?.Invoke(this, storageItems);
                fileDropDeferral.Complete();
                return;
            }

            var deferral = args.GetDeferral();

            try
            {
                args.DataView.Properties.TryGetValue(NotepadsTextEditorMetaData, out object dataObj);

                if (!(dataObj is string data))
                {
                    throw new Exception("Failed to drop editor set: NotepadsTextEditorMetaData is invalid (Not String).");
                }

                TextEditorStateMetaData metaData = JsonConvert.DeserializeObject <TextEditorStateMetaData>(data);

                if (args.DataView.Properties.TryGetValue(NotepadsTextEditorEditingFilePath,
                                                         out object editingFilePathObj) && editingFilePathObj is string editingFilePath)
                {
                    var editor = GetTextEditor(editingFilePath);
                    if (editor != null)
                    {
                        SwitchTo(editor);
                        NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_NotificationMsg_FileAlreadyOpened"), 2500);
                        throw new Exception("Failed to drop editor set: File already opened.");
                    }
                }

                StorageFile editingFile = null;

                if (metaData.HasEditingFile && args.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    var storageItems = await args.DataView.GetStorageItemsAsync();

                    if (storageItems.Count == 1 && storageItems[0] is StorageFile file)
                    {
                        editingFile = file;
                    }
                    else
                    {
                        throw new Exception("Failed to read storage file from dropped set: Expecting only one storage file.");
                    }
                }

                string lastSavedText = null;
                string pendingText   = null;

                if (args.DataView.Properties.TryGetValue(NotepadsTextEditorLastSavedContent, out object lastSavedContentObj) &&
                    lastSavedContentObj is string lastSavedContent)
                {
                    lastSavedText = lastSavedContent;
                }
                else
                {
                    throw new Exception($"Failed to get last saved content from DataView: NotepadsTextEditorLastSavedContent property Is null");
                }

                if (args.DataView.Properties.TryGetValue(NotepadsTextEditorPendingContent, out object pendingContentObj) &&
                    pendingContentObj is string pendingContent)
                {
                    pendingText = pendingContent;
                }

                ApplicationSettingsStore.Write(SetDragAndDropActionStatus, "Handled");

                var index = -1;

                // Determine which items in the list our pointer is in between.
                for (int i = 0; i < sets.Items?.Count; i++)
                {
                    var item = sets.ContainerFromIndex(i) as SetsViewItem;

                    if (args.GetPosition(item).X - item?.ActualWidth < 0)
                    {
                        index = i;
                        break;
                    }
                }

                var atIndex = index == -1 ? sets.Items.Count : index;

                var textFile = new TextFile(lastSavedText,
                                            EncodingUtility.GetEncodingByName(metaData.LastSavedEncoding),
                                            LineEndingUtility.GetLineEndingByName(metaData.LastSavedLineEnding),
                                            metaData.DateModifiedFileTime);

                var newEditor = CreateTextEditor(Guid.NewGuid(), textFile, editingFile, metaData.FileNamePlaceholder, metaData.IsModified);
                OpenTextEditor(newEditor, atIndex);
                newEditor.ResetEditorState(metaData, pendingText);

                if (metaData.IsContentPreviewPanelOpened)
                {
                    newEditor.ShowHideContentPreview();
                }

                if (metaData.IsInDiffPreviewMode)
                {
                    newEditor.OpenSideBySideDiffViewer();
                }

                deferral.Complete();
                Analytics.TrackEvent("OnSetDropped");
            }
            catch (Exception ex)
            {
                LoggingService.LogException(ex);
                deferral.Complete();
            }
        }
Ejemplo n.º 7
0
        private async void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (!(sender is TextEditor textEditor))
            {
                textEditor = (Sets.SelectedItem as SetsViewItem)?.Content as TextEditor;
                if (textEditor == null)
                {
                    return;
                }
            }

            StorageFile file;

            if (textEditor.EditingFile == null || e is SaveAsEventArgs ||
                FileSystemUtility.IsFileReadOnly(textEditor.EditingFile) ||
                !await FileSystemUtility.FileIsWritable(textEditor.EditingFile))
            {
                file = await FilePickerFactory.GetFileSavePicker(e, textEditor, DefaultFileName).PickSaveFileAsync();

                textEditor.Focus(FocusState.Programmatic);
            }
            else
            {
                file = textEditor.EditingFile;
            }

            if (file == null)
            {
                return;
            }

            var success = await textEditor.SaveFile(file);

            if (success)
            {
                if (Sets.Items != null)
                {
                    foreach (SetsViewItem setsItem in Sets.Items)
                    {
                        if (setsItem.Content != textEditor)
                        {
                            continue;
                        }

                        setsItem.Header          = file.Name;
                        setsItem.Icon.Visibility = Visibility.Collapsed;

                        PathIndicator.Text       = textEditor.EditingFile.Path;
                        LineEndingIndicator.Text =
                            LineEndingUtility.GetLineEndingDisplayText(textEditor.LineEnding);
                        EncodingIndicator.Text = textEditor.Encoding.EncodingName;
                        break;
                    }
                }
                ShowNotificationMessage("Saved", 2000);
            }
            else
            {
                await ContentDialogFactory.GetFileSaveErrorDialog(file).ShowAsync();
            }
        }
Ejemplo n.º 8
0
 private string FixLineEnding(string text, LineEnding lineEnding)
 {
     return(LineEndingUtility.ApplyLineEnding(text, lineEnding));
 }