-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsoneditor.html
More file actions
123 lines (109 loc) · 3.57 KB
/
jsoneditor.html
File metadata and controls
123 lines (109 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>JSON Editor</title>
<link rel="stylesheet" href="/jsoneditor/jsoneditor.min.css" />
<script src="/jsoneditor/jsoneditor.min.js"></script>
<style>
.save-btn {
border: 1px solid #ccc;
color: #333;
border-radius: 4px;
background: #e0e0e0;
padding: 6px 16px;
position: fixed;
right: 1rem;
bottom: 1rem;
cursor: pointer;
transition: background 0.2s;
display: none;
}
.save-btn:hover {
background: #d6d6d6;
}
.save-btn.active {
display: block;
}
#jsoneditor {
width: 100%;
height: 100vh;
box-sizing: border-box;
}
</style>
</head>
<body>
<div id="jsoneditor"></div>
<button class="save-btn" id="saveButton">Save</button>
<script>
let editor // Ensure `editor` remains globally accessible
async function fetchText(url) {
try {
const response = await fetch(url)
if (!response.ok)
throw new Error(`Error: ${response.status} ${response.statusText}`)
return await response.text()
} catch (error) {
console.error("Error fetching:", error)
alert("Failed to load mode. Check the console for details.")
return {}
}
}
async function fetchJSON(url) {
try {
const response = await fetch(url)
if (!response.ok)
throw new Error(`Error: ${response.status} ${response.statusText}`)
return await response.json()
} catch (error) {
console.error("Error fetching JSON data:", error)
alert("Failed to load JSON data. Check the console for details.")
return {}
}
}
function initializeEditor(json, mode, initialExpand) {
const saveButton = document.getElementById("saveButton")
if (mode === "tree") saveButton.classList.add("active")
const container = document.getElementById("jsoneditor")
const options = {
mode,
mainMenuBar: true
}
editor = new JSONEditor(container, options)
editor.set(json)
if (initialExpand) {
editor.expandAll()
}
}
async function saveJSON() {
if (!confirm("Please confirm saving.")) return
try {
const updatedJson = editor.get()
const response = await fetch("/saveJSON", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(updatedJson)
})
if (!response.ok)
throw new Error(`Error: ${response.status} ${response.statusText}`)
const result = await response.json()
console.log("Save successful:", result)
alert("JSON saved successfully!")
} catch (error) {
console.error("Error saving JSON:", error)
alert("Failed to save JSON. Check the console for details.")
}
}
document.getElementById("saveButton").addEventListener("click", saveJSON)
;(async function setup() {
const mode = (await fetchText("/getMode")) || "tree"
const initialExpandText =
(await fetchText("/getInitialExpand")) || "true"
const initialExpand = initialExpandText === "true"
const json = await fetchJSON("/getJSON")
initializeEditor(json, mode, initialExpand)
})()
</script>
</body>
</html>