From eba9a2c113e56d20a4450b00b043ddac6c503eef Mon Sep 17 00:00:00 2001 From: huaqo Date: Fri, 24 Apr 2026 19:31:38 +0200 Subject: [PATCH] feat: add secondary search parameter --- src/AnnotationModal.ts | 41 +++++----- src/CiteModal.ts | 17 ++--- src/OpenModal.ts | 11 ++- src/{settings.ts => SettingTab.ts} | 43 ++++++----- src/Settings.ts | 14 ++++ src/Utils.ts | 44 ++++++++--- src/ZoteroAnnotation.ts | 47 ------------ src/ZoteroClient.ts | 9 +-- src/ZoteroFields.ts | 30 ++++++++ src/ZoteroItem.ts | 117 +++++++++++++++++++++-------- src/ZoteroResponse.ts | 29 +++++-- src/main.ts | 13 ++-- 12 files changed, 253 insertions(+), 162 deletions(-) rename src/{settings.ts => SettingTab.ts} (59%) create mode 100644 src/Settings.ts delete mode 100644 src/ZoteroAnnotation.ts create mode 100644 src/ZoteroFields.ts diff --git a/src/AnnotationModal.ts b/src/AnnotationModal.ts index 4d686ba..c539dd5 100644 --- a/src/AnnotationModal.ts +++ b/src/AnnotationModal.ts @@ -1,27 +1,25 @@ import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian'; import { ZoteroClient } from "./ZoteroClient"; -import { ZoteroAnnotation } from "./ZoteroAnnotation"; import { loadAnnotations, renderZoteroAnnotation } from "./Utils" +import { Settings } from "./Settings"; const ZOTERO_BASE_URL = "zotero://select/library/items/"; -export class AnnotationModal extends FuzzySuggestModal { +export class AnnotationModal extends FuzzySuggestModal { private client: ZoteroClient; - private annotations: ZoteroAnnotation[] = []; - private createLink: boolean; - private onChoose: (annotations: string) => void; + private annotations: ZoteroItem[] = []; + private onChoose: (item: string) => void; + private settings: Settings; constructor( app: App, - port: string, - createLink: boolean, - style: string, - onChoose: (annotations: string) => void, + settings: Settings, + onChoose: (item: string) => void, ) { super(app); - this.client = new ZoteroClient(port, style); + this.settings = settings; + this.client = new ZoteroClient(this.settings.port, this.settings.style); this.setPlaceholder("Loading..."); - this.createLink = createLink this.onChoose = onChoose; } @@ -30,28 +28,27 @@ export class AnnotationModal extends FuzzySuggestModal { super.onOpen(); } - getItems(): ZoteroAnnotation[] { + getItems(): ZoteroItem[] { return this.annotations; } - - getItemText(annotation: ZoteroAnnotation): string { - return annotation.toString(); + getItemText(item: ZoteroItem): string { + return `${item.annotationText} ${item.parentItem[this.settings.search]}`; } - renderSuggestion(match: FuzzyMatch, el: HTMLElement) { - renderZoteroAnnotation(match, el); + renderSuggestion(match: FuzzyMatch, el: HTMLElement) { + renderZoteroAnnotation(match, el, this.settings.search); } - async onChooseItem(annotation: ZoteroAnnotation) { + async onChooseItem(item: ZoteroItem) { - let citation = annotation.parentItem.citation; + let citation = item.parentItem.citation ?? ""; - if (this.createLink) { - citation = `[${citation}](${ZOTERO_BASE_URL}${annotation.parentItemKey})`; + if (this.settings.link) { + citation = `[${citation}](${ZOTERO_BASE_URL}${item.parentItemKey})`; } - this.onChoose(`"${annotation.text}" ${citation}`); + this.onChoose(`"${item.annotationText}" ${citation}`); } } diff --git a/src/CiteModal.ts b/src/CiteModal.ts index 59a4ce4..406d700 100644 --- a/src/CiteModal.ts +++ b/src/CiteModal.ts @@ -2,6 +2,7 @@ import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian'; import { ZoteroClient } from "./ZoteroClient"; import { ZoteroItem } from "./ZoteroItem"; import { loadItems, renderZoteroItem } from "./Utils" +import { Settings } from "./Settings"; const ZOTERO_BASE_URL = "zotero://select/library/items/"; @@ -10,21 +11,19 @@ export class CiteModal extends FuzzySuggestModal { private onChoose: (reference: string) => void; private client: ZoteroClient; private items: ZoteroItem[] = []; - private createLink: boolean; + private settings: Settings; private mode: string; constructor( app: App, - port: string, - createLink: boolean, - style: string, + settings: Settings, mode: string, onChoose: (reference: string) => void, ) { super(app); this.setPlaceholder("Loading..."); - this.client = new ZoteroClient(port, style); - this.createLink = createLink; + this.settings = settings; + this.client = new ZoteroClient(this.settings.port, this.settings.style); this.mode = mode; this.onChoose = onChoose; } @@ -39,11 +38,11 @@ export class CiteModal extends FuzzySuggestModal { } getItemText(item: ZoteroItem): string { - return item.toString(); + return `${item.title} ${item[this.settings.search]}`; } renderSuggestion(match: FuzzyMatch, el: HTMLElement) { - renderZoteroItem(match, el); + renderZoteroItem(match, el, this.settings.search); } onChooseItem(item: ZoteroItem) { @@ -58,7 +57,7 @@ export class CiteModal extends FuzzySuggestModal { throw new Error(`Unknown mode: ${this.mode}`); } - if (!this.createLink) { + if (!this.settings.link) { this.onChoose(output); } else { this.onChoose(`[${output}](${ZOTERO_BASE_URL}${item.key})`); diff --git a/src/OpenModal.ts b/src/OpenModal.ts index 9aa7c66..2be1d4e 100644 --- a/src/OpenModal.ts +++ b/src/OpenModal.ts @@ -3,19 +3,22 @@ import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian'; import { ZoteroClient } from "./ZoteroClient"; import { ZoteroItem } from "./ZoteroItem"; import { loadItems, renderZoteroItem } from "./Utils" +import { Settings } from "./Settings"; const ZOTERO_BASE_URL = "zotero://select/library/items/"; export class OpenModal extends FuzzySuggestModal { private client: ZoteroClient; private items: ZoteroItem[] = []; + private settings: Settings constructor( app: App, - port: string, + settings: Settings, ) { super(app); - this.client = new ZoteroClient(port); + this.settings = settings; + this.client = new ZoteroClient(this.settings.port); this.setPlaceholder("Loading..."); } @@ -29,11 +32,11 @@ export class OpenModal extends FuzzySuggestModal { } getItemText(item: ZoteroItem): string { - return item.toString(); + return `${item.title} ${item[this.settings.search]}`; } renderSuggestion(match: FuzzyMatch, el: HTMLElement) { - renderZoteroItem(match, el); + renderZoteroItem(match, el, this.settings.search); } onChooseItem(item: ZoteroItem) { diff --git a/src/settings.ts b/src/SettingTab.ts similarity index 59% rename from src/settings.ts rename to src/SettingTab.ts index 0573099..01c205d 100644 --- a/src/settings.ts +++ b/src/SettingTab.ts @@ -1,16 +1,5 @@ import {App, PluginSettingTab, Setting } from "obsidian"; - -export interface Settings { - portSetting: string; - styleSetting: string; - linkSetting: boolean; -} - -export const DEFAULT_SETTINGS: Settings = { - portSetting: '23119', - styleSetting: 'apa', - linkSetting: true -} +import { ZoteroFields } from "./ZoteroFields"; export class SettingTab extends PluginSettingTab { plugin: Cite; @@ -30,9 +19,9 @@ export class SettingTab extends PluginSettingTab { .setDesc('http://localhost:/api/') .addText(text => text .setPlaceholder('Enter a Port') - .setValue(this.plugin.settings.portSetting) + .setValue(this.plugin.settings.port) .onChange(async (value) => { - this.plugin.settings.portSetting = value; + this.plugin.settings.port = value; await this.plugin.saveSettings(); })); @@ -41,9 +30,9 @@ export class SettingTab extends PluginSettingTab { .setDesc('https://www.zotero.org/styles?q=