Skip to content

Commit

Permalink
add: prefs table local example
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyu committed Jan 10, 2023
1 parent 3c5dd00 commit a8dcf5e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions addon/chrome/locale/en-US/addon.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ menupopup.label=Addon Template: Menupopup
menuitem.submenulabel=Addon Template
menuitem.filemenulabel=Addon Template: File Menuitem
prefs.title=Template
prefs.table.title=Title
prefs.table.detail=Detail
tabpanel.lib.tab.label=Lib Tab
tabpanel.reader.tab.label=Reader Tab
2 changes: 2 additions & 0 deletions addon/chrome/locale/zh-CN/addon.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ menupopup.label=插件模板: 弹出菜单
menuitem.submenulabel=插件模板:子菜单
menuitem.filemenulabel=插件模板: 文件菜单
prefs.title=插件模板
prefs.table.title=标题
prefs.table.detail=详情
tabpanel.lib.tab.label=库标签
tabpanel.reader.tab.label=阅读器标签
21 changes: 19 additions & 2 deletions src/modules/preferenceScript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { config } from "../../package.json";
import { getString } from "./locale";

export function registerPrefsScripts(_window: Window) {
// This function is called when the prefs window is opened
Expand All @@ -9,13 +10,13 @@ export function registerPrefsScripts(_window: Window) {
columns: [
{
dataKey: "title",
label: "Title",
label: "prefs.table.title",
fixedWidth: true,
width: 100,
},
{
dataKey: "detail",
label: "Detail",
label: "prefs.table.detail",
},
],
rows: [
Expand Down Expand Up @@ -45,6 +46,18 @@ async function updatePrefsUI() {
const renderLock = ztoolkit.getGlobal("Zotero").Promise.defer();
const tableHelper = new ztoolkit.VirtualizedTabel(addon.data.prefs?.window!)
.setContainerId(`${config.addonRef}-table-container`)
// Add locale for table columns
.setLocale(
Object.fromEntries(
new Map(
addon.data.prefs?.columns.map((column) => [
column.label,
getString(column.label),
])
)
)
)
// id and getRowCount are required, others are optional.
.setProp({
id: `${config.addonRef}-prefs-table`,
columns: addon.data.prefs?.columns,
Expand All @@ -62,6 +75,7 @@ async function updatePrefsUI() {
detail: "no data",
}
)
// Show a progress window when selection changes
.setProp("onSelectionChange", (selection) => {
new ztoolkit.ProgressWindow(config.addonName)
.createLine({
Expand All @@ -73,6 +87,8 @@ async function updatePrefsUI() {
})
.show();
})
// When pressing delete, delete selected line and refresh table.
// Returning false to prevent default event.
.setProp("onKeyDown", (event: KeyboardEvent) => {
if (event.key == "Delete" || (Zotero.isMac && event.key == "Backspace")) {
addon.data.prefs!.rows =
Expand All @@ -89,6 +105,7 @@ async function updatePrefsUI() {
"getRowString",
(index) => addon.data.prefs?.rows[index].title || ""
)
// Render the table.
.render(-1, () => {
renderLock.resolve();
});
Expand Down

0 comments on commit a8dcf5e

Please sign in to comment.