|
33 | 33 | <h3>处理结果</h3> |
34 | 34 | <div class="panel-actions"> |
35 | 35 | <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> |
36 | 38 | <button @click="copyToClipboard" class="tool-button">复制</button> |
37 | 39 | </div> |
38 | 40 | </div> |
@@ -195,9 +197,12 @@ export default { |
195 | 197 | this.tryRepairJson(); |
196 | 198 | } |
197 | 199 | }, |
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 | + |
201 | 206 | const lines = stringified.split('\n'); |
202 | 207 | |
203 | 208 | // 跟踪每一行的原始索引、内容和缩进级别 |
@@ -529,6 +534,55 @@ export default { |
529 | 534 | this.processJson(); |
530 | 535 | // 提示用户 |
531 | 536 | 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 | + } |
532 | 586 | } |
533 | 587 | } |
534 | 588 | } |
@@ -950,4 +1004,23 @@ export default { |
950 | 1004 | text-align: left; |
951 | 1005 | padding-left: 8px; |
952 | 1006 | } |
| 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 | +} |
953 | 1026 | </style> |
0 commit comments