-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.html
More file actions
457 lines (413 loc) · 16.3 KB
/
Copy patheditor.html
File metadata and controls
457 lines (413 loc) · 16.3 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Atlas Style Editor — MAPBOSS</title>
<link href="https://unpkg.com/maplibre-gl@4/dist/maplibre-gl.css" rel="stylesheet">
<script src="https://unpkg.com/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script src="https://unpkg.com/pmtiles@3/dist/pmtiles.js"></script>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: #0d0d0d; display: flex; color: #e0e0e0;
}
/* ─── Sidebar ─── */
#sidebar {
width: 440px; min-width: 260px; max-width: 680px;
display: flex; flex-direction: column;
background: #141414; border-right: 1px solid #222;
resize: horizontal; overflow: hidden; flex-shrink: 0;
}
#sidebar-header {
display: flex; align-items: center; gap: 8px;
padding: 10px 14px; border-bottom: 1px solid #222;
background: #111; flex-shrink: 0;
}
#brand { font-size: 12px; font-weight: 700; letter-spacing: 0.08em; color: #fff; opacity: 0.85; }
#brand span { opacity: 0.4; font-weight: 400; }
.sep { flex: 1; }
#home-link {
font-size: 11px; color: rgba(255,255,255,0.4); text-decoration: none;
padding: 3px 9px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.1);
margin-right: 4px;
}
#home-link:hover { color: rgba(255,255,255,0.8); border-color: rgba(255,255,255,0.3); }
#demo-link {
font-size: 11px; color: rgba(255,255,255,0.4); text-decoration: none;
padding: 3px 9px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.1);
}
#demo-link:hover { color: rgba(255,255,255,0.8); border-color: rgba(255,255,255,0.3); }
/* style picker */
#style-row {
display: flex; gap: 6px; align-items: center;
padding: 8px 12px; border-bottom: 1px solid #1e1e1e; flex-shrink: 0;
}
.style-pill {
padding: 4px 12px; border-radius: 14px; font-size: 11px; font-weight: 500;
border: 1px solid #333; background: #1c1c1c; color: #aaa; cursor: pointer;
transition: all 0.12s;
}
.style-pill:hover { border-color: #555; color: #ddd; }
.style-pill.active { background: #fff; color: #111; border-color: transparent; }
.load-file-btn {
margin-left: auto; padding: 4px 10px; border-radius: 12px; font-size: 11px;
border: 1px solid #333; background: transparent; color: #888; cursor: pointer;
font-family: inherit;
}
.load-file-btn:hover { border-color: #555; color: #ccc; }
/* toolbar */
#toolbar {
display: flex; gap: 6px; align-items: center;
padding: 7px 12px; border-bottom: 1px solid #1e1e1e; flex-shrink: 0;
}
#status { font-size: 11px; color: #555; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#status.ok { color: #4caf86; }
#status.err { color: #e05c5c; }
.btn {
padding: 4px 12px; border-radius: 12px; font-size: 11px; font-weight: 500;
cursor: pointer; border: 1px solid transparent; transition: all 0.12s;
font-family: inherit; flex-shrink: 0;
}
.btn-apply { background: #2563eb; color: #fff; border-color: #2563eb; }
.btn-apply:hover { background: #1d4ed8; }
.btn-download { background: #1e1e1e; color: #aaa; border-color: #333; }
.btn-download:hover { border-color: #555; color: #ddd; }
.btn-format { background: #1e1e1e; color: #777; border-color: #2a2a2a; }
.btn-format:hover { border-color: #444; color: #bbb; }
/* editor area */
#editor-wrap { flex: 1; overflow: hidden; position: relative; min-height: 0; }
#editor-monaco { width: 100%; height: 100%; }
/* fallback textarea (shown while Monaco loads or if it fails) */
#editor-textarea {
width: 100%; height: 100%; resize: none; border: none; outline: none;
background: #1e1e1e; color: #d4d4d4; font-family: 'Menlo', 'Monaco', 'Consolas', monospace;
font-size: 12px; line-height: 1.6; padding: 12px; tab-size: 2;
}
/* Monaco loading indicator */
#monaco-loading {
position: absolute; inset: 0; z-index: 2;
display: flex; align-items: center; justify-content: center; gap: 10px;
background: #1e1e1e; color: #555; font-size: 12px;
}
#monaco-loading.hidden { display: none; }
.spinner-sm {
width: 16px; height: 16px; border-radius: 50%;
border: 2px solid rgba(255,255,255,0.08);
border-top-color: rgba(255,255,255,0.4);
animation: spin 0.7s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
/* ─── Map ─── */
#map-wrap { flex: 1; min-width: 0; position: relative; }
#map { width: 100%; height: 100%; }
#map-loading {
position: absolute; inset: 0; z-index: 5;
display: flex; align-items: center; justify-content: center;
background: rgba(13,13,13,0.65); pointer-events: none;
transition: opacity 0.3s;
}
#map-loading.hidden { opacity: 0; }
.spinner-map {
width: 28px; height: 28px; border-radius: 50%;
border: 2px solid rgba(255,255,255,0.1);
border-top-color: rgba(255,255,255,0.5);
animation: spin 0.7s linear infinite;
}
.maplibregl-ctrl-attrib { font-size: 10px !important; }
</style>
</head>
<body>
<!-- SIDEBAR -->
<div id="sidebar">
<div id="sidebar-header">
<div id="brand">ATLAS <span>Editor</span></div>
<div class="sep"></div>
<a id="home-link" href="index.html">← Home</a>
<a id="demo-link" href="demo.html">Demo</a>
</div>
<div id="style-row">
<button class="style-pill active" data-style="light" onclick="loadPreset(this)">Light</button>
<button class="style-pill" data-style="dark" onclick="loadPreset(this)">Dark</button>
<button class="style-pill" data-style="mbstyle" onclick="loadPreset(this)">Standard</button>
<input type="file" id="file-input" accept=".json" style="display:none" onchange="loadFromFile(event)">
<button class="load-file-btn" onclick="document.getElementById('file-input').click()">Open file…</button>
</div>
<div id="toolbar">
<span id="status">Loading…</span>
<button class="btn btn-format" onclick="formatJson()">Format</button>
<button class="btn btn-download" onclick="downloadStyle()">Download</button>
<button class="btn btn-apply" onclick="manualApply()">Apply ▶</button>
</div>
<div id="editor-wrap">
<!-- fallback textarea shown until Monaco loads -->
<textarea id="editor-textarea" spellcheck="false"></textarea>
<!-- Monaco mounts here -->
<div id="editor-monaco" style="display:none"></div>
<!-- loading badge -->
<div id="monaco-loading">
<div class="spinner-sm"></div>
Loading editor…
</div>
</div>
</div>
<!-- MAP -->
<div id="map-wrap">
<div id="map-loading"><div class="spinner-map"></div></div>
<div id="map"></div>
</div>
<script>
/* ══════════════════════════════════════════
SETUP
══════════════════════════════════════════ */
const protocol = new pmtiles.Protocol();
maplibregl.addProtocol('pmtiles', protocol.tile.bind(protocol));
const STYLES = {
light: 'styles/light.json',
dark: 'styles/dark.json',
mbstyle: 'styles/mbstyle.json',
};
let map = null;
let monacoEditor = null;
let suppressApply = false; // prevent auto-apply when programmatically setting value
let autoApplyTimer = null;
let currentStyleName = 'light';
/* ══════════════════════════════════════════
BOOTSTRAP — fetch initial style + init map
══════════════════════════════════════════ */
(async () => {
try {
const json = await fetchStyle('light');
setTextarea(JSON.stringify(json, null, 2));
initMap(json);
setStatus('Ready', 'ok');
} catch (e) {
setStatus('Failed to load style: ' + e.message, 'err');
}
})();
loadMonaco();
/* ══════════════════════════════════════════
MAP
══════════════════════════════════════════ */
function initMap(styleJson) {
if (map) {
// reuse existing map — just swap style
const cam = { center: map.getCenter(), zoom: map.getZoom(), bearing: map.getBearing(), pitch: map.getPitch() };
showMapLoading();
map.setStyle(styleJson);
map.once('styledata', () => {
map.jumpTo(cam);
hideMapLoading();
setStatus('Applied', 'ok');
});
return;
}
// first init
map = new maplibregl.Map({
container: 'map',
style: styleJson,
center: [100.5018, 13.7563],
zoom: 10,
attributionControl: { compact: true },
});
map.addControl(new maplibregl.NavigationControl(), 'bottom-right');
map.addControl(new maplibregl.ScaleControl({ unit: 'metric' }), 'bottom-left');
map.on('load', () => { hideMapLoading(); setStatus('Ready', 'ok'); });
map.on('error', e => setStatus('Map error: ' + (e.error?.message ?? e.error), 'err'));
}
/* ══════════════════════════════════════════
MONACO EDITOR
══════════════════════════════════════════ */
function loadMonaco() {
const s = document.createElement('script');
// jsDelivr is more reliable than cdnjs for Monaco
s.src = 'https://cdn.jsdelivr.net/npm/monaco-editor@0.47.0/min/vs/loader.js';
s.onload = () => {
require.config({ paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.47.0/min/vs' } });
require(['vs/editor/editor.main'], mountMonaco);
};
s.onerror = () => {
// Monaco failed — fallback textarea is already visible and functional
document.getElementById('monaco-loading').classList.add('hidden');
setStatus('Editor loaded (plain mode)', '');
wireTextareaAutoApply();
};
document.head.appendChild(s);
}
function mountMonaco() {
const wrap = document.getElementById('editor-wrap');
const monacoDiv = document.getElementById('editor-monaco');
const textarea = document.getElementById('editor-textarea');
const loadingEl = document.getElementById('monaco-loading');
// grab current value from textarea before hiding it
const currentValue = textarea.value;
monacoEditor = monaco.editor.create(monacoDiv, {
language: 'json',
theme: 'vs-dark',
fontSize: 12,
lineHeight: 18,
minimap: { enabled: false },
scrollBeyondLastLine: false,
wordWrap: 'off',
automaticLayout: true,
tabSize: 2,
renderWhitespace: 'none',
});
// swap visibility
textarea.style.display = 'none';
monacoDiv.style.display = 'block';
loadingEl.classList.add('hidden');
// set value without triggering auto-apply
suppressApply = true;
monacoEditor.setValue(currentValue);
monacoEditor.setScrollPosition({ scrollTop: 0 });
requestAnimationFrame(() => { suppressApply = false; });
monacoEditor.onDidChangeModelContent(() => {
if (suppressApply) return;
setStatus('Editing…', '');
clearTimeout(autoApplyTimer);
autoApplyTimer = setTimeout(autoApply, 1500);
});
}
/* ══════════════════════════════════════════
APPLY STYLE
══════════════════════════════════════════ */
function autoApply() {
const raw = getEditorValue();
try {
const json = JSON.parse(raw);
applyJsonToMap(json);
} catch {
setStatus('JSON syntax error', 'err');
}
}
function manualApply() {
clearTimeout(autoApplyTimer);
const raw = getEditorValue();
try {
const json = JSON.parse(raw);
applyJsonToMap(json);
} catch (e) {
setStatus('JSON error: ' + e.message, 'err');
}
}
function applyJsonToMap(json) {
if (!map) { initMap(json); return; }
setStatus('Applying…', '');
showMapLoading();
const cam = { center: map.getCenter(), zoom: map.getZoom(), bearing: map.getBearing(), pitch: map.getPitch() };
map.setStyle(json);
map.once('styledata', () => {
map.jumpTo(cam);
hideMapLoading();
setStatus('Applied', 'ok');
});
}
/* ══════════════════════════════════════════
PRESET / FILE LOADING
══════════════════════════════════════════ */
async function loadPreset(btn) {
const name = btn.dataset.style;
document.querySelectorAll('.style-pill').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentStyleName = name;
setStatus('Loading…', '');
showMapLoading();
try {
const json = await fetchStyle(name);
setEditorValue(JSON.stringify(json, null, 2));
initMap(json);
} catch (e) {
setStatus('Load failed: ' + e.message, 'err');
hideMapLoading();
}
}
function loadFromFile(event) {
const file = event.target.files[0];
if (!file) return;
currentStyleName = file.name.replace(/\.json$/i, '');
const reader = new FileReader();
reader.onload = e => {
try {
const json = JSON.parse(e.target.result);
document.querySelectorAll('.style-pill').forEach(b => b.classList.remove('active'));
setEditorValue(JSON.stringify(json, null, 2));
initMap(json);
setStatus('Loaded: ' + file.name, 'ok');
} catch {
setStatus('Invalid JSON file', 'err');
}
};
reader.readAsText(file);
event.target.value = '';
}
/* ══════════════════════════════════════════
FORMAT / DOWNLOAD
══════════════════════════════════════════ */
function formatJson() {
try {
const json = JSON.parse(getEditorValue());
setEditorValue(JSON.stringify(json, null, 2));
setStatus('Formatted', 'ok');
} catch (e) {
setStatus('Invalid JSON', 'err');
}
}
function downloadStyle() {
try {
const raw = getEditorValue();
JSON.parse(raw); // validate first
const blob = new Blob([raw], { type: 'application/json' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = (currentStyleName || 'style') + '.json';
a.click();
URL.revokeObjectURL(a.href);
} catch {
setStatus('Invalid JSON — fix errors before downloading', 'err');
}
}
/* ══════════════════════════════════════════
HELPERS
══════════════════════════════════════════ */
async function fetchStyle(name) {
const res = await fetch(STYLES[name]);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
}
function setEditorValue(text) {
suppressApply = true;
if (monacoEditor) {
monacoEditor.setValue(text);
monacoEditor.setScrollPosition({ scrollTop: 0 });
} else {
setTextarea(text);
}
requestAnimationFrame(() => { suppressApply = false; });
}
function setTextarea(text) {
document.getElementById('editor-textarea').value = text;
}
function getEditorValue() {
if (monacoEditor) return monacoEditor.getValue();
return document.getElementById('editor-textarea').value;
}
function setStatus(msg, type) {
const el = document.getElementById('status');
el.textContent = msg;
el.className = type || '';
}
function showMapLoading() { document.getElementById('map-loading').classList.remove('hidden'); }
function hideMapLoading() { document.getElementById('map-loading').classList.add('hidden'); }
// textarea auto-apply (used only when Monaco fails to load)
function wireTextareaAutoApply() {
document.getElementById('editor-textarea').addEventListener('input', () => {
clearTimeout(autoApplyTimer);
autoApplyTimer = setTimeout(autoApply, 1500);
});
}
</script>
</body>
</html>