private KeyboardShortcutCollection ConfigureCommands() { var configuration = File.ReadAllLines(@"keyboard.trcfg"); var descriptors = new BlockDescriptor[] { ParagraphBlock.Descriptor, HeadingBlock.Descriptor, }; var addedCommands = new IContextualCommand[] { new BreakTextBlockCommand(), new DeleteNextCharacterCommand(), new DeletePreviousCharacterCommand(), new MergeTextBlocksCommand(), new MoveCaretBackwardCommand(), new MoveCaretForwardCommand(), new MoveCaretUpCommand(), new MoveCaretDownCommand(), new MoveCaretHomeCommand(), new MoveCaretEndCommand(), new MoveCaretPreviousWordCommand(), new MoveCaretNextWordCommand(), new UndoCommand(), new RedoCommand(), }; var allCommands = addedCommands .Concat(descriptors.SelectMany(d => d.GetCommands(_editor.Document))) .ToDictionary(c => c.Id, c => c, StringComparer.InvariantCultureIgnoreCase); var converter = new KeyGestureConverter(); var items = configuration .Where(s => !string.IsNullOrWhiteSpace(s)) .Select(s => s.Trim()) .Select(s => s.Split(new[] { " => " }, StringSplitOptions.RemoveEmptyEntries)) .Select(p => new { StringKey = p[0], Id = p[1] }) .Select(i => new { Key = (KeyGesture)converter.ConvertFromString(i.StringKey), Command = allCommands[i.Id] }) .GroupBy(i => i.Key) ; var keyboardShortcutCollection = new KeyboardShortcutCollection(); foreach (var aGroup in items) { keyboardShortcutCollection.Add(aGroup.Key.Modifiers, aGroup.Key.Key, aGroup.Select(it => it.Command).ToArray()); } return(keyboardShortcutCollection); }
public DocumentEditorContextView(DocumentEditorContext editor) { _viewFactory = new ViewFactory(); _blockSearchHitTester = new BlockSearchHitTester(this); editor.Tag = this; Focusable = true; TextElement.SetFontSize(this, 16); TextElement.SetFontFamily(this, new FontFamily("Times New Roman")); TextOptions.SetTextFormattingMode(this, TextFormattingMode.Display); TextOptions.SetTextHintingMode(this, TextHintingMode.Fixed); TextOptions.SetTextRenderingMode(this, TextRenderingMode.ClearType); Cursor = Cursors.IBeam; _editor = editor; _cursorView = new DocumentCursorView(_editor.Selection); // clear out the existing content _blockCollectionView = new VerticalBlockCollectionView(this, _editor.Document.Root) { Background = Brushes.White, }; _scrollView = new ScrollViewer { Background = new SolidColorBrush(Color.FromRgb(244, 244, 244)), HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, }; Children.Add(_scrollView); SetTop(_scrollView, 0); SetLeft(_scrollView, 0); SetZIndex(_scrollView, 0); // use a grid simply because it's convenient. What we really need is a canvas (for absolute // positioning) that overlays the content of the scroll viewer. var layoutGrid = new Grid { Margin = new Thickness(50), }; _scrollView.Content = new Border() { BorderBrush = new SolidColorBrush(Color.FromRgb(158, 158, 158)), BorderThickness = new Thickness(1), Margin = new Thickness(20), Child = new Border() { Background = new SolidColorBrush(Colors.White), Child = layoutGrid, }, Width = 800, Height = 1000, HorizontalAlignment = HorizontalAlignment.Center, }; // simply add the content in layoutGrid.Children.Add(new StackPanel { Background = Brushes.Transparent, Children = { _blockCollectionView } }); var layoutAbsolute = new Canvas(); layoutGrid.Children.Add(layoutAbsolute); UIElementCollection parentCollection = layoutAbsolute.Children; parentCollection.Add(_cursorView); var keyboardShortcutCollection = ConfigureCommands(); _keyCommands = keyboardShortcutCollection; InsertText("This is an example of a document within the editor. జో It has many features that extend onto " + "multiple lines enough that we can start to create paragraphs. Don't also forget" + "about X & Y and those other things that extend the line length for the X-Files. " + "Isn't that great? జో"); ((IContextualCommand) new BreakTextBlockCommand()).Activate(_editor, _editor.UndoStack); InsertText("Another paragraph with addition text sits here, right where you need it to be."); //var listItem = new ListItemBlock(); //document.Root.Append(listItem); //var cursor = ((Core.ObjectModel.Blocks.Text.TextBlock)listItem.FirstBlock).GetTextCursor(); //cursor.MoveToBeginning(); //cursor.InsertText("A list item"); _editor.UndoStack.Clear(); layoutGrid.PreviewMouseDown += HandlePreviewMouseDown; layoutGrid.PreviewMouseUp += HandlePreviewMouseUp; layoutGrid.PreviewMouseMove += HandlePreviewMouseMove; BlockSearchHitTester.SetShouldStopHitTesting(_cursorView, true); BlockSearchHitTester.SetShouldStopHitTesting(layoutAbsolute, true); Loaded += OnLoaded; Unloaded += OnUnloaded; }