Skip to content

Commit

Permalink
🐛 修复 ClipboardJS 重复加载
Browse files Browse the repository at this point in the history
  • Loading branch information
zkqiang committed Jun 14, 2022
1 parent 9ffbbf1 commit d8b6f51
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 68 deletions.
72 changes: 4 additions & 68 deletions layout/_partials/plugins/code-widget.ejs
Original file line number Diff line number Diff line change
@@ -1,71 +1,7 @@
<%
import_script(`<script>
(function() {
var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
var enableCopy = CONFIG.copy_btn;
if (!enableLang && !enableCopy) {
return;
}
function getBgClass(ele) {
return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark';
}
var copyTmpl = '';
copyTmpl += '<div class="code-widget">';
copyTmpl += 'LANG';
copyTmpl += '</div>';
jQuery('.markdown-body pre').each(function() {
var $pre = jQuery(this);
if ($pre.find('code.mermaid').length > 0) {
return;
}
if ($pre.find('span.line').length > 0) {
return;
}
var lang = '';
if (enableLang) {
lang = CONFIG.code_language.default;
if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
lang = $pre[0].children[0].classList[1];
} else if ($pre[0].getAttribute('data-language')) {
lang = $pre[0].getAttribute('data-language');
} else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
lang = $pre[0].children[0].classList[1];
$pre.parent().addClass('code-wrapper');
} else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
$pre.wrap('<div class="code-wrapper"></div>');
}
lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default);
}
$pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));
if (enableCopy) {
Fluid.utils.createScript('${url_join(theme.static_prefix.clipboard, 'clipboard.min.js')}', function() {
var clipboard = new window.ClipboardJS('.copy-btn', {
target: function(trigger) {
var nodes = trigger.parentNode.childNodes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].tagName === 'CODE') {
return nodes[i];
}
}
}
});
clipboard.on('success', function(e) {
e.clearSelection();
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
setTimeout(function() {
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
}, 2000);
});
});
}
});
})();
</script>
if (theme.code.copy_btn) {
import_script(`<script src=${url_join(theme.static_prefix.clipboard, 'clipboard.min.js')}></script>`)
}
import_script(`<script>Fluid.plugins.codeWidget();</script>
`)
%>
65 changes: 65 additions & 0 deletions source/js/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,70 @@ Fluid.plugins = {
}
}
});
},

codeWidget() {
var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
var enableCopy = CONFIG.copy_btn && 'ClipboardJS' in window;
if (!enableLang && !enableCopy) {
return;
}

function getBgClass(ele) {
return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark';
}

var copyTmpl = '';
copyTmpl += '<div class="code-widget">';
copyTmpl += 'LANG';
copyTmpl += '</div>';
jQuery('.markdown-body pre').each(function() {
var $pre = jQuery(this);
if ($pre.find('code.mermaid').length > 0) {
return;
}
if ($pre.find('span.line').length > 0) {
return;
}

var lang = '';

if (enableLang) {
lang = CONFIG.code_language.default;
if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
lang = $pre[0].children[0].classList[1];
} else if ($pre[0].getAttribute('data-language')) {
lang = $pre[0].getAttribute('data-language');
} else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
lang = $pre[0].children[0].classList[1];
$pre.parent().addClass('code-wrapper');
} else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
$pre.wrap('<div class="code-wrapper"></div>');
}
lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default);
}
$pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));

if (enableCopy) {
var clipboard = new ClipboardJS('.copy-btn', {
target: function(trigger) {
var nodes = trigger.parentNode.childNodes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].tagName === 'CODE') {
return nodes[i];
}
}
}
});
clipboard.on('success', function(e) {
e.clearSelection();
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
setTimeout(function() {
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
}, 2000);
});
}
});
}
};

0 comments on commit d8b6f51

Please sign in to comment.