123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /**
- * 超链接
- * @file
- * @since 1.2.6.1
- */
- /**
- * 插入超链接
- * @command link
- * @method execCommand
- * @param { String } cmd 命令字符串
- * @param { Object } options 设置自定义属性,例如:url、title、target
- * @example
- * ```javascript
- * editor.execCommand( 'link', '{
- * url:'ueditor.baidu.com',
- * title:'ueditor',
- * target:'_blank'
- * }' );
- * ```
- */
- /**
- * 返回当前选中的第一个超链接节点
- * @command link
- * @method queryCommandValue
- * @param { String } cmd 命令字符串
- * @return { Element } 超链接节点
- * @example
- * ```javascript
- * editor.queryCommandValue( 'link' );
- * ```
- */
- /**
- * 取消超链接
- * @command unlink
- * @method execCommand
- * @param { String } cmd 命令字符串
- * @example
- * ```javascript
- * editor.execCommand( 'unlink');
- * ```
- */
- UE.plugins["link"] = function() {
- function optimize(range) {
- var start = range.startContainer,
- end = range.endContainer;
- if ((start = domUtils.findParentByTagName(start, "a", true))) {
- range.setStartBefore(start);
- }
- if ((end = domUtils.findParentByTagName(end, "a", true))) {
- range.setEndAfter(end);
- }
- }
- UE.commands["unlink"] = {
- execCommand: function() {
- var range = this.selection.getRange(),
- bookmark;
- if (
- range.collapsed &&
- !domUtils.findParentByTagName(range.startContainer, "a", true)
- ) {
- return;
- }
- bookmark = range.createBookmark();
- optimize(range);
- range.removeInlineStyle("a").moveToBookmark(bookmark).select();
- },
- queryCommandState: function() {
- return !this.highlight && this.queryCommandValue("link") ? 0 : -1;
- }
- };
- function doLink(range, opt, me) {
- var rngClone = range.cloneRange(),
- link = me.queryCommandValue("link");
- optimize((range = range.adjustmentBoundary()));
- var start = range.startContainer;
- if (start.nodeType == 1 && link) {
- start = start.childNodes[range.startOffset];
- if (
- start &&
- start.nodeType == 1 &&
- start.tagName == "A" &&
- /^(?:https?|ftp|file)\s*:\s*\/\//.test(
- start[browser.ie ? "innerText" : "textContent"]
- )
- ) {
- start[browser.ie ? "innerText" : "textContent"] = utils.html(
- opt.textValue || opt.href
- );
- }
- }
- if (!rngClone.collapsed || link) {
- range.removeInlineStyle("a");
- rngClone = range.cloneRange();
- }
- if (rngClone.collapsed) {
- var a = range.document.createElement("a"),
- text = "";
- if (opt.textValue) {
- text = utils.html(opt.textValue);
- delete opt.textValue;
- } else {
- text = utils.html(opt.href);
- }
- domUtils.setAttributes(a, opt);
- start = domUtils.findParentByTagName(rngClone.startContainer, "a", true);
- if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
- range.setStartAfter(start).collapse(true);
- }
- a[browser.ie ? "innerText" : "textContent"] = text;
- range.insertNode(a).selectNode(a);
- } else {
- range.applyInlineStyle("a", opt);
- }
- }
- UE.commands["link"] = {
- execCommand: function(cmdName, opt) {
- var range;
- opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g));
- opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g));
- opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g));
- doLink((range = this.selection.getRange()), opt, this);
- //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
- range.collapse().select(true);
- },
- queryCommandValue: function() {
- var range = this.selection.getRange(),
- node;
- if (range.collapsed) {
- // node = this.selection.getStart();
- //在ie下getstart()取值偏上了
- node = range.startContainer;
- node = node.nodeType == 1 ? node : node.parentNode;
- if (
- node &&
- (node = domUtils.findParentByTagName(node, "a", true)) &&
- !domUtils.isInNodeEndBoundary(range, node)
- ) {
- return node;
- }
- } else {
- //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
- range.shrinkBoundary();
- var start = range.startContainer.nodeType == 3 ||
- !range.startContainer.childNodes[range.startOffset]
- ? range.startContainer
- : range.startContainer.childNodes[range.startOffset],
- end = range.endContainer.nodeType == 3 || range.endOffset == 0
- ? range.endContainer
- : range.endContainer.childNodes[range.endOffset - 1],
- common = range.getCommonAncestor();
- node = domUtils.findParentByTagName(common, "a", true);
- if (!node && common.nodeType == 1) {
- var as = common.getElementsByTagName("a"),
- ps,
- pe;
- for (var i = 0, ci; (ci = as[i++]); ) {
- (ps = domUtils.getPosition(ci, start)), (pe = domUtils.getPosition(
- ci,
- end
- ));
- if (
- (ps & domUtils.POSITION_FOLLOWING ||
- ps & domUtils.POSITION_CONTAINS) &&
- (pe & domUtils.POSITION_PRECEDING ||
- pe & domUtils.POSITION_CONTAINS)
- ) {
- node = ci;
- break;
- }
- }
- }
- return node;
- }
- },
- queryCommandState: function() {
- //判断如果是视频的话连接不可用
- //fix 853
- var img = this.selection.getRange().getClosedNode(),
- flag =
- img &&
- (img.className == "edui-faked-video" ||
- img.className.indexOf("edui-upload-video") != -1);
- return flag ? -1 : 0;
- }
- };
- };
|