add: outline drag&drop

This commit is contained in:
xiangyu 2022-05-02 01:57:30 +08:00
parent 258a076740
commit 71404137ec
3 changed files with 67 additions and 9 deletions

View File

@ -1,8 +1,22 @@
import { AddonBase, EditorMessage } from "./base";
class AddonEvents extends AddonBase {
notifierCallback: object;
constructor(parent: Knowledge4Zotero) {
super(parent);
this.notifierCallback = {
notify: async (
event: string,
type: string,
ids: Array<string>,
extraData: object
) => {
if (event == "modify") {
Zotero.debug("Knowledge4Zotero: main knowledge midified.");
this._Addon.views.buildOutline();
}
},
};
}
public async onInit() {
@ -10,6 +24,19 @@ class AddonEvents extends AddonBase {
await Zotero.uiReadyPromise;
this._Addon.views.addOpenWorkspaceButton();
this.addEditorInstanceListener();
// Register the callback in Zotero as an item observer
let notifierID = Zotero.Notifier.registerObserver(this.notifierCallback, [
"item",
]);
// Unregister callback when the window closes (important to avoid a memory leak)
window.addEventListener(
"unload",
function (e) {
Zotero.Notifier.unregisterObserver(notifierID);
},
false
);
this.resetState();
}
@ -211,6 +238,34 @@ class AddonEvents extends AddonBase {
}
}
}
} else if (message.type === "moveOutlineTitle") {
/*
message.content = {
params: {
fromID, toID, type: "before" | "after"
}
}
*/
let tree = this._Addon.knowledge.getNoteTree();
let fromNode = this._Addon.knowledge.getNoteTreeNodeById(
undefined,
message.content.params.fromID,
tree
);
let toNode = this._Addon.knowledge.getNoteTreeNodeById(
undefined,
message.content.params.toID,
tree
);
Zotero.debug(fromNode.model);
Zotero.debug(toNode.model);
this._Addon.knowledge.moveHeaderLineInNote(
undefined,
fromNode,
toNode,
message.content.params.type
);
this._Addon.views.buildOutline();
} else if (message.type === "closePreview") {
/*
message.content = {

View File

@ -4,6 +4,7 @@ const TreeModel = require("./treemodel");
class Knowledge extends AddonBase {
currentLine: number;
currentVersion: number;
workspaceWindow: Window;
constructor(parent: Knowledge4Zotero) {
super(parent);
@ -35,7 +36,7 @@ class Knowledge extends AddonBase {
this.workspaceWindow = win;
await this.waitWorkspaceReady();
this.setWorkspaceNote("main");
this._Addon.views.buildOutline(this.getWorkspaceNote());
this._Addon.views.buildOutline();
}
}
@ -117,7 +118,7 @@ class Knowledge extends AddonBase {
let containerIndex = noteText.search(/data-schema-version="8">/g);
if (containerIndex != -1) {
noteText = noteText.substring(
containerIndex + '<div data-schema-version="8">'.length,
containerIndex + 'data-schema-version="8">'.length,
noteText.length - "</div>".length
);
}
@ -296,7 +297,7 @@ class Knowledge extends AddonBase {
this.setLinesToNote(note, newLines);
}
getNoteTree(note: ZoteroItem): TreeModel.Node<object> {
getNoteTree(note: ZoteroItem = undefined): TreeModel.Node<object> {
// See http://jnuno.com/tree-model-js
note = note || this.getWorkspaceNote();
if (!note) {
@ -444,6 +445,8 @@ class Knowledge extends AddonBase {
if (convertNoteLinks) {
let item: ZoteroItem = new Zotero.Item("note");
item.setNote(note.getNote());
item.saveTx();
let noteLines = this.getLinesInNote(note);
let newLines = [];
for (let i in noteLines) {
@ -472,8 +475,7 @@ class Knowledge extends AddonBase {
linkIndex = noteLines[i].search(/zotero:\/\/note\//g);
}
}
item.setNote(`<div data-schema-version="8">${newLines.join("\n")}</div>`);
item.saveTx();
this.setLinesToNote(item, newLines);
exporter.items = [item];
await exporter.save();
await Zotero.Items.erase(item.id);

View File

@ -296,14 +296,15 @@ class AddonViews extends AddonBase {
toNode === null || isDropInsideItem
? toItems.length
: this.findIndex(toItems, toNode.itemData.id);
Zotero.debug(fromNode.itemData);
Zotero.debug(toItems[toIndex]);
this._Addon.events.onEditorEvent(
new EditorMessage("moveOutlineTitle", {
params: {
fromID: fromNode.itemData.id,
toID: toNode ? toNode.itemData.id : -1,
fromID: parseInt(fromNode.itemData.id),
toID: toNode
? parseInt(toNode.itemData.id)
: toItems[toItems.length - 1].itemData.id,
type: toNode ? "before" : "after",
},
})
);