Skip to content

Commit

Permalink
fix(ui): clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonwellsjo committed Oct 16, 2023
1 parent 4cfe851 commit ffad101
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/admin/adminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export interface AdminModeCfg {
predicate?: (e: KeyboardEvent) => boolean

/**
* Allows touch predicate:
* lower left corner, lower right corner, lower left corner, lower right corner, lower left corner
* Enable touch sequence toggling
* (lower left corner, lower right corner, lower left corner, lower right corner, lower left corner)
*
* @default false
*/
allowTouchPredicate?: boolean
allowTouchSequence?: boolean

/**
* Called when RedDot is clicked. Implies that AdminMode is enabled.
Expand Down Expand Up @@ -75,7 +75,7 @@ export class AdminService {
constructor(cfg?: AdminModeCfg) {
this.cfg = {
predicate: e => e.ctrlKey && e.key === 'L',
allowTouchPredicate: false,
allowTouchSequence: false,
persistToLocalStorage: true,
localStorageKey: '__adminMode__',
onRedDotClick: NOOP,
Expand All @@ -93,7 +93,7 @@ export class AdminService {
private listening = false

/**
* Start listening to keyboard events (and touch events if allowTouchPredicate === true) to toggle AdminMode when detected.
* Start listening to keyboard and touch events to toggle AdminMode when detected.
*/
startListening(): void {
if (this.listening || isServerSide()) return
Expand All @@ -104,7 +104,7 @@ export class AdminService {

document.addEventListener('keydown', this.keydownListener.bind(this), { passive: true })

if (this.cfg.allowTouchPredicate) {
if (this.cfg.allowTouchSequence) {
document.addEventListener('touchstart', this.touchListener.bind(this), { passive: true })
}

Expand Down Expand Up @@ -151,7 +151,7 @@ export class AdminService {

if (this.sequenceIndex === sequence.length) {
this.sequenceIndex = 1
await this.predicateFulfilled()
await this.checkAllowToggle()
}
}

Expand All @@ -166,10 +166,10 @@ export class AdminService {
private async keydownListener(e: KeyboardEvent): Promise<void> {
// console.log(e)
if (!this.cfg.predicate(e)) return
await this.predicateFulfilled()
await this.checkAllowToggle()
}

private async predicateFulfilled(): Promise<void> {
private async checkAllowToggle(): Promise<void> {
try {
const allow = await this.cfg[this.adminMode ? 'beforeExit' : 'beforeEnter']()
if (!allow) return // no change
Expand Down

0 comments on commit ffad101

Please sign in to comment.