예제 #1
0
        // ========================================
        // method
        // ========================================
        public override void Execute()
        {
            _command = null;

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

            if (inline.IsLineEndCharacter || inline.IsAnchorCharacter)
            {
                /// _indexのinlineがControlCharならば

                if (_target.HasPrevInline(inline))
                {
                    /// 文頭でなければ

                    var prev = _target.GetPrevInline(inline);
                    if (prev.IsLineEndCharacter || prev.IsAnchorCharacter)
                    {
                        /// inlineの前がControlCharacterなら直前にrunを追加
                        var parent = inline.Parent as LineSegment;
                        var index  = parent._Inlines.IndexOf(inline);

                        var newRun = new Run(_text);
                        if (prev.IsLineEndCharacter && prev.HasPrevSibling)
                        {
                            /// inlineの前のLineEndCharacterの前に
                            /// ControlCharacterでないinlineがあればtransfer

                            var para           = parent.Parent as Paragraph;
                            var pp             = prev.PrevSibling as Inline;
                            var ppLine         = pp.Parent;
                            var ppPara         = ppLine == null ? null : ppLine.Parent as Paragraph;
                            var ppParaIsNormal = ppPara != null && ppPara.ParagraphKind == ParagraphKind.Normal;
                            var ppParaIsEqual  = ppPara != null && ppPara.ParagraphKind == para.ParagraphKind;
                            if (pp != null && !pp.IsLineEndCharacter && !pp.IsAnchorCharacter && ppParaIsNormal && ppParaIsEqual)
                            {
                                var ppRun = pp as Run;
                                if (ppRun != null)
                                {
                                    /// Linkは写さない
                                    ppRun.TransferWithoutLink(newRun);
                                }
                                else
                                {
                                    pp.Transfer(newRun);
                                }
                            }
                            else if (para.ParagraphKind != ParagraphKind.Normal)
                            {
                                newRun.Font = _target.GetDefaultFont(para.ParagraphKind);
                            }
                        }
                        _command = new InsertInlineToLineSegmentCommand(parent, newRun, index);
                    }
                    else
                    {
                        var run = prev as Run;
                        if (run != null && run.HasLink)
                        {
                            /// prevにlinkが設定されている場合はlinkを引き継がないように直前にrunを追加
                            var parent = inline.Parent as LineSegment;
                            var index  = parent._Inlines.IndexOf(inline);
                            var newRun = new Run(_text);
                            run.TransferWithoutLink(newRun);
                            _command = new InsertInlineToLineSegmentCommand(parent, newRun, index);
                        }
                        else
                        {
                            _command = new AppendStringToInlineCommand(prev, _text);
                        }
                    }
                }
                else
                {
                    /// 文頭の場合prevがないので挿入しておく
                    var first  = _target.Blocks.First();
                    var newRun = new Run(_text);
                    _target.Transfer(newRun);
                    _command = new InsertInlineBeforeToBlockCommand(first, newRun);
                }
            }
            else
            {
                /// _indexのinlineがControlCharでなければ

                if (_index == inlineOffset)
                {
                    /// inlineの最初の文字を指している場合,

                    /// StyledText中で一番最初のinlineだったり,
                    /// ControlCharacter直後のinlineの場合はinlineにInsert,
                    /// そうでなければ直前のinlineにAppend
                    if (_target.HasPrevInline(inline))
                    {
                        var prev = _target.GetPrevInline(inline);
                        if (prev.IsLineEndCharacter || prev.IsAnchorCharacter)
                        {
                            _command = new InsertStringToInlineCommand(inline, _text, charIndexInInline);
                        }
                        else
                        {
                            var run = prev as Run;
                            if (run != null && run.HasLink)
                            {
                                /// prevにlinkが設定されている場合はlinkを引き継がないように直前にrunを追加
                                var parent = inline.Parent as LineSegment;
                                var index  = parent._Inlines.IndexOf(inline);
                                var newRun = new Run(_text);
                                run.TransferWithoutLink(newRun);
                                _command = new InsertInlineToLineSegmentCommand(parent, newRun, index);
                            }
                            else
                            {
                                _command = new AppendStringToInlineCommand(prev, _text);
                            }
                        }
                    }
                    else
                    {
                        var run = inline as Run;
                        if (run != null && run.HasLink)
                        {
                            /// inlineにlinkを設定されている場合はlinkにならないように直前にrunを挿入
                            var parent = inline.Parent as LineSegment;
                            var index  = parent._Inlines.IndexOf(inline);
                            var newRun = new Run(_text);
                            run.TransferWithoutLink(newRun);
                            _command = new InsertInlineToLineSegmentCommand(parent, newRun, index);
                        }
                        else
                        {
                            /// inlineにlinkが設定されていなければそのまま文字列をinsert
                            _command = new InsertStringToInlineCommand(inline, _text, charIndexInInline);
                        }
                    }
                }
                else
                {
                    /// inlineの最初でなければ普通にInsert
                    _command = new InsertStringToInlineCommand(inline, _text, charIndexInInline);
                }
            }

            _command.Execute();
        }
        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);
        }