Skip to content

Commit 95bb916

Browse files
committed
增加压缩和格式化按钮
1 parent e54d050 commit 95bb916

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

src/components/JsonTool.vue

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<h3>处理结果</h3>
3434
<div class="panel-actions">
3535
<button v-if="hasRepairSuggestion" @click="applyRepair" class="tool-button">修复</button>
36+
<button @click="compressJson" class="tool-button compress-button">压缩</button>
37+
<button @click="formatJson" class="tool-button format-button">格式化</button>
3638
<button @click="copyToClipboard" class="tool-button">复制</button>
3739
</div>
3840
</div>
@@ -195,9 +197,12 @@ export default {
195197
this.tryRepairJson();
196198
}
197199
},
198-
formatJsonToHtml(json) {
199-
// 将JSON格式化为字符串
200-
const stringified = JSON.stringify(json, null, 2);
200+
formatJsonToHtml(json, isCompressed = false) {
201+
// 根据是否压缩选择不同的格式化方式
202+
const stringified = isCompressed ?
203+
JSON.stringify(json) :
204+
JSON.stringify(json, null, 2);
205+
201206
const lines = stringified.split('\n');
202207
203208
// 跟踪每一行的原始索引、内容和缩进级别
@@ -529,6 +534,55 @@ export default {
529534
this.processJson();
530535
// 提示用户
531536
this.showToastMessage('已插入示例JSON数据');
537+
},
538+
// 压缩JSON
539+
compressJson() {
540+
if (this.jsonError || !this.completeJsonString) {
541+
return;
542+
}
543+
544+
try {
545+
// 解析当前JSON
546+
const parsedJson = JSON.parse(this.completeJsonString);
547+
548+
// 重新转为字符串,但不添加空格和换行符
549+
this.jsonResult = JSON.stringify(parsedJson);
550+
this.completeJsonString = this.jsonResult;
551+
552+
// 重新格式化显示
553+
this.formatJsonToHtml(parsedJson, true);
554+
555+
// 显示提示
556+
this.showToastMessage('JSON 已压缩');
557+
} catch (error) {
558+
console.error('JSON压缩失败:', error);
559+
this.showToastMessage('JSON 压缩失败');
560+
}
561+
},
562+
563+
// 格式化JSON
564+
formatJson() {
565+
if (this.jsonError || !this.completeJsonString) {
566+
return;
567+
}
568+
569+
try {
570+
// 解析当前JSON
571+
const parsedJson = JSON.parse(this.completeJsonString);
572+
573+
// 重新转为格式化的字符串
574+
this.jsonResult = JSON.stringify(parsedJson, null, 2);
575+
this.completeJsonString = this.jsonResult;
576+
577+
// 重新格式化显示
578+
this.formatJsonToHtml(parsedJson);
579+
580+
// 显示提示
581+
this.showToastMessage('JSON 已格式化');
582+
} catch (error) {
583+
console.error('JSON格式化失败:', error);
584+
this.showToastMessage('JSON 格式化失败');
585+
}
532586
}
533587
}
534588
}
@@ -950,4 +1004,23 @@ export default {
9501004
text-align: left;
9511005
padding-left: 8px;
9521006
}
1007+
1008+
/* 添加按钮差异化样式 */
1009+
.tool-button.format-button {
1010+
background: linear-gradient(135deg, rgba(33, 150, 243, 0.1) 0%, rgba(3, 169, 244, 0.1) 100%);
1011+
color: #0277bd;
1012+
}
1013+
1014+
.tool-button.format-button:hover {
1015+
background: linear-gradient(135deg, rgba(33, 150, 243, 0.2) 0%, rgba(3, 169, 244, 0.2) 100%);
1016+
}
1017+
1018+
.tool-button.compress-button {
1019+
background: linear-gradient(135deg, rgba(255, 152, 0, 0.1) 0%, rgba(255, 193, 7, 0.1) 100%);
1020+
color: #e65100;
1021+
}
1022+
1023+
.tool-button.compress-button:hover {
1024+
background: linear-gradient(135deg, rgba(255, 152, 0, 0.2) 0%, rgba(255, 193, 7, 0.2) 100%);
1025+
}
9531026
</style>

0 commit comments

Comments
 (0)