Skip to content

Commit

Permalink
更新为0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
redleafnew committed Jan 25, 2022
1 parent b7dd050 commit 1637ae4
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 35 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

2. 将所选条目语言字段设置为`en`

3. 将附件导出。

# Usage

1. Remove linked attachment(s) when delete the item(s) or collection.
Expand All @@ -15,6 +17,8 @@

2. Set the language field of selected items as `en`.

3. Export attachment(s).

# License
The source code is released under GNU General Public License, version 3.0

Expand Down
21 changes: 11 additions & 10 deletions chrome/content/overlay.xul
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
id="zotero-collectionmenu-delitem"
label="&zotero.delitem.delitems.label;"
oncommand="Zotero.DelItem.DelColl()"/>
<menuitem
id="zotero-collectionmenu-exp-att"
label="&zotero.delitem.exp.coll.atts.label;"
oncommand="Zotero.DelItem.ExpColl()"/>
</menupopup>

<menupopup id="zotero-itemmenu">
Expand All @@ -31,18 +35,15 @@
<menuitem id="zotero-itemmenu-delnote" label="&zotero.delitem.delnote.label;" oncommand="Zotero.DelItem.DelNote()"/> <!-- 仅删除笔记 -->
</menupopup>
</menu>

<!-- 导出附件 -->
<menuitem id="zotero-itemmenu-exp-att" label="&zotero.itemmenu.exp.att.label;"
tooltiptext="&zotero.exp.att.tip;"
oncommand="Zotero.DelItem.ExpAtts()"/> <!-- 导出附件 -->

<!-- 设置语言 -->
<!-- <menu id="zotero-itemmenu-chanlan-namehandler" -->
<!-- label="&zotero.chanlan.namehandler.label;" -->
<!-- oncommand="Zotero.DelItem.chanLanForSel()" -->
<!-- toolkeytip="Set the language field of the selcted item(s) to en">-->
<!-- <menupopup id="chanlan">-->
<menuitem id="zotero-chanlan-forsel" label="&zotero.chanlan-forsel.label;"
<menuitem id="zotero-chanlan-forsel" label="&zotero.chanlan-forsel.label;"
tooltiptext="&zotero.chanlan.Set.lan.to.en;"
oncommand="Zotero.DelItem.chanLanForSel()"/> <!-- 所选条目设为en -->
<!-- <menuitem id="zotero-chanlan-forempty" label="&zotero.chanlan-forempty.label;" oncommand="Zotero.DelItem.chanLanForEmpty()"/> 语言字段为空时设置 -->
<!-- </menupopup> -->
<!-- </menu>-->

</menupopup>
</overlay>
180 changes: 159 additions & 21 deletions chrome/content/scripts/delitem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Zotero.DelItem = {

DelItem: async function () {//右击时删除条目调用的函数

var zoteroPane = Zotero.getActiveZoteroPane();
Expand Down Expand Up @@ -182,6 +182,112 @@ Zotero.DelItem = {
}
},

// 导出附件
// 右击导出分类调用的函数
ExpColl: async function () {
var collection = ZoteroPane.getSelectedCollection();
var items = collection.getChildItems();
Zotero.DelItem.ExpAtt(items);
},

// 右击分类导出附件
ExpAtts: async function () {
var zoteroPane = Zotero.getActiveZoteroPane();
var items = zoteroPane.getSelectedItems();
Zotero.DelItem.ExpAtt(items);
},
// 导出附件实际调用的函数
ExpAtt: async function (items) {
var expDir = await this.chooseDirectory(); //得到导出的目录
var nItems = 0; //导出个数计数器
var lanUI = Zotero.Prefs.get('intl.locale.requested', true); // 得到当前Zotero界面语言
var whiteSpace = ' ';
if (lanUI == 'zh-CN') {whiteSpace = ''};
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);
var file = await attachment.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); // 尝试导出文件
nItems = nItems + 1;
} catch (error) { // 弹出错误
alert (Zotero.DelItem.diwaGetString("file.export.error"));
return; // 弹出错误后终止执行
}

}

} //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); // 尝试导出文件
nItems = nItems + 1;
} catch (error) { // 弹出错误
alert (Zotero.DelItem.diwaGetString("file.export.error"));
return; // 弹出错误后终止执行
}
}

}//5if
} //2 if

}
alert (nItems + whiteSpace + Zotero.DelItem.diwaGetString("file.exported") + whiteSpace + expDir + Zotero.DelItem.diwaGetString("full.stop"));
},

// 导出附件函数
exportAtts: async function (file, expDir) {
var baseName = OS.Path.basename(file); //得到文件名
var destName = OS.Path.join(expDir, baseName);
OS.File.copy(file, destName); // 尝试导出文件
},

//对话框 Form ZotFile
/**
* Choose directory from file picker
* @return {string} Path to file
*/
chooseDirectory: async function () {
if (Zotero.platformMajorVersion >= 60) {
var FilePicker = require('zotero/filePicker').default;
}
else {
var nsIFilePicker = Components.interfaces.nsIFilePicker;
}
var wm = Services.wm;
var win = wm.getMostRecentWindow('navigator:browser');
var ps = Services.prompt;
if (Zotero.platformMajorVersion >= 60) {
var fp = new FilePicker();
fp.init(win, Zotero.DelItem.diwaGetString("file.exp.path"), fp.modeGetFolder);
fp.appendFilters(fp.filterAll);
if (await fp.show() != fp.returnOK) return '';
return fp.file;
}
else {
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(win, Zotero.DelItem.diwaGetString("file.exp.path"), nsIFilePicker.modeGetFolder);
fp.appendFilters(nsIFilePicker.filterAll);
if (fp.show() != nsIFilePicker.returnOK) return '';
var file = fp.file;
return file.path;
}
},

// 将所有所选条目语言字段设为en
chanLanForSel: async function () {
var zoteroPane = Zotero.getActiveZoteroPane();
Expand Down Expand Up @@ -298,39 +404,62 @@ Zotero.DelItem = {
return true;}
},

// 检查是否为链接
// true为链接或没有附件
checkItemExp: function (item){
var num = 0;
if (item && !item.isNote()) {
if (item.isRegularItem()) { // not an attachment already
let attachmentIDs = item.getAttachments();
//return attachmentIDs.length; // 返回数据中元素个数
for (let id of attachmentIDs) {
let attachment = Zotero.Items.get(id);
num = num + attachment.attachmentLinkMode; // attachmentLinkMode 链接类型 3为链接 2为文件

}
var linkURL = attachmentIDs.length == 1 && num == 3
if (linkURL || attachmentIDs.length == 0) {return true;}
}

if (item.isAttachment()) {
//var attType = item.attachmentContentType;
if (item.attachmentLinkMode == 3) {return true;}
}
}

},
// 是否显示菜单函数
displayMenuitem: function () { // 如果无附件则不显示菜单
var pane = Services.wm.getMostRecentWindow("navigator:browser")
.ZoteroPane;
var collection = ZoteroPane.getSelectedCollection();
var items = pane.getSelectedItems();
if (collection) {var items_coll = collection.getChildItems();}
//Zotero.debug("**Jasminum selected item length: " + items.length);
var showMenuAtt = items.some((item) => Zotero.DelItem.checkItemAtt(item)); // 检查附件
var showMenuSnap = items.some((item) => Zotero.DelItem.checkItemSnap(item)); // 检查快照
var showMenuNote = items.some((item) => Zotero.DelItem.checkItemNote(item)); // 检查快照
var showMenuNote = items.some((item) => Zotero.DelItem.checkItemNote(item)); // 检查笔记
var showMenuColl = (collection == false); // 非正常文件夹,如我的出版物、重复条目、未分类条目、回收站,为false,此时返回值为true,隐藏菜单
//pane.document.getElementById("id-delitem-separator").hidden = !( // 分隔条是否出现
// showMenuAtt ||
// showMenuSnap);

/*pane.document.getElementById( //总菜单
// "zotero-itemmenu-delitem-namehandler"
// ).disabled = !( // 总菜单是否可用
// showMenuAtt ||
// showMenuSnap);*/
/*pane.document.getElementById( //删除条目和附件
"zotero-itemmenu-delitem"
).disabled = !( // 删除条目和附件是否可用
showMenuAtt ||
showMenuSnap);*/
if (collection) { // 如果是正常分类才显示
var showMenuCollExp = items_coll.some((item) => Zotero.DelItem.checkItemAtt(item));} else {
var showMenuCollExp = false;} // 检查分类是否有附件及是否为正常分类
var showMenuExpAtt = items.every((item) => Zotero.DelItem.checkItemExp(item)); //检查是否为链接 true为链接或没有附件
// pane.document.getElementById( // 其他分类菜单是否可见 总的id:zotero-collectionmenu
// "zotero-collectionmenu"
// ).hidden = showMenuColl; //


pane.document.getElementById( // 删除分类/文件夹菜单是否可见
pane.document.getElementById( // 删除分类/文件夹菜单是否可见
"zotero-collectionmenu-delitem"
).hidden = showMenuColl; // 仅删除附件菜单是否可用
pane.document.getElementById( // 删除分类/文件夹分隔条是否可见
).hidden = showMenuColl; // 仅删除附件菜单是否可用 zotero-collectionmenu-delitem

pane.document.getElementById( // 删除分类/文件夹分隔条是否可见 id-delcoll-separator
"id-delcoll-separator"
).hidden = showMenuColl; // 仅删除附件菜单是否可用
).hidden = showMenuColl; //

pane.document.getElementById( // 导出分类附件是否可见 zotero-collectionmenu-exp-att
"zotero-collectionmenu-exp-att"
).hidden = showMenuColl; //

pane.document.getElementById( // 仅删除附件菜单
"zotero-itemmenu-delatt"
).disabled = !showMenuAtt; // 仅删除附件菜单是否可用
Expand All @@ -342,6 +471,15 @@ Zotero.DelItem = {
pane.document.getElementById( // 仅删除笔记菜单
"zotero-itemmenu-delnote"
).disabled = !showMenuNote;// 仅删除笔记是否可用

pane.document.getElementById( // 分类导出附件
"zotero-collectionmenu-exp-att"
).disabled = !showMenuCollExp ; // 分类导出附件菜单是否可用

pane.document.getElementById( // 条目导出附件
"zotero-itemmenu-exp-att"
).disabled = !showMenuAtt || showMenuExpAtt; // 条目导出附件菜单是否可用


},
};
Expand Down
4 changes: 4 additions & 0 deletions chrome/locale/en-US/diwa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ delete.attachment.only = Are you sure you want to delete the attach
delete.snapshot = Are you sure you want to delete the snapshot(s) of the item(s)?
delete.note = Are you sure you want to delete the note(s) of the item(s)?
file.is.open = The file can not be deleted. Please close the file if you have opened it and try again.
file.export.error = The file exporting error. Please close the file if you have opened it and try again.
file.exported = attachment (s) was (were) exported to
file.exp.path = Please choose the directory to store the files
full.stop = .
4 changes: 4 additions & 0 deletions chrome/locale/en-US/overlay.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<!ENTITY zotero.delitem.delsnap.label "Delete Snapshot(s) only">
<!ENTITY zotero.delitem.delnote.label "Delete Note(s) only">
<!ENTITY zotero.delitem.namehandler.label "Delete Attachment(s)">
<!-- 导出附件 -->
<!ENTITY zotero.delitem.exp.coll.atts.label "Export Attachment(s)">
<!ENTITY zotero.itemmenu.exp.att.label "Export Attachment(s)">
<!ENTITY zotero.exp.att.tip "Export Attachment(s)">
<!-- 设置语言 -->
<!ENTITY zotero.chanlan-forempty.label "If the Language of Selected Item(s) Is Empty">
<!ENTITY zotero.chanlan-forsel.label "Set Language Field as en">
Expand Down
4 changes: 4 additions & 0 deletions chrome/locale/zh-CN/diwa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ delete.attachment.only = 您确定将所选条目的附件移动到
delete.snapshot = 您确定将所选条目的快照移动到回收站?
delete.note = 您确定将所选条目的笔记删除?
file.is.open = 无法删除文件。如果文件已经打开,请关闭后重试。
file.export.error = 附件导出出错。如果文件已经打开,请关闭后重试。
file.exported = 个附件导出到
file.exp.path = 请选择导出附件存放的目录
full.stop =
4 changes: 4 additions & 0 deletions chrome/locale/zh-CN/overlay.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<!ENTITY zotero.delitem.delsnap.label "仅删除快照">
<!ENTITY zotero.delitem.delnote.label "仅删除笔记">
<!ENTITY zotero.delitem.namehandler.label "删除附件">
<!-- 导出附件 -->
<!ENTITY zotero.delitem.exp.coll.atts.label "导出附件">
<!ENTITY zotero.itemmenu.exp.att.label "导出附件">
<!ENTITY zotero.exp.att.tip "导出附件">
<!-- 设置语言 -->
<!ENTITY zotero.chanlan-forempty.label "所选条目语言为空时设为en">
<!ENTITY zotero.chanlan-forsel.label "将语言字段设为en">
Expand Down
4 changes: 3 additions & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
RDF:about="urn:mozilla:install-manifest"
em:id="[email protected]"
em:name="Del Item With Att"
em:version="0.0.12"
em:version="0.0.13"
em:type="2"
em:creator="Minyi Han"
em:description="Delete Item(s) With Attachment(s)"
Expand Down Expand Up @@ -48,7 +48,9 @@
<Description>
<em:locale>zh-CN</em:locale>
<em:name> delitem</em:name>
<em:creator>Minyi Han</em:creator>
<em:description>删除条目的同时删除附件</em:description>
<em:homepageURL>https://github.com/redleafnew/delitemwithatt</em:homepageURL>
</Description>
</em:localized>
</RDF:Description>
Expand Down
6 changes: 3 additions & 3 deletions update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<em:version>0.0.12</em:version>
<em:version>0.0.13</em:version>
<em:targetApplication>
<rdf:Description>
<em:id>[email protected]</em:id>
<em:minVersion>5.0.0</em:minVersion>
<em:maxVersion>5.*</em:maxVersion>
<em:updateLink>https://github.com/redleafnew/delitemwithatt/releases/download/0.0.12/delitemwithatt.xpi</em:updateLink>
<em:updateLink>https://github.com/redleafnew/delitemwithatt/releases/download/0.0.13/delitemwithatt.xpi</em:updateLink>
</rdf:Description>
</em:targetApplication>

Expand All @@ -21,7 +21,7 @@
<em:id>[email protected]</em:id>
<em:minVersion>4.999</em:minVersion>
<em:maxVersion>5.*</em:maxVersion>
<em:updateLink>https://github.com/redleafnew/delitemwithatt/releases/download/0.0.12/delitemwithatt.xpi</em:updateLink>
<em:updateLink>https://github.com/redleafnew/delitemwithatt/releases/download/0.0.13/delitemwithatt.xpi</em:updateLink>
</rdf:Description>
</em:targetApplication>

Expand Down

0 comments on commit 1637ae4

Please sign in to comment.