Fix card-wide actions.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
Actions,
|
||||
ActionsConfig,
|
||||
@@ -13,6 +14,15 @@ import {
|
||||
import { getStreamCameraID } from '../utils/substream.js';
|
||||
import { CardActionsManagerAPI } from './types.js';
|
||||
|
||||
const interactionSchema = z.object({
|
||||
action: z.enum(['tap', 'double_tap', 'hold', 'start_tap', 'end_tap']),
|
||||
});
|
||||
export type Interaction = z.infer<typeof interactionSchema>;
|
||||
|
||||
const interactionEventSchema = z.object({
|
||||
detail: interactionSchema,
|
||||
});
|
||||
|
||||
export class ActionsManager {
|
||||
protected _api: CardActionsManagerAPI;
|
||||
|
||||
@@ -49,7 +59,14 @@ export class ActionsManager {
|
||||
/**
|
||||
* Handle an human interaction called on an element (e.g. 'tap').
|
||||
*/
|
||||
public handleInteraction(interaction: string): void {
|
||||
public handleInteractionEvent = (ev: Event): void => {
|
||||
const result = interactionEventSchema.safeParse(ev);
|
||||
if (!result.success) {
|
||||
// The event may not be a CustomEvent object, see:
|
||||
// https://github.com/custom-cards/custom-card-helpers/blob/master/src/fire-event.ts#L70
|
||||
return;
|
||||
}
|
||||
const interaction = result.data.detail.action;
|
||||
const hass = this._api.getHASSManager().getHASS();
|
||||
const config = this.getMergedActions();
|
||||
const actionConfig = getActionConfigGivenAction(interaction, config);
|
||||
@@ -70,12 +87,11 @@ export class ActionsManager {
|
||||
actionConfig,
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public handleActionEvent = (ev: Event): void => {
|
||||
if (!('detail' in ev)) {
|
||||
// The event may not actually be a CustomEvent object, but may still have a
|
||||
// detail field. See:
|
||||
// The event may not be a CustomEvent object, see:
|
||||
// https://github.com/custom-cards/custom-card-helpers/blob/master/src/fire-event.ts#L70
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -77,7 +77,11 @@ export class CardElementManager {
|
||||
this._api.getActionsManager().handleActionEvent,
|
||||
);
|
||||
this._element.addEventListener(
|
||||
'@action',
|
||||
'action',
|
||||
this._api.getActionsManager().handleInteractionEvent,
|
||||
);
|
||||
this._element.addEventListener(
|
||||
'action',
|
||||
this._api.getInteractionManager().reportInteraction,
|
||||
);
|
||||
|
||||
@@ -123,7 +127,11 @@ export class CardElementManager {
|
||||
this._api.getActionsManager().handleActionEvent,
|
||||
);
|
||||
this._element.removeEventListener(
|
||||
'@action',
|
||||
'action',
|
||||
this._api.getActionsManager().handleInteractionEvent,
|
||||
);
|
||||
this._element.removeEventListener(
|
||||
'action',
|
||||
this._api.getInteractionManager().reportInteraction,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user