0

I'm trying to remove the formatting of the selection and what I have so far only removes the formatting on a selection when the selection is inside a paragraph. If the selection extends to another paragraph the formatting is not removed.

Here is what I have so far:

var currentFormat:TextLayoutFormat;
var currentParagraphFormat:TextLayoutFormat;
var containerFormat:TextLayoutFormat;
var selectionStart:int;
var selectionEnd:int;
var operationState:SelectionState;
var editManager:IEditManager;

if (richEditableText.textFlow && richEditableText.textFlow.interactionManager is IEditManager) {
    editManager = IEditManager(richEditableText.textFlow.interactionManager);

    selectionStart = Math.min(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);
    selectionEnd = Math.max(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);

    if (operationState == null) {
        operationState = new SelectionState(richEditableText.textFlow, selectionStart, selectionEnd);
    }

    currentFormat = editManager.getCommonCharacterFormat(operationState);
    currentParagraphFormat = editManager.getCommonParagraphFormat(operationState);
    containerFormat = editManager.getCommonContainerFormat(operationState);

    editManager.clearFormat(currentFormat, currentParagraphFormat, containerFormat);

}
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

1 Answers1

0

It seems that SelectionManager.getCommonCharacterFormat() doesn't quite do what I was thinking it was doing. I need to get the format of the characters that are selected and that function doesn't seem to do that.

If I get a ElementRange and then loop through it I can create a TextLayoutFormat that contains the formats on all the leaves in the element range.

var currentFormat:TextLayoutFormat;
var currentParagraphFormat:TextLayoutFormat;
var containerFormat:TextLayoutFormat;
var selectionStart:int;
var selectionEnd:int;
var operationState:SelectionState;
var editManager:IEditManager;

if (richEditableText.textFlow && richEditableText.textFlow.interactionManager is IEditManager) {
    editManager = IEditManager(richEditableText.textFlow.interactionManager);

    selectionStart = Math.min(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);
    selectionEnd = Math.max(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);

    if (operationState == null) {
        operationState = new SelectionState(richEditableText.textFlow, selectionStart, selectionEnd);
    }

    // following lines were change
    elementRange = ElementRange.createElementRange(richEditableText.textFlow, selectionStart, selectionEnd);
    currentFormat = getElementRangeFormat(elementRange);

    editManager.clearFormat(currentFormat, currentParagraphFormat, containerFormat);

}

// method to get format of the selected range
public static function getElementRangeFormat(elementRange:ElementRange):TextLayoutFormat {      
    var leaf:FlowLeafElement = elementRange.firstLeaf;
    var attr:TextLayoutFormat = new TextLayoutFormat(leaf.computedFormat);

    for (;;)
    {
        if (leaf == elementRange.lastLeaf)
            break;
        leaf = leaf.getNextLeaf();
        attr.concatInheritOnly(leaf.computedFormat);
    }

    return Property.extractInCategory(TextLayoutFormat, TextLayoutFormat.description, attr, Category.CHARACTER, false) as TextLayoutFormat;
}
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231