public Block SplitBlock(Block blockToSplit, string verseToSplit, int characterOffsetToSplit)
        {
            Block newBlock = CurrentBook.SplitBlock(blockToSplit, verseToSplit, characterOffsetToSplit);

            AddToRelevantBlocksIfNeeded(newBlock);
            return(newBlock);
        }
Example #2
0
        public void SplitBlock(IEnumerable <BlockSplitData> blockSplits, List <KeyValuePair <int, string> > characters)
        {
            // set the character for the first block
            Block currentBlock     = CurrentBlock;
            var   firstCharacterId = characters.First(c => c.Key == 0).Value;

            if (currentBlock.CharacterId != firstCharacterId)
            {
                currentBlock.CharacterId = firstCharacterId;
                currentBlock.Delivery    = null;
            }

            foreach (var groupOfSplits in blockSplits.GroupBy(s => new { s.BlockToSplit }))
            {
                foreach (var blockSplitData in groupOfSplits.OrderByDescending(s => s, BlockSplitData.BlockSplitDataVerseAndOffsetComparer))
                {
                    // get the character selected for this split
                    var characterId = characters.First(c => c.Key == blockSplitData.Id).Value;

                    var newBlock = CurrentBook.SplitBlock(blockSplitData.BlockToSplit, blockSplitData.VerseToSplit,
                                                          blockSplitData.CharacterOffsetToSplit, true, characterId, m_project.Versification);

                    var newBlockIndices            = GetBlockIndices(newBlock);
                    var blocksIndicesNeedingUpdate = m_relevantBlocks.Where(
                        r => r.BookIndex == newBlockIndices.BookIndex &&
                        r.BlockIndex >= newBlockIndices.BlockIndex);
                    foreach (var block in blocksIndicesNeedingUpdate)
                    {
                        block.BlockIndex++;
                    }

                    AddToRelevantBlocksIfNeeded(newBlock);
                }
            }
        }
        public void SplitBlock(IEnumerable <BlockSplitData> blockSplits, List <KeyValuePair <int, string> > characters)
        {
            try
            {
                CurrentBlockMatchupChanged -= OnCurrentBlockMatchupChanged;

                // set the character for the first block
                Block currentBlock     = CurrentBlock;
                var   firstCharacterId = characters.First(c => c.Key == 0).Value;
                if (currentBlock.CharacterId != firstCharacterId)
                {
                    if (string.IsNullOrEmpty(firstCharacterId))
                    {
                        currentBlock.CharacterId = CharacterVerseData.kUnexpectedCharacter;
                    }
                    else
                    {
                        Debug.Assert(currentBlock.CharacterIdOverrideForScript == null && firstCharacterId.SplitCharacterId().Length == 1,
                                     "This is a case that needs to be fixed for PG-1143");
                        currentBlock.CharacterId = firstCharacterId;
                        AddPendingProjectCharacterVerseDataIfNeeded(currentBlock, firstCharacterId);
                    }
                    currentBlock.Delivery = null;
                }

                foreach (var groupOfSplits in blockSplits.GroupBy(s => new { s.BlockToSplit }))
                {
                    foreach (var blockSplitData in groupOfSplits.OrderByDescending(s => s,
                                                                                   BlockSplitData.BlockSplitDataVerseAndOffsetComparer))
                    {
                        // get the character selected for this split
                        var characterId = characters.First(c => c.Key == blockSplitData.Id).Value;

                        var originalNextBlock  = BlockAccessor.GetNthNextBlockWithinBook(1, blockSplitData.BlockToSplit);
                        var chipOffTheOldBlock = CurrentBook.SplitBlock(blockSplitData.BlockToSplit, blockSplitData.VerseToSplit,
                                                                        blockSplitData.CharacterOffsetToSplit, true, characterId);
                        if (!string.IsNullOrEmpty(characterId))
                        {
                            AddPendingProjectCharacterVerseDataIfNeeded(chipOffTheOldBlock, characterId);
                        }

                        var isNewBlock = originalNextBlock != chipOffTheOldBlock;
                        if (isNewBlock)
                        {
                            var newBlockIndices            = GetBlockIndices(chipOffTheOldBlock);
                            var blocksIndicesNeedingUpdate = m_relevantBookBlockIndices.Where(
                                r => r.BookIndex == newBlockIndices.BookIndex &&
                                r.BlockIndex >= newBlockIndices.BlockIndex);
                            foreach (var bookBlockIndices in blocksIndicesNeedingUpdate)
                            {
                                bookBlockIndices.BlockIndex++;
                            }
                        }
                        else
                        {
                            // We "split" between existing blocks in a multiblock quote,
                            // so we don't need to do the same kind of cleanup above.
                        }
                        AddToRelevantBlocksIfNeeded(chipOffTheOldBlock, isNewBlock);
                    }
                }

                if (AttemptRefBlockMatchup)
                {
                    // A split will always require the current matchup to be re-constructed.
                    SetBlockMatchupForCurrentVerse();
                }

                // This is basically a hack. All kinds of problems were occurring after splits causing our indices to get off.
                // See https://jira.sil.org/browse/PG-1075. This ensures our state is valid every time.
                SetModeInternal(Mode, true);
            }
            finally
            {
                CurrentBlockMatchupChanged += OnCurrentBlockMatchupChanged;
            }
        }