-
Notifications
You must be signed in to change notification settings - Fork 10
/
examples.ts
882 lines (792 loc) · 30 KB
/
examples.ts
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
import { config } from "../../package.json";
import { getString } from "../utils/locale";
import { getPref } from "../utils/prefs";
function example(
target: any,
propertyKey: string | symbol,
descriptor: PropertyDescriptor
) {
const original = descriptor.value;
descriptor.value = function (...args: any) {
try {
ztoolkit.log(`Calling example ${target.name}.${String(propertyKey)}`);
return original.apply(this, args);
} catch (e) {
ztoolkit.log(`Error in example ${target.name}.${String(propertyKey)}`, e);
throw e;
}
};
return descriptor;
}
export class BasicExampleFactory {
// 删除分类条目附件完成提示
@example
static delColItemAttSucess() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("del-col-item-att-sucess"),
type: "success",
progress: 100,
})
.show();
}
// 导出完成提示
@example
static exortSucess(nSucess: number) {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: nSucess + ' ' + getString("exp-sucess"),
type: "success",
progress: 100,
})
.show();
}
// 文件不存在提示
@example
static exortFail(nFail: number) {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: nFail + ' ' + getString("exp-fail"),
type: "fail",
progress: 100,
})
.show();
}
// 语言设置完成提示
@example
static lanSetSucess() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("chan-lan-sucess"),
type: "success",
progress: 100,
})
.show();
}
static softLanFail() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("softLanFail"),
type: "fail",
progress: 100,
})
.show();
}
// 条目附件删除完成提示
@example
static delItemAttSucess(items: any) {
// var zoteroPane = Zotero.getActiveZoteroPane();
// var items = Zotero.getActiveZoteroPane().getSelectedItems();
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("del-item-att-sucess", { args: { count: items.length } }),
type: "success",
progress: 100,
})
.show();
}
// 附件删除完成提示
@example
static delAttSucess() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("del-att-sucess"),
type: "success",
progress: 100,
})
.show();
}
// 快照删除完成提示
@example
static delSnapSucess() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("del-snap-sucess"),
type: "success",
progress: 100,
})
.show();
}
// 笔记删除完成提示
@example
static delNoteSucess() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("del-note-sucess"),
type: "success",
progress: 100,
})
.show();
}
// 其它删除完成提示
@example
static delExtraSucess() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("del-extra-sucess"),
type: "success",
progress: 100,
})
.show();
}
// 摘要删除完成提示
@example
static delAbstractSucess() {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: getString("del-abs-sucess"),
type: "success",
progress: 100,
})
.show();
}
}
export class KeyExampleFactory {
// modifiers逗号分隔,shift就是shift,ctrl跨平台是accel
// @example
// static registerShortcuts() {
// // 修改后重启生效
// // 删除条目和附件默认快捷键:Alt+I
// var key_del_item_att = getPref('key.del.item.att') == undefined ? 'I' : getPref('key.del.item.att');
// // 删除附件默认快捷键.Alt+A
// var key_del_att = getPref('key.del.att') == undefined ? 'A' : getPref('key.del.att');
// //仅删除快照默认快捷键 Alt+S
// var key_del_snap = getPref('key.del.snap') == undefined ? 'S' : getPref('key.del.snap');
// // 仅删除笔记默认快捷键.Alt+N
// var key_del_note = getPref('key.del.note') == undefined ? 'N' : getPref('key.del.note');
// // 仅删除其它默认快捷键.Alt+X
// var key_del_extra = getPref('key.del.extra') == undefined ? 'X' : getPref('key.del.extra');
// // 仅删除摘要默认快捷键.Alt+Z
// var key_del_abs = getPref('key.del.abs') == undefined ? 'Z' : getPref('key.del.abs');
// // 删除条目和附件快捷键:Alt+I
// ztoolkit.Shortcut.register("event", {
// id: `${config.addonRef}-key-del-item-att`,
// key: key_del_item_att as string,
// modifiers: "alt",
// callback: (keyOptions) => {
// HelperExampleFactory.delItemAtt();
// },
// });
// // 删除附件快捷键.Alt+A
// ztoolkit.Shortcut.register("event", {
// id: `${config.addonRef}-key-del-att`,
// key: key_del_att as string,
// modifiers: "alt",
// callback: (keyOptions) => {
// HelperExampleFactory.delAtt();
// },
// });
// //仅删除快照快捷键 Alt+S
// ztoolkit.Shortcut.register("event", {
// id: `${config.addonRef}-key-del-snap`,
// key: key_del_snap as string,
// modifiers: "alt",
// callback: (keyOptions) => {
// HelperExampleFactory.delSnap();
// },
// });
// //删除笔记快捷键Alt+N
// ztoolkit.Shortcut.register("event", {
// id: `${config.addonRef}-key-del-note`,
// key: key_del_note as string,
// modifiers: "alt",
// callback: (keyOptions) => {
// HelperExampleFactory.delNote();
// },
// });
// // 删除其它要快捷键 Alt+X
// ztoolkit.Shortcut.register("event", {
// id: `${config.addonRef}-key-del-extra`,
// key: key_del_extra as string,
// modifiers: "alt",
// callback: (keyOptions) => {
// HelperExampleFactory.delExtra();
// },
// });
// // 删除摘要快捷键Alt+Z
// ztoolkit.Shortcut.register("event", {
// id: `${config.addonRef}-key-del-abs`,
// key: key_del_abs as string,
// modifiers: "alt",
// callback: (keyOptions) => {
// HelperExampleFactory.delAbstract();
// },
// });
// //删除摘要快捷键Alt+Z
// // ztoolkit.Shortcut.register("event", {
// // id: `${config.addonRef}-key-del-abs`,
// // key: "U",
// // modifiers: "alt",
// // callback: (keyOptions) => {
// // // HelperExampleFactory.OSnow();
// // },
// // });
// }
}
export class UIExampleFactory {
// 是否显示分类右键菜单
static displayColMenuitem() {
const collection = ZoteroPane.getSelectedCollection(),
//items = collection.getChildItems(),
menuDelCol = document.getElementById('zotero-collectionmenu-delitemwithatt-del-item-att'), // 删除分类及附件菜单
menuExpColAtt = document.getElementById('zotero-collectionmenu-delitemwithatt-export-att'); // 导出分类附件菜单
// 非正常文件夹,如我的出版物、重复条目、未分类条目、回收站,为false,此时返回值为true,禁用菜单
// 两个!!转表达式为逻辑值
var showMenuDelCol = !!collection;
//ztoolkit.getGlobal("alert")(`Selected ${showMenuDelCol}`);
if (!!collection) { // 如果是正常分类才显示
var items = collection.getChildItems();
var showmenuExpColAtt = items.some((item) => HelperExampleFactory.checkItemAtt(item));
} else {
var showmenuExpColAtt = false;
} // 检查分类是否有附件及是否为正常分类
menuDelCol?.setAttribute('disabled', String(!showMenuDelCol)); // 禁用导出附件
// menuDelCol?.setAttribute('disabled', `${!showMenuDelCol}`); // 禁用导出附件
menuExpColAtt?.setAttribute('disabled', String(!showmenuExpColAtt)); // 禁用删除附件
}
// 是否显示条目右键菜单
static displayMenuitem() {
const items = ZoteroPane.getSelectedItems(),
menuChanLan = document.getElementById('zotero-itemmenu-delitemwithatt-chan-lan'), // 修改语言菜单
menuExpAtt = document.getElementById('zotero-itemmenu-delitemwithatt-export-att'), // 导出附件菜单
menuDelAtt = document.getElementById('zotero-itemmenu-delitemwithatt-del-att'), // 导出附件菜单
menuDelSnap = document.getElementById('zotero-itemmenu-delitemwithatt-del-snap'), // 删除快照
menuDelNote = document.getElementById('zotero-itemmenu-delitemwithatt-del-note'), // 删除笔记
menuDelExtra = document.getElementById('zotero-itemmenu-delitemwithatt-del-extra'), // 删除其它
menuDelAbs = document.getElementById('zotero-itemmenu-delitemwithatt-del-abs'), // 删除摘要
showMenuChanLan = items.some((item) => HelperExampleFactory.checkIsRegularItem(item)),// 检查是否为正常条目
showMenuAtt = items.some((item) => HelperExampleFactory.checkItemAtt(item)), // 检查附件
showMenuSnap = items.some((item) => HelperExampleFactory.checkItemSnap(item)), // 检查快照
showMenuNote = items.some((item) => HelperExampleFactory.checkItemNote(item)), // 检查笔记
showMenuExtra = items.some((item) => HelperExampleFactory.checkItemExtra(item)), // 检查其它内容
showMenuAbstract = items.some((item) => HelperExampleFactory.checkItemAbstract(item)); // 检查摘要
//menu.setAttribute('hidden', 'true');
menuChanLan?.setAttribute('disabled', `${!showMenuChanLan}`); // 禁用修改语言
menuExpAtt?.setAttribute('disabled', `${!showMenuAtt}`); // 禁用导出附件
menuDelAtt?.setAttribute('disabled', String(!showMenuAtt)); // 禁用删除附件
menuDelSnap?.setAttribute('disabled', String(!showMenuSnap)); // 禁用删除快照
menuDelNote?.setAttribute('disabled', String(!showMenuNote)); // 禁用删除笔记
menuDelExtra?.setAttribute('disabled', String(!showMenuExtra)); // 禁用删除其它
menuDelAbs?.setAttribute('disabled', String(!showMenuAbstract)); // 禁用删除摘要
}
// 分隔条
@example
static registerMenuSepartor() {
ztoolkit.Menu.register("item", {
tag: "menuseparator",
id: "zotero-itemmenu-delitemwithatt-separator",
});
}
// 分类右键菜单:删除分类及附件,导出附件
@example
static registerRightClickCollMenu() {
const exportIcon = `chrome://${config.addonRef}/content/icons/export.png`,
delColIcon = `chrome://${config.addonRef}/content/icons/favicon@0.5x.png`;
// 删除分类及附件菜单
ztoolkit.Menu.register("collection", {
tag: "menuitem",
id: "zotero-collectionmenu-delitemwithatt-del-item-att",
label: getString("del-col-item-att"),
commandListener: (ev) => HelperExampleFactory.delColItemAtt(),
icon: delColIcon,
// oncommand: "await HelperExampleFactory.filePickerExample()",
});
// 导出分类附件菜单
ztoolkit.Menu.register("collection", {
tag: "menuitem",
id: "zotero-collectionmenu-delitemwithatt-export-att",
label: getString("eport-att"),
commandListener: (ev) => HelperExampleFactory.colExpAtt(),
icon: exportIcon,
// oncommand: "await HelperExampleFactory.filePickerExample()",
});
}
// 导出附件右键菜单
@example
static registerRightClickMenuItem() {
const exportIcon = `chrome://${config.addonRef}/content/icons/export.png`;
// item menuitem with icon 导出附件
ztoolkit.Menu.register("item", {
tag: "menuitem",
id: "zotero-itemmenu-delitemwithatt-export-att",
label: getString("eport-att"),
commandListener: (ev) => HelperExampleFactory.expAtt(),
icon: exportIcon,
// oncommand: "await HelperExampleFactory.filePickerExample()",
});
}
// 修改语言右键菜单
@example
static registerRightClickChanLan() {
const chanLanIcon = `chrome://${config.addonRef}/content/icons/chanLan.png`;
ztoolkit.Menu.register("item", {
tag: "menuitem",
id: "zotero-itemmenu-delitemwithatt-chan-lan",
label: getString("chan-lan"),
commandListener: (ev) => HelperExampleFactory.chanLan(),
icon: chanLanIcon,
// oncommand: "await HelperExampleFactory.filePickerExample()",
});
}
// 删除附件右键菜单
@example
static registerRightClickMenuPopup() {
// 菜单组无法使用图标
// const delIcon = `chrome://${config.addonRef}/content/icons/del.png`;
ztoolkit.Menu.register(
"item",
{
tag: "menu",
label: getString("del-att"),
children: [
{ // 删除附件和条目菜单
tag: "menuitem",
label: getString("delitem-label"),
id: "zotero-itemmenu-delitemwithatt-del-item-att",
commandListener: (ev) => HelperExampleFactory.delItemAtt(),
},
{// 删除附件
tag: "menuitem",
label: getString("delatt-label"),
id: "zotero-itemmenu-delitemwithatt-del-att",
commandListener: (ev) => HelperExampleFactory.delAtt(),
},
{// 删除快照
tag: "menuitem",
label: getString("delsnap-label"),
id: "zotero-itemmenu-delitemwithatt-del-snap",
commandListener: (ev) => HelperExampleFactory.delSnap(),
},
{// 删除笔记
tag: "menuitem",
label: getString("delnote-label"),
id: "zotero-itemmenu-delitemwithatt-del-note",
commandListener: (ev) => HelperExampleFactory.delNote(),
},
{// 删除摘要
tag: "menuitem",
label: getString("delabstract-label"),
id: "zotero-itemmenu-delitemwithatt-del-abs",
commandListener: (ev) => HelperExampleFactory.delAbstract(),
},
{// 删除其它
tag: "menuitem",
label: getString("delextra-label"),
commandListener: (ev) => HelperExampleFactory.delExtra(),
id: "zotero-itemmenu-delitemwithatt-del-extra",
},
],
},
"before",
document.querySelector(
"#zotero-itemmenu-delitemwithatt-export-att"
) as XUL.MenuItem
);
}
}
export class HelperExampleFactory {
// 检查是否为正常条目,附件不显示修改语言
static checkIsRegularItem(item: Zotero.Item) {
return item && !item.isNote() && item.isRegularItem(); //条目存在,不为笔记,是正常条目
}
// 检查附件是否存在函数
static checkItemAtt(item: Zotero.Item) {
if (item && !item.isNote()) {
if (item.isRegularItem()) { // not an attachment already
let attachmentIDs = item.getAttachments();
for (let id of attachmentIDs) {
let attachment = Zotero.Items.get(id);
var attType = attachment.attachmentContentType;
if (attType != undefined) { //不等于undefined为有附件
return true;
}
}
}
if (item.isAttachment()) {
var attType = item.attachmentContentType;
if (attType != undefined) {
return true;
} else {
return false;
}
}
}
}
// 检查快照是否存在函数
static checkItemSnap(item: Zotero.Item) { // 检查快照
if (item && !item.isNote()) {
if (item.isRegularItem()) { // not an attachment already
let attachmentIDs = item.getAttachments();
for (let id of attachmentIDs) {
let attachment = Zotero.Items.get(id);
var attType = attachment.attachmentContentType;
// 如果是text;/html则为快照
if (attType == 'text/html') {
return true;
}
}
}
if (item.isAttachment()) {
var attType = item.attachmentContentType;
// 如果是text;/html则为快照
if (attType == 'text/html') {
return true;
} else {
return false;
}
}
}
}
// 检查笔记是否存在函数
static checkItemNote(item: Zotero.Item) { // 检查笔记
if (item && !item.isNote()) {
if (item.isRegularItem()) { // not an attachment already
var noteIDs = item.getNotes();
var withNote = ztoolkit.getGlobal("JSON").stringify(noteIDs) === '[]';
if (withNote) {
return false;
} else { //为假时含笔记
return true;
}
}
}
if (item && item.isNote()) {
return true;
}
}
// 检查其它内容是否存在函数
static checkItemExtra(item: Zotero.Item) { // 检查其它内容
if (item && !item.isNote()) {
if (item.isRegularItem()) { // not an attachment already
var extra = item.getField('extra')
var withExtra = extra === '';
if (withExtra) {
return false;//为假时其它中无内容,菜单禁用
} else {
return true;
}
}
}
}
// 检查摘要是否存在函数
static checkItemAbstract(item: Zotero.Item) { // 检查摘要
if (item && !item.isNote()) {
if (item.isRegularItem()) { // not an attachment already
var abstract = item.getField('abstractNote')
var withabstract = abstract === '';
if (withabstract) {
return false;//为假时摘要中无内容,菜单禁用
} else {
return true;
}
}
}
}
// 删除条目和附件
static async delItemAtt() {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
// var iaInfo = items.length > 1 ? 'delete-item-and-attachment-mul' : 'delete-item-and-attachment-sig';
// var truthBeTold = ztoolkit.getGlobal("confirm")(getString(iaInfo))
var truthBeTold = ztoolkit.getGlobal("confirm")(getString("delete-item-and-attachment", { args: { count: items.length } }));
if (truthBeTold) {
HelperExampleFactory.delAttDo(items); // 调用仅删除附件的函数
for (let item of items) {
if (item && !item.isNote()) {
item.deleted = true;
await item.saveTx();
}
}
BasicExampleFactory.delItemAttSucess(items); // 附件条目删除成功提示;
}
}
//删除分类条目包括附件
static async delColItemAtt() {
var collection = ZoteroPane.getSelectedCollection();
var items = collection!.getChildItems();
var truthBeTold = window.confirm(getString("delete-collection-and-attachment"))
if (truthBeTold) {
HelperExampleFactory.delAttDo(items);//删除条目
await collection!.eraseTx()
}
BasicExampleFactory.delColItemAttSucess();
}
//导出分类附件
static async colExpAtt() {
var collection = ZoteroPane.getSelectedCollection();
var items = collection!.getChildItems();
await HelperExampleFactory.expAttDo(items)
// BasicExampleFactory.exortSucess(); // 导出成功提示
}
// 仅删除附件
static async delAtt() {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
// var daoInfo = items.length > 1 ? 'delete-attachment-only-mul' : 'delete-attachment-only-sig';
// var truthBeTold = ztoolkit.getGlobal("confirm")(getString(daoInfo));
var truthBeTold = ztoolkit.getGlobal("confirm")(getString("delete-attachment-only", { args: { count: items.length } }));
if (truthBeTold) {
HelperExampleFactory.delAttDo(items); // 调用仅删除附件的函数
BasicExampleFactory.delAttSucess(); // 附件删除成功提示;
}
}
// 仅删除附件执行的函数
static async delAttDo(items: Zotero.Item[]) {
for (let item of items) {
if (item && !item.isNote()) { //2 if
if (item.isRegularItem()) { // Regular Item 一般条目//3 if
let attachmentIDs = item.getAttachments();
for (let id of attachmentIDs) { //4 for
let attachment = Zotero.Items.get(id);
let ifLinks = (attachment.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE); // 检测是否为链接模式
var file = await attachment.getFilePathAsync();
if (file && ifLinks) { // 如果文件存在(文件可能已经被删除)且为链接模式删除文件
try {
// await OS.File.remove(file); // 尝试删除文件
await Zotero.File.removeIfExists(file);
//await trash.remove(file);
} catch (error) { // 弹出错误
alert(getString("file-is-open"));
return; // 弹出错误后终止执行
}
}
// if (attachment.attachmentContentType == 'text/html' ) { // 可以筛选删除的附件类型
attachment.deleted = true; // 删除附件(快照)
await attachment.saveTx();
// }
} //4 for
} // 3 if
if (item.isAttachment()) { //附件条目 5 if
var ifLinksAtt = (item.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE); //检测是否为链接模式
var file = await item.getFilePathAsync();
if (file && ifLinksAtt) { // 如果文件存在(文件可能已经被删除)且为链接模式删除文件
try {
// await OS.File.remove(file); // 尝试删除文件
await Zotero.File.removeIfExists(file);
} catch (error) { // 弹出错误
alert(getString("file-is-open"));
return; // 弹出错误后终止执行
}
}
item.deleted = true;
await item.saveTx();
}//5if
} //2 if
}
}
// 删除快照
static async delSnap() {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
// var dsInfo = items.length > 1 ? 'delete-snapshot-mul' : 'delete-snapshot-sig'
// var truthBeTold = ztoolkit.getGlobal("confirm")(getString(dsInfo))
var truthBeTold = ztoolkit.getGlobal("confirm")(getString("delete-snapshot", { args: { count: items.length } }))
if (truthBeTold) {
for (let item of items) {
if (item && !item.isNote()) { //2 if
if (item.isRegularItem()) { // Regular Item 一般条目//3 if
let attachmentIDs = item.getAttachments();
for (let id of attachmentIDs) { //4 for
let attachment = Zotero.Items.get(id);
if (attachment.attachmentContentType == 'text/html') { //筛选删除的附件类型
attachment.deleted = true; //删除附件(快照)
await attachment.saveTx();
// 快照不处理文件
/* var file = await attachment.getFilePathAsync();
if (file) { //如果文件存在,文件可能已经被删除
await OS.File.remove(file); //删除文件
} */
}
} //4 for
} // 3 if
if (item.isAttachment()) { //附件条目 5 if
if (item.attachmentContentType == 'text/html') { //筛选删除的附件类型
// 快照不处理文件
/*var file = await item.getFilePathAsync();
if (file) { //如果文件存在,文件可能已经被删除
await OS.File.remove(file); //删除文件
} */
item.deleted = true;
await item.saveTx();
}
}//5if
} //2 if
}
BasicExampleFactory.delSnapSucess(); // 快照删除成功提示;
}
}
// 删除笔记
static async delNote() {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
// var dnInfo = items.length > 1 ? 'delete-note-mul' : 'delete-note-sig';
var truthBeTold = window.confirm(getString("delete-note", { args: { count: items.length } }))
if (truthBeTold) {
for (let item of items) {
if (item && !item.isNote()) { //2 if
if (item.isRegularItem()) { // Regular Item 一般条目//3 if
var noteIDs = item.getNotes();
for (let id of noteIDs) {
await Zotero.Items.trashTx([id]);
}
//Zotero.Items.erase(item.getNotes()); //原用函数,删除笔记不可恢复
await item.saveTx();
} // 3 if
} //2 if
if (item.isNote()) { //如果条目是笔记则直接删除
item.deleted = true;
await item.saveTx();
}//5if
}
BasicExampleFactory.delNoteSucess(); // 附件笔记成功提示;
}
}
// 删除其它
static async delExtra() {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
// var dnInfo = items.length > 1 ? 'delete-extra-mul' : 'delete-extra-sig';
// var truthBeTold = window.confirm(getString(dnInfo));
var truthBeTold = window.confirm(getString('delete-extra', { args: { count: items.length } }));
if (truthBeTold) {
for (let item of items) {
if (item.isRegularItem() && !item.isCollection()) {
try {
item.setField('extra', '');
item.save();
} catch (error) {
// numFail = numFail + 1;
}
}
}
BasicExampleFactory.delExtraSucess(); // 其它删除成功提示;
}
}
// 删除摘要
static async delAbstract() {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
// var dnInfo = items.length > 1 ? 'delete-abstract-mul' : 'delete-abstract-sig';
var truthBeTold = window.confirm(getString("delete-abstract", { args: { count: items.length } }))
if (truthBeTold) {
for (let item of items) {
if (item.isRegularItem() && !item.isCollection()) {
try {
item.setField('abstractNote', '')
item.save();
} catch (error) {
// numFail = numFail + 1;
}
}
}
BasicExampleFactory.delAbstractSucess(); // 摘要删除成功提示;
}
}
// 导出附件
// 暂不能用
static async expAtt() {
var items = ZoteroPane.getSelectedItems();
await HelperExampleFactory.expAttDo(items)
}
// 导出附件执行函数
static async expAttDo(items: Zotero.Item[]) {
var nSucess = 0; //导出成功附件个数
var nFail = 0; //导出失败附件个数
var expDir = await HelperExampleFactory.dirExp() + '\\';
// const OS = ztoolkit.getGlobal("OS");
for (let item of items) {
if (item && !item.isNote()) { //2 if
if (item.isRegularItem()) { // Regular Item 一般条目//3 if
let attachmentIDs = item.getAttachments();
for (let id of attachmentIDs) { //4 for
var file = await Zotero.Items.get(id).getFilePathAsync();
if (file) {
try {
// await exportAtts(file, expDir);
var baseName = PathUtils.filename(file); //得到文件名
var destName = PathUtils.join(expDir, baseName);
IOUtils.copy(file, destName); // 尝试导出文件
nSucess++;
//BasicExampleFactory.exortSucess(); // 导出成功提示
} catch (error) { // 弹出错误
alert('error export');
return; // 弹出错误后终止执行
}
} else {
nFail++;
//BasicExampleFactory.exortFail(); // 导出失败提示
}
} //4 for
} // 3 if
if (item.isAttachment()) { //附件条目 5 if
var file = await item.getFilePathAsync();
if (file) {
try {
// await exportAtts(file, expDir);
// var baseName = OS.Path.basename(file); //得到文件名
// var destName = OS.Path.join(expDir, baseName);
// OS.File.copy(file, destName); // 尝试导出文件
var baseName = PathUtils.filename(file); //得到文件名
var destName = PathUtils.join(expDir, baseName);
IOUtils.copy(file, destName); // 尝试导出文件
// OS.Path.join(replacement: PathUtils.join ? joinRelative ?)
// OS.Path.basename(replacement: PathUtils.filename)
// OS.Path.dirname(replacement: PathUtils.parent)
// await IOUtils.copy(srcPath, destPath);
nSucess++;
//BasicExampleFactory.exortSucess(); // 导出成功提示
} catch (error) { // 弹出错误
alert('error export');
return; // 弹出错误后终止执行
}
} else {
nFail++;
// // 导出失败提示
}
}//5if
} //2 if
}
BasicExampleFactory.exortSucess(nSucess)
BasicExampleFactory.exortFail(nFail);
}
// 选择存贮路径
@example
static async dirExp() {
const path = await new ztoolkit.FilePicker(
getString("exort-dir"),
"folder"
).open();
return path;
// ztoolkit.getGlobal("alert")(`Selected ${path}`);
}
// 修改语言
@example
static async chanLan() {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
var pattern = new RegExp("[\u4E00-\u9FA5]+");
for (let item of items) {
var title = String(item.getField("title"));
if (Zotero.ItemTypes.getName(item.itemTypeID) == 'computerProgram' // 文献类型为软件时返回
) {
continue;
}
var lan = pattern.test(title) ? 'zh-CN' : 'en-US';
try {
if (item && !item.isNote() && item.isRegularItem() &&
Zotero.ItemTypes.getName(item.itemTypeID) != 'computerProgram') { //正常条目,非笔记设置语言
item.setField("language", lan);
await item.saveTx();
}
} catch (error) {
Zotero.debug(`语言设置失败,可能条目类型${Zotero.ItemTypes.getName(item.itemTypeID)}不能设置语言。`)
}
}
if (items.every((item) => // 如果都是软件提示错误
Zotero.ItemTypes.getName(item.itemTypeID) == 'computerProgram')) {
BasicExampleFactory.softLanFail();
} else {
BasicExampleFactory.lanSetSucess(); // 语言设置成功提示;
}
}
}