内容区域
/** * FeHelper 代码美化工具 */ new Vue({ el: '#pageContainer', data: { selectedType: 'Javascript', sourceContent: '', resultContent: '', showCopyBtn: false, examples: { js: `function foo(){var x=10;if(x>5){return x*2;}else{return x/2;}}`, css: `.header{position:fixed;top:0;left:0;width:100%;background:#fff;z-index:100;}.header .logo{float:left;margin:10px;}.header .nav{float:right;}`, html: `
内容区域
' + result + '';
this.resultContent = result;
// 代码高亮
this.$nextTick(() => {
Prism.highlightAll();
this.showCopyBtn = true;
this.toast('格式化完成!', 'success');
});
};
switch (this.selectedType) {
case 'Javascript':
try {
let opts = {
brace_style: "collapse",
break_chained_methods: false,
indent_char: " ",
indent_scripts: "keep",
indent_size: "4",
keep_array_indentation: true,
preserve_newlines: true,
space_after_anon_function: true,
space_before_conditional: true,
unescape_strings: false,
wrap_line_length: "120",
"max_preserve_newlines": "5",
"jslint_happy": false,
"end_with_newline": false,
"indent_inner_html": false,
"comma_first": false,
"e4x": false
};
beauty(js_beautify(this.sourceContent, opts));
} catch (error) {
this.toast('JavaScript格式化失败,请检查代码语法!', 'error');
}
break;
case 'CSS':
try {
css_beautify(this.sourceContent, {}, result => beauty(result));
} catch (error) {
this.toast('CSS格式化失败,请检查代码语法!', 'error');
}
break;
case 'HTML':
try {
beauty(html_beautify(this.sourceContent,{indent_size:4}));
} catch (error) {
this.toast('HTML格式化失败,请检查代码语法!', 'error');
}
break;
case 'SQL':
try {
beauty(vkbeautify.sql(this.sourceContent, 4));
} catch (error) {
this.toast('SQL格式化失败,请检查代码语法!', 'error');
}
break;
default:
try {
beauty(vkbeautify.xml(this.sourceContent));
} catch (error) {
this.toast('XML格式化失败,请检查代码语法!', 'error');
}
}
},
copy: function () {
let _copyToClipboard = (text) => {
let input = document.createElement('textarea');
input.style.position = 'fixed';
input.style.opacity = 0;
input.value = text;
document.body.appendChild(input);
input.select();
document.execCommand('Copy');
document.body.removeChild(input);
this.toast('复制成功,随处粘贴可用!', 'success')
};
let txt = this.$refs.jfContentBox.textContent;
_copyToClipboard(txt);
},
/**
* 自动消失的通知弹窗,仿Notification效果
* @param content 通知内容
* @param type 通知类型:success、error、warning、info(默认)
*/
toast (content, type = 'info') {
window.clearTimeout(window.feHelperAlertMsgTid);
let elAlertMsg = document.querySelector("#fehelper_alertmsg");
// 根据类型配置样式
const typeConfig = {
info: {
background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
borderColor: '#4ade80',
icon: 'ℹ'
},
success: {
background: 'linear-gradient(135deg, #4ade80 0%, #16a34a 100%)',
borderColor: '#22c55e',
icon: '✓'
},
error: {
background: 'linear-gradient(135deg, #ef4444 0%, #dc2626 100%)',
borderColor: '#f87171',
icon: '✕'
},
warning: {
background: 'linear-gradient(135deg, #f59e0b 0%, #d97706 100%)',
borderColor: '#fbbf24',
icon: '⚠'
}
};
const config = typeConfig[type] || typeConfig.info;
if (!elAlertMsg) {
let elWrapper = document.createElement('div');
elWrapper.innerHTML = `