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

@ -1,5 +1,6 @@
"""Used for editor/editor's preview""" """Used for editor/editor's preview"""
import os
import base64 import base64
from flask import Blueprint, request, render_template from flask import Blueprint, request, render_template
@ -7,7 +8,7 @@ editor_routes = Blueprint('simple_page', __name__, template_folder='templates')
@editor_routes.route("/editor") @editor_routes.route("/editor")
def editor(): def editor():
return render_template("editor/editor.html") return render_template("editor/editor.html", hostname=os.getenv("URL"))
@editor_routes.route("/preview/<b64>") @editor_routes.route("/preview/<b64>")
@editor_routes.route("/preview/") @editor_routes.route("/preview/")

View file

@ -13,6 +13,59 @@
<label for="previewSwitch">Preview</label> <label for="previewSwitch">Preview</label>
<input type="checkbox" id="previewSwitch"> <input type="checkbox" id="previewSwitch">
</div> </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> </body>
</html> </html>