@doitian/dsh-music 1.0.0 → 1.1.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/panel.html +305 -171
- package/lib/playback.js +343 -0
- package/lib/router.js +48 -6
- package/package.json +6 -17
package/lib/panel.html
CHANGED
|
@@ -480,6 +480,7 @@
|
|
|
480
480
|
<div class="toast" id="toast"></div>
|
|
481
481
|
|
|
482
482
|
<script src="__BASE__/vendor/qrcode.js"></script>
|
|
483
|
+
<script src="__BASE__/playback.js"></script>
|
|
483
484
|
<script>
|
|
484
485
|
// ------------------------------------------------------------------ setup
|
|
485
486
|
const BASE = '__BASE__';
|
|
@@ -807,6 +808,28 @@
|
|
|
807
808
|
el.innerHTML = icon(name, options);
|
|
808
809
|
}
|
|
809
810
|
|
|
811
|
+
/** The markup `setHtml` last wrote into each element. */
|
|
812
|
+
const painted = new WeakMap();
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Replace `el`'s markup only when it differs from what was last written.
|
|
816
|
+
* Rewriting identical rows on every update replaced every node in them,
|
|
817
|
+
* which dropped hover, focus and a click caught mid-swap.
|
|
818
|
+
*
|
|
819
|
+
* @returns {boolean} whether the markup was written.
|
|
820
|
+
*/
|
|
821
|
+
function setHtml(el, html) {
|
|
822
|
+
if (!el || painted.get(el) === html) return false;
|
|
823
|
+
painted.set(el, html);
|
|
824
|
+
el.innerHTML = html;
|
|
825
|
+
return true;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/** Set `el`'s text only when it changes; a rewrite clears a selection in it. */
|
|
829
|
+
function setText(el, text) {
|
|
830
|
+
if (el && el.textContent !== text) el.textContent = text;
|
|
831
|
+
}
|
|
832
|
+
|
|
810
833
|
function t(key, vars) {
|
|
811
834
|
let text = DICT[LANG]?.[key] ?? DICT.en[key] ?? key;
|
|
812
835
|
if (vars) {
|
|
@@ -842,7 +865,7 @@
|
|
|
842
865
|
render();
|
|
843
866
|
// The taste titles carry no `data-i18n`, and `render()` needs a state
|
|
844
867
|
// document, so they are painted here as well — otherwise a zh shell
|
|
845
|
-
// shows them in English until the first
|
|
868
|
+
// shows them in English until the first state lands.
|
|
846
869
|
paintTaste();
|
|
847
870
|
paintDjModelHeader();
|
|
848
871
|
paintDjModelToggle();
|
|
@@ -933,6 +956,30 @@
|
|
|
933
956
|
};
|
|
934
957
|
const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
935
958
|
|
|
959
|
+
/** Whether the host's event stream is open; see the boot section. */
|
|
960
|
+
let streaming = false;
|
|
961
|
+
/** The newest snapshot version adopted; see {@link newest}. */
|
|
962
|
+
let versionFloor = 0;
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* `next`, unless it is older than the state already shown — then the
|
|
966
|
+
* state shown. A request's answer and a pushed frame travel on different
|
|
967
|
+
* connections, so one read earlier can land later, and adopting it would
|
|
968
|
+
* undo the newer one with nothing coming to repair it. Answers are
|
|
969
|
+
* ordered by `version`, which moves with anything shown — not `rev`,
|
|
970
|
+
* which a setting or a resolved like leaves alone. Only while the stream
|
|
971
|
+
* is open: without it the next poll repairs any such swap, and a
|
|
972
|
+
* reconnect drops the floor, since a restarted host counts its versions
|
|
973
|
+
* from zero again.
|
|
974
|
+
*/
|
|
975
|
+
function newest(next) {
|
|
976
|
+
const version = Number(next?.version);
|
|
977
|
+
if (!Number.isFinite(version)) return next;
|
|
978
|
+
if (streaming && version < versionFloor) return state;
|
|
979
|
+
versionFloor = version;
|
|
980
|
+
return next;
|
|
981
|
+
}
|
|
982
|
+
|
|
936
983
|
let toastTimer;
|
|
937
984
|
function toast(message) {
|
|
938
985
|
const el = $('toast');
|
|
@@ -1021,133 +1068,54 @@
|
|
|
1021
1068
|
return localTransport;
|
|
1022
1069
|
}
|
|
1023
1070
|
|
|
1071
|
+
/**
|
|
1072
|
+
* Origin that serves audio bytes. Under the desktop shell this page runs on
|
|
1073
|
+
* the `dsh-app://` custom scheme, which Chromium cannot byte-range, so seeks
|
|
1074
|
+
* restart the track; the shell publishes its HTTP origin on the parent
|
|
1075
|
+
* window instead. Without one, relative URLs keep the page's own origin.
|
|
1076
|
+
*/
|
|
1077
|
+
function streamOrigin() {
|
|
1078
|
+
try {
|
|
1079
|
+
const base = window.parent?.__DSH_TRANSPORT__?.streamBaseUrl;
|
|
1080
|
+
if (typeof base === 'string' && base) return new URL(base).origin;
|
|
1081
|
+
} catch { /* a cross-origin parent, or a malformed base */ }
|
|
1082
|
+
return '';
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1024
1085
|
/**
|
|
1025
1086
|
* The fallback transport: this page owns an `<audio>` element.
|
|
1026
1087
|
*
|
|
1027
|
-
* It
|
|
1028
|
-
*
|
|
1029
|
-
*
|
|
1088
|
+
* It runs the shell engine's playback core, `playback.js`, which this
|
|
1089
|
+
* page loads from its own host rather than from the bundle — the bundle
|
|
1090
|
+
* is exactly what this fallback cannot trust. The core reconciles on
|
|
1091
|
+
* `transportRev` but enforces play/pause on every update, as the engine
|
|
1092
|
+
* does: a refused `play()` does not change the revision, so gating the
|
|
1093
|
+
* retry would stop the music for good after the first browser refusal.
|
|
1030
1094
|
*/
|
|
1031
1095
|
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', {
|
|
1096
|
+
const playback = window.__dshMusicPlayback.createPlayback({
|
|
1097
|
+
base: BASE,
|
|
1098
|
+
streamOrigin,
|
|
1099
|
+
call: api,
|
|
1100
|
+
report(extra) {
|
|
1101
|
+
if (!state?.current) return;
|
|
1102
|
+
api('/report', {
|
|
1117
1103
|
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)' });
|
|
1104
|
+
body: { trackId: state.current.id, ...playback.progress(), ...extra },
|
|
1105
|
+
}).catch(() => { /* the next update carries the host's view */ });
|
|
1106
|
+
},
|
|
1132
1107
|
});
|
|
1133
|
-
audio.addEventListener('timeupdate', () => { paintProgress(); paintLyrics(); });
|
|
1108
|
+
playback.audio.addEventListener('timeupdate', () => { paintProgress(); paintLyrics(); });
|
|
1134
1109
|
|
|
1135
1110
|
return {
|
|
1136
1111
|
playback: true,
|
|
1137
1112
|
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
|
-
},
|
|
1113
|
+
apply: playback.apply,
|
|
1114
|
+
sync() { playback.apply(state); },
|
|
1115
|
+
playNow: playback.playNow,
|
|
1116
|
+
/** Exact position: this transport owns the element. A held seek counts as already there. */
|
|
1117
|
+
position: playback.position,
|
|
1118
|
+
dispose: playback.dispose,
|
|
1151
1119
|
};
|
|
1152
1120
|
}
|
|
1153
1121
|
|
|
@@ -1159,14 +1127,14 @@
|
|
|
1159
1127
|
} catch { /* a failing transport must not break rendering */ }
|
|
1160
1128
|
}
|
|
1161
1129
|
|
|
1162
|
-
/** Ask the active transport to act now instead of on its next
|
|
1130
|
+
/** Ask the active transport to act now instead of on its next update. */
|
|
1163
1131
|
function pokeEngine() {
|
|
1164
1132
|
try { transport().sync?.(); } catch { /* best effort across documents */ }
|
|
1165
1133
|
}
|
|
1166
1134
|
|
|
1167
1135
|
async function control(body) {
|
|
1168
1136
|
try {
|
|
1169
|
-
state = await api('/control', { method: 'POST', body });
|
|
1137
|
+
state = newest(await api('/control', { method: 'POST', body }));
|
|
1170
1138
|
render();
|
|
1171
1139
|
applyTransport();
|
|
1172
1140
|
} catch (error) { toast(error.message); }
|
|
@@ -1176,9 +1144,11 @@
|
|
|
1176
1144
|
/**
|
|
1177
1145
|
* Playback position.
|
|
1178
1146
|
*
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
* report
|
|
1147
|
+
* The transport reads its own element when it can — the local one, and
|
|
1148
|
+
* the shell engine, whose document this page can reach. An engine from
|
|
1149
|
+
* an older bundle has no `position()`, so its last report to the host is
|
|
1150
|
+
* used, with the time since added back; the host pushes no frame for a
|
|
1151
|
+
* report that only moves the position, so that report can be minutes old.
|
|
1182
1152
|
*/
|
|
1183
1153
|
function positionMs() {
|
|
1184
1154
|
const active = transport();
|
|
@@ -1233,13 +1203,18 @@
|
|
|
1233
1203
|
function render() {
|
|
1234
1204
|
if (!state) return;
|
|
1235
1205
|
const track = state.current;
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
$('
|
|
1239
|
-
|
|
1240
|
-
|
|
1206
|
+
const cover = track?.picUrl || '';
|
|
1207
|
+
// Setting the same `src` again restarts the image load.
|
|
1208
|
+
if ($('cover').dataset.src !== cover) {
|
|
1209
|
+
$('cover').dataset.src = cover;
|
|
1210
|
+
$('cover').src = cover;
|
|
1211
|
+
}
|
|
1212
|
+
$('cover').style.visibility = cover ? 'visible' : 'hidden';
|
|
1213
|
+
setText($('np-title'), track ? track.name : t('nothingPlaying'));
|
|
1214
|
+
setText($('np-artists'), track ? (track.artists || []).join(' / ') : '');
|
|
1215
|
+
setText($('np-album'), track ? track.album || '' : '');
|
|
1241
1216
|
// Details and lyrics belong to the track, and this page fetches them
|
|
1242
|
-
// itself on every
|
|
1217
|
+
// itself on every update — not from the local transport's `applySource`,
|
|
1243
1218
|
// which only ever runs when this page owns the audio element. In the
|
|
1244
1219
|
// normal case the shell engine plays, so that path is dead and the
|
|
1245
1220
|
// pane stayed empty forever.
|
|
@@ -1247,8 +1222,7 @@
|
|
|
1247
1222
|
loadIntro();
|
|
1248
1223
|
prefetchNextDetails();
|
|
1249
1224
|
setIcon($('play'), state.playing ? 'pause' : 'play', { filled: true });
|
|
1250
|
-
$('mode')
|
|
1251
|
-
$('counts').textContent = state.counts ? `(${state.counts.queued})` : '';
|
|
1225
|
+
setText($('mode'), { list: t('modeList'), single: t('modeSingle'), shuffle: t('modeShuffle') }[state.mode] || t('modeList'));
|
|
1252
1226
|
$('queue-clear').disabled = (state.queue?.length ?? 0) <= (state.current ? 1 : 0);
|
|
1253
1227
|
$('dj-toggle').classList.toggle('on', Boolean(state.dj?.enabled));
|
|
1254
1228
|
paintHistory();
|
|
@@ -1258,21 +1232,23 @@
|
|
|
1258
1232
|
|
|
1259
1233
|
// Account chip
|
|
1260
1234
|
const account = state.account;
|
|
1261
|
-
|
|
1235
|
+
const chip = account
|
|
1262
1236
|
? `${account.avatar ? `<img src="${esc(account.avatar)}" alt="">` : ''}<span>${esc(account.nickname)}${account.vip ? ' · VIP' : ''}</span>`
|
|
1263
1237
|
: `<button id="signin">${esc(t('signin'))}</button>`;
|
|
1264
|
-
|
|
1265
|
-
|
|
1238
|
+
if (setHtml($('account'), chip)) {
|
|
1239
|
+
const signin = $('signin');
|
|
1240
|
+
if (signin) signin.onclick = openLogin;
|
|
1241
|
+
}
|
|
1266
1242
|
|
|
1267
1243
|
// DJ status
|
|
1268
1244
|
const dj = state.dj || {};
|
|
1269
|
-
$('dj-status')
|
|
1245
|
+
setHtml($('dj-status'), dj.enabled || dj.lastPlan
|
|
1270
1246
|
? `<div class="plan">${dj.busy ? esc(t('djPlanning')) : dj.error ? `AI DJ: ${esc(dj.error)}` : ''}${
|
|
1271
1247
|
dj.lastPlan
|
|
1272
1248
|
? `<div><b>+${dj.lastPlan.added}</b> ${esc(dj.lastPlan.vibe || '')}</div><div class="muted">${esc((dj.lastPlan.names || []).slice(-3).join(' · '))}</div>`
|
|
1273
1249
|
: ''
|
|
1274
1250
|
}${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
|
-
: '';
|
|
1251
|
+
: '');
|
|
1276
1252
|
|
|
1277
1253
|
if (autoplayBlocked()) {
|
|
1278
1254
|
if (!$('gesture')) {
|
|
@@ -1319,8 +1295,15 @@
|
|
|
1319
1295
|
function renderQueue() {
|
|
1320
1296
|
if (!state) return;
|
|
1321
1297
|
const queue = state.queue || [];
|
|
1298
|
+
const total = state.counts ? `(${state.counts.queued})` : '';
|
|
1322
1299
|
if (queue.length === 0) {
|
|
1323
|
-
$('
|
|
1300
|
+
setText($('counts'), total);
|
|
1301
|
+
// Emptying the list dropped its scroll position, so a queue restored
|
|
1302
|
+
// with the same song playing has to be revealed again.
|
|
1303
|
+
if (setHtml($('queue'), `<div class="empty">${esc(t('queueEmpty'))}</div>`)) {
|
|
1304
|
+
revealedRow = null;
|
|
1305
|
+
revealing = null;
|
|
1306
|
+
}
|
|
1324
1307
|
return;
|
|
1325
1308
|
}
|
|
1326
1309
|
// A window of at most MAX_VISIBLE_ROWS rows that always includes the
|
|
@@ -1330,10 +1313,8 @@
|
|
|
1330
1313
|
const cursor = state.index < 0 ? 0 : state.index;
|
|
1331
1314
|
const start = Math.max(0, Math.min(cursor - 4, queue.length - MAX_VISIBLE_ROWS));
|
|
1332
1315
|
const end = Math.min(queue.length, start + MAX_VISIBLE_ROWS);
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
}
|
|
1336
|
-
$('queue').innerHTML = queue
|
|
1316
|
+
setText($('counts'), end - start < queue.length ? `(${start + 1}–${end} / ${queue.length})` : total);
|
|
1317
|
+
setHtml($('queue'), queue
|
|
1337
1318
|
.slice(start, end)
|
|
1338
1319
|
.map((track, offset) => {
|
|
1339
1320
|
const index = start + offset;
|
|
@@ -1348,7 +1329,93 @@
|
|
|
1348
1329
|
<span class="acts"><button data-remove="${index}" title="${esc(t('remove'))}">${icon('x', { small: true })}</button></span>
|
|
1349
1330
|
</div>`;
|
|
1350
1331
|
})
|
|
1351
|
-
.join('');
|
|
1332
|
+
.join(''));
|
|
1333
|
+
revealPlayingRow();
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
/** The playing row the queue last scrolled to, as `index:trackId`. */
|
|
1337
|
+
let revealedRow = null;
|
|
1338
|
+
|
|
1339
|
+
/**
|
|
1340
|
+
* A smooth reveal still under way: the list edge the row is headed for
|
|
1341
|
+
* and the scroll position that lands it there.
|
|
1342
|
+
*/
|
|
1343
|
+
let revealing = null;
|
|
1344
|
+
|
|
1345
|
+
// A reveal ends when the list comes to rest, or when the user scrolls it
|
|
1346
|
+
// themselves.
|
|
1347
|
+
for (const type of ['scrollend', 'wheel', 'touchstart', 'pointerdown', 'keydown']) {
|
|
1348
|
+
$('queue').addEventListener(type, () => { revealing = null; }, { passive: true });
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
/**
|
|
1352
|
+
* Bring the playing row into the queue's view when a new song starts.
|
|
1353
|
+
*
|
|
1354
|
+
* Only on a change of song: the list is re-rendered on every update, and
|
|
1355
|
+
* scrolling then would yank it back while the user browses what comes
|
|
1356
|
+
* next. Only the list itself scrolls — stacked in a narrow window the list
|
|
1357
|
+
* does not scroll on its own, and moving the whole page for it would lose
|
|
1358
|
+
* whatever the user is reading on the stage. A row already in view stays
|
|
1359
|
+
* put; the first paint jumps, later songs glide.
|
|
1360
|
+
*/
|
|
1361
|
+
function revealPlayingRow() {
|
|
1362
|
+
const list = $('queue');
|
|
1363
|
+
const row = list.querySelector('.row.playing');
|
|
1364
|
+
if (!row || list.offsetParent === null) return;
|
|
1365
|
+
const key = `${state.index}:${state.current?.id}`;
|
|
1366
|
+
if (key === revealedRow) {
|
|
1367
|
+
followReveal(list, row);
|
|
1368
|
+
return;
|
|
1369
|
+
}
|
|
1370
|
+
const first = revealedRow === null;
|
|
1371
|
+
revealedRow = key;
|
|
1372
|
+
revealing = null;
|
|
1373
|
+
const view = list.getBoundingClientRect();
|
|
1374
|
+
const box = row.getBoundingClientRect();
|
|
1375
|
+
const edge = box.top < view.top ? 'top' : box.bottom > view.bottom ? 'bottom' : null;
|
|
1376
|
+
if (!edge) return;
|
|
1377
|
+
const top = revealTarget(list, row, edge);
|
|
1378
|
+
if (first || matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
1379
|
+
list.scrollTo({ top, behavior: 'instant' });
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
revealing = { edge, top };
|
|
1383
|
+
list.scrollTo({ top, behavior: 'smooth' });
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
/**
|
|
1387
|
+
* Keep a glide aimed at the row when a repaint moves it.
|
|
1388
|
+
*
|
|
1389
|
+
* Songs landing in a short queue slide the painted window, and the row
|
|
1390
|
+
* with it, while the list is still scrolling to where the row was. A glide
|
|
1391
|
+
* that has arrived is left alone: from then on a moving row is a change of
|
|
1392
|
+
* queue, not of song.
|
|
1393
|
+
*/
|
|
1394
|
+
function followReveal(list, row) {
|
|
1395
|
+
if (!revealing) return;
|
|
1396
|
+
if (Math.abs(list.scrollTop - revealing.top) < 1) {
|
|
1397
|
+
revealing = null;
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
const top = revealTarget(list, row, revealing.edge);
|
|
1401
|
+
if (Math.abs(top - revealing.top) < 1) return;
|
|
1402
|
+
revealing.top = top;
|
|
1403
|
+
list.scrollTo({ top, behavior: 'smooth' });
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
/**
|
|
1407
|
+
* The scroll position that puts the row at one edge of the list, with
|
|
1408
|
+
* half a row of its neighbours showing so it does not land flush against
|
|
1409
|
+
* the card's edge.
|
|
1410
|
+
*/
|
|
1411
|
+
function revealTarget(list, row, edge) {
|
|
1412
|
+
const view = list.getBoundingClientRect();
|
|
1413
|
+
const box = row.getBoundingClientRect();
|
|
1414
|
+
const margin = box.height / 2;
|
|
1415
|
+
const top = edge === 'top'
|
|
1416
|
+
? list.scrollTop + box.top - view.top - margin
|
|
1417
|
+
: list.scrollTop + box.bottom - view.bottom + margin;
|
|
1418
|
+
return Math.max(0, Math.min(top, list.scrollHeight - list.clientHeight));
|
|
1352
1419
|
}
|
|
1353
1420
|
|
|
1354
1421
|
/**
|
|
@@ -1384,7 +1451,7 @@
|
|
|
1384
1451
|
function paintBoosts() {
|
|
1385
1452
|
const boosts = state?.boosts ?? [];
|
|
1386
1453
|
const now = Date.now();
|
|
1387
|
-
$('boosts')
|
|
1454
|
+
setHtml($('boosts'), boosts.length
|
|
1388
1455
|
? `<span>${esc(t('boostsTitle'))}:</span>` +
|
|
1389
1456
|
boosts
|
|
1390
1457
|
.map((boost) => {
|
|
@@ -1392,14 +1459,14 @@
|
|
|
1392
1459
|
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
1460
|
})
|
|
1394
1461
|
.join('')
|
|
1395
|
-
: '';
|
|
1462
|
+
: '');
|
|
1396
1463
|
}
|
|
1397
1464
|
|
|
1398
1465
|
/** Boost a track, or clear its boost with `none`, and adopt the host's answer. */
|
|
1399
1466
|
async function sendBoost(trackId, direction, button) {
|
|
1400
1467
|
if (button) button.disabled = true;
|
|
1401
1468
|
try {
|
|
1402
|
-
state = await api('/boost', { method: 'POST', body: { trackId, direction } });
|
|
1469
|
+
state = newest(await api('/boost', { method: 'POST', body: { trackId, direction } }));
|
|
1403
1470
|
render();
|
|
1404
1471
|
applyTransport();
|
|
1405
1472
|
toast(t(direction === 'more' ? 'boostedMore' : direction === 'less' ? 'boostedLess' : 'boostCleared'));
|
|
@@ -1431,7 +1498,7 @@
|
|
|
1431
1498
|
// mid-flight is locked out, exactly as the transport switchers do.
|
|
1432
1499
|
if (button) button.disabled = true;
|
|
1433
1500
|
try {
|
|
1434
|
-
state = await api('/queue', { method: 'POST', body: { action: 'remove', index } });
|
|
1501
|
+
state = newest(await api('/queue', { method: 'POST', body: { action: 'remove', index } }));
|
|
1435
1502
|
// Read the answer off the new queue: an index that named a row someone
|
|
1436
1503
|
// else had already dropped must not claim a removal that did not happen.
|
|
1437
1504
|
return !(state.queue || []).some((entry) => entry.id === track.id);
|
|
@@ -1457,7 +1524,7 @@
|
|
|
1457
1524
|
// promised.
|
|
1458
1525
|
if (button) button.disabled = true;
|
|
1459
1526
|
try {
|
|
1460
|
-
state = await api('/taste', { method: 'POST', body: { trackId: track.id, level } });
|
|
1527
|
+
state = newest(await api('/taste', { method: 'POST', body: { trackId: track.id, level } }));
|
|
1461
1528
|
render();
|
|
1462
1529
|
applyTransport();
|
|
1463
1530
|
return true;
|
|
@@ -1478,8 +1545,8 @@
|
|
|
1478
1545
|
if (document.activeElement !== $('seek')) {
|
|
1479
1546
|
$('seek').value = duration > 0 ? String(Math.round((position / duration) * 1000)) : '0';
|
|
1480
1547
|
}
|
|
1481
|
-
$('t-cur')
|
|
1482
|
-
$('t-dur')
|
|
1548
|
+
setText($('t-cur'), fmt(position));
|
|
1549
|
+
setText($('t-dur'), fmt(duration));
|
|
1483
1550
|
}
|
|
1484
1551
|
|
|
1485
1552
|
// ------------------------------------------------------ details + lyrics
|
|
@@ -1572,8 +1639,8 @@
|
|
|
1572
1639
|
|
|
1573
1640
|
/**
|
|
1574
1641
|
* Fetch the next track's details ahead, so they show the moment it
|
|
1575
|
-
* starts. Lazy like the introduction: only
|
|
1576
|
-
*
|
|
1642
|
+
* starts. Lazy like the introduction: only after the shown track's
|
|
1643
|
+
* details have painted, and only while the details are shown.
|
|
1577
1644
|
* Shuffle has no known next track, and repeat-one's next is this one.
|
|
1578
1645
|
*/
|
|
1579
1646
|
function prefetchNextDetails() {
|
|
@@ -1590,6 +1657,7 @@
|
|
|
1590
1657
|
const cached = detailsCache.get(id);
|
|
1591
1658
|
if (cached) {
|
|
1592
1659
|
paintDetails(cached);
|
|
1660
|
+
setTimeout(afterDetails);
|
|
1593
1661
|
return;
|
|
1594
1662
|
}
|
|
1595
1663
|
$('details').innerHTML = `<p class="hint">${esc(t('loading'))}</p>`;
|
|
@@ -1597,12 +1665,24 @@
|
|
|
1597
1665
|
const info = await fetchDetails(id);
|
|
1598
1666
|
if (request !== detailsRequest) return;
|
|
1599
1667
|
paintDetails(info);
|
|
1668
|
+
setTimeout(afterDetails);
|
|
1600
1669
|
} catch (error) {
|
|
1601
1670
|
if (request !== detailsRequest) return;
|
|
1602
1671
|
$('details').innerHTML = `<p class="hint">${esc(error.message)}</p>`;
|
|
1603
1672
|
}
|
|
1604
1673
|
}
|
|
1605
1674
|
|
|
1675
|
+
/**
|
|
1676
|
+
* What waits for the details to paint: the introduction and the next
|
|
1677
|
+
* track's details. Its own turn, so neither is asked for before the
|
|
1678
|
+
* facts are on screen, and not tied to the next update from the host,
|
|
1679
|
+
* which may be minutes away now that it only comes on a change.
|
|
1680
|
+
*/
|
|
1681
|
+
function afterDetails() {
|
|
1682
|
+
loadIntro();
|
|
1683
|
+
prefetchNextDetails();
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1606
1686
|
/** Paint the shown track's details again: a language switch, an introduction, the switch. */
|
|
1607
1687
|
function repaintDetails(options) {
|
|
1608
1688
|
const info = detailsCache.get(detailsTrackId);
|
|
@@ -1612,8 +1692,8 @@
|
|
|
1612
1692
|
/**
|
|
1613
1693
|
* Ask for the shown track's introduction, once its details are painted.
|
|
1614
1694
|
*
|
|
1615
|
-
* Lazy on purpose: a model call takes seconds, so it waits
|
|
1616
|
-
*
|
|
1695
|
+
* Lazy on purpose: a model call takes seconds, so it waits until the
|
|
1696
|
+
* details have painted — playback and the NetEase facts never wait on
|
|
1617
1697
|
* it — and it is asked only for the track the visible pane shows.
|
|
1618
1698
|
*/
|
|
1619
1699
|
async function loadIntro() {
|
|
@@ -2026,7 +2106,7 @@
|
|
|
2026
2106
|
|
|
2027
2107
|
async function saveModelRoute(provider, model, message) {
|
|
2028
2108
|
try {
|
|
2029
|
-
state = await api('/dj', { method: 'POST', body: { provider, model } });
|
|
2109
|
+
state = newest(await api('/dj', { method: 'POST', body: { provider, model } }));
|
|
2030
2110
|
render();
|
|
2031
2111
|
await loadModelPicker();
|
|
2032
2112
|
toast(message);
|
|
@@ -2100,12 +2180,12 @@
|
|
|
2100
2180
|
const last = state?.audio?.last;
|
|
2101
2181
|
const preferred = state?.audio?.preferred;
|
|
2102
2182
|
if (!last || (preferred && last.requested !== preferred && last.trackId !== state?.current?.id)) {
|
|
2103
|
-
$('quality-info')
|
|
2183
|
+
setText($('quality-info'), '');
|
|
2104
2184
|
return;
|
|
2105
2185
|
}
|
|
2106
2186
|
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')})` : '');
|
|
2187
|
+
setText($('quality-info'),
|
|
2188
|
+
`▶ ${String(last.type ?? '').toUpperCase()}${kbps}` + (last.downgraded ? ` (${last.served}, ${t('downgraded')})` : ''));
|
|
2109
2189
|
}
|
|
2110
2190
|
|
|
2111
2191
|
async function loadQuality() {
|
|
@@ -2125,7 +2205,7 @@
|
|
|
2125
2205
|
|
|
2126
2206
|
$('quality').onchange = async (event) => {
|
|
2127
2207
|
try {
|
|
2128
|
-
state = await api('/quality', { method: 'POST', body: { level: event.target.value } });
|
|
2208
|
+
state = newest(await api('/quality', { method: 'POST', body: { level: event.target.value } }));
|
|
2129
2209
|
render();
|
|
2130
2210
|
applyTransport();
|
|
2131
2211
|
pokeEngine();
|
|
@@ -2154,6 +2234,8 @@
|
|
|
2154
2234
|
}
|
|
2155
2235
|
};
|
|
2156
2236
|
$('volume').onchange = (event) => control({ action: 'volume', value: Number(event.target.value) });
|
|
2237
|
+
// Sent at once: the transport silences on the first seek and debounces
|
|
2238
|
+
// the resume, so delaying here would only let the old position play on.
|
|
2157
2239
|
$('seek').onchange = (event) => {
|
|
2158
2240
|
control({ action: 'seek', value: Math.round((Number(event.target.value) / 1000) * durationMs()) });
|
|
2159
2241
|
};
|
|
@@ -2196,7 +2278,7 @@
|
|
|
2196
2278
|
const button = event?.currentTarget;
|
|
2197
2279
|
if (button) button.disabled = true;
|
|
2198
2280
|
try {
|
|
2199
|
-
state = await api('/scrobble', { method: 'POST', body: { enabled: next } });
|
|
2281
|
+
state = newest(await api('/scrobble', { method: 'POST', body: { enabled: next } }));
|
|
2200
2282
|
render();
|
|
2201
2283
|
toast(t(next ? 'scrobbleOn' : 'scrobbleOff'));
|
|
2202
2284
|
} catch (error) {
|
|
@@ -2209,17 +2291,17 @@
|
|
|
2209
2291
|
$('dj-toggle').onclick = async () => {
|
|
2210
2292
|
try {
|
|
2211
2293
|
const data = await api('/dj', { method: 'POST', body: { enabled: !state?.dj?.enabled, prompt: $('dj-prompt').value } });
|
|
2212
|
-
state = data;
|
|
2294
|
+
state = newest(data);
|
|
2213
2295
|
render();
|
|
2214
2296
|
toast(state.dj?.enabled ? t('djOn') : t('djOff'));
|
|
2215
2297
|
} catch (error) { toast(error.message); }
|
|
2216
2298
|
};
|
|
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));
|
|
2299
|
+
$('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
2300
|
|
|
2219
2301
|
// Not row-by-row removal, which reads each row as a dislike.
|
|
2220
2302
|
$('queue-clear').onclick = async () => {
|
|
2221
2303
|
try {
|
|
2222
|
-
state = await api('/queue', { method: 'POST', body: { action: 'clear', keepCurrent: true } });
|
|
2304
|
+
state = newest(await api('/queue', { method: 'POST', body: { action: 'clear', keepCurrent: true } }));
|
|
2223
2305
|
render();
|
|
2224
2306
|
pokeEngine();
|
|
2225
2307
|
} catch (error) { toast(failureText(error)); }
|
|
@@ -2243,7 +2325,7 @@
|
|
|
2243
2325
|
// The confirm names the click, not that side effect of it.
|
|
2244
2326
|
if (await removeFromList(Number(remove.dataset.remove), remove)) toast(t('removed'));
|
|
2245
2327
|
} else if (jump !== undefined) {
|
|
2246
|
-
state = await api('/queue', { method: 'POST', body: { action: 'jump', index: Number(jump) } });
|
|
2328
|
+
state = newest(await api('/queue', { method: 'POST', body: { action: 'jump', index: Number(jump) } }));
|
|
2247
2329
|
} else return;
|
|
2248
2330
|
render();
|
|
2249
2331
|
pokeEngine();
|
|
@@ -2251,29 +2333,81 @@
|
|
|
2251
2333
|
};
|
|
2252
2334
|
|
|
2253
2335
|
// ------------------------------------------------------------------ boot
|
|
2336
|
+
/**
|
|
2337
|
+
* The host pushes its state over `/events` whenever something here would
|
|
2338
|
+
* change. `/state` is re-read every `POLL_MS` only while that stream is
|
|
2339
|
+
* down — a browser without EventSource, a host that predates the route,
|
|
2340
|
+
* a reconnect under way — and every `STREAM_POLL_MS` while it is up, as
|
|
2341
|
+
* a safety net that also ages the boosts' minutes left.
|
|
2342
|
+
*/
|
|
2343
|
+
const POLL_MS = 1500;
|
|
2344
|
+
const STREAM_POLL_MS = 30_000;
|
|
2345
|
+
/** A stream the browser gave up on (an error answer) is opened again after this. */
|
|
2346
|
+
const RELISTEN_MS = 10_000;
|
|
2347
|
+
let events = null;
|
|
2348
|
+
let receivedAt = 0;
|
|
2349
|
+
|
|
2350
|
+
function adopt(next) {
|
|
2351
|
+
receivedAt = Date.now();
|
|
2352
|
+
if (newest(next) !== next) return;
|
|
2353
|
+
state = next;
|
|
2354
|
+
render();
|
|
2355
|
+
applyTransport();
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2254
2358
|
async function refresh() {
|
|
2255
2359
|
try {
|
|
2256
|
-
|
|
2257
|
-
render();
|
|
2258
|
-
applyTransport();
|
|
2360
|
+
adopt(await api('/state'));
|
|
2259
2361
|
} catch (error) {
|
|
2260
|
-
$('np-title')
|
|
2362
|
+
setText($('np-title'), t('connectFailed') + error.message);
|
|
2261
2363
|
}
|
|
2262
2364
|
}
|
|
2263
2365
|
|
|
2366
|
+
function listen() {
|
|
2367
|
+
const Source = window.EventSource;
|
|
2368
|
+
if (typeof Source !== 'function') return;
|
|
2369
|
+
events = new Source(API + '/events');
|
|
2370
|
+
events.onopen = () => {
|
|
2371
|
+
streaming = true;
|
|
2372
|
+
// The host may have restarted, and its versions with it.
|
|
2373
|
+
versionFloor = 0;
|
|
2374
|
+
};
|
|
2375
|
+
events.onerror = () => {
|
|
2376
|
+
streaming = false;
|
|
2377
|
+
// A dropped connection is retried by the browser; an error status
|
|
2378
|
+
// closes the stream for good, so it is opened again from here.
|
|
2379
|
+
if (events.readyState === Source.CLOSED) {
|
|
2380
|
+
events = null;
|
|
2381
|
+
setTimeout(listen, RELISTEN_MS);
|
|
2382
|
+
}
|
|
2383
|
+
};
|
|
2384
|
+
events.addEventListener('state', (event) => {
|
|
2385
|
+
try { adopt(JSON.parse(event.data)); } catch { /* the next frame or poll repairs it */ }
|
|
2386
|
+
});
|
|
2387
|
+
}
|
|
2388
|
+
|
|
2264
2389
|
paintStaticIcons();
|
|
2265
2390
|
applyLocale();
|
|
2266
2391
|
watchShellLocale();
|
|
2267
2392
|
syncShellTheme();
|
|
2268
2393
|
refresh();
|
|
2269
|
-
|
|
2394
|
+
listen();
|
|
2395
|
+
setInterval(() => {
|
|
2396
|
+
if (!streaming || Date.now() - receivedAt >= STREAM_POLL_MS) refresh();
|
|
2397
|
+
// A refused play changes nothing on the host, so no update would come
|
|
2398
|
+
// to retry it: a page that owns its audio retries on this clock.
|
|
2399
|
+
else applyTransport();
|
|
2400
|
+
}, POLL_MS);
|
|
2270
2401
|
void loadModelPicker();
|
|
2271
2402
|
void loadQuality();
|
|
2272
|
-
// Progress and lyric highlighting
|
|
2273
|
-
//
|
|
2274
|
-
//
|
|
2403
|
+
// Progress and lyric highlighting follow the audio element, which lives
|
|
2404
|
+
// in the shell document and reports nothing here, so they need their own
|
|
2405
|
+
// repaint clock.
|
|
2275
2406
|
setInterval(() => { paintProgress(); paintLyrics(); }, 250);
|
|
2276
|
-
window.addEventListener('beforeunload', () => {
|
|
2407
|
+
window.addEventListener('beforeunload', () => {
|
|
2408
|
+
clearInterval(qrTimer);
|
|
2409
|
+
events?.close();
|
|
2410
|
+
});
|
|
2277
2411
|
</script>
|
|
2278
2412
|
</body>
|
|
2279
2413
|
</html>
|