33 lines
1.4 KiB
TypeScript
33 lines
1.4 KiB
TypeScript
const CODING_PROJECT_CREATE_REQUEST_EVENT = 'makelore:coding-project-create-request';
|
|
const CODING_PROJECT_OPEN_REQUEST_EVENT = 'makelore:coding-project-open-request';
|
|
|
|
export function requestCodingProjectCreation(): void {
|
|
if (typeof window === 'undefined') return;
|
|
window.dispatchEvent(new Event(CODING_PROJECT_CREATE_REQUEST_EVENT));
|
|
}
|
|
|
|
export function subscribeCodingProjectCreationRequest(listener: () => void): () => void {
|
|
if (typeof window === 'undefined') return () => undefined;
|
|
window.addEventListener(CODING_PROJECT_CREATE_REQUEST_EVENT, listener);
|
|
return () => window.removeEventListener(CODING_PROJECT_CREATE_REQUEST_EVENT, listener);
|
|
}
|
|
|
|
export function requestCodingProjectOpen(projectId: string): void {
|
|
if (typeof window === 'undefined') return;
|
|
window.dispatchEvent(new CustomEvent(CODING_PROJECT_OPEN_REQUEST_EVENT, {
|
|
detail: { projectId },
|
|
}));
|
|
}
|
|
|
|
export function subscribeCodingProjectOpenRequest(
|
|
listener: (projectId: string) => void,
|
|
): () => void {
|
|
if (typeof window === 'undefined') return () => undefined;
|
|
const handleRequest = (event: Event) => {
|
|
const projectId = (event as CustomEvent<{ projectId?: unknown }>).detail?.projectId;
|
|
if (typeof projectId === 'string' && projectId.trim()) listener(projectId);
|
|
};
|
|
window.addEventListener(CODING_PROJECT_OPEN_REQUEST_EVENT, handleRequest);
|
|
return () => window.removeEventListener(CODING_PROJECT_OPEN_REQUEST_EVENT, handleRequest);
|
|
}
|