Skip to content
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

Refactor open preview logic #870

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is context not necessery in Side panel while it remains necessary in tabpanel?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showView doesn't use it at all, so I removed it 💁‍♂️

From what I quickly went through, SidePanelViewProvider gets context in the constructor and, being a static method, fails gracefully when it isn't provided. I didn't go deeper, as I assume this is outside the scope of this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough

SidePanelViewProvider.showView();
} else {
TabPanel.render(context, fileName, lineNumber);
TabPanel.render(context);
}
}

Expand Down Expand Up @@ -119,6 +119,15 @@ 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 +157,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
Loading