Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update file/folder from file-menu #169

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/bbt/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
103 changes: 101 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -36,6 +36,58 @@ interface ViewEvents {
fileUpdated: (file: TFile) => void;
}

class FormatModal extends FuzzySuggestModal<ExportFormat> {
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<ViewEvents>;
Expand All @@ -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) {
Expand Down