Skip to main content
Inspiring
March 11, 2010
Answered

applyLink Not Removing Existing Hyperlinks?

  • March 11, 2010
  • 1 reply
  • 1332 views

Consider the following code:

private function btnEvent_RemoveHyperlink():void {
         trace('removing hyperlink!');
         editManager.applyLink(null, null, true);
         //currentTLF.color = 0x000000;
         //currentTLF.textDecoration = TextDecoration.NONE;
         //editManager.applyLeafFormat(currentTLF);
}

As part of my rich editing components I'm writing for our Flex 3 apps, I've included add link and remove link functionality like a typical word processor.

From the example code I found in the latest API docs I can use appyLink with the parameters above to remove a link but it seems to only do half of the job. The formatting does not get removed unless I specifically go out of my way to do so - see commented code. Also even though it appears that the link is removed from a functional perspective, when I hold control the little hand icon still appears but clicking doesn't do anything as expected.

This seems like a bug. Any further comments?

This topic has been closed for replies.
Correct answer robin_briggs

Looks like our example code is wrong. When you remove the link, try calling it this way:

     editManager.applyLink("", null, true);

That should work -- I tried it on your code example and it did. I think using null should work, but it doesn't, so just use the empty string instead. I will file a bug.

Thanks,

- robin

1 reply

Adobe Employee
March 11, 2010

Can you provide a sample AS3 program that illustrates the problem?  I ran a simple test where I created a link and then selected it, set the link to null and called applyLink again.  The formatting dissapeared as expected.

Thanks,

Richard

Inspiring
March 12, 2010

Hi Richard,

Below is my code. I am first building my rich text editor as a component for Flex 3. I've stripped out a lot of the UI and event handlers, and just focused on the two hyperlinking functons; and how some of the TLF essentials have been implemented. It sounded to me from your explanation like you performed one extra step before calling applyLink for removal.

rdermer wrote:

I ran a simple test where I created a link and then selected it, set the link to null and called applyLink again.  The formatting dissapeared as expected.

Not sure what this meant, or if I am missing something. Please review and comment; thanks!

Cheers,

Matt

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="772" height="250" initialize="componentInitialize()" borderColor="#dedede" borderStyle="solid" borderThickness="1" horizontalScrollPolicy="off" verticalScrollPolicy="off" backgroundColor="#FFFFFF" backgroundAlpha="1.0">

         <mx:Script>
                 <![CDATA[
                         import mx.controls.Text;
                         import mx.events.ScrollEvent;
                         import mx.core.Application;
                         import mx.managers.PopUpManager;
                
                         import flash.text.engine.*;
                         import flashx.textLayout.container.*;
                         import flashx.textLayout.conversion.*;         
                         import flashx.textLayout.edit.*
                         import flashx.textLayout.elements.*
                         import flashx.textLayout.events.*
                         import flashx.textLayout.formats.*;
                         import flashx.undo.UndoManager;
                        
                         import flashx.textLayout.tlf_internal;
                         use namespace tlf_internal;
                        
                         /** Core declarations for the function of the TLF */   
                         private var textContainer:Sprite = null;
                         private var textFlow:TextFlow;
                         private var textController:ContainerController = null;
                         private var editManager:EditManager = new EditManager(new UndoManager());
                        
                         /** Declarations that are updated by events as the text changes */
                         private var selectionState:SelectionState = null;
                         private var currentTLF:TextLayoutFormat = null;
                        
                         /** Utility declarations */
                         private var defaultSelectionFmt:SelectionFormat = new SelectionFormat(0xffffff, 0.15);
                        
                         private static const emptyText:XML =
                         <TextFlow xmlns="http://ns.adobe.com/textLayout/2008">
                                 <linkNormalFormat><TextLayoutFormat color="0x4682B4" textDecoration="underline" /></linkNormalFormat>
                                 <linkActiveFormat><TextLayoutFormat color="0x4682B4" textDecoration="underline" /></linkActiveFormat>
                                 <linkHoverFormat><TextLayoutFormat color="0x4682B4" textDecoration="underline" /></linkHoverFormat>
                                 <p/>
                         </TextFlow>;   
                        
                         private function componentInitialize():void {
                                 // Create TLF objects
                                 textContainer = new Sprite();
                                 textContainer.y = 35;
                                 this.rawChildren.addChild(textContainer);
                                 textFlow = TextConverter.importToFlow(emptyText, TextConverter.TEXT_LAYOUT_FORMAT);
                                 textController = new ContainerController(textContainer, this.width - 25, this.height - 35);
                                 textFlow.flowComposer.addController(textController);
                                
                                 // Additional configuration for the controller
                                 // Padding settings for the view
                                 textController.paddingLeft = 8
                                 textController.paddingRight = 5;
                                 textController.paddingTop = 8;
                                 textController.paddingBottom = 8;
                                
                                 // Default font size
                                 textController.textFlow.fontSize = 12;
                                
                                 // Set up editability with undo capacity
                                 textFlow.interactionManager = editManager;
                                
                                 // Configure selection styles
                                 editManager.focusedSelectionFormat = defaultSelectionFmt;
                                 editManager.unfocusedSelectionFormat = defaultSelectionFmt;
                                 editManager.inactiveSelectionFormat = defaultSelectionFmt;
                                
                                 // Set up event handlers
                                 textFlow.addEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE, setupScrollBar);
                                 textFlow.addEventListener(SelectionEvent.SELECTION_CHANGE, selectionChangeListener, false, 0, true);
                                
                                 // Draw and update
                                 textFlow.flowComposer.updateAllControllers();
                                 textFlow.interactionManager.setFocus();        
                         }
                        
                         private function selectionChangeListener(e:SelectionEvent):void {
                                 // Copy the selection state from the event object
                                 selectionState = e.selectionState;
                                
                                 // Get the formatting for the selection or wherever the cursor has landed
                                 currentTLF = new TextLayoutFormat(textFlow.interactionManager.getCommonCharacterFormat());
                                
                                 // Update the UI about the current formatting choices where applicable
                                 //cmbFontSize.selectedItem = uiUtil_SelectFontSizeObj(currentTLF.fontSize);
                                 //clrpForeground.selectedColor = currentTLF.color;
                         }                      
                        
                         private function setupScrollBar(event:CompositionCompleteEvent):void {
                              var textHeight:int = Math.ceil(textController.getContentBounds().height);
                              var oldPos:Number = textController.verticalScrollPosition;
                        
                              if (textHeight < textController.compositionHeight) {
                                  vScroll.enabled = false;
                              } else {
                                  vScroll.enabled = true;
                                  vScroll.minScrollPosition = 0;
                                  vScroll.maxScrollPosition = textHeight - textController.compositionHeight;
                                  vScroll.lineScrollSize = 20;
                                  vScroll.pageSize = textController.compositionHeight;
                              }
                             
                              textController.verticalScrollPosition = oldPos;
                         }
                        
                         private function scrollListener(event:ScrollEvent):void {
                             textFlow.removeEventListener(ScrollEvent.SCROLL, scrollTextFlow);
                             textController.verticalScrollPosition = event.position;
                             textFlow.addEventListener(ScrollEvent.SCROLL, scrollTextFlow);
                         }
                        
                         private function scrollTextFlow(event:TextLayoutEvent):void {
                             vScroll.removeEventListener(mx.events.ScrollEvent.SCROLL, scrollListener);
                             vScroll.scrollPosition = Math.ceil(textController.verticalScrollPosition);
                             vScroll.addEventListener(TextLayoutEvent.SCROLL, scrollListener);
                         }                      
                        
                         private function btnEvent_CreateHyperlink():void {
                                 //var newLinkEditor:WndCreateHyperlink = new WndCreateHyperlink();
                                 //PopUpManager.addPopUp(newLinkEditor, DisplayObject(Application.application), true);
                                 //PopUpManager.centerPopUp(newLinkEditor);                             
                                 //newLinkEditor.btnApply.addEventListener(MouseEvent.CLICK, function():void {
                                         editManager.applyLink("http://www.fhda.edu", "_blank");
                                 //});
                         }
                        
                         private function btnEvent_RemoveHyperlink():void {
                                 trace('removing hyperlink!');
                                 editManager.applyLink(null, null, true);
                                
                                 /** For Richard D.: I shouldn't need this extra code here */
                                 //currentTLF.color = 0x000000;
                                 //currentTLF.textDecoration = TextDecoration.NONE;
                                 //editManager.applyLeafFormat(currentTLF);
                         }                      
                        
                         private function getTextScrapFromSelected():TextScrap {
                                 return TextScrap.createTextScrap(new TextRange(textFlow, textFlow.interactionManager.absoluteStart,.interactionManager.absoluteEnd + 1));
                         }
                        
                         private function uiUtil_SelectFontSizeObj(fmtFontSize:Number):Object {
                                 for each(var sizeRec:Object in cmbFontSize.dataProvider) {
                                         if(parseInt(sizeRec.value) == fmtFontSize)
                                                 return sizeRec;
                                 }
                                 return null;
                         }      
                        
                 ]]>
         </mx:Script>
        
         <mx:VScrollBar id="vScroll" right="-2" width="20" scroll="scrollListener(event)" top="35" bottom="0"/>
        
         <mx:HBox y="0" right="0" left="0" height="35" backgroundColor="#EAEAEA" horizontalAlign="left" verticalAlign="middle" paddingLeft="5"fontSize="10" color="#000000" horizontalGap="2">
                 <mx:Button labelPlacement="right" icon="@Embed(source='./icons/link_add.png')" width="24" click="btnEvent_CreateHyperlink()"/>
                 <mx:Button labelPlacement="right" icon="@Embed(source='./icons/link_delete.png')" width="24" click="btnEvent_RemoveHyperlink()"/>
         </mx:HBox>
        

</mx:Canvas>

robin_briggsCorrect answer
Adobe Employee
March 17, 2010

Looks like our example code is wrong. When you remove the link, try calling it this way:

     editManager.applyLink("", null, true);

That should work -- I tried it on your code example and it did. I think using null should work, but it doesn't, so just use the empty string instead. I will file a bug.

Thanks,

- robin