Skip to content

Fix/webview multi open #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
prevent multiple open versions
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jul 24, 2020
commit caaa39d19d2ca56b2a8d9f8d443097531a02c49e
28 changes: 8 additions & 20 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import logger from './services/logger'

export const COMMANDS = {
START: 'coderoad.start',
OPEN_WEBVIEW: 'coderoad.open_webview',
CONFIG_TEST_RUNNER: 'coderoad.config_test_runner',
RUN_TEST: 'coderoad.run_test',
SET_CURRENT_POSITION: 'coderoad.set_current_position',
Expand All @@ -29,27 +28,16 @@ export const createCommands = ({ extensionPath, workspaceState }: CreateCommandP
return {
// initialize
[COMMANDS.START]: async () => {
let webviewState: 'INITIALIZING' | 'RESTARTING'
if (!webview) {
webviewState = 'INITIALIZING'
} else if (webview.loaded) {
// already loaded
vscode.window.showInformationMessage('CodeRoad already open')
return
console.log('start')
if (webview && webview.state.loaded) {
webview.createOrShow()
} else {
webviewState = 'RESTARTING'
// activate machine
webview = createWebView({
extensionPath,
workspaceState,
})
}

// activate machine
webview = createWebView({
extensionPath,
workspaceState,
})
},
// open React webview
[COMMANDS.OPEN_WEBVIEW]: () => {
// setup 1x1 horizontal layout
webview.createOrShow()
},
[COMMANDS.CONFIG_TEST_RUNNER]: async (data: TT.Tutorial) => {
const testRunnerConfig = data.config.testRunner
Expand Down
36 changes: 22 additions & 14 deletions src/services/webview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ interface ReactWebViewProps {
workspaceState: vscode.Memento
}

let state = { loaded: false }

const createReactWebView = ({ extensionPath, workspaceState }: ReactWebViewProps) => {
let loaded = false
// TODO add disposables
const disposables: vscode.Disposable[] = []

Expand All @@ -27,15 +28,23 @@ const createReactWebView = ({ extensionPath, workspaceState }: ReactWebViewProps
// allows scripts to load external resources (eg. markdown images, fonts)
enableCommandUris: true,
}
loaded = true
state.loaded = true
return vscode.window.createWebviewPanel(viewType, title, vscode.ViewColumn.Two, config)
}

let panel: vscode.WebviewPanel = createWebViewPanel()

// Listen for when the panel is disposed
// This happens when the user closes the panel or when the panel is closed programmatically
panel.onDidDispose(panel.dispose, null, disposables)
panel.onDidDispose(
() => {
console.log('dispose panel')
panel.dispose()
state.loaded = false
},
null,
disposables,
)

const channel = new Channel({
workspaceState,
Expand All @@ -49,17 +58,18 @@ const createReactWebView = ({ extensionPath, workspaceState }: ReactWebViewProps

panel.webview.onDidReceiveMessage(receive, null, disposables)

// panel.onDidDispose(() => {
// // Clean up our resources
// loaded = false
// panel.dispose()
// Promise.all(disposables.map((x) => x.dispose()))
// })

const rootPath = path.join(extensionPath, 'build')
render(panel, rootPath)

return {
loaded,
dispose() {
// Clean up our resources
loaded = false
panel.dispose()
Promise.all(disposables.map((x) => x.dispose()))
},
state,
createOrShow() {
vscode.commands.executeCommand('vscode.setEditorLayout', {
orientation: 0,
Expand All @@ -69,10 +79,8 @@ const createReactWebView = ({ extensionPath, workspaceState }: ReactWebViewProps
// Otherwise, create a new panel.

if (panel && panel.webview) {
if (!loaded) {
panel.reveal(vscode.ViewColumn.Two)
loaded = true
}
vscode.window.showInformationMessage('CodeRoad already open')
panel.reveal(vscode.ViewColumn.Two)
} else {
panel = createWebViewPanel()
}
Expand Down