Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/html/templates/songbook_edit/songbook_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<header>
<h1>Kreator Śpiewnika</h1>
<p class="subtitle">Stwórz swój własny śpiewnik wybierając ulubione piosenki</p>
<div class="header-actions" style="margin-top: 15px; display: flex; gap: 10px; justify-content: center;">
<button class="btn btn-secondary" onclick="document.getElementById('yamlLoadInput').click()">Wczytaj Śpiewnik (YAML)</button>
<input type="file" id="yamlLoadInput" accept=".yaml,.yml,.json" style="display: none;" onchange="loadFromYAMLFile(event)">
<button class="btn btn-secondary" onclick="downloadYAMLDirect()">Zapisz Śpiewnik (YAML)</button>
</div>
</header>

<!-- Songbook Description Section -->
Expand Down Expand Up @@ -140,6 +145,7 @@ <h3>Wybrane piosenki</h3>
</div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>
<script src="pdf_renderer.js"></script>
<script src="common.js"></script>
<script src="songbook_edit.js"></script>
Expand Down
106 changes: 106 additions & 0 deletions src/html/templates/songbook_edit/songbook_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,112 @@ function copyToClipboard() {
alert('Skopiowano do schowka!');
}

function downloadYAMLDirect() {
const yaml = generateYAMLString();
if (!yaml) return;

const songbookId = document.getElementById('songbookId').value.trim() || 'spiewnik';
const filename = `${songbookId}.songbook.yaml`;

const blob = new Blob([yaml], { type: 'text/yaml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}

function loadFromYAMLFile(event) {
const file = event.target.files[0];
if (!file) return;

const reader = new FileReader();
reader.onload = function(e) {
try {
const content = e.target.result;
let data;
if (typeof jsyaml !== 'undefined') {
data = jsyaml.load(content);
} else {
data = JSON.parse(content);
}

if (!data || !data.songbook) {
throw new Error("Nieprawidłowy format pliku: brak obiektu 'songbook'");
}

const sb = data.songbook;

// Populate form fields
document.getElementById('songbookId').value = sb.id || '';
document.getElementById('songbookTitle').value = sb.title || '';
document.getElementById('songbookSubtitle').value = sb.subtitle || '';
document.getElementById('songbookPublisher').value = sb.publisher || '';
document.getElementById('songbookPlace').value = sb.place || '';

userEditedId = !!sb.id; // Prevents auto-generation of ID

// Clear current selections
selectedSongIds.clear();
dynamicFilters = [];
filterCounter = 0;
activeAggregatedFilters.clear();

// Parse songs
if (sb.songs && Array.isArray(sb.songs)) {
sb.songs.forEach(item => {
if (item.glob) {
let found = false;
for (const song of allSongs) {
if (song.path === item.glob || `songs/**/${song.id}.xml` === item.glob) {
selectedSongIds.add(song.id);
found = true;
break;
}
}
// Fallback
if (!found) {
const match = item.glob.match(/([^\/]+)\.xml$/);
if (match) {
const songId = match[1];
if (songById.has(songId)) {
selectedSongIds.add(songId);
}
}
}
} else if (item.genre && item.genre.equals) {
addDynamicFilter('genre', item.genre.equals);
activeAggregatedFilters.set(`genre:${item.genre.equals}`, { type: 'genre', value: item.genre.equals });
} else if (item.artist && item.artist.equals) {
addDynamicFilter('artist', item.artist.equals);
activeAggregatedFilters.set(`artist:${item.artist.equals}`, { type: 'artist', value: item.artist.equals });
} else if (item.text_author && item.text_author.equals) {
addDynamicFilter('text_author', item.text_author.equals);
activeAggregatedFilters.set(`text_author:${item.text_author.equals}`, { type: 'text_author', value: item.text_author.equals });
}
});
}

// Render updates
renderDynamicFilters();
renderSongList();
renderSelectedSongs();

alert('Śpiewnik został pomyślnie wczytany!');

} catch (error) {
alert('Błąd podczas wczytywania pliku: ' + error.message);
}

// Clear input
event.target.value = '';
};
reader.readAsText(file);
}

async function renderPDF() {
const yaml = generateYAMLString();
if (!yaml) return;
Expand Down
Loading