Skip to content

Commit 3cea9d9

Browse files
Use history API to navigate from autocomplete
1 parent 7901fcf commit 3cea9d9

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

src/editor/codemirror/CodeMirror.tsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { createUri } from "../../language-server/client";
2121
import { useLanguageServerClient } from "../../language-server/language-server-hooks";
2222
import { Logging } from "../../logging/logging";
2323
import { useLogging } from "../../logging/logging-hooks";
24-
import { useRouterState } from "../../router-hooks";
2524
import { useSessionSettings } from "../../settings/session-settings";
2625
import {
2726
CodeStructureOption,
@@ -238,27 +237,6 @@ const CodeMirror = ({
238237
}
239238
}, [location]);
240239

241-
const [routerState, setRouterState] = useRouterState();
242-
useEffect(() => {
243-
const listener = (event: Event) => {
244-
const { id, tab } = (event as CustomEvent).detail;
245-
setRouterState(
246-
{
247-
tab,
248-
slug: { id },
249-
},
250-
"documentation-from-code"
251-
);
252-
const view = viewRef.current!;
253-
// Put the focus back in the text editor so the docs are immediately useful.
254-
view.focus();
255-
};
256-
document.addEventListener("cm/openDocs", listener);
257-
return () => {
258-
document.removeEventListener("cm/openDocs", listener);
259-
};
260-
}, [routerState, setRouterState]);
261-
262240
return (
263241
<section
264242
data-testid="editor"

src/editor/codemirror/language-server/documentation.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IntlShape } from "react-intl";
99
import { MarkupContent } from "vscode-languageserver-types";
1010
import { moduleAndApiFromId } from "../../../documentation/api/apidocs-util";
1111
import { ApiReferenceMap } from "../../../documentation/mapping/content";
12+
import { pushUrlState, RouterState, TabName } from "../../../router-hooks";
1213
import { splitDocString } from "./docstrings";
1314
import "./documentation.css";
1415

@@ -140,14 +141,7 @@ export const wrapWithDocumentationButton = (
140141
refAnchor.textContent = intl.formatMessage({ id: "help" });
141142
refAnchor.onclick = (e) => {
142143
e.preventDefault();
143-
document.dispatchEvent(
144-
new CustomEvent("cm/openDocs", {
145-
detail: {
146-
tab: "reference",
147-
id: referenceLink,
148-
},
149-
})
150-
);
144+
setRouterState("reference", id);
151145
};
152146
actionsContainer.appendChild(refAnchor);
153147
}
@@ -159,15 +153,7 @@ export const wrapWithDocumentationButton = (
159153
apiAnchor.textContent = intl.formatMessage({ id: "api-tab" });
160154
apiAnchor.onclick = (e) => {
161155
e.preventDefault();
162-
// Could we instead interact with the history API here?
163-
document.dispatchEvent(
164-
new CustomEvent("cm/openDocs", {
165-
detail: {
166-
tab: "api",
167-
id,
168-
},
169-
})
170-
);
156+
setRouterState("api", id);
171157
};
172158
if (referenceLink) {
173159
const verticalDivider = document.createElement("hr");
@@ -198,3 +184,11 @@ export const getLinkToReference = (
198184
}
199185
return referenceLink;
200186
};
187+
188+
const setRouterState = (tab: TabName, id: string): void => {
189+
const newRouterState: RouterState = {
190+
tab,
191+
slug: { id },
192+
};
193+
pushUrlState(newRouterState);
194+
};

src/router-hooks.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ export const RouterProvider = ({ children }: { children: ReactNode }) => {
117117
message: toUrl(newState),
118118
});
119119
}
120-
const url = toUrl(newState);
121-
window.history.pushState(newState, "", url);
122-
120+
pushUrlState(newState);
123121
setState(newState);
124122
},
125123
[logging, setState]
@@ -132,6 +130,11 @@ export const RouterProvider = ({ children }: { children: ReactNode }) => {
132130
);
133131
};
134132

133+
export const pushUrlState = (newState: RouterState) => {
134+
const url = toUrl(newState);
135+
window.history.pushState(newState, "", url);
136+
};
137+
135138
/**
136139
* Access the slug for a particular tab.
137140
*

0 commit comments

Comments
 (0)