Exemplo n.º 1
0
        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)
        {
            // Create the command and execute it.
            var textPosition =
                new TextPosition(
                    displayContext.Caret.Position.LinePosition,
                    displayContext.Caret.Position.CharacterPosition);
            var splitCommand =
                new SplitParagraphCommand <OperationContext>(
                    controller.CommandController, textPosition);

            controller.CommandController.Do(splitCommand, operationContext);

            // If we have a text position, we need to set it.
            if (operationContext.Results.HasValue)
            {
                displayContext.Caret.SetAndScrollToPosition(
                    operationContext.Results.Value.TextPosition);
            }
        }