From 326101ed36b3ebc237977f450ad5d94b0f9f7a08 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Wed, 11 May 2022 20:29:40 +0800 Subject: [PATCH] fix: note parse logical bugs fix: treeview ui bug --- addon/chrome/content/bubbleMap.html | 6 +- addon/chrome/content/mindMap.html | 6 +- addon/chrome/content/treeView.html | 327 ++++++++++++++++++ addon/chrome/content/workspace.xul | 10 - .../default/Knowledge4Zotero/workspace.css | 38 +- src/events.ts | 37 +- src/knowledge.ts | 155 +++++---- src/views.ts | 294 ++-------------- 8 files changed, 487 insertions(+), 386 deletions(-) create mode 100644 addon/chrome/content/treeView.html diff --git a/addon/chrome/content/bubbleMap.html b/addon/chrome/content/bubbleMap.html index 6f727c6..1f83ddd 100644 --- a/addon/chrome/content/bubbleMap.html +++ b/addon/chrome/content/bubbleMap.html @@ -157,7 +157,7 @@ const parent = node.parent.model.id === -1? 999:node.parent.model.id; nodeDataArray.push({ key: node.model.id, - text: `${node.model.rank===7?'🔗':''}${node.model.name.slice(0,15)}${node.model.name.length>=15?'...':''}`, + text: `${node.model.rank===7?'🔗':''}${node.model.name.slice(0,50)}${node.model.name.length>=50?'...':''}`, lineIndex: node.model.lineIndex, noteLink: node.model.rank===7?node.model.link:'', }); @@ -174,9 +174,9 @@ var oldnode = adorn.adornedPart; var olddata = oldnode.data; if(olddata.noteLink){ - window.parent.postMessage({type: "jumpNote", link: olddata.noteLink}, "*"); + window.parent.postMessage({type: "jumpNote", link: olddata.noteLink, id: olddata.key}, "*"); }else{ - window.parent.postMessage({type: "jumpNode", lineIndex: olddata.lineIndex}, "*"); + window.parent.postMessage({type: "jumpNode", lineIndex: olddata.lineIndex, id: olddata.key}, "*"); } } diff --git a/addon/chrome/content/mindMap.html b/addon/chrome/content/mindMap.html index e8472c6..3da14e9 100644 --- a/addon/chrome/content/mindMap.html +++ b/addon/chrome/content/mindMap.html @@ -248,7 +248,7 @@ const parent = node.parent.model.id === -1? 999:node.parent.model.id; data.nodeDataArray.push({ key: node.model.id, - text: `${node.model.rank===7?'🔗':''}${node.model.name.slice(0,10)}${node.model.name.length>=10?'...':''}`, + text: `${node.model.rank===7?'🔗':''}${node.model.name.slice(0,50)}${node.model.name.length>=50?'...':''}`, parent: parent, lineIndex: node.model.lineIndex, noteLink: node.model.rank===7?node.model.link:'', @@ -264,9 +264,9 @@ var oldnode = adorn.adornedPart; var olddata = oldnode.data; if(olddata.noteLink){ - window.parent.postMessage({type: "jumpNote", link: olddata.noteLink}, "*"); + window.parent.postMessage({type: "jumpNote", link: olddata.noteLink, id: olddata.key}, "*"); }else{ - window.parent.postMessage({type: "jumpNode", lineIndex: olddata.lineIndex}, "*"); + window.parent.postMessage({type: "jumpNode", lineIndex: olddata.lineIndex, id: olddata.key}, "*"); } } diff --git a/addon/chrome/content/treeView.html b/addon/chrome/content/treeView.html new file mode 100644 index 0000000..44eebc2 --- /dev/null +++ b/addon/chrome/content/treeView.html @@ -0,0 +1,327 @@ + + +
+ + + + + + + diff --git a/addon/chrome/content/workspace.xul b/addon/chrome/content/workspace.xul index 021d3a0..2b7f531 100644 --- a/addon/chrome/content/workspace.xul +++ b/addon/chrome/content/workspace.xul @@ -22,7 +22,6 @@/g, //g];
- const cacheEnd = [
- /<\/ol>/g,
- /<\/ul>/g,
- /<\/li>/g,
- /<\/blockquote>/g,
- /<\/pre>/g,
- ];
- let parsedLines: string[] = [];
- let appendLater: boolean = false;
- let cacheStartLine = false;
- let cachedLines: string = "";
+ let tagStack = [];
+ let toPush = [];
+ let toRemove = 0;
+
+ let nextAppend = false;
+
+ const forceInline = ["table", "blockquote", "pre", "li"];
+ const selfInline = ["ol", "ul"];
+
+ const parsedLines = [];
for (let line of noteLines) {
- cacheStartLine = false;
- if (
- cachedLines ||
- cacheStart.filter((e) => {
- return line.search(e) !== -1;
- }).length > 0
- ) {
- appendLater = true;
- cachedLines += `${cachedLines.length > 0 ? "\n" : ""}${line}`;
- cacheStartLine = true;
- }
- if (
- cacheEnd.filter((e) => {
- return line.search(e) !== -1;
- }).length > 0
- ) {
- appendLater = false;
- // If already added to cache
- if (!cacheStartLine) {
- cachedLines += `\n${line}`;
+ for (const tag of forceInline) {
+ const startReg = `<${tag}>`;
+ const endReg = `${tag}>`;
+ const startIdx = line.search(startReg);
+ const endIdx = line.search(endReg);
+ if (startIdx !== -1 && endIdx === -1) {
+ toPush.push(tag);
+ } else if (endIdx !== -1) {
+ toRemove += 1;
}
- line = cachedLines;
- cachedLines = "";
}
- if (!appendLater) {
+
+ if (tagStack.filter((e) => forceInline.indexOf(e) !== -1).length === 0) {
+ let nextLoop = false;
+ for (const tag of selfInline) {
+ const startReg = new RegExp(`<${tag}>`);
+ const endReg = new RegExp(`${tag}>`);
+ const startIdx = line.search(startReg);
+ const endIdx = line.search(endReg);
+ if (startIdx !== -1 && endIdx === -1) {
+ nextAppend = true;
+ nextLoop = true;
+ parsedLines.push(line);
+ break;
+ }
+ if (endIdx !== -1) {
+ parsedLines[parsedLines.length - 1] += `\n${line}`;
+ nextLoop = true;
+ break;
+ }
+ }
+ if (nextLoop) {
+ continue;
+ }
+ }
+
+ if (tagStack.length === 0 && !nextAppend) {
parsedLines.push(line);
+ } else {
+ parsedLines[parsedLines.length - 1] += `\n${line}`;
+ nextAppend = false;
+ }
+
+ if (toPush.length > 0) {
+ tagStack = tagStack.concat(toPush);
+ toPush = [];
+ }
+ while (toRemove > 0) {
+ tagStack.pop();
+ toRemove -= 1;
}
}
return parsedLines;
@@ -255,11 +272,20 @@ class Knowledge extends AddonBase {
return;
}
let noteLines = this.getLinesInNote(note);
- if (newLine) {
- noteLines.splice(lineIndex, 0, "
", text);
- } else {
- noteLines.splice(lineIndex, 0, text);
+ Zotero.debug(
+ `insert to ${lineIndex}, it used to be ${noteLines[lineIndex]}`
+ );
+ // Force links not to appear in one line
+ if (lineIndex > 0 && noteLines[lineIndex - 1].search(/ \n${text}`;
}
+ if (
+ lineIndex < noteLines.length &&
+ noteLines[lineIndex].search(/ `;
+ }
+ noteLines.splice(lineIndex, 0, text);
this.setLinesToNote(note, noteLines);
}
@@ -272,8 +298,8 @@ class Knowledge extends AddonBase {
if (lineIndex < 0) {
lineIndex =
this.currentLine >= 0
- ? this.currentLine + 2
- : this.getLinesInNote(note).length + 1;
+ ? this.currentLine
+ : this.getLinesInNote(note).length;
}
// let parentNode = this.getLineParentInNote(note, lineIndex);
// if (!parentNode) {
@@ -466,8 +492,7 @@ class Knowledge extends AddonBase {
endIndex: -1,
});
let id = 0;
- let currentNode = root;
- let lastNode = undefined;
+ let lastNode = root;
let headerStartReg = new RegExp("");
let headerStopReg = new RegExp(" ");
for (let i in noteLines) {
@@ -488,23 +513,35 @@ class Knowledge extends AddonBase {
link = link.slice(0, link.search(/"/g));
}
name = this.parseLineText(lineElement);
- while (currentNode.model.rank >= currentRank) {
- currentNode = currentNode.parent;
+
+ // Find parent node
+ let parentNode = lastNode;
+ while (parentNode.model.rank >= currentRank) {
+ parentNode = parentNode.parent;
}
- if (lastNode) {
- lastNode.model.endIndex = i;
- }
- lastNode = tree.parse({
+
+ const currentNode = tree.parse({
id: id++,
rank: currentRank,
- // @ts-ignore
name: name,
- lineIndex: i,
+ lineIndex: parseInt(i),
endIndex: noteLines.length,
link: link,
});
- currentNode.addChild(lastNode);
- currentNode = lastNode;
+ parentNode.addChild(currentNode);
+ const currentIndex = parentNode.children.indexOf(currentNode);
+ if (currentIndex > 0) {
+ const previousNode = parentNode.children[currentIndex - 1];
+ // Traverse the previous node tree and set the end index
+ previousNode.walk((node) => {
+ if (node.model.endIndex > parseInt(i)) {
+ node.model.endIndex = parseInt(i);
+ }
+ return true;
+ });
+ previousNode.model.endIndex = parseInt(i);
+ }
+ lastNode = currentNode;
}
}
return root;
diff --git a/src/views.ts b/src/views.ts
index f7b33af..309406e 100644
--- a/src/views.ts
+++ b/src/views.ts
@@ -3,7 +3,6 @@ import { AddonBase, EditorMessage, OutlineType } from "./base";
class AddonViews extends AddonBase {
progressWindowIcon: object;
editorIcon: object;
- $: any;
currentOutline: OutlineType;
_initIframe: any;
@@ -23,7 +22,6 @@ class AddonViews extends AddonBase {
export: ``,
close: ``,
openWorkspaceCollectionView: ``,
- note: ``,
};
this.currentOutline = OutlineType.treeView;
this._initIframe = Zotero.Promise.defer();
@@ -490,18 +488,22 @@ class AddonViews extends AddonBase {
new EditorMessage("openUserGuide", {})
);
});
+ _window.addEventListener("resize", (e) => this.resizeOutline(_window));
}
async messageHandler(e) {
const _window = this._Addon.knowledge.getWorkspaceWindow();
+ Zotero.debug(`Knowledge4Zotero: view message ${e.data.type}`);
+ console.log(`Knowledge4Zotero: view message ${e.data.type}`);
if (e.data.type === "ready") {
this._initIframe.resolve();
} else if (e.data.type === "getMindMapData") {
this.updateMindMap();
} else if (e.data.type === "jumpNode") {
- this.scrollToLine(
- await this._Addon.knowledge.getWorkspaceEditorInstance(),
- e.data.lineIndex
+ this._Addon.events.onEditorEvent(
+ new EditorMessage("jumpNode", {
+ params: e.data,
+ })
);
} else if (e.data.type === "jumpNote") {
Zotero.debug(e.data);
@@ -510,6 +512,12 @@ class AddonViews extends AddonBase {
params: await this._Addon.knowledge.getNoteFromLink(e.data.link),
})
);
+ } else if (e.data.type === "moveNode") {
+ this._Addon.events.onEditorEvent(
+ new EditorMessage("moveNode", {
+ params: e.data,
+ })
+ );
}
}
@@ -522,36 +530,23 @@ class AddonViews extends AddonBase {
}
const _window = this._Addon.knowledge.getWorkspaceWindow();
const mindmap = _window.document.getElementById("mindmap-container");
- const outline = _window.document.getElementById("outline-container");
- this.currentOutline = newType;
- if (this.currentOutline === OutlineType.treeView) {
- _window.document.getElementById("mindmapIframe").remove();
- mindmap.setAttribute("hidden", "hidden");
- outline.removeAttribute("hidden");
- } else if (this.currentOutline === OutlineType.mindMap) {
- const iframe = _window.document.createElement("iframe");
- iframe.setAttribute("id", "mindmapIframe");
- iframe.setAttribute(
- "src",
- "chrome://Knowledge4Zotero/content/mindMap.html"
- );
- outline.setAttribute("hidden", "hidden");
- mindmap.removeAttribute("hidden");
- mindmap.append(iframe);
- this.setTreeViewSize();
- } else if (this.currentOutline === OutlineType.bubbleMap) {
- _window.document.getElementById("mindmapIframe").remove();
- const iframe = _window.document.createElement("iframe");
- iframe.setAttribute("id", "mindmapIframe");
- iframe.setAttribute(
- "src",
- "chrome://Knowledge4Zotero/content/bubbleMap.html"
- );
- outline.setAttribute("hidden", "hidden");
- mindmap.removeAttribute("hidden");
- mindmap.append(iframe);
- this.setTreeViewSize();
+
+ const oldIframe = _window.document.getElementById("mindmapIframe");
+ if (oldIframe) {
+ oldIframe.remove();
}
+ this.currentOutline = newType;
+ const srcList = [
+ "",
+ "chrome://Knowledge4Zotero/content/treeView.html",
+ "chrome://Knowledge4Zotero/content/mindMap.html",
+ "chrome://Knowledge4Zotero/content/bubbleMap.html",
+ ];
+ const iframe = _window.document.createElement("iframe");
+ iframe.setAttribute("id", "mindmapIframe");
+ iframe.setAttribute("src", srcList[this.currentOutline]);
+ mindmap.append(iframe);
+ this.resizeOutline(_window);
this.buildOutline(this._Addon.knowledge.getWorkspaceNote());
}
@@ -571,236 +566,23 @@ class AddonViews extends AddonBase {
);
}
- /*
- * Outline Code Start
- */
-
+ // TODO: change this
async buildOutline(note: ZoteroItem) {
Zotero.debug(this.currentOutline);
- if (this.currentOutline === OutlineType.treeView) {
- this._Addon.knowledge.currentNodeID = -1;
- let treeList = this._Addon.knowledge.getNoteTreeAsList(note, true, false);
- const treeData = [];
- treeList.map((node: TreeModel.Node