@doitian/dsh-music 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +177 -160
- package/lib/feed.js +191 -0
- package/lib/index.js +7 -2
- package/lib/netease.js +12 -1
- package/lib/panel.html +426 -207
- package/lib/playback.js +343 -0
- package/lib/router.js +85 -7
- package/package.json +6 -17
package/lib/panel.html
CHANGED
|
@@ -182,9 +182,10 @@
|
|
|
182
182
|
.secondary { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12px; }
|
|
183
183
|
.secondary select, .secondary > button { padding: 4px 8px; font-size: 12px; }
|
|
184
184
|
#quality-info { color: var(--muted); }
|
|
185
|
+
.volume-control { display: flex; align-items: center; gap: 8px; flex: none; }
|
|
185
186
|
#mute { background: none; border-color: transparent; padding: 4px; color: var(--muted); }
|
|
186
187
|
#mute:hover:not(:disabled) { color: var(--fg); }
|
|
187
|
-
#volume {
|
|
188
|
+
#volume { width: 96px; flex: none; }
|
|
188
189
|
|
|
189
190
|
.info-card { display: flex; flex-direction: column; min-height: 0; }
|
|
190
191
|
.tabs { display: flex; gap: 14px; }
|
|
@@ -401,8 +402,10 @@
|
|
|
401
402
|
<select id="quality" data-i18n-title="qualityTitle" title="Streaming quality"></select>
|
|
402
403
|
<span id="quality-info"></span>
|
|
403
404
|
<span class="spacer"></span>
|
|
404
|
-
<
|
|
405
|
-
|
|
405
|
+
<div class="volume-control">
|
|
406
|
+
<button id="mute" data-i18n-title="mute" title="Mute"></button>
|
|
407
|
+
<input type="range" id="volume" min="0" max="1" step="0.01" value="0.8" />
|
|
408
|
+
</div>
|
|
406
409
|
</div>
|
|
407
410
|
|
|
408
411
|
<div class="card info-card">
|
|
@@ -480,6 +483,7 @@
|
|
|
480
483
|
<div class="toast" id="toast"></div>
|
|
481
484
|
|
|
482
485
|
<script src="__BASE__/vendor/qrcode.js"></script>
|
|
486
|
+
<script src="__BASE__/playback.js"></script>
|
|
483
487
|
<script>
|
|
484
488
|
// ------------------------------------------------------------------ setup
|
|
485
489
|
const BASE = '__BASE__';
|
|
@@ -807,6 +811,28 @@
|
|
|
807
811
|
el.innerHTML = icon(name, options);
|
|
808
812
|
}
|
|
809
813
|
|
|
814
|
+
/** The markup `setHtml` last wrote into each element. */
|
|
815
|
+
const painted = new WeakMap();
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Replace `el`'s markup only when it differs from what was last written.
|
|
819
|
+
* Rewriting identical rows on every update replaced every node in them,
|
|
820
|
+
* which dropped hover, focus and a click caught mid-swap.
|
|
821
|
+
*
|
|
822
|
+
* @returns {boolean} whether the markup was written.
|
|
823
|
+
*/
|
|
824
|
+
function setHtml(el, html) {
|
|
825
|
+
if (!el || painted.get(el) === html) return false;
|
|
826
|
+
painted.set(el, html);
|
|
827
|
+
el.innerHTML = html;
|
|
828
|
+
return true;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/** Set `el`'s text only when it changes; a rewrite clears a selection in it. */
|
|
832
|
+
function setText(el, text) {
|
|
833
|
+
if (el && el.textContent !== text) el.textContent = text;
|
|
834
|
+
}
|
|
835
|
+
|
|
810
836
|
function t(key, vars) {
|
|
811
837
|
let text = DICT[LANG]?.[key] ?? DICT.en[key] ?? key;
|
|
812
838
|
if (vars) {
|
|
@@ -842,7 +868,7 @@
|
|
|
842
868
|
render();
|
|
843
869
|
// The taste titles carry no `data-i18n`, and `render()` needs a state
|
|
844
870
|
// document, so they are painted here as well — otherwise a zh shell
|
|
845
|
-
// shows them in English until the first
|
|
871
|
+
// shows them in English until the first state lands.
|
|
846
872
|
paintTaste();
|
|
847
873
|
paintDjModelHeader();
|
|
848
874
|
paintDjModelToggle();
|
|
@@ -903,6 +929,10 @@
|
|
|
903
929
|
let lyricTrackId = null;
|
|
904
930
|
/** Bumped per request so a slow reply for a skipped track is discarded. */
|
|
905
931
|
let lyricRequest = 0;
|
|
932
|
+
/** Lyric answers by track, the last 30 kept, so a prefetched track paints without a loading flash. */
|
|
933
|
+
const lyricCache = new Map();
|
|
934
|
+
/** Lyric requests in flight, so playback and a prefetch share one. */
|
|
935
|
+
const lyricLoads = new Map();
|
|
906
936
|
/** Local-storage key for which pane the info card shows: `details` or `lyrics`. */
|
|
907
937
|
const INFO_VIEW_PREF = 'dsh-music.info.view';
|
|
908
938
|
let infoView = readInfoViewPref();
|
|
@@ -933,6 +963,30 @@
|
|
|
933
963
|
};
|
|
934
964
|
const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
935
965
|
|
|
966
|
+
/** Whether the host's event stream is open; see the boot section. */
|
|
967
|
+
let streaming = false;
|
|
968
|
+
/** The newest snapshot version adopted; see {@link newest}. */
|
|
969
|
+
let versionFloor = 0;
|
|
970
|
+
|
|
971
|
+
/**
|
|
972
|
+
* `next`, unless it is older than the state already shown — then the
|
|
973
|
+
* state shown. A request's answer and a pushed frame travel on different
|
|
974
|
+
* connections, so one read earlier can land later, and adopting it would
|
|
975
|
+
* undo the newer one with nothing coming to repair it. Answers are
|
|
976
|
+
* ordered by `version`, which moves with anything shown — not `rev`,
|
|
977
|
+
* which a setting or a resolved like leaves alone. Only while the stream
|
|
978
|
+
* is open: without it the next poll repairs any such swap, and a
|
|
979
|
+
* reconnect drops the floor, since a restarted host counts its versions
|
|
980
|
+
* from zero again.
|
|
981
|
+
*/
|
|
982
|
+
function newest(next) {
|
|
983
|
+
const version = Number(next?.version);
|
|
984
|
+
if (!Number.isFinite(version)) return next;
|
|
985
|
+
if (streaming && version < versionFloor) return state;
|
|
986
|
+
versionFloor = version;
|
|
987
|
+
return next;
|
|
988
|
+
}
|
|
989
|
+
|
|
936
990
|
let toastTimer;
|
|
937
991
|
function toast(message) {
|
|
938
992
|
const el = $('toast');
|
|
@@ -1021,133 +1075,54 @@
|
|
|
1021
1075
|
return localTransport;
|
|
1022
1076
|
}
|
|
1023
1077
|
|
|
1078
|
+
/**
|
|
1079
|
+
* Origin that serves audio bytes. Under the desktop shell this page runs on
|
|
1080
|
+
* the `dsh-app://` custom scheme, which Chromium cannot byte-range, so seeks
|
|
1081
|
+
* restart the track; the shell publishes its HTTP origin on the parent
|
|
1082
|
+
* window instead. Without one, relative URLs keep the page's own origin.
|
|
1083
|
+
*/
|
|
1084
|
+
function streamOrigin() {
|
|
1085
|
+
try {
|
|
1086
|
+
const base = window.parent?.__DSH_TRANSPORT__?.streamBaseUrl;
|
|
1087
|
+
if (typeof base === 'string' && base) return new URL(base).origin;
|
|
1088
|
+
} catch { /* a cross-origin parent, or a malformed base */ }
|
|
1089
|
+
return '';
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1024
1092
|
/**
|
|
1025
1093
|
* The fallback transport: this page owns an `<audio>` element.
|
|
1026
1094
|
*
|
|
1027
|
-
* It
|
|
1028
|
-
*
|
|
1029
|
-
*
|
|
1095
|
+
* It runs the shell engine's playback core, `playback.js`, which this
|
|
1096
|
+
* page loads from its own host rather than from the bundle — the bundle
|
|
1097
|
+
* is exactly what this fallback cannot trust. The core reconciles on
|
|
1098
|
+
* `transportRev` but enforces play/pause on every update, as the engine
|
|
1099
|
+
* does: a refused `play()` does not change the revision, so gating the
|
|
1100
|
+
* retry would stop the music for good after the first browser refusal.
|
|
1030
1101
|
*/
|
|
1031
1102
|
function createLocalTransport() {
|
|
1032
|
-
const
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
let disposed = false;
|
|
1040
|
-
|
|
1041
|
-
/** Start audio, and report a refusal by the autoplay policy as the engine does. */
|
|
1042
|
-
function attemptPlay() {
|
|
1043
|
-
if (busy || disposed) return;
|
|
1044
|
-
busy = true;
|
|
1045
|
-
audio.play().then(
|
|
1046
|
-
() => {
|
|
1047
|
-
busy = false;
|
|
1048
|
-
report({ blocked: false });
|
|
1049
|
-
},
|
|
1050
|
-
(error) => {
|
|
1051
|
-
busy = false;
|
|
1052
|
-
if (error?.name === 'NotAllowedError') report({ blocked: true });
|
|
1053
|
-
},
|
|
1054
|
-
);
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
function reconcile(next) {
|
|
1058
|
-
if (!next || next.transportRev === applied) return;
|
|
1059
|
-
applied = next.transportRev;
|
|
1060
|
-
if (next.pendingSeek !== null && next.pendingSeek !== undefined) {
|
|
1061
|
-
try { audio.currentTime = next.pendingSeek / 1000; } catch { /* not seekable yet */ }
|
|
1062
|
-
api('/seek-consumed', { method: 'POST', body: {} }).catch(() => {});
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
/**
|
|
1067
|
-
* Pick the resource to play. Every poll, not just on a transport change:
|
|
1068
|
-
* the resource identity includes the streaming quality, so a quality
|
|
1069
|
-
* switch has to be noticed without waiting for the revision to move.
|
|
1070
|
-
* The level rides in the URL because the same track at two levels is two
|
|
1071
|
-
* different files — range-requesting offsets across encodings would
|
|
1072
|
-
* corrupt the stream.
|
|
1073
|
-
*/
|
|
1074
|
-
function applySource(next) {
|
|
1075
|
-
const track = next.current;
|
|
1076
|
-
if (!track) {
|
|
1077
|
-
if (audio.dataset.trackKey !== undefined) {
|
|
1078
|
-
delete audio.dataset.trackKey;
|
|
1079
|
-
delete audio.dataset.trackId;
|
|
1080
|
-
audio.pause();
|
|
1081
|
-
audio.removeAttribute('src');
|
|
1082
|
-
audio.load();
|
|
1083
|
-
}
|
|
1084
|
-
return;
|
|
1085
|
-
}
|
|
1086
|
-
const level = next.audio?.preferred ?? 'exhigh';
|
|
1087
|
-
const key = track.id + ':' + level;
|
|
1088
|
-
if (audio.dataset.trackKey === key) return;
|
|
1089
|
-
|
|
1090
|
-
const sameTrack = audio.dataset.trackId === String(track.id);
|
|
1091
|
-
const resumeAt = sameTrack ? audio.currentTime : 0;
|
|
1092
|
-
audio.dataset.trackKey = key;
|
|
1093
|
-
audio.dataset.trackId = String(track.id);
|
|
1094
|
-
if (resumeAt > 0.5) {
|
|
1095
|
-
audio.addEventListener('loadedmetadata', () => {
|
|
1096
|
-
try { audio.currentTime = resumeAt; } catch { /* not seekable */ }
|
|
1097
|
-
}, { once: true });
|
|
1098
|
-
}
|
|
1099
|
-
audio.src = BASE + '/stream/' + track.id + '?level=' + encodeURIComponent(level);
|
|
1100
|
-
audio.load();
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
function apply(next) {
|
|
1104
|
-
if (disposed || !next) return;
|
|
1105
|
-
reconcile(next);
|
|
1106
|
-
applySource(next);
|
|
1107
|
-
audio.volume = next.muted ? 0 : next.volume;
|
|
1108
|
-
if (!next.current) return;
|
|
1109
|
-
if (next.playing && audio.paused) attemptPlay();
|
|
1110
|
-
else if (!next.playing && !audio.paused) audio.pause();
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
|
-
async function report(extra) {
|
|
1114
|
-
if (!state?.current) return;
|
|
1115
|
-
try {
|
|
1116
|
-
await api('/report', {
|
|
1103
|
+
const playback = window.__dshMusicPlayback.createPlayback({
|
|
1104
|
+
base: BASE,
|
|
1105
|
+
streamOrigin,
|
|
1106
|
+
call: api,
|
|
1107
|
+
report(extra) {
|
|
1108
|
+
if (!state?.current) return;
|
|
1109
|
+
api('/report', {
|
|
1117
1110
|
method: 'POST',
|
|
1118
|
-
body: {
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
duration: Number.isFinite(audio.duration) ? Math.round(audio.duration * 1000) : undefined,
|
|
1122
|
-
playing: !audio.paused,
|
|
1123
|
-
...extra,
|
|
1124
|
-
},
|
|
1125
|
-
});
|
|
1126
|
-
} catch { /* the host will be re-read on the next poll */ }
|
|
1127
|
-
}
|
|
1128
|
-
|
|
1129
|
-
audio.addEventListener('ended', () => report({ ended: true }));
|
|
1130
|
-
audio.addEventListener('error', () => {
|
|
1131
|
-
if (audio.dataset.trackId) report({ error: 'audio element error (track unavailable or network)' });
|
|
1111
|
+
body: { trackId: state.current.id, ...playback.progress(), ...extra },
|
|
1112
|
+
}).catch(() => { /* the next update carries the host's view */ });
|
|
1113
|
+
},
|
|
1132
1114
|
});
|
|
1133
|
-
audio.addEventListener('timeupdate', () => { paintProgress(); paintLyrics(); });
|
|
1115
|
+
playback.audio.addEventListener('timeupdate', () => { paintProgress(); paintLyrics(); });
|
|
1134
1116
|
|
|
1135
1117
|
return {
|
|
1136
1118
|
playback: true,
|
|
1137
1119
|
local: true,
|
|
1138
|
-
apply,
|
|
1139
|
-
sync() { apply(state); },
|
|
1140
|
-
playNow
|
|
1141
|
-
/** Exact position: this transport owns the element. */
|
|
1142
|
-
position:
|
|
1143
|
-
dispose
|
|
1144
|
-
disposed = true;
|
|
1145
|
-
try {
|
|
1146
|
-
audio.pause();
|
|
1147
|
-
audio.removeAttribute('src');
|
|
1148
|
-
audio.remove();
|
|
1149
|
-
} catch { /* the document may already be gone */ }
|
|
1150
|
-
},
|
|
1120
|
+
apply: playback.apply,
|
|
1121
|
+
sync() { playback.apply(state); },
|
|
1122
|
+
playNow: playback.playNow,
|
|
1123
|
+
/** Exact position: this transport owns the element. A held seek counts as already there. */
|
|
1124
|
+
position: playback.position,
|
|
1125
|
+
dispose: playback.dispose,
|
|
1151
1126
|
};
|
|
1152
1127
|
}
|
|
1153
1128
|
|
|
@@ -1159,14 +1134,14 @@
|
|
|
1159
1134
|
} catch { /* a failing transport must not break rendering */ }
|
|
1160
1135
|
}
|
|
1161
1136
|
|
|
1162
|
-
/** Ask the active transport to act now instead of on its next
|
|
1137
|
+
/** Ask the active transport to act now instead of on its next update. */
|
|
1163
1138
|
function pokeEngine() {
|
|
1164
1139
|
try { transport().sync?.(); } catch { /* best effort across documents */ }
|
|
1165
1140
|
}
|
|
1166
1141
|
|
|
1167
1142
|
async function control(body) {
|
|
1168
1143
|
try {
|
|
1169
|
-
state = await api('/control', { method: 'POST', body });
|
|
1144
|
+
state = newest(await api('/control', { method: 'POST', body }));
|
|
1170
1145
|
render();
|
|
1171
1146
|
applyTransport();
|
|
1172
1147
|
} catch (error) { toast(error.message); }
|
|
@@ -1176,9 +1151,11 @@
|
|
|
1176
1151
|
/**
|
|
1177
1152
|
* Playback position.
|
|
1178
1153
|
*
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
* report
|
|
1154
|
+
* The transport reads its own element when it can — the local one, and
|
|
1155
|
+
* the shell engine, whose document this page can reach. An engine from
|
|
1156
|
+
* an older bundle has no `position()`, so its last report to the host is
|
|
1157
|
+
* used, with the time since added back; the host pushes no frame for a
|
|
1158
|
+
* report that only moves the position, so that report can be minutes old.
|
|
1182
1159
|
*/
|
|
1183
1160
|
function positionMs() {
|
|
1184
1161
|
const active = transport();
|
|
@@ -1233,22 +1210,27 @@
|
|
|
1233
1210
|
function render() {
|
|
1234
1211
|
if (!state) return;
|
|
1235
1212
|
const track = state.current;
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
$('
|
|
1239
|
-
|
|
1240
|
-
|
|
1213
|
+
const cover = track?.picUrl || '';
|
|
1214
|
+
// Setting the same `src` again restarts the image load.
|
|
1215
|
+
if ($('cover').dataset.src !== cover) {
|
|
1216
|
+
$('cover').dataset.src = cover;
|
|
1217
|
+
$('cover').src = cover;
|
|
1218
|
+
}
|
|
1219
|
+
$('cover').style.visibility = cover ? 'visible' : 'hidden';
|
|
1220
|
+
setText($('np-title'), track ? track.name : t('nothingPlaying'));
|
|
1221
|
+
setText($('np-artists'), track ? (track.artists || []).join(' / ') : '');
|
|
1222
|
+
setText($('np-album'), track ? track.album || '' : '');
|
|
1241
1223
|
// Details and lyrics belong to the track, and this page fetches them
|
|
1242
|
-
// itself on every
|
|
1224
|
+
// itself on every update — not from the local transport's `applySource`,
|
|
1243
1225
|
// which only ever runs when this page owns the audio element. In the
|
|
1244
1226
|
// normal case the shell engine plays, so that path is dead and the
|
|
1245
1227
|
// pane stayed empty forever.
|
|
1246
1228
|
syncInfo(track?.id ?? null);
|
|
1247
1229
|
loadIntro();
|
|
1248
1230
|
prefetchNextDetails();
|
|
1231
|
+
prefetchNextLyrics();
|
|
1249
1232
|
setIcon($('play'), state.playing ? 'pause' : 'play', { filled: true });
|
|
1250
|
-
$('mode')
|
|
1251
|
-
$('counts').textContent = state.counts ? `(${state.counts.queued})` : '';
|
|
1233
|
+
setText($('mode'), { list: t('modeList'), single: t('modeSingle'), shuffle: t('modeShuffle') }[state.mode] || t('modeList'));
|
|
1252
1234
|
$('queue-clear').disabled = (state.queue?.length ?? 0) <= (state.current ? 1 : 0);
|
|
1253
1235
|
$('dj-toggle').classList.toggle('on', Boolean(state.dj?.enabled));
|
|
1254
1236
|
paintHistory();
|
|
@@ -1258,21 +1240,23 @@
|
|
|
1258
1240
|
|
|
1259
1241
|
// Account chip
|
|
1260
1242
|
const account = state.account;
|
|
1261
|
-
|
|
1243
|
+
const chip = account
|
|
1262
1244
|
? `${account.avatar ? `<img src="${esc(account.avatar)}" alt="">` : ''}<span>${esc(account.nickname)}${account.vip ? ' · VIP' : ''}</span>`
|
|
1263
1245
|
: `<button id="signin">${esc(t('signin'))}</button>`;
|
|
1264
|
-
|
|
1265
|
-
|
|
1246
|
+
if (setHtml($('account'), chip)) {
|
|
1247
|
+
const signin = $('signin');
|
|
1248
|
+
if (signin) signin.onclick = openLogin;
|
|
1249
|
+
}
|
|
1266
1250
|
|
|
1267
1251
|
// DJ status
|
|
1268
1252
|
const dj = state.dj || {};
|
|
1269
|
-
$('dj-status')
|
|
1253
|
+
setHtml($('dj-status'), dj.enabled || dj.lastPlan
|
|
1270
1254
|
? `<div class="plan">${dj.busy ? esc(t('djPlanning')) : dj.error ? `AI DJ: ${esc(dj.error)}` : ''}${
|
|
1271
1255
|
dj.lastPlan
|
|
1272
1256
|
? `<div><b>+${dj.lastPlan.added}</b> ${esc(dj.lastPlan.vibe || '')}</div><div class="muted">${esc((dj.lastPlan.names || []).slice(-3).join(' · '))}</div>`
|
|
1273
1257
|
: ''
|
|
1274
1258
|
}${dj.modelError && !dj.lastPlan?.route ? `<div class="muted">${esc(t('modelTierUnavailable'))}: ${esc(dj.modelError)}</div>` : ''}${dj.lastPlan?.source === 'heuristic' ? `<div class="muted">${esc(t('heuristics'))}</div>` : ''}</div>`
|
|
1275
|
-
: '';
|
|
1259
|
+
: '');
|
|
1276
1260
|
|
|
1277
1261
|
if (autoplayBlocked()) {
|
|
1278
1262
|
if (!$('gesture')) {
|
|
@@ -1319,8 +1303,15 @@
|
|
|
1319
1303
|
function renderQueue() {
|
|
1320
1304
|
if (!state) return;
|
|
1321
1305
|
const queue = state.queue || [];
|
|
1306
|
+
const total = state.counts ? `(${state.counts.queued})` : '';
|
|
1322
1307
|
if (queue.length === 0) {
|
|
1323
|
-
$('
|
|
1308
|
+
setText($('counts'), total);
|
|
1309
|
+
// Emptying the list dropped its scroll position, so a queue restored
|
|
1310
|
+
// with the same song playing has to be revealed again.
|
|
1311
|
+
if (setHtml($('queue'), `<div class="empty">${esc(t('queueEmpty'))}</div>`)) {
|
|
1312
|
+
revealedRow = null;
|
|
1313
|
+
revealing = null;
|
|
1314
|
+
}
|
|
1324
1315
|
return;
|
|
1325
1316
|
}
|
|
1326
1317
|
// A window of at most MAX_VISIBLE_ROWS rows that always includes the
|
|
@@ -1330,10 +1321,8 @@
|
|
|
1330
1321
|
const cursor = state.index < 0 ? 0 : state.index;
|
|
1331
1322
|
const start = Math.max(0, Math.min(cursor - 4, queue.length - MAX_VISIBLE_ROWS));
|
|
1332
1323
|
const end = Math.min(queue.length, start + MAX_VISIBLE_ROWS);
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
}
|
|
1336
|
-
$('queue').innerHTML = queue
|
|
1324
|
+
setText($('counts'), end - start < queue.length ? `(${start + 1}–${end} / ${queue.length})` : total);
|
|
1325
|
+
setHtml($('queue'), queue
|
|
1337
1326
|
.slice(start, end)
|
|
1338
1327
|
.map((track, offset) => {
|
|
1339
1328
|
const index = start + offset;
|
|
@@ -1348,7 +1337,93 @@
|
|
|
1348
1337
|
<span class="acts"><button data-remove="${index}" title="${esc(t('remove'))}">${icon('x', { small: true })}</button></span>
|
|
1349
1338
|
</div>`;
|
|
1350
1339
|
})
|
|
1351
|
-
.join('');
|
|
1340
|
+
.join(''));
|
|
1341
|
+
revealPlayingRow();
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
/** The playing row the queue last scrolled to, as `index:trackId`. */
|
|
1345
|
+
let revealedRow = null;
|
|
1346
|
+
|
|
1347
|
+
/**
|
|
1348
|
+
* A smooth reveal still under way: the list edge the row is headed for
|
|
1349
|
+
* and the scroll position that lands it there.
|
|
1350
|
+
*/
|
|
1351
|
+
let revealing = null;
|
|
1352
|
+
|
|
1353
|
+
// A reveal ends when the list comes to rest, or when the user scrolls it
|
|
1354
|
+
// themselves.
|
|
1355
|
+
for (const type of ['scrollend', 'wheel', 'touchstart', 'pointerdown', 'keydown']) {
|
|
1356
|
+
$('queue').addEventListener(type, () => { revealing = null; }, { passive: true });
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
/**
|
|
1360
|
+
* Bring the playing row into the queue's view when a new song starts.
|
|
1361
|
+
*
|
|
1362
|
+
* Only on a change of song: the list is re-rendered on every update, and
|
|
1363
|
+
* scrolling then would yank it back while the user browses what comes
|
|
1364
|
+
* next. Only the list itself scrolls — stacked in a narrow window the list
|
|
1365
|
+
* does not scroll on its own, and moving the whole page for it would lose
|
|
1366
|
+
* whatever the user is reading on the stage. A row already in view stays
|
|
1367
|
+
* put; the first paint jumps, later songs glide.
|
|
1368
|
+
*/
|
|
1369
|
+
function revealPlayingRow() {
|
|
1370
|
+
const list = $('queue');
|
|
1371
|
+
const row = list.querySelector('.row.playing');
|
|
1372
|
+
if (!row || list.offsetParent === null) return;
|
|
1373
|
+
const key = `${state.index}:${state.current?.id}`;
|
|
1374
|
+
if (key === revealedRow) {
|
|
1375
|
+
followReveal(list, row);
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
const first = revealedRow === null;
|
|
1379
|
+
revealedRow = key;
|
|
1380
|
+
revealing = null;
|
|
1381
|
+
const view = list.getBoundingClientRect();
|
|
1382
|
+
const box = row.getBoundingClientRect();
|
|
1383
|
+
const edge = box.top < view.top ? 'top' : box.bottom > view.bottom ? 'bottom' : null;
|
|
1384
|
+
if (!edge) return;
|
|
1385
|
+
const top = revealTarget(list, row, edge);
|
|
1386
|
+
if (first || matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
1387
|
+
list.scrollTo({ top, behavior: 'instant' });
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
revealing = { edge, top };
|
|
1391
|
+
list.scrollTo({ top, behavior: 'smooth' });
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
/**
|
|
1395
|
+
* Keep a glide aimed at the row when a repaint moves it.
|
|
1396
|
+
*
|
|
1397
|
+
* Songs landing in a short queue slide the painted window, and the row
|
|
1398
|
+
* with it, while the list is still scrolling to where the row was. A glide
|
|
1399
|
+
* that has arrived is left alone: from then on a moving row is a change of
|
|
1400
|
+
* queue, not of song.
|
|
1401
|
+
*/
|
|
1402
|
+
function followReveal(list, row) {
|
|
1403
|
+
if (!revealing) return;
|
|
1404
|
+
if (Math.abs(list.scrollTop - revealing.top) < 1) {
|
|
1405
|
+
revealing = null;
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
const top = revealTarget(list, row, revealing.edge);
|
|
1409
|
+
if (Math.abs(top - revealing.top) < 1) return;
|
|
1410
|
+
revealing.top = top;
|
|
1411
|
+
list.scrollTo({ top, behavior: 'smooth' });
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
/**
|
|
1415
|
+
* The scroll position that puts the row at one edge of the list, with
|
|
1416
|
+
* half a row of its neighbours showing so it does not land flush against
|
|
1417
|
+
* the card's edge.
|
|
1418
|
+
*/
|
|
1419
|
+
function revealTarget(list, row, edge) {
|
|
1420
|
+
const view = list.getBoundingClientRect();
|
|
1421
|
+
const box = row.getBoundingClientRect();
|
|
1422
|
+
const margin = box.height / 2;
|
|
1423
|
+
const top = edge === 'top'
|
|
1424
|
+
? list.scrollTop + box.top - view.top - margin
|
|
1425
|
+
: list.scrollTop + box.bottom - view.bottom + margin;
|
|
1426
|
+
return Math.max(0, Math.min(top, list.scrollHeight - list.clientHeight));
|
|
1352
1427
|
}
|
|
1353
1428
|
|
|
1354
1429
|
/**
|
|
@@ -1384,7 +1459,7 @@
|
|
|
1384
1459
|
function paintBoosts() {
|
|
1385
1460
|
const boosts = state?.boosts ?? [];
|
|
1386
1461
|
const now = Date.now();
|
|
1387
|
-
$('boosts')
|
|
1462
|
+
setHtml($('boosts'), boosts.length
|
|
1388
1463
|
? `<span>${esc(t('boostsTitle'))}:</span>` +
|
|
1389
1464
|
boosts
|
|
1390
1465
|
.map((boost) => {
|
|
@@ -1392,14 +1467,14 @@
|
|
|
1392
1467
|
return `<span class="boost">${icon(boost.direction === 'more' ? 'more' : 'less', { small: true })} ${esc(boost.name)} · ${esc(t('boostMinutes', { n: minutes }))}<button data-unboost="${boost.id}" title="${esc(t('boostClear'))}">${icon('x', { small: true })}</button></span>`;
|
|
1393
1468
|
})
|
|
1394
1469
|
.join('')
|
|
1395
|
-
: '';
|
|
1470
|
+
: '');
|
|
1396
1471
|
}
|
|
1397
1472
|
|
|
1398
1473
|
/** Boost a track, or clear its boost with `none`, and adopt the host's answer. */
|
|
1399
1474
|
async function sendBoost(trackId, direction, button) {
|
|
1400
1475
|
if (button) button.disabled = true;
|
|
1401
1476
|
try {
|
|
1402
|
-
state = await api('/boost', { method: 'POST', body: { trackId, direction } });
|
|
1477
|
+
state = newest(await api('/boost', { method: 'POST', body: { trackId, direction } }));
|
|
1403
1478
|
render();
|
|
1404
1479
|
applyTransport();
|
|
1405
1480
|
toast(t(direction === 'more' ? 'boostedMore' : direction === 'less' ? 'boostedLess' : 'boostCleared'));
|
|
@@ -1431,7 +1506,7 @@
|
|
|
1431
1506
|
// mid-flight is locked out, exactly as the transport switchers do.
|
|
1432
1507
|
if (button) button.disabled = true;
|
|
1433
1508
|
try {
|
|
1434
|
-
state = await api('/queue', { method: 'POST', body: { action: 'remove', index } });
|
|
1509
|
+
state = newest(await api('/queue', { method: 'POST', body: { action: 'remove', index } }));
|
|
1435
1510
|
// Read the answer off the new queue: an index that named a row someone
|
|
1436
1511
|
// else had already dropped must not claim a removal that did not happen.
|
|
1437
1512
|
return !(state.queue || []).some((entry) => entry.id === track.id);
|
|
@@ -1457,7 +1532,7 @@
|
|
|
1457
1532
|
// promised.
|
|
1458
1533
|
if (button) button.disabled = true;
|
|
1459
1534
|
try {
|
|
1460
|
-
state = await api('/taste', { method: 'POST', body: { trackId: track.id, level } });
|
|
1535
|
+
state = newest(await api('/taste', { method: 'POST', body: { trackId: track.id, level } }));
|
|
1461
1536
|
render();
|
|
1462
1537
|
applyTransport();
|
|
1463
1538
|
return true;
|
|
@@ -1478,8 +1553,8 @@
|
|
|
1478
1553
|
if (document.activeElement !== $('seek')) {
|
|
1479
1554
|
$('seek').value = duration > 0 ? String(Math.round((position / duration) * 1000)) : '0';
|
|
1480
1555
|
}
|
|
1481
|
-
$('t-cur')
|
|
1482
|
-
$('t-dur')
|
|
1556
|
+
setText($('t-cur'), fmt(position));
|
|
1557
|
+
setText($('t-dur'), fmt(duration));
|
|
1483
1558
|
}
|
|
1484
1559
|
|
|
1485
1560
|
// ------------------------------------------------------ details + lyrics
|
|
@@ -1570,18 +1645,70 @@
|
|
|
1570
1645
|
return detailsLoads.get(id);
|
|
1571
1646
|
}
|
|
1572
1647
|
|
|
1648
|
+
/**
|
|
1649
|
+
* The track that plays after this one, wrapping past the end as list
|
|
1650
|
+
* playback does. Shuffle has no known next track, and repeat-one's next
|
|
1651
|
+
* is this one, so both answer null.
|
|
1652
|
+
*/
|
|
1653
|
+
function nextTrack() {
|
|
1654
|
+
if (!state?.current || state.mode !== 'list') return null;
|
|
1655
|
+
const next = state.queue?.[(state.index + 1) % state.queue.length];
|
|
1656
|
+
return next?.id !== state.current.id ? next : null;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1573
1659
|
/**
|
|
1574
1660
|
* Fetch the next track's details ahead, so they show the moment it
|
|
1575
|
-
* starts. Lazy like the introduction: only
|
|
1576
|
-
*
|
|
1577
|
-
* Shuffle has no known next track, and repeat-one's next is this one.
|
|
1661
|
+
* starts. Lazy like the introduction: only after the shown track's
|
|
1662
|
+
* details have painted, and only while the details are shown.
|
|
1578
1663
|
*/
|
|
1579
1664
|
function prefetchNextDetails() {
|
|
1580
1665
|
if (infoView !== 'details' || !detailsTrackId || !detailsCache.has(detailsTrackId)) return;
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
if (
|
|
1584
|
-
|
|
1666
|
+
const next = nextTrack();
|
|
1667
|
+
if (!next?.id) return;
|
|
1668
|
+
if (detailsCache.has(next.id)) {
|
|
1669
|
+
prefetchNextIntro();
|
|
1670
|
+
return;
|
|
1671
|
+
}
|
|
1672
|
+
if (detailsLoads.has(next.id)) return;
|
|
1673
|
+
fetchDetails(next.id).then(prefetchNextIntro).catch(() => { /* the track's own load retries */ });
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
/**
|
|
1677
|
+
* Write the next track's introduction ahead. Each one is a model call,
|
|
1678
|
+
* so it waits until this track is past the skip window, when the next
|
|
1679
|
+
* is likely to be heard. At the end of the queue the DJ tops up before
|
|
1680
|
+
* playback wraps, so the first track is not written for while it is on.
|
|
1681
|
+
*/
|
|
1682
|
+
function prefetchNextIntro() {
|
|
1683
|
+
if (infoView !== 'details' || !state?.playing || detailsTrackId !== state.current?.id) return;
|
|
1684
|
+
// Match the host's skip window: 30 seconds, or a quarter of a longer track.
|
|
1685
|
+
if (positionMs() < Math.max(30_000, durationMs() * 0.25)) return;
|
|
1686
|
+
if (state.dj?.enabled && state.index + 1 >= state.queue.length) return;
|
|
1687
|
+
const next = nextTrack();
|
|
1688
|
+
if (next?.id && detailsCache.has(next.id)) void fetchIntro(next.id, LANG);
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
/** One track's lyrics, fetched once however many ask, and kept. */
|
|
1692
|
+
function fetchLyrics(id) {
|
|
1693
|
+
if (!lyricLoads.has(id)) {
|
|
1694
|
+
const load = api('/lyric/' + id)
|
|
1695
|
+
.then((data) => {
|
|
1696
|
+
lyricCache.set(id, data);
|
|
1697
|
+
if (lyricCache.size > 30) lyricCache.delete(lyricCache.keys().next().value);
|
|
1698
|
+
return data;
|
|
1699
|
+
})
|
|
1700
|
+
.finally(() => lyricLoads.delete(id));
|
|
1701
|
+
lyricLoads.set(id, load);
|
|
1702
|
+
}
|
|
1703
|
+
return lyricLoads.get(id);
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
/** Fetch the next track's lyrics ahead, once the shown ones have painted and while the lyrics are shown. */
|
|
1707
|
+
function prefetchNextLyrics() {
|
|
1708
|
+
if (infoView !== 'lyrics' || lyricTrackId !== state?.current?.id || !lyricCache.has(lyricTrackId)) return;
|
|
1709
|
+
const next = nextTrack();
|
|
1710
|
+
if (!next?.id || lyricCache.has(next.id) || lyricLoads.has(next.id)) return;
|
|
1711
|
+
fetchLyrics(next.id).catch(() => { /* the track's own load retries */ });
|
|
1585
1712
|
}
|
|
1586
1713
|
|
|
1587
1714
|
async function loadDetails(id) {
|
|
@@ -1590,6 +1717,7 @@
|
|
|
1590
1717
|
const cached = detailsCache.get(id);
|
|
1591
1718
|
if (cached) {
|
|
1592
1719
|
paintDetails(cached);
|
|
1720
|
+
setTimeout(afterDetails);
|
|
1593
1721
|
return;
|
|
1594
1722
|
}
|
|
1595
1723
|
$('details').innerHTML = `<p class="hint">${esc(t('loading'))}</p>`;
|
|
@@ -1597,12 +1725,24 @@
|
|
|
1597
1725
|
const info = await fetchDetails(id);
|
|
1598
1726
|
if (request !== detailsRequest) return;
|
|
1599
1727
|
paintDetails(info);
|
|
1728
|
+
setTimeout(afterDetails);
|
|
1600
1729
|
} catch (error) {
|
|
1601
1730
|
if (request !== detailsRequest) return;
|
|
1602
1731
|
$('details').innerHTML = `<p class="hint">${esc(error.message)}</p>`;
|
|
1603
1732
|
}
|
|
1604
1733
|
}
|
|
1605
1734
|
|
|
1735
|
+
/**
|
|
1736
|
+
* What waits for the details to paint: the introduction and the next
|
|
1737
|
+
* track's details. Its own turn, so neither is asked for before the
|
|
1738
|
+
* facts are on screen, and not tied to the next update from the host,
|
|
1739
|
+
* which may be minutes away now that it only comes on a change.
|
|
1740
|
+
*/
|
|
1741
|
+
function afterDetails() {
|
|
1742
|
+
loadIntro();
|
|
1743
|
+
prefetchNextDetails();
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1606
1746
|
/** Paint the shown track's details again: a language switch, an introduction, the switch. */
|
|
1607
1747
|
function repaintDetails(options) {
|
|
1608
1748
|
const info = detailsCache.get(detailsTrackId);
|
|
@@ -1612,25 +1752,32 @@
|
|
|
1612
1752
|
/**
|
|
1613
1753
|
* Ask for the shown track's introduction, once its details are painted.
|
|
1614
1754
|
*
|
|
1615
|
-
* Lazy on purpose: a model call takes seconds, so it waits
|
|
1616
|
-
*
|
|
1617
|
-
* it
|
|
1755
|
+
* Lazy on purpose: a model call takes seconds, so it waits until the
|
|
1756
|
+
* details have painted — playback and the NetEase facts never wait on
|
|
1757
|
+
* it. Prefetching the next introduction uses the same cache.
|
|
1618
1758
|
*/
|
|
1619
|
-
|
|
1759
|
+
function loadIntro() {
|
|
1620
1760
|
if (infoView !== 'details' || !detailsTrackId || !detailsCache.has(detailsTrackId)) return;
|
|
1621
|
-
const
|
|
1622
|
-
|
|
1761
|
+
const cached = intros.get(`${detailsTrackId}:${LANG}`);
|
|
1762
|
+
if (typeof cached === 'object') introsUnavailable = !cached.text;
|
|
1763
|
+
void fetchIntro(detailsTrackId, LANG);
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
async function fetchIntro(id, lang) {
|
|
1767
|
+
const key = `${id}:${lang}`;
|
|
1623
1768
|
if (intros.has(key)) return;
|
|
1624
1769
|
intros.set(key, 'pending');
|
|
1625
1770
|
if (intros.size > 60) intros.delete(intros.keys().next().value);
|
|
1626
1771
|
let text = '';
|
|
1627
1772
|
try {
|
|
1628
|
-
const answer = await api(`/intro/${id}?lang=${
|
|
1773
|
+
const answer = await api(`/intro/${id}?lang=${lang}`);
|
|
1629
1774
|
text = typeof answer?.text === 'string' ? answer.text : '';
|
|
1630
1775
|
} catch { /* NetEase's prose stands in */ }
|
|
1631
1776
|
intros.set(key, { text });
|
|
1632
|
-
|
|
1633
|
-
|
|
1777
|
+
if (infoView === 'details' && detailsTrackId === id && LANG === lang) {
|
|
1778
|
+
introsUnavailable = !text;
|
|
1779
|
+
repaintDetails({ keepScroll: true });
|
|
1780
|
+
}
|
|
1634
1781
|
}
|
|
1635
1782
|
|
|
1636
1783
|
/** NetEase dates are midnight in China; read them there, as YYYY-MM-DD. */
|
|
@@ -1719,35 +1866,45 @@
|
|
|
1719
1866
|
const request = ++lyricRequest;
|
|
1720
1867
|
lyric = null;
|
|
1721
1868
|
lyricLine = -1;
|
|
1869
|
+
if (lyricCache.has(id)) {
|
|
1870
|
+
paintLyricData(lyricCache.get(id));
|
|
1871
|
+
setTimeout(prefetchNextLyrics);
|
|
1872
|
+
return;
|
|
1873
|
+
}
|
|
1722
1874
|
$('lyrics').innerHTML = `<p class="hint">${esc(t('loading'))}</p>`;
|
|
1723
1875
|
try {
|
|
1724
|
-
const data = await
|
|
1876
|
+
const data = await fetchLyrics(id);
|
|
1725
1877
|
// Skipping tracks faster than NetEase answers would otherwise let an
|
|
1726
1878
|
// earlier reply land on the newer track's pane.
|
|
1727
|
-
if (request !== lyricRequest) return;
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
return;
|
|
1731
|
-
}
|
|
1732
|
-
const lines = [];
|
|
1733
|
-
for (const raw of data.lrc.split('\n')) {
|
|
1734
|
-
const matches = [...raw.matchAll(/\[(\d+):(\d+(?:[.:]\d+)?)\]/g)];
|
|
1735
|
-
const text = raw.replace(/\[[^\]]*\]/g, '').trim();
|
|
1736
|
-
if (matches.length === 0) continue;
|
|
1737
|
-
for (const match of matches) {
|
|
1738
|
-
const seconds = Number(match[1]) * 60 + Number(String(match[2]).replace(':', '.'));
|
|
1739
|
-
lines.push({ at: seconds * 1000, text });
|
|
1740
|
-
}
|
|
1741
|
-
}
|
|
1742
|
-
lines.sort((a, b) => a.at - b.at);
|
|
1743
|
-
lyric = lines;
|
|
1744
|
-
$('lyrics').innerHTML = lines.map((line, index) => `<p data-line="${index}">${esc(line.text || '♪')}</p>`).join('');
|
|
1879
|
+
if (request !== lyricRequest || lyricTrackId !== id) return;
|
|
1880
|
+
paintLyricData(data);
|
|
1881
|
+
setTimeout(prefetchNextLyrics);
|
|
1745
1882
|
} catch (error) {
|
|
1746
|
-
if (request !== lyricRequest) return;
|
|
1883
|
+
if (request !== lyricRequest || lyricTrackId !== id) return;
|
|
1747
1884
|
$('lyrics').innerHTML = `<p class="hint">${esc(error.message)}</p>`;
|
|
1748
1885
|
}
|
|
1749
1886
|
}
|
|
1750
1887
|
|
|
1888
|
+
function paintLyricData(data) {
|
|
1889
|
+
if (data.noLyric || !data.lrc) {
|
|
1890
|
+
$('lyrics').innerHTML = `<p class="hint">${esc(t('instrumental'))}</p>`;
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
const lines = [];
|
|
1894
|
+
for (const raw of data.lrc.split('\n')) {
|
|
1895
|
+
const matches = [...raw.matchAll(/\[(\d+):(\d+(?:[.:]\d+)?)\]/g)];
|
|
1896
|
+
const text = raw.replace(/\[[^\]]*\]/g, '').trim();
|
|
1897
|
+
if (matches.length === 0) continue;
|
|
1898
|
+
for (const match of matches) {
|
|
1899
|
+
const seconds = Number(match[1]) * 60 + Number(String(match[2]).replace(':', '.'));
|
|
1900
|
+
lines.push({ at: seconds * 1000, text });
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
lines.sort((a, b) => a.at - b.at);
|
|
1904
|
+
lyric = lines;
|
|
1905
|
+
$('lyrics').innerHTML = lines.map((line, index) => `<p data-line="${index}">${esc(line.text || '♪')}</p>`).join('');
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1751
1908
|
function paintLyrics() {
|
|
1752
1909
|
if (!lyric || infoView !== 'lyrics') return;
|
|
1753
1910
|
const at = positionMs();
|
|
@@ -2026,7 +2183,7 @@
|
|
|
2026
2183
|
|
|
2027
2184
|
async function saveModelRoute(provider, model, message) {
|
|
2028
2185
|
try {
|
|
2029
|
-
state = await api('/dj', { method: 'POST', body: { provider, model } });
|
|
2186
|
+
state = newest(await api('/dj', { method: 'POST', body: { provider, model } }));
|
|
2030
2187
|
render();
|
|
2031
2188
|
await loadModelPicker();
|
|
2032
2189
|
toast(message);
|
|
@@ -2100,12 +2257,12 @@
|
|
|
2100
2257
|
const last = state?.audio?.last;
|
|
2101
2258
|
const preferred = state?.audio?.preferred;
|
|
2102
2259
|
if (!last || (preferred && last.requested !== preferred && last.trackId !== state?.current?.id)) {
|
|
2103
|
-
$('quality-info')
|
|
2260
|
+
setText($('quality-info'), '');
|
|
2104
2261
|
return;
|
|
2105
2262
|
}
|
|
2106
2263
|
const kbps = last.br ? ` · ${Math.round(last.br / 1000)} kbps` : '';
|
|
2107
|
-
$('quality-info')
|
|
2108
|
-
`▶ ${String(last.type ?? '').toUpperCase()}${kbps}` + (last.downgraded ? ` (${last.served}, ${t('downgraded')})` : '');
|
|
2264
|
+
setText($('quality-info'),
|
|
2265
|
+
`▶ ${String(last.type ?? '').toUpperCase()}${kbps}` + (last.downgraded ? ` (${last.served}, ${t('downgraded')})` : ''));
|
|
2109
2266
|
}
|
|
2110
2267
|
|
|
2111
2268
|
async function loadQuality() {
|
|
@@ -2125,7 +2282,7 @@
|
|
|
2125
2282
|
|
|
2126
2283
|
$('quality').onchange = async (event) => {
|
|
2127
2284
|
try {
|
|
2128
|
-
state = await api('/quality', { method: 'POST', body: { level: event.target.value } });
|
|
2285
|
+
state = newest(await api('/quality', { method: 'POST', body: { level: event.target.value } }));
|
|
2129
2286
|
render();
|
|
2130
2287
|
applyTransport();
|
|
2131
2288
|
pokeEngine();
|
|
@@ -2138,6 +2295,14 @@
|
|
|
2138
2295
|
|
|
2139
2296
|
// ------------------------------------------------------------------ wiring
|
|
2140
2297
|
$('play').onclick = () => control({ action: 'toggle' });
|
|
2298
|
+
document.addEventListener('keydown', (event) => {
|
|
2299
|
+
if (event.key !== ' ' || event.defaultPrevented || event.isComposing ||
|
|
2300
|
+
event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
2301
|
+
const focused = document.activeElement;
|
|
2302
|
+
if (focused?.isContentEditable || focused?.matches?.('input, textarea, select, button, [role="textbox"]')) return;
|
|
2303
|
+
event.preventDefault();
|
|
2304
|
+
if (!event.repeat) void control({ action: 'toggle' });
|
|
2305
|
+
});
|
|
2141
2306
|
$('next').onclick = () => control({ action: 'next' });
|
|
2142
2307
|
$('prev').onclick = () => control({ action: 'previous' });
|
|
2143
2308
|
$('mode').onclick = () => control({ action: 'mode', value: { list: 'shuffle', shuffle: 'single', single: 'list' }[state?.mode || 'list'] });
|
|
@@ -2154,6 +2319,8 @@
|
|
|
2154
2319
|
}
|
|
2155
2320
|
};
|
|
2156
2321
|
$('volume').onchange = (event) => control({ action: 'volume', value: Number(event.target.value) });
|
|
2322
|
+
// Sent at once: the transport silences on the first seek and debounces
|
|
2323
|
+
// the resume, so delaying here would only let the old position play on.
|
|
2157
2324
|
$('seek').onchange = (event) => {
|
|
2158
2325
|
control({ action: 'seek', value: Math.round((Number(event.target.value) / 1000) * durationMs()) });
|
|
2159
2326
|
};
|
|
@@ -2196,7 +2363,7 @@
|
|
|
2196
2363
|
const button = event?.currentTarget;
|
|
2197
2364
|
if (button) button.disabled = true;
|
|
2198
2365
|
try {
|
|
2199
|
-
state = await api('/scrobble', { method: 'POST', body: { enabled: next } });
|
|
2366
|
+
state = newest(await api('/scrobble', { method: 'POST', body: { enabled: next } }));
|
|
2200
2367
|
render();
|
|
2201
2368
|
toast(t(next ? 'scrobbleOn' : 'scrobbleOff'));
|
|
2202
2369
|
} catch (error) {
|
|
@@ -2209,17 +2376,17 @@
|
|
|
2209
2376
|
$('dj-toggle').onclick = async () => {
|
|
2210
2377
|
try {
|
|
2211
2378
|
const data = await api('/dj', { method: 'POST', body: { enabled: !state?.dj?.enabled, prompt: $('dj-prompt').value } });
|
|
2212
|
-
state = data;
|
|
2379
|
+
state = newest(data);
|
|
2213
2380
|
render();
|
|
2214
2381
|
toast(state.dj?.enabled ? t('djOn') : t('djOff'));
|
|
2215
2382
|
} catch (error) { toast(error.message); }
|
|
2216
2383
|
};
|
|
2217
|
-
$('dj-prompt').onchange = () => api('/dj', { method: 'POST', body: { prompt: $('dj-prompt').value, enabled: state?.dj?.enabled } }).then((data) => { state = data; render(); }).catch((error) => toast(error.message));
|
|
2384
|
+
$('dj-prompt').onchange = () => api('/dj', { method: 'POST', body: { prompt: $('dj-prompt').value, enabled: state?.dj?.enabled } }).then((data) => { state = newest(data); render(); }).catch((error) => toast(error.message));
|
|
2218
2385
|
|
|
2219
2386
|
// Not row-by-row removal, which reads each row as a dislike.
|
|
2220
2387
|
$('queue-clear').onclick = async () => {
|
|
2221
2388
|
try {
|
|
2222
|
-
state = await api('/queue', { method: 'POST', body: { action: 'clear', keepCurrent: true } });
|
|
2389
|
+
state = newest(await api('/queue', { method: 'POST', body: { action: 'clear', keepCurrent: true } }));
|
|
2223
2390
|
render();
|
|
2224
2391
|
pokeEngine();
|
|
2225
2392
|
} catch (error) { toast(failureText(error)); }
|
|
@@ -2243,7 +2410,7 @@
|
|
|
2243
2410
|
// The confirm names the click, not that side effect of it.
|
|
2244
2411
|
if (await removeFromList(Number(remove.dataset.remove), remove)) toast(t('removed'));
|
|
2245
2412
|
} else if (jump !== undefined) {
|
|
2246
|
-
state = await api('/queue', { method: 'POST', body: { action: 'jump', index: Number(jump) } });
|
|
2413
|
+
state = newest(await api('/queue', { method: 'POST', body: { action: 'jump', index: Number(jump) } }));
|
|
2247
2414
|
} else return;
|
|
2248
2415
|
render();
|
|
2249
2416
|
pokeEngine();
|
|
@@ -2251,29 +2418,81 @@
|
|
|
2251
2418
|
};
|
|
2252
2419
|
|
|
2253
2420
|
// ------------------------------------------------------------------ boot
|
|
2421
|
+
/**
|
|
2422
|
+
* The host pushes its state over `/events` whenever something here would
|
|
2423
|
+
* change. `/state` is re-read every `POLL_MS` only while that stream is
|
|
2424
|
+
* down — a browser without EventSource, a host that predates the route,
|
|
2425
|
+
* a reconnect under way — and every `STREAM_POLL_MS` while it is up, as
|
|
2426
|
+
* a safety net that also ages the boosts' minutes left.
|
|
2427
|
+
*/
|
|
2428
|
+
const POLL_MS = 1500;
|
|
2429
|
+
const STREAM_POLL_MS = 30_000;
|
|
2430
|
+
/** A stream the browser gave up on (an error answer) is opened again after this. */
|
|
2431
|
+
const RELISTEN_MS = 10_000;
|
|
2432
|
+
let events = null;
|
|
2433
|
+
let receivedAt = 0;
|
|
2434
|
+
|
|
2435
|
+
function adopt(next) {
|
|
2436
|
+
receivedAt = Date.now();
|
|
2437
|
+
if (newest(next) !== next) return;
|
|
2438
|
+
state = next;
|
|
2439
|
+
render();
|
|
2440
|
+
applyTransport();
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2254
2443
|
async function refresh() {
|
|
2255
2444
|
try {
|
|
2256
|
-
|
|
2257
|
-
render();
|
|
2258
|
-
applyTransport();
|
|
2445
|
+
adopt(await api('/state'));
|
|
2259
2446
|
} catch (error) {
|
|
2260
|
-
$('np-title')
|
|
2447
|
+
setText($('np-title'), t('connectFailed') + error.message);
|
|
2261
2448
|
}
|
|
2262
2449
|
}
|
|
2263
2450
|
|
|
2451
|
+
function listen() {
|
|
2452
|
+
const Source = window.EventSource;
|
|
2453
|
+
if (typeof Source !== 'function') return;
|
|
2454
|
+
events = new Source(API + '/events');
|
|
2455
|
+
events.onopen = () => {
|
|
2456
|
+
streaming = true;
|
|
2457
|
+
// The host may have restarted, and its versions with it.
|
|
2458
|
+
versionFloor = 0;
|
|
2459
|
+
};
|
|
2460
|
+
events.onerror = () => {
|
|
2461
|
+
streaming = false;
|
|
2462
|
+
// A dropped connection is retried by the browser; an error status
|
|
2463
|
+
// closes the stream for good, so it is opened again from here.
|
|
2464
|
+
if (events.readyState === Source.CLOSED) {
|
|
2465
|
+
events = null;
|
|
2466
|
+
setTimeout(listen, RELISTEN_MS);
|
|
2467
|
+
}
|
|
2468
|
+
};
|
|
2469
|
+
events.addEventListener('state', (event) => {
|
|
2470
|
+
try { adopt(JSON.parse(event.data)); } catch { /* the next frame or poll repairs it */ }
|
|
2471
|
+
});
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2264
2474
|
paintStaticIcons();
|
|
2265
2475
|
applyLocale();
|
|
2266
2476
|
watchShellLocale();
|
|
2267
2477
|
syncShellTheme();
|
|
2268
2478
|
refresh();
|
|
2269
|
-
|
|
2479
|
+
listen();
|
|
2480
|
+
setInterval(() => {
|
|
2481
|
+
if (!streaming || Date.now() - receivedAt >= STREAM_POLL_MS) refresh();
|
|
2482
|
+
// A refused play changes nothing on the host, so no update would come
|
|
2483
|
+
// to retry it: a page that owns its audio retries on this clock.
|
|
2484
|
+
else applyTransport();
|
|
2485
|
+
}, POLL_MS);
|
|
2270
2486
|
void loadModelPicker();
|
|
2271
2487
|
void loadQuality();
|
|
2272
|
-
// Progress and lyric highlighting
|
|
2273
|
-
//
|
|
2274
|
-
//
|
|
2275
|
-
setInterval(() => { paintProgress(); paintLyrics(); }, 250);
|
|
2276
|
-
window.addEventListener('beforeunload', () => {
|
|
2488
|
+
// Progress and lyric highlighting follow the audio element, which lives
|
|
2489
|
+
// in the shell document and reports nothing here, so they need their own
|
|
2490
|
+
// repaint clock.
|
|
2491
|
+
setInterval(() => { paintProgress(); paintLyrics(); prefetchNextIntro(); }, 250);
|
|
2492
|
+
window.addEventListener('beforeunload', () => {
|
|
2493
|
+
clearInterval(qrTimer);
|
|
2494
|
+
events?.close();
|
|
2495
|
+
});
|
|
2277
2496
|
</script>
|
|
2278
2497
|
</body>
|
|
2279
2498
|
</html>
|