fix: note editor selection trigger bug

refactor: remove some logs
This commit is contained in:
xiangyu 2023-01-04 18:43:20 +08:00
parent fc391f9943
commit b27e14786d
12 changed files with 49 additions and 119 deletions

View File

@ -37,7 +37,7 @@ class EditorController extends AddonBase {
_window.document.head.append(messageScript);
}
_window.addEventListener("BNMessage", (e: CustomEvent) => {
this._Addon.toolkit.Tool.log("BN: note editor event", e.detail);
this._Addon.toolkit.Tool.log("note editor event", e.detail);
switch (e.detail.type) {
case "exportPDFDone":
this._Addon.NoteExport._pdfPrintPromise.resolve();

View File

@ -200,14 +200,12 @@ class EditorViews extends AddonBase {
addLinkDropDown.addEventListener("click", leaveAction);
// Observe the popup remove triggered by button click
const ob = new MutationObserver((e) => {
this._Addon.toolkit.Tool.log(e);
if (e[0].removedNodes) {
leaveAction();
}
});
ob.observe(addLinkDropDown, { childList: true });
const keyAction = (e: KeyboardEvent) => {
this._Addon.toolkit.Tool.log(e);
if (e.key === "Control") {
hintWindow?.close();
e.preventDefault();
@ -629,7 +627,6 @@ class EditorViews extends AddonBase {
});
}
parentDropDown.append(popup);
this._Addon.toolkit.Tool.log(popup.offsetWidth);
let style: string = "";
if (align === "center") {
style = `right: -${popup.offsetWidth / 2 - 15}px;`;
@ -715,14 +712,12 @@ class EditorViews extends AddonBase {
await this._Addon.NoteUtils.modifyLineInNote(
targetNote,
(oldLine: string) => {
this._Addon.toolkit.Tool.log(oldLine);
const params = this._Addon.NoteParse.parseParamsFromLink(link);
const newLink = !params.ignore
? link + (link.includes("?") ? "&ignore=1" : "?ignore=1")
: link;
const linkIndex =
this._Addon.NoteParse.parseLinkIndexInText(oldLine);
this._Addon.toolkit.Tool.log(linkIndex);
return `${oldLine.slice(0, linkIndex[0])}${newLink}${oldLine.slice(
linkIndex[1]
)}\n${newLineString}`;
@ -768,10 +763,9 @@ class EditorViews extends AddonBase {
);
updateButton.innerHTML = this.icons["updateLinkText"];
updateButton.addEventListener("click", async (e) => {
this._Addon.toolkit.Tool.log("ZBN: Update Link Text");
const noteLines = this._Addon.NoteUtils.getLinesInNote(targetNote);
let line = noteLines[this._Addon.NoteUtils.currentLine[targetNote.id]];
this._Addon.toolkit.Tool.log(line);
this._Addon.toolkit.Tool.log("Update Link Text", line);
let linkStart = line.search(/<a /g);
let linkEnd = line.search(/<\/a>/g) + 4;
@ -833,7 +827,7 @@ class EditorViews extends AddonBase {
openInWindowButton.setAttribute("title", "Open In New Window");
openInWindowButton.innerHTML = this.icons["openInNewWindow"];
openInWindowButton.addEventListener("click", async (e) => {
this._Addon.toolkit.Tool.log("ZBN: Open link in new window");
this._Addon.toolkit.Tool.log("Open link in new window");
ZoteroPane.openNoteWindow(note.id);
});
@ -1185,7 +1179,6 @@ class EditorViews extends AddonBase {
eleList.push(ele);
}
}
this._Addon.toolkit.Tool.log(eleList, lineIndex);
if (lineIndex >= eleList.length) {
lineIndex = eleList.length - 1;
} else if (lineIndex < 0) {

View File

@ -52,7 +52,7 @@ class NoteExport extends AddonBase {
.filter((k) => k.includes("export"))
.find((k) => options[k])
) {
this._Addon.toolkit.Tool.log("[BN] options containing 'export' all false");
this._Addon.toolkit.Tool.log("options containing 'export' all false");
return;
}
this._exportFileInfo = [];
@ -70,7 +70,6 @@ class NoteExport extends AddonBase {
);
await this._Addon.NoteUtils.setLinesToNote(newNote, convertResult.lines);
this._Addon.toolkit.Tool.log(convertResult.subNotes);
await Zotero.DB.executeTransaction(async () => {
await Zotero.Notes.copyEmbeddedImages(note, newNote);
@ -107,7 +106,6 @@ class NoteExport extends AddonBase {
this._docxPromise = Zotero.Promise.defer();
instance._iframeWindow.postMessage({ type: "exportDocx" }, "*");
await this._docxPromise.promise;
this._Addon.toolkit.Tool.log(this._docxBlob);
const filename = await this._Addon.toolkit.Tool.openFilePicker(
`${Zotero.getString("fileInterface.export")} MS Word Document`,
"save",
@ -126,7 +124,6 @@ class NoteExport extends AddonBase {
do {
ZoteroPane.openNoteWindow(newNote.id);
_w = ZoteroPane.findNoteWindow(newNote.id);
this._Addon.toolkit.Tool.log(_w);
await Zotero.Promise.delay(10);
t += 1;
} while (!_w && t < 500);
@ -138,7 +135,10 @@ class NoteExport extends AddonBase {
!(
editor.getCurrentInstance &&
editor.getCurrentInstance() &&
editor.getCurrentInstance()._knowledgeSelectionInitialized
editor
.getCurrentInstance()
._iframeWindow.document.body.getAttribute("betternotes-status") !==
"initialized"
) &&
t < 500
) {
@ -205,7 +205,7 @@ class NoteExport extends AddonBase {
filedir = Zotero.File.normalizeToUnix(filedir);
if (!filedir) {
this._Addon.toolkit.Tool.log("BN:export, filepath invalid");
this._Addon.toolkit.Tool.log("export, filepath invalid");
return;
}
@ -233,7 +233,6 @@ class NoteExport extends AddonBase {
newNote,
convertResult.lines
);
this._Addon.toolkit.Tool.log(convertResult.subNotes);
await Zotero.DB.executeTransaction(async () => {
await Zotero.Notes.copyEmbeddedImages(note, newNote);

View File

@ -98,7 +98,6 @@ class NoteExportWindow extends AddonBase {
};
this.options.forEach(saveOptions);
this._Addon.toolkit.Tool.log(this.io);
this._Addon.toolkit.Tool.log(this.io.dataOut);
}
}

View File

@ -39,7 +39,7 @@ class NoteImport extends AddonBase {
try {
mdStatus = await this._Addon.SyncUtils.getMDStatus(file);
} catch (e) {
this._Addon.toolkit.Tool.log(`BN Import: ${String(e)}`);
this._Addon.toolkit.Tool.log(`Import: ${String(e)}`);
}
if (!options.ignoreVersion && mdStatus.meta?.version < noteItem?._version) {
if (
@ -75,7 +75,7 @@ class NoteImport extends AddonBase {
noteItem,
true
);
this._Addon.toolkit.Tool.log("bn import", noteStatus);
this._Addon.toolkit.Tool.log("import", noteStatus);
if (options.append) {
await this._Addon.NoteUtils.addLineToNote(

View File

@ -211,7 +211,6 @@ class NoteParse extends AddonBase {
if (currentLineIndex <= lineIndex) {
currentLineIndex += 1;
currentElement = _e;
// this._Addon.toolkit.Tool.log(currentLineIndex, _e);
}
}
} else {
@ -221,7 +220,6 @@ class NoteParse extends AddonBase {
} else {
currentLineIndex += 1;
currentElement = e as HTMLElement;
// this._Addon.toolkit.Tool.log(currentLineIndex, e);
}
}
this._Addon.toolkit.Tool.log(currentLineIndex);
@ -431,7 +429,10 @@ class NoteParse extends AddonBase {
}
annotationJSONList.push(annotJson);
}
await this._Addon.NoteUtils.importAnnotationImagesToNote(note, annotationJSONList);
await this._Addon.NoteUtils.importAnnotationImagesToNote(
note,
annotationJSONList
);
const html = this.serializeAnnotations(
annotationJSONList,
false,
@ -483,7 +484,6 @@ class NoteParse extends AddonBase {
)
.slice(0, lineCount)
.join("\n")}</div>`;
this._Addon.toolkit.Tool.log(this.parseHTMLLines(item.getNote()).slice(0, lineCount));
const parser = this._Addon.toolkit.Compat.getDOMParser();
let doc = parser.parseFromString(note, "text/html");
@ -781,24 +781,6 @@ class NoteParse extends AddonBase {
noteItem: Zotero.Item,
isImport: boolean = false
) {
// let editorInstance =
// this._Addon.WorkspaceWindow.getEditorInstance(noteItem);
// if (!editorInstance) {
// ZoteroPane.openNoteWindow(noteItem.id);
// editorInstance = this._Addon.WorkspaceWindow.getEditorInstance(noteItem);
// let t = 0;
// // Wait for editor instance
// while (t < 10 && !editorInstance) {
// await Zotero.Promise.delay(500);
// t += 1;
// editorInstance =
// this._Addon.WorkspaceWindow.getEditorInstance(noteItem);
// }
// }
// if (!editorInstance) {
// this._Addon.toolkit.Tool.log("BN:Import: failed to open note.");
// return;
// }
this._Addon.toolkit.Tool.log("md", mdStatus);
const remark = this._Addon.SyncUtils.md2remark(mdStatus.content);
this._Addon.toolkit.Tool.log("remark", remark);
@ -808,51 +790,7 @@ class NoteParse extends AddonBase {
this._Addon.toolkit.Tool.log("_note", _note);
const rehype = this._Addon.SyncUtils.note2rehype(_note);
this._Addon.toolkit.Tool.log("rehype", rehype);
// Import highlight to note meta
// Annotations don't need to be processed.
// Image annotations are imported with normal images.
// const annotationNodes = getM2NRehypeAnnotationNodes(mdRehype);
// for (const node of annotationNodes) {
// try {
// // {
// // "attachmentURI": "http://zotero.org/users/uid/items/itemkey",
// // "annotationKey": "4FLVQRDG",
// // "color": "#5fb236",
// // "pageLabel": "2503",
// // "position": {
// // "pageIndex": 0,
// // "rects": [
// // [
// // 101.716,
// // 298.162,
// // 135.469,
// // 307.069
// // ]
// // ]
// // },
// // "citationItem": {
// // "uris": [
// // "http://zotero.org/users/uid/items/itemkey"
// // ],
// // "locator": "2503"
// // }
// // }
// const dataAnnotation = JSON.parse(
// decodeURIComponent(node.properties.dataAnnotation)
// );
// const id = dataAnnotation.citationItems.map((c) =>
// Zotero.URI.getURIItemID(dataAnnotation.attachmentURI)
// );
// const html = await this.parseAnnotationHTML(noteItem, []);
// const newNode = note2rehype(html);
// // root -> p -> span(cite, this is what we actually want)
// replace(node, (newNode.children[0] as any).children[0]);
// } catch (e) {
// this._Addon.toolkit.Tool.log(e);
// this._Addon.toolkit.Tool.log(e);
// continue;
// }
// }
// Check if image already belongs to note
this._Addon.SyncUtils.processM2NRehypeMetaImageNodes(
this._Addon.SyncUtils.getM2NRehypeImageNodes(rehype)

View File

@ -588,9 +588,6 @@ class NoteUtils extends AddonBase {
toID,
tree
);
this._Addon.toolkit.Tool.log(fromNode.model);
this._Addon.toolkit.Tool.log(toNode.model);
this._Addon.toolkit.Tool.log(moveType);
this._Addon.toolkit.Tool.log(toNode.model, fromNode.model, moveType);
this.moveHeaderLineInNote(
this._Addon.WorkspaceWindow.getWorkspaceNote(),
@ -775,10 +772,7 @@ class NoteUtils extends AddonBase {
}
}
}
this._Addon.toolkit.Tool.log(
`Knowledge4Zotero: line ${currentLineIndex} selected.`
);
this._Addon.toolkit.Tool.log(currentLineIndex);
this._Addon.toolkit.Tool.log(`line ${currentLineIndex} selected.`);
// Zotero.debug(
// `Current Element: ${focusNode.outerHTML}; Real Element: ${realElement.outerHTML}`
// );

View File

@ -272,7 +272,6 @@ class SyncController extends AddonBase {
progress.startCloseTimer(5000);
}
} catch (e) {
this._Addon.toolkit.Tool.log(e);
this._Addon.toolkit.Tool.log(e);
this._Addon.ZoteroViews.showProgressWindow(
"[Syncing] Better Notes",

View File

@ -134,11 +134,10 @@ class SyncDiffWindow extends AddonBase {
this._window.closed || this._window.close();
break;
case "unsync":
this._Addon.toolkit.Tool.log("remove synce" + noteItem.getNote());
this._Addon.toolkit.Tool.log("remove synce", noteItem.getNote());
await this._Addon.SyncController.removeSyncNote(noteItem);
break;
case "finish":
this._Addon.toolkit.Tool.log("Diff result:" + io.result);
this._Addon.toolkit.Tool.log("Diff result:", io.result);
// return io.result;
noteItem.setNote(noteStatus.meta + io.result + noteStatus.tail);

View File

@ -130,7 +130,6 @@ class SyncUtils extends AddonBase {
ret.meta = YAML.parse(yaml);
} catch (e) {
this._Addon.toolkit.Tool.log(e);
this._Addon.toolkit.Tool.log(e);
}
}
return ret;
@ -168,7 +167,6 @@ class SyncUtils extends AddonBase {
}
} catch (e) {
this._Addon.toolkit.Tool.log(e);
this._Addon.toolkit.Tool.log(e);
}
return ret;
}
@ -636,7 +634,7 @@ class SyncUtils extends AddonBase {
/zotero:\/\/note\/\w+\/\w+\//.test(node.properties?.href),
(node) => nodes.push(node)
);
this._Addon.toolkit.Tool.log("BN:N2M link");
this._Addon.toolkit.Tool.log("N2M link");
this._Addon.toolkit.Tool.log(JSON.stringify(nodes));
return new Array(...new Set(nodes));
}
@ -1050,7 +1048,6 @@ class SyncUtils extends AddonBase {
// root -> p -> span(cite, this is what we actually want)
this.replace(node, (newNode.children[0] as any).children[0]);
} catch (e) {
this._Addon.toolkit.Tool.log(e);
this._Addon.toolkit.Tool.log(e);
continue;
}
@ -1105,7 +1102,7 @@ class SyncUtils extends AddonBase {
if (!(await OS.File.exists(src))) {
src = OS.Path.join(fileDir, src);
if (!(await OS.File.exists(src))) {
this._Addon.toolkit.Tool.log("BN:parse image, path invalid");
this._Addon.toolkit.Tool.log("parse image, path invalid");
continue;
}
}

View File

@ -175,8 +175,7 @@ class WorkspaceWindow extends AddonBase {
}
private async messageHandler(e) {
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: view message ${e.data.type}`);
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: view message ${e.data.type}`);
this._Addon.toolkit.Tool.log(`view message ${e.data.type}`);
if (e.data.type === "ready") {
this._initIframe.resolve();
} else if (e.data.type === "getMindMapData") {

View File

@ -21,7 +21,7 @@ class ZoteroEvents extends AddonBase {
this._Addon.toolkit.Tool.logOptionsGlobal.prefix = `[${addonName}]`;
this._Addon.toolkit.Tool.logOptionsGlobal.disableConsole =
this._Addon.env === "production";
this._Addon.toolkit.Tool.log("init called")
this._Addon.toolkit.Tool.log("init called");
this.initProxyHandler();
this.addEditorInstanceListener();
@ -121,22 +121,26 @@ class ZoteroEvents extends AddonBase {
await instance._initPromise;
instance._knowledgeUIInitialized = false;
const noteItem = instance._item;
// item.getNote may not be initialized yet
if (Zotero.ItemTypes.getID("note") !== noteItem.itemTypeID) {
if (Zotero.ItemTypes.getID("note") !== instance._item.itemTypeID) {
return;
}
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: note editor initializing...`);
this._Addon.toolkit.Tool.log("note editor initializing...");
await this._Addon.EditorViews.initEditor(instance);
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: note editor initialized.`);
this._Addon.toolkit.Tool.log("note editor initialized.");
if (!instance._knowledgeSelectionInitialized) {
if (
instance._iframeWindow.document.body.getAttribute(
"betternotes-status"
) !== "initialized"
) {
// Put event listeners here to access Zotero instance
instance._iframeWindow.document.addEventListener(
"selectionchange",
async (e) => {
e.stopPropagation();
e.preventDefault();
await this._Addon.NoteUtils.onSelectionChange(instance);
}
);
@ -244,7 +248,10 @@ class ZoteroEvents extends AddonBase {
openPreview(e);
}
});
instance._knowledgeSelectionInitialized = true;
instance._iframeWindow.document.body.setAttribute(
"betternotes-status",
"initialized"
);
}
instance._popup.setAttribute(
@ -285,7 +292,9 @@ class ZoteroEvents extends AddonBase {
}
public async onEditorEvent(message: EditorMessage) {
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: onEditorEvent\n${message.type}`);
this._Addon.toolkit.Tool.log(
`Knowledge4Zotero: onEditorEvent\n${message.type}`
);
const mainNote = this._Addon.WorkspaceWindow.getWorkspaceNote();
if (message.type === "openUserGuide") {
/*
@ -399,7 +408,7 @@ class ZoteroEvents extends AddonBase {
params: {itemID, enableConfirm, enableOpen}
}
*/
this._Addon.toolkit.Tool.log("setMainNote")
this._Addon.toolkit.Tool.log("setMainNote");
let mainKnowledgeID = parseInt(
Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") as string
);
@ -497,7 +506,7 @@ class ZoteroEvents extends AddonBase {
editorInstance
}
*/
this._Addon.toolkit.Tool.log("addToNoteEnd")
this._Addon.toolkit.Tool.log("addToNoteEnd");
await this._Addon.NoteUtils.addLinkToNote(
mainNote,
(message.content.editorInstance as Zotero.EditorInstance)._item,
@ -517,7 +526,7 @@ class ZoteroEvents extends AddonBase {
}
}
*/
this._Addon.toolkit.Tool.log("addToNote")
this._Addon.toolkit.Tool.log("addToNote");
let lineIndex = message.content.params?.lineIndex;
if (typeof lineIndex === "undefined") {
const eventInfo = (message.content.event as XUL.XULEvent).target.id;
@ -581,9 +590,13 @@ class ZoteroEvents extends AddonBase {
const forceStandalone = message.content.params.forceStandalone;
let _window = this._Addon.WorkspaceWindow.getWorkspaceWindow();
if (!noteItem) {
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: ${message.content.params.infoText}`);
this._Addon.toolkit.Tool.log(
`Knowledge4Zotero: ${message.content.params.infoText}`
);
}
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: onNoteLink ${noteItem.id}`);
this._Addon.toolkit.Tool.log(
`Knowledge4Zotero: onNoteLink ${noteItem.id}`
);
if (
!forceStandalone &&
_window &&
@ -968,7 +981,7 @@ class ZoteroEvents extends AddonBase {
}
Zotero.Notifier.trigger("open", "file", attachment.id);
} catch (e) {
this._Addon.toolkit.Tool.log("Open attachment failed:")
this._Addon.toolkit.Tool.log("Open attachment failed:");
this._Addon.toolkit.Tool.log(attachment);
this._Addon.ZoteroViews.showProgressWindow(
"Better Notes",
@ -1068,7 +1081,7 @@ class ZoteroEvents extends AddonBase {
"Converted AsciiDoc is updated to the clipboard. You can paste them in the note."
);
} else {
this._Addon.toolkit.Tool.log(`Knowledge4Zotero: message not handled.`);
this._Addon.toolkit.Tool.log("message not handled.");
}
}
}