diff --git a/src/Better Note Markdown.js b/addon/chrome/content/translators/Better Note Markdown.js similarity index 100% rename from src/Better Note Markdown.js rename to addon/chrome/content/translators/Better Note Markdown.js diff --git a/src/zotero-protocol-handler.js b/addon/components/zotero-protocol-handler.js similarity index 100% rename from src/zotero-protocol-handler.js rename to addon/components/zotero-protocol-handler.js diff --git a/build.js b/build.js index f9fc5f5..8a4a1e1 100644 --- a/build.js +++ b/build.js @@ -158,19 +158,6 @@ async function main() { console.log("[Build] Addon prepare OK"); - copyFileSync( - "src/zotero-protocol-handler.js", - path.join(buildDir, "addon/components/zotero-protocol-handler.js") - ); - - copyFileSync( - "src/Better Note Markdown.js", - path.join( - buildDir, - "addon/chrome/content/translators/Better Note Markdown.js" - ) - ); - compressing.zip.compressDir( path.join(buildDir, "addon"), path.join(buildDir, `${name}.xpi`), diff --git a/jsconfig.json b/jsconfig.json deleted file mode 100644 index 8cfb706..0000000 --- a/jsconfig.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "compilerOptions": { - "module": "commonjs", - "target": "es6", - }, - "exclude": ["node_modules", "builds"] -} diff --git a/package.json b/package.json index 10dfce9..de83f43 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "devDependencies": { "@types/jquery": "^3.5.14", "@types/node": "^17.0.31", - "release-it": "^14.14.0" + "release-it": "^14.14.0", + "zotero-types": "^0.0.3" } } diff --git a/src/base.ts b/src/base.ts index 9a73bea..1fb4fe5 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,15 +1,8 @@ -class AddonBase { - protected _Addon: Knowledge4Zotero; - constructor(parent: Knowledge4Zotero) { - this._Addon = parent; - } -} - class EditorMessage { public type: string; public content: { - event?: XULEvent; - editorInstance?: EditorInstance; + event?: XUL.XULEvent; + editorInstance?: Zotero.EditorInstance; params?: any; }; constructor(type: string, content: object) { @@ -83,5 +76,4 @@ class CopyHelper { ); } } - -export { AddonBase, EditorMessage, OutlineType, NoteTemplate, CopyHelper }; +export { EditorMessage, OutlineType, NoteTemplate, CopyHelper }; diff --git a/src/events.ts b/src/events.ts index 327f569..edab5be 100644 --- a/src/events.ts +++ b/src/events.ts @@ -1,7 +1,9 @@ -import { AddonBase, CopyHelper, EditorMessage } from "./base"; +import Knowledge4Zotero from "./addon"; +import { CopyHelper, EditorMessage } from "./base"; +import AddonBase from "./module"; class AddonEvents extends AddonBase { - notifierCallback: object; + notifierCallback: any; notifierCbkDict: any; // Important! // Due to unknown reasons, the DOMParser constructor fails after the tab is opened. @@ -20,8 +22,9 @@ class AddonEvents extends AddonBase { ) => { if (event === "modify" && type === "item") { if ( - ids.indexOf(Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID")) >= - 0 + ids.indexOf( + Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") as number + ) >= 0 ) { Zotero.debug("Knowledge4Zotero: main knowledge modify check."); this._Addon.views.updateOutline(); @@ -40,7 +43,7 @@ class AddonEvents extends AddonBase { extraData[ids[0]].type == "reader") || (event === "add" && type === "item" && - Zotero.Items.get(ids).filter((item) => { + (Zotero.Items.get(ids) as Zotero.Item[]).filter((item) => { return item.isAnnotation(); }).length > 0) || (event === "close" && type === "tab") || @@ -53,10 +56,10 @@ class AddonEvents extends AddonBase { } if (event == "add" && type == "tab") { if (ids[0] === this._Addon.knowledge.workspaceTabId) { - const tabTitle = document - .querySelector(`.tab[data-id=${ids[0]}]`) - .querySelector(".tab-name"); - tabTitle.innerHTML = `${this._Addon.views.editorIcon.tabIcon}${tabTitle.innerHTML}`; + const tabItem = document.querySelector(`.tab[data-id=${ids[0]}]`); + const tabTitle = tabItem && tabItem.querySelector(".tab-name"); + tabTitle && + (tabTitle.innerHTML = `${this._Addon.views.editorIcon.tabIcon}${tabTitle.innerHTML}`); } } if (event == "select" && type == "tab") { @@ -85,8 +88,8 @@ class AddonEvents extends AddonBase { const _tabToolbar = document.getElementById("zotero-tab-toolbar"); _contextPaneSplitter.setAttribute("hidden", true); _contextPane.setAttribute("collapsed", true); - _tabToolbar.hidden = true; - _tabCover.hidden = true; + _tabToolbar && (_tabToolbar.hidden = true); + _tabCover && (_tabCover.hidden = true); this._Addon.views.switchRealMenuBar(false); this._Addon.views.switchKey(false); this._Addon.views.updateWordCount(); @@ -99,14 +102,16 @@ class AddonEvents extends AddonBase { Zotero.Prefs.get("Knowledge4Zotero.autoAnnotation") && event === "add" && type === "item" && - Zotero.Items.get(ids).filter((item) => { + (Zotero.Items.get(ids) as Zotero.Item[]).filter((item) => { return item.isAnnotation(); }).length > 0 ) { Zotero.debug("Knowledge4Zotero: autoAnnotation"); - const annotations = Zotero.Items.get(ids).filter((item) => { - return item.isAnnotation(); - }); + const annotations = (Zotero.Items.get(ids) as Zotero.Item[]).filter( + (item) => { + return item.isAnnotation(); + } + ); this.onEditorEvent( new EditorMessage("addAnnotationToNote", { params: { annotations: annotations }, @@ -216,7 +221,9 @@ class AddonEvents extends AddonBase { Zotero.Notes._knowledgeInit = true; Zotero.Notes._registerEditorInstance = Zotero.Notes.registerEditorInstance; - Zotero.Notes.registerEditorInstance = (instance: EditorInstance) => { + Zotero.Notes.registerEditorInstance = ( + instance: Zotero.EditorInstance + ) => { Zotero.Notes._registerEditorInstance(instance); this.onEditorEvent( new EditorMessage("addNoteInstance", { @@ -228,7 +235,7 @@ class AddonEvents extends AddonBase { } public async addEditorEventListener( - instance: EditorInstance, + instance: Zotero.EditorInstance, event: string, message: EditorMessage ) { @@ -236,23 +243,23 @@ class AddonEvents extends AddonBase { let editorElement: Element = this._Addon.views.getEditorElement( instance._iframeWindow.document ); - editorElement.addEventListener(event, (e: XULEvent) => { - message.content.event = e; + editorElement.addEventListener(event, (e) => { + message.content.event = e as XUL.XULEvent; message.content.editorInstance = instance; this.onEditorEvent(message); }); } public async addEditorDocumentEventListener( - instance: EditorInstance, + instance: Zotero.EditorInstance, event: string, message: EditorMessage ) { await instance._initPromise; let doc: Document = instance._iframeWindow.document; - doc.addEventListener(event, (e: XULEvent) => { - message.content.event = e; + doc.addEventListener(event, (e) => { + message.content.event = e as XUL.XULEvent; message.content.editorInstance = instance; this.onEditorEvent(message); }); @@ -330,7 +337,7 @@ class AddonEvents extends AddonBase { } const header = prompt("Enter new note header:"); const noteID = await ZoteroPane_Local.newNote(); - Zotero.Items.get(noteID).setNote( + (Zotero.Items.get(noteID) as Zotero.Item).setNote( `

${header}

\n
` ); await this.onEditorEvent( @@ -351,7 +358,7 @@ class AddonEvents extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://zotero/content/selectItemsDialog.xul", "", "chrome,dialog=no,centerscreen,resizable=yes", @@ -359,7 +366,7 @@ class AddonEvents extends AddonBase { ); await io.deferred.promise; - const ids = io.dataOut; + const ids = io.dataOut as unknown as number[]; if (ids.length === 0) { this._Addon.views.showProgressWindow("Knowledge", "No note selected."); return; @@ -371,7 +378,7 @@ class AddonEvents extends AddonBase { return; } - const note = Zotero.Items.get(ids[0]); + const note = Zotero.Items.get(ids[0]) as Zotero.Item; if (note && note.isNote()) { this.onEditorEvent( new EditorMessage("setMainKnowledge", { @@ -392,10 +399,10 @@ class AddonEvents extends AddonBase { */ Zotero.debug("Knowledge4Zotero: setMainKnowledge"); let mainKnowledgeID = parseInt( - Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") + Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") as string ); let itemID = message.content.params.itemID; - const item = Zotero.Items.get(itemID); + const item = Zotero.Items.get(itemID) as Zotero.Item; if (itemID === mainKnowledgeID) { this._Addon.views.showProgressWindow( "Better Notes", @@ -432,29 +439,30 @@ class AddonEvents extends AddonBase { editorInstance, params: {noStyle: boolean} } */ - await message.content.editorInstance._initPromise; + const editor = message.content.editorInstance as Zotero.EditorInstance; + await editor._initPromise; - message.content.editorInstance._knowledgeUIInitialized = false; + editor._knowledgeUIInitialized = false; - const currentID = message.content.editorInstance._item.id; + const currentID = editor._item.id; // Check if this is a window for print const isPrint = this._Addon.knowledge._pdfNoteId === currentID; - const noteItem: ZoteroItem = Zotero.Items.get(currentID); + const noteItem = Zotero.Items.get(currentID) as Zotero.Item; if (!noteItem.isNote()) { return; } const mainNoteID = parseInt( - Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") + Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") as string ); const isMainNote = currentID === mainNoteID; const isPreviewNote = currentID === this._Addon.knowledge.previewItemID; Zotero.debug(`Knowledge4Zotero: main Knowledge`); await this._Addon.views.addEditorButton( - message.content.editorInstance, + editor, "knowledge-start", isMainNote ? "isMainKnowledge" @@ -470,7 +478,7 @@ class AddonEvents extends AddonBase { "start" ); const addLinkDropDown: Element = await this._Addon.views.addEditorButton( - message.content.editorInstance, + editor, "knowledge-addlink", "addToKnowledge", "Add Link of Current Note to Main Note", @@ -482,10 +490,7 @@ class AddonEvents extends AddonBase { if (isMainNote) { // This is a main knowledge, hide all buttons except the export button and add title addLinkDropDown.innerHTML = ""; - const header = - message.content.editorInstance._iframeWindow.document.createElement( - "div" - ); + const header = editor._iframeWindow.document.createElement("div"); header.setAttribute("title", "This is a Main Note"); header.innerHTML = "Main Note"; header.setAttribute("style", "font-size: medium"); @@ -495,7 +500,7 @@ class AddonEvents extends AddonBase { if (addLinkDropDown.getElementsByClassName("popup").length > 0) { return; } - const buttonParam = []; + const buttonParam: any[] = []; const nodes = this._Addon.knowledge.getNoteTreeAsList(undefined); for (let node of nodes) { buttonParam.push({ @@ -506,7 +511,7 @@ class AddonEvents extends AddonBase { }); } const popup: Element = await this._Addon.views.addEditorPopup( - message.content.editorInstance, + editor, "knowledge-addlink-popup", // [{ id: ''; icon: string; eventType: string }], buttonParam, @@ -523,7 +528,7 @@ class AddonEvents extends AddonBase { } const addCitationButton = await this._Addon.views.addEditorButton( - message.content.editorInstance, + editor, "knowledge-addcitation", "addCitation", "Insert Citations", @@ -543,7 +548,7 @@ class AddonEvents extends AddonBase { return; } const popup: Element = await this._Addon.views.addEditorPopup( - message.content.editorInstance, + editor, "knowledge-addcitation-popup", [ { @@ -576,7 +581,7 @@ class AddonEvents extends AddonBase { } await this._Addon.views.addEditorButton( - message.content.editorInstance, + editor, "knowledge-switchTex", "switchTex", `LaTex View ${Zotero.isWin ? "Ctrl" : "⌘"}+/`, @@ -585,12 +590,12 @@ class AddonEvents extends AddonBase { "builtin" ); - message.content.editorInstance._iframeWindow.document.onkeyup = (e) => { + editor._iframeWindow.document.onkeyup = (e) => { if (e.ctrlKey && e.key === "/") { console.log(e); this.onEditorEvent( new EditorMessage("switchEditorTex", { - editorInstance: message.content.editorInstance, + editorInstance: editor, }) ); e.stopPropagation(); @@ -598,23 +603,23 @@ class AddonEvents extends AddonBase { }; await this._Addon.views.addEditorButton( - message.content.editorInstance, + editor, "knowledge-end", isPreviewNote ? "close" : "export", isPreviewNote ? "Close Preview" : "Export with linked notes", isPreviewNote ? "closePreview" : "export", "end" ); - if (!message.content.editorInstance._knowledgeSelectionInitialized) { + if (!editor._knowledgeSelectionInitialized) { this.addEditorDocumentEventListener( - message.content.editorInstance, + editor, "selectionchange", new EditorMessage("noteEditorSelectionChange", {}) ); - message.content.editorInstance._knowledgeSelectionInitialized = true; + editor._knowledgeSelectionInitialized = true; } - const _window = message.content.editorInstance._iframeWindow; + const _window = editor._iframeWindow; // Title style only for normal window if (!isPrint) { const style = _window.document.createElement("style"); @@ -732,25 +737,18 @@ class AddonEvents extends AddonBase { _window.document.head.append(mathScript); } - message.content.editorInstance._knowledgeUIInitialized = true; + editor._knowledgeUIInitialized = true; if (isPrint) { - message.content.editorInstance._iframeWindow.postMessage( - { type: "exportPDF" }, - "*" - ); + editor._iframeWindow.postMessage({ type: "exportPDF" }, "*"); this._Addon.knowledge._pdfNoteId = -1; return; } - if ( - this._Addon.views._texNotes.includes( - message.content.editorInstance._item.id - ) - ) { + if (this._Addon.views._texNotes.includes(editor._item.id)) { this.onEditorEvent( new EditorMessage("switchEditorTex", { - editorInstance: message.content.editorInstance, + editorInstance: editor, params: { viewTex: true }, }) ); @@ -761,14 +759,14 @@ class AddonEvents extends AddonBase { editorInstance, params: {viewTex: boolean} } */ - const instance = message.content.editorInstance; + const editor = message.content.editorInstance as Zotero.EditorInstance; let viewTex = false; if (message.content.params && message.content.params.viewTex) { viewTex = true; } - if (!this._Addon.views._texNotes.includes(instance._item.id) || viewTex) { + if (!this._Addon.views._texNotes.includes(editor._item.id) || viewTex) { const editorElement = - instance._iframeWindow.document.getElementsByClassName( + editor._iframeWindow.document.getElementsByClassName( "primary-editor" )[0]; if (!editorElement) { @@ -777,17 +775,17 @@ class AddonEvents extends AddonBase { Zotero.debug("Knowledge4Zotero: latex view on."); const viewNode = editorElement.cloneNode(true) as HTMLElement; - this._Addon.views.switchEditorTexView(instance, true, viewNode); + this._Addon.views.switchEditorTexView(editor, true, viewNode); this._Addon.views.changeEditorButtonView( - instance._iframeWindow.document.getElementById("knowledge-switchTex"), + editor._iframeWindow.document.getElementById("knowledge-switchTex"), "switchEditor", `Editor View ${Zotero.isWin ? "Ctrl" : "⌘"}+/` ); } else { Zotero.debug("Knowledge4Zotero: latex view off."); - this._Addon.views.switchEditorTexView(instance, false); + this._Addon.views.switchEditorTexView(editor, false); this._Addon.views.changeEditorButtonView( - instance._iframeWindow.document.getElementById("knowledge-switchTex"), + editor._iframeWindow.document.getElementById("knowledge-switchTex"), "switchTex", `LaTex View ${Zotero.isWin ? "Ctrl" : "⌘"}+/` ); @@ -804,13 +802,13 @@ class AddonEvents extends AddonBase { let noteItem = message.content.editorInstance ? message.content.editorInstance._item : message.content.params.noteItem; - let topItems: ZoteroItem[] = []; + let topItems: Zotero.Item[] = []; console.log(message); if (message.content.event) { const topItemID = Number( message.content.event.target.id.split("-").pop() ); - topItems = Zotero.Items.get([topItemID]); + topItems = Zotero.Items.get([topItemID]) as Zotero.Item[]; } if (!topItems.length) { const io = { @@ -821,7 +819,7 @@ class AddonEvents extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://zotero/content/selectItemsDialog.xul", "", "chrome,dialog=no,centerscreen,resizable=yes", @@ -829,9 +827,9 @@ class AddonEvents extends AddonBase { ); await io.deferred.promise; - const ids = io.dataOut; - topItems = Zotero.Items.get(ids).filter((item: ZoteroItem) => - item.isRegularItem() + const ids = io.dataOut as unknown as number[]; + topItems = (Zotero.Items.get(ids) as Zotero.Item[]).filter( + (item: Zotero.Item) => item.isRegularItem() ); if (topItems.length === 0) { return; @@ -864,7 +862,7 @@ class AddonEvents extends AddonBase { undefined, // -1 for automatically insert to current selected line or end of note -1, - message.content.editorInstance._item.id + (message.content.editorInstance as Zotero.EditorInstance)._item.id ); } else if (message.type === "addToKnowledgeLine") { /* @@ -875,13 +873,15 @@ class AddonEvents extends AddonBase { } */ Zotero.debug("Knowledge4Zotero: addToKnowledgeLine"); - Zotero.debug(message.content.event.target.id); - const idSplit = message.content.event.target.id.split("-"); + Zotero.debug((message.content.event as XUL.XULEvent).target.id); + const idSplit = (message.content.event as XUL.XULEvent).target.id.split( + "-" + ); const lineIndex = parseInt(idSplit[idSplit.length - 1]); this._Addon.knowledge.addLinkToNote( undefined, lineIndex, - message.content.editorInstance._item.id + (message.content.editorInstance as Zotero.EditorInstance)._item.id ); } else if (message.type === "addAnnotationToNote") { /* @@ -966,14 +966,13 @@ class AddonEvents extends AddonBase { } */ const _window = this._Addon.knowledge.getWorkspaceWindow() as Window; - _window.document - .getElementById("preview-splitter") - .setAttribute("state", "collapsed"); + const splitter = _window.document.getElementById("preview-splitter"); + splitter && splitter.setAttribute("state", "collapsed"); } else if (message.type === "onNoteLink") { /* message.content = { params: { - item: ZoteroItem | boolean, + item: Zotero.Item | boolean, infoText: string } } @@ -1000,16 +999,19 @@ class AddonEvents extends AddonBase { ZoteroPane.openNoteWindow(message.content.params.item.id); } } else if (message.type === "noteEditorSelectionChange") { + const editor = message.content.editorInstance as Zotero.EditorInstance; if ( - message.content.editorInstance._item.id === - Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") + editor._item.id === Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") ) { // Update current line index - const _window = message.content.editorInstance._iframeWindow; + const _window = editor._iframeWindow; const selection = _window.document.getSelection(); + if (!selection || !selection.focusNode) { + return; + } const realElement = selection.focusNode.parentElement; let focusNode = selection.focusNode as XUL.Element; - if (!focusNode) { + if (!focusNode || !realElement) { return; } @@ -1020,8 +1022,10 @@ class AddonEvents extends AddonBase { // Make sure this is a direct child node of editor try { while ( - !focusNode.parentElement.className || - focusNode.parentElement.className.indexOf("primary-editor") === -1 + focusNode.parentElement && + (!focusNode.parentElement.className || + focusNode.parentElement.className.indexOf("primary-editor") === + -1) ) { focusNode = focusNode.parentNode as XUL.Element; } @@ -1029,6 +1033,10 @@ class AddonEvents extends AddonBase { return; } + if (!focusNode.parentElement) { + return; + } + let currentLineIndex = getChildIndex(focusNode); // Parse list @@ -1072,7 +1080,11 @@ class AddonEvents extends AddonBase { let t = 0; let linkPopup = _window.document.querySelector(".link-popup"); while ( - !(linkPopup && linkPopup.querySelector("a").href === link) && + !( + linkPopup && + (linkPopup.querySelector("a") as unknown as HTMLLinkElement) + .href === link + ) && t < 100 ) { t += 1; @@ -1080,12 +1092,12 @@ class AddonEvents extends AddonBase { await Zotero.Promise.delay(30); } await this._Addon.views.updateEditorPopupButtons( - message.content.editorInstance._iframeWindow, + editor._iframeWindow, link ); } else { await this._Addon.views.updateEditorPopupButtons( - message.content.editorInstance._iframeWindow, + editor._iframeWindow, undefined ); } @@ -1099,7 +1111,7 @@ class AddonEvents extends AddonBase { */ const text = prompt(`Enter new heading to Insert:`); this._Addon.knowledge.openWorkspaceWindow(); - if (text.trim()) { + if (text && text.trim()) { if (this._Addon.knowledge.currentNodeID < 0) { // Add a new H1 this._Addon.knowledge.addLineToNote( @@ -1197,7 +1209,7 @@ class AddonEvents extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://zotero/content/selectItemsDialog.xul", "", "chrome,dialog=no,centerscreen,resizable=yes", @@ -1205,15 +1217,15 @@ class AddonEvents extends AddonBase { ); await io.deferred.promise; - const ids = io.dataOut; - const notes = Zotero.Items.get(ids).filter((item: ZoteroItem) => - item.isNote() + const ids = io.dataOut as unknown as number[]; + const notes = (Zotero.Items.get(ids) as Zotero.Item[]).filter( + (item: Zotero.Item) => item.isNote() ); if (notes.length === 0) { return; } - const newLines = []; + const newLines: string[] = []; newLines.push("

Imported Notes

"); newLines.push("

"); @@ -1236,7 +1248,7 @@ class AddonEvents extends AddonBase { params: {templateName, copy} } */ - const newLines = []; + const newLines: string[] = []; const progressWindow = this._Addon.views.showProgressWindow( "Running Template", @@ -1244,9 +1256,9 @@ class AddonEvents extends AddonBase { "default", -1 ); - const renderredTemplate = await this._Addon.template.renderTemplateAsync( + const renderredTemplate = (await this._Addon.template.renderTemplateAsync( message.content.params.templateName - ); + )) as string; if (renderredTemplate) { newLines.push(renderredTemplate); @@ -1282,7 +1294,7 @@ class AddonEvents extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://zotero/content/selectItemsDialog.xul", "", "chrome,dialog=no,centerscreen,resizable=yes", @@ -1290,9 +1302,9 @@ class AddonEvents extends AddonBase { ); await io.deferred.promise; - const ids = io.dataOut; - const items = Zotero.Items.get(ids).filter((item: ZoteroItem) => - item.isRegularItem() + const ids = io.dataOut as unknown as number[]; + const items = (Zotero.Items.get(ids) as Zotero.Item[]).filter( + (item: Zotero.Item) => item.isRegularItem() ); if (items.length === 0) { return; @@ -1303,12 +1315,12 @@ class AddonEvents extends AddonBase { "default", -1 ); - const newLines = []; + const newLines: string[] = []; newLines.push("

"); - const toCopyImage = []; + const toCopyImage: Zotero.Item[] = []; - const copyNoteImage = (noteItem: ZoteroItem) => { + const copyNoteImage = (noteItem: Zotero.Item) => { toCopyImage.push(noteItem); }; @@ -1338,9 +1350,9 @@ class AddonEvents extends AddonBase { topItem, itemNotes, copyNoteImage, editor */ - const itemNotes: ZoteroItem[] = topItem - .getNotes() - .map((e) => Zotero.Items.get(e)); + const itemNotes: Zotero.Item[] = Zotero.Items.get( + topItem.getNotes() + ) as Zotero.Item[]; renderredTemplate = await this._Addon.template.renderTemplateAsync( message.content.params.templateName, @@ -1414,7 +1426,7 @@ class AddonEvents extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://zotero/content/selectItemsDialog.xul", "", "chrome,dialog=no,centerscreen,resizable=yes", @@ -1422,9 +1434,9 @@ class AddonEvents extends AddonBase { ); await io.deferred.promise; - const ids = io.dataOut; - const notes = Zotero.Items.get(ids).filter((item: ZoteroItem) => - item.isNote() + const ids = io.dataOut as unknown as number[]; + const notes = (Zotero.Items.get(ids) as Zotero.Item[]).filter( + (item: Zotero.Item) => item.isNote() ); if (notes.length === 0) { return; @@ -1435,12 +1447,12 @@ class AddonEvents extends AddonBase { "default", -1 ); - const newLines = []; + const newLines: string[] = []; newLines.push("

"); - const toCopyImage = []; + const toCopyImage: Zotero.Item[] = []; - const copyNoteImage = (noteItem: ZoteroItem) => { + const copyNoteImage = (noteItem: Zotero.Item) => { toCopyImage.push(noteItem); }; @@ -1562,7 +1574,7 @@ class AddonEvents extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://Knowledge4Zotero/content/sync.xul", "", "chrome,centerscreen,width=500,height=200", @@ -1579,7 +1591,7 @@ class AddonEvents extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://Knowledge4Zotero/content/export.xul", "", "chrome,centerscreen,width=400,height=400,resizable=yes", @@ -1587,7 +1599,7 @@ class AddonEvents extends AddonBase { ); await io.deferred.promise; - const options = io.dataOut; + const options = io.dataOut as any; if (options.exportFile && options.exportSingleFile) { await this._Addon.knowledge.exportNotesToFile( [item], @@ -1609,13 +1621,17 @@ class AddonEvents extends AddonBase { message.content = {} */ const items = ZoteroPane.getSelectedItems(); - const noteItems = []; + const noteItems: Zotero.Item[] = []; items.forEach((item) => { if (item.isNote()) { noteItems.push(item); } if (item.isRegularItem()) { - noteItems.splice(0, 0, Zotero.Items.get(item.getNotes())); + noteItems.splice( + 0, + 0, + ...(Zotero.Items.get(item.getNotes()) as Zotero.Item[]) + ); } }); if (noteItems.length === 0) { @@ -1652,13 +1668,14 @@ class AddonEvents extends AddonBase { editorInstance } */ - const note = message.content.editorInstance._item; + const editor = message.content.editorInstance as Zotero.EditorInstance; + const note = editor._item; let successCount = 0; let failCount = 0; if (note.parentItem) { - for (const attchment of Zotero.Items.get( - note.parentItem.getAttachments() - ).filter((item: ZoteroItem) => { + for (const attchment of ( + Zotero.Items.get(note.parentItem.getAttachments()) as Zotero.Item[] + ).filter((item: Zotero.Item) => { return item.isPDFAttachment(); })) { Zotero.debug(attchment); @@ -1707,7 +1724,7 @@ class AddonEvents extends AddonBase { params: { annotationItem } } */ - const annotationItem: ZoteroItem = message.content.params.annotationItem; + const annotationItem: Zotero.Item = message.content.params.annotationItem; if (annotationItem.annotationComment) { const text = annotationItem.annotationComment; @@ -1729,13 +1746,13 @@ class AddonEvents extends AddonBase { } } - const note: ZoteroItem = new Zotero.Item("note"); + const note: Zotero.Item = new Zotero.Item("note"); note.libraryID = annotationItem.libraryID; note.parentID = annotationItem.parentItem.parentID; await note.saveTx(); ZoteroPane.openNoteWindow(note.id); - let editorInstance: EditorInstance = + let editorInstance: Zotero.EditorInstance = this._Addon.knowledge.getEditorInstance(note); let t = 0; // Wait for editor instance diff --git a/src/export.ts b/src/export.ts index bae89cc..d8e6724 100644 --- a/src/export.ts +++ b/src/export.ts @@ -1,4 +1,5 @@ -import { AddonBase } from "./base"; +import Knowledge4Zotero from "./addon"; +import AddonBase from "./module"; class AddonExport extends AddonBase { private io: { @@ -13,9 +14,9 @@ class AddonExport extends AddonBase { doLoad(_window: Window) { this._window = _window; - this.io = (this._window as unknown as XULWindow).arguments[0]; + this.io = (this._window as unknown as XUL.XULWindow).arguments[0]; - let exportFile = Zotero.Prefs.get("Knowledge4Zotero.exportFile"); + let exportFile = Zotero.Prefs.get("Knowledge4Zotero.exportFile") as boolean; if (typeof exportFile !== "undefined") { ( this._window.document.getElementById( @@ -25,7 +26,7 @@ class AddonExport extends AddonBase { } let exportSingleFile = Zotero.Prefs.get( "Knowledge4Zotero.exportSingleFile" - ); + ) as boolean; if (typeof exportSingleFile !== "undefined") { ( this._window.document.getElementById( @@ -33,7 +34,9 @@ class AddonExport extends AddonBase { ) as XUL.Checkbox ).checked = exportSingleFile; } - let exportAutoSync = Zotero.Prefs.get("Knowledge4Zotero.exportAutoSync"); + let exportAutoSync = Zotero.Prefs.get( + "Knowledge4Zotero.exportAutoSync" + ) as boolean; if (typeof exportAutoSync !== "undefined") { ( this._window.document.getElementById( @@ -41,7 +44,9 @@ class AddonExport extends AddonBase { ) as XUL.Checkbox ).checked = exportAutoSync; } - let exportHighlight = Zotero.Prefs.get("Knowledge4Zotero.exportHighlight"); + let exportHighlight = Zotero.Prefs.get( + "Knowledge4Zotero.exportHighlight" + ) as boolean; if (typeof exportHighlight !== "undefined") { ( this._window.document.getElementById( @@ -49,7 +54,9 @@ class AddonExport extends AddonBase { ) as XUL.Checkbox ).checked = exportHighlight; } - let convertSquare = Zotero.Prefs.get("Knowledge4Zotero.convertSquare"); + let convertSquare = Zotero.Prefs.get( + "Knowledge4Zotero.convertSquare" + ) as boolean; if (typeof convertSquare !== "undefined") { ( this._window.document.getElementById( @@ -57,7 +64,7 @@ class AddonExport extends AddonBase { ) as XUL.Checkbox ).checked = convertSquare; } - let embedLink = Zotero.Prefs.get("Knowledge4Zotero.embedLink"); + let embedLink = Zotero.Prefs.get("Knowledge4Zotero.embedLink") as boolean; if (typeof embedLink !== "undefined") { ( this._window.document.getElementById( @@ -65,7 +72,7 @@ class AddonExport extends AddonBase { ) as XUL.Checkbox ).checked = embedLink; } - let exportNote = Zotero.Prefs.get("Knowledge4Zotero.exportNote"); + let exportNote = Zotero.Prefs.get("Knowledge4Zotero.exportNote") as boolean; if (typeof exportNote !== "undefined") { ( this._window.document.getElementById( @@ -73,7 +80,7 @@ class AddonExport extends AddonBase { ) as XUL.Checkbox ).checked = exportNote; } - let exportCopy = Zotero.Prefs.get("Knowledge4Zotero.exportCopy"); + let exportCopy = Zotero.Prefs.get("Knowledge4Zotero.exportCopy") as boolean; if (typeof exportCopy !== "undefined") { ( this._window.document.getElementById( @@ -81,7 +88,7 @@ class AddonExport extends AddonBase { ) as XUL.Checkbox ).checked = exportCopy; } - let exportPDF = Zotero.Prefs.get("Knowledge4Zotero.exportPDF"); + let exportPDF = Zotero.Prefs.get("Knowledge4Zotero.exportPDF") as boolean; if (typeof exportPDF !== "undefined") { ( this._window.document.getElementById( @@ -91,7 +98,7 @@ class AddonExport extends AddonBase { } this.doUpdate(); } - doUpdate(event: XULEvent = undefined) { + doUpdate(event?: XUL.XULEvent) { let embedLink = this._window.document.getElementById( "Knowledge4Zotero-export-embedLink" ) as XUL.Checkbox; @@ -112,7 +119,6 @@ class AddonExport extends AddonBase { "Knowledge4Zotero-export-convertsquare" ) as XUL.Checkbox; - if (event) { if ( event.target.id === "Knowledge4Zotero-export-embedLink" && @@ -154,47 +160,47 @@ class AddonExport extends AddonBase { this._window.document.getElementById( "Knowledge4Zotero-export-enablefile" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let exportSingleFile = ( this._window.document.getElementById( "Knowledge4Zotero-export-enablesingle" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let exportAutoSync = ( this._window.document.getElementById( "Knowledge4Zotero-export-enableautosync" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let exportHighlight = ( this._window.document.getElementById( "Knowledge4Zotero-export-enablehighlight" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let convertSquare = ( this._window.document.getElementById( "Knowledge4Zotero-export-convertsquare" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let embedLink = ( this._window.document.getElementById( "Knowledge4Zotero-export-embedLink" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let exportNote = ( this._window.document.getElementById( "Knowledge4Zotero-export-enablenote" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let exportCopy = ( this._window.document.getElementById( "Knowledge4Zotero-export-enablecopy" ) as XUL.Checkbox - ).checked; + ).checked as boolean; let exportPDF = ( this._window.document.getElementById( "Knowledge4Zotero-export-enablepdf" ) as XUL.Checkbox - ).checked; + ).checked as boolean; Zotero.Prefs.set("Knowledge4Zotero.exportFile", exportFile); Zotero.Prefs.set("Knowledge4Zotero.exportSingleFile", exportSingleFile); Zotero.Prefs.set("Knowledge4Zotero.exportAutoSync", exportAutoSync); diff --git a/src/knowledge.ts b/src/knowledge.ts index 4d4d368..2b1af40 100644 --- a/src/knowledge.ts +++ b/src/knowledge.ts @@ -1,13 +1,15 @@ -import { AddonBase, EditorMessage, OutlineType } from "./base"; +import Knowledge4Zotero from "./addon"; +import { OutlineType } from "./base"; import { loadTranslator, TRANSLATOR_ID_BETTER_MARKDOWN } from "./exportMD"; import { pick } from "./file_picker"; +import AddonBase from "./module"; class Knowledge extends AddonBase { currentLine: number; currentNodeID: number; workspaceWindow: Window; workspaceTabId: string; - workspaceNoteEditor: EditorInstance; + workspaceNoteEditor: Zotero.EditorInstance | undefined; previewItemID: number; _firstInit: boolean; _workspacePromise: any; @@ -22,13 +24,12 @@ class Knowledge extends AddonBase { this.currentLine = -1; this.currentNodeID = -1; this._pdfNoteId = -1; - this.workspaceNoteEditor = undefined; } - getWorkspaceNote(): ZoteroItem { + getWorkspaceNote(): Zotero.Item { return Zotero.Items.get( - Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") - ); + Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") as number + ) as Zotero.Item; } getWorkspaceWindow(): Window | boolean { @@ -67,7 +68,7 @@ class Knowledge extends AddonBase { "_blank", "chrome,extrachrome,menubar,resizable,scrollbars,status,width=1000,height=600" ); - this.workspaceWindow = win; + this.workspaceWindow = win as Window; this.workspaceTabId = ""; await this.waitWorkspaceReady(); this.setWorkspaceNote("main"); @@ -87,7 +88,7 @@ class Knowledge extends AddonBase { index: 1, data: {}, select: select, - onClose: undefined, + onClose: undefined }); this.workspaceTabId = id; const _iframe = window.document.createElement("browser"); @@ -150,7 +151,7 @@ class Knowledge extends AddonBase { async getWorkspaceEditorInstance( type: "main" | "preview" = "main", wait: boolean = true - ): Promise { + ): Promise { let noteEditor = (await this.getWorkspaceEditor(type)) as any; if (!noteEditor) { return; @@ -161,11 +162,11 @@ class Knowledge extends AddonBase { await Zotero.Promise.delay(10); } this.workspaceNoteEditor = - noteEditor.getCurrentInstance() as EditorInstance; + noteEditor.getCurrentInstance() as Zotero.EditorInstance; return this.workspaceNoteEditor; } - getEditorInstance(note: ZoteroItem) { + getEditorInstance(note: Zotero.Item) { // If there are multiple editors of main note available, we use the workspace editor. if ( note.id === this.getWorkspaceNote().id && @@ -175,7 +176,9 @@ class Knowledge extends AddonBase { ) { return this.workspaceNoteEditor; } - const editor = (Zotero.Notes._editorInstances as EditorInstance[]).find( + const editor = ( + Zotero.Notes._editorInstances as Zotero.EditorInstance[] + ).find( (e) => e._item.id === note.id && !Components.utils.isDeadWrapper(e._iframeWindow) @@ -188,7 +191,7 @@ class Knowledge extends AddonBase { async setWorkspaceNote( type: "main" | "preview" = "main", - note: ZoteroItem = undefined + note: Zotero.Item | undefined = undefined ) { let _window = this.getWorkspaceWindow() as Window; note = note || this.getWorkspaceNote(); @@ -196,9 +199,8 @@ class Knowledge extends AddonBase { return; } if (type === "preview") { - _window.document - .getElementById("preview-splitter") - .setAttribute("state", "open"); + const splitter = _window.document.getElementById("preview-splitter"); + splitter && splitter.setAttribute("state", "open"); this.previewItemID = note.id; } else { // Set line to default @@ -247,7 +249,7 @@ class Knowledge extends AddonBase { } } - getLinesInNote(note: ZoteroItem): string[] { + getLinesInNote(note: Zotero.Item): string[] { note = note || this.getWorkspaceNote(); if (!note) { return []; @@ -256,7 +258,7 @@ class Knowledge extends AddonBase { return this._Addon.parse.parseHTMLLines(noteText); } - async setLinesToNote(note: ZoteroItem, noteLines: string[]) { + async setLinesToNote(note: Zotero.Item, noteLines: string[]) { note = note || this.getWorkspaceNote(); if (!note) { return []; @@ -278,7 +280,7 @@ class Knowledge extends AddonBase { } private async addLineToNote( - note: ZoteroItem, + note: Zotero.Item, text: string, lineIndex: number, forceMetadata: boolean = false, @@ -342,7 +344,11 @@ class Knowledge extends AddonBase { _dataURLtoBlob(dataurl: string) { let parts = dataurl.split(","); - let mime = parts[0].match(/:(.*?);/)[1]; + let matches = parts[0]?.match(/:(.*?);/); + if (!matches || !matches[1]) { + return; + } + let mime = matches[1]; if (parts[0].indexOf("base64") !== -1) { let bstr = atob(parts[1]); let n = bstr.length; @@ -356,7 +362,7 @@ class Knowledge extends AddonBase { return null; } - async _importImage(note: ZoteroItem, src, download = false) { + async _importImage(note: Zotero.Item, src, download = false) { let blob; if (src.startsWith("data:")) { blob = this._dataURLtoBlob(src); @@ -382,7 +388,7 @@ class Knowledge extends AddonBase { return attachment.key; } - async importImagesToNote(note: ZoteroItem, annotations: any) { + async importImagesToNote(note: Zotero.Item, annotations: any) { for (let annotation of annotations) { if (annotation.image) { annotation.imageAttachmentKey = await this._importImage( @@ -395,8 +401,8 @@ class Knowledge extends AddonBase { } async addAnnotationsToNote( - note: ZoteroItem, - annotations: ZoteroItem[], + note: Zotero.Item, + annotations: Zotero.Item[], lineIndex: number ) { note = note || this.getWorkspaceNote(); @@ -409,7 +415,7 @@ class Knowledge extends AddonBase { } addLinkToNote( - targetNote: ZoteroItem, + targetNote: Zotero.Item, lineIndex: number, linkedNoteID: number ) { @@ -417,7 +423,7 @@ class Knowledge extends AddonBase { if (!targetNote) { return; } - let linkedNote = Zotero.Items.get(linkedNoteID); + let linkedNote = Zotero.Items.get(linkedNoteID) as Zotero.Item; if (!linkedNote.isNote()) { this._Addon.views.showProgressWindow("Better Notes", "Not a note item"); return; @@ -450,7 +456,7 @@ class Knowledge extends AddonBase { ); } - getNoteLink(note: ZoteroItem) { + getNoteLink(note: Zotero.Item) { let libraryID = note.libraryID; let library = Zotero.Libraries.get(libraryID); let groupID: string; @@ -458,12 +464,14 @@ class Knowledge extends AddonBase { groupID = "u"; } else if (library.libraryType === "group") { groupID = `${library.id}`; + } else { + return ""; } let noteKey = note.key; return `zotero://note/${groupID}/${noteKey}/`; } - getAnnotationLink(annotation: ZoteroItem) { + getAnnotationLink(annotation: Zotero.Item) { let position = JSON.parse(annotation.annotationPosition); let openURI: string; @@ -474,6 +482,8 @@ class Knowledge extends AddonBase { openURI = `zotero://open-pdf/library/items/${attachment.key}`; } else if (library.libraryType === "group") { openURI = `zotero://open-pdf/groups/${library.id}/items/${attachment.key}`; + } else { + openURI = ""; } openURI += @@ -485,7 +495,7 @@ class Knowledge extends AddonBase { } async modifyLineInNote( - note: ZoteroItem, + note: Zotero.Item, text: string | Function, lineIndex: number, forceMetadata: boolean = false @@ -531,7 +541,7 @@ class Knowledge extends AddonBase { } async changeHeadingLineInNote( - note: ZoteroItem, + note: Zotero.Item, rankChange: number, lineIndex: number ) { @@ -565,7 +575,7 @@ class Knowledge extends AddonBase { } moveHeaderLineInNote( - note: ZoteroItem, + note: Zotero.Item, currentNode: TreeModel.Node, targetNode: TreeModel.Node, as: "child" | "before" | "after" @@ -630,7 +640,9 @@ class Knowledge extends AddonBase { this.setLinesToNote(note, newLines); } - getNoteTree(note: ZoteroItem = undefined): TreeModel.Node { + getNoteTree( + note: Zotero.Item | undefined = undefined + ): TreeModel.Node { // See http://jnuno.com/tree-model-js note = note || this.getWorkspaceNote(); if (!note) { @@ -640,7 +652,7 @@ class Knowledge extends AddonBase { } getNoteTreeAsList( - note: ZoteroItem, + note: Zotero.Item, filterRoot: boolean = true, filterLink: boolean = true ): TreeModel.Node[] { @@ -656,7 +668,7 @@ class Knowledge extends AddonBase { } getNoteTreeNodeById( - note: ZoteroItem, + note: Zotero.Item, id: number, root: TreeModel.Node = undefined ) { @@ -667,7 +679,7 @@ class Knowledge extends AddonBase { } getNoteTreeNodesByRank( - note: ZoteroItem, + note: Zotero.Item, rank: number, root: TreeModel.Node = undefined ) { @@ -678,7 +690,7 @@ class Knowledge extends AddonBase { } getLineParentNode( - note: ZoteroItem, + note: Zotero.Item, lineIndex: number = -1 ): TreeModel.Node { if (lineIndex < 0) { @@ -712,7 +724,7 @@ class Knowledge extends AddonBase { } async exportNoteToFile( - note: ZoteroItem, + note: Zotero.Item, convertNoteLinks: boolean = true, saveFile: boolean = true, saveNote: boolean = false, @@ -725,10 +737,10 @@ class Knowledge extends AddonBase { this._exportFileDict = []; note = note || this.getWorkspaceNote(); - let newNote: ZoteroItem; + let newNote: Zotero.Item; if (convertNoteLinks || saveNote) { const noteID = await ZoteroPane_Local.newNote(); - newNote = Zotero.Items.get(noteID); + newNote = Zotero.Items.get(noteID) as Zotero.Item; const rootNoteIds = [note.id]; const convertResult = await this.convertNoteLines( @@ -823,7 +835,7 @@ class Knowledge extends AddonBase { const checkPrint = () => { try { const editor: any = _w.document.querySelector("#zotero-note-editor"); - const instance: EditorInstance = editor.getCurrentInstance(); + const instance: Zotero.EditorInstance = editor.getCurrentInstance(); console.log(instance._iframeWindow.document.title); if (instance._iframeWindow.document.title === "Printed") { this._pdfPrintPromise.resolve(); @@ -856,7 +868,7 @@ class Knowledge extends AddonBase { } async exportNotesToFile( - notes: ZoteroItem[], + notes: Zotero.Item[], useEmbed: boolean, useSync: boolean = false ) { @@ -882,10 +894,10 @@ class Knowledge extends AddonBase { if (useEmbed) { for (const note of notes) { - let newNote: ZoteroItem; + let newNote: Zotero.Item; if (this._Addon.parse.parseLinkInText(note.getNote())) { const noteID = await ZoteroPane_Local.newNote(); - newNote = Zotero.Items.get(noteID); + newNote = Zotero.Items.get(noteID) as Zotero.Item; const rootNoteIds = [note.id]; const convertResult = await this.convertNoteLines( @@ -917,7 +929,7 @@ class Knowledge extends AddonBase { } else { // Export every linked note as a markdown file // Find all linked notes that need to be exported - let allNoteIds: number[] = [].concat(notes.map((n) => n.id)); + let allNoteIds: number[] = notes.map((n) => n.id); for (const note of notes) { const linkMatches = note .getNote() @@ -936,7 +948,9 @@ class Knowledge extends AddonBase { } allNoteIds = Array.from(new Set(allNoteIds)); console.log(allNoteIds); - const allNoteItems: ZoteroItem[] = Zotero.Items.get(allNoteIds); + const allNoteItems: Zotero.Item[] = Zotero.Items.get( + allNoteIds + ) as Zotero.Item[]; const noteLinkDict = allNoteItems.map((_note) => { return { link: this.getNoteLink(_note), @@ -963,14 +977,14 @@ class Knowledge extends AddonBase { } } - async syncNotesToFile(notes: ZoteroItem[], filepath: string) { + async syncNotesToFile(notes: Zotero.Item[], filepath: string) { this._exportPath = Zotero.File.pathToFile(filepath).path + "/attachments"; // Convert to unix format this._exportPath = this._exportPath.replace(/\\/g, "/"); // Export every linked note as a markdown file // Find all linked notes that need to be exported - let allNoteIds: number[] = [].concat(notes.map((n) => n.id)); + let allNoteIds: number[] = notes.map((n) => n.id); for (const note of notes) { const linkMatches = note.getNote().match(/zotero:\/\/note\/\w+\/\w+\//g); if (!linkMatches) { @@ -987,7 +1001,9 @@ class Knowledge extends AddonBase { } allNoteIds = new Array(...new Set(allNoteIds)); // console.log(allNoteIds); - const allNoteItems: ZoteroItem[] = Zotero.Items.get(allNoteIds); + const allNoteItems: Zotero.Item[] = Zotero.Items.get( + allNoteIds + ) as Zotero.Item[]; const noteLinkDict = allNoteItems.map((_note) => { return { link: this.getNoteLink(_note), @@ -1009,7 +1025,7 @@ class Knowledge extends AddonBase { } private async _export( - note: ZoteroItem, + note: Zotero.Item, filename: string, deleteAfterExport: boolean ) { @@ -1045,7 +1061,7 @@ class Knowledge extends AddonBase { } } - private _getFileName(noteItem: ZoteroItem) { + private _getFileName(noteItem: Zotero.Item) { return this._Addon.template.renderTemplate( "[ExportMDFileName]", "noteItem", @@ -1054,17 +1070,17 @@ class Knowledge extends AddonBase { } async convertNoteLines( - currentNote: ZoteroItem, + currentNote: Zotero.Item, rootNoteIds: number[], convertNoteLinks: boolean = true - ): Promise<{ lines: string[]; subNotes: ZoteroItem[] }> { + ): Promise<{ lines: string[]; subNotes: Zotero.Item[] }> { Zotero.debug(`convert note ${currentNote.id}`); - let subNotes = []; + let subNotes: Zotero.Item[] = []; const [..._rootNoteIds] = rootNoteIds; _rootNoteIds.push(currentNote.id); - let newLines = []; + let newLines: string[] = []; const noteLines = this.getLinesInNote(currentNote); for (let i in noteLines) { newLines.push(noteLines[i]); diff --git a/src/module.ts b/src/module.ts new file mode 100644 index 0000000..adec849 --- /dev/null +++ b/src/module.ts @@ -0,0 +1,8 @@ +class AddonBase { + protected _Addon: any; + constructor(parent: any) { + this._Addon = parent; + } +} + +export default AddonBase; diff --git a/src/parse.ts b/src/parse.ts index 3cce403..ca73c33 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,10 +1,10 @@ -import { AddonBase } from "./base"; +import AddonBase from "./module"; import { HTML2Markdown, Markdown2HTML } from "./convertMD"; const TreeModel = require("./treemodel"); const asciidoctor = require("asciidoctor")(); class AddonParse extends AddonBase { - public parseNoteTree(note: ZoteroItem): TreeModel.Node { + public parseNoteTree(note: Zotero.Item): TreeModel.Node { const noteLines = this._Addon.knowledge.getLinesInNote(note); let tree = new TreeModel(); /* @@ -221,7 +221,7 @@ class AddonParse extends AddonBase { return currentElement; } - async parseAnnotation(annotationItem: ZoteroItem) { + async parseAnnotation(annotationItem: Zotero.Item) { try { if (!annotationItem || !annotationItem.isAnnotation()) { return null; @@ -242,8 +242,8 @@ class AddonParse extends AddonBase { } async parseAnnotationHTML( - note: ZoteroItem, - annotations: ZoteroItem[], + note: Zotero.Item, + annotations: Zotero.Item[], ignoreComment: boolean = false ) { if (!note) { @@ -265,7 +265,7 @@ class AddonParse extends AddonBase { return html; } - async parseNoteStyleHTML(item: ZoteroItem, lineCount: 5) { + async parseNoteStyleHTML(item: Zotero.Item, lineCount: 5) { if (!item.isNote()) { throw new Error("Item is not a note"); } @@ -381,7 +381,7 @@ class AddonParse extends AddonBase { return eleList; } - parseNoteHTML(note: ZoteroItem): Element { + parseNoteHTML(note: Zotero.Item): Element { note = note || this._Addon.knowledge.getWorkspaceNote(); if (!note) { return undefined; diff --git a/src/sync.ts b/src/sync.ts index 7f3e5f9..919eb4c 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -1,4 +1,5 @@ -import { AddonBase } from "./base"; +import Knowledge4Zotero from "./addon"; +import AddonBase from "./module"; class AddonSync extends AddonBase { triggerTime: number; @@ -17,7 +18,7 @@ class AddonSync extends AddonBase { this._window.close(); } this._window = _window; - this.io = (this._window as unknown as XULWindow).arguments[0]; + this.io = (this._window as unknown as XUL.XULWindow).arguments[0]; this.doUpdate(); } @@ -86,7 +87,7 @@ class AddonSync extends AddonBase { if (!enable) { const note = this.io.dataIn; const allNoteIds = await this.getRelatedNoteIds(note); - const notes = Zotero.Items.get(allNoteIds); + const notes = Zotero.Items.get(allNoteIds) as Zotero.Item[]; for (const item of notes) { await this.removeSyncNote(item); } @@ -101,18 +102,18 @@ class AddonSync extends AddonBase { (this._window.document.querySelector("dialog") as any).acceptDialog(); } - getSyncNoteIds(): Number[] { - const ids = Zotero.Prefs.get("Knowledge4Zotero.syncNoteIds"); + getSyncNoteIds(): number[] { + const ids = Zotero.Prefs.get("Knowledge4Zotero.syncNoteIds") as string; return ids.split(",").map((id: string) => Number(id)); } - isSyncNote(note: ZoteroItem): boolean { + isSyncNote(note: Zotero.Item): boolean { const syncNoteIds = this._Addon.sync.getSyncNoteIds(); return syncNoteIds.includes(note.id); } - async getRelatedNoteIds(note: ZoteroItem): Promise { - let allNoteIds: Number[] = [note.id]; + async getRelatedNoteIds(note: Zotero.Item): Promise { + let allNoteIds: number[] = [note.id]; const linkMatches = note.getNote().match(/zotero:\/\/note\/\w+\/\w+\//g); if (!linkMatches) { return allNoteIds; @@ -131,7 +132,7 @@ class AddonSync extends AddonBase { return allNoteIds; } - async getRelatedNoteIdsFromNotes(notes: ZoteroItem[]): Promise { + async getRelatedNoteIdsFromNotes(notes: Zotero.Item[]): Promise { let allNoteIds: Number[] = []; for (const note of notes) { allNoteIds = allNoteIds.concat(await this.getRelatedNoteIds(note)); @@ -139,7 +140,7 @@ class AddonSync extends AddonBase { return allNoteIds; } - addSyncNote(noteItem: ZoteroItem) { + addSyncNote(noteItem: Zotero.Item) { const ids = this.getSyncNoteIds(); if (ids.includes(noteItem.id)) { return; @@ -148,7 +149,7 @@ class AddonSync extends AddonBase { Zotero.Prefs.set("Knowledge4Zotero.syncNoteIds", ids.join(",")); } - async removeSyncNote(noteItem: ZoteroItem) { + async removeSyncNote(noteItem: Zotero.Item) { const ids = this.getSyncNoteIds(); Zotero.Prefs.set( "Knowledge4Zotero.syncNoteIds", @@ -161,7 +162,7 @@ class AddonSync extends AddonBase { await noteItem.saveTx(); } - getNoteSyncStatus(noteItem: ZoteroItem): any { + getNoteSyncStatus(noteItem: Zotero.Item): any { const sycnInfo = noteItem.getTags().find((t) => t.tag.includes("sync://")); if (!sycnInfo) { return false; @@ -178,7 +179,7 @@ class AddonSync extends AddonBase { } async updateNoteSyncStatus( - noteItem: ZoteroItem, + noteItem: Zotero.Item, path: string = "", filename: string = "" ) { @@ -213,12 +214,12 @@ class AddonSync extends AddonBase { } async doSync( - items: ZoteroItem[] = null, + items: Zotero.Item[] = null, force: boolean = false, useIO: boolean = true ) { Zotero.debug("Better Notes: sync start"); - items = items || Zotero.Items.get(this.getSyncNoteIds()); + items = items || (Zotero.Items.get(this.getSyncNoteIds()) as Zotero.Item[]); const toExport = {}; const forceNoteIds = force ? await this.getRelatedNoteIdsFromNotes(useIO ? [this.io.dataIn] : items) diff --git a/src/syncList.ts b/src/syncList.ts index c2e90e0..51e4b46 100644 --- a/src/syncList.ts +++ b/src/syncList.ts @@ -1,4 +1,5 @@ -import { AddonBase } from "./base"; +import Knowledge4Zotero from "./addon"; +import AddonBase from "./module"; class AddonSyncList extends AddonBase { private _window: Window; @@ -28,7 +29,9 @@ class AddonSyncList extends AddonBase { if (!this._window || this._window.closed) { return; } - const notes = Zotero.Items.get(this._Addon.sync.getSyncNoteIds()); + const notes = Zotero.Items.get( + this._Addon.sync.getSyncNoteIds() + ) as Zotero.Item[]; const listbox = this._window.document.getElementById("sync-list"); let e, es = this._window.document.getElementsByTagName("listitem"); @@ -38,7 +41,8 @@ class AddonSyncList extends AddonBase { } for (const note of notes) { const syncInfo = this._Addon.sync.getNoteSyncStatus(note); - const listitem = this._window.document.createElement("listitem"); + const listitem: XUL.ListItem = + this._window.document.createElement("listitem"); listitem.setAttribute("id", note.id); const icon = this._window.document.createElement("listcell"); @@ -95,14 +99,14 @@ class AddonSyncList extends AddonBase { this._window.focus(); } - getSelectedItems(): ZoteroItem[] { + getSelectedItems(): Zotero.Item[] { return Zotero.Items.get( Array.prototype.map.call( (this._window.document.getElementById("sync-list") as any) .selectedItems, (node) => node.id ) - ); + ) as Zotero.Item[]; } useRelated(): Boolean { @@ -150,10 +154,10 @@ class AddonSyncList extends AddonBase { return; } if (this.useRelated()) { - let noteIds: Number[] = await this._Addon.sync.getRelatedNoteIdsFromNotes( + let noteIds: number[] = await this._Addon.sync.getRelatedNoteIdsFromNotes( selectedItems ); - selectedItems = Zotero.Items.get(noteIds); + selectedItems = Zotero.Items.get(noteIds) as Zotero.Item[]; } for (const note of selectedItems) { await this._Addon.sync.removeSyncNote(note); diff --git a/src/template.ts b/src/template.ts index f01966e..bd42ad4 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,4 +1,6 @@ -import { AddonBase, NoteTemplate } from "./base"; +import Knowledge4Zotero from "./addon"; +import { NoteTemplate } from "./base"; +import AddonBase from "./module"; class AddonTemplate extends AddonBase { private _window: Window; @@ -87,7 +89,7 @@ class AddonTemplate extends AddonBase { resetTemplates() { let oldTemplatesRaw: string = Zotero.Prefs.get( "Knowledge4Zotero.noteTemplate" - ); + ) as string; // Convert old version if (oldTemplatesRaw) { const templates: NoteTemplate[] = JSON.parse(oldTemplatesRaw); @@ -116,7 +118,7 @@ class AddonTemplate extends AddonBase { } getCitationStyle(): Object { - let format = Zotero.Prefs.get("Knowledge4Zotero.citeFormat"); + let format = Zotero.Prefs.get("Knowledge4Zotero.citeFormat") as string; try { if (format) { format = JSON.parse(format); @@ -229,7 +231,7 @@ class AddonTemplate extends AddonBase { deferred: Zotero.Promise.defer(), }; - (window as unknown as XULWindow).openDialog( + (window as unknown as XUL.XULWindow).openDialog( "chrome://zotero/content/selectItemsDialog.xul", "", "chrome,dialog=no,centerscreen,resizable=yes", @@ -238,8 +240,8 @@ class AddonTemplate extends AddonBase { await io.deferred.promise; const ids = io.dataOut; - const note: ZoteroItem = Zotero.Items.get(ids).filter((item: ZoteroItem) => - item.isNote() + const note: Zotero.Item = (Zotero.Items.get(ids) as Zotero.Item[]).filter( + (item: Zotero.Item) => item.isNote() )[0]; if (!note) { return; @@ -404,9 +406,9 @@ class AddonTemplate extends AddonBase { } getTemplateKeys(): NoteTemplate[] { - let templateKeys: string = Zotero.Prefs.get( + let templateKeys = Zotero.Prefs.get( "Knowledge4Zotero.templateKeys" - ); + ) as string; return templateKeys ? JSON.parse(templateKeys) : []; } @@ -442,9 +444,9 @@ class AddonTemplate extends AddonBase { } getTemplateText(keyName: string): string { - let template: string = Zotero.Prefs.get( + let template = Zotero.Prefs.get( `Knowledge4Zotero.template.${keyName}` - ); + ) as string; if (!template) { template = ""; Zotero.Prefs.set(`Knowledge4Zotero.template.${keyName}`, template); diff --git a/src/views.ts b/src/views.ts index a480505..2517c65 100644 --- a/src/views.ts +++ b/src/views.ts @@ -1,4 +1,6 @@ -import { AddonBase, EditorMessage, OutlineType } from "./base"; +import Knowledge4Zotero from "./addon"; +import { EditorMessage, OutlineType } from "./base"; +import AddonBase from "./module"; class AddonViews extends AddonBase { progressWindowIcon: object; @@ -79,7 +81,7 @@ class AddonViews extends AddonBase { .forEach((el) => (el as XUL.Element).setAttribute("disabled", disabled)); } - async addEditorKnowledgeToolBar(editorInstances: EditorInstance) { + async addEditorKnowledgeToolBar(editorInstances: Zotero.EditorInstance) { await editorInstances._initPromise; await new Promise((resolve, reject) => { @@ -105,7 +107,7 @@ class AddonViews extends AddonBase { } async addEditorButton( - editorInstances: EditorInstance, + editorInstances: Zotero.EditorInstance, id: string, icon: string, title: string, @@ -147,9 +149,9 @@ class AddonViews extends AddonBase { itemID: editorInstances._item.id, editorInstances: editorInstances, }); - dropdown.addEventListener("click", (e: XULEvent) => { + dropdown.addEventListener("click", (e: XUL.XULEvent) => { message.type = e.target.getAttribute("eventType"); - message.content.event = e as XULEvent; + message.content.event = e as XUL.XULEvent; message.content.editorInstance = editorInstances; this._Addon.events.onEditorEvent(message); }); @@ -157,7 +159,7 @@ class AddonViews extends AddonBase { } async addEditorPopup( - editorInstances: EditorInstance, + editorInstances: Zotero.EditorInstance, id: string, buttons: { id: string; text: string; rank: number; eventType: string }[], parentDropDown: Element @@ -191,9 +193,9 @@ class AddonViews extends AddonBase { itemID: editorInstances._item.id, editorInstances: editorInstances, }); - button.addEventListener("click", (e: XULEvent) => { + button.addEventListener("click", (e: XUL.XULEvent) => { message.type = e.target.getAttribute("eventType"); - message.content.event = e as XULEvent; + message.content.event = e as XUL.XULEvent; message.content.editorInstance = editorInstances; this._Addon.events.onEditorEvent(message); e.stopPropagation(); @@ -227,7 +229,7 @@ class AddonViews extends AddonBase { } switchEditorTexView( - instance: EditorInstance, + instance: Zotero.EditorInstance, showView: boolean, viewNode: HTMLElement = undefined ) { @@ -274,7 +276,7 @@ class AddonViews extends AddonBase { } } - async scrollToLine(instance: EditorInstance, lineIndex: number) { + async scrollToLine(instance: Zotero.EditorInstance, lineIndex: number) { await instance._initPromise; let editorElement = this.getEditorElement(instance._iframeWindow.document); const eleList = []; @@ -306,7 +308,7 @@ class AddonViews extends AddonBase { } } - scrollToPosition(instance: EditorInstance, offset: number) { + scrollToPosition(instance: Zotero.EditorInstance, offset: number) { let editorElement = this.getEditorElement(instance._iframeWindow.document); // @ts-ignore (editorElement.parentNode as HTMLElement).scrollTo(0, offset); @@ -370,25 +372,25 @@ class AddonViews extends AddonBase { new EditorMessage("openWorkspace", { event: e }) ); }); - treeRow.addEventListener("mouseover", (e: XULEvent) => { + treeRow.addEventListener("mouseover", (e: XUL.XULEvent) => { treeRow.setAttribute( "style", "height: 22px; margin: 0 0 0 0; padding: 0 6px 0 6px; background-color: grey;" ); }); - treeRow.addEventListener("mouseleave", (e: XULEvent) => { + treeRow.addEventListener("mouseleave", (e: XUL.XULEvent) => { treeRow.setAttribute( "style", "height: 22px; margin: 0 0 0 0; padding: 0 6px 0 6px;" ); }); - treeRow.addEventListener("mousedown", (e: XULEvent) => { + treeRow.addEventListener("mousedown", (e: XUL.XULEvent) => { treeRow.setAttribute( "style", "height: 22px; margin: 0 0 0 0; padding: 0 6px 0 6px; color: #FFFFFF;" ); }); - treeRow.addEventListener("mouseup", (e: XULEvent) => { + treeRow.addEventListener("mouseup", (e: XUL.XULEvent) => { treeRow.setAttribute( "style", "height: 22px; margin: 0 0 0 0; padding: 0 6px 0 6px;" @@ -400,7 +402,7 @@ class AddonViews extends AddonBase { } async updateEditorPopupButtons(_window: Window, link: string) { - const note: ZoteroItem = link + const note: Zotero.Item = link ? (await this._Addon.knowledge.getNoteFromLink(link)).item : undefined; // If the note is invalid, we remove the buttons @@ -481,7 +483,7 @@ class AddonViews extends AddonBase { await notifyFlag.promise; let hasAttachemnts = false; for (const _n of [note, ...convertResult.subNotes]) { - if (Zotero.Items.get(_n.getAttachments()).length) { + if ((Zotero.Items.get(_n.getAttachments()) as Zotero.Item[]).length) { hasAttachemnts = true; break; } @@ -522,7 +524,7 @@ class AddonViews extends AddonBase { let afterLink = line.slice(linkEnd); let linkPart = line.slice(linkStart, linkEnd); let link = this._Addon.parse.parseLinkInText(linkPart); - let currentNote: ZoteroItem; + let currentNote: Zotero.Item; if (link) { currentNote = (await this._Addon.knowledge.getNoteFromLink(link)) .item; @@ -615,7 +617,7 @@ class AddonViews extends AddonBase { } } - async addReaderAnnotationButton(reader: ReaderObj) { + async addReaderAnnotationButton(reader: _ZoteroReaderInstance) { if (!reader) { return false; } @@ -639,7 +641,8 @@ class AddonViews extends AddonBase { const itemKey = annotationWrapper.getAttribute( "data-sidebar-annotation-id" ); - const libraryID = Zotero.Items.get(reader.itemID).libraryID; + const libraryID = (Zotero.Items.get(reader.itemID) as Zotero.Item) + .libraryID; const annotationItem = await Zotero.Items.getByLibraryAndKeyAsync( libraryID, itemKey @@ -655,15 +658,21 @@ class AddonViews extends AddonBase { ); e.preventDefault(); }); - addAnnotationNoteButton.addEventListener("mouseover", (e: XULEvent) => { - addAnnotationNoteButton.setAttribute( - "style", - "background: #F0F0F0; margin: 5px;" - ); - }); - addAnnotationNoteButton.addEventListener("mouseout", (e: XULEvent) => { - addAnnotationNoteButton.setAttribute("style", "margin: 5px;"); - }); + addAnnotationNoteButton.addEventListener( + "mouseover", + (e: XUL.XULEvent) => { + addAnnotationNoteButton.setAttribute( + "style", + "background: #F0F0F0; margin: 5px;" + ); + } + ); + addAnnotationNoteButton.addEventListener( + "mouseout", + (e: XUL.XULEvent) => { + addAnnotationNoteButton.setAttribute("style", "margin: 5px;"); + } + ); moreButton.before(addAnnotationNoteButton); if (annotationItem.annotationType === "image" && Zotero.isWin) { // Customize image copy @@ -683,13 +692,13 @@ class AddonViews extends AddonBase { ); e.preventDefault(); }); - copyImageButton.addEventListener("mouseover", (e: XULEvent) => { + copyImageButton.addEventListener("mouseover", (e: XUL.XULEvent) => { copyImageButton.setAttribute( "style", "background: #F0F0F0; margin: 5px;" ); }); - copyImageButton.addEventListener("mouseout", (e: XULEvent) => { + copyImageButton.addEventListener("mouseout", (e: XUL.XULEvent) => { copyImageButton.setAttribute("style", "margin: 5px;"); }); moreButton.before(copyImageButton); diff --git a/src/wizard.ts b/src/wizard.ts index 717fb04..c5fe58b 100644 --- a/src/wizard.ts +++ b/src/wizard.ts @@ -1,4 +1,6 @@ -import { AddonBase, EditorMessage } from "./base"; +import Knowledge4Zotero from "./addon"; +import { EditorMessage } from "./base"; +import AddonBase from "./module"; class AddonWizard extends AddonBase { enableSetup: boolean; @@ -266,7 +268,7 @@ class AddonWizard extends AddonBase { } if (this.enableNote) { const noteID = await ZoteroPane_Local.newNote(); - Zotero.Items.get(noteID).setNote( + (Zotero.Items.get(noteID) as Zotero.Item).setNote( Zotero.locale === "zh-CN" ? this.templateCN : this.template ); await this._Addon.events.onEditorEvent( diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..3b7eef9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "CommonJS", + "target": "ES6", + }, + "include": [ + "src", + "typing", + "node_modules/zotero-types" + ], + "exclude": [ + "builds", + "addon" + ] + } \ No newline at end of file diff --git a/typing/addon.d.ts b/typing/addon.d.ts deleted file mode 100644 index ba742d5..0000000 --- a/typing/addon.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -declare interface Knowledge4Zotero { - events: import("../src/events"); - views: import("../src/view"); - wizard: import("../src/wizard"); - export: import("../src/export"); - parse: import("../src/parse"); - sync: import("../src/sync"); - syncList: import("../src/syncList"); - template: import("../src/template"); - knowledge: import("../src/knowledge"); -} diff --git a/typing/global.d.ts b/typing/global.d.ts deleted file mode 100644 index 3233f21..0000000 --- a/typing/global.d.ts +++ /dev/null @@ -1,245 +0,0 @@ -declare interface DirectoryIterator { - forEach(handler: any): Promise; - close(): void; - next: () => any; -} -declare interface DirectoryIteratorConstructable { - new (path: string): DirectoryIterator; // eslint-disable-line @typescript-eslint/prefer-function-type -} - -declare namespace OS { - namespace File { - type Entry = { - isDir: boolean; - size: number; - path: string; - unixMode?: number; - }; - type FileInfo = { - isDir: boolean; - size: number; - unixMode?: number; - lastModificationDate: Date; - }; - } -} -declare const OS: { - // https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/OSFile.jsm/OS.File_for_the_main_thread - File: { - exists: (path: string) => boolean | Promise; - read: ( - path: string | BufferSource, - options?: { encoding?: string } - ) => - | string - | Promise - | Uint8Array - | Promise - | Promise; - move: (from: string, to: string) => void | Promise; - remove: ( - path: string, - options?: { ignoreAbsent: boolean } - ) => Promise; - writeAtomic: ( - path: string, - data: Uint8Array | string, - options?: { tmpPath?: string; encoding?: string } - ) => void | Promise; - makeDir: ( - path: string, - options?: { ignoreExisting?: boolean } - ) => void | Promise; - stat: (path: string) => OS.File.FileInfo | Promise; - copy: ( - src: string, - tgt: string, - options?: { noOverwrite?: boolean } - ) => void; - removeDir: ( - path: string, - options?: { ignoreAbsent?: boolean; ignorePermissions?: boolean } - ) => void; - - DirectoryIterator: DirectoryIteratorConstructable; - }; - - // https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/OSFile.jsm/OS.Path - Path: { - join: (...args: string[]) => string; - dirname: (path: string) => string; - basename: (path: string) => string; - normalize: (path: string) => string; - split: (path: string) => { - absolute: boolean; - components: string[]; - winDrive?: string; - }; - toFileURI: (path: string) => string; - }; -}; - -declare const NetUtil: { [attr: string]: any }; - -declare interface ZoteroItem { - getFilePathAsync(): any; - id: number; - isRegularItem: () => boolean; - isNote: () => boolean; - getNote: () => string; - setNote: (string) => void; - getNoteTitle: () => string; - isAttachment: () => boolean; - isAnnotation: () => boolean; - isPDFAttachment: () => boolean; - addTag: (name: string, type: number) => boolean; - removeTag(tag: string): boolean; - itemTypeID: number; - libraryID: number; - parentID: number; - parentItem: ZoteroItem; - key: string; - _version: any; - getField: ( - name: string, - unformatted?: boolean, - includeBaseMapped?: boolean - ) => any; - setField: (name: string, value: string | number) => void; - getCreators: () => { - firstName?: string; - lastName: string; - fieldMode: number; - creatorTypeID: number; - }[]; - getCreatorsJSON: () => { - firstName?: string; - lastName?: string; - name?: string; - creatorType: string; - }[]; - getNotes: () => ZoteroItem[]; - getCollections: () => number[]; - getAttachments: () => ZoteroItem[]; - getTags: () => { tag: string; type: number }[]; - annotationType?: string; - annotationComment?: string; - annotationText?: string; - annotationPosition: string; - save: (obj?: any) => Promise; - saveTx: (obj?: any) => Promise; - addToCollection(id: number); -} - -// https://stackoverflow.com/questions/39040108/import-class-in-definition-file-d-ts -declare const Zotero: { - [attr: string]: any; - debug: (args: any) => void; - Prefs: { - get: (key: string) => any; - set: (key: string, value: any) => any; - }; - Items: { - get: (key: string | number) => ZoteroItem; - }; - Reader: Reader; - Notes: Notes; - Knowledge4Zotero: import("../src/addon"); -}; - -declare const ZoteroPane: { - [attr: string]: any; - canEdit: () => boolean; - displayCannotEditLibraryMessage: () => void; - getSelectedCollection: (arg: boolean) => ZoteroCollection; - getSelectedItems: () => Array; -}; - -declare const ZoteroPane_Local: { - getSelectedCollection: () => ZoteroCollection; - newNote: (popup?, parentKey?, text?, citeURI?) => Promise; -}; - -declare const Zotero_File_Interface: { - exportItemsToClipboard: (items: ZoteroItem[], translatorID: string) => void; -}; - -declare class ZoteroCollection { - getName: () => string; - getChildItems: (arg1: boolean, arg2: boolean) => Array; -} - -declare class Zotero_File_Exporter { - items: ZoteroItem[]; - save = async () => {}; -} - -declare const Components: any; -declare const Services: any; - -declare class Reader { - [attr: string]: any; - _readers: Array; - getByTabID: (tabID: string) => ReaderObj; -} - -declare class ReaderObj { - [attr: string]: any; - itemID: number; - _iframeWindow: XULWindow; - _initPromise: Promise; -} - -declare class EditorInstance { - [attr: string]: any; - _iframeWindow: XULWindow; - _item: ZoteroItem; - _initPromise: Promise; -} - -declare class Notes { - _editorInstances: EditorInstance[]; - registerEditorInstance: (instance: EditorInstance) => void; - // custom - _registerEditorInstance?: (instance: EditorInstance) => void; -} - -declare const ZoteroContextPane: { - [attr: string]: any; - getActiveEditor: () => EditorInstance; -}; - -declare class Annotation { - text: string; -} - -declare const Zotero_Tabs: { - _getTab(tabId: string); - jump(workspaceTabId: Number); - close(tabId: string); - select(tabId: string); - add(arg0: { - type: string; - title: any; - index: any; - data: object; - select: boolean; - onClose: Function; - }); - _tabs: Array; - selectedID: string; -}; - -declare const openWindowByType: ( - uri: string, - type: string, - features: string -) => Window; - -declare class Shortcut { - id: number; - func: any; - modifiers: string; - key: string; - keycode?: string; -} diff --git a/typing/xul.d.ts b/typing/xul.d.ts deleted file mode 100644 index 6a9bcba..0000000 --- a/typing/xul.d.ts +++ /dev/null @@ -1,89 +0,0 @@ -declare namespace XUL { - class Element extends HTMLElement { - public tagName?: string; - public hidden?: boolean; - public value?: string; - public width?: number; - public height?: number; - public disabled?: boolean; - public getAttribute?(name: string): string; - public setAttribute?(name: string, value: any): void; - } - - class Label extends Element { - public value: string; - } - - class Textbox extends XUL.Element { - public value?: string; - public readonly?: boolean; - } - - class Checkbox extends XUL.Element { - public checked: boolean; - } - - class Menuitem extends XUL.Element { - public value: string; - public label: string; - } - - class ProgressMeter extends XUL.Element { - public value: string | number; - } - - class Menupopup extends XUL.Element {} - - class Menulist extends XUL.Element { - public firstChild?: Menupopup | ChildNode; - public selectedItem?: Menuitem; - public value?: string; - public itemCount?: number; - public selectedIndex?: number; - public getItemAtIndex?: (i: number) => XUL.Menuitem; - } - - class ItemElement extends XUL.Element { - public item?: ZoteroItem; - } - - class Box extends XUL.Element { - public maxHeight?: number; - public minHeight?: number; - public maxWidth?: number; - public minWidth?: number; - } - - class Button extends XUL.Element { - public checked?: boolean; - public type?: string; - public tooltiptext?: string; - } - - class ListItem extends XUL.Element { - public selectedItem?: XUL.Element; - } -} - -declare class ClassList { - public add(classname: string): void; - public remove(classname: string): void; - public contains(classname: string): boolean; -} - -declare class XULEvent extends Event { - public target: XUL.Element; - clientX: number; - clientY: number; -} - -declare class XULWindow extends Window { - public document: XMLDocument; - public arguments: any; - public openDialog: ( - target: string, - type: string, - params: string, - extraParams?: object - ) => XULWindow; -}