Files
WB-ToonTools/app.js
2025-11-17 10:45:52 +09:00

80 lines
2.3 KiB
JavaScript

/*
* Copyright (C) 2025 Watanabebashi Alpha
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { encode } from './node_modules/@toon-format/toon/dist/index.js';
const jsonInEl = document.getElementById('jsonIn');
const toonOutEl = document.getElementById('toonOut');
const convertBtn = document.getElementById('convertBtn');
const clearBtn = document.getElementById('clearBtn');
const copyBtn = document.getElementById('copyBtn');
let isError = false;
const resetCopyButton = () => {
copyBtn.textContent = 'コピー';
copyBtn.classList.remove('btn-success');
copyBtn.classList.add('btn-outline-secondary');
};
convertBtn.addEventListener('click', () => {
const text = jsonInEl.value.trim();
if (!text) {
toonOutEl.textContent = '';
isError = false;
return;
}
resetCopyButton();
try {
const parsedJson = JSON.parse(text);
const toonData = encode(parsedJson);
toonOutEl.value = toonData;
isError = false;
} catch (e) {
isError = true;
if (e instanceof SyntaxError) {
toonOutEl.value = `JSONの形式が正しくありません`;
} else {
toonOutEl.value = `変換エラー`;
}
}
});
clearBtn.addEventListener('click', () => {
jsonInEl.value = '';
toonOutEl.value = '';
isError = false;
resetCopyButton();
});
copyBtn.addEventListener('click', () => {
const textToCopy = toonOutEl.value;
if (isError || !textToCopy) {
return;
}
navigator.clipboard.writeText(textToCopy).then(() => {
copyBtn.textContent = 'コピーしました!';
copyBtn.classList.remove('btn-outline-secondary');
copyBtn.classList.add('btn-success');
}).catch(err => {
console.error('クリップボードへのコピーに失敗しました: ', err);
});
});