-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-editor.html
More file actions
executable file
·165 lines (156 loc) · 5.33 KB
/
Copy pathexample-editor.html
File metadata and controls
executable file
·165 lines (156 loc) · 5.33 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env local-html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>example.ini Editor</title>
<style>
:root{
--bg:#0a0c12; --fg:#e8f0ff; --muted:#9db1ff;
--accent:#7cf7d4; --accent-2:#7eb6ff;
--card:#0f1422cc; --border:#25314d;
--ok:#1bd89b; --warn:#ffb86c; --err:#ff6b8b;
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
--sans: Inter, system-ui, sans-serif;
}
body {
margin:0; font-family:var(--sans); background:linear-gradient(180deg,#0a0c12,#0e1324);
color:var(--fg); padding:32px; display:flex; flex-direction:column; gap:24px;
}
header {display:flex; justify-content:space-between; align-items:center;}
.brand {display:flex; align-items:center; gap:10px; font-weight:800; text-transform:uppercase;}
.orb {width:14px; height:14px; border-radius:50%; background:radial-gradient(circle,#7cf7d4,#0ea88a); box-shadow:0 0 8px #7cf7d4;}
.card {background:var(--card); border:1px solid var(--border); border-radius:16px; padding:20px; backdrop-filter:blur(12px);}
fieldset {border:1px solid #2b3d63; border-radius:12px; margin-bottom:16px; padding:16px;}
legend {color:var(--accent-2); font-size:13px; text-transform:uppercase;}
label {display:flex; flex-direction:column; font-size:13px; margin-bottom:10px;}
input {background:#0c1528; color:var(--fg); border:1px solid #2b3d63; border-radius:8px; padding:8px; font-family:var(--mono);}
input:focus {outline:none; border-color:#5fa0ff; box-shadow:0 0 0 2px #5fa0ff40;}
button {border:none; padding:10px 16px; border-radius:999px; font-weight:600; cursor:pointer;}
button.primary {background:linear-gradient(180deg,#1ee1b0,#12b48f); color:#04231b;}
.status {display:inline-flex; align-items:center; gap:6px; padding:6px 12px; border-radius:999px; border:1px solid #2b3d63; background:#0a1530dd; font-size:12px;}
.dot {width:8px;height:8px;border-radius:50%;}
.waiting .dot {background:var(--warn);box-shadow:0 0 6px var(--warn);}
.ok .dot {background:var(--ok);box-shadow:0 0 6px var(--ok);}
.failed .dot {background:var(--err);box-shadow:0 0 6px var(--err);}
</style>
</head>
<body>
<header>
<div class="brand"><span class="orb"></span>example.ini Editor</div>
<span id="status" class="status waiting"><span class="dot"></span><span>Waiting</span></span>
</header>
<div class="card">
<form id="iniForm"></form>
<div style="margin-top:16px;">
<button type="submit" form="iniForm" class="primary">Save</button>
</div>
</div>
<script>
const iniUrl = './example.ini';
const statusEl = document.getElementById('status');
const form = document.getElementById('iniForm');
// Parse .ini text into structured object
function parseINI(text){
const data = {};
let section = null;
text.split(/\r?\n/).forEach(line=>{
line = line.trim();
if(!line || line.startsWith(';')) return;
if(line.startsWith('[')){
section = line.slice(1,-1);
data[section] = {};
} else if(section){
const [key,...rest] = line.split('=');
data[section][key.trim()] = rest.join('=').trim();
}
});
return data;
}
// Render fields dynamically
function renderForm(data){
form.innerHTML = '';
Object.entries(data).forEach(([section,fields])=>{
const fs = document.createElement('fieldset');
const lg = document.createElement('legend');
lg.textContent = `[${section}]`;
fs.appendChild(lg);
Object.entries(fields).forEach(([key,value])=>{
const lbl = document.createElement('label');
lbl.textContent = key;
const input = document.createElement('input');
input.name = `${section}.${key}`;
input.value = value;
input.required = true;
lbl.appendChild(input);
fs.appendChild(lbl);
});
form.appendChild(fs);
});
}
// Serialize form back to INI text
function serializeINI(){
const data = {};
[...form.elements].forEach(el=>{
if(!el.name) return;
const [sec,key] = el.name.split('.');
data[sec] = data[sec]||{};
data[sec][key]=el.value;
});
let ini = '; example.ini\n; Generated by editor\n\n';
Object.entries(data).forEach(([sec,fields])=>{
ini += `[${sec}]\n`;
Object.entries(fields).forEach(([k,v])=>{
ini += `${k} = ${v}\n`;
});
ini+='\n';
});
ini+='; End of file\n';
return ini;
}
// Status helper
function setStatus(state,msg){
statusEl.classList.remove('waiting','ok','failed');
statusEl.classList.add(state);
statusEl.querySelector('span:last-child').textContent = msg;
}
// Load INI
async function loadINI(){
try{
const res = await fetch(iniUrl,{cache:'no-store'});
if(!res.ok) throw new Error(res.status);
const text = await res.text();
const parsed = parseINI(text);
renderForm(parsed);
setStatus('ok','Loaded');
}catch(e){
setStatus('failed','Load failed');
console.error(e);
}
}
// Submit handler
form.addEventListener('submit',async e=>{
e.preventDefault();
const raw = serializeINI();
setStatus('waiting','Saving…');
try{
const res = await fetch(iniUrl,{
method:'POST',
headers:{'Content-Type':'text/plain'},
body:raw
});
if(res.ok){
setStatus('ok','Saved');
}else{
setStatus('failed','Save failed ('+res.status+')');
}
}catch(err){
console.error(err);
setStatus('failed','Save failed');
}
});
loadINI();
</script>
</body>
</html>