0ad/binaries/data/mods/public/gui/hotkeys/HotkeyMetadata.js
Dunedan 93ce94655d
Use @stylistic/brace-style for eslint
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace
style for JavaScript code. This plugin was however updated the last time
over 9 years ago and will be incompatible with ESLint v10, as that
[removes `context.getSourceCode()`][1], the plugin relies on.

To keep the eslint config working with ESLint v10, this replaces
`eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule
from `@stylistic/eslint-plugin`, a package we already use.

While `@stylistic/brace-style` doesn't offer an option to format braces
in exactly the same way as before, the "allman" style seems to be the
one closest to the existing code.

[1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members
[2]: https://eslint.style/rules/brace-style
2026-01-12 21:33:52 +01:00

70 lines
2 KiB
JavaScript

/**
*
*/
class HotkeyMetadata
{
constructor()
{
this.DEFAULT_CATEGORY = "other";
this.categories = {};
this.hotkeys = {};
this.parseSpec();
}
parseSpec()
{
const files = this.getFiles();
let hotkey_i = 0;
const categories = {
[this.DEFAULT_CATEGORY]: {
"name": translate(this.DefaultCategoryString),
"desc": translate(this.DefaultCategoryString),
}
};
for (const file of files)
{
const data = Engine.ReadJSONFile(file);
if (data.categories)
for (const cat in data.categories)
categories[cat] = data.categories[cat];
if (data.hotkeys)
for (const hotkey in data.hotkeys)
{
this.hotkeys[hotkey] = data.hotkeys[hotkey];
this.hotkeys[hotkey].order = hotkey_i++;
this.hotkeys[hotkey].categories = data.hotkeys[hotkey].categories || [this.DEFAULT_CATEGORY];
}
if (data.mapped_hotkeys)
for (const cat in data.mapped_hotkeys)
for (const hotkey in data.mapped_hotkeys[cat])
{
if (data.mapped_hotkeys[cat][hotkey].categories)
warn("Categories will be overwritten for mapped hotkey " + hotkey);
this.hotkeys[hotkey] = data.mapped_hotkeys[cat][hotkey];
this.hotkeys[hotkey].order = hotkey_i++;
this.hotkeys[hotkey].categories = [cat];
}
}
// Sort categories (JS objects are (in this case) sorted by insertion order).
this.categories = {};
const keys = Object.keys(categories).sort((a, b) =>
{
if (a === this.DEFAULT_CATEGORY || b === this.DEFAULT_CATEGORY)
return a === this.DEFAULT_CATEGORY ? 1 : -1;
if (categories[a].order === undefined || categories[b].order === undefined)
return categories[a].order === undefined ? -1 : 1; // Likely to keep alphabetical order.
return categories[a].order - categories[b].order;
});
for (const key of keys)
this.categories[key] = categories[key];
// TODO: validate that categories exist.
}
getFiles()
{
return Engine.ListDirectoryFiles("gui/hotkeys/spec/", "*.json");
}
}
HotkeyMetadata.prototype.DefaultCategoryString = markForTranslation("Other Hotkeys");