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
3 changes: 3 additions & 0 deletions src/renderer/panel/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ <h2 id="first-run-setup-title">Get ready for your first service</h2>
<button type="button" id="lt-next-version-btn" class="lt-next-version-btn" hidden onclick="cycleLiveBibleVersion(1)"><svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M17 1l4 4-4 4"/><path d="M3 11V9a4 4 0 0 1 4-4h14"/><path d="M7 23l-4-4 4-4"/><path d="M21 13v2a4 4 0 0 1-4 4H3"/></svg><span>Next</span></button>
</div>
<div id="version-bar"></div>
<div id="song-services-bar" aria-label="Services"></div>
<div id="song-list"></div>
</div>
<div id="sidebar-width-handle"><div class="grip"></div></div>
Expand Down Expand Up @@ -3687,6 +3688,7 @@ <h3 id="panel-inline-style-194" data-i18n="new_song_create_new">Create New</h3>
<script src="../../shared/lyrics-clean.js"></script>
<script src="../../shared/spoken-reference.js"></script>
<script src="../../shared/song-import.js"></script>
<script src="../../shared/song-services.js"></script>
<script src="../../shared/song-cloud-config.js"></script>
<script src="../../shared/song-cloud.js"></script>
<script src="../../shared/displays-view.js"></script>
Expand All @@ -3709,6 +3711,7 @@ <h3 id="panel-inline-style-194" data-i18n="new_song_create_new">Create New</h3>
<script src="./scripts/ui/skeletons.js"></script>
<script src="./scripts/ui/custom-shortcuts.js"></script>
<script src="./scripts/ui/song-history.js"></script>
<script src="./scripts/ui/song-services.js"></script>
<script src="./scripts/integrations/auto-backup.js"></script>
<script src="./scripts/integrations/update-check.js"></script>
<script src="./scripts/integrations/phone-remote.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/panel/scripts/core/bootstrap-and-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4284,7 +4284,7 @@
initializeDefaultStyles();
if (runInit && !hasInitialized) populateLanguageSelect();

songs = songRecords.map(hydrateSongFromRecord);
songs = window.SP_SONG_SERVICES.sortSongs(songRecords.map(hydrateSongFromRecord));
bibles = {};
bibleRecords.forEach(record => {
if (!record || !record.id) return;
Expand Down
9 changes: 7 additions & 2 deletions src/renderer/panel/scripts/core/state-and-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,10 @@
normalizedLines,
searchableText,
createdAt: isNew ? now : (song.createdAt || now),
updatedAt: now
updatedAt: now,
// Place in the list (set by dragging) and the services the song is filed under
order: Number.isFinite(song.order) ? song.order : undefined,
services: Array.isArray(song.services) ? song.services : []
};
}

Expand All @@ -493,7 +496,9 @@
text: record.text || '',
searchableText: record.searchableText || normalizeSearchText(`${record.title || ''}\n${record.text || ''}`),
createdAt: record.createdAt || Date.now(),
updatedAt: record.updatedAt || Date.now()
updatedAt: record.updatedAt || Date.now(),
order: Number.isFinite(record.order) ? record.order : undefined,
services: Array.isArray(record.services) ? record.services : []
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/renderer/panel/scripts/library/render-and-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,11 @@
projectLive(true);
close();
}});
(songServiceState.services || []).forEach((svc) => {
const on = window.SP_SONG_SERVICES.inService(songs[itemIndex] || {}, svc.id);
items.push({ label: `${on ? '✓ ' : ''}Service: ${svc.name}`, icon: '', action: () => { toggleSongInService(songs[itemIndex], svc.id); close(); } });
});
if ((songServiceState.services || []).length) items.push({ type: 'sep' });
items.push({ label: t('common_rename_song'), icon: '✎', action: () => {
const target = songs[itemIndex];
if (!target) { close(); return; }
Expand Down Expand Up @@ -1041,6 +1046,8 @@
}
}
const frag = document.createDocumentFragment();
const isSongsView = sidebarTab === 'songs' && buttonContextTab === 'songs';
if (typeof renderSongServicesBar === 'function') renderSongServicesBar();
// Bible chapters render as a number grid per book; add-to-setlist stays
// on the right-click menu.
const chapterGrid = sidebarTab === 'bible' && buttonContextTab === 'bible';
Expand All @@ -1055,6 +1062,7 @@
};
displayList.forEach((s, i) => {
if (q && !getItemSearchableText(s).includes(q)) return;
if (isSongsView && !window.SP_SONG_SERVICES.inService(s, songServiceState.active)) return;
const itemIndex = (sidebarTab === 'bible' && displayIndices) ? displayIndices[i] : i;
const { book, chap } = chapterGrid ? extractBookAndChapter(s) : {};
if (chapterGrid && chap) {
Expand Down Expand Up @@ -1109,6 +1117,7 @@
div.addEventListener('dragleave', () => div.classList.remove('drop-target'));
div.addEventListener('drop', (e) => { e.preventDefault(); div.classList.remove('drop-target'); if (dragIndex == null || dragIndex === i) return; const item = schedule.splice(dragIndex, 1)[0]; schedule.splice(i, 0, item); saveState(); saveToStorageDebounced(); renderSongs() });
} else {
if (isSongsView && !q) attachSongDrag(div, s, right);
const add = document.createElement('span');
add.style.color = 'var(--success)'; add.style.marginRight = '12px'; add.style.cursor = 'pointer'; add.textContent = '+';
add.onclick = (e) => addToSet(itemIndex, e);
Expand Down Expand Up @@ -1152,6 +1161,7 @@
frag.appendChild(div);
});
list.appendChild(frag);
if (isSongsView && !list.children.length && songServiceState.active !== 'all') list.appendChild(buildSongsEmptyNote());
if (typeof window._refreshNavMirrorResults === 'function') {
window._refreshNavMirrorResults();
}
Expand Down
156 changes: 156 additions & 0 deletions src/renderer/panel/scripts/ui/song-services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// ── Lyrics list: drag to re-arrange, and file songs under services ─────────────────
// Logic is in src/shared/song-services.js. A song's place in the list is song.order and its services are
// song.services; both are saved with the song. The list of services is kept in localStorage.
let songServiceState = window.SP_SONG_SERVICES.load(localStorage);
let draggedSongId = null;

function saveSongServiceState() { window.SP_SONG_SERVICES.save(localStorage, songServiceState); }

// Saves songs without sending them to the cloud library: only their order or services changed
function persistSongRecords(list) {
if (!list.length) return;
dbPutMany(STORE_SONGS, list.map((s) => buildSongRecord(s, { isNew: false }))).catch(() => {});
}

function setActiveSongService(id) {
songServiceState.active = id;
saveSongServiceState();
renderSongs();
}

function createSongService() {
showConfirm('New service', 'Name of the service, e.g. Sunday morning', (name) => {
const svc = window.SP_SONG_SERVICES.addService(songServiceState, name);
if (!svc) { showToast('Enter a name for the service'); return; }
songServiceState.active = svc.id;
saveSongServiceState();
renderSongs();
}, true, '');
}

function renameActiveSongService() {
const svc = songServiceState.services.find((x) => x.id === songServiceState.active);
if (!svc) return;
showConfirm('Rename service', 'New name', (name) => {
if (!window.SP_SONG_SERVICES.renameService(songServiceState, svc.id, name)) { showToast('That name is empty or already used'); return; }
saveSongServiceState();
renderSongs();
}, true, svc.name);
}

function deleteActiveSongService() {
const svc = songServiceState.services.find((x) => x.id === songServiceState.active);
if (!svc) return;
showConfirm('Delete service', `Delete "${svc.name}"? The songs stay in your list; they are only taken out of this service.`, () => {
persistSongRecords(window.SP_SONG_SERVICES.removeService(songServiceState, songs, svc.id));
saveSongServiceState();
renderSongs();
showToast('Service deleted');
});
}

function toggleSongInService(song, serviceId) {
if (!song) return;
const now = window.SP_SONG_SERVICES.toggleService(song, serviceId);
const svc = songServiceState.services.find((x) => x.id === serviceId);
persistSongRecords([song]);
renderSongs();
showToast(`${now ? 'Added to' : 'Removed from'} ${svc ? svc.name : 'service'}`);
}

function renderSongServicesBar() {
const bar = document.getElementById('song-services-bar');
if (!bar) return;
bar.replaceChildren();
if (sidebarTab !== 'songs') return;
const chip = (id, label, count) => {
const b = document.createElement('button');
b.type = 'button';
b.className = 'song-service-chip' + (songServiceState.active === id ? ' active' : '');
b.textContent = label;
if (count != null) { const n = document.createElement('span'); n.className = 'song-service-count'; n.textContent = String(count); b.appendChild(n); }
b.onclick = () => setActiveSongService(id);
return b;
};
bar.appendChild(chip('all', 'All songs', songs.length));
songServiceState.services.forEach((svc) => {
const b = chip(svc.id, svc.name, songs.filter((s) => window.SP_SONG_SERVICES.inService(s, svc.id)).length);
b.title = 'Drop a song here to add it to this service';
b.addEventListener('dragover', (e) => { if (draggedSongId) { e.preventDefault(); b.classList.add('drop-target'); } });
b.addEventListener('dragleave', () => b.classList.remove('drop-target'));
b.addEventListener('drop', (e) => {
e.preventDefault();
b.classList.remove('drop-target');
const song = songs.find((s) => s.id === draggedSongId);
if (song && !window.SP_SONG_SERVICES.inService(song, svc.id)) toggleSongInService(song, svc.id);
});
bar.appendChild(b);
});
const add = document.createElement('button');
add.type = 'button';
add.className = 'song-service-chip song-service-add';
add.textContent = '+ Service';
add.title = 'Create a service, e.g. Sunday morning';
add.onclick = createSongService;
bar.appendChild(add);
if (songServiceState.active !== 'all') {
const tools = document.createElement('span');
tools.className = 'song-service-tools';
[['Rename', renameActiveSongService], ['Delete', deleteActiveSongService]].forEach(([label, fn]) => {
const b = document.createElement('button');
b.type = 'button';
b.className = 'song-service-link';
b.textContent = label;
b.onclick = fn;
tools.appendChild(b);
});
bar.appendChild(tools);
}
}

function buildSongsEmptyNote() {
const note = document.createElement('div');
note.className = 'song-services-empty';
note.textContent = 'No songs in this service yet. Right-click a song and pick this service, or drag a song onto the service above.';
return note;
}

// Drag a row to move it; drop it on another row to take that row's place
function attachSongDrag(row, song, right) {
const handle = document.createElement('span');
handle.className = 'drag-handle';
handle.textContent = '☰';
handle.title = 'Drag to re-arrange';
right.appendChild(handle);
row.draggable = true;
row.addEventListener('dragstart', (e) => {
draggedSongId = song.id;
row.classList.add('dragging');
try { e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setData('text/plain', song.title || ''); } catch (_) { /* some webviews block it */ }
});
row.addEventListener('dragend', () => {
draggedSongId = null;
row.classList.remove('dragging');
document.querySelectorAll('#song-list .drop-target, #song-services-bar .drop-target').forEach((el) => el.classList.remove('drop-target'));
});
row.addEventListener('dragover', (e) => { if (draggedSongId && draggedSongId !== song.id) { e.preventDefault(); row.classList.add('drop-target'); } });
row.addEventListener('dragleave', () => row.classList.remove('drop-target'));
row.addEventListener('drop', (e) => {
e.preventDefault();
row.classList.remove('drop-target');
if (draggedSongId) moveSongInList(draggedSongId, song.id);
});
}

function moveSongInList(fromId, toId) {
// Row numbers shift when the list is re-ordered: follow the selected and the live song by identity
const liveSong = (typeof livePointer !== 'undefined' && livePointer && livePointer.kind === 'songs') ? songs[livePointer.index] : null;
const changed = window.SP_SONG_SERVICES.moveSong(songs, fromId, toId);
if (!changed.length) return;
if (buttonContextTab === 'songs' && currentItem) currentIndex = songs.indexOf(currentItem);
if (liveSong) livePointer = { ...livePointer, index: songs.indexOf(liveSong) };
persistSongRecords(changed);
saveState();
saveToStorageDebounced();
renderSongs();
}
17 changes: 17 additions & 0 deletions src/renderer/panel/styles/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -8634,3 +8634,20 @@
gap: 12px;
justify-content: flex-end;
}

/* Services bar above the Lyrics list */
#song-services-bar { display: none; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 8px; }
body[data-sidebar-tab="songs"] #song-services-bar { display: flex; }
.song-service-chip {
display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px;
border: 1px solid var(--border, rgba(255,255,255,0.15)); background: transparent; color: var(--text-secondary);
font: inherit; font-size: 12px; cursor: pointer;
}
.song-service-chip:hover { color: var(--text-primary, inherit); }
.song-service-chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.song-service-chip.song-service-add { border-style: dashed; }
.song-service-count { opacity: 0.7; font-size: 11px; }
.song-service-tools { margin-left: auto; display: inline-flex; gap: 10px; }
.song-service-link { background: none; border: 0; padding: 0; font: inherit; font-size: 12px; color: var(--text-secondary); cursor: pointer; text-decoration: underline; }
.song-services-empty { padding: 16px 12px; font-size: 12px; color: var(--text-secondary); line-height: 1.5; }
#song-list .song-item[draggable="true"] .drag-handle { cursor: grab; }
97 changes: 97 additions & 0 deletions src/shared/song-services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* Scripture Pod — song order and services.
*
* Songs can be dragged into any order (song.order) and filed under one or more services such as
* "Sunday morning" (song.services holds service ids). The list of services lives in localStorage.
*
* Used by src/renderer/panel/scripts/ui/song-services.js, the hydrate/build steps in
* core/state-and-storage.js and core/bootstrap-and-init.js, and test/song-services.test.js.
*/
(function (root) {
var STORAGE_KEY = 'scripturePod.songServices.v1';

// Songs with a saved order come first, in that order; songs never moved follow, oldest first
function sortSongs(songs) {
return songs
.map(function (s, i) { return { s: s, i: i }; })
.sort(function (a, b) {
var ao = Number.isFinite(a.s.order), bo = Number.isFinite(b.s.order);
if (ao && bo) return a.s.order - b.s.order || a.i - b.i;
if (ao !== bo) return ao ? -1 : 1;
return (a.s.createdAt || 0) - (b.s.createdAt || 0) || a.i - b.i;
})
.map(function (x) { return x.s; });
}

// Puts the dragged song where the target song is, renumbers every song, and returns the songs whose number changed
function moveSong(songs, fromId, toId) {
var from = songs.findIndex(function (s) { return s.id === fromId; });
var to = songs.findIndex(function (s) { return s.id === toId; });
if (from < 0 || to < 0 || from === to) return [];
var moved = songs.splice(from, 1)[0];
songs.splice(to, 0, moved);
var changed = [];
songs.forEach(function (s, i) { if (s.order !== i) { s.order = i; changed.push(s); } });
return changed;
}

function inService(song, serviceId) {
if (!serviceId || serviceId === 'all') return true;
return Array.isArray(song.services) && song.services.indexOf(serviceId) >= 0;
}

// Adds the song to the service, or takes it out if it is already there. Returns true when it is now in it.
function toggleService(song, serviceId) {
var list = Array.isArray(song.services) ? song.services.slice() : [];
var at = list.indexOf(serviceId);
if (at >= 0) list.splice(at, 1); else list.push(serviceId);
song.services = list;
return at < 0;
}

function load(storage) {
try {
var v = JSON.parse(storage.getItem(STORAGE_KEY) || '{}') || {};
var services = (Array.isArray(v.services) ? v.services : []).filter(function (x) { return x && x.id && x.name; });
var active = services.some(function (x) { return x.id === v.active; }) ? v.active : 'all';
return { services: services, active: active };
} catch (e) { return { services: [], active: 'all' }; }
}
function save(storage, state) {
try { storage.setItem(STORAGE_KEY, JSON.stringify(state)); } catch (e) { /* storage unavailable */ }
}

function addService(state, name) {
var clean = String(name || '').trim().slice(0, 40);
if (!clean) return null;
var dupe = state.services.find(function (x) { return x.name.toLowerCase() === clean.toLowerCase(); });
if (dupe) return dupe;
var svc = { id: 'svc_' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6), name: clean };
state.services.push(svc);
return svc;
}
function renameService(state, id, name) {
var clean = String(name || '').trim().slice(0, 40);
var svc = state.services.find(function (x) { return x.id === id; });
if (!svc || !clean) return false;
if (state.services.some(function (x) { return x.id !== id && x.name.toLowerCase() === clean.toLowerCase(); })) return false;
svc.name = clean;
return true;
}
// Removes the service and returns the songs that were filed under it (their lists are already updated)
function removeService(state, songs, id) {
state.services = state.services.filter(function (x) { return x.id !== id; });
if (state.active === id) state.active = 'all';
var touched = [];
songs.forEach(function (s) {
if (Array.isArray(s.services) && s.services.indexOf(id) >= 0) {
s.services = s.services.filter(function (x) { return x !== id; });
touched.push(s);
}
});
return touched;
}

var api = { sortSongs: sortSongs, moveSong: moveSong, inService: inService, toggleService: toggleService, load: load, save: save, addService: addService, renameService: renameService, removeService: removeService };
if (typeof module !== 'undefined' && module.exports) module.exports = api;
if (root) root.SP_SONG_SERVICES = api;
})(typeof window !== 'undefined' ? window : globalThis);
Loading
Loading