fix editor not using URL from env

This commit is contained in:
Mathias DUPEUX 2025-03-06 20:07:04 +01:00
parent 265d29c275
commit d084b546b2
2 changed files with 56 additions and 2 deletions

View file

@ -13,6 +13,59 @@
<label for="previewSwitch">Preview</label>
<input type="checkbox" id="previewSwitch">
</div>
<script src="{{ url_for('static', filename='editor.js') }}"></script>
<script>
const codeArea = document.getElementById('code')
const previewFrame = document.getElementById('preview')
const previewSwitch = document.getElementById('previewSwitch')
let lastCode = ''
let previewMode = false
function forceUpdatePreview() {
const currentCode = codeArea.value
const encodedCode = btoa(currentCode)
let url = `{{ hostname }}/preview/${encodedCode}`
if (previewMode) {
url += "?website=True"
}
previewFrame.src = url
}
function updatePreview() {
const currentCode = codeArea.value
if (currentCode !== lastCode) {
forceUpdatePreview();
lastCode = currentCode
}
}
function saveCode() {
const currentCode = codeArea.value
const blob = new Blob([currentCode], { type: 'text/plain' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'code.html'
a.style.display = 'none'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
}
setInterval(updatePreview, 1000)
updatePreview()
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key === 's') {
event.preventDefault()
saveCode()
}
})
previewSwitch.addEventListener('change', function(event) {
previewMode = event.target.checked
forceUpdatePreview()
})
</script>
</body>
</html>