/// <summary> /// Splits a block in two at the given index. /// </summary> /// <param name="operation">Details of the operation performed.</param> public virtual void SplitBlock(WriteableSplitBlockOperation operation) { int SplitBlockIndex = operation.BlockIndex; Debug.Assert(SplitBlockIndex >= 0 && SplitBlockIndex < BlockStateList.Count); int SplitIndex = operation.Index; Debug.Assert(SplitIndex > 0); IWriteableBlockState BlockState = (IWriteableBlockState)BlockStateList[SplitBlockIndex]; Debug.Assert(SplitIndex < BlockState.StateList.Count); IBlock NewBlock = operation.NewBlock; Debug.Assert(NewBlock != null); NodeTreeHelperBlockList.SplitBlock(Owner.Node, PropertyName, SplitBlockIndex, SplitIndex, NewBlock); NodeTreeHelperBlockList.GetChildNode(NewBlock, 0, out Node NewBlockFirstNode); IWriteableBrowsingNewBlockNodeIndex NewBlockIndex = CreateNewBlockNodeIndex(NewBlockFirstNode, SplitBlockIndex); IWriteableBlockState NewBlockState = (IWriteableBlockState)CreateBlockState(NewBlockIndex, NewBlock); ((IWriteableBlockState <IWriteableInner <IWriteableBrowsingChildIndex> >)NewBlockState).InitBlockState(); InsertInBlockStateList(NewBlockIndex.BlockIndex, NewBlockState); for (int i = 0; i < SplitIndex; i++) { IWriteablePlaceholderNodeState State = (IWriteablePlaceholderNodeState)BlockState.StateList[0]; IWriteableBrowsingExistingBlockNodeIndex ChildNodeIndex = State.ParentIndex as IWriteableBrowsingExistingBlockNodeIndex; Debug.Assert(ChildNodeIndex != null); BlockState.Remove(ChildNodeIndex, 0); NewBlockState.Insert(ChildNodeIndex, i, State); } operation.Update(NewBlockState); for (int i = 0; i < BlockState.StateList.Count; i++) { IWriteablePlaceholderNodeState State = (IWriteablePlaceholderNodeState)BlockState.StateList[i]; IWriteableBrowsingExistingBlockNodeIndex ChildNodeIndex = State.ParentIndex as IWriteableBrowsingExistingBlockNodeIndex; Debug.Assert(ChildNodeIndex != null); ChildNodeIndex.MoveBlockUp(); for (int j = 0; j < SplitIndex; j++) { ChildNodeIndex.MoveDown(); } } for (int i = SplitBlockIndex + 2; i < BlockStateList.Count; i++) { foreach (IWriteablePlaceholderNodeState State in BlockStateList[i].StateList) { IWriteableBrowsingExistingBlockNodeIndex ChildNodeIndex = State.ParentIndex as IWriteableBrowsingExistingBlockNodeIndex; Debug.Assert(ChildNodeIndex != null); ChildNodeIndex.MoveBlockUp(); } } }