Skip to content

Commit

Permalink
finish refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hweinstock committed Nov 8, 2024
1 parent b5801e2 commit 6566d24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
34 changes: 20 additions & 14 deletions packages/core/src/applicationcomposer/composerWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,43 @@

import { join } from 'path'
import * as nls from 'vscode-nls'
const localize = nls.loadMessageBundle()
import * as path from 'path'
import * as vscode from 'vscode'
import { handleMessage } from './handleMessage'
import { FileWatchInfo } from './types'

const localize = nls.loadMessageBundle()

export class ApplicationComposer {
public readonly documentUri: vscode.Uri
public webviewPanel: vscode.WebviewPanel
public webviewPanel: vscode.WebviewPanel = {} as vscode.WebviewPanel
protected readonly disposables: vscode.Disposable[] = []
protected isPanelDisposed = false
private readonly onVisualizationDisposeEmitter = new vscode.EventEmitter<void>()
public workSpacePath: string
public defaultTemplatePath: string
public defaultTemplateName: string
// fileWatches is used to monitor template file changes and achieve bi-direction sync
public fileWatches: Record<string, FileWatchInfo>
private getWebviewContent: () => string

public constructor(
private constructor(
textDocument: vscode.TextDocument,
context: vscode.ExtensionContext,
getWebviewContent: () => string
private getWebviewContent: () => Promise<string>,
public fileWatches: Record<string, FileWatchInfo> = {} // fileWatches is used to monitor template file changes and achieve bi-direction sync
) {
this.getWebviewContent = getWebviewContent
this.documentUri = textDocument.uri
this.webviewPanel = this.setupWebviewPanel(textDocument, context)
this.workSpacePath = path.dirname(textDocument.uri.fsPath)
this.defaultTemplatePath = textDocument.uri.fsPath
this.defaultTemplateName = path.basename(this.defaultTemplatePath)
this.fileWatches = {}
}

public static async create(
textDocument: vscode.TextDocument,
context: vscode.ExtensionContext,
getWebviewContent: () => Promise<string>
) {
const obj = new ApplicationComposer(textDocument, getWebviewContent)
obj.webviewPanel = await obj.setupWebviewPanel(textDocument, context)
return obj
}

public get onVisualizationDisposeEvent(): vscode.Event<void> {
Expand All @@ -56,25 +62,25 @@ export class ApplicationComposer {
if (!this.isPanelDisposed) {
this.webviewPanel.dispose()
const document = await vscode.workspace.openTextDocument(this.documentUri)
this.webviewPanel = this.setupWebviewPanel(document, context)
this.webviewPanel = await this.setupWebviewPanel(document, context)
}
}

protected getText(textDocument: vscode.TextDocument): string {
return textDocument.getText()
}

private setupWebviewPanel(
private async setupWebviewPanel(
textDocument: vscode.TextDocument,
context: vscode.ExtensionContext
): vscode.WebviewPanel {
): Promise<vscode.WebviewPanel> {
const documentUri = textDocument.uri

// Create and show panel
const panel = this.createVisualizationWebviewPanel(documentUri, context)

// Set the initial html for the webpage
panel.webview.html = this.getWebviewContent()
panel.webview.html = await this.getWebviewContent()

// Handle messages from the webview
this.disposables.push(
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/applicationcomposer/webviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ApplicationComposerManager {
}
}

private getWebviewContent = () => {
private getWebviewContent = async () => {
if (!this.webviewHtml) {
void this.fetchWebviewHtml()
return ''
Expand Down Expand Up @@ -78,7 +78,11 @@ export class ApplicationComposerManager {

// Existing visualization does not exist, construct new visualization
try {
const newVisualization = new ApplicationComposer(document, this.extensionContext, this.getWebviewContent)
const newVisualization = await ApplicationComposer.create(
document,
this.extensionContext,
this.getWebviewContent
)
this.handleNewVisualization(document.uri.fsPath, newVisualization)

if (vscode.version === '1.91.0') {
Expand All @@ -101,7 +105,11 @@ export class ApplicationComposerManager {
const document = await vscode.workspace.openTextDocument({
language: 'yaml',
})
const newVisualization = new ApplicationComposer(document, this.extensionContext, this.getWebviewContent)
const newVisualization = await ApplicationComposer.create(
document,
this.extensionContext,
this.getWebviewContent
)
this.handleNewVisualization(document.uri.fsPath, newVisualization)

return newVisualization.getPanel()
Expand Down

0 comments on commit 6566d24

Please sign in to comment.