-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.ts
More file actions
1169 lines (1015 loc) · 35.2 KB
/
Copy pathindex.ts
File metadata and controls
1169 lines (1015 loc) · 35.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* AnkiConnect integration for creating and updating Anki cards.
*
* The CORS permission request pattern is based on code from Mangatan-WebUI
* by KolbyML, licensed under Mozilla Public License 2.0.
* https://github.com/KolbyML/Mangatan-WebUI
*/
import type {
Settings,
AnkiConnectSettings,
AnkiConnectionData,
ModelConfig,
FieldMapping
} from '$lib/settings/settings';
import { settings, DEFAULT_MODEL_CONFIGS } from '$lib/settings';
import { showSnackbar } from '$lib/util';
import { isMobilePlatform } from '$lib/util/platform';
import { get } from 'svelte/store';
export * from './cropper';
// Template variables that can be used in field mappings
export const FIELD_TEMPLATES = [
{ template: '{existing}', description: "Card's existing value (update mode)" },
{ template: '{selection}', description: 'Selected/highlighted text' },
{ template: '{sentence}', description: 'Full sentence/textbox content' },
{ template: '{image}', description: 'Screenshot image' },
{ template: '{series}', description: 'Series title' },
{ template: '{volume}', description: 'Volume title' },
{ template: '{page_num}', description: 'Current page number' },
{ template: '{page_filename}', description: 'Page image filename' }
] as const;
// Keep old DYNAMIC_TAGS for backwards compatibility with tags field
export const DYNAMIC_TAGS = [
{ tag: '{series}', description: 'Series title' },
{ tag: '{volume}', description: 'Volume title' }
] as const;
export type VolumeMetadata = {
seriesTitle?: string;
volumeTitle?: string;
};
/**
* Sanitizes a string for use in a filename.
* Replaces spaces with underscores and removes unsafe characters.
*/
function sanitizeForFilename(str: string): string {
return str
.replace(/\s+/g, '_') // spaces to underscores
.replace(/[<>:"/\\|?*]/g, '') // remove unsafe chars
.replace(/_{2,}/g, '_') // collapse multiple underscores
.substring(0, 50); // limit length
}
/**
* Generates a descriptive image filename from metadata.
* Format: mokuro_{series}_{volume}_{page}.jpg
*/
export function generateImageFilename(metadata?: VolumeMetadata, pageFilename?: string): string {
const parts = ['mokuro'];
if (metadata?.seriesTitle) {
parts.push(sanitizeForFilename(metadata.seriesTitle));
}
if (metadata?.volumeTitle) {
parts.push(sanitizeForFilename(metadata.volumeTitle));
}
if (pageFilename) {
parts.push(sanitizeForFilename(pageFilename));
}
// If no metadata, use timestamp as fallback
if (parts.length === 1) {
parts.push(String(Date.now()));
}
return parts.join('_') + '.jpg';
}
/**
* Resolves dynamic tag templates in a tags string
* e.g., "{series} mining" -> "One_Piece mining"
*/
export function resolveDynamicTags(tags: string, metadata: VolumeMetadata): string {
if (!tags) return '';
let resolved = tags;
// Replace {series} with sanitized series title
if (metadata.seriesTitle) {
// Anki tags can't have spaces, replace with underscores
const sanitized = metadata.seriesTitle.replace(/\s+/g, '_');
resolved = resolved.replace(/\{series\}/g, sanitized);
} else {
// Remove the tag if no series title available
resolved = resolved.replace(/\{series\}/g, '');
}
// Replace {volume} with sanitized volume title
if (metadata.volumeTitle) {
const sanitized = metadata.volumeTitle.replace(/\s+/g, '_');
resolved = resolved.replace(/\{volume\}/g, sanitized);
} else {
resolved = resolved.replace(/\{volume\}/g, '');
}
// Clean up any double spaces and trim
return resolved.replace(/\s+/g, ' ').trim();
}
/**
* Options for resolving templates with additional context
*/
export type ResolveTemplateOptions = {
pageNumber?: number;
pageFilename?: string;
previousValues?: Record<string, string>;
fieldName?: string;
};
/**
* Resolves all template variables in a field template string.
* Returns the resolved string, or null if the template is empty or only contains {image}.
*/
export function resolveTemplate(
template: string,
metadata: VolumeMetadata,
selectedText?: string,
sentence?: string,
options?: ResolveTemplateOptions
): string | null {
if (!template || template === '{image}') {
return null; // {image} is handled specially, not as text
}
let resolved = template;
const existingPlaceholders: string[] = [];
// Replace {selection} with selected text
if (selectedText) {
resolved = resolved.replace(/\{selection\}/g, selectedText);
} else {
resolved = resolved.replace(/\{selection\}/g, '');
}
// Replace {sentence} with full sentence
if (sentence) {
resolved = resolved.replace(/\{sentence\}/g, sentence);
} else {
resolved = resolved.replace(/\{sentence\}/g, '');
}
// Replace {series} with series title
if (metadata.seriesTitle) {
resolved = resolved.replace(/\{series\}/g, metadata.seriesTitle);
} else {
resolved = resolved.replace(/\{series\}/g, '');
}
// Replace {volume} with volume title
if (metadata.volumeTitle) {
resolved = resolved.replace(/\{volume\}/g, metadata.volumeTitle);
} else {
resolved = resolved.replace(/\{volume\}/g, '');
}
// Replace {page_num} with page number (already 1-indexed from callers)
if (options?.pageNumber !== undefined) {
resolved = resolved.replace(/\{page_num\}/g, String(options.pageNumber));
} else {
resolved = resolved.replace(/\{page_num\}/g, '');
}
// Replace {page_filename} with page filename
if (options?.pageFilename) {
resolved = resolved.replace(/\{page_filename\}/g, options.pageFilename);
} else {
resolved = resolved.replace(/\{page_filename\}/g, '');
}
// Replace {existing} with existing value of the current field (for update mode)
if (options?.previousValues && options?.fieldName) {
const existingValue = options.previousValues[options.fieldName] || '';
resolved = resolved.replace(/\{existing\}/g, () => {
const token = `__MOKURO_EXISTING_${existingPlaceholders.length}__`;
existingPlaceholders.push(existingValue);
return token;
});
} else {
resolved = resolved.replace(/\{existing\}/g, '');
}
// Clean up whitespace (but preserve HTML structure)
// Only collapse multiple spaces, don't trim inside HTML tags
resolved = resolved
.replace(/[ \t]+/g, ' ') // Collapse multiple spaces/tabs to single space (not newlines)
.trim();
// Convert newlines to <br> for Anki
resolved = resolved.replace(/\n/g, '<br>');
// Restore raw existing HTML after normalization.
// This prevents converting newlines inside <style> blocks to <br>, which breaks CSS.
if (existingPlaceholders.length > 0) {
resolved = resolved.replace(/__MOKURO_EXISTING_(\d+)__/g, (_match, idx) => {
const index = Number(idx);
return existingPlaceholders[index] ?? '';
});
}
return resolved || null;
}
/**
* Fetches connection data from AnkiConnect including decks, models, and fields.
* Also detects if running on AnkiConnect Android by testing createDeck support.
*/
export async function fetchConnectionData(testUrl?: string): Promise<AnkiConnectionData | null> {
const url = testUrl || get(settings).ankiConnectSettings.url || 'http://127.0.0.1:8765';
try {
// Test connection first
const versionResult = await testConnection(url);
if (!versionResult.success) {
showSnackbar(versionResult.message);
return null;
}
// Fetch deck names
const decks = await ankiConnectRaw(url, 'deckNames', {});
if (!decks) {
showSnackbar('Failed to fetch deck names');
return null;
}
// Fetch model names
const models = await ankiConnectRaw(url, 'modelNames', {});
if (!models) {
showSnackbar('Failed to fetch model names');
return null;
}
// Fetch field names for each model
const modelFields: Record<string, string[]> = {};
for (const model of models) {
const fields = await ankiConnectRaw(url, 'modelFieldNames', { modelName: model });
if (fields) {
modelFields[model] = fields;
}
}
// Detect Android by trying to create a temporary deck
let isAndroid = false;
const tempDeckName = `__mokuro_test_${Date.now()}`;
const createResult = await ankiConnectRaw(url, 'createDeck', { deck: tempDeckName });
if (createResult === null) {
// createDeck failed - likely Android
isAndroid = true;
} else {
// createDeck succeeded - delete the temp deck (desktop only)
await ankiConnectRaw(url, 'deleteDecks', { decks: [tempDeckName], cardsToo: true });
}
return {
connected: true,
version: versionResult.version,
decks,
models,
modelFields,
lastConnected: new Date().toISOString(),
isAndroid
};
} catch (e: any) {
showSnackbar(`Connection failed: ${e?.message ?? String(e)}`);
return null;
}
}
/**
* Raw AnkiConnect call without showing errors (for internal use).
*/
async function ankiConnectRaw(
url: string,
action: string,
params: Record<string, any>
): Promise<any> {
try {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({ action, params, version: 6 })
});
const json = await res.json();
if (json.error) {
return null;
}
return json.result;
} catch {
return null;
}
}
/**
* Check if we're in Android compatibility mode.
*/
export function isAndroidMode(): boolean {
const ankiSettings = get(settings).ankiConnectSettings;
if (ankiSettings.androidModeOverride === 'android') return true;
if (ankiSettings.androidModeOverride === 'desktop') return false;
return ankiSettings.connectionData?.isAndroid ?? false;
}
/**
* Get the model configurations store for the given mode.
*/
function getModelConfigsForMode(
ankiSettings: AnkiConnectSettings,
mode: 'create' | 'update'
): Record<string, ModelConfig> {
if (mode === 'create') {
// For create mode, also check legacy modelConfigs (migration path)
if (ankiSettings.modelConfigs && Object.keys(ankiSettings.modelConfigs).length > 0) {
return { ...ankiSettings.modelConfigs, ...ankiSettings.createModelConfigs };
}
return ankiSettings.createModelConfigs || {};
} else {
// Update mode uses only updateModelConfigs - no legacy fallback
return ankiSettings.updateModelConfigs || {};
}
}
/**
* Check if a model has been explicitly configured for the given mode.
*/
export function hasModelConfig(modelName: string, mode: 'create' | 'update'): boolean {
const ankiSettings = get(settings).ankiConnectSettings;
const configs = getModelConfigsForMode(ankiSettings, mode);
return !!configs[modelName];
}
/**
* Get the current model configuration, or generate a default one.
* Always uses actual fields from connectionData to ensure all fields are included.
*
* @param modelName - The Anki note type name
* @param mode - 'create' or 'update' - determines which config store to use
*/
export function getModelConfig(
modelName: string,
mode: 'create' | 'update' = 'create'
): ModelConfig | null {
const ankiSettings = get(settings).ankiConnectSettings;
// Always use actual fields from connectionData to ensure we include all fields
const actualFields = ankiSettings.connectionData?.modelFields[modelName];
const modeConfigs = getModelConfigsForMode(ankiSettings, mode);
if (!actualFields || actualFields.length === 0) {
// Fall back to saved config if no connection data
if (modeConfigs[modelName]) {
return modeConfigs[modelName];
}
return null;
}
// Get saved config and default config for template suggestions
const savedConfig = modeConfigs[modelName];
const defaultConfig = DEFAULT_MODEL_CONFIGS[modelName];
// Build field mappings from actual Anki fields
const fieldMappings: FieldMapping[] = [];
for (const field of actualFields) {
// Check if we have a saved template for this field
const savedMapping = savedConfig?.fieldMappings.find((m) => m.fieldName === field);
if (savedMapping) {
fieldMappings.push(savedMapping);
continue;
}
// In create mode only, check if default config has a template for this field
// (DEFAULT_MODEL_CONFIGS are for create mode, not update mode)
if (mode === 'create') {
const defaultMapping = defaultConfig?.fieldMappings.find((m) => m.fieldName === field);
if (defaultMapping) {
fieldMappings.push(defaultMapping);
continue;
}
}
// Generate smart default based on field name (mode-specific defaults)
const lowerField = field.toLowerCase();
if (mode === 'update') {
// In update mode, default to {existing} for most fields
if (
lowerField.includes('picture') ||
lowerField.includes('image') ||
lowerField.includes('screenshot')
) {
fieldMappings.push({ fieldName: field, template: '{existing}{image}' });
} else if (lowerField.includes('sentence') || lowerField.includes('context')) {
fieldMappings.push({ fieldName: field, template: '{sentence}' });
} else {
fieldMappings.push({ fieldName: field, template: '{existing}' });
}
} else {
// Create mode defaults
if (
lowerField.includes('front') ||
lowerField.includes('expression') ||
lowerField.includes('word')
) {
fieldMappings.push({ fieldName: field, template: '{selection}' });
} else if (
lowerField.includes('picture') ||
lowerField.includes('image') ||
lowerField.includes('screenshot')
) {
fieldMappings.push({ fieldName: field, template: '{image}' });
} else if (lowerField.includes('sentence') || lowerField.includes('context')) {
fieldMappings.push({ fieldName: field, template: '{sentence}' });
} else {
fieldMappings.push({ fieldName: field, template: '' });
}
}
}
return {
modelName,
deckName: savedConfig?.deckName || defaultConfig?.deckName || 'Default',
fieldMappings,
tags: savedConfig?.tags,
quickCapture: savedConfig?.quickCapture
};
}
export type ConnectionTestResult = {
success: boolean;
error?: 'network' | 'cors' | 'invalid_response' | 'anki_error' | 'permission_denied';
message: string;
version?: number;
};
/**
* Requests permission from AnkiConnect.
* This triggers a popup in Anki asking the user to grant permission to this website.
* Returns true if permission was granted, false otherwise.
*/
async function requestAnkiPermission(url: string): Promise<boolean> {
try {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({ action: 'requestPermission', version: 6 })
});
const json = await res.json();
return json.result?.permission === 'granted';
} catch {
return false;
}
}
/**
* Tests the AnkiConnect connection and returns detailed error information.
* Uses the "version" action which is a simple ping that returns the API version.
* If CORS blocks the request, attempts to request permission from Anki.
*/
export async function testConnection(testUrl?: string): Promise<ConnectionTestResult> {
const url = testUrl || get(settings).ankiConnectSettings.url || 'http://127.0.0.1:8765';
try {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({ action: 'version', version: 6 })
});
const json = await res.json();
if (json.error) {
return {
success: false,
error: 'anki_error',
message: `Anki error: ${json.error}`
};
}
return {
success: true,
message: `Connected to AnkiConnect v${json.result}`,
version: json.result
};
} catch (e: any) {
// Distinguish between different error types
const errorMessage = e?.message ?? String(e);
// CORS errors typically show as "Failed to fetch" or similar network errors
if (e instanceof TypeError && errorMessage.includes('Failed to fetch')) {
// Try requesting permission from Anki - this triggers a popup in Anki
const granted = await requestAnkiPermission(url);
if (granted) {
// Permission granted, retry the connection
return testConnection(testUrl);
}
// Permission not granted or request failed
return {
success: false,
error: 'cors',
message:
'Connection blocked. If Anki showed a permission popup, click "Yes" and try again. Otherwise, add this site to webCorsOriginList in AnkiConnect settings.'
};
}
if (errorMessage.includes('NetworkError') || errorMessage.includes('net::')) {
return {
success: false,
error: 'network',
message: 'Network error: Check that Anki is running and the URL is correct'
};
}
return {
success: false,
error: 'invalid_response',
message: `Connection failed: ${errorMessage}`
};
}
}
export async function ankiConnect(
action: string,
params: Record<string, any>,
options?: { silent?: boolean; retried?: boolean }
) {
const url = get(settings).ankiConnectSettings.url || 'http://127.0.0.1:8765';
try {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({ action, params, version: 6 })
});
const json = await res.json();
if (json.error) {
throw new Error(json.error);
}
return json.result;
} catch (e: any) {
// Skip showing errors if silent mode
if (options?.silent) {
return undefined;
}
// Provide more helpful error messages
const errorMessage = e?.message ?? String(e);
if (e instanceof TypeError && errorMessage.includes('Failed to fetch')) {
// Try requesting permission if we haven't already retried
if (!options?.retried) {
const granted = await requestAnkiPermission(url);
if (granted) {
// Retry the request
return ankiConnect(action, params, { ...options, retried: true });
}
}
showSnackbar(
'Error: Cannot connect to AnkiConnect. If Anki showed a permission popup, click "Yes" and try again.'
);
} else {
showSnackbar(`Error: ${errorMessage}`);
}
}
}
export async function getCardInfo(id: number) {
const [noteInfo] = await ankiConnect('notesInfo', { notes: [id] });
return noteInfo;
}
export async function getLastCardId(): Promise<number | undefined> {
const notesToday = await ankiConnect('findNotes', { query: 'added:1' });
if (!notesToday || !Array.isArray(notesToday) || notesToday.length === 0) {
return undefined;
}
// Sort numerically (not lexicographically) and get the highest ID (most recent)
const id = notesToday.sort((a: number, b: number) => a - b).at(-1);
return id;
}
export async function getLastCardInfo() {
const id = await getLastCardId();
if (id === undefined) return undefined;
return await getCardInfo(id);
}
export function getCardAgeInMin(id: number) {
return Math.floor((Date.now() - id) / 60000);
}
export async function blobToBase64(blob: Blob) {
return new Promise<string | null>((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result as string);
reader.readAsDataURL(blob);
});
}
export async function imageToWebp(source: File, settings: Settings) {
const image = await createImageBitmap(source);
const canvas = new OffscreenCanvas(image.width, image.height);
const context = canvas.getContext('2d');
if (context) {
context.drawImage(image, 0, 0);
await imageResize(
canvas,
context,
settings.ankiConnectSettings.widthField,
settings.ankiConnectSettings.heightField
);
const blob = await canvas.convertToBlob({
type: 'image/jpeg',
quality: settings.ankiConnectSettings.qualityField
});
image.close();
return await blobToBase64(blob);
}
}
export async function imageResize(
canvas: OffscreenCanvas,
ctx: OffscreenCanvasRenderingContext2D,
maxWidth: number,
maxHeight: number
): Promise<OffscreenCanvas> {
return new Promise((resolve, reject) => {
const widthRatio = maxWidth <= 0 ? 1 : maxWidth / canvas.width;
const heightRatio = maxHeight <= 0 ? 1 : maxHeight / canvas.height;
const ratio = Math.min(1, Math.min(widthRatio, heightRatio));
if (ratio < 1) {
const newWidth = canvas.width * ratio;
const newHeight = canvas.height * ratio;
createImageBitmap(canvas, {
resizeWidth: newWidth,
resizeHeight: newHeight,
resizeQuality: 'high'
})
.then((sprite) => {
canvas.width = newWidth;
canvas.height = newHeight;
ctx.drawImage(sprite, 0, 0);
resolve(canvas);
})
.catch((e) => reject(e));
} else {
resolve(canvas);
}
});
}
export type CreateCardOptions = {
fieldMappings?: FieldMapping[];
previousValues?: Record<string, string>;
pageNumber?: number;
pageFilename?: string;
deckName?: string;
};
export async function createCard(
imageData: string | null | undefined,
selectedText?: string,
sentence?: string,
tags?: string,
metadata?: VolumeMetadata,
options?: CreateCardOptions
) {
const ankiSettings = get(settings).ankiConnectSettings;
const { enabled, selectedModel } = ankiSettings;
if (!enabled) {
return;
}
// Get model configuration for create mode
const config = getModelConfig(selectedModel, 'create');
if (!config) {
showSnackbar(`Error: No configuration found for model "${selectedModel}"`);
return;
}
showSnackbar('Creating new card...', 10000);
// Resolve dynamic templates in deck name (e.g., "Mining::{series}" -> "Mining::One_Piece")
// Use provided deckName from options (modal) or fall back to config
const baseDeckName = options?.deckName || config.deckName;
const resolvedDeckName = metadata ? resolveDynamicTags(baseDeckName, metadata) : baseDeckName;
// Resolve dynamic tags with volume metadata
const resolvedTags = tags && metadata ? resolveDynamicTags(tags, metadata) : tags;
const tagList = resolvedTags ? resolvedTags.split(' ').filter((t) => t.length > 0) : [];
if (!imageData) {
showSnackbar('Error: No image data');
return;
}
// Use provided field mappings (from modal) or fall back to saved config
const fieldMappings = options?.fieldMappings || config.fieldMappings;
// Find fields that use {image} - these will receive the picture via AnkiConnect's picture parameter
const imageFields: string[] = [];
for (const mapping of fieldMappings) {
if (mapping.template?.includes('{image}')) {
imageFields.push(mapping.fieldName);
}
}
// Generate image filename
const imageFilename = generateImageFilename(metadata, options?.pageFilename);
// Extract base64 data from data URL
const base64Data = imageData.split(';base64,')[1];
if (!base64Data) {
showSnackbar('Error: Invalid image data format');
return;
}
// Build fields object from field mappings
// For fields with {image}, we resolve without the image (AnkiConnect will insert it)
const fields: Record<string, string> = {};
for (const mapping of fieldMappings) {
if (!mapping.template) continue;
// Remove {image} from template - AnkiConnect's picture parameter handles image insertion
const templateWithoutImage = mapping.template.replace(/\{image\}/g, '');
const resolved = resolveTemplate(templateWithoutImage, metadata || {}, selectedText, sentence, {
pageNumber: options?.pageNumber,
previousValues: options?.previousValues,
fieldName: mapping.fieldName
});
if (resolved) {
fields[mapping.fieldName] = resolved;
}
}
// Ensure we have at least one non-empty field (excluding image-only fields)
const nonImageFields = Object.keys(fields).filter((f) => !imageFields.includes(f) || fields[f]);
if (nonImageFields.length === 0 && imageFields.length === 0) {
showSnackbar('Error: No fields would be populated. Check your field mappings.');
return;
}
const notePayload: Record<string, any> = {
deckName: resolvedDeckName,
modelName: selectedModel,
fields,
options: {
allowDuplicate: true
}
};
// Add picture using AnkiConnect's built-in picture parameter (works on desktop and Android)
if (imageFields.length > 0) {
notePayload.picture = [
{
filename: imageFilename,
data: base64Data,
fields: imageFields
}
];
}
// Only add tags if non-empty
if (tagList.length > 0) {
notePayload.tags = tagList;
}
// Validate deck exists
const existingDecks = await ankiConnect('deckNames', {});
if (!existingDecks) {
// Connection failed - ankiConnect already showed error
return;
}
const deckExists = existingDecks.includes(resolvedDeckName);
if (!deckExists) {
// Try to create deck (not supported by AnkiConnect Android)
const createResult = await ankiConnect(
'createDeck',
{ deck: resolvedDeckName },
{ silent: true }
);
if (createResult === undefined) {
showSnackbar(
`Error: Deck "${resolvedDeckName}" doesn't exist. Please create it in Anki first.`
);
return;
}
}
// Validate model exists
const existingModels = await ankiConnect('modelNames', {});
if (!existingModels) {
return;
}
const modelExists = existingModels.includes(selectedModel);
if (!modelExists) {
showSnackbar(
`Error: Note type "${selectedModel}" doesn't exist. Available: ${existingModels.join(', ')}`
);
return;
}
// Validate fields exist on model
const modelFields = await ankiConnect('modelFieldNames', { modelName: selectedModel });
if (!modelFields) {
return;
}
// Check all configured fields exist
const usedFields = Object.keys(fields);
const missingFields = usedFields.filter((f) => !modelFields.includes(f));
if (missingFields.length > 0) {
showSnackbar(
`Error: Fields ${missingFields.map((f) => `"${f}"`).join(', ')} not found. Available: ${modelFields.join(', ')}`
);
return;
}
const result = await ankiConnect('addNote', { note: notePayload });
if (result) {
showSnackbar('Card created!');
} else {
// If we get here, validation passed but addNote still failed
showSnackbar('Error: Failed to create card. The note may be a duplicate.');
}
}
export type UpdateCardOptions = {
fieldMappings?: FieldMapping[];
previousValues?: Record<string, string>;
previousTags?: string[];
pageNumber?: number;
pageFilename?: string;
selectedText?: string;
};
export async function updateLastCard(
imageData: string | null | undefined,
sentence?: string,
tags?: string,
metadata?: VolumeMetadata,
cardId?: number,
modelName?: string,
options?: UpdateCardOptions
) {
const ankiSettings = get(settings).ankiConnectSettings;
const { enabled, selectedModel } = ankiSettings;
if (!enabled) {
return;
}
// Model name is required for update mode - must know the card's actual note type
if (!modelName) {
showSnackbar('Error: Model name required for update mode');
return;
}
// Get model configuration for update mode
const config = getModelConfig(modelName, 'update');
if (!config) {
showSnackbar(`Error: No configuration found for model "${modelName}"`);
return;
}
showSnackbar('Updating card...', 10000);
// Use provided card ID or fetch the last one
let id = cardId;
if (!id) {
id = await getLastCardId();
if (!id) {
showSnackbar('Error: Could not find recent card (connection failed or no cards today)');
return;
}
// Only check timeout when we're fetching the card (not when ID is provided)
if (getCardAgeInMin(id) >= 5) {
showSnackbar('Error: Card created over 5 minutes ago');
return;
}
}
// Use provided field mappings (from modal) or fall back to saved config
const fieldMappings = options?.fieldMappings || config.fieldMappings;
// Resolve dynamic tags with volume metadata and {existing}
let resolvedTags = tags || '';
// Replace {existing} with previous tags
if (options?.previousTags) {
resolvedTags = resolvedTags.replace(/\{existing\}/g, options.previousTags.join(' '));
} else {
resolvedTags = resolvedTags.replace(/\{existing\}/g, '');
}
// Resolve {series} and {volume}
resolvedTags = metadata ? resolveDynamicTags(resolvedTags, metadata) : resolvedTags;
if (!imageData) {
showSnackbar('Error: No image data');
return;
}
// Find fields that use {image} - these will receive the picture via AnkiConnect's picture parameter
const imageFields: string[] = [];
for (const mapping of fieldMappings) {
if (mapping.template?.includes('{image}')) {
imageFields.push(mapping.fieldName);
}
}
// Generate image filename (use card ID for uniqueness in updates)
const imageFilename = generateImageFilename(metadata, options?.pageFilename);
// Extract base64 data from data URL
const base64Data = imageData.split(';base64,')[1];
if (!base64Data) {
showSnackbar('Error: Invalid image data format');
return;
}
// Build fields object from field mappings
// For fields with {image}, we resolve without the image (AnkiConnect will insert it)
const fields: Record<string, any> = {};
for (const mapping of fieldMappings) {
if (!mapping.template) continue;
// Remove {image} from template - AnkiConnect's picture parameter handles image insertion
const templateWithoutImage = mapping.template.replace(/\{image\}/g, '');
// Resolve text content
const resolved = resolveTemplate(
templateWithoutImage,
metadata || {},
options?.selectedText,
sentence,
{
pageNumber: options?.pageNumber,
previousValues: options?.previousValues,
fieldName: mapping.fieldName
}
);
// For image fields: if template resolves to empty (e.g., just "{image}"),
// we must explicitly clear the field first so the new image replaces rather than appends
if (imageFields.includes(mapping.fieldName)) {
fields[mapping.fieldName] = resolved || '';
} else if (resolved) {
fields[mapping.fieldName] = resolved;
}
}
try {
const noteUpdate: Record<string, any> = {
id,
fields
};
// Add picture using AnkiConnect's built-in picture parameter (works on desktop and Android)
if (imageFields.length > 0) {
noteUpdate.picture = {
filename: imageFilename,
data: base64Data,
fields: imageFields
};
}
const updateResult = await ankiConnect('updateNoteFields', { note: noteUpdate });
// ankiConnect returns undefined on error (after showing snackbar)
if (updateResult === undefined) {
return;
}
// Add tags if provided (AnkiConnect Android doesn't support addTags, so skip on mobile)
if (resolvedTags && resolvedTags.length > 0 && !isMobilePlatform()) {