Skip to content

Commit

Permalink
resolve: #184
Browse files Browse the repository at this point in the history
add: annotation menu
fix: edit action locale
  • Loading branch information
windingwind committed Nov 28, 2023
1 parent ba7a568 commit afe08f7
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 38 deletions.
3 changes: 3 additions & 0 deletions addon/locale/en-US/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ prefs-action-data = Data
prefs-action-shortcut = Shortcut
prefs-action-enabled = Enabled
prefs-action-menu = Menu Label
prefs-action-showInMenuItem = In Item Menu
prefs-action-showInMenuReader = In Reader Menu
prefs-action-showInMenuReaderAnnotation = In Annotation Menu
prefs-action-event-none = None
prefs-action-event-createItem = Create Item
Expand Down
3 changes: 3 additions & 0 deletions addon/locale/it-IT/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ prefs-action-data = Data
prefs-action-shortcut = Scorciatoia
prefs-action-enabled = Attivo
prefs-action-menu = Etichetta menu
prefs-action-showInMenuItem = In Item Menu
prefs-action-showInMenuReader = In Reader Menu
prefs-action-showInMenuReaderAnnotation = In Annotation Menu
prefs-action-event-none = Nessuno
prefs-action-event-createItem = Crea Elemento
Expand Down
3 changes: 3 additions & 0 deletions addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ prefs-action-data = 数据
prefs-action-shortcut = 快捷键
prefs-action-enabled = 启用
prefs-action-menu = 菜单项
prefs-action-showInMenuItem = 条目菜单中
prefs-action-showInMenuReader = 阅读器菜单中
prefs-action-showInMenuReaderAnnotation = 注释菜单中
prefs-action-event-none =
prefs-action-event-createItem = 新建条目
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
"release-it": "^16.2.1",
"replace-in-file": "^7.0.1",
"typescript": "^5.2.2",
"zotero-types": "^1.3.4"
"zotero-types": "^1.3.7"
}
}
6 changes: 4 additions & 2 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
initWindowShortcuts,
unInitWindowShortcuts,
} from "./modules/shortcuts";
import { buildItemMenu, initMenu, initReaderMenu } from "./modules/menu";
import { buildItemMenu, initItemMenu, initReaderAnnotationMenu, initReaderMenu } from "./modules/menu";
import { editAction } from "./modules/edit";
import { exportToFile, importFromFile } from "./modules/backup";

Expand Down Expand Up @@ -41,12 +41,14 @@ async function onStartup() {

initReaderMenu();

initReaderAnnotationMenu();

await onMainWindowLoad(window);
}

async function onMainWindowLoad(win: Window): Promise<void> {
initWindowShortcuts(win);
initMenu(win);
initItemMenu(win);
await addon.api.actionManager.dispatchActionByEvent(
ActionEventTypes.mainWindowLoad,
{
Expand Down
112 changes: 98 additions & 14 deletions src/modules/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ async function editAction(currentKey?: string) {
dialogData.shortcut =
new KeyModifier(action.shortcut || "").getLocalized() ||
`[${getString("prefs-action-edit-shortcut-empty")}]`;
dialogData.showInMenuItem = !(action.showInMenu?.item === false);
dialogData.showInMenuReader = !(action.showInMenu?.reader === false);
dialogData.showInMenuReaderAnnotation = !(
action.showInMenu?.readerAnnotation === false
);
const dialog = new ztoolkit.Dialog(1, 1)
.setDialogData(dialogData)
.addCell(0, 0, {
Expand Down Expand Up @@ -138,7 +143,7 @@ async function editAction(currentKey?: string) {
const content = await openEditorWindow(dialogData.data);
(
dialog.window.document.querySelector(
"#data-input",
"#data-input"
) as HTMLTextAreaElement
).value = content;
dialogData.data = content;
Expand Down Expand Up @@ -172,7 +177,7 @@ async function editAction(currentKey?: string) {
const key = ev.target as HTMLElement;
const win = dialog.window;
key.textContent = `[${getString(
"prefs-rule-edit-shortcut-placeholder",
"prefs-action-edit-shortcut-placeholder"
)}]`;
dialogData.shortcut = "";
const keyDownListener = (e: KeyboardEvent) => {
Expand Down Expand Up @@ -211,17 +216,92 @@ async function editAction(currentKey?: string) {
},
},
{
tag: "input",
attributes: {
"data-bind": "menu",
"data-prop": "value",
},
properties: {
placeholder: getString("prefs-action-edit-menu-placeholder"),
},
tag: "div",
styles: {
width: "fit-content",
display: "grid",
gridTemplateColumns: "1fr 3fr",
rowGap: "10px",
columnGap: "5px",
},
children: [
{
tag: "input",
attributes: {
"data-bind": "menu",
"data-prop": "value",
},
properties: {
placeholder: getString("prefs-action-edit-menu-placeholder"),
},
styles: {
width: "fit-content",
gridColumnStart: "1",
gridColumnEnd: "3",
},
},
{
tag: "label",
namespace: "html",
properties: {
textContent: getString("prefs-action-showInMenuItem"),
},
},
{
tag: "input",
properties: {
type: "checkbox",
},
attributes: {
"data-bind": "showInMenuItem",
"data-prop": "checked",
},
styles: {
width: "fit-content",
},
},
{
tag: "label",
namespace: "html",
properties: {
textContent: getString("prefs-action-showInMenuReader"),
},
},
{
tag: "input",
properties: {
type: "checkbox",
},
attributes: {
"data-bind": "showInMenuReader",
"data-prop": "checked",
},
styles: {
width: "fit-content",
},
},
{
tag: "label",
namespace: "html",
properties: {
textContent: getString(
"prefs-action-showInMenuReaderAnnotation"
),
},
},
{
tag: "input",
properties: {
type: "checkbox",
},
attributes: {
"data-bind": "showInMenuReaderAnnotation",
"data-prop": "checked",
},
styles: {
width: "fit-content",
},
},
],
},
{
tag: "label",
Expand All @@ -234,7 +314,6 @@ async function editAction(currentKey?: string) {
tag: "input",
properties: {
type: "checkbox",
checked: action.enabled,
},
attributes: {
"data-bind": "enabled",
Expand Down Expand Up @@ -277,8 +356,13 @@ async function editAction(currentKey?: string) {
enabled: dialogData.enabled,
menu: dialogData.menu,
name: dialogData.name,
showInMenu: {
item: dialogData.showInMenuItem,
reader: dialogData.showInMenuReader,
readerAnnotation: dialogData.showInMenuReaderAnnotation,
},
},
currentKey,
currentKey
);
edited = true;
}
Expand All @@ -301,7 +385,7 @@ async function openEditorWindow(content: string) {
const editorWin = addon.data.prefs.window?.openDialog(
"chrome://scaffold/content/monaco/monaco.html",
"monaco",
"chrome,centerscreen,dialog=no,resizable,scrollbars=yes,width=800,height=600",
"chrome,centerscreen,dialog=no,resizable,scrollbars=yes,width=800,height=600"
) as
| (Window & {
loadMonaco: (options: Record<string, any>) => Promise<{ editor: any }>;
Expand Down
72 changes: 58 additions & 14 deletions src/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { TagElementProps } from "zotero-plugin-toolkit/dist/tools/ui";
import { config } from "../../package.json";
import { getString } from "../utils/locale";
import { getPref } from "../utils/prefs";
import { ActionData } from "../utils/actions";
import { ActionData, ActionShowInMenu } from "../utils/actions";
import { getCurrentItems } from "../utils/items";

export { initMenu, initReaderMenu, buildItemMenu };
export {
initItemMenu,
initReaderMenu,
initReaderAnnotationMenu,
buildItemMenu,
};

function initMenu(win: Window) {
function initItemMenu(win: Window) {
ztoolkit.Menu.register("item", {
tag: "menu",
popupId: `${config.addonRef}-item-popup`,
Expand Down Expand Up @@ -36,7 +41,7 @@ function initMenu(win: Window) {
},
],
},
win.document.querySelector("popupset")!,
win.document.querySelector("popupset")!
);
}

Expand Down Expand Up @@ -104,31 +109,53 @@ function initReaderMenu() {
classList: ["dropmarker"],
},
],
}),
})
);
append(
ztoolkit.UI.createElement(doc, "style", {
id: `${config.addonRef}-reader-button`,
properties: {
textContent: readerButtonCSS,
},
}),
})
);
});
}

function initReaderAnnotationMenu() {
Zotero.Reader.registerEventListener(
"createAnnotationContextMenu",
(event) => {
const { append, params, reader } = event;
const actions = getActionsByMenu("readerAnnotation");
for (const action of actions) {
append({
label: action.menu!,
onCommand: () => {
triggerMenuCommand(
action.key,
reader._item.libraryID,
...params.ids
);
},
});
}
}
);
}

function buildItemMenu(win: Window, target: "item" | "reader") {
const doc = win.document;
const popup = doc.querySelector(
`#${config.addonRef}-${target}-popup`,
`#${config.addonRef}-${target}-popup`
) as XUL.MenuPopup;
// Remove all children in popup
while (popup.firstChild) {
popup.removeChild(popup.firstChild);
}
// Add new children
let elemProp: TagElementProps;
const enabledActions = getActionsByMenu();
const enabledActions = getActionsByMenu(target);
if (enabledActions.length === 0) {
elemProp = {
tag: "menuitem",
Expand Down Expand Up @@ -159,16 +186,22 @@ function buildItemMenu(win: Window, target: "item" | "reader") {
};
}),
},
popup,
popup
);
}
}

function getActionsByMenu() {
function getActionsByMenu(target: ActionShowInMenu) {
const sortBy = (getPref("menuSortBy") || "menu") as keyof ActionData;
return Array.from(addon.data.actions.map.keys())
.map((k) => Object.assign({}, addon.data.actions.map.get(k), { key: k }))
.filter((action) => action && action.menu && action.enabled)
.filter(
(action) =>
action &&
action.menu &&
action.enabled &&
(!action.showInMenu || action.showInMenu[target] !== false)
)
.sort((x, y) => {
if (!x && !y) {
return 0;
Expand All @@ -181,13 +214,24 @@ function getActionsByMenu() {
}
return ((x[sortBy] as string) || "").localeCompare(
(y[sortBy] || "") as string,
Zotero.locale,
Zotero.locale
);
});
}

async function triggerMenuCommand(key: string) {
const items = getCurrentItems();
async function triggerMenuCommand(
key: string,
libraryID?: number,
...itemKeys: string[]
) {
let items: Zotero.Item[];
if (libraryID && itemKeys) {
items = itemKeys
.map((key) => Zotero.Items.getByLibraryAndKey(libraryID, key))
.filter((item) => item) as Zotero.Item[];
} else {
items = getCurrentItems();
}
// Trigger action for all items
await addon.api.actionManager.dispatchActionByKey(key, {
itemIDs: items.map((item) => item.id),
Expand Down
Loading

0 comments on commit afe08f7

Please sign in to comment.