Skip to content

Commit 1de6403

Browse files
author
xiangyu
committed
feat: quick citation in note editor
fix: create annotation note bug
1 parent 0b7615d commit 1de6403

File tree

3 files changed

+69
-62
lines changed

3 files changed

+69
-62
lines changed

src/events.ts

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class AddonEvents extends AddonBase {
254254

255255
message.content.editorInstance._knowledgeUIInitialized = false;
256256

257-
const noteItem = Zotero.Items.get(
257+
const noteItem: ZoteroItem = Zotero.Items.get(
258258
message.content.editorInstance._item.id
259259
);
260260
if (!noteItem.isNote()) {
@@ -269,15 +269,15 @@ class AddonEvents extends AddonBase {
269269
message.content.editorInstance,
270270
"knowledge-start",
271271
isMainKnowledge ? "isMainKnowledge" : "notMainKnowledge",
272-
isMainKnowledge ? "Edit the main Note in Workspace" : "Open Workspace",
272+
isMainKnowledge ? "Edit the Main Note in Workspace" : "Open Workspace",
273273
"openWorkspace",
274274
"start"
275275
);
276276
const addLinkDropDown: Element = await this._Addon.views.addEditorButton(
277277
message.content.editorInstance,
278278
"knowledge-addlink",
279279
"addToKnowledge",
280-
"Add link of current note to the main note",
280+
"Add Link of Current Note to Main Note",
281281
"addToKnowledge",
282282
"middle"
283283
);
@@ -309,6 +309,35 @@ class AddonEvents extends AddonBase {
309309
popup.remove();
310310
});
311311
});
312+
let topItem = noteItem.parentItem;
313+
while (topItem && !topItem.isRegularItem()) {
314+
topItem = topItem.parentItem;
315+
}
316+
if (topItem) {
317+
const addCitationButton: Element =
318+
await this._Addon.views.addEditorButton(
319+
message.content.editorInstance,
320+
"knowledge-addcitation",
321+
"addCitation",
322+
"Insert Note's Parent Citation",
323+
"addCitation",
324+
"middle"
325+
);
326+
addCitationButton.addEventListener("click", async (e) => {
327+
let format = Zotero.QuickCopy.getFormatFromURL(
328+
Zotero.QuickCopy.lastActiveURL
329+
);
330+
format = Zotero.QuickCopy.unserializeSetting(format);
331+
const cite = Zotero.QuickCopy.getContentFromItems(
332+
[topItem],
333+
format,
334+
null,
335+
0
336+
);
337+
this._Addon.knowledge.addLineToNote(noteItem, cite.html, 65535);
338+
});
339+
}
340+
312341
await this._Addon.views.addEditorButton(
313342
message.content.editorInstance,
314343
"knowledge-end",
@@ -594,7 +623,7 @@ class AddonEvents extends AddonBase {
594623
if (text.trim()) {
595624
if (this._Addon.knowledge.currentNodeID < 0) {
596625
// Add a new H1
597-
this._Addon.knowledge.addSubLineToNote(
626+
this._Addon.knowledge.addLineToNote(
598627
undefined,
599628
`<h1>${text}</h1>`,
600629
-1
@@ -605,7 +634,7 @@ class AddonEvents extends AddonBase {
605634
undefined,
606635
this._Addon.knowledge.currentNodeID
607636
);
608-
this._Addon.knowledge.addSubLineToNote(
637+
this._Addon.knowledge.addLineToNote(
609638
undefined,
610639
`<h${node.model.rank}>${text}</h${node.model.rank}>`,
611640
node.model.endIndex
@@ -809,8 +838,14 @@ class AddonEvents extends AddonBase {
809838
if (link) {
810839
const note = (await this._Addon.knowledge.getNoteFromLink(link)).item;
811840
if (note && note.id) {
812-
Zotero.debug(note);
813-
ZoteroPane.openNoteWindow(note.id);
841+
await this.onEditorEvent(
842+
new EditorMessage("onNoteLink", {
843+
params: {
844+
item: note,
845+
infoText: "OK",
846+
},
847+
})
848+
);
814849
return;
815850
}
816851
}
@@ -830,18 +865,10 @@ class AddonEvents extends AddonBase {
830865
note.addTag(tag.tag, tag.type);
831866
}
832867
await note.saveTx();
833-
let libraryID = note.libraryID;
834-
let library = Zotero.Libraries.get(libraryID);
835-
let groupID: string;
836-
if (library.libraryType === "user") {
837-
groupID = "u";
838-
} else if (library.libraryType === "group") {
839-
groupID = `${library.id}`;
840-
}
841-
let noteKey = note.key;
868+
842869
annotationItem.annotationComment = `${
843870
annotationItem.annotationComment ? annotationItem.annotationComment : ""
844-
}\nnote link: "zotero://note/${groupID}/${noteKey}/"`;
871+
}\nnote link: "${this._Addon.knowledge.getNoteLink(note)}"`;
845872
await annotationItem.saveTx();
846873
ZoteroPane.openNoteWindow(note.id);
847874
let t = 0;

src/knowledge.ts

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class Knowledge extends AddonBase {
261261
);
262262
}
263263

264-
private addLineToNote(
264+
private async addLineToNote(
265265
note: ZoteroItem,
266266
text: string,
267267
lineIndex: number,
@@ -272,6 +272,14 @@ class Knowledge extends AddonBase {
272272
return;
273273
}
274274
let noteLines = this.getLinesInNote(note);
275+
if (lineIndex < 0) {
276+
lineIndex =
277+
this.getWorkspaceNote().id === note.id && this.currentLine >= 0
278+
? this.currentLine
279+
: noteLines.length;
280+
} else if (lineIndex >= noteLines.length) {
281+
lineIndex = noteLines.length;
282+
}
275283
Zotero.debug(
276284
`insert to ${lineIndex}, it used to be ${noteLines[lineIndex]}`
277285
);
@@ -287,6 +295,7 @@ class Knowledge extends AddonBase {
287295
}
288296
noteLines.splice(lineIndex, 0, text);
289297
this.setLinesToNote(note, noteLines);
298+
await this.scrollWithRefresh(lineIndex);
290299
}
291300

292301
async addLinesToNote(
@@ -300,7 +309,12 @@ class Knowledge extends AddonBase {
300309
}
301310
let noteLines = this.getLinesInNote(note);
302311
if (lineIndex < 0) {
303-
lineIndex = 0;
312+
lineIndex =
313+
this.getWorkspaceNote().id === note.id && this.currentLine >= 0
314+
? this.currentLine
315+
: noteLines.length;
316+
} else if (lineIndex >= noteLines.length) {
317+
lineIndex = noteLines.length;
304318
}
305319
this.setLinesToNote(
306320
note,
@@ -309,41 +323,6 @@ class Knowledge extends AddonBase {
309323
await this.scrollWithRefresh(lineIndex);
310324
}
311325

312-
async addSubLineToNote(
313-
note: ZoteroItem,
314-
text: string,
315-
lineIndex: number = -1,
316-
newLine: boolean = false
317-
) {
318-
if (lineIndex < 0) {
319-
lineIndex =
320-
this.currentLine >= 0
321-
? this.currentLine
322-
: this.getLinesInNote(note).length;
323-
}
324-
// let parentNode = this.getLineParentInNote(note, lineIndex);
325-
// if (!parentNode) {
326-
// this.addLineToNote(note, text, lineIndex);
327-
// return;
328-
// }
329-
// let nodes = this.getNoteTreeAsList(note);
330-
// let i = 0;
331-
// for (let node of nodes) {
332-
// if (node.model.lineIndex === parentNode.model.lineIndex) {
333-
// break;
334-
// }
335-
// i++;
336-
// }
337-
// // Get next header line index
338-
// i++;
339-
// if (i >= nodes.length) {
340-
// i = nodes.length - 1;
341-
// }
342-
// Add to next line
343-
this.addLineToNote(note, text, lineIndex, newLine);
344-
await this.scrollWithRefresh(lineIndex);
345-
}
346-
347326
addLinkToNote(
348327
targetNote: ZoteroItem,
349328
lineIndex: number,
@@ -360,7 +339,7 @@ class Knowledge extends AddonBase {
360339
}
361340
const link = this.getNoteLink(linkedNote);
362341
const linkText = linkedNote.getNoteTitle().trim();
363-
this.addSubLineToNote(
342+
this.addLineToNote(
364343
targetNote,
365344
`<a href="${link}" rel="noopener noreferrer nofollow">${
366345
linkText ? linkText : `${link}`

0 commit comments

Comments
 (0)