---
title: "LinkElement的鼠标事件"
author: "diding"
date: "2009-04-07T20:20:23Z"
tags: []
slug: "linkelement"
draft: false
lastmod: "2009-04-07T20:21:14Z"
id: 362988
url: "https://diding.iteye.com/blog/362988"
---

虽然TextFlow的LinkElement支持MouseEvent,但官方手法,很遗憾要打浏览器窗口,还要用户加上CTRL键,如果是前台浏览到无所谓,但如果作为编辑器使用,真得有一种说不出的感觉.

```java

linkElement.addEventListener(MouseEvent.CLICK,LinkElementCtrlClickHandler,false, 0, false);

private function LinkElementCtrlClickHandler(event:FlowElementMouseEvent):void{
	var linkElement:LinkElement=event.flowElement as LinkElement;
        //编辑和删除等
	doOpenURLWinHandler(linkElement);
}

```


经过昨天的研究,终于找到一种更为好的方法来实行作为编辑器的手法实现:
要实现此功能必须解决2个问题:

1.用户在编辑时加入超链接后,像插入图片一样,要获得当前的链接接对象LinkElement,前加上事件(官方没有直接给出,只有自已来摸索实现)
2.如果用户不断的加入超链接,如何得到其实体对象(本想用EventDispatcher,如是用,又走上了老路,于是,又得遍历当前TextLine,得textLine只有两个子对象,shape,不能象ILG那样来根据name对比获取,最后,想到的办法就是根据

```java

var absIndex:int=LE.getAbsoluteStart();
var between:int=absIndex+LE.textLength;

```

来对比实现)
核心代码:

```java

/***********************************************************
		 ************************ 设置链接模块********************
		 * *********************************************************/
		 //打开链接窗口
		public function doOpenURLWinHandler(linkElement:LinkElement=null):void
		{
			if (linkElement){
				var window:URLWindow=URLWindow(PopUpManager.createPopUp(toolBar, URLWindow, true));
				window.getWinFindArg(this,linkElement,linkElement.href, linkElement.target, true)
			}else if (selStart != selFinish){
				var window2:URLWindow=URLWindow(PopUpManager.createPopUp(toolBar, URLWindow, true));
				window2.getWinFindArg(this)
			}else{
				Alert.show("请选择文字", "提示信息")
			}
		}
		//设置或编辑链接
		private var watchIndex:int;
		public function setOrEditURL(currentLinkElement:LinkElement=null,urlStr:String=null, click_target:String=null):void
		{
			var em:EditManager=EditManager(_textFlow.interactionManager);
			if(currentLinkElement!=null){
				//编辑
				var beginIndex:int=currentLinkElement.getAbsoluteStart();
				watchIndex=beginIndex
				var lastIndex:int=beginIndex+currentLinkElement.textLength;
				em.setSelection(beginIndex,lastIndex)
			}else{
				//添加
				em.setSelection(selStart,selFinish);
				watchIndex=selStart;	
			}
			em.applyLink(urlStr, click_target, false, null);
			_textFlow.interactionManager.setFocus();
			_textFlow.flowComposer.updateAllContainers();
			_textFlow.addEventListener(UpdateCompletionEvent.UPDATE_COMPLETE,createLEHandler)
		}
		
		private function createLEHandler(event:UpdateCompletionEvent):void{
			var spanElement:SpanElement=_textFlow.findLeaf(watchIndex) as SpanElement;
			//trace(spanElement.parent);
			var linkElement:LinkElement=spanElement.parent as LinkElement;
			if(linkElement){
			   var LEdispatcher:EventDispatcher=linkElement.getEventMirror();
			   LEdispatcher.addEventListener(MouseEvent.CLICK, LinkElementHandler,false, 0, false);
			    //官方手法,很遗憾要打浏览器窗口,还要用户加上CTRL键
			    //linkElement.addEventListener(MouseEvent.CLICK,LinkElementCtrlClickHandler,false, 0, false);
			}
		}
		
		private function LinkElementCtrlClickHandler(event:FlowElementMouseEvent):void{
			var linkElement:LinkElement=event.flowElement as LinkElement;
			doOpenURLWinHandler(linkElement);
		}
		
		private function LinkElementHandler(event:MouseEvent):void
		{
			var textLine:TextLine=event.currentTarget as TextLine;
			var textFlowLine:TextFlowLine=textLine.userData as TextFlowLine;
			var paraElement:ParagraphElement=textFlowLine.paragraph as ParagraphElement;
			

			trace("numChildren=="+paraElement.numChildren)
			
			for(var k:int=0;k<paraElement.numChildren;k++){
				var childElement:*=paraElement.getChildAtIndex(k);
				if(childElement is LinkElement){
					var currentLE:LinkElement=childElement as LinkElement;
					//判断参考
					//由于鼠标点击时,selStart==selFinish,所以只需要判断selStart相当于caret
					//是否位于linkElement文字块中,
					//调度此事件
					var absIndex:int=currentLE.getAbsoluteStart();
					var between:int=absIndex+currentLE.textLength;
					if(selStart<between){
						trace(currentLE.href)
					    trace(currentLE.target)
						break;
					}
					
				}
			}
			
		}
		//删除链接
		public function delURL(currentLinkElement:LinkElement):void
		{
			//获取linkElement开始和结束位置
			var beginIndex:int=currentLinkElement.getAbsoluteStart();
			watchIndex=beginIndex;
			var lastIndex:int=beginIndex+currentLinkElement.textLength;
			var em:IEditManager=IEditManager(_textFlow.interactionManager);
			em.setSelection(beginIndex,lastIndex)
			em.applyLink("", null, false, null);
			_textFlow.interactionManager.setFocus();
			
		}


```

附上运行测试的图片:
![](/upload/attachment/91868/d346915e-28a1-3a40-a1c8-7a79ba8fe273.png)