Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tablet annotation behavior #482

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion chrome/content/zotfile/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@ var updatePreferenceWindow = function (which) {
if(which=="subfolder-tablet" || which=="all") disablePreference("tablet-subfolder", "tablet-subfolderFormat", revert);

// storeCopyOfFile-tablet
if(which=="storeCopyOfFile" || which=="all") disablePreference("tablet-storeCopyOfFile", "tablet-storeCopyOfFile_suffix", revert);
if(which=="storeCopyOfFile" || which=="all"){
disablePreference("tablet-storeCopyOfFile", "tablet-storeCopyOfFile_suffix", revert);
}

// storeCopyOfFile or tablet-rename
if(which=="storeCopyOfFile" || which=="tablet-rename" || which=="all"){
var storecopy = document.getElementById('pref-zotfile-tablet-storeCopyOfFile').value;
var rename = document.getElementById('pref-zotfile-tablet-rename').value
// the value is the status before clicking
if (which == "storeCopyOfFile") storecopy = !storecopy;
if (which == "tablet-rename") rename = !rename;
// enable keep-one-annotated option when storeCopyOfFile is on but tablet-rename is off
var enable_keep_one_annotated = storecopy && !rename;
document.getElementById('id-zotfile-tablet-keep-one-annotated').disabled = !enable_keep_one_annotated;
}

// batch renaming
if(which=="confirm" || which=="all") disablePreference("confirmation_batch_ask", "confirmation_batch", revert);
Expand Down
4 changes: 3 additions & 1 deletion chrome/content/zotfile/options.xul
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<preference id="pref-zotfile-confirmation_batch" name="extensions.zotfile.confirmation_batch" type="int"/>
<preference id="pref-zotfile-tablet" name="extensions.zotfile.tablet" type="bool"/>
<preference id="pref-zotfile-tablet-rename" name="extensions.zotfile.tablet.rename" type="bool"/>
<preference id="pref-zotfile-tablet-keep-one-annotated" name="extensions.zotfile.tablet.keepOneAnnotated" type="bool"/>
<preference id="pref-zotfile-tablet-dest_dir" name="extensions.zotfile.tablet.dest_dir" type="string"/>
<preference id="pref-zotfile-tablet-storeCopyOfFile" name="extensions.zotfile.tablet.storeCopyOfFile" type="bool"/>
<preference id="pref-zotfile-tablet-storeCopyOfFile_suffix" name="extensions.zotfile.tablet.storeCopyOfFile_suffix" type="string"/>
Expand Down Expand Up @@ -165,12 +166,13 @@

<groupbox>
<caption label="&additional-options;"/>
<checkbox id="id-zotfile-tablet-rename" label="&rename-when-sent-to-tablet;" preference="pref-zotfile-tablet-rename"/>
<checkbox id="id-zotfile-tablet-rename" label="&rename-when-sent-to-tablet;" preference="pref-zotfile-tablet-rename" oncommand="updatePreferenceWindow('tablet-rename')"/>
<hbox style="margin: 0" align="center">
<checkbox id="id-zotfile-tablet-storeCopyOfFile" label="&save-annotated-file;" preference="pref-zotfile-tablet-storeCopyOfFile" oncommand="updatePreferenceWindow('storeCopyOfFile')"/>
<textbox id="id-zotfile-tablet-storeCopyOfFile_suffix" preference="pref-zotfile-tablet-storeCopyOfFile_suffix" width="130"/>
<image src="chrome://zotfile/skin/information.png" tooltiptext="&tablet-copy-information;"/>
</hbox>
<checkbox id="id-zotfile-tablet-keep-one-annotated" label="&keep-one-annotated;" preference="pref-zotfile-tablet-keep-one-annotated"/>
<checkbox id="id-zotfile-pdfExtraction-Pull" label="&automatically-extract-annotations;" preference="pref-zotfile-pdfExtraction-Pull"/>
</groupbox>
</tabpanel>
Expand Down
31 changes: 16 additions & 15 deletions chrome/content/zotfile/overlay.xul
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,27 @@
<menuseparator/>
<menuitem label="&warning.label;" disabled="true" style="font-size: 100%; background: none; -moz-appearance: none;"/>
<menuitem id="id-push2reader-00" label="&send-to-tablet;" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet();"/>
<menuitem id="id-pushannotation2reader-annotation" label="&send-annotation-to-tablet;" tooltiptext="&send-annotation-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(true);"/>
<menuitem label="&update-file-modification-time;" tooltiptext="&sync-attachment-file;" oncommand="Zotero.ZotFile.Tablet.updateSelectedTabletAttachments();"/>
<menuitem label="&get-from-tablet;" tooltiptext="&get-attachment-file-from-tablet;" oncommand="Zotero.ZotFile.Tablet.getSelectedAttachmentsFromTablet();"/>
<menuseparator/>
<menuitem label="&send-to-subfolder-on-tablet;" disabled="true" style="font-size: 80%; background: none; -moz-appearance: none;"/>
<menuitem label="&warning.label;" disabled="true" style="font-size: 100%; background: none; -moz-appearance: none;"/>
<menuitem id="id-push2reader-01" hidden="true" label="menu1" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(0);"/>
<menuitem id="id-push2reader-02" hidden="true" label="menu2" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(1);"/>
<menuitem id="id-push2reader-03" hidden="true" label="menu3" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(2);"/>
<menuitem id="id-push2reader-04" hidden="true" label="menu4" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(3);"/>
<menuitem id="id-push2reader-05" hidden="true" label="menu5" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(4);"/>
<menuitem id="id-push2reader-06" hidden="true" label="menu6" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(5);"/>
<menuitem id="id-push2reader-07" hidden="true" label="menu7" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(6);"/>
<menuitem id="id-push2reader-08" hidden="true" label="menu8" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(7);"/>
<menuitem id="id-push2reader-09" hidden="true" label="menu9" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(8);"/>
<menuitem id="id-push2reader-10" hidden="true" label="menu10" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(9);"/>
<menuitem id="id-push2reader-11" hidden="true" label="menu11" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(10);"/>
<menuitem id="id-push2reader-12" hidden="true" label="menu12" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(11);"/>
<menuitem id="id-push2reader-13" hidden="true" label="menu13" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(12);"/>
<menuitem id="id-push2reader-14" hidden="true" label="menu14" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(13);"/>
<menuitem id="id-push2reader-15" hidden="true" label="menu15" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(14);"/>
<menuitem id="id-push2reader-01" hidden="true" label="menu1" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 0);"/>
<menuitem id="id-push2reader-02" hidden="true" label="menu2" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 1);"/>
<menuitem id="id-push2reader-03" hidden="true" label="menu3" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 2);"/>
<menuitem id="id-push2reader-04" hidden="true" label="menu4" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 3);"/>
<menuitem id="id-push2reader-05" hidden="true" label="menu5" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 4);"/>
<menuitem id="id-push2reader-06" hidden="true" label="menu6" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 5);"/>
<menuitem id="id-push2reader-07" hidden="true" label="menu7" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 6);"/>
<menuitem id="id-push2reader-08" hidden="true" label="menu8" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 7);"/>
<menuitem id="id-push2reader-09" hidden="true" label="menu9" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 8);"/>
<menuitem id="id-push2reader-10" hidden="true" label="menu10" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 9);"/>
<menuitem id="id-push2reader-11" hidden="true" label="menu11" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 10);"/>
<menuitem id="id-push2reader-12" hidden="true" label="menu12" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 11);"/>
<menuitem id="id-push2reader-13" hidden="true" label="menu13" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 12);"/>
<menuitem id="id-push2reader-14" hidden="true" label="menu14" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 13);"/>
<menuitem id="id-push2reader-15" hidden="true" label="menu15" tooltiptext="&send-attachment-file-to-tablet;" oncommand="Zotero.ZotFile.Tablet.sendSelectedAttachmentsToTablet(false, 14);"/>

<menuseparator/>
<menuitem id="id-push2reader-configure" label="&change-subfolders;" tooltiptext="&add-and-delete-subfolders;" oncommand="Zotero.ZotFile.openSubfolderWindow();"/>
Expand Down
125 changes: 93 additions & 32 deletions chrome/content/zotfile/tablet.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,11 @@ Zotero.ZotFile.Tablet = new function() {

/**
* Send selected Zotero attachments to tablet
* @param {bool} annotation_only whether only select the annotation file.
* @param {int} idx_subfolder Index of subfolder
* @yield {void}
*/
this.sendSelectedAttachmentsToTablet = Zotero.Promise.coroutine(function* (idx_subfolder) {
this.sendSelectedAttachmentsToTablet = Zotero.Promise.coroutine(function* (annotation_only, idx_subfolder) {
// get selected attachments
var atts = Zotero.Items.get(this.getSelectedAttachments())
.filter(this.checkFileType);
Expand All @@ -482,6 +483,40 @@ Zotero.ZotFile.Tablet = new function() {
project_folder = subfolders[idx_subfolder].path;
}
}
var annotation_only = typeof annotation_only !== 'undefined' ? annotation_only : false;
// only choose the longest `suffix` (A_suffix_suffix > A_suffix > A)
if (annotation_only) {
var set = new Set();
for (let i = 0; i < atts.length; i++) {
var att = atts[i];
// parentid.filename
var filename = att.attachmentFilename;
var filetype = this.Utils.getFiletype(filename);
var re = new RegExp("\." + filetype + "$", 'i');
if(filetype != '') filename = filename.replace(re, "");
var suffix_re = new RegExp(this.getPref('tablet.storeCopyOfFile_suffix'), 'i');
var rmsuffix = filename.replace(suffix_re, "");
while(rmsuffix != filename) {
set.add(att.parentItemID + "." + rmsuffix);
filename = rmsuffix;
rmsuffix = filename.replace(suffix_re, "");
}
}
var i = 0;
while(i < atts.length) {
var att = atts[i];
var filename = att.attachmentFilename;
var filetype = this.Utils.getFiletype(filename);
var re = new RegExp("\." + filetype + "$", 'i');
if(filetype != '') filename = filename.replace(re, "");
var key = att.parentItemID + "." + filename;
if (set.has(key)) {
atts.splice(i, 1);
} else {
i++;
}
}
}
// confirm
var atts_tablet = yield Zotero.Promise.filter(atts, att =>
this.Tablet.getTabletStatus(att) && this.Tablet.getTabletFilePath(att, false)),
Expand Down Expand Up @@ -606,6 +641,7 @@ Zotero.ZotFile.Tablet = new function() {
var item = Zotero.Items.get(att.parentItemID),
tablet_status = 1,
item_pulled = false,
annotated_pulled = false,
att_deleted = false,
att_mode = this.Tablet.getInfo(att, 'mode'),
attSubfolder = this.Tablet.getInfo(att, 'projectFolder').trim();
Expand Down Expand Up @@ -651,36 +687,61 @@ Zotero.ZotFile.Tablet = new function() {
item_pulled = true;
}
}
// if saving a copy of the file as a new attachment with suffix and reader file was modified
if (this.getPref('tablet.storeCopyOfFile') && tablet_status != 2) {
var filename = this.Utils.addSuffix(OS.Path.basename(path_zotero), this.getPref('tablet.storeCopyOfFile_suffix'));
//add linked attachment
if (item.library.libraryType == 'user' && !this.getPref('import')) {
let path_copied = OS.Path.join(OS.Path.dirname(path_zotero), filename);
yield OS.File.move(path_tablet, path_copied);
var options = {file: path_copied, libraryID: item.libraryID, parentItemID: item.id, collections: undefined};
att_new = yield Zotero.Attachments.linkFromFile(options);
item_pulled = true;
// extract annotations from attachment and add note
if (this.getPref('pdfExtraction.Pull'))
this.Tablet.extractPdfs.push(att_new.id);
}
//imports attachment
if (item.library.libraryType != 'user' || this.getPref('import')) {
// import file on reader
var options = {file: path_tablet, libraryID: item.libraryID, parentItemID: item.id, collections: undefined};
att_new = yield Zotero.Attachments.importFromFile(options);
// rename file associated with attachment
yield att_new.renameAttachmentFile(filename);
// change title of attachment item
att_new.setField('title', filename);
yield att_new.saveTx();
// remove file on reader
yield OS.File.remove(path_tablet);
item_pulled = true;
// extract annotations from attachment and add note
if (this.getPref('pdfExtraction.Pull'))
this.Tablet.extractPdfs.push(att_new.id);

// add filetype to suffix
var suffix = this.getPref('tablet.storeCopyOfFile_suffix');
var filetype = this.Utils.getFiletype(path_zotero);
if(filetype != '') suffix = suffix + '.' + filetype;
if (this.getPref('tablet.storeCopyOfFile') && tablet_status != 2) {
// move the file when keepOneAnnotated and storeCopyOfFile are true and the filename is end with storeCopyOfFile_suffix.
// otherwise, import the file as new attachment with given suffix.
// except for setting annotated_pulled, the behavior is same as previous replacement.
if (this.getPref('tablet.keepOneAnnotated') && path_zotero.endsWith(suffix)) {
// prompt if both file have been modified
if (tablet_status == 1) {
tablet_status = this.promptUser(this.ZFgetString('tablet.fileConflict', [att.attachmentFilename]),
this.ZFgetString('tablet.fileConflict.replaceZ'),
this.ZFgetString('general.cancel'),
this.ZFgetString('tablet.fileConflict.removeT'));
//att_deleted is true to display a special message when the attachments have been deleted from tablet without being sent back to Zotero
if (tablet_status == 2) att_deleted = true;
}
// replace zotero file
if(tablet_status == 0) {
yield OS.File.move(path_tablet, path_zotero);
item_pulled = true;
annotated_pulled = true;
}
} else {
var filename = this.Utils.addSuffix(OS.Path.basename(path_zotero), this.getPref('tablet.storeCopyOfFile_suffix'));
//add linked attachment
if (item.library.libraryType == 'user' && !this.getPref('import')) {
let path_copied = OS.Path.join(OS.Path.dirname(path_zotero), filename);
yield OS.File.move(path_tablet, path_copied);
var options = {file: path_copied, libraryID: item.libraryID, parentItemID: item.id, collections: undefined};
att_new = yield Zotero.Attachments.linkFromFile(options);
item_pulled = true;
// extract annotations from attachment and add note
if (this.getPref('pdfExtraction.Pull'))
this.Tablet.extractPdfs.push(att_new.id);
}
//imports attachment
if (item.library.libraryType != 'user' || this.getPref('import')) {
// import file on reader
var options = {file: path_tablet, libraryID: item.libraryID, parentItemID: item.id, collections: undefined};
att_new = yield Zotero.Attachments.importFromFile(options);
// rename file associated with attachment
yield att_new.renameAttachmentFile(filename);
// change title of attachment item
att_new.setField('title', filename);
yield att_new.saveTx();
// remove file on reader
yield OS.File.remove(path_tablet);
item_pulled = true;
// extract annotations from attachment and add note
if (this.getPref('pdfExtraction.Pull'))
this.Tablet.extractPdfs.push(att_new.id);
}
}
}
// Pull without replacement (i.e. remove file on tablet)
Expand Down Expand Up @@ -716,7 +777,7 @@ Zotero.ZotFile.Tablet = new function() {
// clear attachment note
this.Tablet.clearInfo(att);
// extract annotations from attachment and add note
if (this.getPref('pdfExtraction.Pull') && tablet_status != 2 && !this.getPref('tablet.storeCopyOfFile'))
if (this.getPref('pdfExtraction.Pull') && tablet_status != 2 && (!this.getPref('tablet.storeCopyOfFile') || annotated_pulled))
this.Tablet.extractPdfs.push(att.id);
// remove tag from parent item
var tag_parent = this.getPref('tablet.tagParentPush_tag');
Expand Down
Loading