add: note link actions #172

This commit is contained in:
xiangyu 2022-11-14 22:36:13 +08:00
parent 0ae6953dc9
commit 8c9c2a982d
5 changed files with 132 additions and 42 deletions

View File

@ -20,3 +20,5 @@ pref("extensions.zotero.Knowledge4Zotero.OCRMathpix.APIKey", "");
pref("extensions.zotero.Knowledge4Zotero.linkAction.click", "");
pref("extensions.zotero.Knowledge4Zotero.linkAction.shiftclick", "");
pref("extensions.zotero.Knowledge4Zotero.linkAction.ctrlclick", "");
pref("extensions.zotero.Knowledge4Zotero.linkAction.altclick", "");
pref("extensions.zotero.Knowledge4Zotero.linkAction.preview", true);

View File

@ -754,44 +754,44 @@ class EditorViews extends AddonBase {
".link-popup"
) as HTMLElement;
let previewContainer =
_window.document.getElementById("note-link-preview");
if (previewContainer) {
previewContainer.remove();
}
previewContainer = _window.document.createElementNS(
"http://www.w3.org/1999/xhtml",
"div"
);
previewContainer.id = "note-link-preview";
previewContainer.className = "ProseMirror primary-editor";
previewContainer.innerHTML =
await this._Addon.NoteParse.parseNoteStyleHTML(note);
previewContainer.addEventListener("click", (e) => {
this._Addon.WorkspaceWindow.setWorkspaceNote("preview", note);
});
if (linkPopup) {
linkPopup.append(
insertButton,
updateButton,
openInWindowButton,
previewContainer
);
previewContainer.setAttribute(
"style",
`width: 98%;height: ${
linkPopup ? Math.min(linkPopup.offsetTop, 300) : 300
}px;position: absolute;background: white;bottom: 36px;overflow: hidden;box-shadow: 0 0 5px 5px rgba(0,0,0,0.2);border-radius: 5px;cursor: pointer;opacity: 0.9;`
);
previewContainer
.querySelector("div[data-schema-version]")
.childNodes.forEach((node) => {
if ((node as Element).setAttribute) {
(node as Element).setAttribute("style", "margin: 0");
} else {
node.remove();
}
linkPopup.append(insertButton, updateButton, openInWindowButton);
if (
Zotero.Prefs.get("Knowledge4Zotero.linkAction.preview") as boolean
) {
let previewContainer =
_window.document.getElementById("note-link-preview");
if (previewContainer) {
previewContainer.remove();
}
previewContainer = _window.document.createElementNS(
"http://www.w3.org/1999/xhtml",
"div"
);
previewContainer.id = "note-link-preview";
previewContainer.className = "ProseMirror primary-editor";
previewContainer.innerHTML =
await this._Addon.NoteParse.parseNoteStyleHTML(note);
previewContainer.addEventListener("click", (e) => {
this._Addon.WorkspaceWindow.setWorkspaceNote("preview", note);
});
linkPopup.append(previewContainer);
previewContainer.setAttribute(
"style",
`width: 98%;height: ${
linkPopup ? Math.min(linkPopup.offsetTop, 300) : 300
}px;position: absolute;background: white;bottom: 36px;overflow: hidden;box-shadow: 0 0 5px 5px rgba(0,0,0,0.2);border-radius: 5px;cursor: pointer;opacity: 0.9;`
);
previewContainer
.querySelector("div[data-schema-version]")
.childNodes.forEach((node) => {
if ((node as Element).setAttribute) {
(node as Element).setAttribute("style", "margin: 0");
} else {
node.remove();
}
});
}
}
} else {
const insertLink = _window.document.querySelector("#insert-note-link");

View File

@ -57,8 +57,6 @@ class NoteExport extends AddonBase {
await Zotero.Notes.copyEmbeddedImages(subNote, newNote);
}
});
} else {
newNote = note;
}

View File

@ -619,18 +619,19 @@ class NoteUtils extends AddonBase {
const params = this._Addon.NoteParse.parseParamsFromLink(uri);
if (!params.libraryID) {
return {
item: false,
item: undefined,
args: {},
infoText: "Library does not exist or access denied.",
};
}
Zotero.debug(params);
let item = await Zotero.Items.getByLibraryAndKeyAsync(
let item: Zotero.Item = await Zotero.Items.getByLibraryAndKeyAsync(
params.libraryID,
params.noteKey
);
if (!item || !item.isNote()) {
return {
item: false,
item: undefined,
args: params,
infoText: "Note does not exist or is not a note.",
};
@ -721,7 +722,7 @@ class NoteUtils extends AddonBase {
!(
linkPopup &&
(linkPopup.querySelector("a") as unknown as HTMLLinkElement)
.href === link
?.href === link
) &&
t < 100
) {

View File

@ -324,6 +324,95 @@ class ZoteroEvents extends AddonBase {
await this._Addon.NoteUtils.onSelectionChange(instance);
}
);
instance._iframeWindow.document.addEventListener(
"click",
async (e) => {
if ((e.target as HTMLElement).tagName === "A") {
const link = (e.target as HTMLLinkElement).href;
const actions = {
// @ts-ignore
openLink: () => window.openURL(link),
openLinkIfNote: () => {
link.includes("zotero://note")
? actions.openLink()
: null;
},
openLinkIfSelect: () => {
link.includes("zotero://select")
? actions.openLink()
: null;
},
openLinkIfPDF: () => {
link.includes("zotero://open-pdf")
? actions.openLink()
: null;
},
openLinkInNewWindow: async () => {
if (link.includes("zotero://note")) {
ZoteroPane.openNoteWindow(
(
(await this._Addon.NoteUtils.getNoteFromLink(link))
.item as Zotero.Item
)?.id
);
} else {
actions.openLink();
}
},
copyLink: async () => {
new CopyHelper()
.addText(link, "text/unicode")
.addText(
(e.target as HTMLLinkElement).outerHTML,
"text/html"
)
.copy();
},
setMainNote: async () => {
const noteItem = (
await this._Addon.NoteUtils.getNoteFromLink(link)
).item as Zotero.Item;
if (!noteItem) {
return;
}
await this.onEditorEvent(
new EditorMessage("setMainNote", {
params: {
itemID: noteItem.id,
enableConfirm: false,
enableOpen: true,
},
})
);
},
};
const shiftAction = Zotero.Prefs.get(
"Knowledge4Zotero.linkAction.shiftclick"
) as string;
const ctrlAction = Zotero.Prefs.get(
"Knowledge4Zotero.linkAction.ctrlclick"
) as string;
const altAction = Zotero.Prefs.get(
"Knowledge4Zotero.linkAction.altclick"
) as string;
const clickAction = Zotero.Prefs.get(
"Knowledge4Zotero.linkAction.click"
) as string;
if (e.shiftKey && shiftAction) {
actions[shiftAction]();
} else if (e.ctrlKey && ctrlAction) {
actions[ctrlAction]();
} else if (e.altKey && altAction) {
actions[altAction]();
} else if (
clickAction &&
!(e.shiftKey || e.ctrlKey || e.altKey)
) {
actions[clickAction]();
}
}
}
);
instance._knowledgeSelectionInitialized = true;
}