fix: move heading bug

This commit is contained in:
xiangyu 2022-10-14 21:44:27 +08:00
parent e3792e9988
commit 69e24da549
5 changed files with 59 additions and 57 deletions

View File

@ -72,7 +72,7 @@
// For details, see https://gojs.net/latest/intro/buildingObjects.html
const $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram = $(
const Diagram = $(
go.Diagram,
"myDiagramDiv", // must name or refer to the DIV HTML element
{
@ -88,7 +88,7 @@
);
// dragging a node invalidates the Diagram.layout, causing a layout during the drag
myDiagram.toolManager.draggingTool.doMouseMove = function () {
Diagram.toolManager.draggingTool.doMouseMove = function () {
go.DraggingTool.prototype.doMouseMove.call(this);
if (this.isActive) {
this.diagram.layout.invalidateLayout();
@ -96,7 +96,7 @@
};
// define each Node's appearance
myDiagram.nodeTemplate = $(
Diagram.nodeTemplate = $(
go.Node,
"Vertical", // the whole node panel
// define the node's outer shape, which will surround the TextBlock
@ -126,7 +126,7 @@
// the rest of this app is the same as samples/conceptMap.html
// selected nodes show a button for adding children
myDiagram.nodeTemplate.selectionAdornmentTemplate = $(
Diagram.nodeTemplate.selectionAdornmentTemplate = $(
go.Adornment,
"Spot",
$(
@ -153,7 +153,7 @@
);
// replace the default Link template in the linkTemplateMap
myDiagram.linkTemplate = $(
Diagram.linkTemplate = $(
go.Link, // the whole link panel
$(
go.Shape, // the link shape

View File

@ -26,7 +26,7 @@
// For details, see https://gojs.net/latest/intro/buildingObjects.html
const $ = go.GraphObject.make;
myDiagram = $(go.Diagram, "myDiagramDiv", {
const Diagram = $(go.Diagram, "myDiagramDiv", {
"commandHandler.copiesTree": true,
"commandHandler.copiesParentKey": true,
"commandHandler.deletesTree": true,
@ -39,7 +39,7 @@
});
// a node consists of some text with a line shape underneath
myDiagram.nodeTemplate = $(
Diagram.nodeTemplate = $(
go.Node,
"Vertical",
{ selectionObjectName: "TEXT" },
@ -86,7 +86,7 @@
);
// selected nodes show a button for adding children
myDiagram.nodeTemplate.selectionAdornmentTemplate = $(
Diagram.nodeTemplate.selectionAdornmentTemplate = $(
go.Adornment,
"Spot",
$(
@ -113,7 +113,7 @@
);
// a link is just a Bezier-curved line of the same color as the node to which it is connected
myDiagram.linkTemplate = $(
Diagram.linkTemplate = $(
go.Link,
{
curve: go.Link.Bezier,
@ -131,9 +131,9 @@
)
);
myDiagram.addDiagramListener("SelectionMoved", (e) => {
var rootX = myDiagram.findNodeForKey(0).location.x;
myDiagram.selection.each((node) => {
Diagram.addDiagramListener("SelectionMoved", (e) => {
var rootX = Diagram.findNodeForKey(0).location.x;
Diagram.selection.each((node) => {
if (node.data.parent !== 0) return; // Only consider nodes connected to the root
var nodeX = node.location.x;
if (rootX < nodeX && node.data.dir !== "right") {

View File

@ -363,14 +363,14 @@ class NoteUtils extends AddonBase {
);
}
moveHeaderLineInNote(
async moveHeaderLineInNote(
note: Zotero.Item,
currentNode: TreeModel.Node<object>,
targetNode: TreeModel.Node<object>,
as: "child" | "before" | "after"
) {
if (!note || targetNode.getPath().indexOf(currentNode) >= 0) {
return undefined;
return;
}
let targetIndex = 0;
@ -382,7 +382,7 @@ class NoteUtils extends AddonBase {
targetIndex = targetNode.model.endIndex;
targetRank = targetNode.model.rank === 6 ? 6 : targetNode.model.rank + 1;
} else if (as === "before") {
targetIndex = targetNode.model.lineIndex;
targetIndex = targetNode.model.lineIndex - 1;
targetRank =
targetNode.model.rank === 7
? targetNode.parent.model.rank === 6
@ -401,17 +401,15 @@ class NoteUtils extends AddonBase {
let rankChange = targetRank - currentNode.model.rank;
Zotero.debug(`move to ${targetIndex}`);
let movedLines = lines.splice(
currentNode.model.lineIndex,
currentNode.model.endIndex - currentNode.model.lineIndex
currentNode.model.endIndex - currentNode.model.lineIndex + 1
);
let headerReg = /<\/?h[1-6]>/g;
let headerReg = /<\/?h[1-6]/g;
for (const i in movedLines) {
movedLines[i] = movedLines[i].replace(headerReg, (e) => {
let rank = parseInt(e.slice(-2, -1));
let rank = parseInt(e.slice(-1));
rank += rankChange;
if (rank > 6) {
rank = 6;
@ -419,13 +417,25 @@ class NoteUtils extends AddonBase {
if (rank < 1) {
rank = 1;
}
return `${e.slice(0, -2)}${rank}>`;
return `${e.slice(0, -1)}${rank}`;
});
}
// If the moved lines is before the insert index
// the slice index -= lines length.
if (currentNode.model.endIndex <= targetIndex) {
targetIndex -= movedLines.length;
}
Zotero.debug(`move to ${targetIndex}`);
let newLines = lines
.slice(0, targetIndex)
.concat(movedLines, lines.slice(targetIndex));
this.setLinesToNote(note, newLines);
.slice(0, targetIndex + 1)
.concat(movedLines, lines.slice(targetIndex + 1));
console.log("new lines", newLines);
console.log("moved", movedLines);
console.log("insert after", lines[targetIndex]);
console.log("next line", lines[targetIndex + 1]);
await this.setLinesToNote(note, newLines);
}
getNoteTree(note: Zotero.Item): TreeModel.Node<object> {
@ -502,6 +512,27 @@ class NoteUtils extends AddonBase {
}
}
async moveNode(fromID: number, toID: number, moveType: "before" | "child") {
const workspaceNote = this._Addon.WorkspaceWindow.getWorkspaceNote();
let tree = this.getNoteTree(workspaceNote);
let fromNode = this.getNoteTreeNodeById(workspaceNote, fromID, tree);
let toNode = this._Addon.NoteUtils.getNoteTreeNodeById(
workspaceNote,
toID,
tree
);
Zotero.debug(fromNode.model);
Zotero.debug(toNode.model);
Zotero.debug(moveType);
console.log(toNode.model, fromNode.model, moveType);
this.moveHeaderLineInNote(
this._Addon.WorkspaceWindow.getWorkspaceNote(),
fromNode,
toNode,
moveType
);
}
async scrollWithRefresh(lineIndex: number) {
await Zotero.Promise.delay(500);
let editorInstance =

View File

@ -179,10 +179,10 @@ class WorkspaceWindow extends AddonBase {
})
);
} else if (e.data.type === "moveNode") {
this._Addon.ZoteroEvents.onEditorEvent(
new EditorMessage("moveNode", {
params: e.data,
})
await this._Addon.NoteUtils.moveNode(
e.data.fromID,
e.data.toID,
e.data.moveType
);
}
}

View File

@ -674,35 +674,6 @@ class ZoteroEvents extends AddonBase {
// Scroll to line
message.content.params.lineIndex
);
} else if (message.type === "moveNode") {
/*
message.content = {
params: {
fromID, toID, moveType: "before" | "child"
}
}
*/
const workspaceNote = this._Addon.WorkspaceWindow.getWorkspaceNote();
let tree = this._Addon.NoteUtils.getNoteTree(workspaceNote);
let fromNode = this._Addon.NoteUtils.getNoteTreeNodeById(
workspaceNote,
message.content.params.fromID,
tree
);
let toNode = this._Addon.NoteUtils.getNoteTreeNodeById(
workspaceNote,
message.content.params.toID,
tree
);
Zotero.debug(fromNode.model);
Zotero.debug(toNode.model);
Zotero.debug(message.content.params.moveType);
this._Addon.NoteUtils.moveHeaderLineInNote(
mainNote,
fromNode,
toNode,
message.content.params.moveType
);
} else if (message.type === "closePreview") {
/*
message.content = {