1 /** 2 * link utils 3 * @author yiminghe@gmail.com 4 */ 5 KISSY.add("editor/plugin/link/utils", function (S, Editor) { 6 7 var Node = S.Node, 8 KEStyle = Editor.Style, 9 _ke_saved_href = "_ke_saved_href", 10 link_Style = { 11 element:'a', 12 attributes:{ 13 href:"#(href)", 14 title:"#(title)", 15 // ie < 8 会把锚点地址修改,以及相对地址改为绝对地址 16 // 1. 编辑器位于 http://x.com/edit.htm 17 // 2. 用户输入 ./a.htm 18 // 3. 生成为 <a href='http://x.com/a.htm'> 19 // 另一个问题 refer: http://stackoverflow.com/questions/687552/prevent-tinymce-internet-explorer-from-converting-urls-to-links 20 _ke_saved_href:"#(_ke_saved_href)", 21 target:"#(target)" 22 } 23 }; 24 25 function getAttributes(el) { 26 var attributes = el.attributes, 27 re = {}; 28 for (var i = 0; i < attributes.length; i++) { 29 var a = attributes[i]; 30 if (a.specified) { 31 re[a.name] = a.value; 32 } 33 } 34 if (el.style.cssText) { 35 re.style = el.style.cssText; 36 } 37 return re; 38 } 39 40 41 function removeLink(editor, a) { 42 editor.execCommand("save"); 43 var sel = editor.getSelection(), 44 range = sel.getRanges()[0]; 45 if (range && range.collapsed) { 46 var bs = sel.createBookmarks(); 47 // 不使用核心 styles ,直接清除元素标记即可。 48 a._4e_remove(true); 49 sel.selectBookmarks(bs); 50 } else if (range) { 51 var attrs = getAttributes(a[0]); 52 new KEStyle(link_Style, attrs).remove(editor.get("document")[0]); 53 } 54 editor.execCommand("save"); 55 editor.notifySelectionChange(); 56 } 57 58 function applyLink(editor, attr, _selectedEl) { 59 // 注意同步,取的话要从 _ke_saved_href 取原始值的 60 attr[_ke_saved_href] = attr.href; 61 // 是修改行为 62 if (_selectedEl) { 63 editor.execCommand("save"); 64 _selectedEl.attr(attr); 65 } else { 66 var sel = editor.getSelection(), 67 range = sel && sel.getRanges()[0]; 68 //编辑器没有焦点或没有选择区域时直接插入链接地址 69 if (!range || range.collapsed) { 70 var a = new Node("<a>" + attr.href + "</a>", 71 attr, editor.get("document")[0]); 72 editor.insertElement(a); 73 } else { 74 editor.execCommand("save"); 75 var linkStyle = new KEStyle(link_Style, attr); 76 linkStyle.apply(editor.get("document")[0]); 77 } 78 } 79 editor.execCommand("save"); 80 editor.notifySelectionChange(); 81 } 82 83 84 return { 85 removeLink:removeLink, 86 applyLink:applyLink, 87 _ke_saved_href:_ke_saved_href 88 } 89 }, { 90 requires:['editor'] 91 });