Skip to content

Commit

Permalink
Add item pane API example
Browse files Browse the repository at this point in the history
Fix pref pane API example
Use `onMainWindowLoad` and `onMainWindowUnload`
  • Loading branch information
windingwind committed Mar 29, 2024
1 parent d4fa922 commit 2014a66
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 21 deletions.
14 changes: 8 additions & 6 deletions src-2.0/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ function install() {
async function startup({ id, version, rootURI }) {
log("Starting");

Zotero.PreferencePanes.register({
pluginID: '[email protected]',
src: rootURI + 'preferences.xhtml',
scripts: [rootURI + 'preferences.js']
});

Services.scriptloader.loadSubScript(rootURI + 'make-it-red.js');
MakeItRed.init({ id, version, rootURI });
MakeItRed.addToAllWindows();
await MakeItRed.main();
}

function onMainWindowLoad({ win }) {
MakeItRed.addToWindow(win);
}

function onMainWindowUnload(win) {
MakeItRed.removeFromWindow(win);
}

function shutdown() {
log("Shutting down");
MakeItRed.removeFromAllWindows();
Expand Down
96 changes: 96 additions & 0 deletions src-2.0/itemPaneSections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
(() => {
MakeItRed.registerItemPaneSections = function() {
// A minimal example
Zotero.ItemPaneManager.registerSection({
paneID: "example",
pluginID: this.id,
head: {
l10nID: "make-it-red-item-section-example1-head-text",
icon: "chrome://zotero/skin/16/universal/book.svg",
},
sidenav: {
l10nID: "make-it-red-item-section-example1-sidenav-tooltip",
icon: "chrome://zotero/skin/20/universal/save.svg",
},
onRender: ({ body, item, editable, tabType }) => {
body.textContent
= JSON.stringify({
id: item?.id,
editable,
tabType,
});
},
});

// A full example
Zotero.ItemPaneManager.registerSection({
paneID: "reader-example",
pluginID: this.id,
head: {
l10nID: "make-it-red-item-section-example2-head-text",
// Optional
l10nArgs: `{"status": "Initialized"}`,
// Can also have a optional dark icon
icon: "chrome://zotero/skin/16/universal/book.svg",
},
sidenav: {
l10nID: "make-it-red-item-section-example2-sidenav-tooltip",
icon: "chrome://zotero/skin/20/universal/save.svg",
},
// Optional
bodyXHTML: '<html:h1 id="test">THIS IS TEST</html:h1>',
// Optional, Called when the section is first created, must be synchronous
onInit: ({ item }) => {
this.log("Section init!", item?.id);
},
// Optional, Called when the section is destroyed, must be synchronous
onDestroy: (props) => {
this.log("Section destroy!");
},
// Optional, Called when the section data changes (setting item/mode/tabType/inTrash), must be synchronous. return false to cancel the change
onItemChange: ({ item, setEnabled, tabType }) => {
this.log(`Section item data changed to ${item?.id}`);
setEnabled(tabType === "reader");
return true;
},
// Called when the section is asked to render, must be synchronous.
onRender: ({ body, item, setL10nArgs, setSectionSummary, setSectionButtonStatus }) => {
this.log("Section rendered!", item?.id);
let title = body.querySelector("#test");
title.style.color = "red";
title.textContent = "LOADING";
setL10nArgs(`{ "status": "Loading" }`);
setSectionSummary('loading!');
setSectionButtonStatus("test", { hidden: true });
},
// Optional, can be asynchronous.
onAsyncRender: async ({ body, item, setL10nArgs, setSectionSummary, setSectionButtonStatus }) => {
this.log("Section secondary render start!", item?.id);
await Zotero.Promise.delay(1000);
this.log("Section secondary render finish!", item?.id);
let title = body.querySelector("#test");
title.style.color = "green";
title.textContent = item.getField("title");
setL10nArgs(`{ "status": "Loaded" }`);
setSectionSummary('rendered!');
setSectionButtonStatus("test", { hidden: false });
},
// Optional, Called when the section is toggled. Can happen anytime even if the section is not visible or not rendered
onToggle: ({ item }) => {
this.log("Section toggled!", item?.id);
},
// Optional, Buttons to be shown in the section header
sectionButtons: [
{
type: "test",
icon: "chrome://zotero/skin/16/universal/empty-trash.svg",
l10nID: "make-it-red-item-section-example2-button-tooltip",
onClick: ({ item, paneID }) => {
this.log("Section clicked!", item?.id);
Zotero.ItemPaneManager.unregisterSection(paneID);
},
},
],
});
}
})();
2 changes: 2 additions & 0 deletions src-2.0/locale/en-US/make-it-red-preferences.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
make-it-red-title = Plugin Settings
make-it-red-intensity = Intensity
11 changes: 11 additions & 0 deletions src-2.0/locale/en-US/make-it-red.ftl
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
make-it-red-green-instead =
.label = Make It Green Instead
make-it-red-item-section-example1-head-text =
.label = Make It Red: Item Info
make-it-red-item-section-example1-sidenav-tooltip =
.tooltiptext = This is Make It Red section (item info)
make-it-red-item-section-example2-head-text =
.label = Make It Red: Reader [{$status}]
make-it-red-item-section-example2-sidenav-tooltip =
.tooltiptext = This is Make It Red section (reader)
make-it-red-item-section-example2-button-tooltip =
.tooltiptext = Unregister this section
38 changes: 23 additions & 15 deletions src-2.0/make-it-red.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ MakeItRed = {
},

addToWindow(window) {
if (window._makeItRedAdded) {
return;
}
window._makeItRedAdded = true;

let doc = window.document;

// Add a stylesheet to the main Zotero pane
Expand Down Expand Up @@ -46,12 +51,7 @@ MakeItRed = {
},

addToAllWindows() {
var enumerator = Services.wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
let win = enumerator.getNext();
if (!win.ZoteroPane) continue;
this.addToWindow(win);
}
Zotero.getMainWindows().forEach((win) => this.addToWindow(win));
},

storeAddedElement(elem) {
Expand All @@ -62,21 +62,21 @@ MakeItRed = {
},

removeFromWindow(window) {
var doc = window.document;
if (!window._makeItRedAdded) {
return;
}
window._makeItRedAdded = false;

let doc = window.document;
// Remove all elements added to DOM
for (let id of this.addedElementIDs) {
doc.getElementById(id)?.remove();
}
doc.querySelector('[href="make-it-red.ftl"]').remove();
doc.querySelector('[href="make-it-red.ftl"]')?.remove();
},

removeFromAllWindows() {
var enumerator = Services.wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
let win = enumerator.getNext();
if (!win.ZoteroPane) continue;
this.removeFromWindow(win);
}
Zotero.getMainWindows().forEach((win) => this.removeFromWindow(win));
},

toggleGreen(window, enabled) {
Expand All @@ -86,10 +86,18 @@ MakeItRed = {

async main() {
// Global properties are included automatically in Zotero 7
var host = new URL('https://foo.com/path').host;
let host = new URL('https://foo.com/path').host;
this.log(`Host is ${host}`);

// Retrieve a global pref
this.log(`Intensity is ${Zotero.Prefs.get('extensions.make-it-red.intensity', true)}`);

// Load sub scripts
Services.scriptloader.loadSubScript(this.rootURI + 'preferencePanes.js', this);
Services.scriptloader.loadSubScript(this.rootURI + 'itemPaneSections.js', this);

// Use Zotero plugin API to register stuff
this.registerPrefPanes();
this.registerItemPaneSections();
},
};
10 changes: 10 additions & 0 deletions src-2.0/preferencePanes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(() => {
MakeItRed.registerPrefPanes = function () {
Zotero.PreferencePanes.register({
pluginID: MakeItRed.id,
src: MakeItRed.rootURI + 'preferences.xhtml',
scripts: [MakeItRed.rootURI + 'preferences.js'],
label: "Make It Red",
})
}
})();
1 change: 1 addition & 0 deletions src-2.0/preferences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Zotero.debug("Make It Red Pref Pane Loaded!");
18 changes: 18 additions & 0 deletions src-2.0/preferences.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<linkset>
<html:link rel="localization" href="make-it-red-preferences.ftl" />
</linkset>
<vbox id="zotero-prefpane-make-it-red">
<groupbox>
<label><html:h2 data-l10n-id="make-it-red-title"></html:h2></label>
<hbox>
<html:label
for="make-it-red-intensity"
data-l10n-id="make-it-red-intensity"
></html:label>
<html:input
id="make-it-red-intensity"
preference="extensions.make-it-red.intensity"
></html:input>
</hbox>
</groupbox>
</vbox>

0 comments on commit 2014a66

Please sign in to comment.