Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
add: preference settings
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyu committed Jul 14, 2022
1 parent ec07cdb commit 8ca7e9c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 44 deletions.
1 change: 1 addition & 0 deletions addon/chrome.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ locale __addonRef__ en-US chrome/locale/en-US/
locale __addonRef__ zh-CN chrome/locale/zh-CN/

overlay chrome://zotero/content/zoteroPane.xul chrome://__addonRef__/content/overlay.xul
overlay chrome://zotero/content/preferences/preferences.xul chrome://__addonRef__/content/preferences.xul
4 changes: 0 additions & 4 deletions addon/chrome/content/overlay.xul
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
<overlay id="__addonRef__" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script src="chrome://__addonRef__/content/scripts/index.js" />

<menupopup id="menu_viewPopup">
<menuitem id="menu_autopreview_pdfpreview" type="checkbox" label="&zotero.__addonRef__.autopreview.label;" oncommand="Zotero.PDFPreview.events.setAutoPreview();" />
</menupopup>

<tabbox id="zotero-view-tabbox">
<tabs id="zotero-editpane-tabs">
<tab id="pdf-preview-tab" label="&zotero.__addonRef__.tab.label;"></tab>
Expand Down
28 changes: 28 additions & 0 deletions addon/chrome/content/preferences.xul
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!DOCTYPE window SYSTEM "chrome://__addonRef__/locale/overlay.dtd">

<overlay id="__addonRef__-preferences" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml">

<prefwindow id="zotero-prefs">
<prefpane id="zotero-prefpane-__addonRef__" insertafter="zotero-prefpane-advanced" label="&zotero.__addonRef__.pref.label;" image="chrome://__addonRef__/skin/favicon.png">
<preferences id="zotero-preferences-__addonRef__">
<preference id="pref-__addonRef__-enable" name="extensions.zotero.__addonRef__.enable" type="bool" />
<preference id="pref-__addonRef__-autoPreview" name="extensions.zotero.__addonRef__.autoPreview" type="bool" />
<preference id="pref-__addonRef__-previewPageNum" name="extensions.zotero.__addonRef__.previewPageNum" type="string" />
</preferences>
<rows>
<row>
<checkbox id="zotero-prefpane-__addonRef__-settings-enable" preference="pref-__addonRef__-enable" label="&zotero.__addonRef__.pref.enable.label;" />
</row>
<row>
<checkbox id="zotero-prefpane-__addonRef__-settings-autoPreview" preference="pref-__addonRef__-autoPreview" label="&zotero.__addonRef__.pref.autoPreview.label;" />
</row>
<row>
<label control="zotero-prefpane-__addonRef__-settings-previewPageNum" value="&zotero.__addonRef__.pref.previewPageNumBefore.label;" />
<textbox id="zotero-prefpane-__addonRef__-settings-previewPageNum" preference="pref-__addonRef__-previewPageNum" />
<label value="&zotero.__addonRef__.pref.previewPageNumAfter.label;" />
</row>
</rows>
</prefpane>
</prefwindow>
</overlay>
6 changes: 5 additions & 1 deletion addon/chrome/content/previewPDF.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@
try {
viewer = document.getElementById("pdf-viewer");
viewer.innerHTML = "";
let lastNumber = pdf.numPages > 10 ? 10 : pdf.numPages;
const maxPageNum = Number(
Zotero.Prefs.get("pdfpreview.previewPageNum")
);
let lastNumber =
pdf.numPages > maxPageNum ? maxPageNum : pdf.numPages;
for (i = 1; i <= lastNumber; i++) {
if (id !== itemID) {
// Stop current render if there is a new item
Expand Down
7 changes: 6 additions & 1 deletion addon/chrome/locale/en-US/overlay.dtd
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<!ENTITY zotero.__addonRef__.pref.label "Preview">
<!ENTITY zotero.__addonRef__.pref.enable.label "Enable Preview">
<!ENTITY zotero.__addonRef__.pref.autoPreview.label "Auto Preview PDF">
<!ENTITY zotero.__addonRef__.pref.previewPageNumBefore.label "Preview First">
<!ENTITY zotero.__addonRef__.pref.previewPageNumAfter.label "Pages">

<!ENTITY zotero.__addonRef__.tab.label "Preview">
<!ENTITY zotero.__addonRef__.autopreview.label "Auto Preview PDF">

<!ENTITY zotero.__addonRef__.help.feedback.caption.label "User Guide and Feedback">
<!ENTITY zotero.__addonRef__.help.feedback.label "GitHub">
Expand Down
7 changes: 6 additions & 1 deletion addon/chrome/locale/zh-CN/overlay.dtd
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<!ENTITY zotero.__addonRef__.pref.label "预览">
<!ENTITY zotero.__addonRef__.pref.enable.label "启用预览">
<!ENTITY zotero.__addonRef__.pref.autoPreview.label "自动预览PDF">
<!ENTITY zotero.__addonRef__.pref.previewPageNumBefore.label "预览前">
<!ENTITY zotero.__addonRef__.pref.previewPageNumAfter.label "页">

<!ENTITY zotero.__addonRef__.tab.label "预览">
<!ENTITY zotero.__addonRef__.autopreview.label "自动预览PDF">

<!ENTITY zotero.__addonRef__.help.feedback.caption.label "User Guide and Feedback">
<!ENTITY zotero.__addonRef__.help.feedback.label "GitHub">
Expand Down
3 changes: 3 additions & 0 deletions addon/defaults/preferences/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pref("extensions.zotero.pdfpreview.enable", true);
pref("extensions.zotero.pdfpreview.autoPreview", true);
pref("extensions.zotero.pdfpreview.previewPageNum", "10");
36 changes: 0 additions & 36 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ class AddonEvents extends AddonBase {
public async onInit() {
Zotero.debug("PDFPreview: init called");
await Zotero.uiReadyPromise;
this.resetState();
this.initItemSelectListener();
this.initPreviewResizeListener();
this.updateAutoPreview();
}

private initItemSelectListener() {
Expand All @@ -23,30 +21,6 @@ class AddonEvents extends AddonBase {
});
}

public setAutoPreview() {
let autoPreview = Zotero.Prefs.get("PDFPreview.autoPreview");
autoPreview = !autoPreview;
Zotero.Prefs.set("PDFPreview.autoPreview", autoPreview);
this.updateAutoPreview(autoPreview);
}

private updateAutoPreview(autoPreview: boolean = undefined) {
Zotero.debug("updateAutoPreview");

if (typeof autoPreview === "undefined") {
autoPreview = Zotero.Prefs.get("PDFPreview.autoPreview");
}

const menuitem: XUL.Element = window.document.getElementById(
"menu_autopreview_pdfpreview"
);
if (autoPreview) {
menuitem.setAttribute("checked", true);
} else {
menuitem.removeAttribute("checked");
}
}

public updatePreviewTabSelection() {
const tabbox = window.document.querySelector("#zotero-view-tabbox");
const previewTab = window.document.querySelector("#pdf-preview-tab");
Expand All @@ -56,16 +30,6 @@ class AddonEvents extends AddonBase {
);
(tabbox as any).selectedIndex = tabIndex;
}

private resetState(): void {
// Reset preferrence state.
let autoPreview = Zotero.Prefs.get("PDFPreview.autoPreview");
// Init
if (typeof autoPreview === "undefined") {
autoPreview = true;
Zotero.Prefs.set("PDFPreview.autoPreview", autoPreview);
}
}
}

export default AddonEvents;
5 changes: 4 additions & 1 deletion src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class AddonPreview extends AddonBase {
}

public async preview(force: boolean = false) {
if (Zotero.Prefs.get("pdfpreview.enable") === false) {
return;
}
let item = this.updatePreviewItem();
console.log(item);
if (force && !item) {
Expand Down Expand Up @@ -90,7 +93,7 @@ class AddonPreview extends AddonBase {
},
"*"
);
if (Zotero.Prefs.get("PDFPreview.autoPreview")) {
if (Zotero.Prefs.get("pdfpreview.autoPreview")) {
this._Addon.events.updatePreviewTabSelection();
}
} else {
Expand Down

0 comments on commit 8ca7e9c

Please sign in to comment.