コード例 #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);
            }
        }
コード例 #2
0
        public SplitParagraphCommand(
            ITextEditingCommandController <TContext> controller,
            TextPosition position)
            : base(true, false)
        {
            // The split paragraph consists of inserting a new line, pasting the
            // text to the right of the position into that one, and then removing
            // the text from the current line.
            var line = (int)position.LinePosition;

            // Start by inserting the new line.
            IInsertLineCommand <TContext> insertLineCommand =
                controller.CreateInsertLineCommand((int)position.LinePosition + 1);

            insertLineCommand.UpdateTextPosition = DoTypes.All;

            // Insert the text from the line into the nmew line.
            IInsertTextFromTextRangeCommand <TContext> insertTextCommand =
                controller.CreateInsertTextFromTextRangeCommand(
                    new TextPosition((line + 1), CharacterPosition.Begin),
                    new SingleLineTextRange(
                        position.LinePosition, position.CharacterPosition, CharacterPosition.End));

            // Delete the text from the current line.
            IDeleteTextCommand <TContext> deleteTextCommand =
                controller.CreateDeleteTextCommand(
                    new SingleLineTextRange(
                        line, position.CharacterPosition, CharacterPosition.End));

            // Add the commands into the composite and indicate that the whitespace
            // command controls where the text position will end up.
            Commands.Add(insertLineCommand);
            Commands.Add(insertTextCommand);
            Commands.Add(deleteTextCommand);
        }
コード例 #3
0
        public JoinNextParagraphCommand(
            ITextEditingCommandController <TContext> controller,
            LinePosition line)
            : base(true, false)
        {
            // Joining a paragraph consists of inserting the text of the current
            // paragraph into the previous one with a space and then moving the
            // cursor to the end of the original first paragraph (and space).

            // Insert the text from the line into the prvious line.
            var joinedLine = new LinePosition(line);

            IInsertTextFromTextRangeCommand <TContext> insertCommand =
                controller.CreateInsertTextFromTextRangeCommand(
                    new TextPosition(joinedLine, CharacterPosition.End),
                    new SingleLineTextRange(
                        (int)line + 1, CharacterPosition.Begin, CharacterPosition.End));

            // Finally, delete the current line since we merged it.
            IDeleteLineCommand <TContext> deleteCommand =
                controller.CreateDeleteLineCommand((int)line + 1);

            // Add the commands into the composite and indicate that the whitespace
            // command controls where the text position will end up.
            Commands.Add(insertCommand);
            Commands.Add(deleteCommand);
        }
コード例 #4
0
        public JoinPreviousParagraphCommand(
            ITextEditingCommandController <TContext> controller,
            LinePosition line)
            : base(true, false)
        {
            // Establish our code contracts.
            if (line.Index <= 0)
            {
                throw new InvalidOperationException(
                          "Cannot join the paragraph on the first line.");
            }

            // Joining a paragraph consists of inserting the text of the current
            // paragraph into the previous one with a space and then moving the
            // cursor to the end of the original first paragraph (and space).

            // Insert the text from the line into the prvious line.
            var joinedLine = new LinePosition((int)line - 1);

            IInsertTextFromTextRangeCommand <TContext> insertCommand =
                controller.CreateInsertTextFromTextRangeCommand(
                    new TextPosition(joinedLine, CharacterPosition.End),
                    new SingleLineTextRange(
                        line, CharacterPosition.Begin, CharacterPosition.End));

            insertCommand.UpdateTextPosition = DoTypes.All;

            // Finally, delete the current line since we merged it.
            IDeleteLineCommand <TContext> deleteCommand =
                controller.CreateDeleteLineCommand(line);

            deleteCommand.UpdateTextPosition = DoTypes.None;

            // Add the commands into the composite and indicate that the whitespace
            // command controls where the text position will end up.
            Commands.Add(insertCommand);
            Commands.Add(deleteCommand);
        }