Skip to content

Commit

Permalink
chore: refactore open preview logic
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekstosio committed Dec 20, 2024
1 parent 494c31e commit 6bc676f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
23 changes: 20 additions & 3 deletions packages/vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ export async function activate(context: ExtensionContext) {

commands.executeCommand("setContext", "RNIDE.sidePanelIsClosed", false);

async function showIDEPanel(fileName?: string, lineNumber?: number) {
async function showIDEPanel() {
await commands.executeCommand("setContext", "RNIDE.sidePanelIsClosed", false);

const panelLocation = workspace
.getConfiguration("RadonIDE")
.get<PanelLocation>("panelLocation");

if (panelLocation !== "tab") {
SidePanelViewProvider.showView(context, fileName, lineNumber);
SidePanelViewProvider.showView();
} else {
TabPanel.render(context, fileName, lineNumber);
TabPanel.render(context);
}
}

Expand Down Expand Up @@ -119,6 +119,20 @@ export async function activate(context: ExtensionContext) {
Project.currentProject?.showStorybookStory(componentTitle, storyName);
}

async function showInlinePreview(fileName: string, lineNumber: number) {
commands.executeCommand("RNIDE.openPanel");
if (Project.currentProject) {
Project.currentProject.startPreview(
`preview:/${fileName}:${lineNumber}`
);
} else {
window.showWarningMessage(
"Wait for app to load before lunching preview. ",
"Dismiss"
);
}
}

context.subscriptions.push(
window.registerWebviewViewProvider(
SidePanelViewProvider.viewType,
Expand Down Expand Up @@ -148,6 +162,9 @@ export async function activate(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand("RNIDE.showStorybookStory", showStorybookStory)
);
context.subscriptions.push(
commands.registerCommand("RNIDE.showInlinePreview", showInlinePreview)
);

async function closeAuxiliaryBar(registeredCommandDisposable: Disposable) {
registeredCommandDisposable.dispose(); // must dispose to avoid endless loops
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class SidePanelViewProvider implements WebviewViewProvider, Disposable {
);
}

public static showView(context: ExtensionContext, fileName?: string, lineNumber?: number) {
public static showView() {
if (SidePanelViewProvider.currentProvider) {
commands.executeCommand(`${SidePanelViewProvider.viewType}.focus`);
if (workspace.getConfiguration("RadonIDE").get("panelLocation") === "secondary-side-panel") {
Expand All @@ -47,12 +47,6 @@ export class SidePanelViewProvider implements WebviewViewProvider, Disposable {
Logger.error("SidepanelViewProvider does not exist.");
return;
}

if (fileName !== undefined && lineNumber !== undefined) {
SidePanelViewProvider.currentProvider.webviewController.project.startPreview(
`preview:/${fileName}:${lineNumber}`
);
}
}

//called when a view first becomes visible
Expand Down
8 changes: 1 addition & 7 deletions packages/vscode-extension/src/panels/Tabpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class TabPanel implements Disposable {
});
}

public static render(context: ExtensionContext, fileName?: string, lineNumber?: number) {
public static render(context: ExtensionContext) {
if (TabPanel.currentPanel) {
// If the webview panel already exists reveal it
TabPanel.currentPanel._panel.reveal();
Expand Down Expand Up @@ -85,12 +85,6 @@ export class TabPanel implements Disposable {

commands.executeCommand("workbench.action.lockEditorGroup");
}

if (fileName !== undefined && lineNumber !== undefined) {
TabPanel.currentPanel.webviewController.project.startPreview(
`preview:/${fileName}:${lineNumber}`
);
}
}

public dispose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class PreviewCodeLensProvider implements CodeLensProvider {
const previewCallRange = this.createRange(document, match.index);
const command: Command = {
title: "Open preview",
command: "RNIDE.showPanel",
command: "RNIDE.showInlinePreview",
arguments: [
document.fileName,
jsxOpeningTagLine0Based + 1 /* RNIDE expects 1-based line number */,
Expand Down

0 comments on commit 6bc676f

Please sign in to comment.