mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-18 16:46:08 -08:00
feat(plugins): add JavaScript plugin system with overlay panel (#303)
This commit is contained in:
1 parent
90fc7cf940
commit
a9a4c4f083
110 files changed
+5897
-344
No files matched your search
@@ -10,8 +10,9 @@ import {
|
||||
type MediaTimingReviewRequest,
|
||||
type WordCardKind,
|
||||
} from '../types/anki';
|
||||
import { shiftSubtitleRangeToVideoClock } from '../core/services/subtitle-clock';
|
||||
import { shiftSubtitleRangeToVideoClock, toSubtitleClock } from '../core/services/subtitle-clock';
|
||||
import { createLogger } from '../logger';
|
||||
import type { PluginMiningProvenance } from '../plugin-api';
|
||||
import type { MediaInput } from '../media-input';
|
||||
import { SubtitleTimingTracker } from '../subtitle-timing-tracker';
|
||||
import { AiConfig } from '../types/integrations';
|
||||
@@ -102,6 +103,8 @@ interface CardCreationMediaGenerator {
|
||||
}
|
||||
|
||||
interface CardCreationDeps {
|
||||
captureMiningProvenance?: () => PluginMiningProvenance | null;
|
||||
onNoteCreated?: (noteId: number, provenance: PluginMiningProvenance | null) => void;
|
||||
getConfig: () => AnkiConnectConfig;
|
||||
getAiConfig: () => AiConfig;
|
||||
getTimingTracker: () => SubtitleTimingTracker;
|
||||
@@ -153,6 +156,8 @@ interface CardCreationDeps {
|
||||
trackLastAddedDuplicateNoteIds?: (noteId: number, duplicateNoteIds: number[]) => void;
|
||||
findDuplicateNoteIds?: (expression: string, noteInfo: CardCreationNoteInfo) => Promise<number[]>;
|
||||
recordCardsMinedCallback?: (count: number, noteIds?: number[]) => void;
|
||||
/** Called once card creation is done writing a card; queued YouTube media may still be pending. */
|
||||
onCardsReady?: (noteIds: number[]) => void;
|
||||
reviewMediaTiming?: (
|
||||
request: Omit<MediaTimingReviewRequest, 'audioPadding' | 'maxMediaDuration'>,
|
||||
) => Promise<MediaTimingReviewDecision>;
|
||||
@@ -603,6 +608,15 @@ export class CardCreationService {
|
||||
}
|
||||
|
||||
const maxMediaDuration = this.deps.getConfig().media?.maxMediaDuration ?? 30;
|
||||
const captured = this.deps.captureMiningProvenance?.() ?? null;
|
||||
const provenance = captured && {
|
||||
...captured,
|
||||
subtitle: {
|
||||
text: sentence,
|
||||
startTime: toSubtitleClock(startTime, mpvClient.currentSubDelay ?? 0),
|
||||
endTime: toSubtitleClock(endTime, mpvClient.currentSubDelay ?? 0),
|
||||
},
|
||||
};
|
||||
const cappedEndTime = clampMediaEndTime(startTime, endTime, maxMediaDuration);
|
||||
if (cappedEndTime !== endTime) {
|
||||
log.warn(
|
||||
@@ -611,6 +625,7 @@ export class CardCreationService {
|
||||
endTime = cappedEndTime;
|
||||
}
|
||||
|
||||
let createdNoteId: number | null = null;
|
||||
try {
|
||||
return await this.deps.withUpdateProgress('Creating sentence card', async () => {
|
||||
const timingDecision = this.deps.reviewMediaTiming
|
||||
@@ -723,6 +738,8 @@ export class CardCreationService {
|
||||
fields,
|
||||
this.getConfiguredAnkiTags(),
|
||||
);
|
||||
createdNoteId = noteId;
|
||||
this.deps.onNoteCreated?.(noteId, provenance);
|
||||
log.info('Created sentence card:', noteId);
|
||||
} catch (error) {
|
||||
log.error('Failed to create sentence card:', (error as Error).message);
|
||||
@@ -887,6 +904,8 @@ export class CardCreationService {
|
||||
log.error('Error creating sentence card:', (error as Error).message);
|
||||
this.deps.showUpdateResult(`Sentence card failed: ${(error as Error).message}`, false);
|
||||
return false;
|
||||
} finally {
|
||||
if (createdNoteId !== null) this.deps.onCardsReady?.([createdNoteId]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user