Skip to content

Commit 54bda44

Browse files
authored
Merge pull request #7 from CloudCannon/type-fixes
Enhance event listener to directly match the native implementation
2 parents a743f41 + d4f0358 commit 54bda44

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

src/index.d.ts

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export interface OpenCustomDataPanelOptions extends CloseCustomDataPanelOptions
1919
allowFullDataCascade?: boolean;
2020
}
2121

22+
type EventListenerParameters = Parameters<EventTarget['addEventListener']>;
23+
type EventListenerOrEventListenerObject = EventListenerParameters[1];
24+
type EventListenerOptions = EventListenerParameters[2];
25+
2226
/**
2327
* Interface defining the public JavaScript API for interacting with CloudCannon's Visual Editor.
2428
* This API provides methods for managing content, handling file operations, and controlling the editor's state.
@@ -294,8 +298,16 @@ export interface CloudCannonJavaScriptV1APIFileContent {
294298
*/
295299
set(options: any): Promise<void>;
296300

297-
addEventListener(event: 'change', callback: (event: any) => void): void;
298-
removeEventListener(event: 'change', callback: (event: any) => void): void;
301+
addEventListener(
302+
event: 'change',
303+
listener: EventListenerOrEventListenerObject | null,
304+
options?: EventListenerOptions | boolean
305+
): void;
306+
removeEventListener(
307+
event: 'change',
308+
listener: EventListenerOrEventListenerObject | null,
309+
options?: EventListenerOptions | boolean
310+
): void;
299311
}
300312

301313
export interface CloudCannonJavaScriptV1APIFileData {
@@ -395,8 +407,16 @@ export interface CloudCannonJavaScriptV1APIFileData {
395407
*/
396408
moveArrayItem(options: MoveArrayItemOptions): Promise<void>;
397409

398-
addEventListener(event: 'change', callback: (event: any) => void): void;
399-
removeEventListener(event: 'change', callback: (event: any) => void): void;
410+
addEventListener(
411+
event: 'change',
412+
listener: EventListenerOrEventListenerObject | null,
413+
options?: EventListenerOptions | boolean
414+
): void;
415+
removeEventListener(
416+
event: 'change',
417+
listener: EventListenerOrEventListenerObject | null,
418+
options?: EventListenerOptions | boolean
419+
): void;
400420
}
401421

402422
export interface CloudCannonJavaScriptV1APIFile {
@@ -474,8 +494,16 @@ export interface CloudCannonJavaScriptV1APIFile {
474494
*/
475495
releaseLock(): Promise<{ readOnly: boolean }>;
476496

477-
addEventListener(event: 'change' | 'delete' | 'create', callback: (event: any) => void): void;
478-
removeEventListener(event: 'change' | 'delete' | 'create', callback: (event: any) => void): void;
497+
addEventListener(
498+
event: 'change' | 'delete' | 'create',
499+
listener: EventListenerOrEventListenerObject | null,
500+
options?: EventListenerOptions | boolean
501+
): void;
502+
removeEventListener(
503+
event: 'change' | 'delete' | 'create',
504+
listener: EventListenerOrEventListenerObject | null,
505+
options?: EventListenerOptions | boolean
506+
): void;
479507
}
480508

481509
export interface CloudCannonJavaScriptV1APICollection {
@@ -499,8 +527,16 @@ export interface CloudCannonJavaScriptV1APICollection {
499527
// */
500528
// add(options: any): Promise<CloudCannonJavaScriptV1APIFile>;
501529

502-
addEventListener(event: 'change' | 'delete' | 'create', callback: (event: any) => void): void;
503-
removeEventListener(event: 'change' | 'delete' | 'create', callback: (event: any) => void): void;
530+
addEventListener(
531+
event: 'change' | 'delete' | 'create',
532+
listener: EventListenerOrEventListenerObject | null,
533+
options?: EventListenerOptions | boolean
534+
): void;
535+
removeEventListener(
536+
event: 'change' | 'delete' | 'create',
537+
listener: EventListenerOrEventListenerObject | null,
538+
options?: EventListenerOptions | boolean
539+
): void;
504540
}
505541

506542
export interface CloudCannonJavaScriptV1API {
@@ -548,8 +584,16 @@ export interface CloudCannonJavaScriptV1API {
548584
files(): Promise<CloudCannonJavaScriptV1APIFile[]>;
549585
collections(): Promise<CloudCannonJavaScriptV1APICollection[]>;
550586

551-
addEventListener(event: 'change' | 'delete' | 'create', callback: (event: any) => void): void;
552-
removeEventListener(event: 'change' | 'delete' | 'create', callback: (event: any) => void): void;
587+
addEventListener(
588+
event: 'change' | 'delete' | 'create',
589+
listener: EventListenerOrEventListenerObject | null,
590+
options?: EventListenerOptions | boolean
591+
): void;
592+
removeEventListener(
593+
event: 'change' | 'delete' | 'create',
594+
listener: EventListenerOrEventListenerObject | null,
595+
options?: EventListenerOptions | boolean
596+
): void;
553597
}
554598

555599
export type CloudCannonJavaScriptAPIVersions = 'v0' | 'v1';
@@ -558,11 +602,10 @@ export interface CloudCannonApiEventDetails {
558602
CloudCannonAPI?: CloudCannonJavascriptApiRouter;
559603
CloudCannon?: CloudCannonJavaScriptV0API | CloudCannonJavaScriptV1API;
560604
}
605+
561606
export interface CloudCannonEditorWindow extends Window, CloudCannonApiEventDetails {}
562607

563608
export interface CloudCannonJavascriptApiRouter {
564-
useVersion(
565-
key: CloudCannonJavaScriptAPIVersions,
566-
preventGlobalInstall?: boolean
567-
): CloudCannonJavaScriptV0API | CloudCannonJavaScriptV1API;
609+
useVersion(key: 'v0', preventGlobalInstall?: boolean): CloudCannonJavaScriptV0API;
610+
useVersion(key: 'v1', preventGlobalInstall?: boolean): CloudCannonJavaScriptV1API;
568611
}

0 commit comments

Comments
 (0)