Search code examples
tlf

How to get the specific FlowElement under the cursor or selection


I have a TextFlow that has an SelectionManager attached. I'd like to get the FlowElement that the cursor is inside of or if multiple lines are selected that the selection is inside of.

_textFlow.addEventListener(SelectionEvent.SELECTION_CHANGE,selectionChangeListener,false,0,true);


private function selectionChangeListener(e:SelectionEvent):void
{               
    var selectionState:SelectionState = e.selectionState;
    var selectedElementRange:ElementRange = ElementRange.createElementRange(selectionState.textFlow, selectionState.absoluteStart, selectionState.absoluteEnd);
}

Solution

  • I found how to find the leaf element with the following method but still do not know how to find the element that contains the selection.

    /** Returns the current FlowELement element at the currentStyleLevel */
    private function currentStyleElement(absolutePosition:int):FlowElement
    {
        var e:FlowElement;
        switch(currentStyleLevel)
        {
            case TextFlow:
                e = _textFlow;
                break;
            case ParagraphElement:
                e = _textFlow.findLeaf(absolutePosition).getParagraph();
                break;
            default:
                e = _textFlow.findLeaf(absolutePosition);
        }
        return e;
    }