101 lines
3.4 KiB
JavaScript
101 lines
3.4 KiB
JavaScript
var preNum = 0;
|
||
var preStyle = 'pre[class*="brush:"],pre[class*="prism"],pre>code';
|
||
var copyimg = bloghost + "zb_users/plugin/zxd_ctrlcenter/plugin/code/svg/copy.svg";
|
||
var goimg = bloghost + "zb_users/plugin/zxd_ctrlcenter/plugin/code/svg/yunxing.svg";
|
||
var bjimg = bloghost + "zb_users/plugin/zxd_ctrlcenter/plugin/code/svg/bianji.svg";
|
||
var bjgoimg = bloghost + "zb_users/plugin/zxd_ctrlcenter/plugin/code/svg/bianjigo.svg";
|
||
$(preStyle).each(function() {
|
||
yid = "pre_" + preNum;
|
||
precode = $(this).html();
|
||
xyattr = $(this).attr("class");
|
||
if ($("pre>code").length) {
|
||
$cdom = $(this).parent()
|
||
} else {
|
||
$cdom = $(this)
|
||
}
|
||
$cdom.attr("id", yid);
|
||
$cdom.after('<textarea id="t' + yid + '" class="cdm_code">' + precode + "</textarea>");
|
||
preNum++
|
||
});
|
||
$(document).ready(function() {
|
||
var a = 0;
|
||
$("pre").each(function() {
|
||
var d = "pre_" + a;
|
||
var c = $(this).prev();
|
||
var b = '<img class="z_codeplus z_go" data-clipboard-target="' + d + '" src="' + copyimg +
|
||
'" style="cursor: pointer;vertical-align: middle;" title="点击复制代码"><img class="z_go" onclick="runCode(\'' + d +
|
||
'\')" src="' + goimg +
|
||
'" style="cursor: pointer;vertical-align: middle;" title="运行代码"><img class="z_go editbtn" src="' + bjimg +
|
||
'" style="cursor: pointer;vertical-align: middle;" title="编辑模式">';
|
||
if (c.hasClass("prism-show-language")) {
|
||
c.find("div").prepend(b)
|
||
} else {
|
||
$(this).before(
|
||
'<div class="prism-show-language"><div class="prism-show-language-label" data-language="default">' + b +
|
||
"</div>")
|
||
}
|
||
a++
|
||
})
|
||
});
|
||
$('textarea[id*="tpre"]').blur(function() {
|
||
$(this).text($(this).val())
|
||
});
|
||
|
||
function runCode(a) {
|
||
var b = window.open("", "", "");
|
||
b.document.open("text/html", "replace");
|
||
b.opener = null;
|
||
b.document.write($("#" + a).next("textarea").html().replace(/</g, "<").replace(/>/g, ">").replace(/ /g, " ")
|
||
.replace(/"/g, '"').replace(/&/g, "&"));
|
||
b.document.close()
|
||
}
|
||
document.writeln('<script src="' + bloghost + 'zb_users/plugin/zxd_ctrlcenter/plugin/code/clipboard.min.js" type="text/javascript"><\/script>');
|
||
$(function() {
|
||
$(".editbtn").each(function() {
|
||
var b = $(this).parent().parent().next().children("code").height();
|
||
$(this).click(function() {
|
||
$(this).parent().parent().next().toggleClass("zcodehide");
|
||
$(this).parent().parent().next().next().toggleClass("zcodeshow").css({
|
||
height: b + 80 + "px",
|
||
});
|
||
if ("编辑模式" == $(this)[0].title) {
|
||
$(this)[0].title = "恢复默认";
|
||
$(this)[0].src = bjgoimg
|
||
} else {
|
||
$(this)[0].title = "编辑模式";
|
||
$(this)[0].src = bjimg
|
||
}
|
||
})
|
||
});
|
||
$("pre[id^='pre_']").each(function() {
|
||
$(this).html($(this).html()/*' + <span style="position:absolute;left:-9999px;">\r\n\r\n代码来自:' + bloghost +
|
||
"</span>"*/)
|
||
});
|
||
$(".z_codeplus").click(function() {
|
||
if ($(this).parent().parent().next().next("textarea").is(":visible")) {
|
||
cdMode = 1
|
||
} else {
|
||
cdMode = 0
|
||
}
|
||
if (cdMode == 1) {
|
||
$(this).parent().parent().next().next("textarea").val($(this).parent().parent().next().next("textarea").val() /* + "\r\n\r代码来自:" + bloghost */ )
|
||
}
|
||
});
|
||
var a = new ClipboardJS(".z_codeplus", {
|
||
target: function(b) {
|
||
if (cdMode == 1) {
|
||
return b.parentNode.parentNode.nextSibling.nextSibling
|
||
} else {
|
||
return b.parentNode.parentNode.nextSibling
|
||
}
|
||
}
|
||
});
|
||
a.on("success", function(b) {
|
||
alert("复制成功");
|
||
b.clearSelection()
|
||
});
|
||
a.on("error", function(b) {
|
||
alert("复制失败")
|
||
})
|
||
});
|