Skip to content

Commit

Permalink
update: open workspace in tab
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyu committed Jun 8, 2022
1 parent ef19338 commit 62af980
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
4 changes: 3 additions & 1 deletion addon/chrome/content/workspace.xul
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
</keyset>
<command id="cmd_new" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'createWorkspace'});" />
<command id="cmd_open" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'selectMainKnowledge'});" />
<command id="cmd_openWindow" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'openWorkspaceInWindow'});" />
<command id="cmd_export" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'export', content: {editorInstance: {_item: false}}});" />
<command id="cmd_close" oncommand="window.close();" />
<command id="cmd_close" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'closeWorkspace'});" />
<!-- <command id="cmd_insertNotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'insertNotes'});" /> -->
<command id="cmd_editTemplate" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'editTemplate'});" />
<command id="cmd_addheading" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'addHeading'});" />
Expand All @@ -58,6 +59,7 @@
<menupopup id="menu_FilePopup">
<menuitem id="menu_new" label="&zotero.__addonRef__.workspace.menu.new;" key="key_new" accesskey="N" command="cmd_new" />
<menuitem id="menu_open" label="&zotero.__addonRef__.workspace.menu.open;" key="key_open" accesskey="O" command="cmd_open" />
<menuitem id="menu_openWindow" label="&zotero.__addonRef__.workspace.menu.openWindow;" command="cmd_openWindow" />
<menuitem id="menu_export" label="&zotero.__addonRef__.workspace.menu.export;" key="key_export" accesskey="E" command="cmd_export" />
<menuitem id="menu_close" label="&closeCmd.label;" key="key_close" accesskey="&closeCmd.accesskey;" command="cmd_close" />
</menupopup>
Expand Down
1 change: 1 addition & 0 deletions addon/chrome/locale/en-US/overlay.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<!ENTITY zotero.__addonRef__.workspace.menu.new "Create New Main Note">
<!ENTITY zotero.__addonRef__.workspace.menu.open "Open Main Note...">
<!ENTITY zotero.__addonRef__.workspace.menu.openWindow "Open in New Window">
<!ENTITY zotero.__addonRef__.workspace.menu.export "Export Main Note...">
<!ENTITY zotero.__addonRef__.workspace.menu.insertNotes "Insert Notes...">
<!ENTITY zotero.__addonRef__.workspace.menu.insertTextTemplate "Insert Template">
Expand Down
1 change: 1 addition & 0 deletions addon/chrome/locale/zh-CN/overlay.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<!ENTITY zotero.__addonRef__.workspace.menu.new "创建新主笔记">
<!ENTITY zotero.__addonRef__.workspace.menu.open "打开主笔记...">
<!ENTITY zotero.__addonRef__.workspace.menu.openWindow "在新窗口中打开">
<!ENTITY zotero.__addonRef__.workspace.menu.export "导出主笔记...">
<!ENTITY zotero.__addonRef__.workspace.menu.insertNotes "插入多条笔记...">
<!ENTITY zotero.__addonRef__.workspace.menu.insertTextTemplate "插入模板">
Expand Down
10 changes: 10 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ class AddonEvents extends AddonBase {
message.content = {}
*/
await this._Addon.knowledge.openWorkspaceWindow();
} else if (message.type === "openWorkspaceInWindow") {
/*
message.content = {}
*/
await this._Addon.knowledge.openWorkspaceWindow("window", true);
} else if (message.type === "closeWorkspace") {
/*
message.content = {}
*/
this._Addon.knowledge.closeWorkspaceWindow();
} else if (message.type === "createWorkspace") {
/*
message.content = {}
Expand Down
78 changes: 75 additions & 3 deletions src/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Knowledge extends AddonBase {
currentLine: number;
currentNodeID: number;
workspaceWindow: Window;
workspaceTabId: string;
_exportNote: ZoteroItem;
_exportPath: string;
constructor(parent: Knowledge4Zotero) {
Expand All @@ -29,23 +30,91 @@ class Knowledge extends AddonBase {
return false;
}

async openWorkspaceWindow() {
async openWorkspaceWindow(
type: "window" | "tab" = "tab",
reopen: boolean = false
) {
if (this.getWorkspaceWindow()) {
(this.getWorkspaceWindow() as Window).focus();
} else {
if (!reopen) {
if (this.workspaceTabId) {
Zotero_Tabs.select(this.workspaceTabId);
} else {
(this.getWorkspaceWindow() as Window).focus();
}
return;
} else {
this.closeWorkspaceWindow();
}
}
if (type === "window") {
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;
this.workspaceTabId = "";
await this.waitWorkspaceReady();
this.setWorkspaceNote("main");
this.currentLine = -1;
this._Addon.views.initKnowledgeWindow(win);
this._Addon.views.switchView(OutlineType.treeView);
this._Addon.views.updateOutline();
} else {
this._Addon.views._initIframe = Zotero.Promise.defer();
// Avoid sidebar show up
Zotero_Tabs.jump(0);
let { id, container } = Zotero_Tabs.add({
type: "library",
title: "Better Notes Workspace",
index: 1,
data: {
_item: this.getWorkspaceNote(),
},
select: true,
});
this.workspaceTabId = id;
const _iframe = window.document.createElement("browser");
_iframe.setAttribute("class", "reader");
_iframe.setAttribute("flex", "1");
_iframe.setAttribute("type", "content");
_iframe.setAttribute(
"src",
"chrome://Knowledge4Zotero/content/workspace.xul"
);
container.appendChild(_iframe);
// @ts-ignore
window.addEventListener("DOMContentLoaded", async (event) => {
if (
_iframe &&
// @ts-ignore
_iframe.contentWindow &&
// @ts-ignore
_iframe.contentWindow.document === event.target
) {
// @ts-ignore
this.workspaceWindow = _iframe.contentWindow;
await this.waitWorkspaceReady();

// Need reinit
this.setWorkspaceNote("main");
this.currentLine = -1;
this._Addon.views.initKnowledgeWindow(this.workspaceWindow);
this._Addon.views.switchView(OutlineType.treeView);
this._Addon.views.updateOutline();
}
});
}
}

closeWorkspaceWindow() {
if (this.getWorkspaceWindow()) {
if (this.workspaceTabId) {
Zotero_Tabs.close(this.workspaceTabId);
} else {
(this.getWorkspaceWindow() as Window).close();
}
}
}

Expand Down Expand Up @@ -106,6 +175,9 @@ class Knowledge extends AddonBase {
noteEditor.viewMode = "library";
noteEditor.parent = null;
noteEditor.item = note;
if (this.workspaceTabId) {
noteEditor.initEditor();
}

await noteEditor._initPromise;
let t = 0;
Expand Down
11 changes: 11 additions & 0 deletions typing/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ declare class Annotation {
}

declare const Zotero_Tabs: {
_getTab(tabId: string);
jump(workspaceTabId: Number);
close(tabId: string);
select(tabId: string);
add(arg0: {
type: string;
title: any;
index: any;
data: object;
select: boolean;
});
_tabs: Array<any>;
selectedID: string;
};
Expand Down

0 comments on commit 62af980

Please sign in to comment.