Skip to content

Commit

Permalink
refactor: remove window.__previewjs_navigate()
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Nov 23, 2022
1 parent aa19822 commit b495c94
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { ResponseOf, RPCs } from "@previewjs/api";
import axios from "axios";
import { makeAutoObservable, runInAction } from "mobx";
import type { PersistedStateController } from "../../state/PersistedStateController";
import "../../window";

export class UpdateBannerState {
private checkVersionResponse: CheckVersionResponse | null = null;
Expand Down
14 changes: 7 additions & 7 deletions app-foundations/src/state/PreviewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { makeAutoObservable, observable, runInAction } from "mobx";
import { ActionLogsState } from "../components/ActionLogs";
import { ConsolePanelState } from "../components/ConsolePanel";
import { UpdateBannerState } from "../components/UpdateBanner";
import "../window";
import { decodeComponentId } from "./component-id";
import { ComponentProps } from "./ComponentProps";
import type { PersistedStateController } from "./PersistedStateController";
Expand Down Expand Up @@ -167,10 +166,6 @@ export class PreviewState {
}

async start() {
window.__previewjs_navigate = (componentId) => {
history.pushState(null, "", `/?p=${encodeURIComponent(componentId)}`);
this.onUrlChanged().catch(console.error);
};
window.addEventListener("message", this.messageListener);
window.addEventListener("popstate", this.popStateListener);
this.iframeController.start();
Expand Down Expand Up @@ -221,10 +216,15 @@ export class PreviewState {
private messageListener = (event: MessageEvent) => {
const data = event.data;
if (data && data.kind === "navigate") {
window.__previewjs_navigate(data.componentId);
this.navigate(data.componentId);
}
};

private navigate(componentId: string) {
history.pushState(null, "", `/?p=${encodeURIComponent(componentId)}`);
this.onUrlChanged().catch(console.error);
}

private popStateListener = () => {
this.onUrlChanged().catch(console.error);
};
Expand Down Expand Up @@ -270,7 +270,7 @@ export class PreviewState {
if (componentId === this.component?.componentId) {
this.setVariant(null);
} else {
window.__previewjs_navigate(componentId);
this.navigate(componentId);
}
}

Expand Down
7 changes: 0 additions & 7 deletions app-foundations/src/window.ts

This file was deleted.

6 changes: 4 additions & 2 deletions e2e-test-runner/src/helpers/app-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ export class AppController {
if (this.page.url().startsWith(previewBaseUrl)) {
// Soft refresh.
await this.page.evaluate((componentId: string) => {
// @ts-ignore
window.__previewjs_navigate(componentId);
window.postMessage({
kind: "navigate",
componentId,
});
}, componentId);
} else {
// Hard refresh.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class ProjectService(private val project: Project) : Disposable {
val currentBrowserUrl = browser.cefBrowser.url
if (currentBrowserUrl?.startsWith(previewBaseUrl) == true) {
browser.cefBrowser.executeJavaScript(
"window.__previewjs_navigate(\"${componentId}\");",
"window.postMessage({ kind: \"navigate\", componentId: \"${componentId}\" });",
previewUrl,
0
)
Expand Down

0 comments on commit b495c94

Please sign in to comment.