feat: insert linked note
This commit is contained in:
parent
7bde66723f
commit
c78a776e42
|
|
@ -1,3 +1,4 @@
|
|||
import { link } from "fs";
|
||||
import { AddonBase, EditorMessage } from "./base";
|
||||
|
||||
class AddonEvents extends AddonBase {
|
||||
|
|
@ -356,6 +357,38 @@ class AddonEvents extends AddonBase {
|
|||
header.innerHTML = "Main Note";
|
||||
header.setAttribute("style", "font-size: medium");
|
||||
middle.append(header);
|
||||
|
||||
// Link popup listener
|
||||
const container =
|
||||
_window.document.getElementsByClassName("relative-container")[0];
|
||||
const containerObserver = new MutationObserver(async (mutations) => {
|
||||
for (const mut of mutations) {
|
||||
for (const node of mut.addedNodes) {
|
||||
// wait for ui ready
|
||||
await Zotero.Promise.delay(20);
|
||||
const linkElement = (node as Element).getElementsByTagName(
|
||||
"a"
|
||||
)[0];
|
||||
if (!linkElement) {
|
||||
return;
|
||||
}
|
||||
const linkObserver = new MutationObserver(async (linkMuts) => {
|
||||
this._Addon.views.updateEditorPopupButtons(
|
||||
_window,
|
||||
linkElement.getAttribute("href")
|
||||
);
|
||||
});
|
||||
linkObserver.observe(linkElement, { attributes: true });
|
||||
this._Addon.views.updateEditorPopupButtons(
|
||||
_window,
|
||||
linkElement.getAttribute("href")
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
containerObserver.observe(container, {
|
||||
childList: true,
|
||||
});
|
||||
} else {
|
||||
// This is a preview knowledge, hide openWorkspace button add show close botton
|
||||
this._Addon.views.changeEditorButtonView(
|
||||
|
|
@ -722,8 +755,7 @@ class AddonEvents extends AddonBase {
|
|||
|
||||
if (annotationItem.annotationComment) {
|
||||
const text = annotationItem.annotationComment;
|
||||
let link = text.substring(text.search(/zotero:\/\/note\//g));
|
||||
link = link.substring(0, link.search('"'));
|
||||
let link = this._Addon.knowledge.getLinkFromText(text);
|
||||
|
||||
if (link) {
|
||||
const note = (await this._Addon.knowledge.getNoteFromLink(link)).item;
|
||||
|
|
|
|||
|
|
@ -289,6 +289,26 @@ class Knowledge extends AddonBase {
|
|||
this.setLinesToNote(note, noteLines);
|
||||
}
|
||||
|
||||
async addLinesToNote(
|
||||
note: ZoteroItem,
|
||||
newLines: string[],
|
||||
lineIndex: number
|
||||
) {
|
||||
note = note || this.getWorkspaceNote();
|
||||
if (!note) {
|
||||
return;
|
||||
}
|
||||
let noteLines = this.getLinesInNote(note);
|
||||
if (lineIndex < 0) {
|
||||
lineIndex = 0;
|
||||
}
|
||||
this.setLinesToNote(
|
||||
note,
|
||||
noteLines.slice(0, lineIndex).concat(newLines, noteLines.slice(lineIndex))
|
||||
);
|
||||
await this.scrollWithRefresh(lineIndex);
|
||||
}
|
||||
|
||||
async addSubLineToNote(
|
||||
note: ZoteroItem,
|
||||
text: string,
|
||||
|
|
@ -602,7 +622,7 @@ class Knowledge extends AddonBase {
|
|||
const item = Zotero.Items.get(noteID);
|
||||
const rootNoteIds = [note.id];
|
||||
|
||||
const newLines = await this.converNoteLines(
|
||||
const newLines = await this.convertNoteLines(
|
||||
note,
|
||||
rootNoteIds,
|
||||
convertNoteLinks,
|
||||
|
|
@ -675,7 +695,7 @@ class Knowledge extends AddonBase {
|
|||
return false;
|
||||
}
|
||||
|
||||
async converNoteLines(
|
||||
async convertNoteLines(
|
||||
currentNote: ZoteroItem,
|
||||
rootNoteIds: number[],
|
||||
convertNoteLinks: boolean = true,
|
||||
|
|
@ -701,11 +721,9 @@ class Knowledge extends AddonBase {
|
|||
}
|
||||
newLines.push(noteLines[i]);
|
||||
// Convert Link
|
||||
let linkIndex = noteLines[i].search(/zotero:\/\/note\//g);
|
||||
while (linkIndex >= 0) {
|
||||
let link = this.getLinkFromText(noteLines[i]);
|
||||
while (link) {
|
||||
Zotero.debug("convert link");
|
||||
let link = noteLines[i].substring(linkIndex);
|
||||
link = link.substring(0, link.search('"'));
|
||||
let res = await this.getNoteFromLink(link);
|
||||
const subNote = res.item;
|
||||
if (subNote && _rootNoteIds.indexOf(subNote.id) === -1) {
|
||||
|
|
@ -713,7 +731,7 @@ class Knowledge extends AddonBase {
|
|||
newLines.push("<blockquote>");
|
||||
newLines.push(`<p><strong>Linked Note:</strong></p>`);
|
||||
newLines = newLines.concat(
|
||||
await this.converNoteLines(
|
||||
await this.convertNoteLines(
|
||||
subNote,
|
||||
_rootNoteIds,
|
||||
convertNoteLinks,
|
||||
|
|
@ -728,12 +746,23 @@ class Knowledge extends AddonBase {
|
|||
noteLines[i] = noteLines[i].substring(
|
||||
noteLines[i].search(/<\/a>/g) + "</a>".length
|
||||
);
|
||||
linkIndex = noteLines[i].search(/zotero:\/\/note\//g);
|
||||
link = this.getLinkFromText(noteLines[i]);
|
||||
}
|
||||
}
|
||||
return newLines;
|
||||
}
|
||||
|
||||
getLinkFromText(text: string) {
|
||||
// Must end with "
|
||||
const linkIndex = text.search(/zotero:\/\/note\//g);
|
||||
if (linkIndex === -1) {
|
||||
return "";
|
||||
}
|
||||
let link = text.substring(linkIndex);
|
||||
link = link.substring(0, link.search('"'));
|
||||
return link;
|
||||
}
|
||||
|
||||
async getNoteFromLink(uri: string) {
|
||||
let [groupID, noteKey] = uri.substring("zotero://note/".length).split("/");
|
||||
|
||||
|
|
|
|||
36
src/views.ts
36
src/views.ts
|
|
@ -282,6 +282,42 @@ class AddonViews extends AddonBase {
|
|||
.children[0].before(treeRow);
|
||||
}
|
||||
|
||||
async updateEditorPopupButtons(_window: Window, link: string) {
|
||||
const note: ZoteroItem = (await this._Addon.knowledge.getNoteFromLink(link))
|
||||
.item;
|
||||
if (note) {
|
||||
let insertButton = _window.document.getElementById("insert-note-link");
|
||||
if (insertButton) {
|
||||
insertButton.remove();
|
||||
}
|
||||
insertButton = _window.document.createElement("button");
|
||||
insertButton.setAttribute("id", "insert-note-link");
|
||||
insertButton.setAttribute(
|
||||
"title",
|
||||
`Import Linked Note: ${note.getNoteTitle()}`
|
||||
);
|
||||
insertButton.innerHTML = `<svg t="1652008007954" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10521" width="16" height="16"><path d="M574.3 896H159.7c-17.6 0-31.9-14.3-31.9-32V160c0-17.7 14.3-32 31.9-32h382.7v160c0 35.3 28.6 64 63.8 64h159.5v192c0 17.7 14.3 32 31.9 32 17.6 0 31.9-14.3 31.9-32V270.2c0-8.5-3.3-16.6-9.3-22.6L647.4 73.4c-6-6-14.1-9.4-22.6-9.4h-497C92.6 64 64 92.7 64 128v768c0 35.3 28.6 64 63.8 64h446.5c17.6 0 31.9-14.3 31.9-32s-14.3-32-31.9-32zM638.1 288c-17.6 0-31.9-14.3-31.9-32V128l159.5 160H638.1z" p-id="10522"></path><path d="M418.8 673H225.5c-17.6 0-31.9 14.3-31.9 32s14.3 32 31.9 32h193.3c17.6 0 31.9-14.3 31.9-32s-14.3-32-31.9-32zM608.2 481H225.5c-17.6 0-31.9 14.3-31.9 32s14.3 32 31.9 32h382.7c17.6 0 31.9-14.3 31.9-32s-14.3-32-31.9-32zM225.5 353h191.4c17.6 0 31.9-14.3 31.9-32s-14.3-32-31.9-32H225.5c-17.6 0-31.9 14.3-31.9 32s14.3 32 31.9 32zM862.7 959.4c-23.6 0-47-8.8-64.8-26.6l-24.4-24.4c-12.5-12.5-12.5-32.8 0-45.3s32.7-12.5 45.1 0l24.4 24.4c11.3 11.4 30.7 10.4 43.2-2.1 12.5-12.5 13.4-31.9 2.1-43.3L749.2 702.6c-11.3-11.4-30.7-10.4-43.2 2.1-6.2 6.3-9.8 14.4-10 22.8-0.2 7.9 2.6 15.1 7.9 20.4 12.5 12.5 12.5 32.8 0 45.3s-32.7 12.5-45.1 0c-36.2-36.3-35.2-96.3 2.1-133.8 37.4-37.5 97.2-38.4 133.4-2.1l139.1 139.5c36.2 36.3 35.2 96.3-2.1 133.8-19 19.2-43.9 28.8-68.6 28.8z" p-id="10523"></path><path d="M696.3 883.1c-23.6 0-47-8.8-64.8-26.6l-139-139.6c-17.7-17.8-27.2-41.7-26.6-67.2 0.6-25 10.8-48.6 28.7-66.6 17.9-17.9 41.5-28.2 66.4-28.8 25.5-0.6 49.3 8.9 67 26.6l24.4 24.4c12.5 12.5 12.5 32.8 0 45.3s-32.7 12.5-45.1 0l-24.4-24.4c-5.3-5.3-12.5-8.1-20.4-7.9-8.4 0.2-16.5 3.8-22.8 10-6.2 6.3-9.8 14.4-10 22.8-0.2 7.9 2.6 15.1 7.9 20.4L676.7 811c11.3 11.4 30.7 10.4 43.2-2.1 12.5-12.5 13.4-31.9 2.1-43.3-12.5-12.5-12.5-32.8 0-45.3s32.7-12.5 45.1 0c36.2 36.3 35.3 96.3-2.1 133.8-19.1 19.3-44 29-68.7 29z" p-id="10524"></path></svg>`;
|
||||
insertButton.addEventListener("click", async (e) => {
|
||||
let newLines = [];
|
||||
newLines.push("<blockquote>");
|
||||
newLines.push(`<p><strong>Linked Note:</strong></p>`);
|
||||
newLines = newLines.concat(
|
||||
await this._Addon.knowledge.convertNoteLines(note, [], true, false)
|
||||
);
|
||||
newLines.push("</blockquote>");
|
||||
Zotero.debug(newLines);
|
||||
await this._Addon.knowledge.addLinesToNote(
|
||||
undefined,
|
||||
newLines,
|
||||
this._Addon.knowledge.currentLine + 1
|
||||
);
|
||||
});
|
||||
_window.document
|
||||
.getElementsByClassName("link-popup")[0]
|
||||
.appendChild(insertButton);
|
||||
}
|
||||
}
|
||||
|
||||
async addReaderAnnotationButton(reader: ReaderObj) {
|
||||
if (!reader) {
|
||||
return false;
|
||||
|
|
|
|||
Loading…
Reference in New Issue