|
|
@ -7,19 +7,37 @@ import { Console } from "../common/OutputChannel"; |
|
|
|
import { MySQLTreeDataProvider } from "../provider/treeDataProvider"; |
|
|
|
|
|
|
|
export class ViewOption { |
|
|
|
public viewPath?: string; |
|
|
|
public viewTitle?: string; |
|
|
|
public splitResultView: boolean = false; |
|
|
|
public path?: string; |
|
|
|
public title?: string; |
|
|
|
public splitView: boolean = false; |
|
|
|
/** |
|
|
|
* keep single page by viewType |
|
|
|
*/ |
|
|
|
public singlePage?: boolean; |
|
|
|
/** |
|
|
|
* kill exists panel |
|
|
|
*/ |
|
|
|
public killHidden?: boolean; |
|
|
|
/** |
|
|
|
* receive webview send message |
|
|
|
*/ |
|
|
|
public receiveListener?: (message: any) => {}; |
|
|
|
public receiveListener?: (viewPanel: WebviewPanel, message: any) => void; |
|
|
|
/** |
|
|
|
* callback when init success. |
|
|
|
*/ |
|
|
|
public initListener?: (viewPanel: WebviewPanel) => void; |
|
|
|
} |
|
|
|
|
|
|
|
public disposeListener?: (message: any) => {}; |
|
|
|
interface ViewState { |
|
|
|
instance: WebviewPanel; |
|
|
|
creating: boolean; |
|
|
|
initListener: (viewPanel: WebviewPanel) => void; |
|
|
|
receiveListener: (viewPanel: WebviewPanel, message: any) => void |
|
|
|
} |
|
|
|
|
|
|
|
export class SqlViewManager { |
|
|
|
export class ViewManager { |
|
|
|
|
|
|
|
private static viewStatu: { [key: string]: ViewState } = {}; |
|
|
|
private static webviewPath: string; |
|
|
|
public static initExtesnsionPath(extensionPath: string) { |
|
|
|
this.webviewPath = extensionPath + "/resources/webview" |
|
|
@ -28,11 +46,10 @@ export class SqlViewManager { |
|
|
|
public static showConnectPage(provider: MySQLTreeDataProvider) { |
|
|
|
|
|
|
|
this.createWebviewPanel({ |
|
|
|
viewPath: "pages/connect/connect", |
|
|
|
viewTitle: "connect", |
|
|
|
splitResultView: false, |
|
|
|
}).then((webviewPanel) => { |
|
|
|
webviewPanel.webview.onDidReceiveMessage((params) => { |
|
|
|
path: "pages/connect/connect", |
|
|
|
title: "connect", |
|
|
|
splitView: false, |
|
|
|
receiveListener: (webviewPanel, params) => { |
|
|
|
if (params.type === 'CONNECT_TO_SQL_SERVER') { |
|
|
|
const { connectionOption } = params |
|
|
|
if (connectionOption.usingSSH) { |
|
|
@ -50,16 +67,34 @@ export class SqlViewManager { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
public static createWebviewPanel(viewOption: ViewOption): Promise<WebviewPanel> { |
|
|
|
|
|
|
|
const columnType = viewOption.splitResultView ? vscode.ViewColumn.Two : vscode.ViewColumn.One; |
|
|
|
|
|
|
|
const currentStatus = this.viewStatu[viewOption.title] |
|
|
|
if (!currentStatus) { this.viewStatu[viewOption.title] = { creating: true } as ViewState } |
|
|
|
|
|
|
|
if (typeof (viewOption.singlePage) == 'undefined') { viewOption.singlePage = true } |
|
|
|
if (typeof (viewOption.killHidden) == 'undefined') { viewOption.killHidden = true } |
|
|
|
|
|
|
|
if (viewOption.singlePage && currentStatus) { |
|
|
|
if (viewOption.killHidden && currentStatus.instance.visible == false) { |
|
|
|
currentStatus.instance.dispose() |
|
|
|
} else { |
|
|
|
if (currentStatus.creating) { |
|
|
|
currentStatus.initListener = viewOption.initListener |
|
|
|
} else if (viewOption.initListener) { |
|
|
|
viewOption.initListener(currentStatus.instance) |
|
|
|
} |
|
|
|
if (viewOption.receiveListener) { currentStatus.receiveListener = viewOption.receiveListener } |
|
|
|
return Promise.resolve(currentStatus.instance); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
const targetPath = `${this.webviewPath}/${viewOption.viewPath}.html`; |
|
|
|
const targetPath = `${this.webviewPath}/${viewOption.path}.html`; |
|
|
|
fs.readFile(targetPath, 'utf8', async (err, data) => { |
|
|
|
if (err) { |
|
|
|
Console.log(err); |
|
|
@ -67,15 +102,35 @@ export class SqlViewManager { |
|
|
|
return; |
|
|
|
} |
|
|
|
const webviewPanel = vscode.window.createWebviewPanel( |
|
|
|
"mysql.sql.result", |
|
|
|
viewOption.viewTitle, |
|
|
|
{ viewColumn: columnType, preserveFocus: true }, |
|
|
|
viewOption.title, |
|
|
|
viewOption.title, |
|
|
|
{ |
|
|
|
viewColumn: viewOption.splitView ? vscode.ViewColumn.Two : vscode.ViewColumn.One, |
|
|
|
preserveFocus: true |
|
|
|
}, |
|
|
|
{ enableScripts: true, retainContextWhenHidden: true }, |
|
|
|
); |
|
|
|
webviewPanel.webview.html = this.buildInclude(this.buildPath(data), path.resolve(targetPath, "..")); |
|
|
|
webviewPanel.webview.onDidReceiveMessage(viewOption.receiveListener); |
|
|
|
webviewPanel.onDidDispose(viewOption.disposeListener); |
|
|
|
|
|
|
|
this.viewStatu[viewOption.title] = { |
|
|
|
creating: true, |
|
|
|
instance: webviewPanel, |
|
|
|
initListener: viewOption.initListener, |
|
|
|
receiveListener: viewOption.receiveListener |
|
|
|
} |
|
|
|
webviewPanel.onDidDispose(() => { |
|
|
|
this.viewStatu[viewOption.title] = null |
|
|
|
}) |
|
|
|
const newStatus = this.viewStatu[viewOption.title] |
|
|
|
webviewPanel.webview.onDidReceiveMessage((message) => { |
|
|
|
if (message.type == 'init') { |
|
|
|
newStatus.creating = false |
|
|
|
if (newStatus.initListener) { |
|
|
|
newStatus.initListener(webviewPanel) |
|
|
|
} |
|
|
|
} else if (newStatus.receiveListener) { |
|
|
|
newStatus.receiveListener(webviewPanel, message) |
|
|
|
} |
|
|
|
}) |
|
|
|
resolve(webviewPanel); |
|
|
|
}); |
|
|
|
|