Files
Oliver Hamlet ed24dd0bab Share js-yaml and Octokit between page scripts
js-yaml could probably be shared without the intermediate module, but is handled the same way as Octokit for consistency.
2026-01-11 17:52:18 +00:00

86 lines
2.3 KiB
JavaScript

'use strict';
import { load } from '/js/js-yaml.js';
import TOML from '@ltd/j-toml';
function upgradeOldYaml(yaml) {
if (yaml['Debug Verbosity'] && !yaml.enableDebugLogging) {
yaml.enableDebugLogging = yaml['Debug Verbosity'] > 0;
}
if (yaml['Update Masterlist'] && !yaml.updateMasterlist) {
yaml.updateMasterlist = yaml['Update Masterlist'];
}
if (yaml.Game && !yaml.game) {
yaml.game = yaml.Game;
}
if (yaml.Language && !yaml.language) {
yaml.language = yaml.Language;
}
if (yaml['Last Game'] && !yaml.lastGame) {
yaml.lastGame = yaml['Last Game'];
}
if (yaml.Games && !yaml.games) {
yaml.games = yaml.Games;
yaml.games.forEach(function(game) {
if (game.url) {
game.repo = game.url;
game.branch = 'master';
}
});
}
return yaml;
}
function yamlToToml(evt) {
try {
const settings = upgradeOldYaml(load(evt.target.value));
const options = {
indent: 2,
newline: '\n'
}
const toml = TOML.stringify(settings, options);
document.getElementById('output').value = toml;
const download = document.getElementById('download');
download.href = 'data:,' + toml;
download.textContent = ' - Download';
} catch (e) {
console.error(e);
document.getElementById('output').value = e;
document.getElementById('download').textContent = '';
}
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
const files = evt.dataTransfer.files; // FileList object.
const reader = new FileReader();
reader.onload = function(event) {
const input = document.getElementById('input');
input.value = event.target.result;
input.dispatchEvent(new Event('input'));
}
reader.readAsText(files[0], "UTF-8");
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
const input = document.getElementById('input');
input.addEventListener('input', yamlToToml);
input.addEventListener('dragover', handleDragOver, false);
input.addEventListener('drop', handleFileSelect, false);