// ========================================
        // method
        // ========================================
        public override void Execute()
        {
            _ensureLineSegBreakCmd = null;
            _splitBlockCmd         = null;
            _isExecuted            = false;

            int charIndexInBlock, blockOffset;
            var block = _target.GetBlockAt(_charIndex, out charIndexInBlock, out blockOffset);

            if (charIndexInBlock != 0)
            {
                _ensureLineSegBreakCmd = new EnsureLineSegmentBreakCommand(_target, _charIndex);
                _ensureLineSegBreakCmd.Execute();

                var charIndex = _ensureLineSegBreakCmd.IsExecuted? charIndexInBlock + 1: charIndexInBlock;
                int lineIndex, charIndexInLine;
                block.GetLineSegmentAtLocal(charIndex, out lineIndex, out charIndexInLine);

                _splitBlockCmd = new SplitBlockCommand(block, lineIndex);
                _splitBlockCmd.Execute();
                _isExecuted = true;
            }
        }
Example #2
0
        // ========================================
        // method
        // ========================================
        public override void Execute()
        {
            _commands.Clear();

            if (_length == 1)
            {
                /// 削除する長さが1のとき

                int inlineOffset, charIndexInInline;
                var inline = _target.GetInlineAt(_index, out charIndexInInline, out inlineOffset);

                if (inline.IsLineEndCharacter)
                {
                    var line = inline.Parent as LineSegment;

                    if (inline is LineBreak)
                    {
                        /// line breakならlineをマージ
                        var nextLine = line.NextSibling as LineSegment;
                        var cmd      = new MergeLineSegmentsCommand(line, nextLine);
                        cmd.Execute();
                        _commands.Add(cmd);
                    }
                    else if (inline is BlockBreak)
                    {
                        /// block breakならblockをマージ
                        var block = line.Parent as Block;
                        if (block.HasNextSibling)
                        {
                            var nextBlock     = block.NextSibling as Block;
                            var lastLine      = block.LineSegments.Last();
                            var nextFirstLine = nextBlock.LineSegments.First();

                            var mbcmd = new MergeBlocksCommand(block, nextBlock);
                            mbcmd.Execute();
                            _commands.Add(mbcmd);

                            var mlcmd = new MergeLineSegmentsCommand(lastLine, nextFirstLine);
                            mlcmd.Execute();
                            _commands.Add(mlcmd);
                        }
                    }
                }
                else
                {
                    if (inline.Length == 1)
                    {
                        var line = inline.Parent as LineSegment;
                        var cmd  = new RemoveInlineFromLineSegmentCommand(line, inline);
                        cmd.Execute();
                        _commands.Add(cmd);
                    }
                    else
                    {
                        var cmd = new RemoveStringFromInlineCommand(inline, charIndexInInline, 1);
                        cmd.Execute();
                        _commands.Add(cmd);
                    }
                }
            }
            else if (_length > 1)
            {
                var range = new Range(_index, _length);

                /// rangeの最初と最後でblockを分ける
                var endInline = _target.GetInlineAt(range.End);
                var endcmd    = new EnsureBlockBreakCommand(_target, range.End + 1);
                endcmd.Execute();
                _commands.Add(endcmd);
                range = endcmd.IsExecuted && !endInline.IsLineEndCharacter?
                        new Range(_index, _length + 1):
                        range;

                var prevInline = range.Offset > 0? _target.GetInlineAt(range.Offset - 1): null;
                var startcmd   = new EnsureBlockBreakCommand(_target, range.Offset);
                startcmd.Execute();
                _commands.Add(startcmd);
                range = startcmd.IsExecuted && !prevInline.IsLineEndCharacter?
                        new Range(range.Offset + 1, range.Length):
                        range;

                /// 範囲内のblockをすべて削除
                var removeds = new List <Block>();
                foreach (var block in _target.Blocks)
                {
                    var blockRange = _target.GetRange(block);
                    if (range.Contains(blockRange))
                    {
                        removeds.Add(block);
                    }
                }

                foreach (var block in removeds)
                {
                    var cmd = new RemoveBlockFromStyledTextCommand(_target, block);
                    cmd.Execute();
                    _commands.Add(cmd);
                }

                /// rangeの最初でblockを分けた場合はmergeしておく
                if (startcmd.IsExecuted && !prevInline.IsLineEndCharacter)
                {
                    /// 直前のinlineがLineBreakではなかったならば
                    /// blockをつないでlineもつなげる
                    /// (BlockBreakをLineBreakにして,さらにLineBreakを削除)

                    var block         = _target.GetBlockAt(_index);
                    var nextBlock     = block.NextSibling as Block;
                    var lastLine      = block.LineSegments.Last();
                    var nextFirstLine = nextBlock.LineSegments.First();

                    var mbcmd = new MergeBlocksCommand(block, nextBlock);
                    mbcmd.Execute();
                    _commands.Add(mbcmd);

                    var mlcmd = new MergeLineSegmentsCommand(lastLine, nextFirstLine);
                    mlcmd.Execute();
                    _commands.Add(mlcmd);
                }
                else if (startcmd.IsExecuted && prevInline.IsLineEndCharacter)
                {
                    /// 直前のinlineがLineBreakだったならば
                    /// blockをつなぐ
                    /// (BlockBreakをLineBreakにするだけ)

                    var block     = _target.GetBlockAt(_index - 1);
                    var nextBlock = block.NextSibling as Block;

                    var mbcmd = new MergeBlocksCommand(block, nextBlock);
                    mbcmd.Execute();
                    _commands.Add(mbcmd);
                }
            }
        }
        private int InsertFlows(IEnumerable <Flow> flows)
        {
            _commands = new List <ICommand>();
            {
                var cmd = new EnsureInlineBreakCommand(_target, _index);
                cmd.Execute();
                _commands.Add(cmd);
            }

            var firstBlock = _target.GetBlockAt(_index);
            var bIndex     = _target._Blocks.IndexOf(firstBlock);
            var cIndex     = _index;

            foreach (var flow in flows)
            {
                if (flow is Block)
                {
                    var block = flow as Block;

                    int charIndexInLine, lineSegOffset;
                    _target.GetLineSegmentAt(cIndex, out charIndexInLine, out lineSegOffset);

                    if (charIndexInLine == 0)
                    {
                        /// Line中でなければそのまま挿入
                        var cmd = new InsertBlockToStyledTextCommand(_target, block, bIndex);
                        cmd.Execute();
                        _commands.Add(cmd);
                        cIndex += flow.Length;
                    }
                    else
                    {
                        /// Line中ならInlineだけ挿入
                        foreach (var line in block.LineSegments)
                        {
                            foreach (var inline in line.Inlines)
                            {
                                if (inline.IsLineEndCharacter)
                                {
                                    if (inline is BlockBreak)
                                    {
                                        var cmd = new InsertBlockBreakCommand(_target, cIndex);
                                        cmd.Execute();
                                        _commands.Add(cmd);
                                    }
                                    else if (inline is LineBreak)
                                    {
                                        var cmd = new InsertLineBreakCommand(_target, cIndex);
                                        cmd.Execute();
                                        _commands.Add(cmd);
                                    }
                                }
                                else
                                {
                                    var clone = inline.CloneDeeply() as Inline;
                                    var cmd   = new InsertInlineCommand(_target, cIndex, clone);
                                    cmd.Execute();
                                    _commands.Add(cmd);
                                }
                                cIndex += inline.Length;
                            }
                        }
                    }
                    ++bIndex;
                }
                else if (flow is Inline)
                {
                    var inline = flow as Inline;
                    if (inline.IsLineEndCharacter)
                    {
                        if (inline is BlockBreak)
                        {
                            var cmd = new InsertBlockBreakCommand(_target, cIndex);
                            cmd.Execute();
                            _commands.Add(cmd);
                            ++bIndex;
                        }
                        else if (inline is LineBreak)
                        {
                            var cmd = new InsertLineBreakCommand(_target, cIndex);
                            cmd.Execute();
                            _commands.Add(cmd);
                        }
                    }
                    else
                    {
                        var clone = inline.CloneDeeply() as Inline;
                        var cmd   = new InsertInlineCommand(_target, cIndex, clone);
                        cmd.Execute();
                        _commands.Add(cmd);
                    }
                    cIndex += flow.Length;
                }
            }

            /// 最初のinlineがmergeできるならmerge
            var firstInline = _target.GetInlineAt(_index);

            if (_target.HasPrevInline(firstInline))
            {
                var prev = _target.GetPrevInline(firstInline);
                if (prev.CanMerge(firstInline))
                {
                    var cmd = new MergeInlineCommand(prev, firstInline);
                    cmd.Execute();
                    _commands.Add(cmd);
                }
            }

            /// 最後のinlineがmergeできるならmerge
            var lastInline = _target.GetInlineAt(cIndex - 1);

            if (_target.HasNextInline(lastInline))
            {
                var next = _target.GetNextInline(lastInline);
                if (lastInline.CanMerge(next))
                {
                    var cmd = new MergeInlineCommand(lastInline, next);
                    cmd.Execute();
                    _commands.Add(cmd);
                }
            }

            return(cIndex);
        }