kpr-utils/templates/yaml.html

138 lines
3.7 KiB
HTML
Raw Permalink Normal View History

2025-05-21 05:50:10 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>YAML Formatter &amp; Validator</title>
<style>
body {
font-family: sans-serif;
padding: 1rem;
}
h1 {
text-align: center;
margin-bottom: 1rem;
}
.container {
display: flex;
gap: 1rem;
width: 80%;
margin: auto;
}
.pane {
flex: 1;
display: flex;
flex-direction: column;
}
.pane label {
font-weight: bold;
margin-bottom: 0.25rem;
}
.pane textarea {
flex: 1;
width: 100%;
padding: 0.5rem;
font-family: monospace;
font-size: 0.9rem;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
.controls {
margin-top: 0.5rem;
display: flex;
gap: 0.5rem;
}
.controls button {
padding: 0.4rem 0.8rem;
cursor: pointer;
}
.error {
color: #c00;
margin-top: 0.5rem;
min-height: 1.2em;
}
</style>
</head>
<body>
<div class="container">
<!-- Input pane -->
<div class="pane">
<label for="input-yaml">Input YAML</label>
<textarea id="input-yaml" placeholder="Paste or type YAML here…"></textarea>
</div>
<!-- Output pane -->
<div class="pane">
<label for="output-yaml">Formatted YAML</label>
<textarea id="output-yaml" readonly placeholder="Valid YAML will appear here…"></textarea>
<div id="error-yaml" class="error"></div>
<div class="controls">
<button id="copy-yaml">Copy Formatted</button>
<button id="clear-yaml">Clear All</button>
</div>
</div>
</div>
<!-- js-yaml from CDN -->
<script src="https://cdn.jsdelivr.net/npm/js-yaml@4.1.0/dist/js-yaml.min.js"></script>
<script>
const inTA = document.getElementById('input-yaml');
const outTA = document.getElementById('output-yaml');
const errDiv = document.getElementById('error-yaml');
const copyBtn = document.getElementById('copy-yaml');
const clearBtn = document.getElementById('clear-yaml');
function formatYAML() {
const text = inTA.value.trim();
if (!text) {
outTA.value = '';
errDiv.textContent = '';
return;
}
try {
// parse then dump with 2-space indent
const obj = jsyaml.load(text);
outTA.value = jsyaml.dump(obj, { indent: 2 });
errDiv.textContent = '';
} catch (e) {
outTA.value = '';
errDiv.textContent = '❌ ' + e.message;
}
}
// Live update on input
inTA.addEventListener('input', formatYAML);
// Copy formatted YAML
copyBtn.addEventListener('click', () => {
if (!outTA.value) return;
navigator.clipboard.writeText(outTA.value)
.then(() => alert('Formatted YAML copied!'));
});
// Clear both panes
clearBtn.addEventListener('click', () => {
inTA.value = '';
outTA.value = '';
errDiv.textContent = '';
inTA.focus();
});
// Initialize
window.addEventListener('DOMContentLoaded', () => {
clearBtn.click();
});
</script>
</body>
</html>