464 lines
15 KiB
HTML
464 lines
15 KiB
HTML
<!doctype html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>JSON 格式化 · 功能工具箱</title>
|
|
<link rel="icon" href="assets/favicon.svg" type="image/svg+xml" />
|
|
<link rel="stylesheet" href="assets/styles.css" />
|
|
<script src="assets/common.js"></script>
|
|
<style>
|
|
.toolbar-label-inline {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
font-size: 0.78rem;
|
|
color: var(--text-secondary);
|
|
font-weight: 550;
|
|
}
|
|
|
|
.toolbar-label-inline select {
|
|
width: auto;
|
|
padding: 5px 30px 5px 9px;
|
|
font-size: 0.78rem;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.editor-head .head-right {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
|
|
.editor-head .copy-btn {
|
|
padding: 3px 8px;
|
|
font-size: 0.7rem;
|
|
}
|
|
|
|
.status-error {
|
|
color: var(--danger);
|
|
}
|
|
|
|
#errorBox {
|
|
flex: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="editor-app">
|
|
<header class="tool-header">
|
|
<div class="tool-icon">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<path
|
|
d="M8 3H7a2 2 0 0 0-2 2v4a2 2 0 0 1-2 2 2 2 0 0 1 2 2v4a2 2 0 0 0 2 2h1M16 3h1a2 2 0 0 1 2 2v4a2 2 0 0 0 2 2 2 2 0 0 0-2 2v4a2 2 0 0 1-2 2h-1"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
<div>
|
|
<h1>JSON 格式化</h1>
|
|
<p>美化、压缩与校验 JSON,实时高亮并定位语法错误。</p>
|
|
</div>
|
|
<div class="header-actions">
|
|
<button
|
|
class="btn btn--icon btn--ghost"
|
|
data-theme-toggle
|
|
title="切换主题"
|
|
aria-label="切换主题"
|
|
>
|
|
<svg class="icon-moon" viewBox="0 0 24 24" aria-hidden="true">
|
|
<path d="M21 12.8A8.5 8.5 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z" />
|
|
</svg>
|
|
<svg class="icon-sun" viewBox="0 0 24 24" aria-hidden="true">
|
|
<circle cx="12" cy="12" r="4" />
|
|
<path
|
|
d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"
|
|
/>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="editor-toolbar">
|
|
<div class="toolbar-group">
|
|
<button class="btn btn--primary" id="formatBtn">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<path d="M4 6h16M4 12h10M4 18h7" />
|
|
</svg>
|
|
格式化
|
|
</button>
|
|
<button class="btn" id="minifyBtn">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<path d="M4 8h16M4 16h16" />
|
|
</svg>
|
|
压缩
|
|
</button>
|
|
<button class="btn btn--soft" id="copyBtn">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<rect x="9" y="9" width="11" height="11" rx="2" />
|
|
<path d="M5 15V5a1 1 0 0 1 1-1h10" />
|
|
</svg>
|
|
复制结果
|
|
</button>
|
|
<button class="btn btn--ghost" id="sampleBtn">示例</button>
|
|
<button class="btn btn--ghost" id="clearBtn">清空</button>
|
|
</div>
|
|
|
|
<span class="spacer"></span>
|
|
|
|
<div class="toolbar-group">
|
|
<label class="toolbar-label-inline" for="indentSize">
|
|
缩进
|
|
<select id="indentSize">
|
|
<option value="2" selected>2 空格</option>
|
|
<option value="4">4 空格</option>
|
|
<option value="8">8 空格</option>
|
|
<option value="tab">Tab</option>
|
|
</select>
|
|
</label>
|
|
<label class="checkline">
|
|
<input type="checkbox" id="sortKeys" />
|
|
排序键名
|
|
</label>
|
|
<label class="checkline">
|
|
<input type="checkbox" id="autoFormat" checked />
|
|
自动格式化
|
|
</label>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="alert alert--error hidden" id="errorBox" role="alert">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<circle cx="12" cy="12" r="9" />
|
|
<path d="M12 8v5M12 16.5v.01" />
|
|
</svg>
|
|
<span id="errorText"></span>
|
|
</div>
|
|
|
|
<div class="editor-split">
|
|
<section class="editor-pane">
|
|
<div class="editor-head">
|
|
<span>输入 JSON</span>
|
|
<span class="head-right">
|
|
<span class="count" id="inputCount">0 字符</span>
|
|
</span>
|
|
</div>
|
|
<div class="editor-body">
|
|
<textarea
|
|
id="jsonInput"
|
|
class="editor-textarea"
|
|
wrap="off"
|
|
spellcheck="false"
|
|
placeholder='在此粘贴或输入 JSON,例如:
|
|
{"name": "张三", "age": 25, "skills": ["JavaScript", "Python"]}'
|
|
></textarea>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="editor-pane">
|
|
<div class="editor-head">
|
|
<span>格式化结果</span>
|
|
<span class="head-right">
|
|
<button class="copy-btn" id="copyOutputBtn">复制</button>
|
|
<span class="count" id="outputCount">0 字符</span>
|
|
</span>
|
|
</div>
|
|
<div class="editor-body">
|
|
<pre class="code-output" id="jsonOutput"><span class="placeholder">格式化结果将显示在这里…</span></pre>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
|
|
<div class="editor-status">
|
|
<span class="status-text" id="status">就绪</span>
|
|
<span class="spacer"></span>
|
|
<span>
|
|
<span class="kbd">Ctrl/⌘</span> + <span class="kbd">Enter</span>
|
|
格式化 ·
|
|
<span class="kbd">Ctrl/⌘</span> + <span class="kbd">Shift</span> +
|
|
<span class="kbd">M</span> 压缩
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
(function () {
|
|
"use strict";
|
|
|
|
var input = document.getElementById("jsonInput");
|
|
var output = document.getElementById("jsonOutput");
|
|
var inputCount = document.getElementById("inputCount");
|
|
var outputCount = document.getElementById("outputCount");
|
|
var status = document.getElementById("status");
|
|
var errorBox = document.getElementById("errorBox");
|
|
var errorText = document.getElementById("errorText");
|
|
var indentSelect = document.getElementById("indentSize");
|
|
var sortKeysCheckbox = document.getElementById("sortKeys");
|
|
var autoFormatCheckbox = document.getElementById("autoFormat");
|
|
|
|
var EMPTY_OUTPUT =
|
|
'<span class="placeholder">格式化结果将显示在这里…</span>';
|
|
|
|
var SAMPLE = {
|
|
name: "张三",
|
|
age: 25,
|
|
city: "北京",
|
|
skills: ["JavaScript", "Python", "Java"],
|
|
profile: {
|
|
email: "zhangsan@example.com",
|
|
phone: "13800138000",
|
|
address: {
|
|
street: "朝阳区示例街道 88 号",
|
|
zipcode: "100000",
|
|
},
|
|
},
|
|
isActive: true,
|
|
registeredAt: null,
|
|
};
|
|
|
|
function indentValue() {
|
|
var value = indentSelect.value;
|
|
return value === "tab" ? "\t" : parseInt(value, 10);
|
|
}
|
|
|
|
function setStatus(text, isError) {
|
|
status.textContent = text;
|
|
status.classList.toggle("status-error", !!isError);
|
|
}
|
|
|
|
function showError(message) {
|
|
errorText.textContent = message;
|
|
errorBox.classList.remove("hidden");
|
|
}
|
|
|
|
function clearError() {
|
|
errorBox.classList.add("hidden");
|
|
}
|
|
|
|
function updateCounts() {
|
|
inputCount.textContent = input.value.length + " 字符";
|
|
var isEmpty = !!output.querySelector(".placeholder");
|
|
outputCount.textContent =
|
|
(isEmpty ? 0 : output.textContent.length) + " 字符";
|
|
}
|
|
|
|
function sortObjectKeys(value) {
|
|
if (Array.isArray(value)) {
|
|
return value.map(sortObjectKeys);
|
|
}
|
|
if (value !== null && typeof value === "object") {
|
|
var sorted = {};
|
|
Object.keys(value)
|
|
.sort()
|
|
.forEach(function (key) {
|
|
sorted[key] = sortObjectKeys(value[key]);
|
|
});
|
|
return sorted;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function highlightJSON(json) {
|
|
var pattern =
|
|
/("(?:\\u[a-fA-F0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(?:true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g;
|
|
var html = "";
|
|
var lastIndex = 0;
|
|
var match;
|
|
|
|
while ((match = pattern.exec(json)) !== null) {
|
|
html += Toolbox.escapeHtml(json.slice(lastIndex, match.index));
|
|
var token = match[0];
|
|
var type = "number";
|
|
|
|
if (token.charAt(0) === '"') {
|
|
type = /:\s*$/.test(token) ? "key" : "string";
|
|
} else if (token === "true" || token === "false") {
|
|
type = "boolean";
|
|
} else if (token === "null") {
|
|
type = "null";
|
|
}
|
|
|
|
html +=
|
|
'<span class="tok-' +
|
|
type +
|
|
'">' +
|
|
Toolbox.escapeHtml(token) +
|
|
"</span>";
|
|
lastIndex = pattern.lastIndex;
|
|
}
|
|
|
|
html += Toolbox.escapeHtml(json.slice(lastIndex));
|
|
return html;
|
|
}
|
|
|
|
function prepareValue(raw) {
|
|
var value = JSON.parse(raw);
|
|
if (sortKeysCheckbox.checked && value !== null) {
|
|
value = sortObjectKeys(value);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function formatJSON(options) {
|
|
options = options || {};
|
|
var raw = input.value.trim();
|
|
|
|
if (!raw) {
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
clearError();
|
|
setStatus("就绪");
|
|
updateCounts();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
var value = prepareValue(raw);
|
|
var formatted = JSON.stringify(value, null, indentValue());
|
|
output.innerHTML = highlightJSON(formatted);
|
|
clearError();
|
|
setStatus("已格式化 · " + formatted.length + " 字符");
|
|
if (options.notify) {
|
|
Toolbox.toast("JSON 格式化完成", "success");
|
|
}
|
|
} catch (error) {
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
showError("JSON 语法错误:" + error.message);
|
|
setStatus("格式错误", true);
|
|
}
|
|
|
|
updateCounts();
|
|
}
|
|
|
|
function minifyJSON(notify) {
|
|
var raw = input.value.trim();
|
|
|
|
if (!raw) {
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
clearError();
|
|
setStatus("请先输入 JSON", true);
|
|
updateCounts();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
var value = prepareValue(raw);
|
|
var minified = JSON.stringify(value);
|
|
output.innerHTML = highlightJSON(minified);
|
|
clearError();
|
|
setStatus("已压缩 · " + minified.length + " 字符");
|
|
if (notify) {
|
|
Toolbox.toast("JSON 压缩完成", "success");
|
|
}
|
|
} catch (error) {
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
showError("JSON 语法错误:" + error.message);
|
|
setStatus("格式错误", true);
|
|
}
|
|
|
|
updateCounts();
|
|
}
|
|
|
|
function copyResult() {
|
|
var text = output.textContent;
|
|
if (!text || text === "格式化结果将显示在这里…") {
|
|
Toolbox.toast("没有可复制的内容", "error");
|
|
return;
|
|
}
|
|
Toolbox.copyWithToast(text, "JSON 已复制");
|
|
}
|
|
|
|
function clearAll() {
|
|
input.value = "";
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
clearError();
|
|
setStatus("就绪");
|
|
updateCounts();
|
|
input.focus();
|
|
}
|
|
|
|
var autoFormat = Toolbox.debounce(function () {
|
|
if (autoFormatCheckbox.checked) {
|
|
formatJSON({});
|
|
}
|
|
}, 350);
|
|
|
|
input.addEventListener("input", function () {
|
|
updateCounts();
|
|
autoFormat();
|
|
});
|
|
|
|
input.addEventListener("keydown", function (event) {
|
|
// Tab 插入缩进而不是切换焦点
|
|
if (event.key === "Tab") {
|
|
event.preventDefault();
|
|
var start = input.selectionStart;
|
|
var end = input.selectionEnd;
|
|
input.value =
|
|
input.value.slice(0, start) + " " + input.value.slice(end);
|
|
input.selectionStart = input.selectionEnd = start + 2;
|
|
updateCounts();
|
|
autoFormat();
|
|
}
|
|
});
|
|
|
|
document.getElementById("formatBtn").addEventListener("click", function () {
|
|
formatJSON({ notify: true });
|
|
});
|
|
|
|
document.getElementById("minifyBtn").addEventListener("click", function () {
|
|
minifyJSON(true);
|
|
});
|
|
|
|
document.getElementById("copyBtn").addEventListener("click", copyResult);
|
|
document
|
|
.getElementById("copyOutputBtn")
|
|
.addEventListener("click", copyResult);
|
|
document.getElementById("clearBtn").addEventListener("click", clearAll);
|
|
|
|
document.getElementById("sampleBtn").addEventListener("click", function () {
|
|
input.value = JSON.stringify(SAMPLE, null, 2);
|
|
updateCounts();
|
|
formatJSON({});
|
|
Toolbox.toast("已载入示例 JSON", "success");
|
|
});
|
|
|
|
indentSelect.addEventListener("change", function () {
|
|
if (autoFormatCheckbox.checked) formatJSON({});
|
|
});
|
|
|
|
sortKeysCheckbox.addEventListener("change", function () {
|
|
if (autoFormatCheckbox.checked) formatJSON({});
|
|
});
|
|
|
|
autoFormatCheckbox.addEventListener("change", function () {
|
|
if (autoFormatCheckbox.checked) formatJSON({});
|
|
});
|
|
|
|
document.addEventListener("keydown", function (event) {
|
|
var modifier = event.metaKey || event.ctrlKey;
|
|
|
|
if (modifier && event.key === "Enter") {
|
|
event.preventDefault();
|
|
formatJSON({ notify: true });
|
|
return;
|
|
}
|
|
|
|
if (modifier && event.shiftKey && event.key.toLowerCase() === "c") {
|
|
event.preventDefault();
|
|
copyResult();
|
|
return;
|
|
}
|
|
|
|
if (modifier && event.shiftKey && event.key.toLowerCase() === "m") {
|
|
event.preventDefault();
|
|
minifyJSON(true);
|
|
}
|
|
});
|
|
|
|
// 初始化
|
|
updateCounts();
|
|
input.focus();
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|