Skip to content

Commit

Permalink
feat(platform-browser): expose EventManagerPlugin in the public API.
Browse files Browse the repository at this point in the history
    The exposed type of the `EVENT_MANAGER_PLUGINS` token should be in the public API.
  • Loading branch information
JeanMeche committed Aug 28, 2023
1 parent 440684d commit 2c61afe
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
9 changes: 9 additions & 0 deletions goldens/public-api/platform-browser/index.md
Expand Up @@ -92,6 +92,15 @@ export class EventManager {
static ɵprov: i0.ɵɵInjectableDeclaration<EventManager>;
}

// @public
export abstract class EventManagerPlugin {
constructor(_doc: any);
abstract addEventListener(element: HTMLElement, eventName: string, handler: Function): Function;
// (undocumented)
manager: EventManager;
abstract supports(eventName: string): boolean;
}

// @public
export const HAMMER_GESTURE_CONFIG: InjectionToken<HammerGestureConfig>;

Expand Down
17 changes: 16 additions & 1 deletion packages/platform-browser/src/dom/events/event_manager.ts
Expand Up @@ -12,7 +12,7 @@ import {Inject, Injectable, InjectionToken, NgZone, ɵRuntimeError as RuntimeErr
import {RuntimeErrorCode} from '../../errors';

/**
* The injection token for the event-manager plug-in service.
* The injection token for plugins of the `EventManager` service.
*
* @publicApi
*/
Expand Down Expand Up @@ -82,13 +82,28 @@ export class EventManager {
}
}

/**
* The plugin definition for the `EventManager` class
*
* It can be used as a base class to create custom manager plugins, i.e. you can create your own
* class that extends the `EventManagerPlugin` one.
*
* @publicApi
*/
export abstract class EventManagerPlugin {
// TODO: remove (has some usage in G3)
constructor(private _doc: any) {}

// Using non-null assertion because it's set by EventManager's constructor
manager!: EventManager;

/**
* Should return `true` for every event name that should be supported by this plugin
*/
abstract supports(eventName: string): boolean;

/**
* Implement the behaviour for the supported events
*/
abstract addEventListener(element: HTMLElement, eventName: string, handler: Function): Function;
}
Expand Up @@ -272,7 +272,7 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
* HammerJS to detect gesture events.
*
* Note that applications still need to include the HammerJS script itself. This module
* simply sets up the coordination layer between HammerJS and Angular's EventManager.
* simply sets up the coordination layer between HammerJS and Angular's `EventManager`.
*
* @publicApi
*/
Expand Down
9 changes: 1 addition & 8 deletions packages/platform-browser/src/dom/events/key_events.ts
Expand Up @@ -45,7 +45,6 @@ const MODIFIER_KEY_GETTERS: {[key: string]: (event: KeyboardEvent) => boolean} =
};

/**
* @publicApi
* A browser plug-in that provides support for handling of key events in Angular.
*/
@Injectable()
Expand Down Expand Up @@ -188,12 +187,6 @@ export class KeyEventsPlugin extends EventManagerPlugin {

/** @internal */
static _normalizeKey(keyName: string): string {
// TODO: switch to a Map if the mapping grows too much
switch (keyName) {
case 'esc':
return 'escape';
default:
return keyName;
}
return keyName === 'esc' ? 'escape' : keyName;
}
}
2 changes: 1 addition & 1 deletion packages/platform-browser/src/platform-browser.ts
Expand Up @@ -75,7 +75,7 @@ export {Title} from './browser/title';
export {disableDebugTools, enableDebugTools} from './browser/tools/tools';
export {By} from './dom/debug/by';
export {REMOVE_STYLES_ON_COMPONENT_DESTROY} from './dom/dom_renderer';
export {EVENT_MANAGER_PLUGINS, EventManager} from './dom/events/event_manager';
export {EVENT_MANAGER_PLUGINS, EventManager, EventManagerPlugin} from './dom/events/event_manager';
export {HAMMER_GESTURE_CONFIG, HAMMER_LOADER, HammerGestureConfig, HammerLoader, HammerModule} from './dom/events/hammer_gestures';
export {DomSanitizer, SafeHtml, SafeResourceUrl, SafeScript, SafeStyle, SafeUrl, SafeValue} from './security/dom_sanitization_service';
export {HydrationFeature, provideClientHydration, HydrationFeatureKind, withNoDomReuse, withNoHttpTransferCache} from './hydration';
Expand Down
11 changes: 7 additions & 4 deletions packages/platform-server/src/server_events.ts
Expand Up @@ -8,17 +8,20 @@

import {DOCUMENT, ɵgetDOM as getDOM} from '@angular/common';
import {Inject, Injectable} from '@angular/core';
import {EventManagerPlugin} from '@angular/platform-browser';

@Injectable()
export class ServerEventManagerPlugin /* extends EventManagerPlugin which is private */ {
constructor(@Inject(DOCUMENT) private doc: any) {}
export class ServerEventManagerPlugin extends EventManagerPlugin {
constructor(@Inject(DOCUMENT) private doc: any) {
super(doc);
}

// Handle all events on the server.
supports(eventName: string) {
override supports(eventName: string) {
return true;
}

addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
override addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
return getDOM().onAndCancel(element, eventName, handler);
}
}

0 comments on commit 2c61afe

Please sign in to comment.