traceback.js
Version:
代码回溯。指定文本的位置,格式化显示选定区域
2 lines (1 loc) • 3.31 kB
JavaScript
var TracebackJS=function(){"use strict";const e=/^-(\d+)\+(\d+)$/,t={upward:5,downward:5};function n(n){let a={};if(-1===n)a={upward:-1,downward:-1};else if("object"==typeof n)a=n;else if("string"==typeof n&&e.test(n)){const[,t,c]=n.match(e);a={upward:+t,downward:+c}}return Object.assign({},t,a)}function a(e,t){const{separator:n,highlightRow:a,displayRows:c,start:r}=t,{upward:o,downward:i}=c,s=[],d=function(e,t){return e.trim().split(t)}(e,n);return a-r>d.length?(console.warn("traceback.js渲染异常:高亮行数超出源文本行数"),[]):(d.forEach((e,t)=>{const n=t+r;if(-1!==o&&n<a-o)return;if(-1!==i&&n>a+i)return;const c=function(e,t,n){const a={lineno:e,content:t};n&&(a.highlighted=!0);return a}(n,e,t===a-r);s.push(c)}),s)}const c="traceback-js_container",r="traceback-js_list",o="traceback-js_item",i="traceback-js_item highlight_row",s="clicked_row",d="traceback-js_index",l="traceback-js_content";function u(e){if(0===e.length)return null;const t=document.createElement("div"),n=document.createElement("table"),a=document.createElement("tbody");if(e.forEach(e=>{const t=document.createElement("tr"),n=document.createElement("td"),c=document.createElement("td");let r=o;e.highlighted&&(r=i),n.dataset.lineno=e.lineno,c.textContent=e.content,n.className=d,c.className=l,t.className=r,t.appendChild(n),t.appendChild(c),a.appendChild(t)}),n.className=r,t.className=c,n.appendChild(a),t.appendChild(n),!document.getElementById("traceback-jsid_style")){const e=document.createElement("style");e.id="traceback-jsid_style",e.textContent=".traceback-js_container {width: 100%;margin: 0;padding: 8px 0;border: 1px solid #eee;border-radius: 2px;overflow-x: auto;}.traceback-js_list {min-width: 100%;border-spacing: 0;color: #4b4b4b;font-size: 14px;font-family: Consolas, Monaco, Liberation Mono, Menlo;white-space: pre;line-height: 24px;tab-size: 4;}.traceback-js_item {}.traceback-js_item.clicked_row {background-color: rgba(0, 128, 128, .1);}.traceback-js_item.highlight_row,.traceback-js_item.highlight_row .traceback-js_index::before {background-color: #008080;color: #fff;}.traceback-js_index {padding: 0;width: 36px;min-width: 36px;color: #999;text-align: right;cursor: pointer;}.traceback-js_index::before {content: attr(data-lineno);}.traceback-js_index:hover {color: #666;-webkit-user-select: none;user-select: none;}.traceback-js_content {padding: 0 16px;}",document.head.appendChild(e)}return t}const p={start:1,separator:"\n",highlightRow:1,displayRows:"-5+5"};function b(e,t){return u(a(e,Object.assign(Object.assign(Object.assign({},p),t),{displayRows:n(t.displayRows||p.displayRows)})))}return{version:"0.3.0",init:function(e,t=p){var n;const a=document.querySelector(e);if(!a)return void console.warn(`查询${e}失败,请确保页面上存在此元素`);const c=b((null===(n=a.textContent)||void 0===n?void 0:n.trim())||"",t);if(!c)return;a.textContent=null,a.appendChild(c);let r=null;a.addEventListener("click",e=>{const{className:t,parentNode:n}=e.target;t===d&&n!==r&&(null==r||r.classList.remove(s),n.classList.add(s),r=n)})},render:b,renderToString:function(e,t=p){const c=u(a(e,Object.assign(Object.assign(Object.assign({},p),t),{displayRows:n(t.displayRows||p.displayRows)})));if(!c)return"";const r=document.createElement("div");return r.appendChild(c),r.innerHTML}}}();