예제 #1
0
        /// <summary>
        /// 设置文本
        /// </summary>
        /// <param name="newText">新文本</param>
        /// <param name="flags">标记</param>
        /// <param name="disablePermissioin">禁止权限控制</param>
        /// <returns>操作是否修改了对象内容</returns>
        public bool SetEditorTextExt(string newText, DomAccessFlags flags, bool disablePermissioin, bool updateContent)
        {
            bool result = false;

            if (this.OwnerDocument != null)
            {
                string oldText = this.Text;
                if (oldText == null || oldText != newText)
                {
                    // 为了提高效率,只有出现不同的文本才进行文本替换操作
                    bool innerLogUndo = this.OwnerDocument.CanLogUndo;
                    if (innerLogUndo == false)
                    {
                        this.OwnerDocument.BeginLogUndo();
                    }
                    if (string.IsNullOrEmpty(newText))
                    {
                        // 直接设置空内容
                        ReplaceElementsArgs args = new ReplaceElementsArgs(
                            this,
                            0,
                            this.Elements.Count,
                            null,
                            true,
                            true,
                            true);
                        args.DisablePermission = disablePermissioin;
                        args.AccessFlags       = flags;
                        args.ChangeSelection   = false;
                        args.UpdateContent     = updateContent;
                        if (args.DisablePermission == false &&
                            this.OwnerDocument.Options.SecurityOptions.EnablePermission)
                        {
                            // 很多时候,本属性是为下拉列表文本输入域调用的,此时输入域前面的文本可能是被
                            // 当前用户输入的,后面的元素已经被逻辑删除了,此时应该应该是执行物理删除,
                            // 在此进行判断,修正删除区间。
                            for (int iCount = 0; iCount < this.Elements.Count; iCount++)
                            {
                                DomElement element = this.Elements[iCount];
                                if (element.Style.DeleterIndex >= 0)
                                {
                                    bool fix = true;
                                    for (int iCount2 = iCount; iCount2 < this.Elements.Count; iCount2++)
                                    {
                                        if (this.Elements[iCount2].Style.DeleterIndex < 0)
                                        {
                                            fix = false;
                                            break;
                                        }
                                    }
                                    if (fix)
                                    {
                                        args.DeleteLength = iCount;
                                    }
                                    break;
                                }
                            }//for
                        }
                        result = this.OwnerDocument.ReplaceElements(args) != 0;
                    }
                    else
                    {
                        // 设置文本内容
                        DomElementList list = this.OwnerDocument.CreateTextElements(newText, null, this.Style);
                        if (list != null && list.Count > 0)
                        {
                            ReplaceElementsArgs args = new ReplaceElementsArgs(
                                this,
                                0,
                                this.Elements.Count,
                                list,
                                true,
                                true,
                                true);
                            args.DisablePermission = disablePermissioin;
                            args.AccessFlags       = flags;
                            args.ChangeSelection   = false;
                            args.UpdateContent     = updateContent;
                            if (args.DisablePermission == false &&
                                this.OwnerDocument.Options.SecurityOptions.EnablePermission)
                            {
                                // 很多时候,本属性是为下拉列表文本输入域调用的,此时输入域前面的文本可能是被
                                // 当前用户输入的,后面的元素已经被逻辑删除了,此时应该应该是执行物理删除,
                                // 在此进行判断,修正删除区间。
                                for (int iCount = 0; iCount < this.Elements.Count; iCount++)
                                {
                                    DomElement element = this.Elements[iCount];
                                    if (element.Style.DeleterIndex >= 0)
                                    {
                                        bool fix = true;
                                        for (int iCount2 = iCount; iCount2 < this.Elements.Count; iCount2++)
                                        {
                                            if (this.Elements[iCount2].Style.DeleterIndex < 0)
                                            {
                                                fix = false;
                                                break;
                                            }
                                        }
                                        if (fix)
                                        {
                                            args.DeleteLength = iCount;
                                        }
                                        break;
                                    }
                                }//for
                            }
                            result = this.OwnerDocument.ReplaceElements(args) != 0;
                        }
                    }
                    if (innerLogUndo == false)
                    {
                        if (result)
                        {
                            this.OwnerDocument.EndLogUndo();
                        }
                        else
                        {
                            this.OwnerDocument.CancelLogUndo();
                        }
                    }
                    if (result)
                    {
                        this.OwnerDocument.OnDocumentContentChanged();
                        this.OwnerDocument.OnSelectionChanged();
                    }
                }
            }
            else
            {
                DomElementList list = this.SetInnerTextFast(newText);
                result = list != null && list.Count > 0;
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 全局替换
        /// </summary>
        /// <param name="args">参数</param>
        /// <returns>替换的次数</returns>
        public int ReplaceAll(SearchReplaceCommandArgs args)
        {
            int        result = 0;
            List <int> indexs = new List <int>();
            SearchReplaceCommandArgs args2 = args.Clone();

            args2.Backward = false;
            int currentIndex = this.Content.Count - 1;

            this.Document.BeginLogUndo();
            DocumentControler controler = this.Document.DocumentControler;
            Dictionary <DomContentElement, int> startIndexs = new Dictionary <DomContentElement, int>();

            while (true)
            {
                int index = Search(args2, false, currentIndex);
                if (index >= 0)
                {
                    DomSelection mySelection = new DomSelection(this.Document.CurrentContentElement);
                    mySelection.Refresh(index, args.SearchString.Length);
                    DomContainerElement container = null;
                    int elementIndex = 0;
                    this.Content.GetPositonInfo(index, out container, out elementIndex, false);
                    DomContentElement contentElement = container.ContentElement;
                    int pi = contentElement.PrivateContent.IndexOf(this.Content[index]);
                    if (startIndexs.ContainsKey(contentElement))
                    {
                        startIndexs[contentElement] = Math.Min(startIndexs[contentElement], pi);
                    }
                    else
                    {
                        startIndexs[contentElement] = pi;
                    }
                    indexs.Add(index);
                    if (string.IsNullOrEmpty(args.ReplaceString))
                    {
                        this.Content.DeleteSelection(true, false, true, mySelection);
                    }
                    else
                    {
                        DomElementList newElements = this.Document.CreateTextElements(
                            args.ReplaceString,
                            (DocumentContentStyle)this.Document.CurrentParagraphStyle,
                            (DocumentContentStyle)this.Document.EditorCurrentStyle.Clone());
                        ReplaceElementsArgs args3 = new ReplaceElementsArgs(
                            container,
                            index,
                            0,
                            newElements,
                            true,
                            false,
                            true);
                        int repResult = this.Document.ReplaceElements(args3);
                    }
                    result++;
                }
                else
                {
                    break;
                }
                currentIndex = index + args2.SearchString.Length;
            }//while
            this.Document.EndLogUndo();
            if (startIndexs.Count > 0)
            {
                bool refreshPage = false;
                foreach (DomContentElement ce in startIndexs.Keys)
                {
                    ce.UpdateContentElements(true);
                    ce.UpdateContentVersion();
                    ce._NeedRefreshPage = false;
                    ce.RefreshPrivateContent(startIndexs[ce]);
                    if (ce._NeedRefreshPage)
                    {
                        refreshPage = true;
                    }
                }
                if (refreshPage)
                {
                    this.Document.RefreshPages();
                    if (this.Document.EditorControl != null)
                    {
                        this.Document.EditorControl.UpdatePages();
                        this.Document.EditorControl.UpdateTextCaret();
                        this.Document.EditorControl.Invalidate();
                    }
                }
            }
            return(startIndexs.Count);
        }