feat: add saving

This commit is contained in:
2025-04-28 19:07:39 +02:00
parent 477e8951a9
commit d19ab90f38
2 changed files with 94 additions and 26 deletions

View File

@ -4,6 +4,8 @@ let audioTable
/** @type TracksTable */
let subtitleTable
let data = {}
function flattenObj(obj) {
const res = {}
Object.entries(obj).forEach(([key, value]) => {
@ -152,39 +154,64 @@ class TracksTable {
}
function fetchData(filename) {
fetch("/api/file", {
method: "POST",
body: JSON.stringify({
file: filename
}),
headers: {
"Content-Type": "application/json"
}
}).then(res => {
fetch(`/api/file/${filename}`).then(res => {
if (res.ok) {
return res.json()
}
return null
}).then(res => {
if (res !== null) {
displayData(res)
data = res
displayData()
}
})
}
function displayData(data) {
function displayData() {
document.getElementById("title").value = data.title
audioTable.showTracks(data.audio_tracks)
subtitleTable.showTracks(data.subtitle_tracks)
}
function setDirty() {
dirty = true
document.getElementById("unsaved").classList.add("show")
}
function save(filename) {
fetch(`/api/file/${filename}`, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
}).then(res => {
if (res.ok) {
dirty = false
document.getElementById("unsaved").classList.remove("show")
} else {
alert(`Error ${res.status}: ${res.statusText}`)
}
})
}
function editTrack(listKey, trackIdx, key, value) {
const keyParts = key.split("/")
let obj = data[listKey][trackIdx]
for (const part of keyParts.slice(0, -1)) {
obj = obj[part]
}
obj[keyParts[keyParts.length - 1]] = value
setDirty()
}
window.addEventListener("load", () => {
audioTable = new TracksTable(document.getElementById("audio-tracks"), (i, key, value) => {
console.log(i, key, value)
editTrack("audio_tracks", i, key, value)
})
subtitleTable = new TracksTable(document.getElementById("subtitle-tracks"), (i, key, value) => {
console.log(i, key, value)
editTrack("subtitle_tracks", i, key, value)
})
const params = new URLSearchParams(window.location.search)
@ -192,4 +219,13 @@ window.addEventListener("load", () => {
document.getElementById("filename").innerText = file
fetchData(file)
})
window.addEventListener("keydown", e => {
if (e.key === "s" && e.ctrlKey) {
e.preventDefault()
const params = new URLSearchParams(window.location.search)
const file = params.get("f")
save(file)
}
})