feat: add toolbar + notifications

This commit is contained in:
2025-04-30 15:34:21 +02:00
committed by Louis Heredero
parent 609b32d09a
commit fef6c727cb
8 changed files with 285 additions and 35 deletions

View File

@ -1,4 +1,4 @@
import { flattenObj, getLanguageOptions } from "./utils.mjs"
import { findLanguage, flattenObj, getLanguageOptions } from "./utils.mjs"
export class Track {
constructor(table, idx, fields) {
@ -54,29 +54,36 @@ export class Track {
case "bool":
input.type = "checkbox"
input.checked = value
getValue = () => input.checked
const hotone = this.table.CONSTRAINTS[field.key]?.type == "hotone"
if (listeners && hotone) {
getValue = () => input.checked
const onehot = this.table.CONSTRAINTS[field.key]?.type == "onehot"
if (listeners && onehot) {
if (value) {
if (field.key in this.table.hotones) {
alert(`Error in metadata file: field ${field.name} is hotone but multiple tracks are enabled`)
if (field.key in this.table.onehots) {
this.table.editor.notify(
`Error in metadata file: field '${field.name}' is onehot but multiple tracks are enabled. Only the first one will be enabled`,
"error",
20000
)
value = false
} else {
this.table.onehots[field.key] = input
}
this.table.hotones[field.key] = input
}
input.addEventListener("click", e => {
if (!input.checked) {
e.preventDefault()
} else {
if (field.key in this.table.hotones) {
this.table.hotones[field.key].checked = false
this.table.hotones[field.key].dispatchEvent(new Event("change"))
if (field.key in this.table.onehots) {
this.table.onehots[field.key].checked = false
this.table.onehots[field.key].dispatchEvent(new Event("change"))
}
this.table.hotones[field.key] = input
this.table.onehots[field.key] = input
}
})
}
input.checked = value
break
case "sel":
@ -91,11 +98,31 @@ export class Track {
opt.value = option.value
input.appendChild(opt)
})
if (field.key === "language") {
const lang = findLanguage(value)
if (lang === null) {
this.table.editor.notify(
`Unknown language '${value}' for ${this.table.type} track ${this.idx}`,
"error",
20000
)
} else if (lang !== value) {
this.table.editor.notify(
`Language of ${this.table.type} track ${this.idx} was corrected (${value} -> ${lang})`,
"warning"
)
value = lang
}
}
input.value = value
default:
break
}
input.name = field.key + "[]"
input.dataset.key = field.key
if (this.table.CONSTRAINTS[field.key]?.type === "readonly") {
input.disabled = true
@ -135,7 +162,7 @@ export default class TracksTable {
}
CONSTRAINTS = {
"flags/default": {
type: "hotone"
type: "onehot"
},
"index": {
type: "readonly"
@ -160,7 +187,7 @@ export default class TracksTable {
this.fields = []
this.tracks = []
this.dataKey = dataKey
this.hotones = {}
this.onehots = {}
}
getFieldProps(key) {