From 414bf81fd155a46f10b9c87dab4ceeb9ef501083 Mon Sep 17 00:00:00 2001 From: windingwind Date: Mon, 13 Mar 2023 00:32:29 +0800 Subject: [PATCH] fix: prompt example type error update: zotero-types --- package.json | 4 +- src/modules/examples.ts | 368 ++++++++++++++++++++++------------------ 2 files changed, 201 insertions(+), 171 deletions(-) diff --git a/package.json b/package.json index 4726912..9486d51 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,6 @@ "release-it": "^15.6.0", "replace-in-file": "^6.3.5", "typescript": "^4.9.4", - "zotero-types": "^1.0.6" + "zotero-types": "^1.0.12" } -} +} \ No newline at end of file diff --git a/src/modules/examples.ts b/src/modules/examples.ts index 23de441..77dc765 100644 --- a/src/modules/examples.ts +++ b/src/modules/examples.ts @@ -478,173 +478,201 @@ export class PromptExampleFactory { @example static registerAnonymousCommandExample() { - ztoolkit.Prompt.register([{ - id: "search", - callback: async (prompt) => { - // https://github.com/zotero/zotero/blob/7262465109c21919b56a7ab214f7c7a8e1e63909/chrome/content/zotero/integration/quickFormat.js#L589 - function getItemDescription(item: Zotero.Item) { - var nodes = []; - var str = ""; - var author, authorDate = ""; - if (item.firstCreator) { author = authorDate = item.firstCreator; } - var date = item.getField("date", true, true) as string; - if (date && (date = date.substr(0, 4)) !== "0000") { - authorDate += " (" + parseInt(date) + ")"; - } - authorDate = authorDate.trim(); - if (authorDate) nodes.push(authorDate); - - var publicationTitle = item.getField("publicationTitle", false, true); - if (publicationTitle) { - nodes.push(`${publicationTitle}`); - } - var volumeIssue = item.getField("volume"); - var issue = item.getField("issue"); - if (issue) volumeIssue += "(" + issue + ")"; - if (volumeIssue) nodes.push(volumeIssue); - - var publisherPlace = [], field; - if ((field = item.getField("publisher"))) publisherPlace.push(field); - if ((field = item.getField("place"))) publisherPlace.push(field); - if (publisherPlace.length) nodes.push(publisherPlace.join(": ")); - - var pages = item.getField("pages"); - if (pages) nodes.push(pages); - - if (!nodes.length) { - var url = item.getField("url"); - if (url) nodes.push(url); - } - - // compile everything together - for (var i = 0, n = nodes.length; i < n; i++) { - var node = nodes[i]; - - if (i != 0) str += ", "; - - if (typeof node === "object") { - var label = document.createElement("label"); - label.setAttribute("value", str); - label.setAttribute("crop", "end"); - str = ""; - } else { - str += node; + ztoolkit.Prompt.register([ + { + id: "search", + callback: async (prompt) => { + // https://github.com/zotero/zotero/blob/7262465109c21919b56a7ab214f7c7a8e1e63909/chrome/content/zotero/integration/quickFormat.js#L589 + function getItemDescription(item: Zotero.Item) { + var nodes = []; + var str = ""; + var author, + authorDate = ""; + if (item.firstCreator) { + author = authorDate = item.firstCreator; } + var date = item.getField("date", true, true) as string; + if (date && (date = date.substr(0, 4)) !== "0000") { + authorDate += " (" + parseInt(date) + ")"; + } + authorDate = authorDate.trim(); + if (authorDate) nodes.push(authorDate); + + var publicationTitle = item.getField( + "publicationTitle", + false, + true + ); + if (publicationTitle) { + nodes.push(`${publicationTitle}`); + } + var volumeIssue = item.getField("volume"); + var issue = item.getField("issue"); + if (issue) volumeIssue += "(" + issue + ")"; + if (volumeIssue) nodes.push(volumeIssue); + + var publisherPlace = [], + field; + if ((field = item.getField("publisher"))) + publisherPlace.push(field); + if ((field = item.getField("place"))) publisherPlace.push(field); + if (publisherPlace.length) nodes.push(publisherPlace.join(": ")); + + var pages = item.getField("pages"); + if (pages) nodes.push(pages); + + if (!nodes.length) { + var url = item.getField("url"); + if (url) nodes.push(url); + } + + // compile everything together + for (var i = 0, n = nodes.length; i < n; i++) { + var node = nodes[i]; + + if (i != 0) str += ", "; + + if (typeof node === "object") { + var label = document.createElement("label"); + label.setAttribute("value", str); + label.setAttribute("crop", "end"); + str = ""; + } else { + str += node; + } + } + str.length && (str += "."); + return str; } - str.length && (str += ".") - return str - }; - function filter(ids: number[]) { - ids = ids.filter(async (id) => { - const item = await Zotero.Items.getAsync(id) - return item.isRegularItem() && !item.isFeedItem - }) - return ids - } - const text = prompt.inputNode.value; - prompt.showTip("Searching...") - const s = new Zotero.Search(); - s.addCondition("quicksearch-titleCreatorYear", "contains", text); - s.addCondition("itemType", "isNot", "attachment"); - let ids = await s.search(); - // prompt.exit will remove current container element. - // @ts-ignore - prompt.exit(); - const container = prompt.createCommandsContainer(); - container.classList.add("suggestions"); - ids = filter(ids) - console.log(ids.length) - if (ids.length == 0) { + function filter(ids: number[]) { + ids = ids.filter(async (id) => { + const item = (await Zotero.Items.getAsync(id)) as Zotero.Item; + return item.isRegularItem() && !(item as any).isFeedItem; + }); + return ids; + } + const text = prompt.inputNode.value; + prompt.showTip("Searching..."); const s = new Zotero.Search(); - const operators = ['is', 'isNot', 'true', 'false', 'isInTheLast', 'isBefore', 'isAfter', 'contains', 'doesNotContain', 'beginsWith']; - let hasValidCondition = false - let joinMode: string = "all" - if (/\s*\|\|\s*/.test(text)) { - joinMode = "any" - } - text.split(/\s*(&&|\|\|)\s*/g).forEach((conditinString: string) => { - let conditions = conditinString.split(/\s+/g); - if (conditions.length == 3 && operators.indexOf(conditions[1]) != -1) { - hasValidCondition = true - s.addCondition("joinMode", joinMode); - s.addCondition( - conditions[0] as string, - conditions[1] as Zotero.Search.Operator, - conditions[2] as string - ); - } - }) - if (hasValidCondition) { - ids = await s.search(); - } - } - ids = filter(ids) - console.log(ids.length) - if (ids.length > 0) { - ids.forEach((id: number) => { - const item = Zotero.Items.get(id) - const title = item.getField("title") - const ele = ztoolkit.UI.createElement(document, "div", { - namespace: "html", - classList: ["command"], - listeners: [ - { - type: "mousemove", - listener: function () { - // @ts-ignore - prompt.selectItem(this) - } - }, - { - type: "click", - listener: () => { - prompt.promptNode.style.display = "none" - Zotero_Tabs.select('zotero-pane'); - ZoteroPane.selectItem(item.id); - } - } - ], - styles: { - display: "flex", - flexDirection: "column", - justifyContent: "start", - }, - children: [ - { - tag: "span", - styles: { - fontWeight: "bold", - overflow: "hidden", - textOverflow: "ellipsis", - whiteSpace: "nowrap" - - }, - properties: { - innerText: title - } - }, - { - tag: "span", - styles: { - overflow: "hidden", - textOverflow: "ellipsis", - whiteSpace: "nowrap" - }, - properties: { - innerHTML: getItemDescription(item) - } - } - ] - }) - container.appendChild(ele) - }) - } else { + s.addCondition("quicksearch-titleCreatorYear", "contains", text); + s.addCondition("itemType", "isNot", "attachment"); + let ids = await s.search(); + // prompt.exit will remove current container element. // @ts-ignore - prompt.exit() - prompt.showTip("Not Found.") - } - } - }]) + prompt.exit(); + const container = prompt.createCommandsContainer(); + container.classList.add("suggestions"); + ids = filter(ids); + console.log(ids.length); + if (ids.length == 0) { + const s = new Zotero.Search(); + const operators = [ + "is", + "isNot", + "true", + "false", + "isInTheLast", + "isBefore", + "isAfter", + "contains", + "doesNotContain", + "beginsWith", + ]; + let hasValidCondition = false; + let joinMode: string = "all"; + if (/\s*\|\|\s*/.test(text)) { + joinMode = "any"; + } + text.split(/\s*(&&|\|\|)\s*/g).forEach((conditinString: string) => { + let conditions = conditinString.split(/\s+/g); + if ( + conditions.length == 3 && + operators.indexOf(conditions[1]) != -1 + ) { + hasValidCondition = true; + s.addCondition( + "joinMode", + joinMode as Zotero.Search.Operator, + "" + ); + s.addCondition( + conditions[0] as string, + conditions[1] as Zotero.Search.Operator, + conditions[2] as string + ); + } + }); + if (hasValidCondition) { + ids = await s.search(); + } + } + ids = filter(ids); + console.log(ids.length); + if (ids.length > 0) { + ids.forEach((id: number) => { + const item = Zotero.Items.get(id); + const title = item.getField("title"); + const ele = ztoolkit.UI.createElement(document, "div", { + namespace: "html", + classList: ["command"], + listeners: [ + { + type: "mousemove", + listener: function () { + // @ts-ignore + prompt.selectItem(this); + }, + }, + { + type: "click", + listener: () => { + prompt.promptNode.style.display = "none"; + Zotero_Tabs.select("zotero-pane"); + ZoteroPane.selectItem(item.id); + }, + }, + ], + styles: { + display: "flex", + flexDirection: "column", + justifyContent: "start", + }, + children: [ + { + tag: "span", + styles: { + fontWeight: "bold", + overflow: "hidden", + textOverflow: "ellipsis", + whiteSpace: "nowrap", + }, + properties: { + innerText: title, + }, + }, + { + tag: "span", + styles: { + overflow: "hidden", + textOverflow: "ellipsis", + whiteSpace: "nowrap", + }, + properties: { + innerHTML: getItemDescription(item), + }, + }, + ], + }); + container.appendChild(ele); + }); + } else { + // @ts-ignore + prompt.exit(); + prompt.showTip("Not Found."); + } + }, + }, + ]); } @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. when: () => { const items = ZoteroPane.getSelectedItems(); - return items.length > 0 + return items.length > 0; }, callback(prompt) { - prompt.inputNode.placeholder = "Hello World!" + prompt.inputNode.placeholder = "Hello World!"; const items = ZoteroPane.getSelectedItems(); ztoolkit.getGlobal("alert")( - `You select ${items.length} items!\n\n${ - items.map( - (item, index) => String(index+1) + ". " + item.getDisplayTitle() - ).join("\n")}` + `You select ${items.length} items!\n\n${items + .map( + (item, index) => + String(index + 1) + ". " + item.getDisplayTitle() + ) + .join("\n")}` ); }, },