From 2c8d571996b395e36863c96ae9127d91371c9402 Mon Sep 17 00:00:00 2001 From: Severin Gartland Date: Wed, 25 Jan 2023 12:27:17 +0100 Subject: [PATCH] Update file/folder from file-menu --- src/bbt/export.ts | 34 +++++++++++---- src/main.ts | 103 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 10 deletions(-) diff --git a/src/bbt/export.ts b/src/bbt/export.ts index f338206..b2698c2 100644 --- a/src/bbt/export.ts +++ b/src/bbt/export.ts @@ -467,32 +467,34 @@ export function getATemplatePath({ exportFormat }: ExportToMarkdownParams) { ); } -export async function exportToMarkdown( +export async function batchExportToMarkdown() { + doTheActualExport() +} + +export async function doTheActualExport( + citeKeys: string[], params: ExportToMarkdownParams, - importEvents?: Events + importEvents?: Events, ) { + const importDate = moment(); const { database, exportFormat, settings } = params; const sourcePath = getATemplatePath(params); const canExtract = doesEXEExist(); - const citeKeys: string[] = await getCiteKeys(database); - - if (!citeKeys.length) return false; - let itemData: any; try { itemData = await getItemJSONFromCiteKeys(citeKeys, database); } catch (e) { return false; } - // Variable to store the paths of the markdown files that will be created on import. // This is an array of an interface defined by a citekey and a path. // We first store the citekey in the order of the retrieved item data to save the order input by the user. // Further down below, when the Markdown file path has been sanitized, we associate the path to the key. const createdOrUpdatedMarkdownFiles: string[] = []; - + console.log(itemData) + if (!itemData) return false; // Zotero can't find the citekey for (let i = 0, len = itemData.length; i < len; i++) { await processItem(itemData[i], importDate, database, exportFormat.cslStyle); } @@ -739,6 +741,22 @@ export async function exportToMarkdown( return true; } +export async function exportToMarkdown( + params: ExportToMarkdownParams, + importEvents?: Events, +) { + const importDate = moment(); + const { database, exportFormat, settings } = params; + const sourcePath = getATemplatePath(params); + const canExtract = doesEXEExist(); + const citeKeys: string[] = await getCiteKeys(database); // I think this opens the zotero connector + if (!citeKeys.length) return false; + + doTheActualExport(citeKeys, params, importEvents) + + +} + export async function renderCiteTemplate(params: RenderCiteTemplateParams) { const importDate = moment(); const { database, format } = params; diff --git a/src/main.ts b/src/main.ts index 665bc63..0b14b80 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,10 @@ import './styles.css'; import './bbt/template.helpers'; -import { Plugin, TFile, Events, EventRef } from 'obsidian'; +import { Plugin, TFile, TFolder, Events, EventRef, Notice, FuzzySuggestModal, Modal, App, Setting } from 'obsidian'; import { getCAYW } from './bbt/cayw'; -import { exportToMarkdown, renderCiteTemplate } from './bbt/export'; +import { doTheActualExport, exportToMarkdown, renderCiteTemplate } from './bbt/export'; import { filesFromNotes, insertNotesIntoCurrentDoc, @@ -36,6 +36,58 @@ interface ViewEvents { fileUpdated: (file: TFile) => void; } +class FormatModal extends FuzzySuggestModal { + items: ExportFormat[] + constructor(app: any, items: ExportFormat[], callback: (item: ExportFormat) => void) { + super(app) + this.items = items + this.onChooseItem = callback + } + + getItems(): ExportFormat[] { + return this.items; + } + + getItemText(format: ExportFormat): string { + return format.name; + } +} + +export class UseSuggestedFormatModal extends Modal { + format: ExportFormat + useFormat: () => void + + constructor(app: App, format: ExportFormat, useFormat: (useFormat) => void) { + super(app); + this.format = format + this.useFormat = useFormat + } + + onOpen() { + let { contentEl } = this; + contentEl.createEl("h3", {text: "Do you want to use the detected format: "+this.format.name}) + + new Setting(contentEl).addButton((btn) => + btn.setButtonText("Yes").setCta().onClick(() => { + this.close(); + this.useFormat(true) + }) + ); + + new Setting(contentEl).addButton((btn) => + btn.setButtonText("No, selected other template").setCta().onClick(() => { + this.close(); + this.useFormat(false) + }) + ); + } + + onClose() { + let { contentEl } = this; + contentEl.empty(); + } +} + export default class ZoteroConnector extends Plugin { settings: ZoteroConnectorSettings; emitter: Emitter; @@ -58,6 +110,53 @@ export default class ZoteroConnector extends Plugin { this.addExportCommand(f); }); + this.registerEvent( + this.app.workspace.on("file-menu", (menu, selectedFile) => { + menu.addItem((item) => { + item.setTitle("Update from Zotero").setIcon("document").onClick(async () => { + // Function to export file + const exportFile = (file: any, format: ExportFormat) => { + doTheActualExport( + [file.basename], + { settings: this.settings, + database: this.settings.database, + exportFormat: format}, + this.importEvents) + } + + const exportFileOrFolder = (fileorfolder: any, format: ExportFormat) => { + if (selectedFile instanceof TFile) { + exportFile(selectedFile, format) + } else if (selectedFile instanceof TFolder) { + selectedFile.children.forEach(file => exportFile(file, format)) + } + } + + const selectFormatAndImport = () => { + new FormatModal(this.app, this.settings.exportFormats, (selectedFormat) => { + exportFileOrFolder(selectedFile, selectedFormat) + }).open() + } + const inCorrectPath = this.settings.exportFormats.filter(format => format.outputPathTemplate.contains(selectedFile.name)) + if(inCorrectPath.length == 1) { + new UseSuggestedFormatModal(this.app, inCorrectPath[0], (useFormat) => { + if(!useFormat) { + selectFormatAndImport() + } else { + exportFileOrFolder(selectedFile, inCorrectPath[0]) + } + }).open() + } else { + selectFormatAndImport() + } + + new Notice(selectedFile.path); + }); + }); + }) + ); + + // When an import is completed, proceed to open the crated or updated notes if the setting is enabled this.importEventsRef = this.importEvents.on("import-complete", (createdOrUpdatedMarkdownFilesPaths) => { if(this.settings.openNoteAfterImport) {