Annotation at start of paragraph changes tab behavior

3 posts, 0 answers
  1. Ted
    Ted avatar
    3 posts
    Member since:
    Apr 2014

    Posted 16 Mar 2015 Link to this post

    When the cursor position is at the start of a paragraph, the tab/shift-tab keys are used to increase/decrease the indent.  If an annotation, such as a bookmark, is placed at the start of the paragraph, this behavior changes and a tab is inserted instead.

    We use annotations to mark areas of our documents and this causes inconsistent behavior.  What's the best way to resolve this?

    Here is an example of some XAML that has 2 lines that work and 1 line that fails because it has a bookmark at the start of the paragraph:
    <t:Paragraph LeftIndent="0">
      <t:Span Text="This paragraph can be indented using the tab key." />
    </t:Paragraph>
    <t:Paragraph LeftIndent="0">
      <t:BookmarkRangeStart AnnotationID="1" Name="MyBookmark" />
      <t:BookmarkRangeEnd AnnotationID="1" />
      <t:Span Text="This paragraph cannot be indented using the tab key." />
    </t:Paragraph>
    <t:Paragraph>
      <t:Span Text="This paragraph can be indented using the tab key." />
    </t:Paragraph>
    <t:Paragraph />

    Thank You
  2. Boby
    Admin
    Boby avatar
    595 posts

    Posted 18 Mar 2015 Link to this post

    Hi Ted,

    This behavior is caused by a specificity of the Bookmark annotation. The position before it is special (non-navigable, due to the value of the SkipPositionBefore property) - meaning that in the case when paragraph starts with Bookmark, you cannot put the caret in the beginning of the paragraph - as the caret will always move after the Bookmark.

    On the other hand, the behavior when pressing Tab key is different in the different contexts, and when the caret is not at the beginning of the paragraph, Tab character ("\t") is inserted instead of changing the left indent. I will log the issue in our backlog, but it will be with low priority as it is more of a small usability problem. You can track the issue here:
    Pressing Tab key when the caret is at the beginning of a Paragraph which starts with Bookmark do not change the indent

    You have some possible workarounds:
    - Bind the "Tab" key directly to IncrementParagraphLeftIndent command. This way you will lose the before-mentioned logic from TabForward command for inserting Tab character, moving to next table cell, etc.; and pressing Tab will always  change the left indent.

    - Use RadRichTextBox.CommandExecuting event, and in this specific case cancel the insertion of the tab character and execute the IncrementParagraphLeftIndent:

    void radRichTextBox_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
    {
        if (e.Command is InsertTextCommand && e.CommandParameter == "\t")
        {
            if (IsAtTheBeginningOfParagraph(this.radRichTextBox.Document.CaretPosition))
            {
                e.Cancel = true;
                this.radRichTextBox.IncrementParagraphLeftIndent();
            }
        }
    }
     
    private bool IsAtTheBeginningOfParagraph(DocumentPosition position)
    {
        var currentParagraph = position.GetCurrentParagraphBox().AssociatedParagraph;
     
        DocumentPosition testPosition = new DocumentPosition(position);
     
        bool canMoveToPrevious = false;
     
        while ((canMoveToPrevious = testPosition.MoveToPrevious()) && testPosition.GetCurrentInline() is AnnotationRangeMarkerBase)
        {
        }
     
        return !canMoveToPrevious || testPosition.GetCurrentParagraphBox().AssociatedParagraph != currentParagraph;
    }

    I hope this was helpful.

    Regards,
    Boby
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ted
    Ted avatar
    3 posts
    Member since:
    Apr 2014

    Posted 18 Mar 2015 in reply to Boby Link to this post

    Thanks Boby.  I'll explore those solutions.  Both sound like good workarounds.
Back to Top