UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

70 lines (69 loc) 1.99 kB
import type { Promisable } from '../types.js'; export interface AdminModeCfg { /** * Function (predicate) to detect if needed keys are pressed. * * @example * predicate: e => e.ctrlKey && e.key === 'L' * * @default * Detects Ctrl+Shift+L */ predicate?: (e: KeyboardEvent) => boolean; /** * Called when RedDot is clicked. Implies that AdminMode is enabled. */ onRedDotClick?: () => any; /** * Called when AdminMode was changed. */ onChange?: (adminMode: boolean) => any; /** * Called BEFORE entering AdminMode. * Serves as a predicate that can cancel entering AdminMode if false is returned. * Return true to allow. * Function is awaited before proceeding. */ beforeEnter?: () => Promisable<boolean>; /** * Called BEFORE exiting AdminMode. * Serves as a predicate that can cancel exiting AdminMode if false is returned. * Return true to allow. * Function is awaited before proceeding. */ beforeExit?: () => Promisable<boolean>; /** * @default true * If true - it will "persist" the adminMode state in LocalStorage */ persistToLocalStorage?: boolean; /** * The key for LocalStorage persistence. * * @default '__adminMode__' */ localStorageKey?: string; } /** * @experimental * * Allows to listen for AdminMode keypress combination (Ctrl+Shift+L by default) to toggle AdminMode, * indicated by RedDot DOM element. * * todo: help with Authentication */ export declare class AdminService { constructor(cfg?: AdminModeCfg); cfg: Required<AdminModeCfg>; adminMode: boolean; private listening; /** * Start listening to keyboard events to toggle AdminMode when detected. */ startListening(): void; stopListening(): void; private keydownListener; toggleRedDot(): Promise<void>; private toggleRedDotVisibility; private getRedDotElement; }