public void ProcessImmediateEdits( BlockCommandContext context, Block block, int textIndex) { // Get the plugin settings from the project. ImmediateBlockTypesSettings settings = Settings; // Grab the substring from the beginning to the index and compare that // in the dictionary. string text = block.Text.Substring(0, textIndex); if (!settings.Replacements.ContainsKey(text)) { // We want to fail as fast as possible. return; } // If the block type is already set to the same name, skip it. string blockTypeName = settings.Replacements[text]; BlockType blockType = Project.BlockTypes[blockTypeName]; if (block.BlockType == blockType) { return; } // Perform the substitution with a replace operation and a block change // operation. var replaceCommand = new ReplaceTextCommand( new BlockPosition(block.BlockKey, 0), textIndex, string.Empty); var changeCommand = new ChangeBlockTypeCommand(block.BlockKey, blockType); // Create a composite command that binds everything together. var compositeCommand = new CompositeCommand <BlockCommandContext>(true, false); compositeCommand.Commands.Add(replaceCommand); compositeCommand.Commands.Add(changeCommand); // Add the command to the deferred execution so the command could // be properly handled via the undo/redo management. block.Project.Commands.DeferDo(compositeCommand); }
public void TestUndoRedoUndoCommand() { // Arrange ProjectBlockCollection blocks; BlockCommandSupervisor commands; BlockTypeSupervisor blockTypes; BlockCommandContext context; SetupMultilineTest(out context, out blocks, out blockTypes, out commands); var command = new ChangeBlockTypeCommand( blocks[0].BlockKey, blockTypes.Paragraph); commands.Do(command, context); commands.Undo(context); commands.Redo(context); // Act commands.Undo(context); // Assert Assert.AreEqual(4, blocks.Count); Assert.AreEqual(new BlockPosition(blocks[0], 0), commands.LastPosition); int index = 0; Assert.AreEqual("Line 1", blocks[index].Text); Assert.AreEqual(blockTypes.Chapter, blocks[index].BlockType); index++; Assert.AreEqual("Line 2", blocks[index].Text); Assert.AreEqual(blockTypes.Scene, blocks[index].BlockType); index++; Assert.AreEqual("Line 3", blocks[index].Text); Assert.AreEqual(blockTypes.Scene, blocks[index].BlockType); index++; Assert.AreEqual("Line 4", blocks[index].Text); Assert.AreEqual(blockTypes.Scene, blocks[index].BlockType); }