import { AddonBase, EditorMessage, OutlineType } from "./base"; const TreeModel = require("./treemodel"); class Knowledge extends AddonBase { currentLine: number; currentNodeID: number; workspaceWindow: Window; constructor(parent: Knowledge4Zotero) { super(parent); this.currentLine = -1; this.currentNodeID = -1; } getWorkspaceNote() { return Zotero.Items.get( Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") ); } getWorkspaceWindow(): Window | boolean { if (this.workspaceWindow && !this.workspaceWindow.closed) { return this.workspaceWindow; } return false; } async openWorkspaceWindow() { if (this.getWorkspaceWindow()) { (this.getWorkspaceWindow() as Window).focus(); } else { this._Addon.views._initIframe = Zotero.Promise.defer(); let win = window.open( "chrome://Knowledge4Zotero/content/workspace.xul", "_blank", "chrome,extrachrome,menubar,resizable,scrollbars,status,width=1000,height=600" ); this.workspaceWindow = win; await this.waitWorkspaceReady(); win.addEventListener("resize", (e) => { this._Addon.views.setTreeViewSize(); }); this._Addon.views.bindTreeViewResize(); this.setWorkspaceNote("main"); this.currentLine = -1; this._Addon.views.initKnowledgeWindow(win); this._Addon.views.switchView(OutlineType.treeView); this._Addon.views.buildOutline(); // this._Addon.views.buildMindMap(); } } async waitWorkspaceReady() { let _window = this.getWorkspaceWindow() as Window; if (!_window) { return false; } let t = 0; while (_window.document.readyState !== "complete" && t < 500) { t += 1; await Zotero.Promise.delay(10); } return t < 500; } async getWorkspaceEditor(type: "main" | "preview" = "main") { let _window = this.getWorkspaceWindow() as Window; if (!_window) { return; } await this.waitWorkspaceReady(); return _window.document.getElementById(`zotero-note-editor-${type}`); } async getWorkspaceEditorInstance( type: "main" | "preview" = "main" ): Promise { let noteEditor = (await this.getWorkspaceEditor(type)) as any; let t = 0; while (!noteEditor.getCurrentInstance() && t < 500) { t += 1; await Zotero.Promise.delay(10); } return noteEditor.getCurrentInstance() as EditorInstance; } async setWorkspaceNote( type: "main" | "preview" = "main", note: ZoteroItem = undefined ) { let _window = this.getWorkspaceWindow() as Window; note = note || this.getWorkspaceNote(); if (!_window) { return; } if (type === "preview") { _window.document .getElementById("preview-splitter") .setAttribute("state", "open"); } else { // Set line to default this.currentLine = -1; } await this.waitWorkspaceReady(); let noteEditor: any = await this.getWorkspaceEditor(type); noteEditor.mode = "edit"; noteEditor.viewMode = "library"; noteEditor.parent = null; noteEditor.item = note; await noteEditor._initPromise; let t = 0; while (!noteEditor.getCurrentInstance() && t < 500) { t += 1; await Zotero.Promise.delay(10); } await this._Addon.events.onEditorEvent( new EditorMessage("enterWorkspace", { editorInstance: noteEditor.getCurrentInstance(), params: type, }) ); this._Addon.views.buildOutline(); } getLinesInNote(note: ZoteroItem): string[] { note = note || this.getWorkspaceNote(); if (!note) { return []; } let noteText: string = note.getNote(); let containerIndex = noteText.search(/data-schema-version="8">/g); if (containerIndex != -1) { noteText = noteText.substring( containerIndex + 'data-schema-version="8">'.length, noteText.length - "".length ); } let noteLines: string[] = noteText.split("\n"); const cacheStart = [/
    /g, /