public PasteCommand( ITextEditingCommandController <TContext> controller, TextPosition position, string text) : base(true, false) { // Split the clipboard text into different lines. string[] lines = text.Split('\n'); // If we have only one line, then we just insert the command. if (lines.Length == 1) { IInsertTextCommand <TContext> singleCommand = controller.CreateInsertTextCommand(position, text); singleCommand.UpdateTextPosition = DoTypes.All; Commands.Add(singleCommand); return; } // Start by splitting the first paragraph at that position. var splitCommand = new SplitParagraphCommand <TContext>(controller, position); Commands.Add(splitCommand); // The first line is inserted into at the position. IInsertTextCommand <TContext> firstCommand = controller.CreateInsertTextCommand(position, lines[0]); firstCommand.UpdateTextPosition = DoTypes.All; Commands.Add(firstCommand); // Loop through and add all the blank lines we'll need for the paste // operation. for (int i = 2; i < lines.Length; i++) { IInsertLineCommand <TContext> lineCommand = controller.CreateInsertLineCommand((int)position.LinePosition + 1); Commands.Add(lineCommand); } // For every other line, we add the line and paste it. for (int i = 1; i < lines.Length; i++) { IInsertTextCommand <TContext> textCommand = controller.CreateInsertTextCommand( new TextPosition((int)position.LinePosition + i, CharacterPosition.Begin), lines[i]); textCommand.UpdateTextPosition = DoTypes.DoAndRedo; Commands.Add(textCommand); } }
protected override void Do( object context, CommandFactoryManager <OperationContext> commandFactory, object commandData, OperationContext operationContext, EditorViewController controller, IDisplayContext displayContext, TextPosition position) { // If we don't have a selection, this is a simple insert command. TextPosition bufferPosition = displayContext.Caret.Position; TextRange selection = displayContext.Caret.Selection; if (!selection.IsEmpty) { // Create and execute the delete command. We do this separately // so they show up as a different undo item. IUndoableCommand <OperationContext> deleteCommand = DeleteSelectionCommandFactory.CreateCommand(controller, displayContext); controller.CommandController.Do(deleteCommand, operationContext); // We have to reset the position so the insert happens as if // the text doesn't exist. bufferPosition = selection.FirstTextPosition; operationContext = new OperationContext( operationContext.LineBuffer, bufferPosition); } // Create the insert command using the (potentially) modified selection. string text = commandData.ToString(); IInsertTextCommand <OperationContext> insertCommand = controller.CommandController.CreateInsertTextCommand(bufferPosition, text); insertCommand.UpdateTextPosition = DoTypes.All; controller.CommandController.Do(insertCommand, operationContext); // If we have a text position, we need to set it. if (operationContext.Results.HasValue) { displayContext.Caret.SetAndScrollToPosition( operationContext.Results.Value.TextPosition); } }