WPF Flowdocument “change case” feature

2019-02-21 03:29发布

问题:

I am implementing a "change case" functionality for my RichTextBox like word has with Shift+F3. All it does is switching between lower->upper->title case, which is very simple once I get access to the string I need.

My question is, how to change (and find it in the first place) a string in flowdocument without losing any embedded elements (losing formatting is not a problem) that may be contained within the string. Same as word, I need this functionality for 2 cases:

1) Mouse-selected text. I tried simply

this.Selection.Text = newText;

But that of course lost my embedded elements.

2) The last word before caret position. Any non-text element is a word delimiter, however one word can be

"He<weird formatting begin>ll<weird formatting end>o".

回答1:

SOLUTION

This way it mimics MS WORD Shift+F3 behaviour. Only problem that in very few cases occurs is the carret being moved to the word beginning instead of keeping its position. I suppose that a short sleep after EditingCommands.MoveLeftByWord.Execute(null, this); would fix this, but this would be a dirty hack and I am trying to find out a nicer solution.

private void ChangeCase()
    {
        try
        {
            TextPointer start;
            TextPointer end;
            FindSelectedRange(out start, out end);
            List<TextRange> textToChange = SplitToTextRanges(start, end);
            ChangeCaseToAllRanges(textToChange);
        }
        catch (Exception ex)
        {
            mLog.Error("Change case error", ex);
        }


    }

    private void FindSelectedRange(out TextPointer start, out TextPointer end)
    {
        if (!this.Selection.IsEmpty)
        {
            start = this.Selection.Start;
            end = this.Selection.End;
        }
        else
        {
            end = this.CaretPosition;
            EditingCommands.MoveLeftByWord.Execute(null, this);
            start = this.CaretPosition;
            this.CaretPosition = end;

        }
    }

    private static List<TextRange> SplitToTextRanges(TextPointer start, TextPointer end)
    {
        List<TextRange> textToChange = new List<TextRange>();
        var previousPointer = start;
        for (var pointer = start; pointer.CompareTo(end) <= 0; pointer = pointer.GetPositionAtOffset(1, LogicalDirection.Forward))
        {
            var contextAfter = pointer.GetPointerContext(LogicalDirection.Forward);
            var contextBefore = pointer.GetPointerContext(LogicalDirection.Backward);
            if (contextBefore != TextPointerContext.Text && contextAfter == TextPointerContext.Text)
            {
                previousPointer = pointer;
            }
            if (contextBefore == TextPointerContext.Text &&  contextAfter != TextPointerContext.Text && previousPointer != pointer)
            {
                textToChange.Add(new TextRange(previousPointer, pointer));
                previousPointer = null;
            }
        }
        textToChange.Add(new TextRange(previousPointer ?? end, end));
        return textToChange;
    }

    private void ChangeCaseToAllRanges(List<TextRange> textToChange)
    {
        var textInfo = (mCasingCulture ?? CultureInfo.CurrentUICulture).TextInfo;
        var allText = String.Join(" ", textToChange.Select(x => x.Text).Where(x => !string.IsNullOrWhiteSpace(x)));
        Func<string, string> caseChanger = GetConvertorToNextState(textInfo, allText);
        foreach (var range in textToChange)
        {
            if (!range.IsEmpty && !string.IsNullOrWhiteSpace(range.Text))
            {
                range.Text = caseChanger(range.Text);
            }
        }
    }

    private static Func<string, string> GetConvertorToNextState(TextInfo textInfo, string allText)
    {
        Func<string, string> caseChanger;
        if (textInfo.ToLower(allText) == allText)
        {
            caseChanger = (text) => textInfo.ToTitleCase(text);
        }
        else if (textInfo.ToTitleCase(textInfo.ToLower(allText)) == allText)
        {
            caseChanger = (text) => textInfo.ToUpper(text);
        }
        else
        {
            caseChanger = (text) => textInfo.ToLower(text);
        }
        return caseChanger;
    }