fix: prompt example type error

update: zotero-types
This commit is contained in:
windingwind 2023-03-13 00:32:29 +08:00
parent bb4c31b54c
commit 414bf81fd1
2 changed files with 201 additions and 171 deletions

View File

@ -49,6 +49,6 @@
"release-it": "^15.6.0", "release-it": "^15.6.0",
"replace-in-file": "^6.3.5", "replace-in-file": "^6.3.5",
"typescript": "^4.9.4", "typescript": "^4.9.4",
"zotero-types": "^1.0.6" "zotero-types": "^1.0.12"
} }
} }

View File

@ -478,15 +478,19 @@ export class PromptExampleFactory {
@example @example
static registerAnonymousCommandExample() { static registerAnonymousCommandExample() {
ztoolkit.Prompt.register([{ ztoolkit.Prompt.register([
{
id: "search", id: "search",
callback: async (prompt) => { callback: async (prompt) => {
// https://github.com/zotero/zotero/blob/7262465109c21919b56a7ab214f7c7a8e1e63909/chrome/content/zotero/integration/quickFormat.js#L589 // https://github.com/zotero/zotero/blob/7262465109c21919b56a7ab214f7c7a8e1e63909/chrome/content/zotero/integration/quickFormat.js#L589
function getItemDescription(item: Zotero.Item) { function getItemDescription(item: Zotero.Item) {
var nodes = []; var nodes = [];
var str = ""; var str = "";
var author, authorDate = ""; var author,
if (item.firstCreator) { author = authorDate = item.firstCreator; } authorDate = "";
if (item.firstCreator) {
author = authorDate = item.firstCreator;
}
var date = item.getField("date", true, true) as string; var date = item.getField("date", true, true) as string;
if (date && (date = date.substr(0, 4)) !== "0000") { if (date && (date = date.substr(0, 4)) !== "0000") {
authorDate += " (" + parseInt(date) + ")"; authorDate += " (" + parseInt(date) + ")";
@ -494,7 +498,11 @@ export class PromptExampleFactory {
authorDate = authorDate.trim(); authorDate = authorDate.trim();
if (authorDate) nodes.push(authorDate); if (authorDate) nodes.push(authorDate);
var publicationTitle = item.getField("publicationTitle", false, true); var publicationTitle = item.getField(
"publicationTitle",
false,
true
);
if (publicationTitle) { if (publicationTitle) {
nodes.push(`<i>${publicationTitle}</i>`); nodes.push(`<i>${publicationTitle}</i>`);
} }
@ -503,8 +511,10 @@ export class PromptExampleFactory {
if (issue) volumeIssue += "(" + issue + ")"; if (issue) volumeIssue += "(" + issue + ")";
if (volumeIssue) nodes.push(volumeIssue); if (volumeIssue) nodes.push(volumeIssue);
var publisherPlace = [], field; var publisherPlace = [],
if ((field = item.getField("publisher"))) publisherPlace.push(field); field;
if ((field = item.getField("publisher")))
publisherPlace.push(field);
if ((field = item.getField("place"))) publisherPlace.push(field); if ((field = item.getField("place"))) publisherPlace.push(field);
if (publisherPlace.length) nodes.push(publisherPlace.join(": ")); if (publisherPlace.length) nodes.push(publisherPlace.join(": "));
@ -531,18 +541,18 @@ export class PromptExampleFactory {
str += node; str += node;
} }
} }
str.length && (str += ".") str.length && (str += ".");
return str return str;
}; }
function filter(ids: number[]) { function filter(ids: number[]) {
ids = ids.filter(async (id) => { ids = ids.filter(async (id) => {
const item = await Zotero.Items.getAsync(id) const item = (await Zotero.Items.getAsync(id)) as Zotero.Item;
return item.isRegularItem() && !item.isFeedItem return item.isRegularItem() && !(item as any).isFeedItem;
}) });
return ids return ids;
} }
const text = prompt.inputNode.value; const text = prompt.inputNode.value;
prompt.showTip("Searching...") prompt.showTip("Searching...");
const s = new Zotero.Search(); const s = new Zotero.Search();
s.addCondition("quicksearch-titleCreatorYear", "contains", text); s.addCondition("quicksearch-titleCreatorYear", "contains", text);
s.addCondition("itemType", "isNot", "attachment"); s.addCondition("itemType", "isNot", "attachment");
@ -552,38 +562,56 @@ export class PromptExampleFactory {
prompt.exit(); prompt.exit();
const container = prompt.createCommandsContainer(); const container = prompt.createCommandsContainer();
container.classList.add("suggestions"); container.classList.add("suggestions");
ids = filter(ids) ids = filter(ids);
console.log(ids.length) console.log(ids.length);
if (ids.length == 0) { if (ids.length == 0) {
const s = new Zotero.Search(); const s = new Zotero.Search();
const operators = ['is', 'isNot', 'true', 'false', 'isInTheLast', 'isBefore', 'isAfter', 'contains', 'doesNotContain', 'beginsWith']; const operators = [
let hasValidCondition = false "is",
let joinMode: string = "all" "isNot",
"true",
"false",
"isInTheLast",
"isBefore",
"isAfter",
"contains",
"doesNotContain",
"beginsWith",
];
let hasValidCondition = false;
let joinMode: string = "all";
if (/\s*\|\|\s*/.test(text)) { if (/\s*\|\|\s*/.test(text)) {
joinMode = "any" joinMode = "any";
} }
text.split(/\s*(&&|\|\|)\s*/g).forEach((conditinString: string) => { text.split(/\s*(&&|\|\|)\s*/g).forEach((conditinString: string) => {
let conditions = conditinString.split(/\s+/g); let conditions = conditinString.split(/\s+/g);
if (conditions.length == 3 && operators.indexOf(conditions[1]) != -1) { if (
hasValidCondition = true conditions.length == 3 &&
s.addCondition("joinMode", joinMode); operators.indexOf(conditions[1]) != -1
) {
hasValidCondition = true;
s.addCondition(
"joinMode",
joinMode as Zotero.Search.Operator,
""
);
s.addCondition( s.addCondition(
conditions[0] as string, conditions[0] as string,
conditions[1] as Zotero.Search.Operator, conditions[1] as Zotero.Search.Operator,
conditions[2] as string conditions[2] as string
); );
} }
}) });
if (hasValidCondition) { if (hasValidCondition) {
ids = await s.search(); ids = await s.search();
} }
} }
ids = filter(ids) ids = filter(ids);
console.log(ids.length) console.log(ids.length);
if (ids.length > 0) { if (ids.length > 0) {
ids.forEach((id: number) => { ids.forEach((id: number) => {
const item = Zotero.Items.get(id) const item = Zotero.Items.get(id);
const title = item.getField("title") const title = item.getField("title");
const ele = ztoolkit.UI.createElement(document, "div", { const ele = ztoolkit.UI.createElement(document, "div", {
namespace: "html", namespace: "html",
classList: ["command"], classList: ["command"],
@ -592,17 +620,17 @@ export class PromptExampleFactory {
type: "mousemove", type: "mousemove",
listener: function () { listener: function () {
// @ts-ignore // @ts-ignore
prompt.selectItem(this) prompt.selectItem(this);
} },
}, },
{ {
type: "click", type: "click",
listener: () => { listener: () => {
prompt.promptNode.style.display = "none" prompt.promptNode.style.display = "none";
Zotero_Tabs.select('zotero-pane'); Zotero_Tabs.select("zotero-pane");
ZoteroPane.selectItem(item.id); ZoteroPane.selectItem(item.id);
} },
} },
], ],
styles: { styles: {
display: "flex", display: "flex",
@ -616,35 +644,35 @@ export class PromptExampleFactory {
fontWeight: "bold", fontWeight: "bold",
overflow: "hidden", overflow: "hidden",
textOverflow: "ellipsis", textOverflow: "ellipsis",
whiteSpace: "nowrap" whiteSpace: "nowrap",
}, },
properties: { properties: {
innerText: title innerText: title,
} },
}, },
{ {
tag: "span", tag: "span",
styles: { styles: {
overflow: "hidden", overflow: "hidden",
textOverflow: "ellipsis", textOverflow: "ellipsis",
whiteSpace: "nowrap" whiteSpace: "nowrap",
}, },
properties: { properties: {
innerHTML: getItemDescription(item) innerHTML: getItemDescription(item),
} },
} },
] ],
}) });
container.appendChild(ele) container.appendChild(ele);
}) });
} else { } else {
// @ts-ignore // @ts-ignore
prompt.exit() prompt.exit();
prompt.showTip("Not Found.") prompt.showTip("Not Found.");
} }
} },
}]) },
]);
} }
@example @example
@ -656,16 +684,18 @@ export class PromptExampleFactory {
// The when function is executed when Prompt UI is woken up by `Shift + P`, and this command does not display when false is returned. // The when function is executed when Prompt UI is woken up by `Shift + P`, and this command does not display when false is returned.
when: () => { when: () => {
const items = ZoteroPane.getSelectedItems(); const items = ZoteroPane.getSelectedItems();
return items.length > 0 return items.length > 0;
}, },
callback(prompt) { callback(prompt) {
prompt.inputNode.placeholder = "Hello World!" prompt.inputNode.placeholder = "Hello World!";
const items = ZoteroPane.getSelectedItems(); const items = ZoteroPane.getSelectedItems();
ztoolkit.getGlobal("alert")( ztoolkit.getGlobal("alert")(
`You select ${items.length} items!\n\n${ `You select ${items.length} items!\n\n${items
items.map( .map(
(item, index) => String(index+1) + ". " + item.getDisplayTitle() (item, index) =>
).join("\n")}` String(index + 1) + ". " + item.getDisplayTitle()
)
.join("\n")}`
); );
}, },
}, },