mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-22 16:46:10 -08:00
feat(jimaku): auto-open modal when a video has no Japanese subtitle (#319)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Ivan Melck <ivan@spec-kitty.ai>
This commit is contained in:
24 files changed
+907
-22
No files matched your search
@@ -573,3 +573,166 @@ test('media info arriving after the modal closed does not fill inputs or search'
|
||||
Object.defineProperty(globalThis, 'document', { configurable: true, value: previousDocument });
|
||||
}
|
||||
});
|
||||
|
||||
type MediaInfoFixture = { filename: string; season: number; episode: number };
|
||||
|
||||
const mediaInfo = (info: MediaInfoFixture) => ({
|
||||
...info,
|
||||
title: 'Show',
|
||||
confidence: 'medium',
|
||||
rawTitle: info.filename,
|
||||
});
|
||||
|
||||
// A Jimaku modal whose media-info replies are either immediate (`current`) or held until a test
|
||||
// resolves them (`deferReplies`), to drive re-open and reply-ordering races.
|
||||
async function withReopenHarness(
|
||||
run: (h: {
|
||||
modal: ReturnType<typeof createJimakuModal>;
|
||||
episodeInput: { value: string };
|
||||
setCurrent: (info: MediaInfoFixture) => void;
|
||||
deferReplies: () => void;
|
||||
pendingReplies: Array<(info: MediaInfoFixture) => void>;
|
||||
pressEnter: () => void;
|
||||
}) => Promise<void>,
|
||||
): Promise<void> {
|
||||
const globals = globalThis as typeof globalThis & { window?: unknown; document?: unknown };
|
||||
const previousWindow = globals.window;
|
||||
const previousDocument = globals.document;
|
||||
|
||||
let current = mediaInfo({ filename: '01.mkv', season: 1, episode: 1 });
|
||||
let deferred = false;
|
||||
const pendingReplies: Array<(info: MediaInfoFixture) => void> = [];
|
||||
const electronAPI = {
|
||||
getJimakuMediaInfo: () =>
|
||||
deferred
|
||||
? new Promise((resolve) => pendingReplies.push((info) => resolve(mediaInfo(info))))
|
||||
: Promise.resolve(current),
|
||||
jimakuSearchEntries: async () => ({ ok: true, data: [] }),
|
||||
notifyOverlayModalClosed: () => {},
|
||||
} as unknown as ElectronAPI;
|
||||
Object.defineProperty(globalThis, 'window', { configurable: true, value: { electronAPI } });
|
||||
Object.defineProperty(globalThis, 'document', {
|
||||
configurable: true,
|
||||
value: { activeElement: null, createElement: () => createElementStub() },
|
||||
});
|
||||
|
||||
try {
|
||||
const episodeInput = { value: '' };
|
||||
const ctx = {
|
||||
dom: {
|
||||
overlay: { classList: createClassList() },
|
||||
jimakuModal: { classList: createClassList(['hidden']), setAttribute: () => {} },
|
||||
jimakuTitleInput: { value: '' },
|
||||
jimakuSeasonInput: { value: '' },
|
||||
jimakuEpisodeInput: episodeInput,
|
||||
jimakuSearchButton: { addEventListener: () => {} },
|
||||
jimakuCloseButton: { addEventListener: () => {} },
|
||||
jimakuStatus: { textContent: '', style: { color: '' } },
|
||||
jimakuEntriesSection: { classList: createClassList(['hidden']) },
|
||||
jimakuEntriesList: createListStub(),
|
||||
jimakuFilesSection: { classList: createClassList(['hidden']) },
|
||||
jimakuFilesList: createListStub(),
|
||||
jimakuBroadenButton: { classList: createClassList(['hidden']), addEventListener: () => {} },
|
||||
jimakuTabAnimeButton: { classList: createClassList(['active']), setAttribute: () => {} },
|
||||
jimakuTabLiveActionButton: { classList: createClassList(), setAttribute: () => {} },
|
||||
},
|
||||
state: createRendererState(),
|
||||
};
|
||||
const modal = createJimakuModal(ctx as never, {
|
||||
modalStateReader: { isAnyModalOpen: () => false },
|
||||
syncSettingsModalSubtitleSuppression: () => {},
|
||||
});
|
||||
|
||||
await run({
|
||||
modal,
|
||||
episodeInput,
|
||||
setCurrent: (info) => {
|
||||
current = mediaInfo(info);
|
||||
},
|
||||
deferReplies: () => {
|
||||
deferred = true;
|
||||
},
|
||||
pendingReplies,
|
||||
// Enter with no results and no focused input starts a search with the current inputs.
|
||||
pressEnter: () => {
|
||||
modal.handleJimakuKeydown({ key: 'Enter', preventDefault: () => {} } as KeyboardEvent);
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
Object.defineProperty(globalThis, 'window', { configurable: true, value: previousWindow });
|
||||
Object.defineProperty(globalThis, 'document', { configurable: true, value: previousDocument });
|
||||
}
|
||||
}
|
||||
|
||||
for (const c of [
|
||||
{
|
||||
name: 'reloads media info when the video changed',
|
||||
next: { filename: '02.mkv', season: 1, episode: 2 },
|
||||
reloads: true,
|
||||
},
|
||||
{
|
||||
name: 'reloads media info for the same file name in another season',
|
||||
next: { filename: '01.mkv', season: 2, episode: 1 },
|
||||
reloads: true,
|
||||
},
|
||||
{
|
||||
name: 'keeps the current search for the same video',
|
||||
next: { filename: '01.mkv', season: 1, episode: 1 },
|
||||
reloads: false,
|
||||
},
|
||||
]) {
|
||||
test(`re-opening the open Jimaku modal ${c.name}`, async () => {
|
||||
await withReopenHarness(async (h) => {
|
||||
h.modal.openJimakuModal();
|
||||
await flushAsyncWork();
|
||||
h.episodeInput.value = '7'; // the user's own edit
|
||||
h.setCurrent(c.next);
|
||||
h.modal.openJimakuModal();
|
||||
await flushAsyncWork();
|
||||
await flushAsyncWork();
|
||||
|
||||
assert.equal(h.episodeInput.value, c.reloads ? String(c.next.episode) : '7');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test('a search started during a changed-video refresh does not cancel the refresh', async () => {
|
||||
await withReopenHarness(async (h) => {
|
||||
h.modal.openJimakuModal();
|
||||
await flushAsyncWork();
|
||||
h.deferReplies();
|
||||
|
||||
h.modal.openJimakuModal(); // refresh request now pending
|
||||
h.pressEnter();
|
||||
await flushAsyncWork();
|
||||
h.pendingReplies.shift()?.({ filename: '02.mkv', season: 1, episode: 2 });
|
||||
await flushAsyncWork();
|
||||
h.pendingReplies.shift()?.({ filename: '02.mkv', season: 1, episode: 2 }); // the reload
|
||||
await flushAsyncWork();
|
||||
|
||||
assert.equal(h.episodeInput.value, '2');
|
||||
});
|
||||
});
|
||||
|
||||
test('a stale media-info reply from an earlier open does not make the current video look changed', async () => {
|
||||
await withReopenHarness(async (h) => {
|
||||
h.deferReplies();
|
||||
h.modal.openJimakuModal(); // load for episode 1 stays pending
|
||||
h.modal.closeJimakuModal();
|
||||
h.modal.openJimakuModal(); // load for episode 2
|
||||
h.pendingReplies[1]?.({ filename: '02.mkv', season: 1, episode: 2 });
|
||||
await flushAsyncWork();
|
||||
h.pendingReplies[0]?.({ filename: '01.mkv', season: 1, episode: 1 }); // stale
|
||||
await flushAsyncWork();
|
||||
h.episodeInput.value = '7'; // the user's own edit
|
||||
|
||||
h.modal.openJimakuModal(); // same video: must keep the user's search
|
||||
h.pendingReplies[2]?.({ filename: '02.mkv', season: 1, episode: 2 });
|
||||
await flushAsyncWork();
|
||||
h.pendingReplies[3]?.({ filename: '02.mkv', season: 1, episode: 2 }); // a reload, if any
|
||||
await flushAsyncWork();
|
||||
|
||||
assert.equal(h.pendingReplies.length, 3);
|
||||
assert.equal(h.episodeInput.value, '7');
|
||||
});
|
||||
});
|
||||
@@ -294,15 +294,46 @@ export function createJimakuModal(
|
||||
return tag === 'input' || tag === 'textarea';
|
||||
}
|
||||
|
||||
// Identity of the video this modal session was opened for, so a re-open can tell whether the
|
||||
// video changed. The file name alone repeats across seasons (`Season 2/01.mkv`).
|
||||
let loadedMediaKey: string | null = null;
|
||||
const mediaKey = (info: JimakuMediaInfo): string =>
|
||||
JSON.stringify([info.filename, info.title, info.season, info.episode]);
|
||||
// Media-info replies are tracked apart from searchGeneration: a search the user starts must not
|
||||
// cancel a changed-video refresh or let an older load overwrite loadedMediaKey.
|
||||
let modalSession = 0; // bumped on open and close
|
||||
let mediaLoadToken = 0; // bumped per media-info load; only the latest load records its key
|
||||
|
||||
function openJimakuModal(): void {
|
||||
if (ctx.state.jimakuModalOpen) return;
|
||||
if (ctx.state.jimakuModalOpen) {
|
||||
refreshMediaInfoIfChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.state.jimakuModalOpen = true;
|
||||
options.syncSettingsModalSubtitleSuppression();
|
||||
ctx.dom.overlay.classList.add('interactive');
|
||||
ctx.dom.jimakuModal.classList.remove('hidden');
|
||||
ctx.dom.jimakuModal.setAttribute('aria-hidden', 'false');
|
||||
modalSession += 1;
|
||||
loadMediaInfo();
|
||||
}
|
||||
|
||||
// An open request while already open (e.g. the next episode auto-opening Jimaku) must not
|
||||
// leave the previous episode's search on screen. The same video keeps the user's search.
|
||||
function refreshMediaInfoIfChanged(): void {
|
||||
const session = modalSession;
|
||||
window.electronAPI
|
||||
.getJimakuMediaInfo()
|
||||
.then((info: JimakuMediaInfo) => {
|
||||
if (session !== modalSession || !ctx.state.jimakuModalOpen) return;
|
||||
if (mediaKey(info) === loadedMediaKey) return;
|
||||
loadMediaInfo();
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
function loadMediaInfo(): void {
|
||||
setJimakuStatus('Loading media info...');
|
||||
resetJimakuLists();
|
||||
renderTabs();
|
||||
@@ -310,9 +341,13 @@ export function createJimakuModal(
|
||||
// Media info can resolve after the user already closed the modal or
|
||||
// started their own search; a stale reply must not touch the inputs.
|
||||
const generation = searchGeneration;
|
||||
const loadToken = ++mediaLoadToken;
|
||||
window.electronAPI
|
||||
.getJimakuMediaInfo()
|
||||
.then((info: JimakuMediaInfo) => {
|
||||
if (loadToken !== mediaLoadToken) return;
|
||||
// Recorded even when the user already started their own search, which skips the fill.
|
||||
loadedMediaKey = mediaKey(info);
|
||||
if (generation !== searchGeneration) return;
|
||||
ctx.dom.jimakuTitleInput.value = info.title || '';
|
||||
ctx.dom.jimakuSeasonInput.value = info.season ? String(info.season) : '';
|
||||
@@ -337,6 +372,7 @@ export function createJimakuModal(
|
||||
if (!ctx.state.jimakuModalOpen) return;
|
||||
|
||||
ctx.state.jimakuModalOpen = false;
|
||||
modalSession += 1;
|
||||
options.syncSettingsModalSubtitleSuppression();
|
||||
ctx.dom.jimakuModal.classList.add('hidden');
|
||||
ctx.dom.jimakuModal.setAttribute('aria-hidden', 'true');
|
||||
|
||||
Reference in new issue
Block a user