如何在 Flex/Spark TextArea 或 TextFlow 中的特定位置添加图像

发布于 2024-09-15 08:04:13 字数 3091 浏览 2 评论 0原文

我有一个 Spark TextArea:

<s:TextArea id="editor">
    <s:textFlow>
        <s:TextFlow id="_tf" >
            <s:p>Lorem ipsum etc.</s:p>
            <s:p>
                 <s:img source="http://example.com/example.jpg" />
             </s:p>
            <s:p>Aliquam tincidunt tempor etc.</s:p>
        </s:TextFlow>
    </s:textFlow>
</s:TextArea>

和一个用于添加图像的按钮:

<s:Button id="imageBtn" 
    label="insert image" 
    width="89"
    click="imageBtn_clickHandler(event);" />

以下脚本有效:

    import flashx.textLayout.elements.*;

    import mx.events.FlexEvent;

    protected function imageBtn_clickHandler(evt:MouseEvent):void {
        var img:InlineGraphicElement = new InlineGraphicElement();
        img.source = "http://example.com/example.jpg";

        var p:ParagraphElement = new ParagraphElement();
        p.addChild(img);
        _tf.addChild(p);
        _tf.flowComposer.updateAllControllers();
        editor.setFocus();
    }

但仅附加到 TextFlow 的末尾。如何在 TextArea 中的活动插入符处插入 InlineGraphicElement?我的第一个想法是:

    import flashx.textLayout.elements.*;

    import mx.events.FlexEvent;

    protected function imageBtn_clickHandler(evt:MouseEvent):void {
        var img:InlineGraphicElement = new InlineGraphicElement();
        img.source = "http://example.com/example.jpg";

        var p:ParagraphElement;
    //pseudocode
    p = _tf.getCaret.AncestorThatIsParagraphElement;
    // end pseudocode
    p.addChild(img);
        _tf.flowComposer.updateAllControllers();
        editor.setFocus();
    }

但这仍然只会附加在当前段落的末尾,假设我什至可以找到当前段落作为 .addChild() 的对象。那么如何将 InlineGraphicElement 插入 TextFlow 对象的子段落中的文本中间(甚至替换文本)。

感谢您的见解。

更新:越来越近了。

我可以在段落的开头或结尾添加内容。

protected function imageBtn_clickHandler(evt:MouseEvent):void {
    var img:InlineGraphicElement = new InlineGraphicElement();
    img.source = "http://example.com/example.jpg";
    var insertInto:ParagraphElement = new ParagraphElement();
    insertInto = editor.textFlow.getChildAt(
        editor.textFlow.findChildIndexAtPosition(
            editor.selectionAnchorPosition
        )).getParagraph();
    insertInto.addChildAt(0, img); // inserts at beginning of paragraph
    insertInto.addChildAt(1, img); // inserts at end of paragraph

    _tf.flowComposer.updateAllControllers();
    editor.setFocus();
}

但插入中间仍然没有任何乐趣。另外,上面的代码不会替换突出显示的文本,但这不是这里真正关心的问题。


解决方案:

根据 Eugene 的链接,关键是 EditManager。

以下代码代表工作更新函数:

protected function imageBtn_clickHandler(evt:MouseEvent):void {
    var em:EditManager = editor.textFlow.interactionManager as EditManager;
    em.selectRange(editor.selectionAnchorPosition, editor.selectionActivePosition);
    em.insertInlineGraphic("http://example.com/example.jpg","auto","auto");
    _tf.flowComposer.updateAllControllers();
    editor.setFocus();
}

I have a Spark TextArea:

<s:TextArea id="editor">
    <s:textFlow>
        <s:TextFlow id="_tf" >
            <s:p>Lorem ipsum etc.</s:p>
            <s:p>
                 <s:img source="http://example.com/example.jpg" />
             </s:p>
            <s:p>Aliquam tincidunt tempor etc.</s:p>
        </s:TextFlow>
    </s:textFlow>
</s:TextArea>

And a button to add an image:

<s:Button id="imageBtn" 
    label="insert image" 
    width="89"
    click="imageBtn_clickHandler(event);" />

The following script works:

    import flashx.textLayout.elements.*;

    import mx.events.FlexEvent;

    protected function imageBtn_clickHandler(evt:MouseEvent):void {
        var img:InlineGraphicElement = new InlineGraphicElement();
        img.source = "http://example.com/example.jpg";

        var p:ParagraphElement = new ParagraphElement();
        p.addChild(img);
        _tf.addChild(p);
        _tf.flowComposer.updateAllControllers();
        editor.setFocus();
    }

but only appends to the end of the TextFlow. How can I insert the InlineGraphicElement at the active caret in the TextArea? My first thought is something like:

    import flashx.textLayout.elements.*;

    import mx.events.FlexEvent;

    protected function imageBtn_clickHandler(evt:MouseEvent):void {
        var img:InlineGraphicElement = new InlineGraphicElement();
        img.source = "http://example.com/example.jpg";

        var p:ParagraphElement;
    //pseudocode
    p = _tf.getCaret.AncestorThatIsParagraphElement;
    // end pseudocode
    p.addChild(img);
        _tf.flowComposer.updateAllControllers();
        editor.setFocus();
    }

But this still would only append at the end of the current paragraph, assuming I could even find the current paragraph as an object to .addChild() with. So how can I insert the InlineGraphicElement in the middle of text (or even replacing text) in a child paragraph of a TextFlow object.

Thanks for your insight.

UPDATE: Getting closer.

I'm able to add to the beginning or end of a paragraph.

protected function imageBtn_clickHandler(evt:MouseEvent):void {
    var img:InlineGraphicElement = new InlineGraphicElement();
    img.source = "http://example.com/example.jpg";
    var insertInto:ParagraphElement = new ParagraphElement();
    insertInto = editor.textFlow.getChildAt(
        editor.textFlow.findChildIndexAtPosition(
            editor.selectionAnchorPosition
        )).getParagraph();
    insertInto.addChildAt(0, img); // inserts at beginning of paragraph
    insertInto.addChildAt(1, img); // inserts at end of paragraph

    _tf.flowComposer.updateAllControllers();
    editor.setFocus();
}

But still no joy in inserting into the middle. Also, the above code wouldn't replace highlighted text, but that's not the real concern here.


SOLUTION:

Based on Eugene's link the key to this is EditManager.

The following code represents the working updated function:

protected function imageBtn_clickHandler(evt:MouseEvent):void {
    var em:EditManager = editor.textFlow.interactionManager as EditManager;
    em.selectRange(editor.selectionAnchorPosition, editor.selectionActivePosition);
    em.insertInlineGraphic("http://example.com/example.jpg","auto","auto");
    _tf.flowComposer.updateAllControllers();
    editor.setFocus();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-09-22 08:04:13

使用此代码即可获得解决方案。

这是从系统文件夹获取的图像

        protected function addImg_clickHandler(event:MouseEvent):void {
            _file_ref = new FileReference();
            _file_ref.addEventListener( Event.SELECT, handleFileSelect,false,0,true );
            var filter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
            _file_ref.browse([filter]);
        }

        private function handleFileSelect(e:Event):void {
            _file_ref.removeEventListener( Event.SELECT, handleFileSelect );
            _file_ref.addEventListener(Event.COMPLETE, handleFileOpen,false,0,true );
            _file_ref.load();
        }

        private function handleFileOpen(e:Event):void {
            _file_ref.removeEventListener(Event.COMPLETE, handleFileOpen );
            var data:ByteArray = _file_ref.data as ByteArray;
            _img_loader= new Loader();
            _img_loader.loadBytes(data);
            _img_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoadComplete,false,0,true);
        }

        protected function imageLoadComplete(e:Event):void{
            _img_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,imageLoadComplete);
            var bmd:BitmapData=Bitmap(_img_loader.content).bitmapData;
            var bm:Bitmap=new Bitmap(bmd);  
            var em:EditManager = editorText.textFlow.interactionManager as EditManager;
            em.selectRange(editorText.selectionAnchorPosition, editorText.selectionActivePosition);
            em.insertInlineGraphic(bm,bm.width,bm.height);
            editorText.textFlow.flowComposer.updateAllControllers();
            editorText.setFocus();
        } 


        protected function addTable_clickHandler(event:MouseEvent):void
        {
            var tblement:TableElement = new TableElement();
            var em:EditManager = editorText.textFlow.interactionManager as EditManager;
            em.selectRange(editorText.selectionAnchorPosition, editorText.selectionActivePosition);
            em.insertTableElement(tblement);
            editorText.textFlow.flowComposer.updateAllControllers();
            editorText.setFocus();
        } 

Use this code you get solution.

here image getting from your system folder

        protected function addImg_clickHandler(event:MouseEvent):void {
            _file_ref = new FileReference();
            _file_ref.addEventListener( Event.SELECT, handleFileSelect,false,0,true );
            var filter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
            _file_ref.browse([filter]);
        }

        private function handleFileSelect(e:Event):void {
            _file_ref.removeEventListener( Event.SELECT, handleFileSelect );
            _file_ref.addEventListener(Event.COMPLETE, handleFileOpen,false,0,true );
            _file_ref.load();
        }

        private function handleFileOpen(e:Event):void {
            _file_ref.removeEventListener(Event.COMPLETE, handleFileOpen );
            var data:ByteArray = _file_ref.data as ByteArray;
            _img_loader= new Loader();
            _img_loader.loadBytes(data);
            _img_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoadComplete,false,0,true);
        }

        protected function imageLoadComplete(e:Event):void{
            _img_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,imageLoadComplete);
            var bmd:BitmapData=Bitmap(_img_loader.content).bitmapData;
            var bm:Bitmap=new Bitmap(bmd);  
            var em:EditManager = editorText.textFlow.interactionManager as EditManager;
            em.selectRange(editorText.selectionAnchorPosition, editorText.selectionActivePosition);
            em.insertInlineGraphic(bm,bm.width,bm.height);
            editorText.textFlow.flowComposer.updateAllControllers();
            editorText.setFocus();
        } 


        protected function addTable_clickHandler(event:MouseEvent):void
        {
            var tblement:TableElement = new TableElement();
            var em:EditManager = editorText.textFlow.interactionManager as EditManager;
            em.selectRange(editorText.selectionAnchorPosition, editorText.selectionActivePosition);
            em.insertTableElement(tblement);
            editorText.textFlow.flowComposer.updateAllControllers();
            editorText.setFocus();
        } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文