<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CopyAsHtmlFromVScode</title>
<style>
</style>
</head>
<body>
<div id="codePart">
<div id="codeBox" class="codeBox" style='width: 100%; height: auto; line-height: 34px; position: relative; font-family: "Meslo LG L DZ", "monospace";'>
<div id="banner" class="banner" style="width: 100%; height: 34px; position: absolute; top: 0; left: 0; background: rgba(0, 0, 0, .1); border-top-left-radius: 8px; border-bottom-left-radius: 8px; box-sizing: border-box; border-bottom: 1px solid rgba(0, 0, 0, .15)">
<div id="bannerStamp" style="height: 34px; width: 34px; float: left; display: flex; align-items: center; justify-content: center">
<span style="display: block; width: 60%; height: 60%; border-radius: 50%; background: green"> </span>
</div>
<div style="height: 34px; float: left; font-size: 16px; line-height: 34px; display: flex; align-items: center;">
<span style="display: block; font-family: sans-serif" id="bannerTitle">123.html</span>
</div>
<div style="height: 34px; float: right; font-size: 16px; line-height: 34px; display: flex; align-items: center; justify-content: center;">
<span style="display: block; background: rgba(255, 255, 255, .65); font-family: sans-serif; padding: 0 5px; margin-right: 10px;" data-clipboard-target="">COPY</span>
<span class="expandBtn" style="display: block; background: rgba(255, 0, 0, .65); color: #f5f5f5; font-family: sans-serif; padding: 0 5px; margin-right: 10px;">EXPAND</span>
</div>
</div>
<div class="side" id="vsSide" style="width: 50px; background: none; float: left; padding: 40px 0 45px; background: #f5f5f5; border-top-left-radius: 8px; border-bottom-left-radius: 8px;">
</div>
<div id="containerOuter" style="width: calc(100% - 70px); float: left; white-space: nowrap; background: #f5f5f5; overflow: auto; padding: 40px 0 45px 20px; border-bottom-right-radius: 8px; border-top-right-radius: 8px;">
<div id="_containerBox">
</div>
</div>
<div class="banner" style="width: 100%; height: 34px; position: absolute; bottom: 55px; left: 0; box-sizing: border-box; border-bottom: 1px solid rgba(0, 0, 0, .15)">
<div style="height: 34px; float: right; font-size: 16px; line-height: 34px; display: flex; align-items: center; justify-content: center;">
<span style="display: block; background: rgba(255, 255, 255, .65); font-family: sans-serif; padding: 0 5px; margin-right: 10px;" data-clipboard-target="">COPY</span>
<span class="expandBtn" style="display: block; background: rgba(255, 0, 0, .65); color: #f5f5f5; font-family: sans-serif; padding: 0 5px; margin-right: 10px;">EXPAND</span>
</div>
</div>
<div style="clear: both;">
</div>
</div>
</div>
</body>
</html>
<script src="./clipBoard.js"></script>
<script>
var themes = { // 主题,自己定义了两种,可以自己加~
"dark": {
c_title_bg: "#252525", // 标题栏背景色
c_title_fg: "#ededed", // 前景色(文字颜色)
c_editor_bg: "#1e1e1e", // 代码区背景
c_sideNum_bg: "#1e1e1e", // 行数栏背景
c_sideNum_fg: "#727272" // 前景色
},
"light": {
c_title_bg: "#dcdcdc",
c_title_fg: "#999",
c_editor_bg: "#f5f5f5",
c_sideNum_bg: "#e5e5e5",
c_sideNum_fg: "#999"
}
};
var config = { // 调整基本样式
"theme": "dark", // 主题名
"fontFamily": '"Meslo LG L DZ", "monospace"',
"lineHeight": "25px",
"fontSize": "15px",
"fileName": "advancedHighLight_ver_2_0.html", // 标题名称
"stampColor": "", // 留空会根据文件拓展名设置圆点的颜色
"maxHeight": 400, // 限制高度,px
"highLight": [true, "#569cd6"]
};
/* 兼容 IE, getComputedStyle */
if (!window.getComputedStyle) {
window.getComputedStyle = function(el, pseudo) {
this.el = el;
this.getPropertyValue = function(prop) {
var re = /(\-([a-z]){1})/g;
if (prop == 'float') prop = 'styleFloat';
if (re.test(prop)) {
prop = prop.replace(re, function () {
return arguments[2].toUpperCase();
});
}
return el.currentStyle[prop] ? el.currentStyle[prop] : null;
}
return this;
}
};
document.onpaste = function(e){
format(); // 初始化代码外框模板
var cb_str = e.clipboardData.getData('text\/html') || e.clipboardData.getData('text\/plain'); // 获取从 VSCode 里面复制的含 html 标签的代码
console.log(cb_str)
var tmpDiv = document.createElement("div");
tmpDiv.innerHTML = cb_str;
var containerBox = tmpDiv.children[0];
var arrLineDiv = containerBox.children;
var HollowArr = [];
for(var i=0; i<arrLineDiv.length; i++){
var arrLineSpanChar = arrLineDiv[i].children;
var tmp_arrLine = [];
for(var j=0; j<arrLineSpanChar.length; j++){
var arrEmpty = [];
arrEmpty[0] = arrLineSpanChar[j].style.color;
// 空格替换为 HTML 转义符
arrEmpty[1] = arrLineSpanChar[j].innerHTML.replace(/ /g, " ");
tmp_arrLine.push(arrEmpty);
}
HollowArr.push(tmp_arrLine);
}
var _containerBox = document.getElementById("_containerBox");
var stamp = new Date().getTime();
_containerBox.id = "d" + stamp;
var clipBoard_tars = document.querySelectorAll("[data-clipboard-target]");
for(var c=0; c<clipBoard_tars.length; c++) clipBoard_tars[c].dataset.clipboardTarget = "#d" + stamp;
var _sideNums = document.getElementById("vsSide");
for(var l=0; l<HollowArr.length; l++){
_containerBox.append(returnLine(HollowArr[l]));
var tmpSpan = returnSpan("", l+1);
tmpSpan.style = "display: block; width: 40px; text-align: right; padding-right: 10px; float: left; font-size: 14px; height: " + config.lineHeight;
_sideNums.append(tmpSpan);
}
var g_Span = _sideNums.querySelectorAll("span");
if(_sideNums.innerHTML) g_Span[g_Span.length - 1].style.borderBottomRightRadius = "4px";
var cb = document.getElementById("codeBox");
var codeHeight = parseFloat(window.getComputedStyle(codeBox, "").getPropertyValue("height"));
console.log(codeHeight)
var expandBtn = document.querySelector(".expandBtn");
var tog = {
"expand": function(){
document.getElementById("banner").style.borderTopRightRadius = "8px";
// cb.style.maxHeight = codeHeight + "px";
cb.style.overflow = "auto";
return "SHRINK";
},
"shrink": function(){
document.getElementById("banner").style.borderTopRightRadius = 0;
// cb.style.maxHeight = config.maxHeight + "px";
cb.style.overflow = "auto";
return "EXPAND";
}
};
if(codeHeight > config.maxHeight){
expandBtn.style.display = "block";
tog["shrink"]();
expandBtn.onclick = function(){
var lbl = this.innerHTML;
this.innerHTML = (lbl === "EXPAND" ? tog["expand"]() : tog["shrink"]());
}
}else{
expandBtn.style.display = "none";
}
// 最终结果在看控制台处输出查看
console.log(document.getElementById("codePart").innerHTML);
function returnLine(array){
var oDiv = document.createElement("div");
var bFill = config.highLight[0];
oDiv.style = "height: " + config.lineHeight + "; line-height: " + config.lineHeight + "; font-size: " + config.fontSize + ";";
if(!bFill) oDiv.style.color = config.highLight[1];
for(var k=0; k<array.length; k++){
tspan = returnSpan(array[k][0], array[k][1], bFill);
tspan.style.lineHeight = "100%";
oDiv.append(tspan);
}
var groupSpan = oDiv.querySelectorAll("span");
if(oDiv.innerHTML) groupSpan[groupSpan.length - 1].style.paddingRight = "20px";
return oDiv;
}
function returnSpan(cr, ct, fill = true){
var oSpan = document.createElement("span");
oSpan.innerHTML = ct;
if(!cr) return oSpan;
if(fill) oSpan.style.color = cr;
return oSpan;
}
function format(){
var stampColors = { // 左上角圆点颜色主题集合
"html": "#d13239",
"css": "#4286f4",
"js": "#fbb507",
"txt": "#373a41",
"plist": "#8bc34a"
}
var oCodeBox = document.getElementById("codeBox");
var oBanner = document.getElementById("banner");
var bannerStamp = document.getElementById("bannerStamp");
var bannerTitle = document.getElementById("bannerTitle");
var oVsSide = document.getElementById("vsSide");
var oOuter = document.getElementById("containerOuter");
var theStampColor = config.stampColor;
var configTheme = config.theme;
if(config.maxHeight){
oCodeBox.dataset.maxHeight = config.maxHeight;
};
if(!config.stampColor) theStampColor = stampColors[config.fileName.split(".").pop()];
oCodeBox.style.fontFamily = config.fontFamily;
oCodeBox.style.lineHeight = config.lineHeight;
bannerTitle.innerText = config.fileName;
bannerStamp.querySelector("span").style.backgroundColor = theStampColor;
oBanner.style.backgroundColor = themes[configTheme].c_title_bg;
bannerTitle.style.color = themes[configTheme].c_title_fg;
oOuter.style.backgroundColor = themes[configTheme].c_editor_bg;
oVsSide.style.backgroundColor = themes[configTheme].c_sideNum_bg;
oVsSide.style.color = themes[configTheme].c_sideNum_fg;
}
}
new ClipboardJS('[data-clipboard-target]');
</script>