add: support creating note from template in library and group
This commit is contained in:
parent
98fa74ed89
commit
ec01f1c51f
|
|
@ -30,7 +30,7 @@ import { showSyncDiff } from "./modules/sync/diffWindow";
|
|||
import { showSyncInfo } from "./modules/sync/infoWindow";
|
||||
import { showSyncManager } from "./modules/sync/managerWindow";
|
||||
import { showTemplateEditor } from "./modules/template/editorWindow";
|
||||
import { createNoteFromTemplate, createNoteFromMD } from "./modules/createNote";
|
||||
import { createNoteFromTemplate, createNoteFromMD, createNote } from "./modules/createNote";
|
||||
import { createZToolkit } from "./utils/ztoolkit";
|
||||
import { waitUtilAsync } from "./utils/wait";
|
||||
import { initSyncList } from "./modules/sync/api";
|
||||
|
|
@ -256,6 +256,8 @@ const onShowTemplateEditor = showTemplateEditor;
|
|||
|
||||
const onCreateNoteFromTemplate = createNoteFromTemplate;
|
||||
|
||||
const onCreateNote = createNote;
|
||||
|
||||
const onCreateNoteFromMD = createNoteFromMD;
|
||||
|
||||
const onShowUserGuide = showUserGuide;
|
||||
|
|
@ -285,6 +287,7 @@ export default {
|
|||
onShowTemplateEditor,
|
||||
onCreateNoteFromTemplate,
|
||||
onCreateNoteFromMD,
|
||||
onCreateNote,
|
||||
restoreNoteTabs,
|
||||
onShowUserGuide,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { getString } from "../utils/locale";
|
||||
import { formatPath } from "../utils/str";
|
||||
|
||||
export { createNoteFromTemplate, createNoteFromMD };
|
||||
export { createNoteFromTemplate, createNoteFromMD, createNote };
|
||||
|
||||
function getLibraryParentId() {
|
||||
return ZoteroPane.getSelectedItems().filter((item) => item.isRegularItem())[0]
|
||||
|
|
@ -46,9 +46,8 @@ async function createNoteFromTemplate(
|
|||
}
|
||||
|
||||
async function createNoteFromMD() {
|
||||
const currentCollection = ZoteroPane.getSelectedCollection();
|
||||
if (!currentCollection) {
|
||||
Zotero.getMainWindow().alert(getString("alert.notValidCollectionError"));
|
||||
// Check if we can create a note
|
||||
if (!(await createNote({ dryRun: true }))) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -67,7 +66,12 @@ async function createNoteFromMD() {
|
|||
}
|
||||
|
||||
for (const filepath of filepaths) {
|
||||
const noteItem = await addon.api.$import.fromMD(filepath, {
|
||||
const noteItem = await createNote();
|
||||
if (!noteItem) {
|
||||
continue;
|
||||
}
|
||||
await addon.api.$import.fromMD(filepath, {
|
||||
noteId: noteItem.id,
|
||||
ignoreVersion: true,
|
||||
});
|
||||
if (noteItem && syncNotes) {
|
||||
|
|
@ -83,3 +87,44 @@ async function createNoteFromMD() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function createNote(): Promise<Zotero.Item | false>;
|
||||
async function createNote(options: {
|
||||
dryRun: true;
|
||||
noSave?: boolean;
|
||||
}): Promise<boolean>;
|
||||
async function createNote(options: {
|
||||
dryRun?: false;
|
||||
noSave?: boolean;
|
||||
}): Promise<Zotero.Item | false>;
|
||||
async function createNote(
|
||||
options: { dryRun?: boolean; noSave?: boolean } = {},
|
||||
) {
|
||||
let noteItem: Zotero.Item;
|
||||
const ZoteroPane = Zotero.getActiveZoteroPane();
|
||||
|
||||
const cView = ZoteroPane.collectionsView;
|
||||
if (!cView) {
|
||||
Zotero.getMainWindow().alert(getString("alert.notValidCollectionError"));
|
||||
return false;
|
||||
}
|
||||
const cRow = cView.selectedTreeRow;
|
||||
if (["library", "group", "collection"].includes(cRow.type)) {
|
||||
if (options.dryRun) {
|
||||
return true;
|
||||
}
|
||||
noteItem = new Zotero.Item("note");
|
||||
noteItem.libraryID = ZoteroPane.getSelectedLibraryID();
|
||||
if (cRow.type === "collection") {
|
||||
noteItem.addToCollection(cRow.ref.id);
|
||||
}
|
||||
} else {
|
||||
Zotero.getMainWindow().alert(getString("alert.notValidCollectionError"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!options.noSave) {
|
||||
await noteItem.saveTx();
|
||||
}
|
||||
return noteItem;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,11 +41,13 @@ export async function fromMD(
|
|||
};
|
||||
|
||||
if (!noteItem) {
|
||||
noteItem = new Zotero.Item("note");
|
||||
noteItem.libraryID = ZoteroPane.getSelectedLibraryID();
|
||||
if (ZoteroPane.getCollectionTreeRow()?.isCollection()) {
|
||||
noteItem.addToCollection(ZoteroPane.getCollectionTreeRow()?.ref.id);
|
||||
const _noteItem = await addon.hooks.onCreateNote({
|
||||
noSave: true,
|
||||
});
|
||||
if (!_noteItem) {
|
||||
return;
|
||||
}
|
||||
noteItem = _noteItem;
|
||||
await noteItem.saveTx({
|
||||
notifierData: {
|
||||
autoSyncDelay: Zotero.Notes.AUTO_SYNC_DELAY,
|
||||
|
|
|
|||
|
|
@ -80,17 +80,11 @@ async function createTemplateNoteCallback(name: string) {
|
|||
ZoteroPane.getSelectedItems(true);
|
||||
switch (addon.data.template.picker.data.noteType) {
|
||||
case "standalone": {
|
||||
const currentCollection = ZoteroPane.getSelectedCollection();
|
||||
if (!currentCollection) {
|
||||
Zotero.getMainWindow().alert(
|
||||
getString("alert.notValidCollectionError"),
|
||||
);
|
||||
const noteItem = await addon.hooks.onCreateNote();
|
||||
if (!noteItem) {
|
||||
return;
|
||||
}
|
||||
const noteID = await ZoteroPane.newNote();
|
||||
const noteItem = Zotero.Items.get(noteID);
|
||||
await noteItem.saveTx();
|
||||
addon.data.template.picker.data.noteId = noteID;
|
||||
addon.data.template.picker.data.noteId = noteItem.id;
|
||||
break;
|
||||
}
|
||||
case "item": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue