(fileType, source, callback)
| 228 | * 代码美化 |
| 229 | */ |
| 230 | let format = (fileType, source, callback) => { |
| 231 | |
| 232 | let beauty = txtResult => { |
| 233 | let code = document.getElementsByTagName('pre')[0]; |
| 234 | formattedCodes = txtResult; |
| 235 | document.querySelector('html').classList.add('jf-cb'); |
| 236 | |
| 237 | let highlightedHtml; |
| 238 | try { |
| 239 | highlightedHtml = fileType === 'javascript' ? highlightJS(txtResult) : highlightCSS(txtResult); |
| 240 | } catch (e) { |
| 241 | highlightedHtml = esc(txtResult); |
| 242 | } |
| 243 | |
| 244 | let lines = highlightedHtml.split('\n'); |
| 245 | code.innerHTML = '<ol>' + lines.map(l => '<li><span>' + l + '</span></li>').join('') + '</ol>'; |
| 246 | callback && callback('ok'); |
| 247 | }; |
| 248 | |
| 249 | try { |
| 250 | if (fileType === 'javascript') { |
| 251 | if (typeof window.js_beautify !== 'function') { |
| 252 | callback && callback('error', 'js_beautify 未加载,请刷新页面重试'); |
| 253 | return; |
| 254 | } |
| 255 | let opts = { |
| 256 | brace_style: "collapse", |
| 257 | break_chained_methods: false, |
| 258 | indent_char: " ", |
| 259 | indent_scripts: "keep", |
| 260 | indent_size: "4", |
| 261 | keep_array_indentation: true, |
| 262 | preserve_newlines: true, |
| 263 | space_after_anon_function: true, |
| 264 | space_before_conditional: true, |
| 265 | unescape_strings: false, |
| 266 | wrap_line_length: "120" |
| 267 | }; |
| 268 | beauty(window.js_beautify(source, opts)); |
| 269 | } else if (fileType === 'css') { |
| 270 | beauty(cssBeautify(source)); |
| 271 | } |
| 272 | } catch (e) { |
| 273 | console.error('[FeHelper] code-beautify error:', e); |
| 274 | callback && callback('error', e.message || String(e)); |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | /** |
| 279 | * 检测 |
no test coverage detected