Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

previewActionBus.ts

27 lines837 Bytes

1export type PreviewAction =
2 | "toggle-panel"
3 | "refresh"
4 | "back"
5 | "forward"
6 | "focus-url"
7 | "zoom-in"
8 | "zoom-out"
9 | "reset-zoom"
10 | "find";
11​
12const EVENT_NAME = "t3code:preview-action";
13​
14export function dispatchPreviewAction(action: PreviewAction): void {
15 if (typeof window === "undefined") return;
16 window.dispatchEvent(new CustomEvent<PreviewAction>(EVENT_NAME, { detail: action }));
18​
19export function subscribePreviewAction(listener: (action: PreviewAction) => void): () => void {
20 if (typeof window === "undefined") return () => {};
21 const handler = (event: Event) => {
22 const detail = (event as CustomEvent<PreviewAction>).detail;
23 if (typeof detail === "string") listener(detail);
24 };
25 window.addEventListener(EVENT_NAME, handler);
26 return () => window.removeEventListener(EVENT_NAME, handler);