MCPcopy Create free account
hub / github.com/TruthHun/BookStack / imgLink

Function imgLink

static/wangEditor/js/wangEditor.js:7633–7691  ·  view source on GitHub ↗
(e, url)

Source from the content-addressed store, hash-verified

7631
7632 // 获取 / 设置 链接
7633 function imgLink(e, url) {
7634 if (!$currentImg) {
7635 return;
7636 }
7637 var commandFn;
7638 var callback = function () {
7639 // 及时保存currentLink
7640 if (url != null) {
7641 currentLink = url;
7642 }
7643 if (html !== $txt.html()) {
7644 $txt.change();
7645 }
7646 };
7647 var $link;
7648 var inLink = false;
7649 var $parent = $currentImg.parent();
7650 if ($parent.get(0).nodeName.toLowerCase() === 'a') {
7651 // 父元素就是图片链接
7652 $link = $parent;
7653 inLink = true;
7654 } else {
7655 // 父元素不是图片链接,则重新创建一个链接
7656 $link = $('<a target="_blank"></a>');
7657 }
7658
7659 if (url == null) {
7660 // url 无值,是获取链接
7661 return $link.attr('href') || '';
7662 } else if (url === '') {
7663 // url 是空字符串,是取消链接
7664 if (inLink) {
7665 commandFn = function () {
7666 $currentImg.unwrap();
7667 };
7668 }
7669 } else {
7670 // url 有值,是设置链接
7671 if (url === currentLink) {
7672 return;
7673 }
7674 commandFn = function () {
7675 $link.attr('href', url);
7676
7677 if (!inLink) {
7678 // 当前图片未包含在链接中,则包含进来
7679 $currentImg.wrap($link);
7680 }
7681 };
7682 }
7683
7684 // 执行命令
7685 if (commandFn) {
7686 // 记录下执行命令之前的html内容
7687 html = $txt.html();
7688 // 执行命令
7689 editor.customCommand(e, commandFn, callback);
7690 }

Callers 1

bindToolbarEventFunction · 0.85

Calls 2

$Function · 0.50
getMethod · 0.45

Tested by

no test coverage detected